Match PSDK STATUS_* definitions.
[wine] / server / object.c
1 /*
2  * Server-side objects
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdarg.h>
31
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "winternl.h"
35
36 #include "file.h"
37 #include "thread.h"
38 #include "unicode.h"
39
40
41 struct object_name
42 {
43     struct list         entry;    /* entry in the hash list */
44     struct object      *obj;
45     size_t              len;
46     WCHAR               name[1];
47 };
48
49 struct namespace
50 {
51     unsigned int        hash_size;       /* size of hash table */
52     struct list         names[1];        /* array of hash entry lists */
53 };
54
55
56 #ifdef DEBUG_OBJECTS
57 static struct list object_list = LIST_INIT(object_list);
58
59 void dump_objects(void)
60 {
61     struct list *p;
62
63     LIST_FOR_EACH( p, &object_list )
64     {
65         struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
66         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
67         ptr->ops->dump( ptr, 1 );
68     }
69 }
70 #endif
71
72 /*****************************************************************/
73
74 /* malloc replacement */
75 void *mem_alloc( size_t size )
76 {
77     void *ptr = malloc( size );
78     if (ptr) memset( ptr, 0x55, size );
79     else set_error( STATUS_NO_MEMORY );
80     return ptr;
81 }
82
83 /* duplicate a block of memory */
84 void *memdup( const void *data, size_t len )
85 {
86     void *ptr = malloc( len );
87     if (ptr) memcpy( ptr, data, len );
88     else set_error( STATUS_NO_MEMORY );
89     return ptr;
90 }
91
92
93 /*****************************************************************/
94
95 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, size_t len )
96 {
97     WCHAR hash = 0;
98     len /= sizeof(WCHAR);
99     while (len--) hash ^= tolowerW(*name++);
100     return hash % namespace->hash_size;
101 }
102
103 /* allocate a name for an object */
104 static struct object_name *alloc_name( const struct unicode_str *name )
105 {
106     struct object_name *ptr;
107
108     if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
109     {
110         ptr->len = name->len;
111         memcpy( ptr->name, name->str, name->len );
112     }
113     return ptr;
114 }
115
116 /* free the name of an object */
117 static void free_name( struct object *obj )
118 {
119     struct object_name *ptr = obj->name;
120     list_remove( &ptr->entry );
121     free( ptr );
122 }
123
124 /* set the name of an existing object */
125 static void set_object_name( struct namespace *namespace,
126                              struct object *obj, struct object_name *ptr )
127 {
128     int hash = get_name_hash( namespace, ptr->name, ptr->len );
129
130     list_add_head( &namespace->names[hash], &ptr->entry );
131     ptr->obj = obj;
132     obj->name = ptr;
133 }
134
135 /* get the name of an existing object */
136 const WCHAR *get_object_name( struct object *obj, size_t *len )
137 {
138     struct object_name *ptr = obj->name;
139     if (!ptr) return NULL;
140     *len = ptr->len;
141     return ptr->name;
142 }
143
144 /* allocate and initialize an object */
145 void *alloc_object( const struct object_ops *ops )
146 {
147     struct object *obj = mem_alloc( ops->size );
148     if (obj)
149     {
150         obj->refcount = 1;
151         obj->ops      = ops;
152         obj->name     = NULL;
153         list_init( &obj->wait_queue );
154 #ifdef DEBUG_OBJECTS
155         list_add_head( &object_list, &obj->obj_list );
156 #endif
157         return obj;
158     }
159     return NULL;
160 }
161
162 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
163                            const struct unicode_str *name, unsigned int attributes )
164 {
165     struct object *obj;
166     struct object_name *name_ptr;
167
168     if (!name || !name->len) return alloc_object( ops );
169
170     if ((obj = find_object( namespace, name, attributes )))
171     {
172         if (attributes & OBJ_OPENIF && obj->ops == ops)
173             set_error( STATUS_OBJECT_NAME_EXISTS );
174         else
175         {
176             release_object( obj );
177             obj = NULL;
178             if (attributes & OBJ_OPENIF)
179                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
180             else
181                 set_error( STATUS_OBJECT_NAME_COLLISION );
182         }
183         return obj;
184     }
185     if (!(name_ptr = alloc_name( name ))) return NULL;
186     if ((obj = alloc_object( ops )))
187     {
188         set_object_name( namespace, obj, name_ptr );
189         clear_error();
190     }
191     else free( name_ptr );
192     return obj;
193 }
194
195 /* dump the name of an object to stderr */
196 void dump_object_name( struct object *obj )
197 {
198     if (!obj->name) fprintf( stderr, "name=\"\"" );
199     else
200     {
201         fprintf( stderr, "name=L\"" );
202         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
203         fputc( '\"', stderr );
204     }
205 }
206
207 /* grab an object (i.e. increment its refcount) and return the object */
208 struct object *grab_object( void *ptr )
209 {
210     struct object *obj = (struct object *)ptr;
211     assert( obj->refcount < INT_MAX );
212     obj->refcount++;
213     return obj;
214 }
215
216 /* release an object (i.e. decrement its refcount) */
217 void release_object( void *ptr )
218 {
219     struct object *obj = (struct object *)ptr;
220     assert( obj->refcount );
221     if (!--obj->refcount)
222     {
223         /* if the refcount is 0, nobody can be in the wait queue */
224         assert( list_empty( &obj->wait_queue ));
225         obj->ops->destroy( obj );
226         if (obj->name) free_name( obj );
227 #ifdef DEBUG_OBJECTS
228         list_remove( &obj->obj_list );
229         memset( obj, 0xaa, obj->ops->size );
230 #endif
231         free( obj );
232     }
233 }
234
235 /* find an object by its name; the refcount is incremented */
236 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
237                             unsigned int attributes )
238 {
239     const struct list *list, *p;
240
241     if (!name || !name->len) return NULL;
242
243     list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
244     LIST_FOR_EACH( p, list )
245     {
246         const struct object_name *ptr = LIST_ENTRY( p, const struct object_name, entry );
247         if (ptr->len != name->len) continue;
248         if (attributes & OBJ_CASE_INSENSITIVE)
249         {
250             if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
251                 return grab_object( ptr->obj );
252         }
253         else
254         {
255             if (!memcmp( ptr->name, name->str, name->len ))
256                 return grab_object( ptr->obj );
257         }
258     }
259     return NULL;
260 }
261
262 /* allocate a namespace */
263 struct namespace *create_namespace( unsigned int hash_size )
264 {
265     struct namespace *namespace;
266     unsigned int i;
267
268     namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
269     if (namespace)
270     {
271         namespace->hash_size      = hash_size;
272         for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
273     }
274     return namespace;
275 }
276
277 /* functions for unimplemented/default object operations */
278
279 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
280 {
281     set_error( STATUS_OBJECT_TYPE_MISMATCH );
282     return 0;
283 }
284
285 int no_satisfied( struct object *obj, struct thread *thread )
286 {
287     return 0;  /* not abandoned */
288 }
289
290 int no_signal( struct object *obj, unsigned int access )
291 {
292     set_error( STATUS_OBJECT_TYPE_MISMATCH );
293     return 0;
294 }
295
296 struct fd *no_get_fd( struct object *obj )
297 {
298     set_error( STATUS_OBJECT_TYPE_MISMATCH );
299     return NULL;
300 }
301
302 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
303                                unsigned int attr )
304 {
305     return NULL;
306 }
307
308 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
309 {
310     return 1;  /* ok to close */
311 }
312
313 void no_destroy( struct object *obj )
314 {
315 }