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( STATUS_NO_MEMORY
);
58 /* duplicate a block of memory */
59 void *memdup( const void *data
, size_t len
)
61 void *ptr
= mem_alloc( len
);
62 if (ptr
) memcpy( ptr
, data
, len
);
67 /*****************************************************************/
69 static int get_name_hash( const WCHAR
*name
, size_t len
)
72 while (len
--) hash
^= *name
++;
73 return hash
% NAME_HASH_SIZE
;
76 /* allocate a name for an object */
77 static struct object_name
*alloc_name( const WCHAR
*name
, size_t len
)
79 struct object_name
*ptr
;
81 if ((ptr
= mem_alloc( sizeof(*ptr
) + len
* sizeof(ptr
->name
[0]) )))
84 memcpy( ptr
->name
, name
, len
* sizeof(ptr
->name
[0]) );
90 /* free the name of an object */
91 static void free_name( struct object
*obj
)
93 struct object_name
*ptr
= obj
->name
;
94 if (ptr
->next
) ptr
->next
->prev
= ptr
->prev
;
95 if (ptr
->prev
) ptr
->prev
->next
= ptr
->next
;
99 for (hash
= 0; hash
< NAME_HASH_SIZE
; hash
++)
100 if (names
[hash
] == ptr
)
102 names
[hash
] = ptr
->next
;
109 /* set the name of an existing object */
110 static void set_object_name( struct object
*obj
, struct object_name
*ptr
)
112 int hash
= get_name_hash( ptr
->name
, ptr
->len
);
114 if ((ptr
->next
= names
[hash
]) != NULL
) ptr
->next
->prev
= ptr
;
118 assert( !obj
->name
);
122 /* allocate and initialize an object */
123 /* if the function fails the fd is closed */
124 void *alloc_object( const struct object_ops
*ops
, int fd
)
126 struct object
*obj
= mem_alloc( ops
->size
);
136 if ((fd
!= -1) && (add_select_user( obj
) == -1))
144 if ((obj
->next
= first
) != NULL
) obj
->next
->prev
= obj
;
149 if (fd
!= -1) close( fd
);
153 void *create_named_object( const struct object_ops
*ops
, const WCHAR
*name
, size_t len
)
156 struct object_name
*name_ptr
;
158 if (!name
|| !len
) return alloc_object( ops
, -1 );
159 if (!(name_ptr
= alloc_name( name
, len
))) return NULL
;
161 if ((obj
= find_object( name_ptr
->name
, name_ptr
->len
)))
163 free( name_ptr
); /* we no longer need it */
166 set_error( STATUS_OBJECT_NAME_COLLISION
);
169 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
172 if ((obj
= alloc_object( ops
, -1 )))
174 set_object_name( obj
, name_ptr
);
177 else free( name_ptr
);
181 /* dump the name of an object to stderr */
182 void dump_object_name( struct object
*obj
)
184 if (!obj
->name
) fprintf( stderr
, "name=\"\"" );
187 fprintf( stderr
, "name=L\"" );
188 dump_strW( obj
->name
->name
, strlenW(obj
->name
->name
), stderr
, "\"\"" );
189 fputc( '\"', stderr
);
193 /* grab an object (i.e. increment its refcount) and return the object */
194 struct object
*grab_object( void *ptr
)
196 struct object
*obj
= (struct object
*)ptr
;
197 assert( obj
->refcount
< INT_MAX
);
202 /* release an object (i.e. decrement its refcount) */
203 void release_object( void *ptr
)
205 struct object
*obj
= (struct object
*)ptr
;
206 assert( obj
->refcount
);
207 if (!--obj
->refcount
)
209 /* if the refcount is 0, nobody can be in the wait queue */
210 assert( !obj
->head
);
211 assert( !obj
->tail
);
212 obj
->ops
->destroy( obj
);
213 if (obj
->name
) free_name( obj
);
214 if (obj
->select
!= -1) remove_select_user( obj
);
215 if (obj
->fd
!= -1) close( obj
->fd
);
217 if (obj
->next
) obj
->next
->prev
= obj
->prev
;
218 if (obj
->prev
) obj
->prev
->next
= obj
->next
;
219 else first
= obj
->next
;
220 memset( obj
, 0xaa, obj
->ops
->size
);
226 /* find an object by its name; the refcount is incremented */
227 struct object
*find_object( const WCHAR
*name
, size_t len
)
229 struct object_name
*ptr
;
230 if (!name
|| !len
) return NULL
;
231 for (ptr
= names
[ get_name_hash( name
, len
) ]; ptr
; ptr
= ptr
->next
)
233 if (ptr
->len
!= len
) continue;
234 if (!memcmp( ptr
->name
, name
, len
*sizeof(WCHAR
) )) return grab_object( ptr
->obj
);
239 /* functions for unimplemented/default object operations */
241 int no_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
243 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
247 int no_satisfied( struct object
*obj
, struct thread
*thread
)
249 return 0; /* not abandoned */
252 int no_read_fd( struct object
*obj
)
254 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
258 int no_write_fd( struct object
*obj
)
260 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
264 int no_flush( struct object
*obj
)
266 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
270 int no_get_file_info( struct object
*obj
, struct get_file_info_request
*info
)
272 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
276 void no_destroy( struct object
*obj
)
280 /* default add_queue() routine for objects that poll() on an fd */
281 int default_poll_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
283 if (!obj
->head
) /* first on the queue */
284 set_select_events( obj
, obj
->ops
->get_poll_events( obj
) );
285 add_queue( obj
, entry
);
289 /* default remove_queue() routine for objects that poll() on an fd */
290 void default_poll_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
293 remove_queue( obj
, entry
);
294 if (!obj
->head
) /* last on the queue is gone */
295 set_select_events( obj
, 0 );
296 release_object( obj
);
299 /* default signaled() routine for objects that poll() on an fd */
300 int default_poll_signaled( struct object
*obj
, struct thread
*thread
)
302 int events
= obj
->ops
->get_poll_events( obj
);
304 if (check_select_events( obj
->fd
, events
))
306 /* stop waiting on select() if we are signaled */
307 set_select_events( obj
, 0 );
310 /* restart waiting on select() if we are no longer signaled */
311 if (obj
->head
) set_select_events( obj
, events
);
315 /* default handler for poll() events */
316 void default_poll_event( struct object
*obj
, int event
)
318 /* an error occurred, stop polling this fd to avoid busy-looping */
319 if (event
& (POLLERR
| POLLHUP
)) set_select_events( obj
, -1 );