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 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
=
32 NULL
, /* remove_wait */
38 const K32OBJ_OPS
* const K32OBJ_Ops
[K32OBJ_NBOBJECTS
] =
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 */
68 static NAME_ENTRY
*K32OBJ_FirstEntry
= NULL
;
71 /***********************************************************************
74 void K32OBJ_IncCount( K32OBJ
*ptr
)
76 assert( ptr
->type
&& ((unsigned)ptr
->type
< K32OBJ_NBOBJECTS
) );
80 assert( ptr
->refcount
> 0 ); /* No wrap-around allowed */
84 /***********************************************************************
87 void K32OBJ_DecCount( K32OBJ
*ptr
)
91 assert( ptr
->type
&& ((unsigned)ptr
->type
< K32OBJ_NBOBJECTS
) );
92 assert( ptr
->refcount
> 0 );
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
;
106 NAME_ENTRY
*entry
= *pptr
;
108 HeapFree( SystemHeap
, 0, entry
);
111 /* Free the object */
113 if (K32OBJ_Ops
[ptr
->type
]->destroy
) K32OBJ_Ops
[ptr
->type
]->destroy( ptr
);
118 /***********************************************************************
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 /***********************************************************************
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
)
141 if (!name
) return TRUE
; /* Anonymous object */
142 len
= strlen( name
);
144 if (!(entry
= HeapAlloc( SystemHeap
, 0, sizeof(NAME_ENTRY
) + len
)))
147 SetLastError( ERROR_OUTOFMEMORY
);
150 entry
->next
= K32OBJ_FirstEntry
;
153 lstrcpy32A( entry
->name
, name
);
154 K32OBJ_FirstEntry
= entry
;
160 /***********************************************************************
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
, int server_handle
,
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
);
177 if (obj
->type
== type
)
179 SetLastError( ERROR_ALREADY_EXISTS
);
180 *handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, server_handle
);
184 SetLastError( ERROR_DUP_NAME
);
185 *handle
= INVALID_HANDLE_VALUE32
;
186 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
188 K32OBJ_DecCount( obj
);
192 /* Create the object */
195 if (!(obj
= HeapAlloc( SystemHeap
, 0, size
)))
198 *handle
= INVALID_HANDLE_VALUE32
;
199 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
205 /* Add a name for it */
207 if (!K32OBJ_AddName( obj
, name
))
209 /* Don't call the destroy function, as the object wasn't
210 * initialized properly */
211 HeapFree( SystemHeap
, 0, obj
);
213 *handle
= INVALID_HANDLE_VALUE32
;
214 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
218 /* Allocate a handle */
220 *handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, server_handle
);
226 /***********************************************************************
229 * Find the object referenced by a given name.
230 * The reference count is incremented.
232 K32OBJ
*K32OBJ_FindName( LPCSTR name
)
237 if (!name
) return NULL
; /* Anonymous object */
238 len
= strlen( name
);
240 entry
= K32OBJ_FirstEntry
;
243 if ((len
== entry
->len
) && !strcmp( name
, entry
->name
))
245 K32OBJ
*obj
= entry
->obj
;
246 K32OBJ_IncCount( obj
);
257 /***********************************************************************
258 * K32OBJ_FindNameType
260 * Find an object by name and check its type.
261 * The reference count is incremented.
263 K32OBJ
*K32OBJ_FindNameType( LPCSTR name
, K32OBJ_TYPE type
)
265 K32OBJ
*obj
= K32OBJ_FindName( name
);
266 if (!obj
) return NULL
;
267 if (obj
->type
== type
) return obj
;
268 SetLastError( ERROR_DUP_NAME
);
269 K32OBJ_DecCount( obj
);