- changed ATOM support in wineserver to match NTDLL needs
[wine] / server / atom.c
1 /*
2  * Server-side atom management
3  *
4  * Copyright (C) 1999, 2000 Alexandre Julliard
5  * Copyright (C) 2000 Turchanov Sergei
6  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "unicode.h"
31 #include "request.h"
32 #include "object.h"
33 #include "process.h"
34 #include "handle.h"
35
36 #define HASH_SIZE     37
37 #define MIN_HASH_SIZE 4
38 #define MAX_HASH_SIZE 0x200
39
40 #define MAX_ATOM_LEN  255
41 #define MIN_STR_ATOM  0xc000
42 #define MAX_ATOMS     0x4000
43
44 struct atom_entry
45 {
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 */
53 };
54
55 struct atom_table
56 {
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 */
63 };
64
65 static void atom_table_dump( struct object *obj, int verbose );
66 static void atom_table_destroy( struct object *obj );
67
68 static const struct object_ops atom_table_ops =
69 {
70     sizeof(struct atom_table),    /* size */
71     atom_table_dump,              /* dump */
72     no_add_queue,                 /* add_queue */
73     NULL,                         /* remove_queue */
74     NULL,                         /* signaled */
75     NULL,                         /* satisfied */
76     no_signal,                    /* signal */
77     no_get_fd,                    /* get_fd */
78     atom_table_destroy            /* destroy */
79 };
80
81 static struct atom_table *global_table;
82
83
84 /* copy an atom name from the request to a temporary area */
85 static const WCHAR *copy_request_name(void)
86 {
87     static WCHAR buffer[MAX_ATOM_LEN+1];
88
89     const WCHAR *str = get_req_data();
90     size_t len = get_req_data_size();
91
92     if (len > MAX_ATOM_LEN*sizeof(WCHAR))
93     {
94         set_error( STATUS_INVALID_PARAMETER );
95         return NULL;
96     }
97     memcpy( buffer, str, len );
98     buffer[len / sizeof(WCHAR)] = 0;
99     return buffer;
100 }
101
102 /* create an atom table */
103 static struct atom_table *create_table(int entries_count)
104 {
105     struct atom_table *table;
106
107     if ((table = alloc_object( &atom_table_ops )))
108     {
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 )))
113         {
114             set_error( STATUS_NO_MEMORY );
115             goto fail;
116         }
117         memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
118         table->count = 64;
119         table->last  = -1;
120         if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
121             return table;
122 fail:
123         release_object( table );
124         table = NULL;
125     }
126     return table;
127 }
128
129 /* retrieve an entry pointer from its atom */
130 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
131 {
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 );
136     return entry;
137 }
138
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 )
141 {
142     int i;
143     for (i = 0; i <= table->last; i++)
144         if (!table->handles[i]) goto found;
145     if (i == table->count)
146     {
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 );
152         if (!new_table)
153         {
154             set_error( STATUS_NO_MEMORY );
155             return 0;
156         }
157         table->count = new_size;
158         table->handles = new_table;
159     }
160     table->last = i;
161  found:
162     table->handles[i] = entry;
163     entry->atom = i + MIN_STR_ATOM;
164     return entry->atom;
165 }
166
167 /* compute the hash code for a string */
168 static int atom_hash( struct atom_table *table, const WCHAR *str )
169 {
170     int i;
171     WCHAR hash = 0;
172     for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
173     return hash % table->entries_count;
174 }
175
176 /* dump an atom table */
177 static void atom_table_dump( struct object *obj, int verbose )
178 {
179     int i;
180     struct atom_table *table = (struct atom_table *)obj;
181     assert( obj->ops == &atom_table_ops );
182
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++)
187     {
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" );
194     }
195 }
196
197 /* destroy the atom table */
198 static void atom_table_destroy( struct object *obj )
199 {
200     int i;
201     struct atom_table *table = (struct atom_table *)obj;
202     assert( obj->ops == &atom_table_ops );
203     if (table->handles)
204     {
205         for (i = 0; i <= table->last; i++) free( table->handles[i] );
206         free( table->handles );
207     }
208     if (table->entries) free( table->entries );
209 }
210
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 )
213 {
214     struct atom_entry *entry = table->entries[hash];
215     while (entry)
216     {
217         if (!strcmpiW( entry->str, str )) break;
218         entry = entry->next;
219     }
220     return entry;
221 }
222
223 /* close the atom table; used on server exit */
224 void close_atom_table(void)
225 {
226     if (global_table) release_object( global_table );
227 }
228
229 /* add an atom to the table */
230 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
231 {
232     struct atom_entry *entry;
233     int hash = atom_hash( table, str );
234     atom_t atom = 0;
235
236     if (!*str)
237     {
238         set_error( STATUS_OBJECT_NAME_INVALID );
239         return 0;
240     }
241     if ((entry = find_atom_entry( table, str, hash )))  /* exists already */
242     {
243         entry->count++;
244         return entry->atom;
245     }
246
247     if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
248     {
249         if ((atom = add_atom_entry( table, entry )))
250         {
251             entry->prev  = NULL;
252             if ((entry->next = table->entries[hash])) entry->next->prev = entry;
253             table->entries[hash] = entry;
254             entry->count = 1;
255             entry->pinned = 0;
256             entry->hash  = hash;
257             strcpyW( entry->str, str );
258         }
259         else free( entry );
260     }
261     else set_error( STATUS_NO_MEMORY );
262     return atom;
263 }
264
265 /* delete an atom from the table */
266 static void delete_atom( struct atom_table *table, atom_t atom )
267 {
268     struct atom_entry *entry = get_atom_entry( table, atom );
269     if (entry && !--entry->count)
270     {
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;
275         free( entry );
276     }
277 }
278
279 /* find an atom in the table */
280 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
281 {
282     struct atom_entry *entry;
283
284     if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
285         return entry->atom;
286     if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
287     else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
288     return 0;
289 }
290
291 /* increment the ref count of a global atom; used for window properties */
292 int grab_global_atom( atom_t atom )
293 {
294     if (atom >= MIN_STR_ATOM)
295     {
296         struct atom_entry *entry = get_atom_entry( global_table, atom );
297         if (entry) entry->count++;
298         return (entry != NULL);
299     }
300     else return 1;
301 }
302
303 /* decrement the ref count of a global atom; used for window properties */
304 void release_global_atom( atom_t atom )
305 {
306     if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom );
307 }
308
309 static struct atom_table* get_table( obj_handle_t h )
310 {
311     struct atom_table *table;
312
313     if (h)
314     {
315         table = (struct atom_table*)get_handle_obj( current->process, h,
316                                                     0, &atom_table_ops );
317     }
318     else
319     {
320         if (!global_table && !(global_table = create_table( HASH_SIZE )))
321             return NULL;
322         table = (struct atom_table*)grab_object( global_table );
323     }
324     return table;
325 }
326
327 /* add a global atom */
328 DECL_HANDLER(add_atom)
329 {
330     struct atom_table *table = get_table( req->table );
331     if (table)
332     {
333         const WCHAR *name = copy_request_name();
334         if (name) reply->atom = add_atom( table, name );
335         release_object( table );
336     }
337 }
338
339 /* delete a global atom */
340 DECL_HANDLER(delete_atom)
341 {
342     struct atom_table *table = get_table( req->table );
343     if (table)
344     {
345         delete_atom( table, req->atom );
346         release_object( table );
347     }
348 }
349
350 /* find a global atom */
351 DECL_HANDLER(find_atom)
352 {
353     struct atom_table *table = get_table( req->table );
354     if (table)
355     {
356         const WCHAR *name = copy_request_name();
357         if (name)
358             reply->atom = find_atom( table, name );
359         release_object( table );
360     }
361 }
362
363 /* get global atom name */
364 DECL_HANDLER(get_atom_information)
365 {
366     struct atom_table *table = get_table( req->table );
367     if (table)
368     {
369         struct atom_entry *entry;
370
371         if ((entry = get_atom_entry( table, req->atom )))
372         {
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;
378         }
379         else reply->count = -1;
380         release_object( table );
381     }
382 }
383
384 /* set global atom name */
385 DECL_HANDLER(set_atom_information)
386 {
387     struct atom_table *table = get_table( req->table );
388     if (table)
389     {
390         struct atom_entry *entry;
391
392         if ((entry = get_atom_entry( table, req->atom )))
393         {
394             if (req->pinned) entry->pinned = 1;
395         }
396         release_object( table );
397     }
398 }
399
400 /* init a (local) atom table */
401 DECL_HANDLER(init_atom_table)
402 {
403     struct atom_table* table;
404
405     table = create_table( req->entries );
406     reply->table = alloc_handle( current->process, table, 0, FALSE);
407     release_object( table );
408 }
409
410 /* set global atom name */
411 DECL_HANDLER(empty_atom_table)
412 {
413     struct atom_table *table = get_table( req->table );
414     if (table)
415     {
416         int i;
417         struct atom_entry *entry;
418
419         for (i = 0; i <= table->last; i++)
420         {
421             entry = table->handles[i];
422             if (entry && (!entry->pinned || req->if_pinned))
423             {
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;
428                 free( entry );
429             }
430         }
431         release_object( table );
432     }
433 }