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
22 #include "wine/port.h"
37 struct object_name *next;
38 struct object_name *prev;
44 #define NAME_HASH_SIZE 37
46 static struct object_name *names[NAME_HASH_SIZE];
49 static struct object *first;
51 void dump_objects(void)
53 struct object *ptr = first;
56 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
57 ptr->ops->dump( ptr, 1 );
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 );
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 );
84 /*****************************************************************/
86 static int get_name_hash( const WCHAR *name, size_t len )
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) )))
102 memcpy( ptr->name, name, len );
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;
116 for (hash = 0; hash < NAME_HASH_SIZE; hash++)
117 if (names[hash] == ptr)
119 names[hash] = ptr->next;
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;
135 assert( !obj->name );
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 );
153 if ((fd != -1) && (add_select_user( obj ) == -1))
161 if ((obj->next = first) != NULL) obj->next->prev = obj;
166 if (fd != -1) close( fd );
170 void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
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 */
183 set_error( STATUS_OBJECT_NAME_COLLISION );
186 set_error( STATUS_OBJECT_TYPE_MISMATCH );
189 if ((obj = alloc_object( ops, -1 )))
191 set_object_name( obj, name_ptr );
194 else free( name_ptr );
198 /* dump the name of an object to stderr */
199 void dump_object_name( struct object *obj )
201 if (!obj->name) fprintf( stderr, "name=\"\"" );
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 );
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 );
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 );
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 );
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 );
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 );
276 int no_flush( struct object *obj )
278 set_error( STATUS_OBJECT_TYPE_MISMATCH );
282 int no_get_file_info( struct object *obj, struct get_file_info_reply *info, int *flags )
284 set_error( STATUS_OBJECT_TYPE_MISMATCH );
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 );
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 )
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 );
323 /* restart waiting on select() if we are no longer signaled */
324 if (obj->head) set_select_events( obj, events );
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 );