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 no_lookup_name, /* lookup_name */
87 no_close_handle, /* close_handle */
88 atom_table_destroy /* destroy */
91 static struct atom_table *global_table;
93 /* create an atom table */
94 static struct atom_table *create_table(int entries_count)
96 struct atom_table *table;
98 if ((table = alloc_object( &atom_table_ops )))
100 if ((entries_count < MIN_HASH_SIZE) ||
101 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
102 table->entries_count = entries_count;
103 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
105 set_error( STATUS_NO_MEMORY );
108 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
111 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
114 release_object( table );
120 /* retrieve an entry pointer from its atom */
121 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
123 struct atom_entry *entry = NULL;
124 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
125 entry = table->handles[atom - MIN_STR_ATOM];
126 if (!entry) set_error( STATUS_INVALID_HANDLE );
130 /* add an atom entry in the table and return its handle */
131 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
134 for (i = 0; i <= table->last; i++)
135 if (!table->handles[i]) goto found;
136 if (i == table->count)
138 struct atom_entry **new_table = NULL;
139 int new_size = table->count + table->count / 2;
140 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
141 if (new_size > table->count)
142 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
145 set_error( STATUS_NO_MEMORY );
148 table->count = new_size;
149 table->handles = new_table;
153 table->handles[i] = entry;
154 entry->atom = i + MIN_STR_ATOM;
158 /* compute the hash code for a string */
159 static unsigned short atom_hash( struct atom_table *table, const WCHAR *str, data_size_t len )
162 unsigned short hash = 0;
163 for (i = 0; i < len; i++) hash ^= toupperW(str[i]) + i;
164 return hash % table->entries_count;
167 /* dump an atom table */
168 static void atom_table_dump( struct object *obj, int verbose )
171 struct atom_table *table = (struct atom_table *)obj;
172 assert( obj->ops == &atom_table_ops );
174 fprintf( stderr, "Atom table size=%d entries=%d\n",
175 table->last + 1, table->entries_count );
176 if (!verbose) return;
177 for (i = 0; i <= table->last; i++)
179 struct atom_entry *entry = table->handles[i];
180 if (!entry) continue;
181 fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
182 entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
183 dump_strW( entry->str, entry->len, stderr, "\"\"");
184 fprintf( stderr, "\"\n" );
188 /* destroy the atom table */
189 static void atom_table_destroy( struct object *obj )
192 struct atom_table *table = (struct atom_table *)obj;
193 assert( obj->ops == &atom_table_ops );
196 for (i = 0; i <= table->last; i++) free( table->handles[i] );
197 free( table->handles );
199 free( table->entries );
202 /* find an atom entry in its hash list */
203 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
204 data_size_t len, unsigned short hash )
206 struct atom_entry *entry = table->entries[hash];
209 if (entry->len == len && !memicmpW( entry->str, str, len )) break;
215 /* add an atom to the table */
216 static atom_t add_atom( struct atom_table *table, const WCHAR *str, data_size_t len )
218 struct atom_entry *entry;
219 unsigned short hash = atom_hash( table, str, len );
224 set_error( STATUS_OBJECT_NAME_INVALID );
227 if (len > MAX_ATOM_LEN)
229 set_error( STATUS_INVALID_PARAMETER );
232 if ((entry = find_atom_entry( table, str, len, hash ))) /* exists already */
238 if ((entry = mem_alloc( sizeof(*entry) + (len - 1) * sizeof(WCHAR) )))
240 if ((atom = add_atom_entry( table, entry )))
243 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
244 table->entries[hash] = entry;
249 memcpy( entry->str, str, len * sizeof(WCHAR) );
253 else set_error( STATUS_NO_MEMORY );
257 /* delete an atom from the table */
258 static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
260 struct atom_entry *entry = get_atom_entry( table, atom );
262 if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
263 else if (!--entry->count)
265 if (entry->next) entry->next->prev = entry->prev;
266 if (entry->prev) entry->prev->next = entry->next;
267 else table->entries[entry->hash] = entry->next;
268 table->handles[atom - MIN_STR_ATOM] = NULL;
273 /* find an atom in the table */
274 static atom_t find_atom( struct atom_table *table, const WCHAR *str, data_size_t len )
276 struct atom_entry *entry;
280 set_error( STATUS_OBJECT_NAME_INVALID );
283 if (len > MAX_ATOM_LEN)
285 set_error( STATUS_INVALID_PARAMETER );
288 if (table && (entry = find_atom_entry( table, str, len, atom_hash(table, str, len) )))
290 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
294 static struct atom_table *get_global_table( struct winstation *winstation, int create )
296 struct atom_table *table = winstation ? winstation->atom_table : global_table;
301 table = create_table( HASH_SIZE );
302 if (winstation) winstation->atom_table = table;
305 global_table = table;
306 make_object_static( &global_table->obj );
309 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
314 static struct atom_table *get_table( obj_handle_t h, int create )
316 struct atom_table *table = NULL;
320 table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
324 table = get_global_table( NULL, 1 );
325 if (table) grab_object( table );
330 /* add an atom in the global table; used for window properties */
331 atom_t add_global_atom( struct winstation *winstation, const WCHAR *str, data_size_t len )
333 struct atom_table *global_table = get_global_table( winstation, 1 );
334 if (!global_table) return 0;
335 return add_atom( global_table, str, len );
338 /* find an atom in the global table; used for window properties */
339 atom_t find_global_atom( struct winstation *winstation, const WCHAR *str, data_size_t len )
341 struct atom_table *global_table = get_global_table( winstation, 0 );
342 struct atom_entry *entry;
344 if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
345 if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
350 /* increment the ref count of a global atom; used for window properties */
351 int grab_global_atom( struct winstation *winstation, atom_t atom )
353 if (atom >= MIN_STR_ATOM)
355 struct atom_table *global_table = get_global_table( winstation, 0 );
358 struct atom_entry *entry = get_atom_entry( global_table, atom );
359 if (entry) entry->count++;
360 return (entry != NULL);
367 /* decrement the ref count of a global atom; used for window properties */
368 void release_global_atom( struct winstation *winstation, atom_t atom )
370 if (atom >= MIN_STR_ATOM)
372 struct atom_table *global_table = get_global_table( winstation, 0 );
373 if (global_table) delete_atom( global_table, atom, 1 );
377 /* add a global atom */
378 DECL_HANDLER(add_atom)
380 struct atom_table *table = get_table( req->table, 1 );
383 reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
384 release_object( table );
388 /* delete a global atom */
389 DECL_HANDLER(delete_atom)
391 struct atom_table *table = get_table( req->table, 0 );
394 delete_atom( table, req->atom, 0 );
395 release_object( table );
399 /* find a global atom */
400 DECL_HANDLER(find_atom)
402 struct atom_table *table = get_table( req->table, 0 );
405 reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
406 release_object( table );
410 /* get global atom name */
411 DECL_HANDLER(get_atom_information)
413 struct atom_table *table = get_table( req->table, 0 );
416 struct atom_entry *entry;
418 if ((entry = get_atom_entry( table, req->atom )))
420 data_size_t len = entry->len * sizeof(WCHAR);
421 if (get_reply_max_size())
422 set_reply_data( entry->str, min( len, get_reply_max_size()));
423 reply->count = entry->count;
424 reply->pinned = entry->pinned;
427 else reply->count = -1;
428 release_object( table );
432 /* set global atom name */
433 DECL_HANDLER(set_atom_information)
435 struct atom_table *table = get_table( req->table, 0 );
438 struct atom_entry *entry;
440 if ((entry = get_atom_entry( table, req->atom )))
442 if (req->pinned) entry->pinned = 1;
444 release_object( table );
448 /* init a (local) atom table */
449 DECL_HANDLER(init_atom_table)
451 struct atom_table* table = create_table( req->entries );
455 reply->table = alloc_handle( current->process, table, 0, 0 );
456 release_object( table );
460 /* set global atom name */
461 DECL_HANDLER(empty_atom_table)
463 struct atom_table *table = get_table( req->table, 1 );
467 struct atom_entry *entry;
469 for (i = 0; i <= table->last; i++)
471 entry = table->handles[i];
472 if (entry && (!entry->pinned || req->if_pinned))
474 if (entry->next) entry->next->prev = entry->prev;
475 if (entry->prev) entry->prev->next = entry->next;
476 else table->entries[entry->hash] = entry->next;
477 table->handles[i] = NULL;
481 release_object( table );