Release 980822
[wine/multimedia.git] / scheduler / k32obj.c
blobaebca61c313a5835f24bfab64cb64a4c13b63eec
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 CHANGE_Ops;
23 extern const K32OBJ_OPS MEM_MAPPED_FILE_Ops;
24 extern const K32OBJ_OPS DEVICE_Ops;
25 extern const K32OBJ_OPS CONSOLE_Ops;
27 static const K32OBJ_OPS K32OBJ_NullOps =
29 NULL, /* signaled */
30 NULL, /* satisfied */
31 NULL, /* add_wait */
32 NULL, /* remove_wait */
33 NULL, /* read */
34 NULL, /* write */
35 NULL /* destroy */
38 const K32OBJ_OPS * const K32OBJ_Ops[K32OBJ_NBOBJECTS] =
40 NULL,
41 &SEMAPHORE_Ops, /* K32OBJ_SEMAPHORE */
42 &EVENT_Ops, /* K32OBJ_EVENT */
43 &MUTEX_Ops, /* K32OBJ_MUTEX */
44 &CRITICAL_SECTION_Ops, /* K32OBJ_CRITICAL_SECTION */
45 &PROCESS_Ops, /* K32OBJ_PROCESS */
46 &THREAD_Ops, /* K32OBJ_THREAD */
47 &FILE_Ops, /* K32OBJ_FILE */
48 &CHANGE_Ops, /* K32OBJ_CHANGE */
49 &CONSOLE_Ops, /* K32OBJ_CONSOLE */
50 &K32OBJ_NullOps, /* K32OBJ_SCREEN_BUFFER */
51 &MEM_MAPPED_FILE_Ops, /* K32OBJ_MEM_MAPPED_FILE */
52 &K32OBJ_NullOps, /* K32OBJ_SERIAL */
53 &DEVICE_Ops, /* K32OBJ_DEVICE_IOCTL */
54 &K32OBJ_NullOps, /* K32OBJ_PIPE */
55 &K32OBJ_NullOps, /* K32OBJ_MAILSLOT */
56 &K32OBJ_NullOps, /* K32OBJ_TOOLHELP_SNAPSHOT */
57 &K32OBJ_NullOps /* K32OBJ_SOCKET */
60 typedef struct _NE
62 struct _NE *next;
63 K32OBJ *obj;
64 UINT32 len;
65 char name[1];
66 } NAME_ENTRY;
68 static NAME_ENTRY *K32OBJ_FirstEntry = NULL;
71 /***********************************************************************
72 * K32OBJ_IncCount
74 void K32OBJ_IncCount( K32OBJ *ptr )
76 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
77 SYSTEM_LOCK();
78 ptr->refcount++;
79 SYSTEM_UNLOCK();
80 assert( ptr->refcount > 0 ); /* No wrap-around allowed */
84 /***********************************************************************
85 * K32OBJ_DecCount
87 void K32OBJ_DecCount( K32OBJ *ptr )
89 NAME_ENTRY **pptr;
91 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
92 assert( ptr->refcount > 0 );
93 SYSTEM_LOCK();
94 if (--ptr->refcount)
96 SYSTEM_UNLOCK();
97 return;
100 /* Check if the object has a name entry and free it */
102 pptr = &K32OBJ_FirstEntry;
103 while (*pptr && ((*pptr)->obj != ptr)) pptr = &(*pptr)->next;
104 if (*pptr)
106 NAME_ENTRY *entry = *pptr;
107 *pptr = entry->next;
108 HeapFree( SystemHeap, 0, entry );
111 /* Free the object */
113 if (K32OBJ_Ops[ptr->type]->destroy) K32OBJ_Ops[ptr->type]->destroy( ptr );
114 SYSTEM_UNLOCK();
118 /***********************************************************************
119 * K32OBJ_IsValid
121 * Check if a pointer is a valid kernel object
123 BOOL32 K32OBJ_IsValid( K32OBJ *ptr, K32OBJ_TYPE type )
125 if (IsBadReadPtr32( ptr, sizeof(*ptr) )) return FALSE;
126 return (ptr->type == type);
130 /***********************************************************************
131 * K32OBJ_AddName
133 * Add a name entry for an object. We don't check for duplicates here.
134 * FIXME: should use some sort of hashing.
136 BOOL32 K32OBJ_AddName( K32OBJ *obj, LPCSTR name )
138 NAME_ENTRY *entry;
139 UINT32 len;
141 if (!name) return TRUE; /* Anonymous object */
142 len = strlen( name );
143 SYSTEM_LOCK();
144 if (!(entry = HeapAlloc( SystemHeap, 0, sizeof(NAME_ENTRY) + len )))
146 SYSTEM_UNLOCK();
147 SetLastError( ERROR_OUTOFMEMORY );
148 return FALSE;
150 entry->next = K32OBJ_FirstEntry;
151 entry->obj = obj;
152 entry->len = len;
153 lstrcpy32A( entry->name, name );
154 K32OBJ_FirstEntry = entry;
155 SYSTEM_UNLOCK();
156 return TRUE;
160 /***********************************************************************
161 * K32OBJ_Create
163 * Create a named kernel object.
164 * Returns NULL if there was an error _or_ if the object already existed.
165 * The refcount of the object must be decremented once it is initialized.
167 K32OBJ *K32OBJ_Create( K32OBJ_TYPE type, DWORD size, LPCSTR name,
168 DWORD access, SECURITY_ATTRIBUTES *sa, HANDLE32 *handle)
170 BOOL32 inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
172 /* Check if the name already exists */
174 K32OBJ *obj = K32OBJ_FindName( name );
175 if (obj)
177 if (obj->type == type)
179 SetLastError( ERROR_ALREADY_EXISTS );
180 *handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, -1 );
182 else
184 SetLastError( ERROR_DUP_NAME );
185 *handle = INVALID_HANDLE_VALUE32;
187 K32OBJ_DecCount( obj );
188 return NULL;
191 /* Create the object */
193 SYSTEM_LOCK();
194 if (!(obj = HeapAlloc( SystemHeap, 0, size )))
196 SYSTEM_UNLOCK();
197 *handle = INVALID_HANDLE_VALUE32;
198 return NULL;
200 obj->type = type;
201 obj->refcount = 1;
203 /* Add a name for it */
205 if (!K32OBJ_AddName( obj, name ))
207 /* Don't call the destroy function, as the object wasn't
208 * initialized properly */
209 HeapFree( SystemHeap, 0, obj );
210 SYSTEM_UNLOCK();
211 *handle = INVALID_HANDLE_VALUE32;
212 return NULL;
215 /* Allocate a handle */
217 *handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit, -1 );
218 SYSTEM_UNLOCK();
219 return obj;
223 /***********************************************************************
224 * K32OBJ_FindName
226 * Find the object referenced by a given name.
227 * The reference count is incremented.
229 K32OBJ *K32OBJ_FindName( LPCSTR name )
231 NAME_ENTRY *entry;
232 UINT32 len;
234 if (!name) return NULL; /* Anonymous object */
235 len = strlen( name );
236 SYSTEM_LOCK();
237 entry = K32OBJ_FirstEntry;
238 while (entry)
240 if ((len == entry->len) && !strcmp( name, entry->name))
242 K32OBJ *obj = entry->obj;
243 K32OBJ_IncCount( obj );
244 SYSTEM_UNLOCK();
245 return entry->obj;
247 entry = entry->next;
249 SYSTEM_UNLOCK();
250 return NULL;
254 /***********************************************************************
255 * K32OBJ_FindNameType
257 * Find an object by name and check its type.
258 * The reference count is incremented.
260 K32OBJ *K32OBJ_FindNameType( LPCSTR name, K32OBJ_TYPE type )
262 K32OBJ *obj = K32OBJ_FindName( name );
263 if (!obj) return NULL;
264 if (obj->type == type) return obj;
265 SetLastError( ERROR_DUP_NAME );
266 K32OBJ_DecCount( obj );
267 return NULL;