4 * Copyright 1996 Alexandre Julliard
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 =
30 NULL, /* remove_wait */
36 const K32OBJ_OPS * const K32OBJ_Ops[K32OBJ_NBOBJECTS] =
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 */
66 static NAME_ENTRY *K32OBJ_FirstEntry = NULL;
69 /***********************************************************************
72 void K32OBJ_IncCount( K32OBJ *ptr )
74 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
78 assert( ptr->refcount > 0 ); /* No wrap-around allowed */
82 /***********************************************************************
85 void K32OBJ_DecCount( K32OBJ *ptr )
89 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
90 assert( ptr->refcount > 0 );
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;
104 NAME_ENTRY *entry = *pptr;
106 HeapFree( SystemHeap, 0, entry );
109 /* Free the object */
111 if (K32OBJ_Ops[ptr->type]->destroy) K32OBJ_Ops[ptr->type]->destroy( ptr );
116 /***********************************************************************
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 /***********************************************************************
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 )
139 if (!name) return TRUE; /* Anonymous object */
140 len = strlen( name );
142 if (!(entry = HeapAlloc( SystemHeap, 0, sizeof(NAME_ENTRY) + len )))
145 SetLastError( ERROR_OUTOFMEMORY );
148 entry->next = K32OBJ_FirstEntry;
151 lstrcpy32A( entry->name, name );
152 K32OBJ_FirstEntry = entry;
158 /***********************************************************************
161 * Create a named kernel object.
162 * Returns NULL if there was an error _or_ if the object already existed.
163 * The refcount of the object must be decremented once it is initialized.
165 K32OBJ *K32OBJ_Create( K32OBJ_TYPE type, DWORD size, LPCSTR name,
166 DWORD access, SECURITY_ATTRIBUTES *sa, HANDLE32 *handle)
168 BOOL32 inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
170 /* Check if the name already exists */
172 K32OBJ *obj = K32OBJ_FindName( name );
175 if (obj->type == type)
177 SetLastError( ERROR_ALREADY_EXISTS );
178 *handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit );
182 SetLastError( ERROR_DUP_NAME );
183 *handle = INVALID_HANDLE_VALUE32;
185 K32OBJ_DecCount( obj );
189 /* Create the object */
192 if (!(obj = HeapAlloc( SystemHeap, 0, size )))
195 *handle = INVALID_HANDLE_VALUE32;
201 /* Add a name for it */
203 if (!K32OBJ_AddName( obj, name ))
205 /* Don't call the destroy function, as the object wasn't
206 * initialized properly */
207 HeapFree( SystemHeap, 0, obj );
209 *handle = INVALID_HANDLE_VALUE32;
213 /* Allocate a handle */
215 *handle = HANDLE_Alloc( PROCESS_Current(), obj, access, inherit );
221 /***********************************************************************
224 * Find the object referenced by a given name.
225 * The reference count is incremented.
227 K32OBJ *K32OBJ_FindName( LPCSTR name )
232 if (!name) return NULL; /* Anonymous object */
233 len = strlen( name );
235 entry = K32OBJ_FirstEntry;
238 if ((len == entry->len) && !strcmp( name, entry->name))
240 K32OBJ *obj = entry->obj;
241 K32OBJ_IncCount( obj );
252 /***********************************************************************
253 * K32OBJ_FindNameType
255 * Find an object by name and check its type.
256 * The reference count is incremented.
258 K32OBJ *K32OBJ_FindNameType( LPCSTR name, K32OBJ_TYPE type )
260 K32OBJ *obj = K32OBJ_FindName( name );
261 if (!obj) return NULL;
262 if (obj->type == type) return obj;
263 SetLastError( ERROR_DUP_NAME );
264 K32OBJ_DecCount( obj );