Added server-side infrastructure for the thread input structure.
[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 "thread.h"
32 #include "unicode.h"
33 #include "list.h"
34
35
36 struct object_name
37 {
38     struct list         entry;    /* entry in the hash list */
39     struct object      *obj;
40     size_t              len;
41     WCHAR               name[1];
42 };
43
44 struct namespace
45 {
46     unsigned int        hash_size;       /* size of hash table */
47     int                 case_sensitive;  /* are names case sensitive? */
48     struct list         names[1];        /* array of hash entry lists */
49 };
50
51
52 #ifdef DEBUG_OBJECTS
53 static struct list object_list = LIST_INIT(object_list);
54
55 void dump_objects(void)
56 {
57     struct list *p;
58
59     LIST_FOR_EACH( p, &object_list )
60     {
61         struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
62         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
63         ptr->ops->dump( ptr, 1 );
64     }
65 }
66 #endif
67
68 /*****************************************************************/
69
70 /* malloc replacement */
71 void *mem_alloc( size_t size )
72 {
73     void *ptr = malloc( size );
74     if (ptr) memset( ptr, 0x55, size );
75     else set_error( STATUS_NO_MEMORY );
76     return ptr;
77 }
78
79 /* duplicate a block of memory */
80 void *memdup( const void *data, size_t len )
81 {
82     void *ptr = malloc( len );
83     if (ptr) memcpy( ptr, data, len );
84     else set_error( STATUS_NO_MEMORY );
85     return ptr;
86 }
87
88
89 /*****************************************************************/
90
91 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, size_t len )
92 {
93     WCHAR hash = 0;
94     len /= sizeof(WCHAR);
95     if (namespace->case_sensitive) while (len--) hash ^= *name++;
96     else 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 /* allocate and initialize an object */
133 /* if the function fails the fd is closed */
134 void *alloc_object( const struct object_ops *ops, int fd )
135 {
136     struct object *obj = mem_alloc( ops->size );
137     if (obj)
138     {
139         obj->refcount = 1;
140         obj->fd       = fd;
141         obj->select   = -1;
142         obj->ops      = ops;
143         obj->head     = NULL;
144         obj->tail     = NULL;
145         obj->name     = NULL;
146         if ((fd != -1) && (add_select_user( obj ) == -1))
147         {
148             close( fd );
149             free( obj );
150             return NULL;
151         }
152 #ifdef DEBUG_OBJECTS
153         list_add_head( &object_list, &obj->obj_list );
154 #endif
155         return obj;
156     }
157     if (fd != -1) close( fd );
158     return NULL;
159 }
160
161 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
162                            const WCHAR *name, size_t len )
163 {
164     struct object *obj;
165     struct object_name *name_ptr;
166
167     if (!name || !len) return alloc_object( ops, -1 );
168
169     if ((obj = find_object( namespace, name, len )))
170     {
171         if (obj->ops == ops)
172         {
173             set_error( STATUS_OBJECT_NAME_COLLISION );
174             return obj;
175         }
176         set_error( STATUS_OBJECT_TYPE_MISMATCH );
177         return NULL;
178     }
179     if (!(name_ptr = alloc_name( name, len ))) return NULL;
180     if ((obj = alloc_object( ops, -1 )))
181     {
182         set_object_name( namespace, obj, name_ptr );
183         clear_error();
184     }
185     else free( name_ptr );
186     return obj;
187 }
188
189 /* dump the name of an object to stderr */
190 void dump_object_name( struct object *obj )
191 {
192     if (!obj->name) fprintf( stderr, "name=\"\"" );
193     else
194     {
195         fprintf( stderr, "name=L\"" );
196         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
197         fputc( '\"', stderr );
198     }
199 }
200
201 /* grab an object (i.e. increment its refcount) and return the object */
202 struct object *grab_object( void *ptr )
203 {
204     struct object *obj = (struct object *)ptr;
205     assert( obj->refcount < INT_MAX );
206     obj->refcount++;
207     return obj;
208 }
209
210 /* release an object (i.e. decrement its refcount) */
211 void release_object( void *ptr )
212 {
213     struct object *obj = (struct object *)ptr;
214     assert( obj->refcount );
215     if (!--obj->refcount)
216     {
217         /* if the refcount is 0, nobody can be in the wait queue */
218         assert( !obj->head );
219         assert( !obj->tail );
220         obj->ops->destroy( obj );
221         if (obj->name) free_name( obj );
222         if (obj->select != -1) remove_select_user( obj );
223         if (obj->fd != -1) close( obj->fd );
224 #ifdef DEBUG_OBJECTS
225         list_remove( &obj->obj_list );
226         memset( obj, 0xaa, obj->ops->size );
227 #endif
228         free( obj );
229     }
230 }
231
232 /* find an object by its name; the refcount is incremented */
233 struct object *find_object( const struct namespace *namespace, const WCHAR *name, size_t len )
234 {
235     const struct list *list, *p;
236
237     if (!name || !len) return NULL;
238
239     list = &namespace->names[ get_name_hash( namespace, name, len ) ];
240     if (namespace->case_sensitive)
241     {
242         LIST_FOR_EACH( p, list )
243         {
244             struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
245             if (ptr->len != len) continue;
246             if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
247         }
248     }
249     else
250     {
251         LIST_FOR_EACH( p, list )
252         {
253             struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
254             if (ptr->len != len) continue;
255             if (!strncmpiW( ptr->name, name, len/sizeof(WCHAR) )) return grab_object( ptr->obj );
256         }
257     }
258     return NULL;
259 }
260
261 /* allocate a namespace */
262 struct namespace *create_namespace( unsigned int hash_size, int case_sensitive )
263 {
264     struct namespace *namespace;
265     unsigned int i;
266
267     namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
268     if (namespace)
269     {
270         namespace->hash_size      = hash_size;
271         namespace->case_sensitive = case_sensitive;
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_get_fd( struct object *obj )
291 {
292     set_error( STATUS_OBJECT_TYPE_MISMATCH );
293     return -1;
294 }
295
296 int no_flush( struct object *obj )
297 {
298     set_error( STATUS_OBJECT_TYPE_MISMATCH );
299     return 0;
300 }
301
302 int no_get_file_info( struct object *obj, struct get_file_info_reply *info, int *flags )
303 {
304     set_error( STATUS_OBJECT_TYPE_MISMATCH );
305     *flags = 0;
306     return FD_TYPE_INVALID;
307 }
308
309 void no_destroy( struct object *obj )
310 {
311 }
312
313 /* default add_queue() routine for objects that poll() on an fd */
314 int default_poll_add_queue( struct object *obj, struct wait_queue_entry *entry )
315 {
316     if (!obj->head)  /* first on the queue */
317         set_select_events( obj, obj->ops->get_poll_events( obj ) );
318     add_queue( obj, entry );
319     return 1;
320 }
321
322 /* default remove_queue() routine for objects that poll() on an fd */
323 void default_poll_remove_queue( struct object *obj, struct wait_queue_entry *entry )
324 {
325     grab_object(obj);
326     remove_queue( obj, entry );
327     if (!obj->head)  /* last on the queue is gone */
328         set_select_events( obj, 0 );
329     release_object( obj );
330 }
331
332 /* default signaled() routine for objects that poll() on an fd */
333 int default_poll_signaled( struct object *obj, struct thread *thread )
334 {
335     int events = obj->ops->get_poll_events( obj );
336
337     if (check_select_events( obj->fd, events ))
338     {
339         /* stop waiting on select() if we are signaled */
340         set_select_events( obj, 0 );
341         return 1;
342     }
343     /* restart waiting on select() if we are no longer signaled */
344     if (obj->head) set_select_events( obj, events );
345     return 0;
346 }
347
348 /* default handler for poll() events */
349 void default_poll_event( struct object *obj, int event )
350 {
351     /* an error occurred, stop polling this fd to avoid busy-looping */
352     if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
353     wake_up( obj, 0 );
354 }