Started moving functions that deal with Unix file descriptors to a
[wine] / server / object.c
1 /*
2  * Server-side objects
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "file.h"
32 #include "thread.h"
33 #include "unicode.h"
34 #include "list.h"
35
36
37 struct object_name
38 {
39     struct list         entry;    /* entry in the hash list */
40     struct object      *obj;
41     size_t              len;
42     WCHAR               name[1];
43 };
44
45 struct namespace
46 {
47     unsigned int        hash_size;       /* size of hash table */
48     int                 case_sensitive;  /* are names case sensitive? */
49     struct list         names[1];        /* array of hash entry lists */
50 };
51
52
53 #ifdef DEBUG_OBJECTS
54 static struct list object_list = LIST_INIT(object_list);
55
56 void dump_objects(void)
57 {
58     struct list *p;
59
60     LIST_FOR_EACH( p, &object_list )
61     {
62         struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
63         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
64         ptr->ops->dump( ptr, 1 );
65     }
66 }
67 #endif
68
69 /*****************************************************************/
70
71 /* malloc replacement */
72 void *mem_alloc( size_t size )
73 {
74     void *ptr = malloc( size );
75     if (ptr) memset( ptr, 0x55, size );
76     else set_error( STATUS_NO_MEMORY );
77     return ptr;
78 }
79
80 /* duplicate a block of memory */
81 void *memdup( const void *data, size_t len )
82 {
83     void *ptr = malloc( len );
84     if (ptr) memcpy( ptr, data, len );
85     else set_error( STATUS_NO_MEMORY );
86     return ptr;
87 }
88
89
90 /*****************************************************************/
91
92 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, size_t len )
93 {
94     WCHAR hash = 0;
95     len /= sizeof(WCHAR);
96     if (namespace->case_sensitive) while (len--) hash ^= *name++;
97     else while (len--) hash ^= tolowerW(*name++);
98     return hash % namespace->hash_size;
99 }
100
101 /* allocate a name for an object */
102 static struct object_name *alloc_name( const WCHAR *name, size_t len )
103 {
104     struct object_name *ptr;
105
106     if ((ptr = mem_alloc( sizeof(*ptr) + len - sizeof(ptr->name) )))
107     {
108         ptr->len = len;
109         memcpy( ptr->name, name, len );
110     }
111     return ptr;
112 }
113
114 /* free the name of an object */
115 static void free_name( struct object *obj )
116 {
117     struct object_name *ptr = obj->name;
118     list_remove( &ptr->entry );
119     free( ptr );
120 }
121
122 /* set the name of an existing object */
123 static void set_object_name( struct namespace *namespace,
124                              struct object *obj, struct object_name *ptr )
125 {
126     int hash = get_name_hash( namespace, ptr->name, ptr->len );
127
128     list_add_head( &namespace->names[hash], &ptr->entry );
129     ptr->obj = obj;
130     obj->name = ptr;
131 }
132
133 /* allocate and initialize an object */
134 /* if the function fails the fd is closed */
135 void *alloc_object( const struct object_ops *ops, int fd )
136 {
137     struct object *obj = mem_alloc( ops->size );
138     if (obj)
139     {
140         obj->refcount = 1;
141         obj->fd_obj   = NULL;
142         obj->fd       = fd;
143         obj->select   = -1;
144         obj->ops      = ops;
145         obj->head     = NULL;
146         obj->tail     = NULL;
147         obj->name     = NULL;
148         if ((fd != -1) && (add_select_user( obj ) == -1))
149         {
150             close( fd );
151             free( obj );
152             return NULL;
153         }
154 #ifdef DEBUG_OBJECTS
155         list_add_head( &object_list, &obj->obj_list );
156 #endif
157         return obj;
158     }
159     if (fd != -1) close( fd );
160     return NULL;
161 }
162
163 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
164                            const WCHAR *name, size_t len )
165 {
166     struct object *obj;
167     struct object_name *name_ptr;
168
169     if (!name || !len) return alloc_object( ops, -1 );
170
171     if ((obj = find_object( namespace, name, len )))
172     {
173         if (obj->ops == ops)
174         {
175             set_error( STATUS_OBJECT_NAME_COLLISION );
176             return obj;
177         }
178         set_error( STATUS_OBJECT_TYPE_MISMATCH );
179         return NULL;
180     }
181     if (!(name_ptr = alloc_name( name, len ))) return NULL;
182     if ((obj = alloc_object( ops, -1 )))
183     {
184         set_object_name( namespace, obj, name_ptr );
185         clear_error();
186     }
187     else free( name_ptr );
188     return obj;
189 }
190
191 /* dump the name of an object to stderr */
192 void dump_object_name( struct object *obj )
193 {
194     if (!obj->name) fprintf( stderr, "name=\"\"" );
195     else
196     {
197         fprintf( stderr, "name=L\"" );
198         dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
199         fputc( '\"', stderr );
200     }
201 }
202
203 /* grab an object (i.e. increment its refcount) and return the object */
204 struct object *grab_object( void *ptr )
205 {
206     struct object *obj = (struct object *)ptr;
207     assert( obj->refcount < INT_MAX );
208     obj->refcount++;
209     return obj;
210 }
211
212 /* release an object (i.e. decrement its refcount) */
213 void release_object( void *ptr )
214 {
215     struct object *obj = (struct object *)ptr;
216     assert( obj->refcount );
217     if (!--obj->refcount)
218     {
219         /* if the refcount is 0, nobody can be in the wait queue */
220         assert( !obj->head );
221         assert( !obj->tail );
222         obj->ops->destroy( obj );
223         if (obj->fd_obj) close_fd( obj->fd_obj );
224         if (obj->name) free_name( obj );
225         if (obj->select != -1) remove_select_user( obj );
226         if (obj->fd != -1) close( obj->fd );
227 #ifdef DEBUG_OBJECTS
228         list_remove( &obj->obj_list );
229         memset( obj, 0xaa, obj->ops->size );
230 #endif
231         free( obj );
232     }
233 }
234
235 /* find an object by its name; the refcount is incremented */
236 struct object *find_object( const struct namespace *namespace, const WCHAR *name, size_t len )
237 {
238     const struct list *list, *p;
239
240     if (!name || !len) return NULL;
241
242     list = &namespace->names[ get_name_hash( namespace, name, len ) ];
243     if (namespace->case_sensitive)
244     {
245         LIST_FOR_EACH( p, list )
246         {
247             struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
248             if (ptr->len != len) continue;
249             if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
250         }
251     }
252     else
253     {
254         LIST_FOR_EACH( p, list )
255         {
256             struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
257             if (ptr->len != len) continue;
258             if (!strncmpiW( ptr->name, name, len/sizeof(WCHAR) )) return grab_object( ptr->obj );
259         }
260     }
261     return NULL;
262 }
263
264 /* allocate a namespace */
265 struct namespace *create_namespace( unsigned int hash_size, int case_sensitive )
266 {
267     struct namespace *namespace;
268     unsigned int i;
269
270     namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
271     if (namespace)
272     {
273         namespace->hash_size      = hash_size;
274         namespace->case_sensitive = case_sensitive;
275         for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
276     }
277     return namespace;
278 }
279
280 /* functions for unimplemented/default object operations */
281
282 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
283 {
284     set_error( STATUS_OBJECT_TYPE_MISMATCH );
285     return 0;
286 }
287
288 int no_satisfied( struct object *obj, struct thread *thread )
289 {
290     return 0;  /* not abandoned */
291 }
292
293 struct fd *no_get_fd( struct object *obj )
294 {
295     set_error( STATUS_OBJECT_TYPE_MISMATCH );
296     return NULL;
297 }
298
299 struct fd *default_get_fd( struct object *obj )
300 {
301     if (obj->fd_obj) return (struct fd *)grab_object( obj->fd_obj );
302     set_error( STATUS_OBJECT_TYPE_MISMATCH );
303     return NULL;
304 }
305
306 int no_get_file_info( struct object *obj, struct get_file_info_reply *info, int *flags )
307 {
308     set_error( STATUS_OBJECT_TYPE_MISMATCH );
309     *flags = 0;
310     return FD_TYPE_INVALID;
311 }
312
313 void no_destroy( struct object *obj )
314 {
315 }
316
317 /* default handler for poll() events */
318 void default_poll_event( struct object *obj, int event )
319 {
320     /* an error occurred, stop polling this fd to avoid busy-looping */
321     if (event & (POLLERR | POLLHUP)) set_select_events( obj, -1 );
322     wake_up( obj, 0 );
323 }