If there is enough space in the buffer and the type is REG_SZ and the
[wine.git] / server / object.c
blob28cfe67e91fe4bd3fd483967d384a9a130c2d67f
1 /*
2 * Server-side objects
3 * These are the server equivalent of K32OBJ
5 * Copyright (C) 1998 Alexandre Julliard
6 */
8 #include <assert.h>
9 #include <limits.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
15 #include "winerror.h"
16 #include "thread.h"
17 #include "unicode.h"
19 int debug_level = 0;
21 struct object_name
23 struct object_name *next;
24 struct object_name *prev;
25 struct object *obj;
26 size_t len;
27 WCHAR name[1];
30 #define NAME_HASH_SIZE 37
32 static struct object_name *names[NAME_HASH_SIZE];
34 #ifdef DEBUG_OBJECTS
35 static struct object *first;
37 void dump_objects(void)
39 struct object *ptr = first;
40 while (ptr)
42 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
43 ptr->ops->dump( ptr, 1 );
44 ptr = ptr->next;
47 #endif
49 /*****************************************************************/
51 /* malloc replacement */
52 void *mem_alloc( size_t size )
54 void *ptr = malloc( size );
55 if (ptr) memset( ptr, 0x55, size );
56 else if (current) set_error( ERROR_OUTOFMEMORY );
57 return ptr;
60 /* duplicate a block of memory */
61 void *memdup( const void *data, size_t len )
63 void *ptr = mem_alloc( len );
64 if (ptr) memcpy( ptr, data, len );
65 return ptr;
69 /*****************************************************************/
71 static int get_name_hash( const WCHAR *name, size_t len )
73 WCHAR hash = 0;
74 while (len--) hash ^= *name++;
75 return hash % NAME_HASH_SIZE;
78 /* allocate a name for an object */
79 static struct object_name *alloc_name( const WCHAR *name, size_t len )
81 struct object_name *ptr;
83 if ((ptr = mem_alloc( sizeof(*ptr) + len * sizeof(ptr->name[0]) )))
85 ptr->len = len;
86 memcpy( ptr->name, name, len * sizeof(ptr->name[0]) );
87 ptr->name[len] = 0;
89 return ptr;
92 /* free the name of an object */
93 static void free_name( struct object *obj )
95 struct object_name *ptr = obj->name;
96 if (ptr->next) ptr->next->prev = ptr->prev;
97 if (ptr->prev) ptr->prev->next = ptr->next;
98 else
100 int hash;
101 for (hash = 0; hash < NAME_HASH_SIZE; hash++)
102 if (names[hash] == ptr)
104 names[hash] = ptr->next;
105 break;
108 free( ptr );
111 /* set the name of an existing object */
112 static void set_object_name( struct object *obj, struct object_name *ptr )
114 int hash = get_name_hash( ptr->name, ptr->len );
116 if ((ptr->next = names[hash]) != NULL) ptr->next->prev = ptr;
117 ptr->obj = obj;
118 ptr->prev = NULL;
119 names[hash] = ptr;
120 assert( !obj->name );
121 obj->name = ptr;
124 /* allocate and initialize an object */
125 /* if the function fails the fd is closed */
126 void *alloc_object( const struct object_ops *ops, int fd )
128 struct object *obj = mem_alloc( ops->size );
129 if (obj)
131 obj->refcount = 1;
132 obj->fd = fd;
133 obj->select = -1;
134 obj->ops = ops;
135 obj->head = NULL;
136 obj->tail = NULL;
137 obj->name = NULL;
138 if ((fd != -1) && (add_select_user( obj ) == -1))
140 close( fd );
141 free( obj );
142 return NULL;
144 #ifdef DEBUG_OBJECTS
145 obj->prev = NULL;
146 if ((obj->next = first) != NULL) obj->next->prev = obj;
147 first = obj;
148 #endif
149 return obj;
151 if (fd != -1) close( fd );
152 return NULL;
155 void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
157 struct object *obj;
158 struct object_name *name_ptr;
160 if (!name || !len) return alloc_object( ops, -1 );
161 if (!(name_ptr = alloc_name( name, len ))) return NULL;
163 if ((obj = find_object( name_ptr->name, name_ptr->len )))
165 free( name_ptr ); /* we no longer need it */
166 if (obj->ops == ops)
168 set_error( ERROR_ALREADY_EXISTS );
169 return obj;
171 set_error( ERROR_INVALID_HANDLE );
172 return NULL;
174 if ((obj = alloc_object( ops, -1 )))
176 set_object_name( obj, name_ptr );
177 clear_error();
179 else free( name_ptr );
180 return obj;
183 /* dump the name of an object to stderr */
184 void dump_object_name( struct object *obj )
186 if (!obj->name) fprintf( stderr, "name=\"\"" );
187 else
189 fprintf( stderr, "name=L\"" );
190 dump_strW( obj->name->name, strlenW(obj->name->name), stderr, "\"\"" );
191 fputc( '\"', stderr );
195 /* grab an object (i.e. increment its refcount) and return the object */
196 struct object *grab_object( void *ptr )
198 struct object *obj = (struct object *)ptr;
199 assert( obj->refcount < INT_MAX );
200 obj->refcount++;
201 return obj;
204 /* release an object (i.e. decrement its refcount) */
205 void release_object( void *ptr )
207 struct object *obj = (struct object *)ptr;
208 assert( obj->refcount );
209 if (!--obj->refcount)
211 /* if the refcount is 0, nobody can be in the wait queue */
212 assert( !obj->head );
213 assert( !obj->tail );
214 obj->ops->destroy( obj );
215 if (obj->name) free_name( obj );
216 if (obj->select != -1) remove_select_user( obj );
217 if (obj->fd != -1) close( obj->fd );
218 #ifdef DEBUG_OBJECTS
219 if (obj->next) obj->next->prev = obj->prev;
220 if (obj->prev) obj->prev->next = obj->next;
221 else first = obj->next;
222 memset( obj, 0xaa, obj->ops->size );
223 #endif
224 free( obj );
228 /* find an object by its name; the refcount is incremented */
229 struct object *find_object( const WCHAR *name, size_t len )
231 struct object_name *ptr;
232 if (!name || !len) return NULL;
233 for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
235 if (ptr->len != len) continue;
236 if (!memcmp( ptr->name, name, len*sizeof(WCHAR) )) return grab_object( ptr->obj );
238 return NULL;
241 /* functions for unimplemented/default object operations */
243 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
245 set_error( ERROR_INVALID_HANDLE );
246 return 0;
249 int no_satisfied( struct object *obj, struct thread *thread )
251 return 0; /* not abandoned */
254 int no_read_fd( struct object *obj )
256 set_error( ERROR_INVALID_HANDLE );
257 return -1;
260 int no_write_fd( struct object *obj )
262 set_error( ERROR_INVALID_HANDLE );
263 return -1;
266 int no_flush( struct object *obj )
268 set_error( ERROR_INVALID_HANDLE );
269 return 0;
272 int no_get_file_info( struct object *obj, struct get_file_info_request *info )
274 set_error( ERROR_INVALID_HANDLE );
275 return 0;
278 void no_destroy( struct object *obj )
282 /* default add_queue() routine for objects that poll() on an fd */
283 int default_poll_add_queue( struct object *obj, struct wait_queue_entry *entry )
285 if (!obj->head) /* first on the queue */
286 set_select_events( obj, obj->ops->get_poll_events( obj ) );
287 add_queue( obj, entry );
288 return 1;
291 /* default remove_queue() routine for objects that poll() on an fd */
292 void default_poll_remove_queue( struct object *obj, struct wait_queue_entry *entry )
294 grab_object(obj);
295 remove_queue( obj, entry );
296 if (!obj->head) /* last on the queue is gone */
297 set_select_events( obj, 0 );
298 release_object( obj );
301 /* default signaled() routine for objects that poll() on an fd */
302 int default_poll_signaled( struct object *obj, struct thread *thread )
304 int events = obj->ops->get_poll_events( obj );
306 if (check_select_events( obj->fd, events ))
308 /* stop waiting on select() if we are signaled */
309 set_select_events( obj, 0 );
310 return 1;
312 /* restart waiting on select() if we are no longer signaled */
313 if (obj->head) set_select_events( obj, events );
314 return 0;
317 /* default handler for poll() events */
318 void default_poll_event( struct object *obj, int event )
320 /* an error occurred, stop polling this fd to avoid busy-looping */
321 if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
322 wake_up( obj, 0 );