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_signal, /* signal */
75 no_get_fd, /* get_fd */
76 atom_table_destroy /* destroy */
79 static struct atom_table *global_table;
82 /* copy an atom name from the request to a temporary area */
83 static const WCHAR *copy_request_name(void)
85 static WCHAR buffer[MAX_ATOM_LEN+1];
87 const WCHAR *str = get_req_data();
88 size_t len = get_req_data_size();
90 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
92 set_error( STATUS_INVALID_PARAMETER );
95 memcpy( buffer, str, len );
96 buffer[len / sizeof(WCHAR)] = 0;
100 /* create an atom table */
101 static struct atom_table *create_table(int entries_count)
103 struct atom_table *table;
105 if ((table = alloc_object( &atom_table_ops )))
107 if ((entries_count < MIN_HASH_SIZE) ||
108 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
109 table->entries_count = entries_count;
110 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
112 set_error( STATUS_NO_MEMORY );
115 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
118 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
121 release_object( table );
127 /* retrieve an entry pointer from its atom */
128 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
130 struct atom_entry *entry = NULL;
131 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
132 entry = table->handles[atom - MIN_STR_ATOM];
133 if (!entry) set_error( STATUS_INVALID_HANDLE );
137 /* add an atom entry in the table and return its handle */
138 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
141 for (i = 0; i <= table->last; i++)
142 if (!table->handles[i]) goto found;
143 if (i == table->count)
145 struct atom_entry **new_table = NULL;
146 int new_size = table->count + table->count / 2;
147 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
148 if (new_size > table->count)
149 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
152 set_error( STATUS_NO_MEMORY );
155 table->count = new_size;
156 table->handles = new_table;
160 table->handles[i] = entry;
161 entry->atom = i + MIN_STR_ATOM;
165 /* compute the hash code for a string */
166 static int atom_hash( struct atom_table *table, const WCHAR *str )
170 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
171 return hash % table->entries_count;
174 /* dump an atom table */
175 static void atom_table_dump( struct object *obj, int verbose )
178 struct atom_table *table = (struct atom_table *)obj;
179 assert( obj->ops == &atom_table_ops );
181 fprintf( stderr, "Atom table size=%d entries=%d\n",
182 table->last + 1, table->entries_count );
183 if (!verbose) return;
184 for (i = 0; i <= table->last; i++)
186 struct atom_entry *entry = table->handles[i];
187 if (!entry) continue;
188 fprintf( stderr, " %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
189 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
190 fprintf( stderr, "\"\n" );
194 /* destroy the atom table */
195 static void atom_table_destroy( struct object *obj )
198 struct atom_table *table = (struct atom_table *)obj;
199 assert( obj->ops == &atom_table_ops );
202 for (i = 0; i <= table->last; i++) free( table->handles[i] );
203 free( table->handles );
205 if (table->entries) free( table->entries );
208 /* find an atom entry in its hash list */
209 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
211 struct atom_entry *entry = table->entries[hash];
214 if (!strcmpiW( entry->str, str )) break;
220 /* close the atom table; used on server exit */
221 void close_atom_table(void)
223 if (global_table) release_object( global_table );
226 /* add an atom to the table */
227 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
229 struct atom_entry *entry;
230 int hash = atom_hash( table, str );
235 set_error( STATUS_OBJECT_NAME_INVALID );
238 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
244 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
246 if ((atom = add_atom_entry( table, entry )))
249 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
250 table->entries[hash] = entry;
253 strcpyW( entry->str, str );
257 else set_error( STATUS_NO_MEMORY );
261 /* delete an atom from the table */
262 static void delete_atom( struct atom_table *table, atom_t atom )
264 struct atom_entry *entry = get_atom_entry( table, atom );
265 if (entry && !--entry->count)
267 if (entry->next) entry->next->prev = entry->prev;
268 if (entry->prev) entry->prev->next = entry->next;
269 else table->entries[entry->hash] = entry->next;
270 table->handles[atom - MIN_STR_ATOM] = NULL;
275 /* find an atom in the table */
276 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
278 struct atom_entry *entry;
280 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
282 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
283 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
287 /* increment the ref count of a global atom; used for window properties */
288 int grab_global_atom( atom_t atom )
290 if (atom >= MIN_STR_ATOM)
292 struct atom_entry *entry = get_atom_entry( global_table, atom );
293 if (entry) entry->count++;
294 return (entry != NULL);
299 /* decrement the ref count of a global atom; used for window properties */
300 void release_global_atom( atom_t atom )
302 if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom );
305 /* add a global atom */
306 DECL_HANDLER(add_atom)
308 struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
310 if (!*table_ptr) *table_ptr = create_table(0);
313 const WCHAR *name = copy_request_name();
314 if (name) reply->atom = add_atom( *table_ptr, name );
318 /* delete a global atom */
319 DECL_HANDLER(delete_atom)
321 delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
324 /* find a global atom */
325 DECL_HANDLER(find_atom)
327 const WCHAR *name = copy_request_name();
329 reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
332 /* get global atom name */
333 DECL_HANDLER(get_atom_name)
335 struct atom_entry *entry;
339 if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
342 reply->count = entry->count;
343 len = strlenW( entry->str ) * sizeof(WCHAR);
344 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
345 else set_error( STATUS_BUFFER_OVERFLOW );
349 /* init the process atom table */
350 DECL_HANDLER(init_atom_table)
352 if (!current->process->atom_table)
353 current->process->atom_table = create_table( req->entries );