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_close_handle, /* close_handle */
82 atom_table_destroy /* destroy */
86 /* create an atom table */
87 static struct atom_table *create_table(int entries_count)
89 struct atom_table *table;
91 if ((table = alloc_object( &atom_table_ops )))
93 if ((entries_count < MIN_HASH_SIZE) ||
94 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
95 table->entries_count = entries_count;
96 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
98 set_error( STATUS_NO_MEMORY );
101 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
104 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
107 release_object( table );
113 /* retrieve an entry pointer from its atom */
114 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
116 struct atom_entry *entry = NULL;
117 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
118 entry = table->handles[atom - MIN_STR_ATOM];
119 if (!entry) set_error( STATUS_INVALID_HANDLE );
123 /* add an atom entry in the table and return its handle */
124 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
127 for (i = 0; i <= table->last; i++)
128 if (!table->handles[i]) goto found;
129 if (i == table->count)
131 struct atom_entry **new_table = NULL;
132 int new_size = table->count + table->count / 2;
133 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
134 if (new_size > table->count)
135 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
138 set_error( STATUS_NO_MEMORY );
141 table->count = new_size;
142 table->handles = new_table;
146 table->handles[i] = entry;
147 entry->atom = i + MIN_STR_ATOM;
151 /* compute the hash code for a string */
152 static unsigned short atom_hash( struct atom_table *table, const WCHAR *str, size_t len )
155 unsigned short hash = 0;
156 for (i = 0; i < len; i++) hash ^= toupperW(str[i]) + i;
157 return hash % table->entries_count;
160 /* dump an atom table */
161 static void atom_table_dump( struct object *obj, int verbose )
164 struct atom_table *table = (struct atom_table *)obj;
165 assert( obj->ops == &atom_table_ops );
167 fprintf( stderr, "Atom table size=%d entries=%d\n",
168 table->last + 1, table->entries_count );
169 if (!verbose) return;
170 for (i = 0; i <= table->last; i++)
172 struct atom_entry *entry = table->handles[i];
173 if (!entry) continue;
174 fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
175 entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
176 dump_strW( entry->str, entry->len, stderr, "\"\"");
177 fprintf( stderr, "\"\n" );
181 /* destroy the atom table */
182 static void atom_table_destroy( struct object *obj )
185 struct atom_table *table = (struct atom_table *)obj;
186 assert( obj->ops == &atom_table_ops );
189 for (i = 0; i <= table->last; i++) free( table->handles[i] );
190 free( table->handles );
192 if (table->entries) free( table->entries );
195 /* find an atom entry in its hash list */
196 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
197 size_t len, unsigned short hash )
199 struct atom_entry *entry = table->entries[hash];
202 if (entry->len == len && !memicmpW( entry->str, str, len )) break;
208 /* add an atom to the table */
209 static atom_t add_atom( struct atom_table *table, const WCHAR *str, size_t len )
211 struct atom_entry *entry;
212 unsigned short hash = atom_hash( table, str, len );
217 set_error( STATUS_OBJECT_NAME_INVALID );
220 if (len > MAX_ATOM_LEN)
222 set_error( STATUS_INVALID_PARAMETER );
225 if ((entry = find_atom_entry( table, str, len, hash ))) /* exists already */
231 if ((entry = mem_alloc( sizeof(*entry) + (len - 1) * sizeof(WCHAR) )))
233 if ((atom = add_atom_entry( table, entry )))
236 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
237 table->entries[hash] = entry;
242 memcpy( entry->str, str, len * sizeof(WCHAR) );
246 else set_error( STATUS_NO_MEMORY );
250 /* delete an atom from the table */
251 static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
253 struct atom_entry *entry = get_atom_entry( table, atom );
255 if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
256 else if (!--entry->count)
258 if (entry->next) entry->next->prev = entry->prev;
259 if (entry->prev) entry->prev->next = entry->next;
260 else table->entries[entry->hash] = entry->next;
261 table->handles[atom - MIN_STR_ATOM] = NULL;
266 /* find an atom in the table */
267 static atom_t find_atom( struct atom_table *table, const WCHAR *str, size_t len )
269 struct atom_entry *entry;
273 set_error( STATUS_OBJECT_NAME_INVALID );
276 if (len > MAX_ATOM_LEN)
278 set_error( STATUS_INVALID_PARAMETER );
281 if (table && (entry = find_atom_entry( table, str, len, atom_hash(table, str, len) )))
283 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
287 static struct atom_table *get_global_table( struct winstation *winstation, int create )
289 if (!winstation->atom_table)
291 if (create) winstation->atom_table = create_table( HASH_SIZE );
292 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
294 return winstation->atom_table;
297 static struct atom_table *get_table( obj_handle_t h, int create )
299 struct atom_table *table = NULL;
303 table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
307 struct winstation *winstation = get_process_winstation( current->process,
308 WINSTA_ACCESSGLOBALATOMS );
311 table = get_global_table( winstation, 1 );
312 if (table) grab_object( table );
313 release_object( winstation );
319 /* add an atom in the global table; used for window properties */
320 atom_t add_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
322 struct atom_table *global_table = get_global_table( winstation, 1 );
323 if (!global_table) return 0;
324 return add_atom( global_table, str, len );
327 /* find an atom in the global table; used for window properties */
328 atom_t find_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
330 struct atom_table *global_table = get_global_table( winstation, 0 );
331 struct atom_entry *entry;
333 if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
334 if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
339 /* increment the ref count of a global atom; used for window properties */
340 int grab_global_atom( struct winstation *winstation, atom_t atom )
342 if (atom >= MIN_STR_ATOM)
344 struct atom_table *global_table = get_global_table( winstation, 0 );
347 struct atom_entry *entry = get_atom_entry( global_table, atom );
348 if (entry) entry->count++;
349 return (entry != NULL);
356 /* decrement the ref count of a global atom; used for window properties */
357 void release_global_atom( struct winstation *winstation, atom_t atom )
359 if (atom >= MIN_STR_ATOM)
361 struct atom_table *global_table = get_global_table( winstation, 0 );
362 if (global_table) delete_atom( global_table, atom, 1 );
366 /* add a global atom */
367 DECL_HANDLER(add_atom)
369 struct atom_table *table = get_table( req->table, 1 );
372 reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
373 release_object( table );
377 /* delete a global atom */
378 DECL_HANDLER(delete_atom)
380 struct atom_table *table = get_table( req->table, 0 );
383 delete_atom( table, req->atom, 0 );
384 release_object( table );
388 /* find a global atom */
389 DECL_HANDLER(find_atom)
391 struct atom_table *table = get_table( req->table, 0 );
394 reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
395 release_object( table );
399 /* get global atom name */
400 DECL_HANDLER(get_atom_information)
402 struct atom_table *table = get_table( req->table, 0 );
405 struct atom_entry *entry;
407 if ((entry = get_atom_entry( table, req->atom )))
409 size_t len = entry->len * sizeof(WCHAR);
410 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
411 else if (get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
412 reply->count = entry->count;
413 reply->pinned = entry->pinned;
415 else reply->count = -1;
416 release_object( table );
420 /* set global atom name */
421 DECL_HANDLER(set_atom_information)
423 struct atom_table *table = get_table( req->table, 0 );
426 struct atom_entry *entry;
428 if ((entry = get_atom_entry( table, req->atom )))
430 if (req->pinned) entry->pinned = 1;
432 release_object( table );
436 /* init a (local) atom table */
437 DECL_HANDLER(init_atom_table)
439 struct atom_table* table = create_table( req->entries );
443 reply->table = alloc_handle( current->process, table, 0, FALSE);
444 release_object( table );
448 /* set global atom name */
449 DECL_HANDLER(empty_atom_table)
451 struct atom_table *table = get_table( req->table, 1 );
455 struct atom_entry *entry;
457 for (i = 0; i <= table->last; i++)
459 entry = table->handles[i];
460 if (entry && (!entry->pinned || req->if_pinned))
462 if (entry->next) entry->next->prev = entry->prev;
463 if (entry->prev) entry->prev->next = entry->next;
464 else table->entries[entry->hash] = entry->next;
465 table->handles[i] = NULL;
469 release_object( table );