Release 20030408.
[wine/gsoc-2012-control.git] / server / object.c
blob8ad76fcb196f8d79e4798d462f0abb1f9eef5d22
1 /*
2 * Server-side objects
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "file.h"
32 #include "thread.h"
33 #include "unicode.h"
34 #include "list.h"
37 struct object_name
39 struct list entry; /* entry in the hash list */
40 struct object *obj;
41 size_t len;
42 WCHAR name[1];
45 struct namespace
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 */
53 #ifdef DEBUG_OBJECTS
54 static struct list object_list = LIST_INIT(object_list);
56 void dump_objects(void)
58 struct list *p;
60 LIST_FOR_EACH( p, &object_list )
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 );
67 #endif
69 /*****************************************************************/
71 /* malloc replacement */
72 void *mem_alloc( size_t size )
74 void *ptr = malloc( size );
75 if (ptr) memset( ptr, 0x55, size );
76 else set_error( STATUS_NO_MEMORY );
77 return ptr;
80 /* duplicate a block of memory */
81 void *memdup( const void *data, size_t len )
83 void *ptr = malloc( len );
84 if (ptr) memcpy( ptr, data, len );
85 else set_error( STATUS_NO_MEMORY );
86 return ptr;
90 /*****************************************************************/
92 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, size_t len )
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;
101 /* allocate a name for an object */
102 static struct object_name *alloc_name( const WCHAR *name, size_t len )
104 struct object_name *ptr;
106 if ((ptr = mem_alloc( sizeof(*ptr) + len - sizeof(ptr->name) )))
108 ptr->len = len;
109 memcpy( ptr->name, name, len );
111 return ptr;
114 /* free the name of an object */
115 static void free_name( struct object *obj )
117 struct object_name *ptr = obj->name;
118 list_remove( &ptr->entry );
119 free( ptr );
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 )
126 int hash = get_name_hash( namespace, ptr->name, ptr->len );
128 list_add_head( &namespace->names[hash], &ptr->entry );
129 ptr->obj = obj;
130 obj->name = ptr;
133 /* allocate and initialize an object */
134 void *alloc_object( const struct object_ops *ops )
136 struct object *obj = mem_alloc( ops->size );
137 if (obj)
139 obj->refcount = 1;
140 obj->ops = ops;
141 obj->head = NULL;
142 obj->tail = NULL;
143 obj->name = NULL;
144 #ifdef DEBUG_OBJECTS
145 list_add_head( &object_list, &obj->obj_list );
146 #endif
147 return obj;
149 return NULL;
152 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
153 const WCHAR *name, size_t len )
155 struct object *obj;
156 struct object_name *name_ptr;
158 if (!name || !len) return alloc_object( ops );
160 if ((obj = find_object( namespace, name, len )))
162 if (obj->ops == ops)
164 set_error( STATUS_OBJECT_NAME_COLLISION );
165 return obj;
167 set_error( STATUS_OBJECT_TYPE_MISMATCH );
168 return NULL;
170 if (!(name_ptr = alloc_name( name, len ))) return NULL;
171 if ((obj = alloc_object( ops )))
173 set_object_name( namespace, obj, name_ptr );
174 clear_error();
176 else free( name_ptr );
177 return obj;
180 /* dump the name of an object to stderr */
181 void dump_object_name( struct object *obj )
183 if (!obj->name) fprintf( stderr, "name=\"\"" );
184 else
186 fprintf( stderr, "name=L\"" );
187 dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
188 fputc( '\"', stderr );
192 /* grab an object (i.e. increment its refcount) and return the object */
193 struct object *grab_object( void *ptr )
195 struct object *obj = (struct object *)ptr;
196 assert( obj->refcount < INT_MAX );
197 obj->refcount++;
198 return obj;
201 /* release an object (i.e. decrement its refcount) */
202 void release_object( void *ptr )
204 struct object *obj = (struct object *)ptr;
205 assert( obj->refcount );
206 if (!--obj->refcount)
208 /* if the refcount is 0, nobody can be in the wait queue */
209 assert( !obj->head );
210 assert( !obj->tail );
211 obj->ops->destroy( obj );
212 if (obj->name) free_name( obj );
213 #ifdef DEBUG_OBJECTS
214 list_remove( &obj->obj_list );
215 memset( obj, 0xaa, obj->ops->size );
216 #endif
217 free( obj );
221 /* find an object by its name; the refcount is incremented */
222 struct object *find_object( const struct namespace *namespace, const WCHAR *name, size_t len )
224 const struct list *list, *p;
226 if (!name || !len) return NULL;
228 list = &namespace->names[ get_name_hash( namespace, name, len ) ];
229 if (namespace->case_sensitive)
231 LIST_FOR_EACH( p, list )
233 struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
234 if (ptr->len != len) continue;
235 if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
238 else
240 LIST_FOR_EACH( p, list )
242 struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
243 if (ptr->len != len) continue;
244 if (!strncmpiW( ptr->name, name, len/sizeof(WCHAR) )) return grab_object( ptr->obj );
247 return NULL;
250 /* allocate a namespace */
251 struct namespace *create_namespace( unsigned int hash_size, int case_sensitive )
253 struct namespace *namespace;
254 unsigned int i;
256 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
257 if (namespace)
259 namespace->hash_size = hash_size;
260 namespace->case_sensitive = case_sensitive;
261 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
263 return namespace;
266 /* functions for unimplemented/default object operations */
268 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
270 set_error( STATUS_OBJECT_TYPE_MISMATCH );
271 return 0;
274 int no_satisfied( struct object *obj, struct thread *thread )
276 return 0; /* not abandoned */
279 struct fd *no_get_fd( struct object *obj )
281 set_error( STATUS_OBJECT_TYPE_MISMATCH );
282 return NULL;
285 void no_destroy( struct object *obj )