4 * Copyright 1996 Alexandre Julliard
15 /* The declarations are here to avoid including a lot of unnecessary files */
16 extern const K32OBJ_OPS PROCESS_Ops
;
17 extern const K32OBJ_OPS THREAD_Ops
;
18 extern const K32OBJ_OPS MEM_MAPPED_FILE_Ops
;
20 static const K32OBJ_OPS K32OBJ_NullOps
=
25 static void K32OBJ_Destroy( K32OBJ
*obj
);
27 static const K32OBJ_OPS K32OBJ_DefaultOps
=
29 K32OBJ_Destroy
/* destroy */
33 const K32OBJ_OPS
* const K32OBJ_Ops
[K32OBJ_NBOBJECTS
] =
36 &K32OBJ_DefaultOps
, /* K32OBJ_SEMAPHORE */
37 &K32OBJ_DefaultOps
, /* K32OBJ_EVENT */
38 &K32OBJ_DefaultOps
, /* K32OBJ_MUTEX */
39 &K32OBJ_NullOps
, /* K32OBJ_CRITICAL_SECTION */
40 &PROCESS_Ops
, /* K32OBJ_PROCESS */
41 &THREAD_Ops
, /* K32OBJ_THREAD */
42 &K32OBJ_DefaultOps
, /* K32OBJ_FILE */
43 &K32OBJ_DefaultOps
, /* K32OBJ_CHANGE */
44 &K32OBJ_DefaultOps
, /* K32OBJ_CONSOLE */
45 &K32OBJ_NullOps
, /* K32OBJ_SCREEN_BUFFER */
46 &MEM_MAPPED_FILE_Ops
, /* K32OBJ_MEM_MAPPED_FILE */
47 &K32OBJ_NullOps
, /* K32OBJ_SERIAL */
48 &K32OBJ_NullOps
, /* K32OBJ_DEVICE_IOCTL */
49 &K32OBJ_DefaultOps
, /* K32OBJ_PIPE */
50 &K32OBJ_NullOps
, /* K32OBJ_MAILSLOT */
51 &K32OBJ_DefaultOps
, /* K32OBJ_TOOLHELP_SNAPSHOT */
52 &K32OBJ_NullOps
/* K32OBJ_SOCKET */
63 static NAME_ENTRY
*K32OBJ_FirstEntry
= NULL
;
66 /***********************************************************************
69 void K32OBJ_IncCount( K32OBJ
*ptr
)
71 assert( ptr
->type
&& ((unsigned)ptr
->type
< K32OBJ_NBOBJECTS
) );
75 assert( ptr
->refcount
> 0 ); /* No wrap-around allowed */
79 /***********************************************************************
82 void K32OBJ_DecCount( K32OBJ
*ptr
)
86 assert( ptr
->type
&& ((unsigned)ptr
->type
< K32OBJ_NBOBJECTS
) );
87 assert( ptr
->refcount
> 0 );
95 /* Check if the object has a name entry and free it */
97 pptr
= &K32OBJ_FirstEntry
;
98 while (*pptr
&& ((*pptr
)->obj
!= ptr
)) pptr
= &(*pptr
)->next
;
101 NAME_ENTRY
*entry
= *pptr
;
103 HeapFree( SystemHeap
, 0, entry
);
106 /* Free the object */
108 if (K32OBJ_Ops
[ptr
->type
]->destroy
) K32OBJ_Ops
[ptr
->type
]->destroy( ptr
);
113 /***********************************************************************
116 * Generic destroy functions for objects that don't need any special treatment.
118 static void K32OBJ_Destroy( K32OBJ
*obj
)
120 obj
->type
= K32OBJ_UNKNOWN
;
121 HeapFree( SystemHeap
, 0, obj
);
125 /***********************************************************************
128 * Check if a pointer is a valid kernel object
130 BOOL32
K32OBJ_IsValid( K32OBJ
*ptr
, K32OBJ_TYPE type
)
132 if (IsBadReadPtr32( ptr
, sizeof(*ptr
) )) return FALSE
;
133 return (ptr
->type
== type
);
137 /***********************************************************************
140 * Add a name entry for an object. We don't check for duplicates here.
141 * FIXME: should use some sort of hashing.
143 BOOL32
K32OBJ_AddName( K32OBJ
*obj
, LPCSTR name
)
148 if (!name
) return TRUE
; /* Anonymous object */
149 len
= strlen( name
);
151 if (!(entry
= HeapAlloc( SystemHeap
, 0, sizeof(NAME_ENTRY
) + len
)))
154 SetLastError( ERROR_OUTOFMEMORY
);
157 entry
->next
= K32OBJ_FirstEntry
;
160 lstrcpy32A( entry
->name
, name
);
161 K32OBJ_FirstEntry
= entry
;
167 /***********************************************************************
170 * Create a named kernel object.
171 * Returns NULL if there was an error _or_ if the object already existed.
172 * The refcount of the object must be decremented once it is initialized.
174 K32OBJ
*K32OBJ_Create( K32OBJ_TYPE type
, DWORD size
, LPCSTR name
, int server_handle
,
175 DWORD access
, SECURITY_ATTRIBUTES
*sa
, HANDLE32
*handle
)
177 BOOL32 inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
179 /* Check if the name already exists */
181 K32OBJ
*obj
= K32OBJ_FindName( name
);
184 if (obj
->type
== type
)
186 SetLastError( ERROR_ALREADY_EXISTS
);
187 *handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, server_handle
);
191 SetLastError( ERROR_DUP_NAME
);
192 *handle
= INVALID_HANDLE_VALUE32
;
193 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
195 K32OBJ_DecCount( obj
);
199 /* Create the object */
202 if (!(obj
= HeapAlloc( SystemHeap
, 0, size
)))
205 *handle
= INVALID_HANDLE_VALUE32
;
206 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
212 /* Add a name for it */
214 if (!K32OBJ_AddName( obj
, name
))
216 /* Don't call the destroy function, as the object wasn't
217 * initialized properly */
218 HeapFree( SystemHeap
, 0, obj
);
220 *handle
= INVALID_HANDLE_VALUE32
;
221 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
225 /* Allocate a handle */
227 *handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, server_handle
);
233 /***********************************************************************
236 * Find the object referenced by a given name.
237 * The reference count is incremented.
239 K32OBJ
*K32OBJ_FindName( LPCSTR name
)
244 if (!name
) return NULL
; /* Anonymous object */
245 len
= strlen( name
);
247 entry
= K32OBJ_FirstEntry
;
250 if ((len
== entry
->len
) && !strcmp( name
, entry
->name
))
252 K32OBJ
*obj
= entry
->obj
;
253 K32OBJ_IncCount( obj
);
264 /***********************************************************************
265 * K32OBJ_FindNameType
267 * Find an object by name and check its type.
268 * The reference count is incremented.
270 K32OBJ
*K32OBJ_FindNameType( LPCSTR name
, K32OBJ_TYPE type
)
272 K32OBJ
*obj
= K32OBJ_FindName( name
);
273 if (!obj
) return NULL
;
274 if (obj
->type
== type
) return obj
;
275 SetLastError( ERROR_DUP_NAME
);
276 K32OBJ_DecCount( obj
);