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