object.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Server-side objects
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <limits.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include "file.h"
  29. #include "thread.h"
  30. #include "unicode.h"
  31. struct object_name
  32. {
  33. struct list entry; /* entry in the hash list */
  34. struct object *obj;
  35. size_t len;
  36. WCHAR name[1];
  37. };
  38. struct namespace
  39. {
  40. unsigned int hash_size; /* size of hash table */
  41. int case_sensitive; /* are names case sensitive? */
  42. struct list names[1]; /* array of hash entry lists */
  43. };
  44. #ifdef DEBUG_OBJECTS
  45. static struct list object_list = LIST_INIT(object_list);
  46. void dump_objects(void)
  47. {
  48. struct list *p;
  49. LIST_FOR_EACH( p, &object_list )
  50. {
  51. struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
  52. fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
  53. ptr->ops->dump( ptr, 1 );
  54. }
  55. }
  56. #endif
  57. /*****************************************************************/
  58. /* malloc replacement */
  59. void *mem_alloc( size_t size )
  60. {
  61. void *ptr = malloc( size );
  62. if (ptr) memset( ptr, 0x55, size );
  63. else set_error( STATUS_NO_MEMORY );
  64. return ptr;
  65. }
  66. /* duplicate a block of memory */
  67. void *memdup( const void *data, size_t len )
  68. {
  69. void *ptr = malloc( len );
  70. if (ptr) memcpy( ptr, data, len );
  71. else set_error( STATUS_NO_MEMORY );
  72. return ptr;
  73. }
  74. /*****************************************************************/
  75. static int get_name_hash( const struct namespace *namespace, const WCHAR *name, size_t len )
  76. {
  77. WCHAR hash = 0;
  78. len /= sizeof(WCHAR);
  79. if (namespace->case_sensitive) while (len--) hash ^= *name++;
  80. else while (len--) hash ^= tolowerW(*name++);
  81. return hash % namespace->hash_size;
  82. }
  83. /* allocate a name for an object */
  84. static struct object_name *alloc_name( const WCHAR *name, size_t len )
  85. {
  86. struct object_name *ptr;
  87. if ((ptr = mem_alloc( sizeof(*ptr) + len - sizeof(ptr->name) )))
  88. {
  89. ptr->len = len;
  90. memcpy( ptr->name, name, len );
  91. }
  92. return ptr;
  93. }
  94. /* free the name of an object */
  95. static void free_name( struct object *obj )
  96. {
  97. struct object_name *ptr = obj->name;
  98. list_remove( &ptr->entry );
  99. free( ptr );
  100. }
  101. /* set the name of an existing object */
  102. static void set_object_name( struct namespace *namespace,
  103. struct object *obj, struct object_name *ptr )
  104. {
  105. int hash = get_name_hash( namespace, ptr->name, ptr->len );
  106. list_add_head( &namespace->names[hash], &ptr->entry );
  107. ptr->obj = obj;
  108. obj->name = ptr;
  109. }
  110. /* allocate and initialize an object */
  111. void *alloc_object( const struct object_ops *ops )
  112. {
  113. struct object *obj = mem_alloc( ops->size );
  114. if (obj)
  115. {
  116. obj->refcount = 1;
  117. obj->ops = ops;
  118. obj->head = NULL;
  119. obj->tail = NULL;
  120. obj->name = NULL;
  121. #ifdef DEBUG_OBJECTS
  122. list_add_head( &object_list, &obj->obj_list );
  123. #endif
  124. return obj;
  125. }
  126. return NULL;
  127. }
  128. void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
  129. const WCHAR *name, size_t len )
  130. {
  131. struct object *obj;
  132. struct object_name *name_ptr;
  133. if (!name || !len) return alloc_object( ops );
  134. if ((obj = find_object( namespace, name, len )))
  135. {
  136. if (obj->ops == ops)
  137. {
  138. set_error( STATUS_OBJECT_NAME_COLLISION );
  139. return obj;
  140. }
  141. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  142. return NULL;
  143. }
  144. if (!(name_ptr = alloc_name( name, len ))) return NULL;
  145. if ((obj = alloc_object( ops )))
  146. {
  147. set_object_name( namespace, obj, name_ptr );
  148. clear_error();
  149. }
  150. else free( name_ptr );
  151. return obj;
  152. }
  153. /* dump the name of an object to stderr */
  154. void dump_object_name( struct object *obj )
  155. {
  156. if (!obj->name) fprintf( stderr, "name=\"\"" );
  157. else
  158. {
  159. fprintf( stderr, "name=L\"" );
  160. dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
  161. fputc( '\"', stderr );
  162. }
  163. }
  164. /* grab an object (i.e. increment its refcount) and return the object */
  165. struct object *grab_object( void *ptr )
  166. {
  167. struct object *obj = (struct object *)ptr;
  168. assert( obj->refcount < INT_MAX );
  169. obj->refcount++;
  170. return obj;
  171. }
  172. /* release an object (i.e. decrement its refcount) */
  173. void release_object( void *ptr )
  174. {
  175. struct object *obj = (struct object *)ptr;
  176. assert( obj->refcount );
  177. if (!--obj->refcount)
  178. {
  179. /* if the refcount is 0, nobody can be in the wait queue */
  180. assert( !obj->head );
  181. assert( !obj->tail );
  182. obj->ops->destroy( obj );
  183. if (obj->name) free_name( obj );
  184. #ifdef DEBUG_OBJECTS
  185. list_remove( &obj->obj_list );
  186. memset( obj, 0xaa, obj->ops->size );
  187. #endif
  188. free( obj );
  189. }
  190. }
  191. /* find an object by its name; the refcount is incremented */
  192. struct object *find_object( const struct namespace *namespace, const WCHAR *name, size_t len )
  193. {
  194. const struct list *list, *p;
  195. if (!name || !len) return NULL;
  196. list = &namespace->names[ get_name_hash( namespace, name, len ) ];
  197. if (namespace->case_sensitive)
  198. {
  199. LIST_FOR_EACH( p, list )
  200. {
  201. const struct object_name *ptr = LIST_ENTRY( p, const struct object_name, entry );
  202. if (ptr->len != len) continue;
  203. if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
  204. }
  205. }
  206. else
  207. {
  208. LIST_FOR_EACH( p, list )
  209. {
  210. const struct object_name *ptr = LIST_ENTRY( p, const struct object_name, entry );
  211. if (ptr->len != len) continue;
  212. if (!strncmpiW( ptr->name, name, len/sizeof(WCHAR) )) return grab_object( ptr->obj );
  213. }
  214. }
  215. return NULL;
  216. }
  217. /* allocate a namespace */
  218. struct namespace *create_namespace( unsigned int hash_size, int case_sensitive )
  219. {
  220. struct namespace *namespace;
  221. unsigned int i;
  222. namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
  223. if (namespace)
  224. {
  225. namespace->hash_size = hash_size;
  226. namespace->case_sensitive = case_sensitive;
  227. for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
  228. }
  229. return namespace;
  230. }
  231. /* functions for unimplemented/default object operations */
  232. int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
  233. {
  234. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  235. return 0;
  236. }
  237. int no_satisfied( struct object *obj, struct thread *thread )
  238. {
  239. return 0; /* not abandoned */
  240. }
  241. struct fd *no_get_fd( struct object *obj )
  242. {
  243. set_error( STATUS_OBJECT_TYPE_MISMATCH );
  244. return NULL;
  245. }
  246. void no_destroy( struct object *obj )
  247. {
  248. }