Link the tools and the server against libwine so we can use the
[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
34
35 struct object_name
36 {
37     struct object_name *next;
38     struct object_name *prev;
39     struct object      *obj;
40     size_t              len;
41     WCHAR               name[1];
42 };
43
44 #define NAME_HASH_SIZE 37
45
46 static struct object_name *names[NAME_HASH_SIZE];
47
48 #ifdef DEBUG_OBJECTS
49 static struct object *first;
50
51 void dump_objects(void)
52 {
53     struct object *ptr = first;
54     while (ptr)
55     {
56         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
57         ptr->ops->dump( ptr, 1 );
58         ptr = ptr->next;
59     }
60 }
61 #endif
62
63 /*****************************************************************/
64
65 /* malloc replacement */
66 void *mem_alloc( size_t size )
67 {
68     void *ptr = malloc( size );
69     if (ptr) memset( ptr, 0x55, size );
70     else set_error( STATUS_NO_MEMORY );
71     return ptr;
72 }
73
74 /* duplicate a block of memory */
75 void *memdup( const void *data, size_t len )
76 {
77     void *ptr = malloc( len );
78     if (ptr) memcpy( ptr, data, len );
79     else set_error( STATUS_NO_MEMORY );
80     return ptr;
81 }
82
83
84 /*****************************************************************/
85
86 static int get_name_hash( const WCHAR *name, size_t len )
87 {
88     WCHAR hash = 0;
89     len /= sizeof(WCHAR);
90     while (len--) hash ^= *name++;
91     return hash % NAME_HASH_SIZE;
92 }
93
94 /* allocate a name for an object */
95 static struct object_name *alloc_name( const WCHAR *name, size_t len )
96 {
97     struct object_name *ptr;
98
99     if ((ptr = mem_alloc( sizeof(*ptr) + len - sizeof(ptr->name) )))
100     {
101         ptr->len = len;
102         memcpy( ptr->name, name, len );
103     }
104     return ptr;
105 }
106
107 /* free the name of an object */
108 static void free_name( struct object *obj )
109 {
110     struct object_name *ptr = obj->name;
111     if (ptr->next) ptr->next->prev = ptr->prev;
112     if (ptr->prev) ptr->prev->next = ptr->next;
113     else
114     {
115         int hash;
116         for (hash = 0; hash < NAME_HASH_SIZE; hash++)
117             if (names[hash] == ptr)
118             {
119                 names[hash] = ptr->next;
120                 break;
121             }
122     }
123     free( ptr );
124 }
125
126 /* set the name of an existing object */
127 static void set_object_name( struct object *obj, struct object_name *ptr )
128 {
129     int hash = get_name_hash( ptr->name, ptr->len );
130
131     if ((ptr->next = names[hash]) != NULL) ptr->next->prev = ptr;
132     ptr->obj = obj;
133     ptr->prev = NULL;
134     names[hash] = ptr;
135     assert( !obj->name );
136     obj->name = ptr;
137 }
138
139 /* allocate and initialize an object */
140 /* if the function fails the fd is closed */
141 void *alloc_object( const struct object_ops *ops, int fd )
142 {
143     struct object *obj = mem_alloc( ops->size );
144     if (obj)
145     {
146         obj->refcount = 1;
147         obj->fd       = fd;
148         obj->select   = -1;
149         obj->ops      = ops;
150         obj->head     = NULL;
151         obj->tail     = NULL;
152         obj->name     = NULL;
153         if ((fd != -1) && (add_select_user( obj ) == -1))
154         {
155             close( fd );
156             free( obj );
157             return NULL;
158         }
159 #ifdef DEBUG_OBJECTS
160         obj->prev = NULL;
161         if ((obj->next = first) != NULL) obj->next->prev = obj;
162         first = obj;
163 #endif
164         return obj;
165     }
166     if (fd != -1) close( fd );
167     return NULL;
168 }
169
170 void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
171 {
172     struct object *obj;
173     struct object_name *name_ptr;
174
175     if (!name || !len) return alloc_object( ops, -1 );
176     if (!(name_ptr = alloc_name( name, len ))) return NULL;
177
178     if ((obj = find_object( name_ptr->name, name_ptr->len )))
179     {
180         free( name_ptr );  /* we no longer need it */
181         if (obj->ops == ops)
182         {
183             set_error( STATUS_OBJECT_NAME_COLLISION );
184             return obj;
185         }
186         set_error( STATUS_OBJECT_TYPE_MISMATCH );
187         return NULL;
188     }
189     if ((obj = alloc_object( ops, -1 )))
190     {
191         set_object_name( obj, name_ptr );
192         clear_error();
193     }
194     else free( name_ptr );
195     return obj;
196 }
197
198 /* dump the name of an object to stderr */
199 void dump_object_name( struct object *obj )
200 {
201     if (!obj->name) fprintf( stderr, "name=\"\"" );
202     else
203     {
204         fprintf( stderr, "name=L\"" );
205         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
206         fputc( '\"', stderr );
207     }
208 }
209
210 /* grab an object (i.e. increment its refcount) and return the object */
211 struct object *grab_object( void *ptr )
212 {
213     struct object *obj = (struct object *)ptr;
214     assert( obj->refcount < INT_MAX );
215     obj->refcount++;
216     return obj;
217 }
218
219 /* release an object (i.e. decrement its refcount) */
220 void release_object( void *ptr )
221 {
222     struct object *obj = (struct object *)ptr;
223     assert( obj->refcount );
224     if (!--obj->refcount)
225     {
226         /* if the refcount is 0, nobody can be in the wait queue */
227         assert( !obj->head );
228         assert( !obj->tail );
229         obj->ops->destroy( obj );
230         if (obj->name) free_name( obj );
231         if (obj->select != -1) remove_select_user( obj );
232         if (obj->fd != -1) close( obj->fd );
233 #ifdef DEBUG_OBJECTS
234         if (obj->next) obj->next->prev = obj->prev;
235         if (obj->prev) obj->prev->next = obj->next;
236         else first = obj->next;
237         memset( obj, 0xaa, obj->ops->size );
238 #endif
239         free( obj );
240     }
241 }
242
243 /* find an object by its name; the refcount is incremented */
244 struct object *find_object( const WCHAR *name, size_t len )
245 {
246     struct object_name *ptr;
247
248     if (!name || !len) return NULL;
249     for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
250     {
251         if (ptr->len != len) continue;
252         if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
253     }
254     return NULL;
255 }
256
257 /* functions for unimplemented/default object operations */
258
259 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
260 {
261     set_error( STATUS_OBJECT_TYPE_MISMATCH );
262     return 0;
263 }
264
265 int no_satisfied( struct object *obj, struct thread *thread )
266 {
267     return 0;  /* not abandoned */
268 }
269
270 int no_get_fd( struct object *obj )
271 {
272     set_error( STATUS_OBJECT_TYPE_MISMATCH );
273     return -1;
274 }
275
276 int no_flush( struct object *obj )
277 {
278     set_error( STATUS_OBJECT_TYPE_MISMATCH );
279     return 0;
280 }
281
282 int no_get_file_info( struct object *obj, struct get_file_info_reply *info, int *flags )
283 {
284     set_error( STATUS_OBJECT_TYPE_MISMATCH );
285     *flags = 0;
286     return FD_TYPE_INVALID;
287 }
288
289 void no_destroy( struct object *obj )
290 {
291 }
292
293 /* default add_queue() routine for objects that poll() on an fd */
294 int default_poll_add_queue( struct object *obj, struct wait_queue_entry *entry )
295 {
296     if (!obj->head)  /* first on the queue */
297         set_select_events( obj, obj->ops->get_poll_events( obj ) );
298     add_queue( obj, entry );
299     return 1;
300 }
301
302 /* default remove_queue() routine for objects that poll() on an fd */
303 void default_poll_remove_queue( struct object *obj, struct wait_queue_entry *entry )
304 {
305     grab_object(obj);
306     remove_queue( obj, entry );
307     if (!obj->head)  /* last on the queue is gone */
308         set_select_events( obj, 0 );
309     release_object( obj );
310 }
311
312 /* default signaled() routine for objects that poll() on an fd */
313 int default_poll_signaled( struct object *obj, struct thread *thread )
314 {
315     int events = obj->ops->get_poll_events( obj );
316
317     if (check_select_events( obj->fd, events ))
318     {
319         /* stop waiting on select() if we are signaled */
320         set_select_events( obj, 0 );
321         return 1;
322     }
323     /* restart waiting on select() if we are no longer signaled */
324     if (obj->head) set_select_events( obj, events );
325     return 0;
326 }
327
328 /* default handler for poll() events */
329 void default_poll_event( struct object *obj, int event )
330 {
331     /* an error occurred, stop polling this fd to avoid busy-looping */
332     if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
333     wake_up( obj, 0 );
334 }