Release 980329
[wine/multimedia.git] / scheduler / k32obj.c
blob30717fbbafd0c7d9a48de1418d0a40f327dd59f4
1 /*
2 * KERNEL32 objects
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include "winerror.h"
9 #include "k32obj.h"
10 #include "heap.h"
11 #include "process.h"
14 /* The declarations are here to avoid including a lot of unnecessary files */
15 extern const K32OBJ_OPS SEMAPHORE_Ops;
16 extern const K32OBJ_OPS EVENT_Ops;
17 extern const K32OBJ_OPS MUTEX_Ops;
18 extern const K32OBJ_OPS CRITICAL_SECTION_Ops;
19 extern const K32OBJ_OPS PROCESS_Ops;
20 extern const K32OBJ_OPS THREAD_Ops;
21 extern const K32OBJ_OPS FILE_Ops;
22 extern const K32OBJ_OPS MEM_MAPPED_FILE_Ops;
23 extern const K32OBJ_OPS CONSOLE_Ops;
25 static const K32OBJ_OPS K32OBJ_NullOps =
27 NULL, /* signaled */
28 NULL, /* satisfied */
29 NULL, /* add_wait */
30 NULL, /* remove_wait */
31 NULL, /* read */
32 NULL, /* write */
33 NULL /* destroy */
36 const K32OBJ_OPS * const K32OBJ_Ops[K32OBJ_NBOBJECTS] =
38 NULL,
39 &SEMAPHORE_Ops, /* K32OBJ_SEMAPHORE */
40 &EVENT_Ops, /* K32OBJ_EVENT */
41 &MUTEX_Ops, /* K32OBJ_MUTEX */
42 &CRITICAL_SECTION_Ops, /* K32OBJ_CRITICAL_SECTION */
43 &PROCESS_Ops, /* K32OBJ_PROCESS */
44 &THREAD_Ops, /* K32OBJ_THREAD */
45 &FILE_Ops, /* K32OBJ_FILE */
46 &K32OBJ_NullOps, /* K32OBJ_CHANGE */
47 &CONSOLE_Ops, /* K32OBJ_CONSOLE */
48 &K32OBJ_NullOps, /* K32OBJ_SCREEN_BUFFER */
49 &MEM_MAPPED_FILE_Ops, /* K32OBJ_MEM_MAPPED_FILE */
50 &K32OBJ_NullOps, /* K32OBJ_SERIAL */
51 &K32OBJ_NullOps, /* K32OBJ_DEVICE_IOCTL */
52 &K32OBJ_NullOps, /* K32OBJ_PIPE */
53 &K32OBJ_NullOps, /* K32OBJ_MAILSLOT */
54 &K32OBJ_NullOps, /* K32OBJ_TOOLHELP_SNAPSHOT */
55 &K32OBJ_NullOps /* K32OBJ_SOCKET */
58 typedef struct _NE
60 struct _NE *next;
61 K32OBJ *obj;
62 UINT32 len;
63 char name[1];
64 } NAME_ENTRY;
66 static NAME_ENTRY *K32OBJ_FirstEntry = NULL;
69 /***********************************************************************
70 * K32OBJ_IncCount
72 void K32OBJ_IncCount( K32OBJ *ptr )
74 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
75 SYSTEM_LOCK();
76 ptr->refcount++;
77 SYSTEM_UNLOCK();
78 assert( ptr->refcount > 0 ); /* No wrap-around allowed */
82 /***********************************************************************
83 * K32OBJ_DecCount
85 void K32OBJ_DecCount( K32OBJ *ptr )
87 NAME_ENTRY **pptr;
89 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
90 assert( ptr->refcount > 0 );
91 SYSTEM_LOCK();
92 if (--ptr->refcount)
94 SYSTEM_UNLOCK();
95 return;
98 /* Check if the object has a name entry and free it */
100 pptr = &K32OBJ_FirstEntry;
101 while (*pptr && ((*pptr)->obj != ptr)) pptr = &(*pptr)->next;
102 if (*pptr)
104 NAME_ENTRY *entry = *pptr;
105 *pptr = entry->next;
106 HeapFree( SystemHeap, 0, entry );
109 /* Free the object */
111 if (K32OBJ_Ops[ptr->type]->destroy) K32OBJ_Ops[ptr->type]->destroy( ptr );
112 SYSTEM_UNLOCK();
116 /***********************************************************************
117 * K32OBJ_IsValid
119 * Check if a pointer is a valid kernel object
121 BOOL32 K32OBJ_IsValid( K32OBJ *ptr, K32OBJ_TYPE type )
123 if (IsBadReadPtr32( ptr, sizeof(*ptr) )) return FALSE;
124 return (ptr->type == type);
128 /***********************************************************************
129 * K32OBJ_AddName
131 * Add a name entry for an object. We don't check for duplicates here.
132 * FIXME: should use some sort of hashing.
134 BOOL32 K32OBJ_AddName( K32OBJ *obj, LPCSTR name )
136 NAME_ENTRY *entry;
137 UINT32 len;
139 if (!name) return TRUE; /* Anonymous object */
140 len = strlen( name );
141 SYSTEM_LOCK();
142 if (!(entry = HeapAlloc( SystemHeap, 0, sizeof(NAME_ENTRY) + len )))
144 SYSTEM_UNLOCK();
145 SetLastError( ERROR_OUTOFMEMORY );
146 return FALSE;
148 entry->next = K32OBJ_FirstEntry;
149 entry->obj = obj;
150 lstrcpy32A( entry->name, name );
151 K32OBJ_FirstEntry = entry;
152 SYSTEM_UNLOCK();
153 return TRUE;
157 /***********************************************************************
158 * K32OBJ_Create
160 * Create a named kernel object.
161 * Returns NULL if there was an error _or_ if the object already existed.
162 * The refcount of the object must be decremented once it is initialized.
164 K32OBJ *K32OBJ_Create( K32OBJ_TYPE type, DWORD size, LPCSTR name,
165 DWORD access, HANDLE32 *handle )
167 /* Check if the name already exists */
169 K32OBJ *obj = K32OBJ_FindName( name );
170 if (obj)
172 if (obj->type == type)
174 SetLastError( ERROR_ALREADY_EXISTS );
175 *handle = HANDLE_Alloc( obj, access, FALSE );
177 else
179 SetLastError( ERROR_DUP_NAME );
180 *handle = INVALID_HANDLE_VALUE32;
182 K32OBJ_DecCount( obj );
183 return NULL;
186 /* Create the object */
188 SYSTEM_LOCK();
189 if (!(obj = HeapAlloc( SystemHeap, 0, size )))
191 SYSTEM_UNLOCK();
192 *handle = INVALID_HANDLE_VALUE32;
193 return NULL;
195 obj->type = type;
196 obj->refcount = 1;
198 /* Add a name for it */
200 if (!K32OBJ_AddName( obj, name ))
202 /* Don't call the destroy function, as the object wasn't
203 * initialized properly */
204 HeapFree( SystemHeap, 0, obj );
205 SYSTEM_UNLOCK();
206 *handle = INVALID_HANDLE_VALUE32;
207 return NULL;
210 /* Allocate a handle */
212 *handle = HANDLE_Alloc( obj, access, FALSE );
213 SYSTEM_UNLOCK();
214 return obj;
218 /***********************************************************************
219 * K32OBJ_FindName
221 * Find the object referenced by a given name.
222 * The reference count is incremented.
224 K32OBJ *K32OBJ_FindName( LPCSTR name )
226 NAME_ENTRY *entry;
227 UINT32 len;
229 if (!name) return NULL; /* Anonymous object */
230 len = strlen( name );
231 SYSTEM_LOCK();
232 entry = K32OBJ_FirstEntry;
233 while (entry)
235 if ((len == entry->len) && !lstrcmp32A( name, entry->name))
237 K32OBJ *obj = entry->obj;
238 K32OBJ_IncCount( obj );
239 SYSTEM_UNLOCK();
240 return entry->obj;
242 entry = entry->next;
244 SYSTEM_UNLOCK();
245 return NULL;
249 /***********************************************************************
250 * K32OBJ_FindNameType
252 * Find an object by name and check its type.
253 * The reference count is incremented.
255 K32OBJ *K32OBJ_FindNameType( LPCSTR name, K32OBJ_TYPE type )
257 K32OBJ *obj = K32OBJ_FindName( name );
258 if (!obj) return NULL;
259 if (obj->type == type) return obj;
260 SetLastError( ERROR_DUP_NAME );
261 K32OBJ_DecCount( obj );
262 return NULL;