3 * These are the server equivalent of K32OBJ
5 * Copyright (C) 1998 Alexandre Julliard
22 struct object_name *next;
23 struct object_name *prev;
29 #define NAME_HASH_SIZE 37
31 static struct object_name *names[NAME_HASH_SIZE];
34 static struct object *first;
36 void dump_objects(void)
38 struct object *ptr = first;
41 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
42 ptr->ops->dump( ptr, 1 );
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 );
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 );
68 /*****************************************************************/
70 static int get_name_hash( const WCHAR *name, size_t len )
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]) )))
85 memcpy( ptr->name, name, len * sizeof(ptr->name[0]) );
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;
100 for (hash = 0; hash < NAME_HASH_SIZE; hash++)
101 if (names[hash] == ptr)
103 names[hash] = ptr->next;
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;
119 assert( !obj->name );
123 /* allocate and initialize an object */
124 void *alloc_object( const struct object_ops *ops )
126 struct object *obj = mem_alloc( ops->size );
136 if ((obj->next = first) != NULL) obj->next->prev = obj;
143 void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
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 */
156 set_error( ERROR_ALREADY_EXISTS );
159 set_error( ERROR_INVALID_HANDLE );
162 if ((obj = alloc_object( ops )))
164 set_object_name( obj, name_ptr );
167 else free( name_ptr );
171 /* dump the name of an object to stderr */
172 void dump_object_name( struct object *obj )
174 if (!obj->name) fprintf( stderr, "name=\"\"" );
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 );
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 );
204 if (obj->next) obj->next->prev = obj->prev;
205 if (obj->prev) obj->prev->next = obj->next;
206 else first = obj->next;
208 obj->ops->destroy( obj );
209 memset( obj, 0xaa, obj->ops->size );
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 );
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 );
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 );
246 int no_write_fd( struct object *obj )
248 set_error( ERROR_INVALID_HANDLE );
252 int no_flush( struct object *obj )
254 set_error( ERROR_INVALID_HANDLE );
258 int no_get_file_info( struct object *obj, struct get_file_info_request *info )
260 set_error( ERROR_INVALID_HANDLE );
264 void no_destroy( struct object *obj )
268 void default_select_event( int event, void *private )
270 struct object *obj = (struct object *)private;