Skip queue cleanups if queue has been destroyed already.
[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
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "unicode.h"
14 #include "request.h"
15 #include "object.h"
16 #include "process.h"
17
18 #define HASH_SIZE     37
19 #define MIN_HASH_SIZE 4
20 #define MAX_HASH_SIZE 0x200
21
22 #define MAX_ATOM_LEN  255
23 #define MIN_STR_ATOM  0xc000
24 #define MAX_ATOMS     0x4000
25
26 struct atom_entry
27 {
28     struct atom_entry *next;   /* hash table list */
29     struct atom_entry *prev;   /* hash table list */
30     int                count;  /* reference count */
31     int                hash;   /* string hash */
32     atom_t             atom;   /* atom handle */
33     WCHAR              str[1]; /* atom string */
34 };
35
36 struct atom_table
37 {
38     struct object       obj;                 /* object header */
39     int                 count;               /* count of atom handles */
40     int                 last;                /* last handle in-use */
41     struct atom_entry **handles;             /* atom handles */
42     int                 entries_count;       /* humber of hash entries */
43     struct atom_entry **entries;             /* hash table entries */
44 };
45
46 static void atom_table_dump( struct object *obj, int verbose );
47 static void atom_table_destroy( struct object *obj );
48
49 static const struct object_ops atom_table_ops =
50 {
51     sizeof(struct atom_table),    /* size */
52     atom_table_dump,              /* dump */
53     no_add_queue,                 /* add_queue */
54     NULL,                         /* remove_queue */
55     NULL,                         /* signaled */
56     NULL,                         /* satified */
57     NULL,                         /* get_poll_events */
58     NULL,                         /* poll_event */
59     no_get_fd,                    /* get_fd */
60     no_flush,                     /* flush */
61     no_get_file_info,             /* get_file_info */
62     atom_table_destroy            /* destroy */
63 };
64
65 static struct atom_table *global_table;
66
67
68 /* copy an atom name to a temporary area */
69 static const WCHAR *copy_name( const WCHAR *str, size_t len )
70 {
71     static WCHAR buffer[MAX_ATOM_LEN+1];
72
73     if (len > MAX_ATOM_LEN*sizeof(WCHAR))
74     {
75         set_error( STATUS_INVALID_PARAMETER );
76         return NULL;
77     }
78     memcpy( buffer, str, len );
79     buffer[len / sizeof(WCHAR)] = 0;
80     return buffer;
81 }
82
83 /* create an atom table */
84 static struct atom_table *create_table(int entries_count)
85 {
86     struct atom_table *table;
87
88     if ((table = alloc_object( &atom_table_ops, -1 )))
89     {
90         if ((entries_count < MIN_HASH_SIZE) ||
91             (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
92         table->entries_count = entries_count;
93         if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
94         {
95             set_error( STATUS_NO_MEMORY );
96             goto fail;
97         }
98         memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
99         table->count = 64;
100         table->last  = -1;
101         if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
102             return table;
103 fail:
104         release_object( table );
105         table = NULL;
106     }
107     return table;
108 }
109
110 /* retrieve an entry pointer from its atom */
111 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
112 {
113     struct atom_entry *entry = NULL;
114     if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
115         entry = table->handles[atom - MIN_STR_ATOM];
116     if (!entry) set_error( STATUS_INVALID_HANDLE );
117     return entry;
118 }
119
120 /* add an atom entry in the table and return its handle */
121 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
122 {
123     int i;
124     for (i = 0; i <= table->last; i++)
125         if (!table->handles[i]) goto found;
126     if (i == table->count)
127     {
128         struct atom_entry **new_table = NULL;
129         int new_size = table->count + table->count / 2;
130         if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
131         if (new_size > table->count)
132             new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
133         if (!new_table)
134         {
135             set_error( STATUS_NO_MEMORY );
136             return 0;
137         }
138         table->count = new_size;
139         table->handles = new_table;
140     }
141     table->last = i;
142  found:
143     table->handles[i] = entry;
144     entry->atom = i + MIN_STR_ATOM;
145     return entry->atom;
146 }
147
148 /* compute the hash code for a string */
149 static int atom_hash( struct atom_table *table, const WCHAR *str )
150 {
151     int i;
152     WCHAR hash = 0;
153     for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
154     return hash % table->entries_count;
155 }
156
157 /* dump an atom table */
158 static void atom_table_dump( struct object *obj, int verbose )
159 {
160     int i;
161     struct atom_table *table = (struct atom_table *)obj;
162     assert( obj->ops == &atom_table_ops );
163
164     fprintf( stderr, "Atom table size=%d entries=%d\n",
165              table->last + 1, table->entries_count );
166     if (!verbose) return;
167     for (i = 0; i <= table->last; i++)
168     {
169         struct atom_entry *entry = table->handles[i];
170         if (!entry) continue;
171         fprintf( stderr, "  %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
172         dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
173         fprintf( stderr, "\"\n" );
174     }
175 }
176
177 /* destroy the atom table */
178 static void atom_table_destroy( struct object *obj )
179 {
180     int i;
181     struct atom_table *table = (struct atom_table *)obj;
182     assert( obj->ops == &atom_table_ops );
183     if (table->handles)
184     {
185         for (i = 0; i <= table->last; i++) free( table->handles[i] );
186         free( table->handles );
187     }
188     if (table->entries) free( table->entries );
189 }
190
191 /* find an atom entry in its hash list */
192 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
193 {
194     struct atom_entry *entry = table->entries[hash];
195     while (entry)
196     {
197         if (!strcmpiW( entry->str, str )) break;
198         entry = entry->next;
199     }
200     return entry;
201 }
202
203 /* close the atom table; used on server exit */
204 void close_atom_table(void)
205 {
206     if (global_table) release_object( global_table );
207 }
208
209 /* add an atom to the table */
210 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
211 {
212     struct atom_entry *entry;
213     int hash = atom_hash( table, str );
214     atom_t atom = 0;
215
216     if (!*str)
217     {
218         set_error( STATUS_OBJECT_NAME_INVALID );
219         return 0;
220     }
221     if ((entry = find_atom_entry( table, str, hash )))  /* exists already */
222     {
223         entry->count++;
224         return entry->atom;
225     }
226
227     if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
228     {
229         if ((atom = add_atom_entry( table, entry )))
230         {
231             entry->prev  = NULL;
232             if ((entry->next = table->entries[hash])) entry->next->prev = entry;
233             table->entries[hash] = entry;
234             entry->count = 1;
235             entry->hash  = hash;
236             strcpyW( entry->str, str );
237         }
238         else free( entry );
239     }
240     else set_error( STATUS_NO_MEMORY );
241     return atom;
242 }
243
244 /* delete an atom from the table */
245 static void delete_atom( struct atom_table *table, atom_t atom )
246 {
247     struct atom_entry *entry = get_atom_entry( table, atom );
248     if (entry && !--entry->count)
249     {
250         if (entry->next) entry->next->prev = entry->prev;
251         if (entry->prev) entry->prev->next = entry->next;
252         else table->entries[entry->hash] = entry->next;
253         table->handles[atom - MIN_STR_ATOM] = NULL;
254         free( entry );
255     }
256 }
257
258 /* find an atom in the table */
259 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
260 {
261     struct atom_entry *entry;
262
263     if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
264         return entry->atom;
265     if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
266     else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
267     return 0;
268 }
269
270 /* get an atom name and refcount*/
271 static size_t get_atom_name( struct atom_table *table, atom_t atom,
272                              WCHAR *str, size_t maxsize, int *count )
273 {
274     size_t len = 0;
275     struct atom_entry *entry = get_atom_entry( table, atom );
276     *count = -1;
277     if (entry)
278     {
279         *count = entry->count;
280         len = strlenW( entry->str ) * sizeof(WCHAR);
281         if (len <= maxsize) memcpy( str, entry->str, len );
282         else
283         {
284             set_error( STATUS_BUFFER_OVERFLOW );
285             len = 0;
286         }
287     }
288     return len;
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     struct atom_entry *entry = get_atom_entry( global_table, atom );
295     if (entry) entry->count++;
296     return (entry != NULL);
297 }
298
299 /* decrement the ref count of a global atom; used for window properties */
300 void release_global_atom( atom_t atom )
301 {
302     delete_atom( global_table, atom );
303 }
304
305 /* add a global atom */
306 DECL_HANDLER(add_atom)
307 {
308     struct atom_table **table_ptr = req->local ? &current->process->atom_table : &global_table;
309
310     if (!*table_ptr) *table_ptr = create_table(0);
311     if (*table_ptr)
312     {
313         const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
314         if (name) req->atom = add_atom( *table_ptr, name );
315     }
316 }
317
318 /* delete a global atom */
319 DECL_HANDLER(delete_atom)
320 {
321     delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
322 }
323
324 /* find a global atom */
325 DECL_HANDLER(find_atom)
326 {
327     const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
328     if (name)
329         req->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
330 }
331
332 /* get global atom name */
333 DECL_HANDLER(get_atom_name)
334 {
335     WCHAR *name = get_req_data(req);
336     size_t size = get_atom_name( req->local ? current->process->atom_table : global_table,
337                                  req->atom, name, get_req_data_size(req), &req->count );
338     set_req_data_size( req, size );
339 }
340
341 /* init the process atom table */
342 DECL_HANDLER(init_atom_table)
343 {
344     if (!current->process->atom_table)
345         current->process->atom_table = create_table( req->entries );
346 }