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
,
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
, -1 );
184 SetLastError( ERROR_DUP_NAME
);
185 *handle
= INVALID_HANDLE_VALUE32
;
187 K32OBJ_DecCount( obj
);
191 /* Create the object */
194 if (!(obj
= HeapAlloc( SystemHeap
, 0, size
)))
197 *handle
= INVALID_HANDLE_VALUE32
;
203 /* Add a name for it */
205 if (!K32OBJ_AddName( obj
, name
))
207 /* Don't call the destroy function, as the object wasn't
208 * initialized properly */
209 HeapFree( SystemHeap
, 0, obj
);
211 *handle
= INVALID_HANDLE_VALUE32
;
215 /* Allocate a handle */
217 *handle
= HANDLE_Alloc( PROCESS_Current(), obj
, access
, inherit
, -1 );
223 /***********************************************************************
226 * Find the object referenced by a given name.
227 * The reference count is incremented.
229 K32OBJ
*K32OBJ_FindName( LPCSTR name
)
234 if (!name
) return NULL
; /* Anonymous object */
235 len
= strlen( name
);
237 entry
= K32OBJ_FirstEntry
;
240 if ((len
== entry
->len
) && !strcmp( name
, entry
->name
))
242 K32OBJ
*obj
= entry
->obj
;
243 K32OBJ_IncCount( obj
);
254 /***********************************************************************
255 * K32OBJ_FindNameType
257 * Find an object by name and check its type.
258 * The reference count is incremented.
260 K32OBJ
*K32OBJ_FindNameType( LPCSTR name
, K32OBJ_TYPE type
)
262 K32OBJ
*obj
= K32OBJ_FindName( name
);
263 if (!obj
) return NULL
;
264 if (obj
->type
== type
) return obj
;
265 SetLastError( ERROR_DUP_NAME
);
266 K32OBJ_DecCount( obj
);