atom.c 12 KB

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