COMDLG32_DllEntryPoint,COMDLG32_SetCommDlgExtendedError: delay
[wine/multimedia.git] / server / object.c
blob4ade00630237b9aa50377ea870523c22d043facc
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 "thread.h"
16 #include "unicode.h"
19 struct object_name
21 struct object_name *next;
22 struct object_name *prev;
23 struct object *obj;
24 size_t len;
25 WCHAR name[1];
28 #define NAME_HASH_SIZE 37
30 static struct object_name *names[NAME_HASH_SIZE];
32 #ifdef DEBUG_OBJECTS
33 static struct object *first;
35 void dump_objects(void)
37 struct object *ptr = first;
38 while (ptr)
40 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
41 ptr->ops->dump( ptr, 1 );
42 ptr = ptr->next;
45 #endif
47 /*****************************************************************/
49 /* malloc replacement */
50 void *mem_alloc( size_t size )
52 void *ptr = malloc( size );
53 if (ptr) memset( ptr, 0x55, size );
54 else if (current) set_error( STATUS_NO_MEMORY );
55 return ptr;
58 /* duplicate a block of memory */
59 void *memdup( const void *data, size_t len )
61 void *ptr = mem_alloc( len );
62 if (ptr) memcpy( ptr, data, len );
63 return ptr;
67 /*****************************************************************/
69 static int get_name_hash( const WCHAR *name, size_t len )
71 WCHAR hash = 0;
72 while (len--) hash ^= *name++;
73 return hash % NAME_HASH_SIZE;
76 /* allocate a name for an object */
77 static struct object_name *alloc_name( const WCHAR *name, size_t len )
79 struct object_name *ptr;
81 if ((ptr = mem_alloc( sizeof(*ptr) + len * sizeof(ptr->name[0]) )))
83 ptr->len = len;
84 memcpy( ptr->name, name, len * sizeof(ptr->name[0]) );
85 ptr->name[len] = 0;
87 return ptr;
90 /* free the name of an object */
91 static void free_name( struct object *obj )
93 struct object_name *ptr = obj->name;
94 if (ptr->next) ptr->next->prev = ptr->prev;
95 if (ptr->prev) ptr->prev->next = ptr->next;
96 else
98 int hash;
99 for (hash = 0; hash < NAME_HASH_SIZE; hash++)
100 if (names[hash] == ptr)
102 names[hash] = ptr->next;
103 break;
106 free( ptr );
109 /* set the name of an existing object */
110 static void set_object_name( struct object *obj, struct object_name *ptr )
112 int hash = get_name_hash( ptr->name, ptr->len );
114 if ((ptr->next = names[hash]) != NULL) ptr->next->prev = ptr;
115 ptr->obj = obj;
116 ptr->prev = NULL;
117 names[hash] = ptr;
118 assert( !obj->name );
119 obj->name = ptr;
122 /* allocate and initialize an object */
123 /* if the function fails the fd is closed */
124 void *alloc_object( const struct object_ops *ops, int fd )
126 struct object *obj = mem_alloc( ops->size );
127 if (obj)
129 obj->refcount = 1;
130 obj->fd = fd;
131 obj->select = -1;
132 obj->ops = ops;
133 obj->head = NULL;
134 obj->tail = NULL;
135 obj->name = NULL;
136 if ((fd != -1) && (add_select_user( obj ) == -1))
138 close( fd );
139 free( obj );
140 return NULL;
142 #ifdef DEBUG_OBJECTS
143 obj->prev = NULL;
144 if ((obj->next = first) != NULL) obj->next->prev = obj;
145 first = obj;
146 #endif
147 return obj;
149 if (fd != -1) close( fd );
150 return NULL;
153 void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
155 struct object *obj;
156 struct object_name *name_ptr;
158 if (!name || !len) return alloc_object( ops, -1 );
159 if (!(name_ptr = alloc_name( name, len ))) return NULL;
161 if ((obj = find_object( name_ptr->name, name_ptr->len )))
163 free( name_ptr ); /* we no longer need it */
164 if (obj->ops == ops)
166 set_error( STATUS_OBJECT_NAME_COLLISION );
167 return obj;
169 set_error( STATUS_OBJECT_TYPE_MISMATCH );
170 return NULL;
172 if ((obj = alloc_object( ops, -1 )))
174 set_object_name( obj, name_ptr );
175 clear_error();
177 else free( name_ptr );
178 return obj;
181 /* dump the name of an object to stderr */
182 void dump_object_name( struct object *obj )
184 if (!obj->name) fprintf( stderr, "name=\"\"" );
185 else
187 fprintf( stderr, "name=L\"" );
188 dump_strW( obj->name->name, strlenW(obj->name->name), stderr, "\"\"" );
189 fputc( '\"', stderr );
193 /* grab an object (i.e. increment its refcount) and return the object */
194 struct object *grab_object( void *ptr )
196 struct object *obj = (struct object *)ptr;
197 assert( obj->refcount < INT_MAX );
198 obj->refcount++;
199 return obj;
202 /* release an object (i.e. decrement its refcount) */
203 void release_object( void *ptr )
205 struct object *obj = (struct object *)ptr;
206 assert( obj->refcount );
207 if (!--obj->refcount)
209 /* if the refcount is 0, nobody can be in the wait queue */
210 assert( !obj->head );
211 assert( !obj->tail );
212 obj->ops->destroy( obj );
213 if (obj->name) free_name( obj );
214 if (obj->select != -1) remove_select_user( obj );
215 if (obj->fd != -1) close( obj->fd );
216 #ifdef DEBUG_OBJECTS
217 if (obj->next) obj->next->prev = obj->prev;
218 if (obj->prev) obj->prev->next = obj->next;
219 else first = obj->next;
220 memset( obj, 0xaa, obj->ops->size );
221 #endif
222 free( obj );
226 /* find an object by its name; the refcount is incremented */
227 struct object *find_object( const WCHAR *name, size_t len )
229 struct object_name *ptr;
230 if (!name || !len) return NULL;
231 for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
233 if (ptr->len != len) continue;
234 if (!memcmp( ptr->name, name, len*sizeof(WCHAR) )) return grab_object( ptr->obj );
236 return NULL;
239 /* functions for unimplemented/default object operations */
241 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
243 set_error( STATUS_OBJECT_TYPE_MISMATCH );
244 return 0;
247 int no_satisfied( struct object *obj, struct thread *thread )
249 return 0; /* not abandoned */
252 int no_read_fd( struct object *obj )
254 set_error( STATUS_OBJECT_TYPE_MISMATCH );
255 return -1;
258 int no_write_fd( struct object *obj )
260 set_error( STATUS_OBJECT_TYPE_MISMATCH );
261 return -1;
264 int no_flush( struct object *obj )
266 set_error( STATUS_OBJECT_TYPE_MISMATCH );
267 return 0;
270 int no_get_file_info( struct object *obj, struct get_file_info_request *info )
272 set_error( STATUS_OBJECT_TYPE_MISMATCH );
273 return 0;
276 void no_destroy( struct object *obj )
280 /* default add_queue() routine for objects that poll() on an fd */
281 int default_poll_add_queue( struct object *obj, struct wait_queue_entry *entry )
283 if (!obj->head) /* first on the queue */
284 set_select_events( obj, obj->ops->get_poll_events( obj ) );
285 add_queue( obj, entry );
286 return 1;
289 /* default remove_queue() routine for objects that poll() on an fd */
290 void default_poll_remove_queue( struct object *obj, struct wait_queue_entry *entry )
292 grab_object(obj);
293 remove_queue( obj, entry );
294 if (!obj->head) /* last on the queue is gone */
295 set_select_events( obj, 0 );
296 release_object( obj );
299 /* default signaled() routine for objects that poll() on an fd */
300 int default_poll_signaled( struct object *obj, struct thread *thread )
302 int events = obj->ops->get_poll_events( obj );
304 if (check_select_events( obj->fd, events ))
306 /* stop waiting on select() if we are signaled */
307 set_select_events( obj, 0 );
308 return 1;
310 /* restart waiting on select() if we are no longer signaled */
311 if (obj->head) set_select_events( obj, events );
312 return 0;
315 /* default handler for poll() events */
316 void default_poll_event( struct object *obj, int event )
318 /* an error occurred, stop polling this fd to avoid busy-looping */
319 if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
320 wake_up( obj, 0 );