blob: 2539e21610391bf166df402fbf49834bfcf9012a [file] [log] [blame]
Alexandre Julliard642d3131998-07-12 19:29:36 +00001/*
2 * Server-side objects
Alexandre Julliard642d3131998-07-12 19:29:36 +00003 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Alexandre Julliard642d3131998-07-12 19:29:36 +000019 */
20
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliard642d3131998-07-12 19:29:36 +000024#include <assert.h>
Alexandre Julliard767e6f61998-08-09 12:47:43 +000025#include <limits.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000026#include <stdlib.h>
Alexandre Julliard1bdd1541999-06-04 19:47:04 +000027#include <stdio.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000028#include <string.h>
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000029#include <unistd.h>
Alexandre Julliard642d3131998-07-12 19:29:36 +000030
Alexandre Julliard43c190e1999-05-15 10:48:19 +000031#include "thread.h"
Alexandre Julliardd16319c1999-11-25 21:30:24 +000032#include "unicode.h"
Alexandre Julliard767e6f61998-08-09 12:47:43 +000033
Alexandre Julliard642d3131998-07-12 19:29:36 +000034
35struct object_name
36{
37 struct object_name *next;
Alexandre Julliard5bc78081999-06-22 17:26:53 +000038 struct object_name *prev;
Alexandre Julliard642d3131998-07-12 19:29:36 +000039 struct object *obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +000040 size_t len;
Alexandre Julliardd16319c1999-11-25 21:30:24 +000041 WCHAR name[1];
Alexandre Julliard642d3131998-07-12 19:29:36 +000042};
43
44#define NAME_HASH_SIZE 37
45
46static struct object_name *names[NAME_HASH_SIZE];
47
Alexandre Julliard1bdd1541999-06-04 19:47:04 +000048#ifdef DEBUG_OBJECTS
49static struct object *first;
50
51void dump_objects(void)
52{
53 struct object *ptr = first;
54 while (ptr)
55 {
56 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
57 ptr->ops->dump( ptr, 1 );
58 ptr = ptr->next;
59 }
60}
61#endif
62
Alexandre Julliard642d3131998-07-12 19:29:36 +000063/*****************************************************************/
64
Alexandre Julliard5bc78081999-06-22 17:26:53 +000065/* malloc replacement */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000066void *mem_alloc( size_t size )
67{
68 void *ptr = malloc( size );
69 if (ptr) memset( ptr, 0x55, size );
Alexandre Julliard6c8d9172000-08-26 04:40:07 +000070 else set_error( STATUS_NO_MEMORY );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000071 return ptr;
72}
73
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000074/* duplicate a block of memory */
75void *memdup( const void *data, size_t len )
76{
Alexandre Julliardba3e2ab2000-05-03 17:45:34 +000077 void *ptr = malloc( len );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000078 if (ptr) memcpy( ptr, data, len );
Alexandre Julliard6c8d9172000-08-26 04:40:07 +000079 else set_error( STATUS_NO_MEMORY );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000080 return ptr;
81}
82
83
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000084/*****************************************************************/
85
Alexandre Julliardd16319c1999-11-25 21:30:24 +000086static int get_name_hash( const WCHAR *name, size_t len )
Alexandre Julliard642d3131998-07-12 19:29:36 +000087{
Alexandre Julliardd16319c1999-11-25 21:30:24 +000088 WCHAR hash = 0;
Alexandre Julliard9c2370b2000-08-30 00:00:48 +000089 len /= sizeof(WCHAR);
Alexandre Julliard5bc78081999-06-22 17:26:53 +000090 while (len--) hash ^= *name++;
Alexandre Julliard642d3131998-07-12 19:29:36 +000091 return hash % NAME_HASH_SIZE;
92}
93
Alexandre Julliard5bc78081999-06-22 17:26:53 +000094/* allocate a name for an object */
Alexandre Julliardd16319c1999-11-25 21:30:24 +000095static struct object_name *alloc_name( const WCHAR *name, size_t len )
Alexandre Julliard642d3131998-07-12 19:29:36 +000096{
97 struct object_name *ptr;
Alexandre Julliard642d3131998-07-12 19:29:36 +000098
Alexandre Julliard9c2370b2000-08-30 00:00:48 +000099 if ((ptr = mem_alloc( sizeof(*ptr) + len - sizeof(ptr->name) )))
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000100 {
101 ptr->len = len;
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000102 memcpy( ptr->name, name, len );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000103 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000104 return ptr;
105}
106
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000107/* free the name of an object */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000108static void free_name( struct object *obj )
109{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000110 struct object_name *ptr = obj->name;
111 if (ptr->next) ptr->next->prev = ptr->prev;
112 if (ptr->prev) ptr->prev->next = ptr->next;
113 else
114 {
115 int hash;
116 for (hash = 0; hash < NAME_HASH_SIZE; hash++)
117 if (names[hash] == ptr)
118 {
119 names[hash] = ptr->next;
120 break;
121 }
122 }
123 free( ptr );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000124}
125
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000126/* set the name of an existing object */
127static void set_object_name( struct object *obj, struct object_name *ptr )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000128{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000129 int hash = get_name_hash( ptr->name, ptr->len );
130
131 if ((ptr->next = names[hash]) != NULL) ptr->next->prev = ptr;
132 ptr->obj = obj;
133 ptr->prev = NULL;
134 names[hash] = ptr;
135 assert( !obj->name );
136 obj->name = ptr;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000137}
138
Alexandre Julliard1bdd1541999-06-04 19:47:04 +0000139/* allocate and initialize an object */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000140/* if the function fails the fd is closed */
141void *alloc_object( const struct object_ops *ops, int fd )
Alexandre Julliard1bdd1541999-06-04 19:47:04 +0000142{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000143 struct object *obj = mem_alloc( ops->size );
144 if (obj)
145 {
146 obj->refcount = 1;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000147 obj->fd = fd;
148 obj->select = -1;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000149 obj->ops = ops;
150 obj->head = NULL;
151 obj->tail = NULL;
152 obj->name = NULL;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000153 if ((fd != -1) && (add_select_user( obj ) == -1))
154 {
155 close( fd );
156 free( obj );
157 return NULL;
158 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000159#ifdef DEBUG_OBJECTS
160 obj->prev = NULL;
161 if ((obj->next = first) != NULL) obj->next->prev = obj;
162 first = obj;
163#endif
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000164 return obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000165 }
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000166 if (fd != -1) close( fd );
167 return NULL;
Alexandre Julliard1bdd1541999-06-04 19:47:04 +0000168}
169
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000170void *create_named_object( const struct object_ops *ops, const WCHAR *name, size_t len )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000171{
172 struct object *obj;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000173 struct object_name *name_ptr;
174
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000175 if (!name || !len) return alloc_object( ops, -1 );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000176 if (!(name_ptr = alloc_name( name, len ))) return NULL;
177
178 if ((obj = find_object( name_ptr->name, name_ptr->len )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000179 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000180 free( name_ptr ); /* we no longer need it */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000181 if (obj->ops == ops)
182 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000183 set_error( STATUS_OBJECT_NAME_COLLISION );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000184 return obj;
185 }
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000186 set_error( STATUS_OBJECT_TYPE_MISMATCH );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000187 return NULL;
188 }
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000189 if ((obj = alloc_object( ops, -1 )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000190 {
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000191 set_object_name( obj, name_ptr );
192 clear_error();
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000193 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000194 else free( name_ptr );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000195 return obj;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000196}
197
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000198/* dump the name of an object to stderr */
199void dump_object_name( struct object *obj )
Alexandre Julliard05625391999-01-03 11:55:56 +0000200{
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000201 if (!obj->name) fprintf( stderr, "name=\"\"" );
202 else
203 {
204 fprintf( stderr, "name=L\"" );
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000205 dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000206 fputc( '\"', stderr );
207 }
Alexandre Julliard05625391999-01-03 11:55:56 +0000208}
209
Alexandre Julliard767e6f61998-08-09 12:47:43 +0000210/* grab an object (i.e. increment its refcount) and return the object */
211struct object *grab_object( void *ptr )
212{
213 struct object *obj = (struct object *)ptr;
214 assert( obj->refcount < INT_MAX );
215 obj->refcount++;
216 return obj;
217}
218
219/* release an object (i.e. decrement its refcount) */
Alexandre Julliard642d3131998-07-12 19:29:36 +0000220void release_object( void *ptr )
221{
222 struct object *obj = (struct object *)ptr;
223 assert( obj->refcount );
224 if (!--obj->refcount)
225 {
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000226 /* if the refcount is 0, nobody can be in the wait queue */
227 assert( !obj->head );
228 assert( !obj->tail );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000229 obj->ops->destroy( obj );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000230 if (obj->name) free_name( obj );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000231 if (obj->select != -1) remove_select_user( obj );
232 if (obj->fd != -1) close( obj->fd );
Alexandre Julliard1bdd1541999-06-04 19:47:04 +0000233#ifdef DEBUG_OBJECTS
234 if (obj->next) obj->next->prev = obj->prev;
235 if (obj->prev) obj->prev->next = obj->next;
236 else first = obj->next;
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000237 memset( obj, 0xaa, obj->ops->size );
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000238#endif
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000239 free( obj );
Alexandre Julliard642d3131998-07-12 19:29:36 +0000240 }
241}
242
243/* find an object by its name; the refcount is incremented */
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000244struct object *find_object( const WCHAR *name, size_t len )
Alexandre Julliard642d3131998-07-12 19:29:36 +0000245{
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000246 struct object_name *ptr;
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000247
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000248 if (!name || !len) return NULL;
249 for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
250 {
251 if (ptr->len != len) continue;
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000252 if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000253 }
254 return NULL;
Alexandre Julliard642d3131998-07-12 19:29:36 +0000255}
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000256
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000257/* functions for unimplemented/default object operations */
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000258
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000259int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
260{
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000261 set_error( STATUS_OBJECT_TYPE_MISMATCH );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000262 return 0;
263}
264
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000265int no_satisfied( struct object *obj, struct thread *thread )
266{
267 return 0; /* not abandoned */
268}
269
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000270int no_get_fd( struct object *obj )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000271{
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000272 set_error( STATUS_OBJECT_TYPE_MISMATCH );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000273 return -1;
274}
275
276int no_flush( struct object *obj )
277{
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000278 set_error( STATUS_OBJECT_TYPE_MISMATCH );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000279 return 0;
280}
281
Martin Wilck88cd32b2002-01-09 20:30:51 +0000282int no_get_file_info( struct object *obj, struct get_file_info_reply *info, int *flags )
Alexandre Julliard05625391999-01-03 11:55:56 +0000283{
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000284 set_error( STATUS_OBJECT_TYPE_MISMATCH );
Martin Wilck88cd32b2002-01-09 20:30:51 +0000285 *flags = 0;
Mike McCormackff58be52001-10-04 16:18:15 +0000286 return FD_TYPE_INVALID;
Alexandre Julliard05625391999-01-03 11:55:56 +0000287}
288
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000289void no_destroy( struct object *obj )
290{
291}
292
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000293/* default add_queue() routine for objects that poll() on an fd */
294int default_poll_add_queue( struct object *obj, struct wait_queue_entry *entry )
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000295{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000296 if (!obj->head) /* first on the queue */
297 set_select_events( obj, obj->ops->get_poll_events( obj ) );
298 add_queue( obj, entry );
299 return 1;
300}
301
302/* default remove_queue() routine for objects that poll() on an fd */
303void default_poll_remove_queue( struct object *obj, struct wait_queue_entry *entry )
304{
305 grab_object(obj);
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 );
310}
311
312/* default signaled() routine for objects that poll() on an fd */
313int default_poll_signaled( struct object *obj, struct thread *thread )
314{
315 int events = obj->ops->get_poll_events( obj );
316
317 if (check_select_events( obj->fd, events ))
318 {
319 /* stop waiting on select() if we are signaled */
320 set_select_events( obj, 0 );
321 return 1;
322 }
323 /* restart waiting on select() if we are no longer signaled */
324 if (obj->head) set_select_events( obj, events );
325 return 0;
326}
327
328/* default handler for poll() events */
329void default_poll_event( struct object *obj, int event )
330{
331 /* an error occurred, stop polling this fd to avoid busy-looping */
332 if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
Alexandre Julliardaa0ebd01998-12-30 12:06:45 +0000333 wake_up( obj, 0 );
334}