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
;
150 lstrcpy32A( entry
->name
, name
);
151 K32OBJ_FirstEntry
= entry
;
157 /***********************************************************************
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
);
172 if (obj
->type
== type
)
174 SetLastError( ERROR_ALREADY_EXISTS
);
175 *handle
= HANDLE_Alloc( obj
, access
, FALSE
);
179 SetLastError( ERROR_DUP_NAME
);
180 *handle
= INVALID_HANDLE_VALUE32
;
182 K32OBJ_DecCount( obj
);
186 /* Create the object */
189 if (!(obj
= HeapAlloc( SystemHeap
, 0, size
)))
192 *handle
= INVALID_HANDLE_VALUE32
;
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
);
206 *handle
= INVALID_HANDLE_VALUE32
;
210 /* Allocate a handle */
212 *handle
= HANDLE_Alloc( obj
, access
, FALSE
);
218 /***********************************************************************
221 * Find the object referenced by a given name.
222 * The reference count is incremented.
224 K32OBJ
*K32OBJ_FindName( LPCSTR name
)
229 if (!name
) return NULL
; /* Anonymous object */
230 len
= strlen( name
);
232 entry
= K32OBJ_FirstEntry
;
235 if ((len
== entry
->len
) && !lstrcmp32A( name
, entry
->name
))
237 K32OBJ
*obj
= entry
->obj
;
238 K32OBJ_IncCount( obj
);
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
);