Several bug fixes in save_key().
[wine/multimedia.git] / server / object.c
blob9d44965eef22743a1f70fb3bfdf6a063407c4e75
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>
14 #include "winerror.h"
15 #include "thread.h"
16 #include "unicode.h"
18 int debug_level = 0;
20 struct object_name
22 struct object_name *next;
23 struct object_name *prev;
24 struct object *obj;
25 size_t len;
26 WCHAR name[1];
29 #define NAME_HASH_SIZE 37
31 static struct object_name *names[NAME_HASH_SIZE];
33 #ifdef DEBUG_OBJECTS
34 static struct object *first;
36 void dump_objects(void)
38 struct object *ptr = first;
39 while (ptr)
41 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
42 ptr->ops->dump( ptr, 1 );
43 ptr = ptr->next;
46 #endif
48 /*****************************************************************/
50 /* malloc replacement */
51 void *mem_alloc( size_t size )
53 void *ptr = malloc( size );
54 if (ptr) memset( ptr, 0x55, size );
55 else if (current) set_error( ERROR_OUTOFMEMORY );
56 return ptr;
59 /* duplicate a block of memory */
60 void *memdup( const void *data, size_t len )
62 void *ptr = mem_alloc( len );
63 if (ptr) memcpy( ptr, data, len );
64 return ptr;
68 /*****************************************************************/
70 static int get_name_hash( const WCHAR *name, size_t len )
72 WCHAR hash = 0;
73 while (len--) hash ^= *name++;
74 return hash % NAME_HASH_SIZE;
77 /* allocate a name for an object */
78 static struct object_name *alloc_name( const WCHAR *name, size_t len )
80 struct object_name *ptr;
82 if ((ptr = mem_alloc( sizeof(*ptr) + len * sizeof(ptr->name[0]) )))
84 ptr->len = len;
85 memcpy( ptr->name, name, len * sizeof(ptr->name[0]) );
86 ptr->name[len] = 0;
88 return ptr;
91 /* free the name of an object */
92 static void free_name( struct object *obj )
94 struct object_name *ptr = obj->name;
95 if (ptr->next) ptr->next->prev = ptr->prev;
96 if (ptr->prev) ptr->prev->next = ptr->next;
97 else
99 int hash;
100 for (hash = 0; hash < NAME_HASH_SIZE; hash++)
101 if (names[hash] == ptr)
103 names[hash] = ptr->next;
104 break;
107 free( ptr );
110 /* set the name of an existing object */
111 static void set_object_name( struct object *obj, struct object_name *ptr )
113 int hash = get_name_hash( ptr->name, ptr->len );
115 if ((ptr->next = names[hash]) != NULL) ptr->next->prev = ptr;
116 ptr->obj = obj;
117 ptr->prev = NULL;
118 names[hash] = ptr;
119 assert( !obj->name );
120 obj->name = ptr;
123 /* allocate and initialize an object */
124 void *alloc_object( const struct object_ops *ops )
126 struct object *obj = mem_alloc( ops->size );
127 if (obj)
129 obj->refcount = 1;
130 obj->ops = ops;
131 obj->head = NULL;
132 obj->tail = NULL;
133 obj->name = NULL;
134 #ifdef DEBUG_OBJECTS
135 obj->prev = NULL;
136 if ((obj->next = first) != NULL) obj->next->prev = obj;
137 first = obj;
138 #endif
140 return obj;
143 void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
145 struct object *obj;
146 struct object_name *name_ptr;
148 if (!name || !len) return alloc_object( ops );
149 if (!(name_ptr = alloc_name( name, len ))) return NULL;
151 if ((obj = find_object( name_ptr->name, name_ptr->len )))
153 free( name_ptr ); /* we no longer need it */
154 if (obj->ops == ops)
156 set_error( ERROR_ALREADY_EXISTS );
157 return obj;
159 set_error( ERROR_INVALID_HANDLE );
160 return NULL;
162 if ((obj = alloc_object( ops )))
164 set_object_name( obj, name_ptr );
165 clear_error();
167 else free( name_ptr );
168 return obj;
171 /* dump the name of an object to stderr */
172 void dump_object_name( struct object *obj )
174 if (!obj->name) fprintf( stderr, "name=\"\"" );
175 else
177 fprintf( stderr, "name=L\"" );
178 dump_strW( obj->name->name, strlenW(obj->name->name), stderr, "\"\"" );
179 fputc( '\"', stderr );
183 /* grab an object (i.e. increment its refcount) and return the object */
184 struct object *grab_object( void *ptr )
186 struct object *obj = (struct object *)ptr;
187 assert( obj->refcount < INT_MAX );
188 obj->refcount++;
189 return obj;
192 /* release an object (i.e. decrement its refcount) */
193 void release_object( void *ptr )
195 struct object *obj = (struct object *)ptr;
196 assert( obj->refcount );
197 if (!--obj->refcount)
199 /* if the refcount is 0, nobody can be in the wait queue */
200 assert( !obj->head );
201 assert( !obj->tail );
202 if (obj->name) free_name( obj );
203 #ifdef DEBUG_OBJECTS
204 if (obj->next) obj->next->prev = obj->prev;
205 if (obj->prev) obj->prev->next = obj->next;
206 else first = obj->next;
207 #endif
208 obj->ops->destroy( obj );
209 memset( obj, 0xaa, obj->ops->size );
210 free( obj );
214 /* find an object by its name; the refcount is incremented */
215 struct object *find_object( const WCHAR *name, size_t len )
217 struct object_name *ptr;
218 if (!name || !len) return NULL;
219 for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
221 if (ptr->len != len) continue;
222 if (!memcmp( ptr->name, name, len*sizeof(WCHAR) )) return grab_object( ptr->obj );
224 return NULL;
227 /* functions for unimplemented object operations */
229 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
231 set_error( ERROR_INVALID_HANDLE );
232 return 0;
235 int no_satisfied( struct object *obj, struct thread *thread )
237 return 0; /* not abandoned */
240 int no_read_fd( struct object *obj )
242 set_error( ERROR_INVALID_HANDLE );
243 return -1;
246 int no_write_fd( struct object *obj )
248 set_error( ERROR_INVALID_HANDLE );
249 return -1;
252 int no_flush( struct object *obj )
254 set_error( ERROR_INVALID_HANDLE );
255 return 0;
258 int no_get_file_info( struct object *obj, struct get_file_info_request *info )
260 set_error( ERROR_INVALID_HANDLE );
261 return 0;
264 void no_destroy( struct object *obj )
268 void default_select_event( int event, void *private )
270 struct object *obj = (struct object *)private;
271 assert( obj );
272 wake_up( obj, 0 );