Rename back local_compact to LOCAL_Compact for consistency.
[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     short              pinned; /* whether the atom is pinned or not */
50     atom_t             atom;   /* atom handle */
51     unsigned short     hash;   /* string hash */
52     unsigned short     len;    /* string len */
53     WCHAR              str[1]; /* atom string */
54 };
55
56 struct atom_table
57 {
58     struct object       obj;                 /* object header */
59     int                 count;               /* count of atom handles */
60     int                 last;                /* last handle in-use */
61     struct atom_entry **handles;             /* atom handles */
62     int                 entries_count;       /* humber of hash entries */
63     struct atom_entry **entries;             /* hash table entries */
64 };
65
66 static void atom_table_dump( struct object *obj, int verbose );
67 static void atom_table_destroy( struct object *obj );
68
69 static const struct object_ops atom_table_ops =
70 {
71     sizeof(struct atom_table),    /* size */
72     atom_table_dump,              /* dump */
73     no_add_queue,                 /* add_queue */
74     NULL,                         /* remove_queue */
75     NULL,                         /* signaled */
76     NULL,                         /* satisfied */
77     no_signal,                    /* signal */
78     no_get_fd,                    /* get_fd */
79     atom_table_destroy            /* destroy */
80 };
81
82 static struct atom_table *global_table;
83
84
85 /* create an atom table */
86 static struct atom_table *create_table(int entries_count)
87 {
88     struct atom_table *table;
89
90     if ((table = alloc_object( &atom_table_ops )))
91     {
92         if ((entries_count < MIN_HASH_SIZE) ||
93             (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
94         table->entries_count = entries_count;
95         if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
96         {
97             set_error( STATUS_NO_MEMORY );
98             goto fail;
99         }
100         memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
101         table->count = 64;
102         table->last  = -1;
103         if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
104             return table;
105 fail:
106         release_object( table );
107         table = NULL;
108     }
109     return table;
110 }
111
112 /* retrieve an entry pointer from its atom */
113 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
114 {
115     struct atom_entry *entry = NULL;
116     if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
117         entry = table->handles[atom - MIN_STR_ATOM];
118     if (!entry) set_error( STATUS_INVALID_HANDLE );
119     return entry;
120 }
121
122 /* add an atom entry in the table and return its handle */
123 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
124 {
125     int i;
126     for (i = 0; i <= table->last; i++)
127         if (!table->handles[i]) goto found;
128     if (i == table->count)
129     {
130         struct atom_entry **new_table = NULL;
131         int new_size = table->count + table->count / 2;
132         if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
133         if (new_size > table->count)
134             new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
135         if (!new_table)
136         {
137             set_error( STATUS_NO_MEMORY );
138             return 0;
139         }
140         table->count = new_size;
141         table->handles = new_table;
142     }
143     table->last = i;
144  found:
145     table->handles[i] = entry;
146     entry->atom = i + MIN_STR_ATOM;
147     return entry->atom;
148 }
149
150 /* compute the hash code for a string */
151 static unsigned short atom_hash( struct atom_table *table, const WCHAR *str, size_t len )
152 {
153     unsigned int i;
154     unsigned short hash = 0;
155     for (i = 0; i < len; i++) hash ^= toupperW(str[i]) + i;
156     return hash % table->entries_count;
157 }
158
159 /* dump an atom table */
160 static void atom_table_dump( struct object *obj, int verbose )
161 {
162     int i;
163     struct atom_table *table = (struct atom_table *)obj;
164     assert( obj->ops == &atom_table_ops );
165
166     fprintf( stderr, "Atom table size=%d entries=%d\n",
167              table->last + 1, table->entries_count );
168     if (!verbose) return;
169     for (i = 0; i <= table->last; i++)
170     {
171         struct atom_entry *entry = table->handles[i];
172         if (!entry) continue;
173         fprintf( stderr, "  %04x: ref=%d pinned=%c hash=%d \"",
174                  entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
175         dump_strW( entry->str, entry->len, stderr, "\"\"");
176         fprintf( stderr, "\"\n" );
177     }
178 }
179
180 /* destroy the atom table */
181 static void atom_table_destroy( struct object *obj )
182 {
183     int i;
184     struct atom_table *table = (struct atom_table *)obj;
185     assert( obj->ops == &atom_table_ops );
186     if (table->handles)
187     {
188         for (i = 0; i <= table->last; i++) free( table->handles[i] );
189         free( table->handles );
190     }
191     if (table->entries) free( table->entries );
192 }
193
194 /* find an atom entry in its hash list */
195 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
196                                            size_t len, unsigned short hash )
197 {
198     struct atom_entry *entry = table->entries[hash];
199     while (entry)
200     {
201         if (entry->len == len && !memicmpW( entry->str, str, len )) break;
202         entry = entry->next;
203     }
204     return entry;
205 }
206
207 /* close the atom table; used on server exit */
208 void close_atom_table(void)
209 {
210     if (global_table) release_object( global_table );
211 }
212
213 /* add an atom to the table */
214 static atom_t add_atom( struct atom_table *table, const WCHAR *str, size_t len )
215 {
216     struct atom_entry *entry;
217     unsigned short hash = atom_hash( table, str, len );
218     atom_t atom = 0;
219
220     if (!len)
221     {
222         set_error( STATUS_OBJECT_NAME_INVALID );
223         return 0;
224     }
225     if (len > MAX_ATOM_LEN)
226     {
227         set_error( STATUS_INVALID_PARAMETER );
228         return 0;
229     }
230     if ((entry = find_atom_entry( table, str, len, hash )))  /* exists already */
231     {
232         entry->count++;
233         return entry->atom;
234     }
235
236     if ((entry = mem_alloc( sizeof(*entry) + (len - 1) * sizeof(WCHAR) )))
237     {
238         if ((atom = add_atom_entry( table, entry )))
239         {
240             entry->prev  = NULL;
241             if ((entry->next = table->entries[hash])) entry->next->prev = entry;
242             table->entries[hash] = entry;
243             entry->count  = 1;
244             entry->pinned = 0;
245             entry->hash   = hash;
246             entry->len    = len;
247             memcpy( entry->str, str, len * sizeof(WCHAR) );
248         }
249         else free( entry );
250     }
251     else set_error( STATUS_NO_MEMORY );
252     return atom;
253 }
254
255 /* delete an atom from the table */
256 static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
257 {
258     struct atom_entry *entry = get_atom_entry( table, atom );
259     if (!entry) return;
260     if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
261     else if (!--entry->count)
262     {
263         if (entry->next) entry->next->prev = entry->prev;
264         if (entry->prev) entry->prev->next = entry->next;
265         else table->entries[entry->hash] = entry->next;
266         table->handles[atom - MIN_STR_ATOM] = NULL;
267         free( entry );
268     }
269 }
270
271 /* find an atom in the table */
272 static atom_t find_atom( struct atom_table *table, const WCHAR *str, size_t len )
273 {
274     struct atom_entry *entry;
275
276     if (!len)
277     {
278         set_error( STATUS_OBJECT_NAME_INVALID );
279         return 0;
280     }
281     if (len > MAX_ATOM_LEN)
282     {
283         set_error( STATUS_INVALID_PARAMETER );
284         return 0;
285     }
286     if (table && (entry = find_atom_entry( table, str, len, atom_hash(table, str, len) )))
287         return entry->atom;
288     set_error( STATUS_OBJECT_NAME_NOT_FOUND );
289     return 0;
290 }
291
292 static struct atom_table* get_table( obj_handle_t h )
293 {
294     if (h) return (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
295
296     if (!global_table && !(global_table = create_table( HASH_SIZE ))) return NULL;
297     return (struct atom_table *)grab_object( global_table );
298 }
299
300 /* add an atom in the global table; used for window properties */
301 atom_t add_global_atom( const WCHAR *str, size_t len )
302 {
303     if (!global_table && !(global_table = create_table( HASH_SIZE ))) return 0;
304     return add_atom( global_table, str, len );
305 }
306
307 /* find an atom in the global table; used for window properties */
308 atom_t find_global_atom( const WCHAR *str, size_t len )
309 {
310     struct atom_entry *entry;
311
312     if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
313     if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
314         return entry->atom;
315     return 0;
316 }
317
318 /* increment the ref count of a global atom; used for window properties */
319 int grab_global_atom( atom_t atom )
320 {
321     if (atom >= MIN_STR_ATOM)
322     {
323         struct atom_entry *entry = get_atom_entry( global_table, atom );
324         if (entry) entry->count++;
325         return (entry != NULL);
326     }
327     else return 1;
328 }
329
330 /* decrement the ref count of a global atom; used for window properties */
331 void release_global_atom( atom_t atom )
332 {
333     if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom, 1 );
334 }
335
336 /* add a global atom */
337 DECL_HANDLER(add_atom)
338 {
339     struct atom_table *table = get_table( req->table );
340     if (table)
341     {
342         reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
343         release_object( table );
344     }
345 }
346
347 /* delete a global atom */
348 DECL_HANDLER(delete_atom)
349 {
350     struct atom_table *table = get_table( req->table );
351     if (table)
352     {
353         delete_atom( table, req->atom, 0 );
354         release_object( table );
355     }
356 }
357
358 /* find a global atom */
359 DECL_HANDLER(find_atom)
360 {
361     struct atom_table *table = get_table( req->table );
362     if (table)
363     {
364         reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
365         release_object( table );
366     }
367 }
368
369 /* get global atom name */
370 DECL_HANDLER(get_atom_information)
371 {
372     struct atom_table *table = get_table( req->table );
373     if (table)
374     {
375         struct atom_entry *entry;
376
377         if ((entry = get_atom_entry( table, req->atom )))
378         {
379             size_t len = entry->len * sizeof(WCHAR);
380             if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
381             else if (get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
382             reply->count = entry->count;
383             reply->pinned = entry->pinned;
384         }
385         else reply->count = -1;
386         release_object( table );
387     }
388 }
389
390 /* set global atom name */
391 DECL_HANDLER(set_atom_information)
392 {
393     struct atom_table *table = get_table( req->table );
394     if (table)
395     {
396         struct atom_entry *entry;
397
398         if ((entry = get_atom_entry( table, req->atom )))
399         {
400             if (req->pinned) entry->pinned = 1;
401         }
402         release_object( table );
403     }
404 }
405
406 /* init a (local) atom table */
407 DECL_HANDLER(init_atom_table)
408 {
409     struct atom_table* table = create_table( req->entries );
410
411     if (table)
412     {
413         reply->table = alloc_handle( current->process, table, 0, FALSE);
414         release_object( table );
415     }
416 }
417
418 /* set global atom name */
419 DECL_HANDLER(empty_atom_table)
420 {
421     struct atom_table *table = get_table( req->table );
422     if (table)
423     {
424         int i;
425         struct atom_entry *entry;
426
427         for (i = 0; i <= table->last; i++)
428         {
429             entry = table->handles[i];
430             if (entry && (!entry->pinned || req->if_pinned))
431             {
432                 if (entry->next) entry->next->prev = entry->prev;
433                 if (entry->prev) entry->prev->next = entry->next;
434                 else table->entries[entry->hash] = entry->next;
435                 table->handles[i] = NULL;
436                 free( entry );
437             }
438         }
439         release_object( table );
440     }
441 }