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
22 #include "wine/port.h"
33 #define WIN32_NO_STATUS
43 struct list entry; /* entry in the hash list */
44 struct object *obj; /* object owning this name */
45 struct object *parent; /* parent object */
46 size_t len; /* name length in bytes */
52 unsigned int hash_size; /* size of hash table */
53 struct list names[1]; /* array of hash entry lists */
58 static struct list object_list = LIST_INIT(object_list);
59 static struct list static_object_list = LIST_INIT(static_object_list);
61 void dump_objects(void)
65 LIST_FOR_EACH( p, &static_object_list )
67 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
68 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
69 ptr->ops->dump( ptr, 1 );
71 LIST_FOR_EACH( p, &object_list )
73 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
74 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
75 ptr->ops->dump( ptr, 1 );
79 void close_objects(void)
83 /* release the static objects */
84 while ((ptr = list_head( &static_object_list )))
86 struct object *obj = LIST_ENTRY( ptr, struct object, obj_list );
87 /* move it back to the standard list before freeing */
88 list_remove( &obj->obj_list );
89 list_add_head( &object_list, &obj->obj_list );
90 release_object( obj );
93 dump_objects(); /* dump any remaining objects */
96 #endif /* DEBUG_OBJECTS */
98 /*****************************************************************/
100 /* malloc replacement */
101 void *mem_alloc( size_t size )
103 void *ptr = malloc( size );
104 if (ptr) memset( ptr, 0x55, size );
105 else set_error( STATUS_NO_MEMORY );
109 /* duplicate a block of memory */
110 void *memdup( const void *data, size_t len )
112 void *ptr = malloc( len );
113 if (ptr) memcpy( ptr, data, len );
114 else set_error( STATUS_NO_MEMORY );
119 /*****************************************************************/
121 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, size_t len )
124 len /= sizeof(WCHAR);
125 while (len--) hash ^= tolowerW(*name++);
126 return hash % namespace->hash_size;
129 /* allocate a name for an object */
130 static struct object_name *alloc_name( const struct unicode_str *name )
132 struct object_name *ptr;
134 if ((ptr = mem_alloc( sizeof(*ptr) + name->len - sizeof(ptr->name) )))
136 ptr->len = name->len;
138 memcpy( ptr->name, name->str, name->len );
143 /* free the name of an object */
144 static void free_name( struct object *obj )
146 struct object_name *ptr = obj->name;
147 list_remove( &ptr->entry );
148 if (ptr->parent) release_object( ptr->parent );
152 /* set the name of an existing object */
153 static void set_object_name( struct namespace *namespace,
154 struct object *obj, struct object_name *ptr )
156 int hash = get_name_hash( namespace, ptr->name, ptr->len );
158 list_add_head( &namespace->names[hash], &ptr->entry );
163 /* get the name of an existing object */
164 const WCHAR *get_object_name( struct object *obj, size_t *len )
166 struct object_name *ptr = obj->name;
167 if (!ptr) return NULL;
172 /* allocate and initialize an object */
173 void *alloc_object( const struct object_ops *ops )
175 struct object *obj = mem_alloc( ops->size );
181 list_init( &obj->wait_queue );
183 list_add_head( &object_list, &obj->obj_list );
190 void *create_object( struct namespace *namespace, const struct object_ops *ops,
191 const struct unicode_str *name, struct object *parent )
194 struct object_name *name_ptr;
196 if (!(name_ptr = alloc_name( name ))) return NULL;
197 if ((obj = alloc_object( ops )))
199 set_object_name( namespace, obj, name_ptr );
200 if (parent) name_ptr->parent = grab_object( parent );
207 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
208 const struct unicode_str *name, unsigned int attributes )
212 if (!name || !name->len) return alloc_object( ops );
214 if ((obj = find_object( namespace, name, attributes )))
216 if (attributes & OBJ_OPENIF && obj->ops == ops)
217 set_error( STATUS_OBJECT_NAME_EXISTS );
220 release_object( obj );
222 if (attributes & OBJ_OPENIF)
223 set_error( STATUS_OBJECT_TYPE_MISMATCH );
225 set_error( STATUS_OBJECT_NAME_COLLISION );
229 if ((obj = create_object( namespace, ops, name, NULL ))) clear_error();
233 /* dump the name of an object to stderr */
234 void dump_object_name( struct object *obj )
236 if (!obj->name) fprintf( stderr, "name=\"\"" );
239 fprintf( stderr, "name=L\"" );
240 dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
241 fputc( '\"', stderr );
245 /* unlink a named object from its namespace, without freeing the object itself */
246 void unlink_named_object( struct object *obj )
248 if (obj->name) free_name( obj );
252 /* mark an object as being stored statically, i.e. only released at shutdown */
253 void make_object_static( struct object *obj )
256 list_remove( &obj->obj_list );
257 list_add_head( &static_object_list, &obj->obj_list );
261 /* grab an object (i.e. increment its refcount) and return the object */
262 struct object *grab_object( void *ptr )
264 struct object *obj = (struct object *)ptr;
265 assert( obj->refcount < INT_MAX );
270 /* release an object (i.e. decrement its refcount) */
271 void release_object( void *ptr )
273 struct object *obj = (struct object *)ptr;
274 assert( obj->refcount );
275 if (!--obj->refcount)
277 /* if the refcount is 0, nobody can be in the wait queue */
278 assert( list_empty( &obj->wait_queue ));
279 obj->ops->destroy( obj );
280 if (obj->name) free_name( obj );
282 list_remove( &obj->obj_list );
283 memset( obj, 0xaa, obj->ops->size );
289 /* find an object by its name; the refcount is incremented */
290 struct object *find_object( const struct namespace *namespace, const struct unicode_str *name,
291 unsigned int attributes )
293 const struct list *list, *p;
295 if (!name || !name->len) return NULL;
297 list = &namespace->names[ get_name_hash( namespace, name->str, name->len ) ];
298 LIST_FOR_EACH( p, list )
300 const struct object_name *ptr = LIST_ENTRY( p, const struct object_name, entry );
301 if (ptr->len != name->len) continue;
302 if (attributes & OBJ_CASE_INSENSITIVE)
304 if (!strncmpiW( ptr->name, name->str, name->len/sizeof(WCHAR) ))
305 return grab_object( ptr->obj );
309 if (!memcmp( ptr->name, name->str, name->len ))
310 return grab_object( ptr->obj );
316 /* allocate a namespace */
317 struct namespace *create_namespace( unsigned int hash_size )
319 struct namespace *namespace;
322 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
325 namespace->hash_size = hash_size;
326 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
331 /* functions for unimplemented/default object operations */
333 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
335 set_error( STATUS_OBJECT_TYPE_MISMATCH );
339 int no_satisfied( struct object *obj, struct thread *thread )
341 return 0; /* not abandoned */
344 int no_signal( struct object *obj, unsigned int access )
346 set_error( STATUS_OBJECT_TYPE_MISMATCH );
350 struct fd *no_get_fd( struct object *obj )
352 set_error( STATUS_OBJECT_TYPE_MISMATCH );
356 unsigned int no_map_access( struct object *obj, unsigned int access )
358 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
359 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
360 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
361 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
362 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
365 struct object *no_lookup_name( struct object *obj, struct unicode_str *name,
371 int no_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
373 return 1; /* ok to close */
376 void no_destroy( struct object *obj )