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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_lookup_name, /* lookup_name */
86 no_close_handle, /* close_handle */
87 atom_table_destroy /* destroy */
91 /* create an atom table */
92 static struct atom_table *create_table(int entries_count)
94 struct atom_table *table;
96 if ((table = alloc_object( &atom_table_ops )))
98 if ((entries_count < MIN_HASH_SIZE) ||
99 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
100 table->entries_count = entries_count;
101 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
103 set_error( STATUS_NO_MEMORY );
106 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
109 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
112 release_object( table );
118 /* retrieve an entry pointer from its atom */
119 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
121 struct atom_entry *entry = NULL;
122 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
123 entry = table->handles[atom - MIN_STR_ATOM];
124 if (!entry) set_error( STATUS_INVALID_HANDLE );
128 /* add an atom entry in the table and return its handle */
129 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
132 for (i = 0; i <= table->last; i++)
133 if (!table->handles[i]) goto found;
134 if (i == table->count)
136 struct atom_entry **new_table = NULL;
137 int new_size = table->count + table->count / 2;
138 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
139 if (new_size > table->count)
140 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
143 set_error( STATUS_NO_MEMORY );
146 table->count = new_size;
147 table->handles = new_table;
151 table->handles[i] = entry;
152 entry->atom = i + MIN_STR_ATOM;
156 /* compute the hash code for a string */
157 static unsigned short atom_hash( struct atom_table *table, const WCHAR *str, size_t len )
160 unsigned short hash = 0;
161 for (i = 0; i < len; i++) hash ^= toupperW(str[i]) + i;
162 return hash % table->entries_count;
165 /* dump an atom table */
166 static void atom_table_dump( struct object *obj, int verbose )
169 struct atom_table *table = (struct atom_table *)obj;
170 assert( obj->ops == &atom_table_ops );
172 fprintf( stderr, "Atom table size=%d entries=%d\n",
173 table->last + 1, table->entries_count );
174 if (!verbose) return;
175 for (i = 0; i <= table->last; i++)
177 struct atom_entry *entry = table->handles[i];
178 if (!entry) continue;
179 fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
180 entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
181 dump_strW( entry->str, entry->len, stderr, "\"\"");
182 fprintf( stderr, "\"\n" );
186 /* destroy the atom table */
187 static void atom_table_destroy( struct object *obj )
190 struct atom_table *table = (struct atom_table *)obj;
191 assert( obj->ops == &atom_table_ops );
194 for (i = 0; i <= table->last; i++) free( table->handles[i] );
195 free( table->handles );
197 if (table->entries) free( table->entries );
200 /* find an atom entry in its hash list */
201 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
202 size_t len, unsigned short hash )
204 struct atom_entry *entry = table->entries[hash];
207 if (entry->len == len && !memicmpW( entry->str, str, len )) break;
213 /* add an atom to the table */
214 static atom_t add_atom( struct atom_table *table, const WCHAR *str, size_t len )
216 struct atom_entry *entry;
217 unsigned short hash = atom_hash( table, str, len );
222 set_error( STATUS_OBJECT_NAME_INVALID );
225 if (len > MAX_ATOM_LEN)
227 set_error( STATUS_INVALID_PARAMETER );
230 if ((entry = find_atom_entry( table, str, len, hash ))) /* exists already */
236 if ((entry = mem_alloc( sizeof(*entry) + (len - 1) * sizeof(WCHAR) )))
238 if ((atom = add_atom_entry( table, entry )))
241 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
242 table->entries[hash] = entry;
247 memcpy( entry->str, str, len * sizeof(WCHAR) );
251 else set_error( STATUS_NO_MEMORY );
255 /* delete an atom from the table */
256 static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
258 struct atom_entry *entry = get_atom_entry( table, atom );
260 if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
261 else if (!--entry->count)
263 if (entry->next) entry->next->prev = entry->prev;
264 if (entry->prev) entry->prev->next = entry->next;
265 else table->entries[entry->hash] = entry->next;
266 table->handles[atom - MIN_STR_ATOM] = NULL;
271 /* find an atom in the table */
272 static atom_t find_atom( struct atom_table *table, const WCHAR *str, size_t len )
274 struct atom_entry *entry;
278 set_error( STATUS_OBJECT_NAME_INVALID );
281 if (len > MAX_ATOM_LEN)
283 set_error( STATUS_INVALID_PARAMETER );
286 if (table && (entry = find_atom_entry( table, str, len, atom_hash(table, str, len) )))
288 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
292 static struct atom_table *get_global_table( struct winstation *winstation, int create )
294 if (!winstation->atom_table)
296 if (create) winstation->atom_table = create_table( HASH_SIZE );
297 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
299 return winstation->atom_table;
302 static struct atom_table *get_table( obj_handle_t h, int create )
304 struct atom_table *table = NULL;
308 table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
312 struct winstation *winstation = get_process_winstation( current->process,
313 WINSTA_ACCESSGLOBALATOMS );
316 table = get_global_table( winstation, 1 );
317 if (table) grab_object( table );
318 release_object( winstation );
324 /* add an atom in the global table; used for window properties */
325 atom_t add_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
327 struct atom_table *global_table = get_global_table( winstation, 1 );
328 if (!global_table) return 0;
329 return add_atom( global_table, str, len );
332 /* find an atom in the global table; used for window properties */
333 atom_t find_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
335 struct atom_table *global_table = get_global_table( winstation, 0 );
336 struct atom_entry *entry;
338 if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
339 if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
344 /* increment the ref count of a global atom; used for window properties */
345 int grab_global_atom( struct winstation *winstation, atom_t atom )
347 if (atom >= MIN_STR_ATOM)
349 struct atom_table *global_table = get_global_table( winstation, 0 );
352 struct atom_entry *entry = get_atom_entry( global_table, atom );
353 if (entry) entry->count++;
354 return (entry != NULL);
361 /* decrement the ref count of a global atom; used for window properties */
362 void release_global_atom( struct winstation *winstation, atom_t atom )
364 if (atom >= MIN_STR_ATOM)
366 struct atom_table *global_table = get_global_table( winstation, 0 );
367 if (global_table) delete_atom( global_table, atom, 1 );
371 /* add a global atom */
372 DECL_HANDLER(add_atom)
374 struct atom_table *table = get_table( req->table, 1 );
377 reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
378 release_object( table );
382 /* delete a global atom */
383 DECL_HANDLER(delete_atom)
385 struct atom_table *table = get_table( req->table, 0 );
388 delete_atom( table, req->atom, 0 );
389 release_object( table );
393 /* find a global atom */
394 DECL_HANDLER(find_atom)
396 struct atom_table *table = get_table( req->table, 0 );
399 reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
400 release_object( table );
404 /* get global atom name */
405 DECL_HANDLER(get_atom_information)
407 struct atom_table *table = get_table( req->table, 0 );
410 struct atom_entry *entry;
412 if ((entry = get_atom_entry( table, req->atom )))
414 size_t len = entry->len * sizeof(WCHAR);
415 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
416 else if (get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
417 reply->count = entry->count;
418 reply->pinned = entry->pinned;
420 else reply->count = -1;
421 release_object( table );
425 /* set global atom name */
426 DECL_HANDLER(set_atom_information)
428 struct atom_table *table = get_table( req->table, 0 );
431 struct atom_entry *entry;
433 if ((entry = get_atom_entry( table, req->atom )))
435 if (req->pinned) entry->pinned = 1;
437 release_object( table );
441 /* init a (local) atom table */
442 DECL_HANDLER(init_atom_table)
444 struct atom_table* table = create_table( req->entries );
448 reply->table = alloc_handle( current->process, table, 0, FALSE);
449 release_object( table );
453 /* set global atom name */
454 DECL_HANDLER(empty_atom_table)
456 struct atom_table *table = get_table( req->table, 1 );
460 struct atom_entry *entry;
462 for (i = 0; i <= table->last; i++)
464 entry = table->handles[i];
465 if (entry && (!entry->pinned || req->if_pinned))
467 if (entry->next) entry->next->prev = entry->prev;
468 if (entry->prev) entry->prev->next = entry->next;
469 else table->entries[entry->hash] = entry->next;
470 table->handles[i] = NULL;
474 release_object( table );