3 * These are the server equivalent of K32OBJ
5 * Copyright (C) 1998 Alexandre Julliard
21 struct object_name
*next
;
22 struct object_name
*prev
;
28 #define NAME_HASH_SIZE 37
30 static struct object_name
*names
[NAME_HASH_SIZE
];
33 static struct object
*first
;
35 void dump_objects(void)
37 struct object
*ptr
= first
;
40 fprintf( stderr
, "%p:%d: ", ptr
, ptr
->refcount
);
41 ptr
->ops
->dump( ptr
, 1 );
47 /*****************************************************************/
49 /* malloc replacement */
50 void *mem_alloc( size_t size
)
52 void *ptr
= malloc( size
);
53 if (ptr
) memset( ptr
, 0x55, size
);
54 else if (current
) set_error( ERROR_OUTOFMEMORY
);
58 /*****************************************************************/
60 static int get_name_hash( const char *name
, size_t len
)
63 while (len
--) hash
^= *name
++;
64 return hash
% NAME_HASH_SIZE
;
67 /* allocate a name for an object */
68 static struct object_name
*alloc_name( const char *name
, size_t len
)
70 struct object_name
*ptr
;
72 if ((ptr
= mem_alloc( sizeof(*ptr
) + len
)))
75 memcpy( ptr
->name
, name
, len
);
81 /* free the name of an object */
82 static void free_name( struct object
*obj
)
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
;
90 for (hash
= 0; hash
< NAME_HASH_SIZE
; hash
++)
91 if (names
[hash
] == ptr
)
93 names
[hash
] = ptr
->next
;
100 /* set the name of an existing object */
101 static void set_object_name( struct object
*obj
, struct object_name
*ptr
)
103 int hash
= get_name_hash( ptr
->name
, ptr
->len
);
105 if ((ptr
->next
= names
[hash
]) != NULL
) ptr
->next
->prev
= ptr
;
109 assert( !obj
->name
);
113 /* allocate and initialize an object */
114 void *alloc_object( const struct object_ops
*ops
)
116 struct object
*obj
= mem_alloc( ops
->size
);
126 if ((obj
->next
= first
) != NULL
) obj
->next
->prev
= obj
;
133 void *create_named_object( const struct object_ops
*ops
, const char *name
, size_t len
)
136 struct object_name
*name_ptr
;
138 if (!name
|| !len
) return alloc_object( ops
);
139 if (!(name_ptr
= alloc_name( name
, len
))) return NULL
;
141 if ((obj
= find_object( name_ptr
->name
, name_ptr
->len
)))
143 free( name_ptr
); /* we no longer need it */
146 set_error( ERROR_ALREADY_EXISTS
);
149 set_error( ERROR_INVALID_HANDLE
);
152 if ((obj
= alloc_object( ops
)))
154 set_object_name( obj
, name_ptr
);
157 else free( name_ptr
);
161 /* return a pointer to the object name, or to an empty string */
162 const char *get_object_name( struct object
*obj
)
164 if (!obj
->name
) return "";
165 return obj
->name
->name
;
168 /* grab an object (i.e. increment its refcount) and return the object */
169 struct object
*grab_object( void *ptr
)
171 struct object
*obj
= (struct object
*)ptr
;
172 assert( obj
->refcount
< INT_MAX
);
177 /* release an object (i.e. decrement its refcount) */
178 void release_object( void *ptr
)
180 struct object
*obj
= (struct object
*)ptr
;
181 assert( obj
->refcount
);
182 if (!--obj
->refcount
)
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
);
189 if (obj
->next
) obj
->next
->prev
= obj
->prev
;
190 if (obj
->prev
) obj
->prev
->next
= obj
->next
;
191 else first
= obj
->next
;
193 obj
->ops
->destroy( obj
);
194 memset( obj
, 0xaa, obj
->ops
->size
);
199 /* find an object by its name; the refcount is incremented */
200 struct object
*find_object( const char *name
, size_t len
)
202 struct object_name
*ptr
;
203 if (!name
|| !len
) return NULL
;
204 for (ptr
= names
[ get_name_hash( name
, len
) ]; ptr
; ptr
= ptr
->next
)
206 if (ptr
->len
!= len
) continue;
207 if (!memcmp( ptr
->name
, name
, len
)) return grab_object( ptr
->obj
);
212 /* functions for unimplemented object operations */
214 int no_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
216 set_error( ERROR_INVALID_HANDLE
);
220 int no_satisfied( struct object
*obj
, struct thread
*thread
)
222 return 0; /* not abandoned */
225 int no_read_fd( struct object
*obj
)
227 set_error( ERROR_INVALID_HANDLE
);
231 int no_write_fd( struct object
*obj
)
233 set_error( ERROR_INVALID_HANDLE
);
237 int no_flush( struct object
*obj
)
239 set_error( ERROR_INVALID_HANDLE
);
243 int no_get_file_info( struct object
*obj
, struct get_file_info_request
*info
)
245 set_error( ERROR_INVALID_HANDLE
);
249 void no_destroy( struct object
*obj
)
253 void default_select_event( int event
, void *private )
255 struct object
*obj
= (struct object
*)private;