Release 970914
[wine] / win32 / k32obj.c
1 /*
2  * KERNEL32 objects
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
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"
14
15 typedef void (*destroy_object)(K32OBJ *);
16
17 extern void VIRTUAL_DestroyMapping( K32OBJ *obj );
18
19 static const destroy_object K32OBJ_Destroy[K32OBJ_NBOBJECTS] =
20 {
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 */
39 };
40
41 typedef struct _NE
42 {
43     struct _NE *next;
44     K32OBJ     *obj;
45     UINT32      len;
46     char        name[1];
47 } NAME_ENTRY;
48
49 static NAME_ENTRY *K32OBJ_FirstEntry = NULL;
50
51
52 /***********************************************************************
53  *           K32OBJ_IncCount
54  */
55 void K32OBJ_IncCount( K32OBJ *ptr )
56 {
57     /* FIXME: not atomic */
58     assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
59     ptr->refcount++;
60 }
61
62
63 /***********************************************************************
64  *           K32OBJ_DecCount
65  */
66 void K32OBJ_DecCount( K32OBJ *ptr )
67 {
68     NAME_ENTRY **pptr;
69
70     /* FIXME: not atomic */
71     assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
72     assert( ptr->refcount );
73     if (--ptr->refcount) return;
74
75     /* Check if the object has a name entry and free it */
76
77     pptr = &K32OBJ_FirstEntry;
78     while (*pptr && ((*pptr)->obj != ptr)) pptr = &(*pptr)->next;
79     if (*pptr)
80     {
81         NAME_ENTRY *entry = *pptr;
82         *pptr = entry->next;
83         HeapFree( SystemHeap, 0, entry );
84     }
85
86     /* Free the object */
87
88     if (K32OBJ_Destroy[ptr->type]) K32OBJ_Destroy[ptr->type]( ptr );
89 }
90
91
92 /***********************************************************************
93  *           K32OBJ_AddHead
94  *
95  * Add an object at the head of the list and increment its ref count.
96  */
97 void K32OBJ_AddHead( K32OBJ_LIST *list, K32OBJ *ptr )
98 {
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;
107 }
108
109
110 /***********************************************************************
111  *           K32OBJ_AddTail
112  *
113  * Add an object at the tail of the list and increment its ref count.
114  */
115 void K32OBJ_AddTail( K32OBJ_LIST *list, K32OBJ *ptr )
116 {
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;
125 }
126
127
128 /***********************************************************************
129  *           K32OBJ_Remove
130  *
131  * Remove an object from the list and decrement its ref count.
132  */
133 void K32OBJ_Remove( K32OBJ_LIST *list, K32OBJ *ptr )
134 {
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 );
144 }
145
146
147 /***********************************************************************
148  *           K32OBJ_RemoveHead
149  *
150  * Remove the head of the list; its ref count is NOT decremented.
151  */
152 K32OBJ *K32OBJ_RemoveHead( K32OBJ_LIST *list )
153 {
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;
163 }
164
165
166 /***********************************************************************
167  *           K32OBJ_AddName
168  *
169  * Add a name entry for an object. We don't check for duplicates here.
170  * FIXME: should use some sort of hashing.
171  */
172 BOOL32 K32OBJ_AddName( K32OBJ *obj, LPCSTR name )
173 {
174     NAME_ENTRY *entry;
175     UINT32 len = strlen( name );
176
177     if (!(entry = HeapAlloc( SystemHeap, 0, sizeof(NAME_ENTRY) + len )))
178     {
179         SetLastError( ERROR_OUTOFMEMORY );
180         return FALSE;
181     }
182     entry->next = K32OBJ_FirstEntry;
183     entry->obj = obj;
184     lstrcpy32A( entry->name, name );
185     K32OBJ_FirstEntry = entry;
186     return TRUE;
187 }
188
189
190 /***********************************************************************
191  *           K32OBJ_FindName
192  *
193  * Find the object referenced by a given name.
194  * The reference count is not incremented.
195  */
196 K32OBJ *K32OBJ_FindName( LPCSTR name )
197 {
198     NAME_ENTRY *entry = K32OBJ_FirstEntry;
199     UINT32 len;
200
201     if (!name) return NULL;  /* Anonymous object */
202     len = strlen( name );
203     while (entry)
204     {
205         if ((len == entry->len) && !lstrcmp32A( name, entry->name))
206             return entry->obj;
207         entry = entry->next;
208     }
209     return NULL;
210 }
211
212
213 /***********************************************************************
214  *           K32OBJ_FindNameType
215  *
216  * Find an object by name and check its type.
217  * The reference count is not incremented.
218  */
219 K32OBJ *K32OBJ_FindNameType( LPCSTR name, K32OBJ_TYPE type )
220 {
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;
226 }