ANSI C fixes.
[wine] / server / object.c
1 /*
2  * Server-side objects
3  * These are the server equivalent of K32OBJ
4  *
5  * Copyright (C) 1998 Alexandre Julliard
6  */
7
8 #include <assert.h>
9 #include <limits.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13
14 #include "winerror.h"
15 #include "thread.h"
16
17 int debug_level = 0;
18
19 struct object_name
20 {
21     struct object_name *next;
22     struct object_name *prev;
23     struct object      *obj;
24     size_t              len;
25     char                name[1];
26 };
27
28 #define NAME_HASH_SIZE 37
29
30 static struct object_name *names[NAME_HASH_SIZE];
31
32 #ifdef DEBUG_OBJECTS
33 static struct object *first;
34
35 void dump_objects(void)
36 {
37     struct object *ptr = first;
38     while (ptr)
39     {
40         fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
41         ptr->ops->dump( ptr, 1 );
42         ptr = ptr->next;
43     }
44 }
45 #endif
46
47 /*****************************************************************/
48
49 /* malloc replacement */
50 void *mem_alloc( size_t size )
51 {
52     void *ptr = malloc( size );
53     if (ptr) memset( ptr, 0x55, size );
54     else if (current) set_error( ERROR_OUTOFMEMORY );
55     return ptr;
56 }
57
58 /*****************************************************************/
59
60 static int get_name_hash( const char *name, size_t len )
61 {
62     char hash = 0;
63     while (len--) hash ^= *name++;
64     return hash % NAME_HASH_SIZE;
65 }
66
67 /* allocate a name for an object */
68 static struct object_name *alloc_name( const char *name, size_t len )
69 {
70     struct object_name *ptr;
71
72     if ((ptr = mem_alloc( sizeof(*ptr) + len )))
73     {
74         ptr->len = len;
75         memcpy( ptr->name, name, len );
76         ptr->name[len] = 0;
77     }
78     return ptr;
79 }
80
81 /* free the name of an object */
82 static void free_name( struct object *obj )
83 {
84     struct object_name *ptr = obj->name;
85     if (ptr->next) ptr->next->prev = ptr->prev;
86     if (ptr->prev) ptr->prev->next = ptr->next;
87     else
88     {
89         int hash;
90         for (hash = 0; hash < NAME_HASH_SIZE; hash++)
91             if (names[hash] == ptr)
92             {
93                 names[hash] = ptr->next;
94                 break;
95             }
96     }
97     free( ptr );
98 }
99
100 /* set the name of an existing object */
101 static void set_object_name( struct object *obj, struct object_name *ptr )
102 {
103     int hash = get_name_hash( ptr->name, ptr->len );
104
105     if ((ptr->next = names[hash]) != NULL) ptr->next->prev = ptr;
106     ptr->obj = obj;
107     ptr->prev = NULL;
108     names[hash] = ptr;
109     assert( !obj->name );
110     obj->name = ptr;
111 }
112
113 /* allocate and initialize an object */
114 void *alloc_object( const struct object_ops *ops )
115 {
116     struct object *obj = mem_alloc( ops->size );
117     if (obj)
118     {
119         obj->refcount = 1;
120         obj->ops      = ops;
121         obj->head     = NULL;
122         obj->tail     = NULL;
123         obj->name     = NULL;
124 #ifdef DEBUG_OBJECTS
125         obj->prev = NULL;
126         if ((obj->next = first) != NULL) obj->next->prev = obj;
127         first = obj;
128 #endif
129     }
130     return obj;
131 }
132
133 void *create_named_object( const struct object_ops *ops, const char *name, size_t len )
134 {
135     struct object *obj;
136     struct object_name *name_ptr;
137
138     if (!name || !len) return alloc_object( ops );
139     if (!(name_ptr = alloc_name( name, len ))) return NULL;
140
141     if ((obj = find_object( name_ptr->name, name_ptr->len )))
142     {
143         free( name_ptr );  /* we no longer need it */
144         if (obj->ops == ops)
145         {
146             set_error( ERROR_ALREADY_EXISTS );
147             return obj;
148         }
149         set_error( ERROR_INVALID_HANDLE );
150         return NULL;
151     }
152     if ((obj = alloc_object( ops )))
153     {
154         set_object_name( obj, name_ptr );
155         clear_error();
156     }
157     else free( name_ptr );
158     return obj;
159 }
160
161 /* return a pointer to the object name, or to an empty string */
162 const char *get_object_name( struct object *obj )
163 {
164     if (!obj->name) return "";
165     return obj->name->name;
166 }
167
168 /* grab an object (i.e. increment its refcount) and return the object */
169 struct object *grab_object( void *ptr )
170 {
171     struct object *obj = (struct object *)ptr;
172     assert( obj->refcount < INT_MAX );
173     obj->refcount++;
174     return obj;
175 }
176
177 /* release an object (i.e. decrement its refcount) */
178 void release_object( void *ptr )
179 {
180     struct object *obj = (struct object *)ptr;
181     assert( obj->refcount );
182     if (!--obj->refcount)
183     {
184         /* if the refcount is 0, nobody can be in the wait queue */
185         assert( !obj->head );
186         assert( !obj->tail );
187         if (obj->name) free_name( obj );
188 #ifdef DEBUG_OBJECTS
189         if (obj->next) obj->next->prev = obj->prev;
190         if (obj->prev) obj->prev->next = obj->next;
191         else first = obj->next;
192 #endif
193         obj->ops->destroy( obj );
194         memset( obj, 0xaa, obj->ops->size );
195         free( obj );
196     }
197 }
198
199 /* find an object by its name; the refcount is incremented */
200 struct object *find_object( const char *name, size_t len )
201 {
202     struct object_name *ptr;
203     if (!name || !len) return NULL;
204     for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
205     {
206         if (ptr->len != len) continue;
207         if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
208     }
209     return NULL;
210 }
211
212 /* functions for unimplemented object operations */
213
214 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
215 {
216     set_error( ERROR_INVALID_HANDLE );
217     return 0;
218 }
219
220 int no_satisfied( struct object *obj, struct thread *thread )
221 {
222     return 0;  /* not abandoned */
223 }
224
225 int no_read_fd( struct object *obj )
226 {
227     set_error( ERROR_INVALID_HANDLE );
228     return -1;
229 }
230
231 int no_write_fd( struct object *obj )
232 {
233     set_error( ERROR_INVALID_HANDLE );
234     return -1;
235 }
236
237 int no_flush( struct object *obj )
238 {
239     set_error( ERROR_INVALID_HANDLE );
240     return 0;
241 }
242
243 int no_get_file_info( struct object *obj, struct get_file_info_request *info )
244 {
245     set_error( ERROR_INVALID_HANDLE );
246     return 0;
247 }
248
249 void no_destroy( struct object *obj )
250 {
251 }
252
253 void default_select_event( int event, void *private )
254 {
255     struct object *obj = (struct object *)private;
256     assert( obj );
257     wake_up( obj, 0 );
258 }