Implement FC_STRUCTPAD2 for complex types.
[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 "winternl.h"
33
34 #include "file.h"
35 #include "thread.h"
36 #include "unicode.h"
37
38
39 struct object_name
40 {
41     struct list         entry;    /* entry in the hash list */
42     struct object      *obj;
43     size_t              len;
44     WCHAR               name[1];
45 };
46
47 struct namespace
48 {
49     unsigned int        hash_size;       /* size of hash table */
50     struct list         names[1];        /* array of hash entry lists */
51 };
52
53
54 #ifdef DEBUG_OBJECTS
55 static struct list object_list = LIST_INIT(object_list);
56
57 void dump_objects(void)
58 {
59     struct list *p;
60
61     LIST_FOR_EACH( p, &object_list )
62     {
63         struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
64         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
65         ptr->ops->dump( ptr, 1 );
66     }
67 }
68 #endif
69
70 /*****************************************************************/
71
72 /* malloc replacement */
73 void *mem_alloc( size_t size )
74 {
75     void *ptr = malloc( size );
76     if (ptr) memset( ptr, 0x55, size );
77     else set_error( STATUS_NO_MEMORY );
78     return ptr;
79 }
80
81 /* duplicate a block of memory */
82 void *memdup( const void *data, size_t len )
83 {
84     void *ptr = malloc( len );
85     if (ptr) memcpy( ptr, data, len );
86     else set_error( STATUS_NO_MEMORY );
87     return ptr;
88 }
89
90
91 /*****************************************************************/
92
93 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, size_t len )
94 {
95     WCHAR hash = 0;
96     len /= sizeof(WCHAR);
97     while (len--) hash ^= tolowerW(*name++);
98     return hash % namespace->hash_size;
99 }
100
101 /* allocate a name for an object */
102 static struct object_name *alloc_name( const struct unicode_str *name )
103 {
104     struct object_name *ptr;
105
106     if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
107     {
108         ptr->len = name->len;
109         memcpy( ptr->name, name->str, name->len );
110     }
111     return ptr;
112 }
113
114 /* free the name of an object */
115 static void free_name( struct object *obj )
116 {
117     struct object_name *ptr = obj->name;
118     list_remove( &ptr->entry );
119     free( ptr );
120 }
121
122 /* set the name of an existing object */
123 static void set_object_name( struct namespace *namespace,
124                              struct object *obj, struct object_name *ptr )
125 {
126     int hash = get_name_hash( namespace, ptr->name, ptr->len );
127
128     list_add_head( &namespace->names[hash], &ptr->entry );
129     ptr->obj = obj;
130     obj->name = ptr;
131 }
132
133 /* get the name of an existing object */
134 const WCHAR *get_object_name( struct object *obj, size_t *len )
135 {
136     struct object_name *ptr = obj->name;
137     if (!ptr) return NULL;
138     *len = ptr->len;
139     return ptr->name;
140 }
141
142 /* allocate and initialize an object */
143 void *alloc_object( const struct object_ops *ops )
144 {
145     struct object *obj = mem_alloc( ops->size );
146     if (obj)
147     {
148         obj->refcount = 1;
149         obj->ops      = ops;
150         obj->name     = NULL;
151         list_init( &obj->wait_queue );
152 #ifdef DEBUG_OBJECTS
153         list_add_head( &object_list, &obj->obj_list );
154 #endif
155         return obj;
156     }
157     return NULL;
158 }
159
160 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
161                            const struct unicode_str *name, unsigned int attributes )
162 {
163     struct object *obj;
164     struct object_name *name_ptr;
165
166     if (!name || !name->len) return alloc_object( ops );
167
168     if ((obj = find_object( namespace, name, attributes )))
169     {
170         if (attributes & OBJ_OPENIF && obj->ops == ops)
171             set_error( STATUS_OBJECT_NAME_EXISTS );
172         else
173         {
174             release_object( obj );
175             obj = NULL;
176             if (attributes & OBJ_OPENIF)
177                 set_error( STATUS_OBJECT_TYPE_MISMATCH );
178             else
179                 set_error( STATUS_OBJECT_NAME_COLLISION );
180         }
181         return obj;
182     }
183     if (!(name_ptr = alloc_name( name ))) return NULL;
184     if ((obj = alloc_object( ops )))
185     {
186         set_object_name( namespace, obj, name_ptr );
187         clear_error();
188     }
189     else free( name_ptr );
190     return obj;
191 }
192
193 /* dump the name of an object to stderr */
194 void dump_object_name( struct object *obj )
195 {
196     if (!obj->name) fprintf( stderr, "name=\"\"" );
197     else
198     {
199         fprintf( stderr, "name=L\"" );
200         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
201         fputc( '\"', stderr );
202     }
203 }
204
205 /* grab an object (i.e. increment its refcount) and return the object */
206 struct object *grab_object( void *ptr )
207 {
208     struct object *obj = (struct object *)ptr;
209     assert( obj->refcount < INT_MAX );
210     obj->refcount++;
211     return obj;
212 }
213
214 /* release an object (i.e. decrement its refcount) */
215 void release_object( void *ptr )
216 {
217     struct object *obj = (struct object *)ptr;
218     assert( obj->refcount );
219     if (!--obj->refcount)
220     {
221         /* if the refcount is 0, nobody can be in the wait queue */
222         assert( list_empty( &obj->wait_queue ));
223         obj->ops->destroy( obj );
224         if (obj->name) free_name( obj );
225 #ifdef DEBUG_OBJECTS
226         list_remove( &obj->obj_list );
227         memset( obj, 0xaa, obj->ops->size );
228 #endif
229         free( obj );
230     }
231 }
232
233 /* find an object by its name; the refcount is incremented */
234 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
235                             unsigned int attributes )
236 {
237     const struct list *list, *p;
238
239     if (!name || !name->len) return NULL;
240
241     list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
242     LIST_FOR_EACH( p, list )
243     {
244         const struct object_name *ptr = LIST_ENTRY( p, const struct object_name, entry );
245         if (ptr->len != name->len) continue;
246         if (attributes & OBJ_CASE_INSENSITIVE)
247         {
248             if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
249                 return grab_object( ptr->obj );
250         }
251         else
252         {
253             if (!memcmp( ptr->name, name->str, name->len ))
254                 return grab_object( ptr->obj );
255         }
256     }
257     return NULL;
258 }
259
260 /* allocate a namespace */
261 struct namespace *create_namespace( unsigned int hash_size )
262 {
263     struct namespace *namespace;
264     unsigned int i;
265
266     namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
267     if (namespace)
268     {
269         namespace->hash_size      = hash_size;
270         for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
271     }
272     return namespace;
273 }
274
275 /* functions for unimplemented/default object operations */
276
277 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
278 {
279     set_error( STATUS_OBJECT_TYPE_MISMATCH );
280     return 0;
281 }
282
283 int no_satisfied( struct object *obj, struct thread *thread )
284 {
285     return 0;  /* not abandoned */
286 }
287
288 int no_signal( struct object *obj, unsigned int access )
289 {
290     set_error( STATUS_OBJECT_TYPE_MISMATCH );
291     return 0;
292 }
293
294 struct fd *no_get_fd( struct object *obj )
295 {
296     set_error( STATUS_OBJECT_TYPE_MISMATCH );
297     return NULL;
298 }
299
300 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
301                                unsigned int attr )
302 {
303     return NULL;
304 }
305
306 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
307 {
308     return 1;  /* ok to close */
309 }
310
311 void no_destroy( struct object *obj )
312 {
313 }