+
+struct nitni_global_ref_list_t *nitni_global_ref_list;
+void nitni_global_ref_list_init() {
+ nitni_global_ref_list = (struct nitni_global_ref_list_t*)malloc(sizeof(struct nitni_global_ref_list_t));
+ nitni_global_ref_list->head = NULL;
+ nitni_global_ref_list->tail = NULL;
+}
+
+void nitni_global_ref_add( struct nitni_ref *ref ) {
+ if ( nitni_global_ref_list->head == NULL ) {
+ nitni_global_ref_list->head = ref;
+ ref->prev = NULL;
+ } else {
+ nitni_global_ref_list->tail->next = ref;
+ ref->prev = nitni_global_ref_list->tail;
+ }
+ nitni_global_ref_list->tail = ref;
+
+ ref->next = NULL;
+}
+
+void nitni_global_ref_remove( struct nitni_ref *ref ) {
+ if ( ref->prev == NULL ) {
+ nitni_global_ref_list->head = ref->next;
+ } else {
+ ref->prev->next = ref->next;
+ }
+
+ if ( ref->next == NULL ) {
+ nitni_global_ref_list->tail = ref->prev;
+ } else {
+ ref->next->prev = ref->prev;
+ }
+}
+
+extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
+ if ( ref->count == 0 ) /* not registered */
+ {
+ /* add to list */
+ nitni_global_ref_add( ref );
+ }
+
+ ref->count ++;
+}
+
+extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
+ if ( ref->count == 1 ) /* was last reference */
+ {
+ /* remove from list */
+ nitni_global_ref_remove( ref );
+ }
+
+ ref->count --;
+}