atom.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. atom_table_dump, /* dump */
  69. no_get_type, /* get_type */
  70. no_add_queue, /* add_queue */
  71. NULL, /* remove_queue */
  72. NULL, /* signaled */
  73. NULL, /* get_esync_fd */
  74. NULL, /* satisfied */
  75. no_signal, /* signal */
  76. no_get_fd, /* get_fd */
  77. no_map_access, /* map_access */
  78. default_get_sd, /* get_sd */
  79. default_set_sd, /* set_sd */
  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_alloc_handle, /* alloc_handle */
  86. no_close_handle, /* close_handle */
  87. atom_table_destroy /* destroy */
  88. };
  89. static struct atom_table *global_table;
  90. /* create an atom table */
  91. static struct atom_table *create_table(int entries_count)
  92. {
  93. struct atom_table *table;
  94. if ((table = alloc_object( &atom_table_ops )))
  95. {
  96. if ((entries_count < MIN_HASH_SIZE) ||
  97. (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
  98. table->handles = NULL;
  99. table->entries_count = entries_count;
  100. if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
  101. {
  102. set_error( STATUS_NO_MEMORY );
  103. goto fail;
  104. }
  105. memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
  106. table->count = 64;
  107. table->last = -1;
  108. if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
  109. return table;
  110. fail:
  111. release_object( table );
  112. table = NULL;
  113. }
  114. return table;
  115. }
  116. /* retrieve an entry pointer from its atom */
  117. static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
  118. {
  119. struct atom_entry *entry = NULL;
  120. if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
  121. entry = table->handles[atom - MIN_STR_ATOM];
  122. if (!entry) set_error( STATUS_INVALID_HANDLE );
  123. return entry;
  124. }
  125. /* add an atom entry in the table and return its handle */
  126. static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
  127. {
  128. int i;
  129. for (i = 0; i <= table->last; i++)
  130. if (!table->handles[i]) goto found;
  131. if (i == table->count)
  132. {
  133. struct atom_entry **new_table = NULL;
  134. int new_size = table->count + table->count / 2;
  135. if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
  136. if (new_size > table->count)
  137. new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
  138. if (!new_table)
  139. {
  140. set_error( STATUS_NO_MEMORY );
  141. return 0;
  142. }
  143. table->count = new_size;
  144. table->handles = new_table;
  145. }
  146. table->last = i;
  147. found:
  148. table->handles[i] = entry;
  149. entry->atom = i + MIN_STR_ATOM;
  150. return entry->atom;
  151. }
  152. /* dump an atom table */
  153. static void atom_table_dump( struct object *obj, int verbose )
  154. {
  155. int i;
  156. struct atom_table *table = (struct atom_table *)obj;
  157. assert( obj->ops == &atom_table_ops );
  158. fprintf( stderr, "Atom table size=%d entries=%d\n",
  159. table->last + 1, table->entries_count );
  160. if (!verbose) return;
  161. for (i = 0; i <= table->last; i++)
  162. {
  163. struct atom_entry *entry = table->handles[i];
  164. if (!entry) continue;
  165. fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
  166. entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
  167. dump_strW( entry->str, entry->len, stderr, "\"\"");
  168. fprintf( stderr, "\"\n" );
  169. }
  170. }
  171. /* destroy the atom table */
  172. static void atom_table_destroy( struct object *obj )
  173. {
  174. int i;
  175. struct atom_table *table = (struct atom_table *)obj;
  176. assert( obj->ops == &atom_table_ops );
  177. if (table->handles)
  178. {
  179. for (i = 0; i <= table->last; i++) free( table->handles[i] );
  180. free( table->handles );
  181. }
  182. free( table->entries );
  183. }
  184. /* find an atom entry in its hash list */
  185. static struct atom_entry *find_atom_entry( struct atom_table *table, const struct unicode_str *str,
  186. unsigned short hash )
  187. {
  188. struct atom_entry *entry = table->entries[hash];
  189. while (entry)
  190. {
  191. if (entry->len == str->len && !memicmp_strW( entry->str, str->str, str->len )) break;
  192. entry = entry->next;
  193. }
  194. return entry;
  195. }
  196. /* add an atom to the table */
  197. static atom_t add_atom( struct atom_table *table, const struct unicode_str *str )
  198. {
  199. struct atom_entry *entry;
  200. unsigned short hash = hash_strW( str->str, str->len, table->entries_count );
  201. atom_t atom = 0;
  202. if (!str->len)
  203. {
  204. set_error( STATUS_OBJECT_NAME_INVALID );
  205. return 0;
  206. }
  207. if (str->len > MAX_ATOM_LEN)
  208. {
  209. set_error( STATUS_INVALID_PARAMETER );
  210. return 0;
  211. }
  212. if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
  213. {
  214. entry->count++;
  215. return entry->atom;
  216. }
  217. if ((entry = mem_alloc( FIELD_OFFSET( struct atom_entry, str[str->len / sizeof(WCHAR)] ) )))
  218. {
  219. if ((atom = add_atom_entry( table, entry )))
  220. {
  221. entry->prev = NULL;
  222. if ((entry->next = table->entries[hash])) entry->next->prev = entry;
  223. table->entries[hash] = entry;
  224. entry->count = 1;
  225. entry->pinned = 0;
  226. entry->hash = hash;
  227. entry->len = str->len;
  228. memcpy( entry->str, str->str, str->len );
  229. }
  230. else free( entry );
  231. }
  232. else set_error( STATUS_NO_MEMORY );
  233. return atom;
  234. }
  235. /* delete an atom from the table */
  236. static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
  237. {
  238. struct atom_entry *entry = get_atom_entry( table, atom );
  239. if (!entry) return;
  240. if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
  241. else if (!--entry->count)
  242. {
  243. if (entry->next) entry->next->prev = entry->prev;
  244. if (entry->prev) entry->prev->next = entry->next;
  245. else table->entries[entry->hash] = entry->next;
  246. table->handles[atom - MIN_STR_ATOM] = NULL;
  247. free( entry );
  248. }
  249. }
  250. /* find an atom in the table */
  251. static atom_t find_atom( struct atom_table *table, const struct unicode_str *str )
  252. {
  253. struct atom_entry *entry;
  254. if (!str->len)
  255. {
  256. set_error( STATUS_OBJECT_NAME_INVALID );
  257. return 0;
  258. }
  259. if (str->len > MAX_ATOM_LEN)
  260. {
  261. set_error( STATUS_INVALID_PARAMETER );
  262. return 0;
  263. }
  264. if (table && (entry = find_atom_entry( table, str,
  265. hash_strW( str->str, str->len, table->entries_count ))))
  266. return entry->atom;
  267. set_error( STATUS_OBJECT_NAME_NOT_FOUND );
  268. return 0;
  269. }
  270. static struct atom_table *get_global_table( struct winstation *winstation, int create )
  271. {
  272. struct atom_table *table = winstation ? winstation->atom_table : global_table;
  273. if (!table)
  274. {
  275. if (create)
  276. {
  277. table = create_table( HASH_SIZE );
  278. if (winstation) winstation->atom_table = table;
  279. else
  280. {
  281. global_table = table;
  282. make_object_static( &global_table->obj );
  283. }
  284. }
  285. else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
  286. }
  287. return table;
  288. }
  289. static struct atom_table *get_table( obj_handle_t h, int create )
  290. {
  291. struct atom_table *table = NULL;
  292. if (h)
  293. {
  294. table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
  295. }
  296. else
  297. {
  298. table = get_global_table( NULL, 1 );
  299. if (table) grab_object( table );
  300. }
  301. return table;
  302. }
  303. /* add an atom in the global table; used for window properties */
  304. atom_t add_global_atom( struct winstation *winstation, const struct unicode_str *str )
  305. {
  306. struct atom_table *table = get_global_table( winstation, 1 );
  307. if (!table) return 0;
  308. return add_atom( table, str );
  309. }
  310. /* find an atom in the global table; used for window properties */
  311. atom_t find_global_atom( struct winstation *winstation, const struct unicode_str *str )
  312. {
  313. struct atom_table *table = get_global_table( winstation, 0 );
  314. struct atom_entry *entry;
  315. if (!str->len || str->len > MAX_ATOM_LEN || !table) return 0;
  316. if ((entry = find_atom_entry( table, str, hash_strW( str->str, str->len, table->entries_count ))))
  317. return entry->atom;
  318. return 0;
  319. }
  320. /* increment the ref count of a global atom; used for window properties */
  321. int grab_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)
  327. {
  328. struct atom_entry *entry = get_atom_entry( table, atom );
  329. if (entry) entry->count++;
  330. return (entry != NULL);
  331. }
  332. else return 0;
  333. }
  334. else return 1;
  335. }
  336. /* decrement the ref count of a global atom; used for window properties */
  337. void release_global_atom( struct winstation *winstation, atom_t atom )
  338. {
  339. if (atom >= MIN_STR_ATOM)
  340. {
  341. struct atom_table *table = get_global_table( winstation, 0 );
  342. if (table) delete_atom( table, atom, 1 );
  343. }
  344. }
  345. /* add a global atom */
  346. DECL_HANDLER(add_atom)
  347. {
  348. struct unicode_str name = get_req_unicode_str();
  349. struct atom_table *table = get_table( req->table, 1 );
  350. if (table)
  351. {
  352. reply->atom = add_atom( table, &name );
  353. release_object( table );
  354. }
  355. }
  356. /* delete a global atom */
  357. DECL_HANDLER(delete_atom)
  358. {
  359. struct atom_table *table = get_table( req->table, 0 );
  360. if (table)
  361. {
  362. delete_atom( table, req->atom, 0 );
  363. release_object( table );
  364. }
  365. }
  366. /* find a global atom */
  367. DECL_HANDLER(find_atom)
  368. {
  369. struct unicode_str name = get_req_unicode_str();
  370. struct atom_table *table = get_table( req->table, 0 );
  371. if (table)
  372. {
  373. reply->atom = find_atom( table, &name );
  374. release_object( table );
  375. }
  376. }
  377. /* get global atom name */
  378. DECL_HANDLER(get_atom_information)
  379. {
  380. struct atom_table *table = get_table( req->table, 0 );
  381. if (table)
  382. {
  383. struct atom_entry *entry;
  384. if ((entry = get_atom_entry( table, req->atom )))
  385. {
  386. set_reply_data( entry->str, min( entry->len, get_reply_max_size() ));
  387. reply->count = entry->count;
  388. reply->pinned = entry->pinned;
  389. reply->total = entry->len;
  390. }
  391. else reply->count = -1;
  392. release_object( table );
  393. }
  394. }
  395. /* set global atom name */
  396. DECL_HANDLER(set_atom_information)
  397. {
  398. struct atom_table *table = get_table( req->table, 0 );
  399. if (table)
  400. {
  401. struct atom_entry *entry;
  402. if ((entry = get_atom_entry( table, req->atom )))
  403. {
  404. if (req->pinned) entry->pinned = 1;
  405. }
  406. release_object( table );
  407. }
  408. }
  409. /* init a (local) atom table */
  410. DECL_HANDLER(init_atom_table)
  411. {
  412. struct atom_table* table = create_table( req->entries );
  413. if (table)
  414. {
  415. reply->table = alloc_handle( current->process, table, 0, 0 );
  416. release_object( table );
  417. }
  418. }
  419. /* set global atom name */
  420. DECL_HANDLER(empty_atom_table)
  421. {
  422. struct atom_table *table = get_table( req->table, 1 );
  423. if (table)
  424. {
  425. int i;
  426. struct atom_entry *entry;
  427. for (i = 0; i <= table->last; i++)
  428. {
  429. entry = table->handles[i];
  430. if (entry && (!entry->pinned || req->if_pinned))
  431. {
  432. if (entry->next) entry->next->prev = entry->prev;
  433. if (entry->prev) entry->prev->next = entry->next;
  434. else table->entries[entry->hash] = entry->next;
  435. table->handles[i] = NULL;
  436. free( entry );
  437. }
  438. }
  439. release_object( table );
  440. }
  441. }