3 * These are the server equivalent of K32OBJ
5 * Copyright (C) 1998 Alexandre Julliard
15 #include "server/object.h"
21 struct object_name *next;
27 #define NAME_HASH_SIZE 37
29 static struct object_name *names[NAME_HASH_SIZE];
31 /*****************************************************************/
33 static int get_name_hash( const char *name )
36 while (*name) hash ^= *name++;
37 return hash % NAME_HASH_SIZE;
40 static struct object_name *add_name( struct object *obj, const char *name )
42 struct object_name *ptr;
43 int hash = get_name_hash( name );
44 int len = strlen( name );
46 if (!(ptr = (struct object_name *)malloc( sizeof(*ptr) + len )))
48 ptr->next = names[hash];
51 strcpy( ptr->name, name );
56 static void free_name( struct object *obj )
58 int hash = get_name_hash( obj->name->name );
59 struct object_name **pptr = &names[hash];
60 while (*pptr && *pptr != obj->name) pptr = &(*pptr)->next;
62 *pptr = (*pptr)->next;
66 /* initialize an already allocated object */
67 void init_object( struct object *obj, const struct object_ops *ops,
74 if (!name) obj->name = NULL;
75 else obj->name = add_name( obj, name );
78 /* grab an object (i.e. increment its refcount) and return the object */
79 struct object *grab_object( void *ptr )
81 struct object *obj = (struct object *)ptr;
82 assert( obj->refcount < INT_MAX );
87 /* release an object (i.e. decrement its refcount) */
88 void release_object( void *ptr )
90 struct object *obj = (struct object *)ptr;
91 assert( obj->refcount );
94 /* if the refcount is 0, nobody can be in the wait queue */
97 if (obj->name) free_name( obj );
98 obj->ops->destroy( obj );
102 /* find an object by its name; the refcount is incremented */
103 struct object *find_object( const char *name )
105 int hash = get_name_hash( name );
106 struct object_name *ptr = names[hash];
107 while (ptr && strcmp( ptr->name, name )) ptr = ptr->next;
108 if (!ptr) return NULL;
109 grab_object( ptr->obj );