blob: e14c05eff2bdfa4dd610dc3f88b543f1aded5112 [file] [log] [blame]
Alexandre Julliardb849d792000-02-13 13:56:13 +00001/*
2 * Server-side atom management
3 *
4 * Copyright (C) 1999, 2000 Alexandre Julliard
5 * Copyright (C) 2000 Turchanov Sergei
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00006 *
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.
11 *
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.
16 *
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
Alexandre Julliardb849d792000-02-13 13:56:13 +000020 */
21
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000022#include "config.h"
23#include "wine/port.h"
24
Alexandre Julliardb849d792000-02-13 13:56:13 +000025#include <assert.h>
26#include <stdlib.h>
27#include <stdio.h>
Jeff Garzikf46eb1d2001-03-21 20:30:46 +000028#include <string.h>
Alexandre Julliardb849d792000-02-13 13:56:13 +000029
30#include "unicode.h"
31#include "request.h"
32#include "object.h"
Turchanov Sergei43a27e32000-05-30 20:32:06 +000033#include "process.h"
Alexandre Julliardb849d792000-02-13 13:56:13 +000034
35#define HASH_SIZE 37
Turchanov Sergei43a27e32000-05-30 20:32:06 +000036#define MIN_HASH_SIZE 4
37#define MAX_HASH_SIZE 0x200
38
Alexandre Julliardb849d792000-02-13 13:56:13 +000039#define MAX_ATOM_LEN 255
Alexandre Julliardd8a8c112001-10-12 18:45:29 +000040#define MIN_STR_ATOM 0xc000
Alexandre Julliardb849d792000-02-13 13:56:13 +000041#define MAX_ATOMS 0x4000
42
43struct atom_entry
44{
45 struct atom_entry *next; /* hash table list */
46 struct atom_entry *prev; /* hash table list */
Alexandre Julliardb849d792000-02-13 13:56:13 +000047 int count; /* reference count */
48 int hash; /* string hash */
Alexandre Julliardd8a8c112001-10-12 18:45:29 +000049 atom_t atom; /* atom handle */
Alexandre Julliardb849d792000-02-13 13:56:13 +000050 WCHAR str[1]; /* atom string */
51};
52
53struct atom_table
54{
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 */
Turchanov Sergei43a27e32000-05-30 20:32:06 +000059 int entries_count; /* humber of hash entries */
60 struct atom_entry **entries; /* hash table entries */
Alexandre Julliardb849d792000-02-13 13:56:13 +000061};
62
Alexandre Julliardb849d792000-02-13 13:56:13 +000063static void atom_table_dump( struct object *obj, int verbose );
64static void atom_table_destroy( struct object *obj );
65
66static const struct object_ops atom_table_ops =
67{
68 sizeof(struct atom_table), /* size */
69 atom_table_dump, /* dump */
70 no_add_queue, /* add_queue */
71 NULL, /* remove_queue */
72 NULL, /* signaled */
73 NULL, /* satified */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000074 no_get_fd, /* get_fd */
Alexandre Julliardb849d792000-02-13 13:56:13 +000075 atom_table_destroy /* destroy */
76};
77
78static struct atom_table *global_table;
79
80
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000081/* copy an atom name from the request to a temporary area */
82static const WCHAR *copy_request_name(void)
Alexandre Julliardb849d792000-02-13 13:56:13 +000083{
84 static WCHAR buffer[MAX_ATOM_LEN+1];
Alexandre Julliardb849d792000-02-13 13:56:13 +000085
Alexandre Julliard9caa71e2001-11-30 18:46:42 +000086 const WCHAR *str = get_req_data();
87 size_t len = get_req_data_size();
88
Alexandre Julliard9c2370b2000-08-30 00:00:48 +000089 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
90 {
91 set_error( STATUS_INVALID_PARAMETER );
92 return NULL;
93 }
94 memcpy( buffer, str, len );
95 buffer[len / sizeof(WCHAR)] = 0;
Alexandre Julliardb849d792000-02-13 13:56:13 +000096 return buffer;
97}
98
99/* create an atom table */
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000100static struct atom_table *create_table(int entries_count)
Alexandre Julliardb849d792000-02-13 13:56:13 +0000101{
102 struct atom_table *table;
103
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000104 if ((table = alloc_object( &atom_table_ops )))
Alexandre Julliardb849d792000-02-13 13:56:13 +0000105 {
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000106 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 )))
110 {
111 set_error( STATUS_NO_MEMORY );
112 goto fail;
113 }
114 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
Alexandre Julliardb849d792000-02-13 13:56:13 +0000115 table->count = 64;
116 table->last = -1;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000117 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
118 return table;
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000119fail:
Alexandre Julliardb849d792000-02-13 13:56:13 +0000120 release_object( table );
121 table = NULL;
122 }
123 return table;
124}
125
126/* retrieve an entry pointer from its atom */
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000127static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
Alexandre Julliardb849d792000-02-13 13:56:13 +0000128{
129 struct atom_entry *entry = NULL;
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000130 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
131 entry = table->handles[atom - MIN_STR_ATOM];
Alexandre Julliardb849d792000-02-13 13:56:13 +0000132 if (!entry) set_error( STATUS_INVALID_HANDLE );
133 return entry;
134}
135
136/* add an atom entry in the table and return its handle */
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000137static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
Alexandre Julliardb849d792000-02-13 13:56:13 +0000138{
139 int i;
140 for (i = 0; i <= table->last; i++)
141 if (!table->handles[i]) goto found;
142 if (i == table->count)
143 {
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 );
149 if (!new_table)
150 {
151 set_error( STATUS_NO_MEMORY );
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000152 return 0;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000153 }
154 table->count = new_size;
155 table->handles = new_table;
156 }
157 table->last = i;
158 found:
159 table->handles[i] = entry;
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000160 entry->atom = i + MIN_STR_ATOM;
161 return entry->atom;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000162}
163
164/* compute the hash code for a string */
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000165static int atom_hash( struct atom_table *table, const WCHAR *str )
Alexandre Julliardb849d792000-02-13 13:56:13 +0000166{
167 int i;
168 WCHAR hash = 0;
Alexandre Julliard7e495e12000-07-25 21:01:59 +0000169 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000170 return hash % table->entries_count;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000171}
172
173/* dump an atom table */
174static void atom_table_dump( struct object *obj, int verbose )
175{
176 int i;
177 struct atom_table *table = (struct atom_table *)obj;
178 assert( obj->ops == &atom_table_ops );
179
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000180 fprintf( stderr, "Atom table size=%d entries=%d\n",
181 table->last + 1, table->entries_count );
Alexandre Julliardb849d792000-02-13 13:56:13 +0000182 if (!verbose) return;
183 for (i = 0; i <= table->last; i++)
184 {
185 struct atom_entry *entry = table->handles[i];
186 if (!entry) continue;
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000187 fprintf( stderr, " %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
Alexandre Julliardb849d792000-02-13 13:56:13 +0000188 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
189 fprintf( stderr, "\"\n" );
190 }
191}
192
193/* destroy the atom table */
194static void atom_table_destroy( struct object *obj )
195{
196 int i;
197 struct atom_table *table = (struct atom_table *)obj;
198 assert( obj->ops == &atom_table_ops );
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000199 if (table->handles)
200 {
201 for (i = 0; i <= table->last; i++) free( table->handles[i] );
202 free( table->handles );
203 }
204 if (table->entries) free( table->entries );
Alexandre Julliardb849d792000-02-13 13:56:13 +0000205}
206
207/* find an atom entry in its hash list */
208static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
209{
210 struct atom_entry *entry = table->entries[hash];
211 while (entry)
212 {
213 if (!strcmpiW( entry->str, str )) break;
214 entry = entry->next;
215 }
216 return entry;
217}
218
219/* close the atom table; used on server exit */
220void close_atom_table(void)
221{
222 if (global_table) release_object( global_table );
223}
224
225/* add an atom to the table */
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000226static atom_t add_atom( struct atom_table *table, const WCHAR *str )
Alexandre Julliardb849d792000-02-13 13:56:13 +0000227{
228 struct atom_entry *entry;
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000229 int hash = atom_hash( table, str );
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000230 atom_t atom = 0;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000231
232 if (!*str)
233 {
234 set_error( STATUS_OBJECT_NAME_INVALID );
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000235 return 0;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000236 }
237 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
238 {
239 entry->count++;
240 return entry->atom;
241 }
242
243 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
244 {
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000245 if ((atom = add_atom_entry( table, entry )))
Alexandre Julliardb849d792000-02-13 13:56:13 +0000246 {
247 entry->prev = NULL;
248 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
249 table->entries[hash] = entry;
250 entry->count = 1;
251 entry->hash = hash;
252 strcpyW( entry->str, str );
253 }
254 else free( entry );
255 }
256 else set_error( STATUS_NO_MEMORY );
257 return atom;
258}
259
260/* delete an atom from the table */
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000261static void delete_atom( struct atom_table *table, atom_t atom )
Alexandre Julliardb849d792000-02-13 13:56:13 +0000262{
263 struct atom_entry *entry = get_atom_entry( table, atom );
264 if (entry && !--entry->count)
265 {
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;
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000269 table->handles[atom - MIN_STR_ATOM] = NULL;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000270 free( entry );
271 }
272}
273
274/* find an atom in the table */
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000275static atom_t find_atom( struct atom_table *table, const WCHAR *str )
Alexandre Julliardb849d792000-02-13 13:56:13 +0000276{
277 struct atom_entry *entry;
278
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000279 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
280 return entry->atom;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000281 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
282 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000283 return 0;
Alexandre Julliardb849d792000-02-13 13:56:13 +0000284}
285
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000286/* increment the ref count of a global atom; used for window properties */
287int grab_global_atom( atom_t atom )
288{
Rein Klazes7e105b42002-01-31 23:34:18 +0000289 if (atom >= MIN_STR_ATOM)
290 {
291 struct atom_entry *entry = get_atom_entry( global_table, atom );
292 if (entry) entry->count++;
293 return (entry != NULL);
294 }
295 else return 1;
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000296}
297
298/* decrement the ref count of a global atom; used for window properties */
299void release_global_atom( atom_t atom )
300{
Rein Klazes7e105b42002-01-31 23:34:18 +0000301 if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom );
Alexandre Julliardd8a8c112001-10-12 18:45:29 +0000302}
303
Alexandre Julliardb849d792000-02-13 13:56:13 +0000304/* add a global atom */
305DECL_HANDLER(add_atom)
306{
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000307 struct atom_table **table_ptr = req->local ? &current->process->atom_table : &global_table;
308
309 if (!*table_ptr) *table_ptr = create_table(0);
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000310 if (*table_ptr)
311 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000312 const WCHAR *name = copy_request_name();
313 if (name) reply->atom = add_atom( *table_ptr, name );
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000314 }
Alexandre Julliardb849d792000-02-13 13:56:13 +0000315}
316
317/* delete a global atom */
318DECL_HANDLER(delete_atom)
319{
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000320 delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
Alexandre Julliardb849d792000-02-13 13:56:13 +0000321}
322
323/* find a global atom */
324DECL_HANDLER(find_atom)
325{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000326 const WCHAR *name = copy_request_name();
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000327 if (name)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000328 reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
Alexandre Julliardb849d792000-02-13 13:56:13 +0000329}
330
331/* get global atom name */
332DECL_HANDLER(get_atom_name)
333{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000334 struct atom_entry *entry;
335 size_t len = 0;
336
337 reply->count = -1;
338 if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
339 req->atom )))
340 {
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 );
345 }
Turchanov Sergei43a27e32000-05-30 20:32:06 +0000346}
347
348/* init the process atom table */
349DECL_HANDLER(init_atom_table)
350{
351 if (!current->process->atom_table)
352 current->process->atom_table = create_table( req->entries );
Alexandre Julliardb849d792000-02-13 13:56:13 +0000353}