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"
37 #define MIN_HASH_SIZE 4
38 #define MAX_HASH_SIZE 0x200
40 #define MAX_ATOM_LEN 255
41 #define MIN_STR_ATOM 0xc000
42 #define MAX_ATOMS 0x4000
46 struct atom_entry *next; /* hash table list */
47 struct atom_entry *prev; /* hash table list */
48 int count; /* reference count */
49 int pinned; /* whether the atom is pinned or not */
50 int hash; /* string hash */
51 atom_t atom; /* atom handle */
52 WCHAR str[1]; /* atom string */
57 struct object obj; /* object header */
58 int count; /* count of atom handles */
59 int last; /* last handle in-use */
60 struct atom_entry **handles; /* atom handles */
61 int entries_count; /* humber of hash entries */
62 struct atom_entry **entries; /* hash table entries */
65 static void atom_table_dump( struct object *obj, int verbose );
66 static void atom_table_destroy( struct object *obj );
68 static const struct object_ops atom_table_ops =
70 sizeof(struct atom_table), /* size */
71 atom_table_dump, /* dump */
72 no_add_queue, /* add_queue */
73 NULL, /* remove_queue */
76 no_signal, /* signal */
77 no_get_fd, /* get_fd */
78 atom_table_destroy /* destroy */
81 static struct atom_table *global_table;
84 /* copy an atom name from the request to a temporary area */
85 static const WCHAR *copy_request_name(void)
87 static WCHAR buffer[MAX_ATOM_LEN+1];
89 const WCHAR *str = get_req_data();
90 size_t len = get_req_data_size();
92 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
94 set_error( STATUS_INVALID_PARAMETER );
97 memcpy( buffer, str, len );
98 buffer[len / sizeof(WCHAR)] = 0;
102 /* create an atom table */
103 static struct atom_table *create_table(int entries_count)
105 struct atom_table *table;
107 if ((table = alloc_object( &atom_table_ops )))
109 if ((entries_count < MIN_HASH_SIZE) ||
110 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
111 table->entries_count = entries_count;
112 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
114 set_error( STATUS_NO_MEMORY );
117 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
120 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
123 release_object( table );
129 /* retrieve an entry pointer from its atom */
130 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
132 struct atom_entry *entry = NULL;
133 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
134 entry = table->handles[atom - MIN_STR_ATOM];
135 if (!entry) set_error( STATUS_INVALID_HANDLE );
139 /* add an atom entry in the table and return its handle */
140 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
143 for (i = 0; i <= table->last; i++)
144 if (!table->handles[i]) goto found;
145 if (i == table->count)
147 struct atom_entry **new_table = NULL;
148 int new_size = table->count + table->count / 2;
149 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
150 if (new_size > table->count)
151 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
154 set_error( STATUS_NO_MEMORY );
157 table->count = new_size;
158 table->handles = new_table;
162 table->handles[i] = entry;
163 entry->atom = i + MIN_STR_ATOM;
167 /* compute the hash code for a string */
168 static int atom_hash( struct atom_table *table, const WCHAR *str )
172 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
173 return hash % table->entries_count;
176 /* dump an atom table */
177 static void atom_table_dump( struct object *obj, int verbose )
180 struct atom_table *table = (struct atom_table *)obj;
181 assert( obj->ops == &atom_table_ops );
183 fprintf( stderr, "Atom table size=%d entries=%d\n",
184 table->last + 1, table->entries_count );
185 if (!verbose) return;
186 for (i = 0; i <= table->last; i++)
188 struct atom_entry *entry = table->handles[i];
189 if (!entry) continue;
190 fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
191 entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
192 dump_strW( entry->str, strlenW( entry->str ), stderr, "\"\"");
193 fprintf( stderr, "\"\n" );
197 /* destroy the atom table */
198 static void atom_table_destroy( struct object *obj )
201 struct atom_table *table = (struct atom_table *)obj;
202 assert( obj->ops == &atom_table_ops );
205 for (i = 0; i <= table->last; i++) free( table->handles[i] );
206 free( table->handles );
208 if (table->entries) free( table->entries );
211 /* find an atom entry in its hash list */
212 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
214 struct atom_entry *entry = table->entries[hash];
217 if (!strcmpiW( entry->str, str )) break;
223 /* close the atom table; used on server exit */
224 void close_atom_table(void)
226 if (global_table) release_object( global_table );
229 /* add an atom to the table */
230 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
232 struct atom_entry *entry;
233 int hash = atom_hash( table, str );
238 set_error( STATUS_OBJECT_NAME_INVALID );
241 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
247 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
249 if ((atom = add_atom_entry( table, entry )))
252 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
253 table->entries[hash] = entry;
257 strcpyW( entry->str, str );
261 else set_error( STATUS_NO_MEMORY );
265 /* delete an atom from the table */
266 static void delete_atom( struct atom_table *table, atom_t atom )
268 struct atom_entry *entry = get_atom_entry( table, atom );
269 if (entry && !--entry->count)
271 if (entry->next) entry->next->prev = entry->prev;
272 if (entry->prev) entry->prev->next = entry->next;
273 else table->entries[entry->hash] = entry->next;
274 table->handles[atom - MIN_STR_ATOM] = NULL;
279 /* find an atom in the table */
280 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
282 struct atom_entry *entry;
284 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
286 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
287 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
291 /* increment the ref count of a global atom; used for window properties */
292 int grab_global_atom( atom_t atom )
294 if (atom >= MIN_STR_ATOM)
296 struct atom_entry *entry = get_atom_entry( global_table, atom );
297 if (entry) entry->count++;
298 return (entry != NULL);
303 /* decrement the ref count of a global atom; used for window properties */
304 void release_global_atom( atom_t atom )
306 if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom );
309 static struct atom_table* get_table( obj_handle_t h )
311 struct atom_table *table;
315 table = (struct atom_table*)get_handle_obj( current->process, h,
316 0, &atom_table_ops );
320 if (!global_table && !(global_table = create_table( HASH_SIZE )))
322 table = (struct atom_table*)grab_object( global_table );
327 /* add a global atom */
328 DECL_HANDLER(add_atom)
330 struct atom_table *table = get_table( req->table );
333 const WCHAR *name = copy_request_name();
334 if (name) reply->atom = add_atom( table, name );
335 release_object( table );
339 /* delete a global atom */
340 DECL_HANDLER(delete_atom)
342 struct atom_table *table = get_table( req->table );
345 delete_atom( table, req->atom );
346 release_object( table );
350 /* find a global atom */
351 DECL_HANDLER(find_atom)
353 struct atom_table *table = get_table( req->table );
356 const WCHAR *name = copy_request_name();
358 reply->atom = find_atom( table, name );
359 release_object( table );
363 /* get global atom name */
364 DECL_HANDLER(get_atom_information)
366 struct atom_table *table = get_table( req->table );
369 struct atom_entry *entry;
371 if ((entry = get_atom_entry( table, req->atom )))
373 size_t len = strlenW( entry->str ) * sizeof(WCHAR);
374 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
375 else set_error( STATUS_BUFFER_OVERFLOW );
376 reply->count = entry->count;
377 reply->pinned = entry->pinned;
379 else reply->count = -1;
380 release_object( table );
384 /* set global atom name */
385 DECL_HANDLER(set_atom_information)
387 struct atom_table *table = get_table( req->table );
390 struct atom_entry *entry;
392 if ((entry = get_atom_entry( table, req->atom )))
394 if (req->pinned) entry->pinned = 1;
396 release_object( table );
400 /* init a (local) atom table */
401 DECL_HANDLER(init_atom_table)
403 struct atom_table* table;
405 table = create_table( req->entries );
406 reply->table = alloc_handle( current->process, table, 0, FALSE);
407 release_object( table );
410 /* set global atom name */
411 DECL_HANDLER(empty_atom_table)
413 struct atom_table *table = get_table( req->table );
417 struct atom_entry *entry;
419 for (i = 0; i <= table->last; i++)
421 entry = table->handles[i];
422 if (entry && (!entry->pinned || req->if_pinned))
424 if (entry->next) entry->next->prev = entry->prev;
425 if (entry->prev) entry->prev->next = entry->next;
426 else table->entries[entry->hash] = entry->next;
427 table->handles[i] = NULL;
431 release_object( table );