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
;
19 extern const K32OBJ_OPS CONSOLE_Ops
;
20 extern const K32OBJ_OPS SNAPSHOT_Ops
;
22 static const K32OBJ_OPS K32OBJ_NullOps
=
27 static void K32OBJ_Destroy( K32OBJ
*obj
);
29 static const K32OBJ_OPS K32OBJ_DefaultOps
=
31 K32OBJ_Destroy
/* destroy */
35 const K32OBJ_OPS
* const K32OBJ_Ops
[K32OBJ_NBOBJECTS
] =
38 &K32OBJ_DefaultOps
, /* K32OBJ_SEMAPHORE */
39 &K32OBJ_DefaultOps
, /* K32OBJ_EVENT */
40 &K32OBJ_DefaultOps
, /* K32OBJ_MUTEX */
41 &K32OBJ_NullOps
, /* K32OBJ_CRITICAL_SECTION */
42 &PROCESS_Ops
, /* K32OBJ_PROCESS */
43 &THREAD_Ops
, /* K32OBJ_THREAD */
44 &K32OBJ_DefaultOps
, /* K32OBJ_FILE */
45 &K32OBJ_DefaultOps
, /* K32OBJ_CHANGE */
46 &CONSOLE_Ops
, /* K32OBJ_CONSOLE */
47 &K32OBJ_NullOps
, /* K32OBJ_SCREEN_BUFFER */
48 &MEM_MAPPED_FILE_Ops
, /* K32OBJ_MEM_MAPPED_FILE */
49 &K32OBJ_NullOps
, /* K32OBJ_SERIAL */
50 &K32OBJ_NullOps
, /* K32OBJ_DEVICE_IOCTL */
51 &K32OBJ_DefaultOps
, /* K32OBJ_PIPE */
52 &K32OBJ_NullOps
, /* K32OBJ_MAILSLOT */
53 &K32OBJ_NullOps
, /* K32OBJ_TOOLHELP_SNAPSHOT */
54 &K32OBJ_NullOps
/* K32OBJ_SOCKET */
65 static NAME_ENTRY
*K32OBJ_FirstEntry
= NULL
;
68 /***********************************************************************
71 void K32OBJ_IncCount( K32OBJ
*ptr
)
73 assert( ptr
->type
&& ((unsigned)ptr
->type
< K32OBJ_NBOBJECTS
) );
77 assert( ptr
->refcount
> 0 ); /* No wrap-around allowed */
81 /***********************************************************************
84 void K32OBJ_DecCount( K32OBJ
*ptr
)
88 assert( ptr
->type
&& ((unsigned)ptr
->type
< K32OBJ_NBOBJECTS
) );
89 assert( ptr
->refcount
> 0 );
97 /* Check if the object has a name entry and free it */
99 pptr
= &K32OBJ_FirstEntry
;
100 while (*pptr
&& ((*pptr
)->obj
!= ptr
)) pptr
= &(*pptr
)->next
;
103 NAME_ENTRY
*entry
= *pptr
;
105 HeapFree( SystemHeap
, 0, entry
);
108 /* Free the object */
110 if (K32OBJ_Ops
[ptr
->type
]->destroy
) K32OBJ_Ops
[ptr
->type
]->destroy( ptr
);
115 /***********************************************************************
118 * Generic destroy functions for objects that don't need any special treatment.
120 static void K32OBJ_Destroy( K32OBJ
*obj
)
122 obj
->type
= K32OBJ_UNKNOWN
;
123 HeapFree( SystemHeap
, 0, obj
);
127 /***********************************************************************
130 * Check if a pointer is a valid kernel object
132 BOOL32
K32OBJ_IsValid( K32OBJ
*ptr
, K32OBJ_TYPE type
)
134 if (IsBadReadPtr32( ptr
, sizeof(*ptr
) )) return FALSE
;
135 return (ptr
->type
== type
);
139 /***********************************************************************
142 * Add a name entry for an object. We don't check for duplicates here.
143 * FIXME: should use some sort of hashing.
145 BOOL32
K32OBJ_AddName( K32OBJ
*obj
, LPCSTR name
)
150 if (!name
) return TRUE
; /* Anonymous object */
151 len
= strlen( name
);
153 if (!(entry
= HeapAlloc( SystemHeap
, 0, sizeof(NAME_ENTRY
) + len
)))
156 SetLastError( ERROR_OUTOFMEMORY
);
159 entry
->next
= K32OBJ_FirstEntry
;
162 lstrcpy32A( entry
->name
, name
);
163 K32OBJ_FirstEntry
= entry
;
169 /***********************************************************************
172 * Create a named kernel object.
173 * Returns NULL if there was an error _or_ if the object already existed.
174 * The refcount of the object must be decremented once it is initialized.
176 K32OBJ
*K32OBJ_Create( K32OBJ_TYPE type
, DWORD size
, LPCSTR name
, int server_handle
,
177 DWORD access
, SECURITY_ATTRIBUTES
*sa
, HANDLE32
*handle
)
179 BOOL32 inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
181 /* Check if the name already exists */
183 K32OBJ
*obj
= K32OBJ_FindName( name
);
186 if (obj
->type
== type
)
188 SetLastError( ERROR_ALREADY_EXISTS
);
189 *handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, server_handle
);
193 SetLastError( ERROR_DUP_NAME
);
194 *handle
= INVALID_HANDLE_VALUE32
;
195 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
197 K32OBJ_DecCount( obj
);
201 /* Create the object */
204 if (!(obj
= HeapAlloc( SystemHeap
, 0, size
)))
207 *handle
= INVALID_HANDLE_VALUE32
;
208 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
214 /* Add a name for it */
216 if (!K32OBJ_AddName( obj
, name
))
218 /* Don't call the destroy function, as the object wasn't
219 * initialized properly */
220 HeapFree( SystemHeap
, 0, obj
);
222 *handle
= INVALID_HANDLE_VALUE32
;
223 if (server_handle
!= -1) CLIENT_CloseHandle( server_handle
);
227 /* Allocate a handle */
229 *handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, server_handle
);
235 /***********************************************************************
238 * Find the object referenced by a given name.
239 * The reference count is incremented.
241 K32OBJ
*K32OBJ_FindName( LPCSTR name
)
246 if (!name
) return NULL
; /* Anonymous object */
247 len
= strlen( name
);
249 entry
= K32OBJ_FirstEntry
;
252 if ((len
== entry
->len
) && !strcmp( name
, entry
->name
))
254 K32OBJ
*obj
= entry
->obj
;
255 K32OBJ_IncCount( obj
);
266 /***********************************************************************
267 * K32OBJ_FindNameType
269 * Find an object by name and check its type.
270 * The reference count is incremented.
272 K32OBJ
*K32OBJ_FindNameType( LPCSTR name
, K32OBJ_TYPE type
)
274 K32OBJ
*obj
= K32OBJ_FindName( name
);
275 if (!obj
) return NULL
;
276 if (obj
->type
== type
) return obj
;
277 SetLastError( ERROR_DUP_NAME
);
278 K32OBJ_DecCount( obj
);