Merged the two serializer and unserializer functions into one, cleaned
[wine.git] / server / object.c
blob2539e21610391bf166df402fbf49834bfcf9012a
1 /*
2 * Server-side objects
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "thread.h"
32 #include "unicode.h"
35 struct object_name
37 struct object_name *next;
38 struct object_name *prev;
39 struct object *obj;
40 size_t len;
41 WCHAR name[1];
44 #define NAME_HASH_SIZE 37
46 static struct object_name *names[NAME_HASH_SIZE];
48 #ifdef DEBUG_OBJECTS
49 static struct object *first;
51 void dump_objects(void)
53 struct object *ptr = first;
54 while (ptr)
56 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
57 ptr->ops->dump( ptr, 1 );
58 ptr = ptr->next;
61 #endif
63 /*****************************************************************/
65 /* malloc replacement */
66 void *mem_alloc( size_t size )
68 void *ptr = malloc( size );
69 if (ptr) memset( ptr, 0x55, size );
70 else set_error( STATUS_NO_MEMORY );
71 return ptr;
74 /* duplicate a block of memory */
75 void *memdup( const void *data, size_t len )
77 void *ptr = malloc( len );
78 if (ptr) memcpy( ptr, data, len );
79 else set_error( STATUS_NO_MEMORY );
80 return ptr;
84 /*****************************************************************/
86 static int get_name_hash( const WCHAR *name, size_t len )
88 WCHAR hash = 0;
89 len /= sizeof(WCHAR);
90 while (len--) hash ^= *name++;
91 return hash % NAME_HASH_SIZE;
94 /* allocate a name for an object */
95 static struct object_name *alloc_name( const WCHAR *name, size_t len )
97 struct object_name *ptr;
99 if ((ptr = mem_alloc( sizeof(*ptr) + len - sizeof(ptr->name) )))
101 ptr->len = len;
102 memcpy( ptr->name, name, len );
104 return ptr;
107 /* free the name of an object */
108 static void free_name( struct object *obj )
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
115 int hash;
116 for (hash = 0; hash < NAME_HASH_SIZE; hash++)
117 if (names[hash] == ptr)
119 names[hash] = ptr->next;
120 break;
123 free( ptr );
126 /* set the name of an existing object */
127 static void set_object_name( struct object *obj, struct object_name *ptr )
129 int hash = get_name_hash( ptr->name, ptr->len );
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;
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 )
143 struct object *obj = mem_alloc( ops->size );
144 if (obj)
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))
155 close( fd );
156 free( obj );
157 return NULL;
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;
166 if (fd != -1) close( fd );
167 return NULL;
170 void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
172 struct object *obj;
173 struct object_name *name_ptr;
175 if (!name || !len) return alloc_object( ops, -1 );
176 if (!(name_ptr = alloc_name( name, len ))) return NULL;
178 if ((obj = find_object( name_ptr->name, name_ptr->len )))
180 free( name_ptr ); /* we no longer need it */
181 if (obj->ops == ops)
183 set_error( STATUS_OBJECT_NAME_COLLISION );
184 return obj;
186 set_error( STATUS_OBJECT_TYPE_MISMATCH );
187 return NULL;
189 if ((obj = alloc_object( ops, -1 )))
191 set_object_name( obj, name_ptr );
192 clear_error();
194 else free( name_ptr );
195 return obj;
198 /* dump the name of an object to stderr */
199 void dump_object_name( struct object *obj )
201 if (!obj->name) fprintf( stderr, "name=\"\"" );
202 else
204 fprintf( stderr, "name=L\"" );
205 dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
206 fputc( '\"', stderr );
210 /* grab an object (i.e. increment its refcount) and return the object */
211 struct object *grab_object( void *ptr )
213 struct object *obj = (struct object *)ptr;
214 assert( obj->refcount < INT_MAX );
215 obj->refcount++;
216 return obj;
219 /* release an object (i.e. decrement its refcount) */
220 void release_object( void *ptr )
222 struct object *obj = (struct object *)ptr;
223 assert( obj->refcount );
224 if (!--obj->refcount)
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 );
243 /* find an object by its name; the refcount is incremented */
244 struct object *find_object( const WCHAR *name, size_t len )
246 struct object_name *ptr;
248 if (!name || !len) return NULL;
249 for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
251 if (ptr->len != len) continue;
252 if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
254 return NULL;
257 /* functions for unimplemented/default object operations */
259 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
261 set_error( STATUS_OBJECT_TYPE_MISMATCH );
262 return 0;
265 int no_satisfied( struct object *obj, struct thread *thread )
267 return 0; /* not abandoned */
270 int no_get_fd( struct object *obj )
272 set_error( STATUS_OBJECT_TYPE_MISMATCH );
273 return -1;
276 int no_flush( struct object *obj )
278 set_error( STATUS_OBJECT_TYPE_MISMATCH );
279 return 0;
282 int no_get_file_info( struct object *obj, struct get_file_info_reply *info, int *flags )
284 set_error( STATUS_OBJECT_TYPE_MISMATCH );
285 *flags = 0;
286 return FD_TYPE_INVALID;
289 void no_destroy( struct object *obj )
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 )
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;
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 )
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 );
312 /* default signaled() routine for objects that poll() on an fd */
313 int default_poll_signaled( struct object *obj, struct thread *thread )
315 int events = obj->ops->get_poll_events( obj );
317 if (check_select_events( obj->fd, events ))
319 /* stop waiting on select() if we are signaled */
320 set_select_events( obj, 0 );
321 return 1;
323 /* restart waiting on select() if we are no longer signaled */
324 if (obj->head) set_select_events( obj, events );
325 return 0;
328 /* default handler for poll() events */
329 void default_poll_event( struct object *obj, int event )
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 );