2 * Server-side atom management
4 * Copyright (C) 1999, 2000 Alexandre Julliard
5 * Copyright (C) 2000 Turchanov Sergei
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.
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.
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
23 #include "wine/port.h"
31 #define WIN32_NO_STATUS
43 #define MIN_HASH_SIZE 4
44 #define MAX_HASH_SIZE 0x200
46 #define MAX_ATOM_LEN 255
47 #define MIN_STR_ATOM 0xc000
48 #define MAX_ATOMS 0x4000
52 struct atom_entry *next; /* hash table list */
53 struct atom_entry *prev; /* hash table list */
54 int count; /* reference count */
55 short pinned; /* whether the atom is pinned or not */
56 atom_t atom; /* atom handle */
57 unsigned short hash; /* string hash */
58 unsigned short len; /* string len */
59 WCHAR str[1]; /* atom string */
64 struct object obj; /* object header */
65 int count; /* count of atom handles */
66 int last; /* last handle in-use */
67 struct atom_entry **handles; /* atom handles */
68 int entries_count; /* humber of hash entries */
69 struct atom_entry **entries; /* hash table entries */
72 static void atom_table_dump( struct object *obj, int verbose );
73 static void atom_table_destroy( struct object *obj );
75 static const struct object_ops atom_table_ops =
77 sizeof(struct atom_table), /* size */
78 atom_table_dump, /* dump */
79 no_add_queue, /* add_queue */
80 NULL, /* remove_queue */
83 no_signal, /* signal */
84 no_get_fd, /* get_fd */
85 no_map_access, /* map_access */
86 default_get_sd, /* get_sd */
87 default_set_sd, /* set_sd */
88 no_lookup_name, /* lookup_name */
89 no_open_file, /* open_file */
90 no_close_handle, /* close_handle */
91 atom_table_destroy /* destroy */
94 static struct atom_table *global_table;
96 /* create an atom table */
97 static struct atom_table *create_table(int entries_count)
99 struct atom_table *table;
101 if ((table = alloc_object( &atom_table_ops )))
103 if ((entries_count < MIN_HASH_SIZE) ||
104 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
105 table->entries_count = entries_count;
106 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
108 set_error( STATUS_NO_MEMORY );
111 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
114 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
117 release_object( table );
123 /* retrieve an entry pointer from its atom */
124 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
126 struct atom_entry *entry = NULL;
127 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
128 entry = table->handles[atom - MIN_STR_ATOM];
129 if (!entry) set_error( STATUS_INVALID_HANDLE );
133 /* add an atom entry in the table and return its handle */
134 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
137 for (i = 0; i <= table->last; i++)
138 if (!table->handles[i]) goto found;
139 if (i == table->count)
141 struct atom_entry **new_table = NULL;
142 int new_size = table->count + table->count / 2;
143 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
144 if (new_size > table->count)
145 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
148 set_error( STATUS_NO_MEMORY );
151 table->count = new_size;
152 table->handles = new_table;
156 table->handles[i] = entry;
157 entry->atom = i + MIN_STR_ATOM;
161 /* compute the hash code for a string */
162 static unsigned short atom_hash( struct atom_table *table, const WCHAR *str, data_size_t len )
165 unsigned short hash = 0;
166 for (i = 0; i < len; i++) hash ^= toupperW(str[i]) + i;
167 return hash % table->entries_count;
170 /* dump an atom table */
171 static void atom_table_dump( struct object *obj, int verbose )
174 struct atom_table *table = (struct atom_table *)obj;
175 assert( obj->ops == &atom_table_ops );
177 fprintf( stderr, "Atom table size=%d entries=%d\n",
178 table->last + 1, table->entries_count );
179 if (!verbose) return;
180 for (i = 0; i <= table->last; i++)
182 struct atom_entry *entry = table->handles[i];
183 if (!entry) continue;
184 fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
185 entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
186 dump_strW( entry->str, entry->len, stderr, "\"\"");
187 fprintf( stderr, "\"\n" );
191 /* destroy the atom table */
192 static void atom_table_destroy( struct object *obj )
195 struct atom_table *table = (struct atom_table *)obj;
196 assert( obj->ops == &atom_table_ops );
199 for (i = 0; i <= table->last; i++) free( table->handles[i] );
200 free( table->handles );
202 free( table->entries );
205 /* find an atom entry in its hash list */
206 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
207 data_size_t len, unsigned short hash )
209 struct atom_entry *entry = table->entries[hash];
212 if (entry->len == len && !memicmpW( entry->str, str, len )) break;
218 /* add an atom to the table */
219 static atom_t add_atom( struct atom_table *table, const WCHAR *str, data_size_t len )
221 struct atom_entry *entry;
222 unsigned short hash = atom_hash( table, str, len );
227 set_error( STATUS_OBJECT_NAME_INVALID );
230 if (len > MAX_ATOM_LEN)
232 set_error( STATUS_INVALID_PARAMETER );
235 if ((entry = find_atom_entry( table, str, len, hash ))) /* exists already */
241 if ((entry = mem_alloc( sizeof(*entry) + (len - 1) * sizeof(WCHAR) )))
243 if ((atom = add_atom_entry( table, entry )))
246 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
247 table->entries[hash] = entry;
252 memcpy( entry->str, str, len * sizeof(WCHAR) );
256 else set_error( STATUS_NO_MEMORY );
260 /* delete an atom from the table */
261 static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
263 struct atom_entry *entry = get_atom_entry( table, atom );
265 if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
266 else if (!--entry->count)
268 if (entry->next) entry->next->prev = entry->prev;
269 if (entry->prev) entry->prev->next = entry->next;
270 else table->entries[entry->hash] = entry->next;
271 table->handles[atom - MIN_STR_ATOM] = NULL;
276 /* find an atom in the table */
277 static atom_t find_atom( struct atom_table *table, const WCHAR *str, data_size_t len )
279 struct atom_entry *entry;
283 set_error( STATUS_OBJECT_NAME_INVALID );
286 if (len > MAX_ATOM_LEN)
288 set_error( STATUS_INVALID_PARAMETER );
291 if (table && (entry = find_atom_entry( table, str, len, atom_hash(table, str, len) )))
293 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
297 static struct atom_table *get_global_table( struct winstation *winstation, int create )
299 struct atom_table *table = winstation ? winstation->atom_table : global_table;
304 table = create_table( HASH_SIZE );
305 if (winstation) winstation->atom_table = table;
308 global_table = table;
309 make_object_static( &global_table->obj );
312 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
317 static struct atom_table *get_table( obj_handle_t h, int create )
319 struct atom_table *table = NULL;
323 table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
327 table = get_global_table( NULL, 1 );
328 if (table) grab_object( table );
333 /* add an atom in the global table; used for window properties */
334 atom_t add_global_atom( struct winstation *winstation, const WCHAR *str, data_size_t len )
336 struct atom_table *global_table = get_global_table( winstation, 1 );
337 if (!global_table) return 0;
338 return add_atom( global_table, str, len );
341 /* find an atom in the global table; used for window properties */
342 atom_t find_global_atom( struct winstation *winstation, const WCHAR *str, data_size_t len )
344 struct atom_table *global_table = get_global_table( winstation, 0 );
345 struct atom_entry *entry;
347 if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
348 if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
353 /* increment the ref count of a global atom; used for window properties */
354 int grab_global_atom( struct winstation *winstation, atom_t atom )
356 if (atom >= MIN_STR_ATOM)
358 struct atom_table *global_table = get_global_table( winstation, 0 );
361 struct atom_entry *entry = get_atom_entry( global_table, atom );
362 if (entry) entry->count++;
363 return (entry != NULL);
370 /* decrement the ref count of a global atom; used for window properties */
371 void release_global_atom( struct winstation *winstation, atom_t atom )
373 if (atom >= MIN_STR_ATOM)
375 struct atom_table *global_table = get_global_table( winstation, 0 );
376 if (global_table) delete_atom( global_table, atom, 1 );
380 /* add a global atom */
381 DECL_HANDLER(add_atom)
383 struct atom_table *table = get_table( req->table, 1 );
386 reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
387 release_object( table );
391 /* delete a global atom */
392 DECL_HANDLER(delete_atom)
394 struct atom_table *table = get_table( req->table, 0 );
397 delete_atom( table, req->atom, 0 );
398 release_object( table );
402 /* find a global atom */
403 DECL_HANDLER(find_atom)
405 struct atom_table *table = get_table( req->table, 0 );
408 reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
409 release_object( table );
413 /* get global atom name */
414 DECL_HANDLER(get_atom_information)
416 struct atom_table *table = get_table( req->table, 0 );
419 struct atom_entry *entry;
421 if ((entry = get_atom_entry( table, req->atom )))
423 data_size_t len = entry->len * sizeof(WCHAR);
424 if (get_reply_max_size())
425 set_reply_data( entry->str, min( len, get_reply_max_size()));
426 reply->count = entry->count;
427 reply->pinned = entry->pinned;
430 else reply->count = -1;
431 release_object( table );
435 /* set global atom name */
436 DECL_HANDLER(set_atom_information)
438 struct atom_table *table = get_table( req->table, 0 );
441 struct atom_entry *entry;
443 if ((entry = get_atom_entry( table, req->atom )))
445 if (req->pinned) entry->pinned = 1;
447 release_object( table );
451 /* init a (local) atom table */
452 DECL_HANDLER(init_atom_table)
454 struct atom_table* table = create_table( req->entries );
458 reply->table = alloc_handle( current->process, table, 0, 0 );
459 release_object( table );
463 /* set global atom name */
464 DECL_HANDLER(empty_atom_table)
466 struct atom_table *table = get_table( req->table, 1 );
470 struct atom_entry *entry;
472 for (i = 0; i <= table->last; i++)
474 entry = table->handles[i];
475 if (entry && (!entry->pinned || req->if_pinned))
477 if (entry->next) entry->next->prev = entry->prev;
478 if (entry->prev) entry->prev->next = entry->next;
479 else table->entries[entry->hash] = entry->next;
480 table->handles[i] = NULL;
484 release_object( table );