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
33 #define MIN_HASH_SIZE 4
34 #define MAX_HASH_SIZE 0x200
36 #define MAX_ATOM_LEN 255
37 #define MIN_STR_ATOM 0xc000
38 #define MAX_ATOMS 0x4000
42 struct atom_entry *next; /* hash table list */
43 struct atom_entry *prev; /* hash table list */
44 int count; /* reference count */
45 int hash; /* string hash */
46 atom_t atom; /* atom handle */
47 WCHAR str[1]; /* atom string */
52 struct object obj; /* object header */
53 int count; /* count of atom handles */
54 int last; /* last handle in-use */
55 struct atom_entry **handles; /* atom handles */
56 int entries_count; /* humber of hash entries */
57 struct atom_entry **entries; /* hash table entries */
60 static void atom_table_dump( struct object *obj, int verbose );
61 static void atom_table_destroy( struct object *obj );
63 static const struct object_ops atom_table_ops =
65 sizeof(struct atom_table), /* size */
66 atom_table_dump, /* dump */
67 no_add_queue, /* add_queue */
68 NULL, /* remove_queue */
71 NULL, /* get_poll_events */
72 NULL, /* poll_event */
73 no_get_fd, /* get_fd */
75 no_get_file_info, /* get_file_info */
76 NULL, /* queue_async */
77 atom_table_destroy /* destroy */
80 static struct atom_table *global_table;
83 /* copy an atom name from the request to a temporary area */
84 static const WCHAR *copy_request_name(void)
86 static WCHAR buffer[MAX_ATOM_LEN+1];
88 const WCHAR *str = get_req_data();
89 size_t len = get_req_data_size();
91 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
93 set_error( STATUS_INVALID_PARAMETER );
96 memcpy( buffer, str, len );
97 buffer[len / sizeof(WCHAR)] = 0;
101 /* create an atom table */
102 static struct atom_table *create_table(int entries_count)
104 struct atom_table *table;
106 if ((table = alloc_object( &atom_table_ops, -1 )))
108 if ((entries_count < MIN_HASH_SIZE) ||
109 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
110 table->entries_count = entries_count;
111 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
113 set_error( STATUS_NO_MEMORY );
116 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
119 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
122 release_object( table );
128 /* retrieve an entry pointer from its atom */
129 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
131 struct atom_entry *entry = NULL;
132 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
133 entry = table->handles[atom - MIN_STR_ATOM];
134 if (!entry) set_error( STATUS_INVALID_HANDLE );
138 /* add an atom entry in the table and return its handle */
139 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
142 for (i = 0; i <= table->last; i++)
143 if (!table->handles[i]) goto found;
144 if (i == table->count)
146 struct atom_entry **new_table = NULL;
147 int new_size = table->count + table->count / 2;
148 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
149 if (new_size > table->count)
150 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
153 set_error( STATUS_NO_MEMORY );
156 table->count = new_size;
157 table->handles = new_table;
161 table->handles[i] = entry;
162 entry->atom = i + MIN_STR_ATOM;
166 /* compute the hash code for a string */
167 static int atom_hash( struct atom_table *table, const WCHAR *str )
171 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
172 return hash % table->entries_count;
175 /* dump an atom table */
176 static void atom_table_dump( struct object *obj, int verbose )
179 struct atom_table *table = (struct atom_table *)obj;
180 assert( obj->ops == &atom_table_ops );
182 fprintf( stderr, "Atom table size=%d entries=%d\n",
183 table->last + 1, table->entries_count );
184 if (!verbose) return;
185 for (i = 0; i <= table->last; i++)
187 struct atom_entry *entry = table->handles[i];
188 if (!entry) continue;
189 fprintf( stderr, " %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
190 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
191 fprintf( stderr, "\"\n" );
195 /* destroy the atom table */
196 static void atom_table_destroy( struct object *obj )
199 struct atom_table *table = (struct atom_table *)obj;
200 assert( obj->ops == &atom_table_ops );
203 for (i = 0; i <= table->last; i++) free( table->handles[i] );
204 free( table->handles );
206 if (table->entries) free( table->entries );
209 /* find an atom entry in its hash list */
210 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
212 struct atom_entry *entry = table->entries[hash];
215 if (!strcmpiW( entry->str, str )) break;
221 /* close the atom table; used on server exit */
222 void close_atom_table(void)
224 if (global_table) release_object( global_table );
227 /* add an atom to the table */
228 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
230 struct atom_entry *entry;
231 int hash = atom_hash( table, str );
236 set_error( STATUS_OBJECT_NAME_INVALID );
239 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
245 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
247 if ((atom = add_atom_entry( table, entry )))
250 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
251 table->entries[hash] = entry;
254 strcpyW( entry->str, str );
258 else set_error( STATUS_NO_MEMORY );
262 /* delete an atom from the table */
263 static void delete_atom( struct atom_table *table, atom_t atom )
265 struct atom_entry *entry = get_atom_entry( table, atom );
266 if (entry && !--entry->count)
268 if (entry->next) entry->next->prev = entry->prev;
269 if (entry->prev) entry->prev->next = entry->next;
270 else table->entries[entry->hash] = entry->next;
271 table->handles[atom - MIN_STR_ATOM] = NULL;
276 /* find an atom in the table */
277 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
279 struct atom_entry *entry;
281 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
283 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
284 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
288 /* increment the ref count of a global atom; used for window properties */
289 int grab_global_atom( atom_t atom )
291 if (atom >= MIN_STR_ATOM)
293 struct atom_entry *entry = get_atom_entry( global_table, atom );
294 if (entry) entry->count++;
295 return (entry != NULL);
300 /* decrement the ref count of a global atom; used for window properties */
301 void release_global_atom( atom_t atom )
303 if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom );
306 /* add a global atom */
307 DECL_HANDLER(add_atom)
309 struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
311 if (!*table_ptr) *table_ptr = create_table(0);
314 const WCHAR *name = copy_request_name();
315 if (name) reply->atom = add_atom( *table_ptr, name );
319 /* delete a global atom */
320 DECL_HANDLER(delete_atom)
322 delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
325 /* find a global atom */
326 DECL_HANDLER(find_atom)
328 const WCHAR *name = copy_request_name();
330 reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
333 /* get global atom name */
334 DECL_HANDLER(get_atom_name)
336 struct atom_entry *entry;
340 if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
343 reply->count = entry->count;
344 len = strlenW( entry->str ) * sizeof(WCHAR);
345 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
346 else set_error( STATUS_BUFFER_OVERFLOW );
350 /* init the process atom table */
351 DECL_HANDLER(init_atom_table)
353 if (!current->process->atom_table)
354 current->process->atom_table = create_table( req->entries );