4 * Copyright 1996 Alexandre Julliard
15 typedef void (*destroy_object)(K32OBJ *);
17 extern void VIRTUAL_DestroyMapping( K32OBJ *obj );
19 static const destroy_object K32OBJ_Destroy[K32OBJ_NBOBJECTS] =
22 NULL, /* K32OBJ_SEMAPHORE */
23 NULL, /* K32OBJ_EVENT */
24 NULL, /* K32OBJ_MUTEX */
25 NULL, /* K32OBJ_CRITICAL_SECTION */
26 PROCESS_Destroy, /* K32OBJ_PROCESS */
27 THREAD_Destroy, /* K32OBJ_THREAD */
28 FILE_Destroy, /* K32OBJ_FILE */
29 NULL, /* K32OBJ_CHANGE */
30 NULL, /* K32OBJ_CONSOLE */
31 NULL, /* K32OBJ_SCREEN_BUFFER */
32 VIRTUAL_DestroyMapping, /* K32OBJ_MEM_MAPPED_FILE */
33 NULL, /* K32OBJ_SERIAL */
34 NULL, /* K32OBJ_DEVICE_IOCTL */
35 NULL, /* K32OBJ_PIPE */
36 NULL, /* K32OBJ_MAILSLOT */
37 NULL, /* K32OBJ_TOOLHELP_SNAPSHOT */
38 NULL /* K32OBJ_SOCKET */
49 static NAME_ENTRY *K32OBJ_FirstEntry = NULL;
52 /***********************************************************************
55 void K32OBJ_IncCount( K32OBJ *ptr )
57 /* FIXME: not atomic */
58 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
63 /***********************************************************************
66 void K32OBJ_DecCount( K32OBJ *ptr )
70 /* FIXME: not atomic */
71 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
72 assert( ptr->refcount );
73 if (--ptr->refcount) return;
75 /* Check if the object has a name entry and free it */
77 pptr = &K32OBJ_FirstEntry;
78 while (*pptr && ((*pptr)->obj != ptr)) pptr = &(*pptr)->next;
81 NAME_ENTRY *entry = *pptr;
83 HeapFree( SystemHeap, 0, entry );
88 if (K32OBJ_Destroy[ptr->type]) K32OBJ_Destroy[ptr->type]( ptr );
92 /***********************************************************************
95 * Add an object at the head of the list and increment its ref count.
97 void K32OBJ_AddHead( K32OBJ_LIST *list, K32OBJ *ptr )
99 K32OBJ_ENTRY *entry = HeapAlloc( SystemHeap, 0, sizeof(*entry) );
101 K32OBJ_IncCount( ptr );
103 if ((entry->next = list->head) != NULL) entry->next->prev = entry;
104 else list->tail = entry;
110 /***********************************************************************
113 * Add an object at the tail of the list and increment its ref count.
115 void K32OBJ_AddTail( K32OBJ_LIST *list, K32OBJ *ptr )
117 K32OBJ_ENTRY *entry = HeapAlloc( SystemHeap, 0, sizeof(*entry) );
119 K32OBJ_IncCount( ptr );
121 if ((entry->prev = list->tail) != NULL) entry->prev->next = entry;
122 else list->head = entry;
128 /***********************************************************************
131 * Remove an object from the list and decrement its ref count.
133 void K32OBJ_Remove( K32OBJ_LIST *list, K32OBJ *ptr )
135 K32OBJ_ENTRY *entry = list->head;
136 while ((entry && (entry->obj != ptr))) entry = entry->next;
138 if (entry->next) entry->next->prev = entry->prev;
139 else list->tail = entry->prev;
140 if (entry->prev) entry->prev->next = entry->next;
141 else list->head = entry->next;
142 HeapFree( SystemHeap, 0, entry );
143 K32OBJ_DecCount( ptr );
147 /***********************************************************************
150 * Remove the head of the list; its ref count is NOT decremented.
152 K32OBJ *K32OBJ_RemoveHead( K32OBJ_LIST *list )
155 K32OBJ_ENTRY *entry = list->head;
157 assert(!entry->prev);
158 if ((list->head = entry->next) != NULL) entry->next->prev = NULL;
159 else list->tail = NULL;
161 HeapFree( SystemHeap, 0, entry );
166 /***********************************************************************
169 * Add a name entry for an object. We don't check for duplicates here.
170 * FIXME: should use some sort of hashing.
172 BOOL32 K32OBJ_AddName( K32OBJ *obj, LPCSTR name )
175 UINT32 len = strlen( name );
177 if (!(entry = HeapAlloc( SystemHeap, 0, sizeof(NAME_ENTRY) + len )))
179 SetLastError( ERROR_OUTOFMEMORY );
182 entry->next = K32OBJ_FirstEntry;
184 lstrcpy32A( entry->name, name );
185 K32OBJ_FirstEntry = entry;
190 /***********************************************************************
193 * Find the object referenced by a given name.
194 * The reference count is not incremented.
196 K32OBJ *K32OBJ_FindName( LPCSTR name )
198 NAME_ENTRY *entry = K32OBJ_FirstEntry;
201 if (!name) return NULL; /* Anonymous object */
202 len = strlen( name );
205 if ((len == entry->len) && !lstrcmp32A( name, entry->name))
213 /***********************************************************************
214 * K32OBJ_FindNameType
216 * Find an object by name and check its type.
217 * The reference count is not incremented.
219 K32OBJ *K32OBJ_FindNameType( LPCSTR name, K32OBJ_TYPE type )
221 K32OBJ *obj = K32OBJ_FindName( name );
222 if (!obj) return NULL;
223 if (obj->type == type) return obj;
224 SetLastError( ERROR_DUP_NAME );