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"
36 #define MIN_HASH_SIZE 4
37 #define MAX_HASH_SIZE 0x200
39 #define MAX_ATOM_LEN 255
40 #define MIN_STR_ATOM 0xc000
41 #define MAX_ATOMS 0x4000
45 struct atom_entry *next; /* hash table list */
46 struct atom_entry *prev; /* hash table list */
47 int count; /* reference count */
48 int hash; /* string hash */
49 atom_t atom; /* atom handle */
50 WCHAR str[1]; /* atom string */
55 struct object obj; /* object header */
56 int count; /* count of atom handles */
57 int last; /* last handle in-use */
58 struct atom_entry **handles; /* atom handles */
59 int entries_count; /* humber of hash entries */
60 struct atom_entry **entries; /* hash table entries */
63 static void atom_table_dump( struct object *obj, int verbose );
64 static void atom_table_destroy( struct object *obj );
66 static const struct object_ops atom_table_ops =
68 sizeof(struct atom_table), /* size */
69 atom_table_dump, /* dump */
70 no_add_queue, /* add_queue */
71 NULL, /* remove_queue */
74 no_get_fd, /* get_fd */
75 atom_table_destroy /* destroy */
78 static struct atom_table *global_table;
81 /* copy an atom name from the request to a temporary area */
82 static const WCHAR *copy_request_name(void)
84 static WCHAR buffer[MAX_ATOM_LEN+1];
86 const WCHAR *str = get_req_data();
87 size_t len = get_req_data_size();
89 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
91 set_error( STATUS_INVALID_PARAMETER );
94 memcpy( buffer, str, len );
95 buffer[len / sizeof(WCHAR)] = 0;
99 /* create an atom table */
100 static struct atom_table *create_table(int entries_count)
102 struct atom_table *table;
104 if ((table = alloc_object( &atom_table_ops )))
106 if ((entries_count < MIN_HASH_SIZE) ||
107 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
108 table->entries_count = entries_count;
109 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
111 set_error( STATUS_NO_MEMORY );
114 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
117 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
120 release_object( table );
126 /* retrieve an entry pointer from its atom */
127 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
129 struct atom_entry *entry = NULL;
130 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
131 entry = table->handles[atom - MIN_STR_ATOM];
132 if (!entry) set_error( STATUS_INVALID_HANDLE );
136 /* add an atom entry in the table and return its handle */
137 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
140 for (i = 0; i <= table->last; i++)
141 if (!table->handles[i]) goto found;
142 if (i == table->count)
144 struct atom_entry **new_table = NULL;
145 int new_size = table->count + table->count / 2;
146 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
147 if (new_size > table->count)
148 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
151 set_error( STATUS_NO_MEMORY );
154 table->count = new_size;
155 table->handles = new_table;
159 table->handles[i] = entry;
160 entry->atom = i + MIN_STR_ATOM;
164 /* compute the hash code for a string */
165 static int atom_hash( struct atom_table *table, const WCHAR *str )
169 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
170 return hash % table->entries_count;
173 /* dump an atom table */
174 static void atom_table_dump( struct object *obj, int verbose )
177 struct atom_table *table = (struct atom_table *)obj;
178 assert( obj->ops == &atom_table_ops );
180 fprintf( stderr, "Atom table size=%d entries=%d\n",
181 table->last + 1, table->entries_count );
182 if (!verbose) return;
183 for (i = 0; i <= table->last; i++)
185 struct atom_entry *entry = table->handles[i];
186 if (!entry) continue;
187 fprintf( stderr, " %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
188 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
189 fprintf( stderr, "\"\n" );
193 /* destroy the atom table */
194 static void atom_table_destroy( struct object *obj )
197 struct atom_table *table = (struct atom_table *)obj;
198 assert( obj->ops == &atom_table_ops );
201 for (i = 0; i <= table->last; i++) free( table->handles[i] );
202 free( table->handles );
204 if (table->entries) free( table->entries );
207 /* find an atom entry in its hash list */
208 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
210 struct atom_entry *entry = table->entries[hash];
213 if (!strcmpiW( entry->str, str )) break;
219 /* close the atom table; used on server exit */
220 void close_atom_table(void)
222 if (global_table) release_object( global_table );
225 /* add an atom to the table */
226 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
228 struct atom_entry *entry;
229 int hash = atom_hash( table, str );
234 set_error( STATUS_OBJECT_NAME_INVALID );
237 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
243 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
245 if ((atom = add_atom_entry( table, entry )))
248 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
249 table->entries[hash] = entry;
252 strcpyW( entry->str, str );
256 else set_error( STATUS_NO_MEMORY );
260 /* delete an atom from the table */
261 static void delete_atom( struct atom_table *table, atom_t atom )
263 struct atom_entry *entry = get_atom_entry( table, atom );
264 if (entry && !--entry->count)
266 if (entry->next) entry->next->prev = entry->prev;
267 if (entry->prev) entry->prev->next = entry->next;
268 else table->entries[entry->hash] = entry->next;
269 table->handles[atom - MIN_STR_ATOM] = NULL;
274 /* find an atom in the table */
275 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
277 struct atom_entry *entry;
279 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
281 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
282 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
286 /* increment the ref count of a global atom; used for window properties */
287 int grab_global_atom( atom_t atom )
289 if (atom >= MIN_STR_ATOM)
291 struct atom_entry *entry = get_atom_entry( global_table, atom );
292 if (entry) entry->count++;
293 return (entry != NULL);
298 /* decrement the ref count of a global atom; used for window properties */
299 void release_global_atom( atom_t atom )
301 if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom );
304 /* add a global atom */
305 DECL_HANDLER(add_atom)
307 struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
309 if (!*table_ptr) *table_ptr = create_table(0);
312 const WCHAR *name = copy_request_name();
313 if (name) reply->atom = add_atom( *table_ptr, name );
317 /* delete a global atom */
318 DECL_HANDLER(delete_atom)
320 delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
323 /* find a global atom */
324 DECL_HANDLER(find_atom)
326 const WCHAR *name = copy_request_name();
328 reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
331 /* get global atom name */
332 DECL_HANDLER(get_atom_name)
334 struct atom_entry *entry;
338 if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
341 reply->count = entry->count;
342 len = strlenW( entry->str ) * sizeof(WCHAR);
343 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
344 else set_error( STATUS_BUFFER_OVERFLOW );
348 /* init the process atom table */
349 DECL_HANDLER(init_atom_table)
351 if (!current->process->atom_table)
352 current->process->atom_table = create_table( req->entries );