3 * These are the server equivalent of K32OBJ
5 * Copyright (C) 1998 Alexandre Julliard
20 struct object_name *next;
26 #define NAME_HASH_SIZE 37
28 static struct object_name *names[NAME_HASH_SIZE];
30 /*****************************************************************/
32 void *mem_alloc( size_t size )
34 void *ptr = malloc( size );
35 if (ptr) memset( ptr, 0x55, size );
36 else if (current) SET_ERROR( ERROR_OUTOFMEMORY );
40 /*****************************************************************/
42 static int get_name_hash( const char *name )
45 while (*name) hash ^= *name++;
46 return hash % NAME_HASH_SIZE;
49 static struct object_name *add_name( struct object *obj, const char *name )
51 struct object_name *ptr;
52 int hash = get_name_hash( name );
53 int len = strlen( name );
55 if (!(ptr = (struct object_name *)mem_alloc( sizeof(*ptr) + len )))
57 ptr->next = names[hash];
60 strcpy( ptr->name, name );
65 static void free_name( struct object *obj )
67 int hash = get_name_hash( obj->name->name );
68 struct object_name **pptr = &names[hash];
69 while (*pptr && *pptr != obj->name) pptr = &(*pptr)->next;
71 *pptr = (*pptr)->next;
75 /* initialize an already allocated object */
76 /* return 1 if OK, 0 on error */
77 int init_object( struct object *obj, const struct object_ops *ops,
84 if (!name) obj->name = NULL;
85 else if (!(obj->name = add_name( obj, name ))) return 0;
89 struct object *create_named_object( const char *name, const struct object_ops *ops, size_t size )
92 if ((obj = find_object( name )))
96 SET_ERROR( ERROR_ALREADY_EXISTS );
99 SET_ERROR( ERROR_INVALID_HANDLE );
102 if (!(obj = mem_alloc( size ))) return NULL;
103 if (!init_object( obj, ops, name ))
112 /* return a pointer to the object name, or to an empty string */
113 const char *get_object_name( struct object *obj )
115 if (!obj->name) return "";
116 return obj->name->name;
119 /* grab an object (i.e. increment its refcount) and return the object */
120 struct object *grab_object( void *ptr )
122 struct object *obj = (struct object *)ptr;
123 assert( obj->refcount < INT_MAX );
128 /* release an object (i.e. decrement its refcount) */
129 void release_object( void *ptr )
131 struct object *obj = (struct object *)ptr;
132 assert( obj->refcount );
133 if (!--obj->refcount)
135 /* if the refcount is 0, nobody can be in the wait queue */
136 assert( !obj->head );
137 assert( !obj->tail );
138 if (obj->name) free_name( obj );
139 obj->ops->destroy( obj );
143 /* find an object by its name; the refcount is incremented */
144 struct object *find_object( const char *name )
146 struct object_name *ptr;
147 if (!name) return NULL;
148 ptr = names[ get_name_hash( name ) ];
149 while (ptr && strcmp( ptr->name, name )) ptr = ptr->next;
150 if (!ptr) return NULL;
151 return grab_object( ptr->obj );
154 /* functions for unimplemented object operations */
156 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
158 SET_ERROR( ERROR_INVALID_HANDLE );
162 int no_satisfied( struct object *obj, struct thread *thread )
164 return 0; /* not abandoned */
167 int no_read_fd( struct object *obj )
169 SET_ERROR( ERROR_INVALID_HANDLE );
173 int no_write_fd( struct object *obj )
175 SET_ERROR( ERROR_INVALID_HANDLE );
179 int no_flush( struct object *obj )
181 SET_ERROR( ERROR_INVALID_HANDLE );
185 int no_get_file_info( struct object *obj, struct get_file_info_reply *info )
187 SET_ERROR( ERROR_INVALID_HANDLE );
191 void default_select_event( int event, void *private )
193 struct object *obj = (struct object *)private;