atom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Server-side atom management
  3. *
  4. * Copyright (C) 1999, 2000 Alexandre Julliard
  5. * Copyright (C) 2000 Turchanov Sergei
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include "wine/port.h"
  23. #include <assert.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "ntstatus.h"
  28. #define WIN32_NO_STATUS
  29. #include "unicode.h"
  30. #include "request.h"
  31. #include "object.h"
  32. #include "process.h"
  33. #include "handle.h"
  34. #include "user.h"
  35. #include "winuser.h"
  36. #include "winternl.h"
  37. #define HASH_SIZE 37
  38. #define MIN_HASH_SIZE 4
  39. #define MAX_HASH_SIZE 0x200
  40. #define MAX_ATOM_LEN (255 * sizeof(WCHAR))
  41. #define MIN_STR_ATOM 0xc000
  42. #define MAX_ATOMS 0x4000
  43. struct atom_entry
  44. {
  45. struct atom_entry *next; /* hash table list */
  46. struct atom_entry *prev; /* hash table list */
  47. int count; /* reference count */
  48. short pinned; /* whether the atom is pinned or not */
  49. atom_t atom; /* atom handle */
  50. unsigned short hash; /* string hash */
  51. unsigned short len; /* string len */
  52. WCHAR str[1]; /* atom string */
  53. };
  54. struct atom_table
  55. {
  56. struct object obj; /* object header */
  57. int count; /* count of atom handles */
  58. int last; /* last handle in-use */
  59. struct atom_entry **handles; /* atom handles */
  60. int entries_count; /* number of hash entries */
  61. struct atom_entry **entries; /* hash table entries */
  62. };
  63. static void atom_table_dump( struct object *obj, int verbose );
  64. static void atom_table_destroy( struct object *obj );
  65. static const struct object_ops atom_table_ops =
  66. {
  67. sizeof(struct atom_table), /* size */
  68. &no_type, /* type */
  69. atom_table_dump, /* dump */
  70. no_add_queue, /* add_queue */
  71. NULL, /* remove_queue */
  72. NULL, /* signaled */
  73. NULL, /* satisfied */
  74. no_signal, /* signal */
  75. no_get_fd, /* get_fd */
  76. default_map_access, /* map_access */
  77. default_get_sd, /* get_sd */
  78. default_set_sd, /* set_sd */
  79. no_get_full_name, /* get_full_name */
  80. no_lookup_name, /* lookup_name */
  81. no_link_name, /* link_name */
  82. NULL, /* unlink_name */
  83. no_open_file, /* open_file */
  84. no_kernel_obj_list, /* get_kernel_obj_list */
  85. no_close_handle, /* close_handle */
  86. atom_table_destroy /* destroy */
  87. };
  88. static struct atom_table *global_table;
  89. /* create an atom table */
  90. static struct atom_table *create_table(int entries_count)
  91. {
  92. struct atom_table *table;
  93. if ((table = alloc_object( &atom_table_ops )))
  94. {
  95. if ((entries_count < MIN_HASH_SIZE) ||
  96. (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
  97. table->handles = NULL;
  98. table->entries_count = entries_count;
  99. if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
  100. {
  101. set_error( STATUS_NO_MEMORY );
  102. goto fail;
  103. }
  104. memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
  105. table->count = 64;
  106. table->last = -1;
  107. if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
  108. return table;
  109. fail:
  110. release_object( table );
  111. table = NULL;
  112. }
  113. return table;
  114. }
  115. /* retrieve an entry pointer from its atom */
  116. static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
  117. {
  118. struct atom_entry *entry = NULL;
  119. if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
  120. entry = table->handles[atom - MIN_STR_ATOM];
  121. if (!entry) set_error( STATUS_INVALID_HANDLE );
  122. return entry;
  123. }
  124. /* add an atom entry in the table and return its handle */
  125. static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
  126. {
  127. int i;
  128. for (i = 0; i <= table->last; i++)
  129. if (!table->handles[i]) goto found;
  130. if (i == table->count)
  131. {
  132. struct atom_entry **new_table = NULL;
  133. int new_size = table->count + table->count / 2;
  134. if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
  135. if (new_size > table->count)
  136. new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
  137. if (!new_table)
  138. {
  139. set_error( STATUS_NO_MEMORY );
  140. return 0;
  141. }
  142. table->count = new_size;
  143. table->handles = new_table;
  144. }
  145. table->last = i;
  146. found:
  147. table->handles[i] = entry;
  148. entry->atom = i + MIN_STR_ATOM;
  149. return entry->atom;
  150. }
  151. /* dump an atom table */
  152. static void atom_table_dump( struct object *obj, int verbose )
  153. {
  154. int i;
  155. struct atom_table *table = (struct atom_table *)obj;
  156. assert( obj->ops == &atom_table_ops );
  157. fprintf( stderr, "Atom table size=%d entries=%d\n",
  158. table->last + 1, table->entries_count );
  159. if (!verbose) return;
  160. for (i = 0; i <= table->last; i++)
  161. {
  162. struct atom_entry *entry = table->handles[i];
  163. if (!entry) continue;
  164. fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
  165. entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
  166. dump_strW( entry->str, entry->len, stderr, "\"\"");
  167. fprintf( stderr, "\"\n" );
  168. }
  169. }
  170. /* destroy the atom table */
  171. static void atom_table_destroy( struct object *obj )
  172. {
  173. int i;
  174. struct atom_table *table = (struct atom_table *)obj;
  175. assert( obj->ops == &atom_table_ops );
  176. if (table->handles)
  177. {
  178. for (i = 0; i <= table->last; i++) free( table->handles[i] );
  179. free( table->handles );
  180. }
  181. free( table->entries );
  182. }
  183. /* find an atom entry in its hash list */
  184. static struct atom_entry *find_atom_entry( struct atom_table *table, const struct unicode_str *str,
  185. unsigned short hash )
  186. {
  187. struct atom_entry *entry = table->entries[hash];
  188. while (entry)
  189. {
  190. if (entry->len == str->len && !memicmp_strW( entry->str, str->str, str->len )) break;
  191. entry = entry->next;
  192. }
  193. return entry;
  194. }
  195. /* add an atom to the table */
  196. static atom_t add_atom( struct atom_table *table, const struct unicode_str *str )
  197. {
  198. struct atom_entry *entry;
  199. unsigned short hash = hash_strW( str->str, str->len, table->entries_count );
  200. atom_t atom = 0;
  201. if (!str->len)
  202. {
  203. set_error( STATUS_OBJECT_NAME_INVALID );
  204. return 0;
  205. }
  206. if (str->len > MAX_ATOM_LEN)
  207. {
  208. set_error( STATUS_INVALID_PARAMETER );
  209. return 0;
  210. }
  211. if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
  212. {
  213. entry->count++;
  214. return entry->atom;
  215. }
  216. if ((entry = mem_alloc( FIELD_OFFSET( struct atom_entry, str[str->len / sizeof(WCHAR)] ) )))
  217. {
  218. if ((atom = add_atom_entry( table, entry )))
  219. {
  220. entry->prev = NULL;
  221. if ((entry->next = table->entries[hash])) entry->next->prev = entry;
  222. table->entries[hash] = entry;
  223. entry->count = 1;
  224. entry->pinned = 0;
  225. entry->hash = hash;
  226. entry->len = str->len;
  227. memcpy( entry->str, str->str, str->len );
  228. }
  229. else free( entry );
  230. }
  231. else set_error( STATUS_NO_MEMORY );
  232. return atom;
  233. }
  234. /* delete an atom from the table */
  235. static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
  236. {
  237. struct atom_entry *entry = get_atom_entry( table, atom );
  238. if (!entry) return;
  239. if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
  240. else if (!--entry->count)
  241. {
  242. if (entry->next) entry->next->prev = entry->prev;
  243. if (entry->prev) entry->prev->next = entry->next;
  244. else table->entries[entry->hash] = entry->next;
  245. table->handles[atom - MIN_STR_ATOM] = NULL;
  246. free( entry );
  247. }
  248. }
  249. /* find an atom in the table */
  250. static atom_t find_atom( struct atom_table *table, const struct unicode_str *str )
  251. {
  252. struct atom_entry *entry;
  253. if (!str->len)
  254. {
  255. set_error( STATUS_OBJECT_NAME_INVALID );
  256. return 0;
  257. }
  258. if (str->len > MAX_ATOM_LEN)
  259. {
  260. set_error( STATUS_INVALID_PARAMETER );
  261. return 0;
  262. }
  263. if (table && (entry = find_atom_entry( table, str,
  264. hash_strW( str->str, str->len, table->entries_count ))))
  265. return entry->atom;
  266. set_error( STATUS_OBJECT_NAME_NOT_FOUND );
  267. return 0;
  268. }
  269. static struct atom_table *get_global_table( struct winstation *winstation, int create )
  270. {
  271. struct atom_table *table = winstation ? winstation->atom_table : global_table;
  272. if (!table)
  273. {
  274. if (create)
  275. {
  276. table = create_table( HASH_SIZE );
  277. if (winstation) winstation->atom_table = table;
  278. else
  279. {
  280. global_table = table;
  281. make_object_permanent( &global_table->obj );
  282. }
  283. }
  284. else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
  285. }
  286. return table;
  287. }
  288. /* add an atom in the global table; used for window properties */
  289. atom_t add_global_atom( struct winstation *winstation, const struct unicode_str *str )
  290. {
  291. struct atom_table *table = get_global_table( winstation, 1 );
  292. if (!table) return 0;
  293. return add_atom( table, str );
  294. }
  295. /* find an atom in the global table; used for window properties */
  296. atom_t find_global_atom( struct winstation *winstation, const struct unicode_str *str )
  297. {
  298. struct atom_table *table = get_global_table( winstation, 0 );
  299. struct atom_entry *entry;
  300. if (!str->len || str->len > MAX_ATOM_LEN || !table) return 0;
  301. if ((entry = find_atom_entry( table, str, hash_strW( str->str, str->len, table->entries_count ))))
  302. return entry->atom;
  303. return 0;
  304. }
  305. /* increment the ref count of a global atom; used for window properties */
  306. int grab_global_atom( struct winstation *winstation, atom_t atom )
  307. {
  308. if (atom >= MIN_STR_ATOM)
  309. {
  310. struct atom_table *table = get_global_table( winstation, 0 );
  311. if (table)
  312. {
  313. struct atom_entry *entry = get_atom_entry( table, atom );
  314. if (entry) entry->count++;
  315. return (entry != NULL);
  316. }
  317. else return 0;
  318. }
  319. else return 1;
  320. }
  321. /* decrement the ref count of a global atom; used for window properties */
  322. void release_global_atom( struct winstation *winstation, atom_t atom )
  323. {
  324. if (atom >= MIN_STR_ATOM)
  325. {
  326. struct atom_table *table = get_global_table( winstation, 0 );
  327. if (table) delete_atom( table, atom, 1 );
  328. }
  329. }
  330. /* add a global atom */
  331. DECL_HANDLER(add_atom)
  332. {
  333. struct unicode_str name = get_req_unicode_str();
  334. struct atom_table *table = get_global_table( NULL, 1 );
  335. if (table) reply->atom = add_atom( table, &name );
  336. }
  337. /* delete a global atom */
  338. DECL_HANDLER(delete_atom)
  339. {
  340. struct atom_table *table = get_global_table( NULL, 0 );
  341. if (table) delete_atom( table, req->atom, 0 );
  342. }
  343. /* find a global atom */
  344. DECL_HANDLER(find_atom)
  345. {
  346. struct unicode_str name = get_req_unicode_str();
  347. struct atom_table *table = get_global_table( NULL, 0 );
  348. if (table) reply->atom = find_atom( table, &name );
  349. }
  350. /* get global atom name */
  351. DECL_HANDLER(get_atom_information)
  352. {
  353. struct atom_table *table = get_global_table( NULL, 0 );
  354. if (table)
  355. {
  356. struct atom_entry *entry;
  357. if ((entry = get_atom_entry( table, req->atom )))
  358. {
  359. set_reply_data( entry->str, min( entry->len, get_reply_max_size() ));
  360. reply->count = entry->count;
  361. reply->pinned = entry->pinned;
  362. reply->total = entry->len;
  363. }
  364. else reply->count = -1;
  365. }
  366. }