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"
39 #define MIN_HASH_SIZE 4
40 #define MAX_HASH_SIZE 0x200
42 #define MAX_ATOM_LEN 255
43 #define MIN_STR_ATOM 0xc000
44 #define MAX_ATOMS 0x4000
48 struct atom_entry *next; /* hash table list */
49 struct atom_entry *prev; /* hash table list */
50 int count; /* reference count */
51 short pinned; /* whether the atom is pinned or not */
52 atom_t atom; /* atom handle */
53 unsigned short hash; /* string hash */
54 unsigned short len; /* string len */
55 WCHAR str[1]; /* atom string */
60 struct object obj; /* object header */
61 int count; /* count of atom handles */
62 int last; /* last handle in-use */
63 struct atom_entry **handles; /* atom handles */
64 int entries_count; /* humber of hash entries */
65 struct atom_entry **entries; /* hash table entries */
68 static void atom_table_dump( struct object *obj, int verbose );
69 static void atom_table_destroy( struct object *obj );
71 static const struct object_ops atom_table_ops =
73 sizeof(struct atom_table), /* size */
74 atom_table_dump, /* dump */
75 no_add_queue, /* add_queue */
76 NULL, /* remove_queue */
79 no_signal, /* signal */
80 no_get_fd, /* get_fd */
81 no_lookup_name, /* lookup_name */
82 no_close_handle, /* close_handle */
83 atom_table_destroy /* destroy */
87 /* create an atom table */
88 static struct atom_table *create_table(int entries_count)
90 struct atom_table *table;
92 if ((table = alloc_object( &atom_table_ops )))
94 if ((entries_count < MIN_HASH_SIZE) ||
95 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
96 table->entries_count = entries_count;
97 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
99 set_error( STATUS_NO_MEMORY );
102 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
105 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
108 release_object( table );
114 /* retrieve an entry pointer from its atom */
115 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
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 );
124 /* add an atom entry in the table and return its handle */
125 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
128 for (i = 0; i <= table->last; i++)
129 if (!table->handles[i]) goto found;
130 if (i == table->count)
132 struct atom_entry **new_table = NULL;
133 int new_size = table->count + table->count / 2;
134 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
135 if (new_size > table->count)
136 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
139 set_error( STATUS_NO_MEMORY );
142 table->count = new_size;
143 table->handles = new_table;
147 table->handles[i] = entry;
148 entry->atom = i + MIN_STR_ATOM;
152 /* compute the hash code for a string */
153 static unsigned short atom_hash( struct atom_table *table, const WCHAR *str, size_t len )
156 unsigned short hash = 0;
157 for (i = 0; i < len; i++) hash ^= toupperW(str[i]) + i;
158 return hash % table->entries_count;
161 /* dump an atom table */
162 static void atom_table_dump( struct object *obj, int verbose )
165 struct atom_table *table = (struct atom_table *)obj;
166 assert( obj->ops == &atom_table_ops );
168 fprintf( stderr, "Atom table size=%d entries=%d\n",
169 table->last + 1, table->entries_count );
170 if (!verbose) return;
171 for (i = 0; i <= table->last; i++)
173 struct atom_entry *entry = table->handles[i];
174 if (!entry) continue;
175 fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
176 entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
177 dump_strW( entry->str, entry->len, stderr, "\"\"");
178 fprintf( stderr, "\"\n" );
182 /* destroy the atom table */
183 static void atom_table_destroy( struct object *obj )
186 struct atom_table *table = (struct atom_table *)obj;
187 assert( obj->ops == &atom_table_ops );
190 for (i = 0; i <= table->last; i++) free( table->handles[i] );
191 free( table->handles );
193 if (table->entries) free( table->entries );
196 /* find an atom entry in its hash list */
197 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
198 size_t len, unsigned short hash )
200 struct atom_entry *entry = table->entries[hash];
203 if (entry->len == len && !memicmpW( entry->str, str, len )) break;
209 /* add an atom to the table */
210 static atom_t add_atom( struct atom_table *table, const WCHAR *str, size_t len )
212 struct atom_entry *entry;
213 unsigned short hash = atom_hash( table, str, len );
218 set_error( STATUS_OBJECT_NAME_INVALID );
221 if (len > MAX_ATOM_LEN)
223 set_error( STATUS_INVALID_PARAMETER );
226 if ((entry = find_atom_entry( table, str, len, hash ))) /* exists already */
232 if ((entry = mem_alloc( sizeof(*entry) + (len - 1) * sizeof(WCHAR) )))
234 if ((atom = add_atom_entry( table, entry )))
237 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
238 table->entries[hash] = entry;
243 memcpy( entry->str, str, len * sizeof(WCHAR) );
247 else set_error( STATUS_NO_MEMORY );
251 /* delete an atom from the table */
252 static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
254 struct atom_entry *entry = get_atom_entry( table, atom );
256 if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
257 else if (!--entry->count)
259 if (entry->next) entry->next->prev = entry->prev;
260 if (entry->prev) entry->prev->next = entry->next;
261 else table->entries[entry->hash] = entry->next;
262 table->handles[atom - MIN_STR_ATOM] = NULL;
267 /* find an atom in the table */
268 static atom_t find_atom( struct atom_table *table, const WCHAR *str, size_t len )
270 struct atom_entry *entry;
274 set_error( STATUS_OBJECT_NAME_INVALID );
277 if (len > MAX_ATOM_LEN)
279 set_error( STATUS_INVALID_PARAMETER );
282 if (table && (entry = find_atom_entry( table, str, len, atom_hash(table, str, len) )))
284 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
288 static struct atom_table *get_global_table( struct winstation *winstation, int create )
290 if (!winstation->atom_table)
292 if (create) winstation->atom_table = create_table( HASH_SIZE );
293 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
295 return winstation->atom_table;
298 static struct atom_table *get_table( obj_handle_t h, int create )
300 struct atom_table *table = NULL;
304 table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
308 struct winstation *winstation = get_process_winstation( current->process,
309 WINSTA_ACCESSGLOBALATOMS );
312 table = get_global_table( winstation, 1 );
313 if (table) grab_object( table );
314 release_object( winstation );
320 /* add an atom in the global table; used for window properties */
321 atom_t add_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
323 struct atom_table *global_table = get_global_table( winstation, 1 );
324 if (!global_table) return 0;
325 return add_atom( global_table, str, len );
328 /* find an atom in the global table; used for window properties */
329 atom_t find_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
331 struct atom_table *global_table = get_global_table( winstation, 0 );
332 struct atom_entry *entry;
334 if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
335 if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
340 /* increment the ref count of a global atom; used for window properties */
341 int grab_global_atom( struct winstation *winstation, atom_t atom )
343 if (atom >= MIN_STR_ATOM)
345 struct atom_table *global_table = get_global_table( winstation, 0 );
348 struct atom_entry *entry = get_atom_entry( global_table, atom );
349 if (entry) entry->count++;
350 return (entry != NULL);
357 /* decrement the ref count of a global atom; used for window properties */
358 void release_global_atom( struct winstation *winstation, atom_t atom )
360 if (atom >= MIN_STR_ATOM)
362 struct atom_table *global_table = get_global_table( winstation, 0 );
363 if (global_table) delete_atom( global_table, atom, 1 );
367 /* add a global atom */
368 DECL_HANDLER(add_atom)
370 struct atom_table *table = get_table( req->table, 1 );
373 reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
374 release_object( table );
378 /* delete a global atom */
379 DECL_HANDLER(delete_atom)
381 struct atom_table *table = get_table( req->table, 0 );
384 delete_atom( table, req->atom, 0 );
385 release_object( table );
389 /* find a global atom */
390 DECL_HANDLER(find_atom)
392 struct atom_table *table = get_table( req->table, 0 );
395 reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
396 release_object( table );
400 /* get global atom name */
401 DECL_HANDLER(get_atom_information)
403 struct atom_table *table = get_table( req->table, 0 );
406 struct atom_entry *entry;
408 if ((entry = get_atom_entry( table, req->atom )))
410 size_t len = entry->len * sizeof(WCHAR);
411 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
412 else if (get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
413 reply->count = entry->count;
414 reply->pinned = entry->pinned;
416 else reply->count = -1;
417 release_object( table );
421 /* set global atom name */
422 DECL_HANDLER(set_atom_information)
424 struct atom_table *table = get_table( req->table, 0 );
427 struct atom_entry *entry;
429 if ((entry = get_atom_entry( table, req->atom )))
431 if (req->pinned) entry->pinned = 1;
433 release_object( table );
437 /* init a (local) atom table */
438 DECL_HANDLER(init_atom_table)
440 struct atom_table* table = create_table( req->entries );
444 reply->table = alloc_handle( current->process, table, 0, FALSE);
445 release_object( table );
449 /* set global atom name */
450 DECL_HANDLER(empty_atom_table)
452 struct atom_table *table = get_table( req->table, 1 );
456 struct atom_entry *entry;
458 for (i = 0; i <= table->last; i++)
460 entry = table->handles[i];
461 if (entry && (!entry->pinned || req->if_pinned))
463 if (entry->next) entry->next->prev = entry->prev;
464 if (entry->prev) entry->prev->next = entry->next;
465 else table->entries[entry->hash] = entry->next;
466 table->handles[i] = NULL;
470 release_object( table );