Release 970914
[wine.git] / win32 / k32obj.c
blob9eb1b8cc9861eef5c32e0223154b080c147f8fd7
1 /*
2 * KERNEL32 objects
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include "winerror.h"
9 #include "handle32.h"
10 #include "heap.h"
11 #include "file.h"
12 #include "process.h"
13 #include "thread.h"
15 typedef void (*destroy_object)(K32OBJ *);
17 extern void VIRTUAL_DestroyMapping( K32OBJ *obj );
19 static const destroy_object K32OBJ_Destroy[K32OBJ_NBOBJECTS] =
21 NULL,
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 */
41 typedef struct _NE
43 struct _NE *next;
44 K32OBJ *obj;
45 UINT32 len;
46 char name[1];
47 } NAME_ENTRY;
49 static NAME_ENTRY *K32OBJ_FirstEntry = NULL;
52 /***********************************************************************
53 * K32OBJ_IncCount
55 void K32OBJ_IncCount( K32OBJ *ptr )
57 /* FIXME: not atomic */
58 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
59 ptr->refcount++;
63 /***********************************************************************
64 * K32OBJ_DecCount
66 void K32OBJ_DecCount( K32OBJ *ptr )
68 NAME_ENTRY **pptr;
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;
79 if (*pptr)
81 NAME_ENTRY *entry = *pptr;
82 *pptr = entry->next;
83 HeapFree( SystemHeap, 0, entry );
86 /* Free the object */
88 if (K32OBJ_Destroy[ptr->type]) K32OBJ_Destroy[ptr->type]( ptr );
92 /***********************************************************************
93 * K32OBJ_AddHead
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) );
100 assert(entry);
101 K32OBJ_IncCount( ptr );
102 entry->obj = ptr;
103 if ((entry->next = list->head) != NULL) entry->next->prev = entry;
104 else list->tail = entry;
105 entry->prev = NULL;
106 list->head = entry;
110 /***********************************************************************
111 * K32OBJ_AddTail
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) );
118 assert(entry);
119 K32OBJ_IncCount( ptr );
120 entry->obj = ptr;
121 if ((entry->prev = list->tail) != NULL) entry->prev->next = entry;
122 else list->head = entry;
123 entry->next = NULL;
124 list->tail = entry;
128 /***********************************************************************
129 * K32OBJ_Remove
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;
137 assert(entry);
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 /***********************************************************************
148 * K32OBJ_RemoveHead
150 * Remove the head of the list; its ref count is NOT decremented.
152 K32OBJ *K32OBJ_RemoveHead( K32OBJ_LIST *list )
154 K32OBJ *ptr;
155 K32OBJ_ENTRY *entry = list->head;
156 assert(entry);
157 assert(!entry->prev);
158 if ((list->head = entry->next) != NULL) entry->next->prev = NULL;
159 else list->tail = NULL;
160 ptr = entry->obj;
161 HeapFree( SystemHeap, 0, entry );
162 return ptr;
166 /***********************************************************************
167 * K32OBJ_AddName
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 )
174 NAME_ENTRY *entry;
175 UINT32 len = strlen( name );
177 if (!(entry = HeapAlloc( SystemHeap, 0, sizeof(NAME_ENTRY) + len )))
179 SetLastError( ERROR_OUTOFMEMORY );
180 return FALSE;
182 entry->next = K32OBJ_FirstEntry;
183 entry->obj = obj;
184 lstrcpy32A( entry->name, name );
185 K32OBJ_FirstEntry = entry;
186 return TRUE;
190 /***********************************************************************
191 * K32OBJ_FindName
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;
199 UINT32 len;
201 if (!name) return NULL; /* Anonymous object */
202 len = strlen( name );
203 while (entry)
205 if ((len == entry->len) && !lstrcmp32A( name, entry->name))
206 return entry->obj;
207 entry = entry->next;
209 return NULL;
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 );
225 return NULL;