Grab the thread object in case the thread terminates with pending
[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
35 #define HASH_SIZE     37
36 #define MIN_HASH_SIZE 4
37 #define MAX_HASH_SIZE 0x200
38
39 #define MAX_ATOM_LEN  255
40 #define MIN_STR_ATOM  0xc000
41 #define MAX_ATOMS     0x4000
42
43 struct atom_entry
44 {
45     struct atom_entry *next;   /* hash table list */
46     struct atom_entry *prev;   /* hash table list */
47     int                count;  /* reference count */
48     int                hash;   /* string hash */
49     atom_t             atom;   /* atom handle */
50     WCHAR              str[1]; /* atom string */
51 };
52
53 struct 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 */
59     int                 entries_count;       /* humber of hash entries */
60     struct atom_entry **entries;             /* hash table entries */
61 };
62
63 static void atom_table_dump( struct object *obj, int verbose );
64 static void atom_table_destroy( struct object *obj );
65
66 static 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 */
74     NULL,                         /* get_poll_events */
75     NULL,                         /* poll_event */
76     no_get_fd,                    /* get_fd */
77     no_flush,                     /* flush */
78     no_get_file_info,             /* get_file_info */
79     NULL,                         /* queue_async */
80     atom_table_destroy            /* destroy */
81 };
82
83 static struct atom_table *global_table;
84
85
86 /* copy an atom name from the request to a temporary area */
87 static const WCHAR *copy_request_name(void)
88 {
89     static WCHAR buffer[MAX_ATOM_LEN+1];
90
91     const WCHAR *str = get_req_data();
92     size_t len = get_req_data_size();
93
94     if (len > MAX_ATOM_LEN*sizeof(WCHAR))
95     {
96         set_error( STATUS_INVALID_PARAMETER );
97         return NULL;
98     }
99     memcpy( buffer, str, len );
100     buffer[len / sizeof(WCHAR)] = 0;
101     return buffer;
102 }
103
104 /* create an atom table */
105 static struct atom_table *create_table(int entries_count)
106 {
107     struct atom_table *table;
108
109     if ((table = alloc_object( &atom_table_ops, -1 )))
110     {
111         if ((entries_count < MIN_HASH_SIZE) ||
112             (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
113         table->entries_count = entries_count;
114         if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
115         {
116             set_error( STATUS_NO_MEMORY );
117             goto fail;
118         }
119         memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
120         table->count = 64;
121         table->last  = -1;
122         if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
123             return table;
124 fail:
125         release_object( table );
126         table = NULL;
127     }
128     return table;
129 }
130
131 /* retrieve an entry pointer from its atom */
132 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
133 {
134     struct atom_entry *entry = NULL;
135     if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
136         entry = table->handles[atom - MIN_STR_ATOM];
137     if (!entry) set_error( STATUS_INVALID_HANDLE );
138     return entry;
139 }
140
141 /* add an atom entry in the table and return its handle */
142 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
143 {
144     int i;
145     for (i = 0; i <= table->last; i++)
146         if (!table->handles[i]) goto found;
147     if (i == table->count)
148     {
149         struct atom_entry **new_table = NULL;
150         int new_size = table->count + table->count / 2;
151         if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
152         if (new_size > table->count)
153             new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
154         if (!new_table)
155         {
156             set_error( STATUS_NO_MEMORY );
157             return 0;
158         }
159         table->count = new_size;
160         table->handles = new_table;
161     }
162     table->last = i;
163  found:
164     table->handles[i] = entry;
165     entry->atom = i + MIN_STR_ATOM;
166     return entry->atom;
167 }
168
169 /* compute the hash code for a string */
170 static int atom_hash( struct atom_table *table, const WCHAR *str )
171 {
172     int i;
173     WCHAR hash = 0;
174     for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
175     return hash % table->entries_count;
176 }
177
178 /* dump an atom table */
179 static void atom_table_dump( struct object *obj, int verbose )
180 {
181     int i;
182     struct atom_table *table = (struct atom_table *)obj;
183     assert( obj->ops == &atom_table_ops );
184
185     fprintf( stderr, "Atom table size=%d entries=%d\n",
186              table->last + 1, table->entries_count );
187     if (!verbose) return;
188     for (i = 0; i <= table->last; i++)
189     {
190         struct atom_entry *entry = table->handles[i];
191         if (!entry) continue;
192         fprintf( stderr, "  %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
193         dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
194         fprintf( stderr, "\"\n" );
195     }
196 }
197
198 /* destroy the atom table */
199 static void atom_table_destroy( struct object *obj )
200 {
201     int i;
202     struct atom_table *table = (struct atom_table *)obj;
203     assert( obj->ops == &atom_table_ops );
204     if (table->handles)
205     {
206         for (i = 0; i <= table->last; i++) free( table->handles[i] );
207         free( table->handles );
208     }
209     if (table->entries) free( table->entries );
210 }
211
212 /* find an atom entry in its hash list */
213 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
214 {
215     struct atom_entry *entry = table->entries[hash];
216     while (entry)
217     {
218         if (!strcmpiW( entry->str, str )) break;
219         entry = entry->next;
220     }
221     return entry;
222 }
223
224 /* close the atom table; used on server exit */
225 void close_atom_table(void)
226 {
227     if (global_table) release_object( global_table );
228 }
229
230 /* add an atom to the table */
231 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
232 {
233     struct atom_entry *entry;
234     int hash = atom_hash( table, str );
235     atom_t atom = 0;
236
237     if (!*str)
238     {
239         set_error( STATUS_OBJECT_NAME_INVALID );
240         return 0;
241     }
242     if ((entry = find_atom_entry( table, str, hash )))  /* exists already */
243     {
244         entry->count++;
245         return entry->atom;
246     }
247
248     if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
249     {
250         if ((atom = add_atom_entry( table, entry )))
251         {
252             entry->prev  = NULL;
253             if ((entry->next = table->entries[hash])) entry->next->prev = entry;
254             table->entries[hash] = entry;
255             entry->count = 1;
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 /* add a global atom */
310 DECL_HANDLER(add_atom)
311 {
312     struct atom_table **table_ptr = req->local ? &current->process->atom_table : &global_table;
313
314     if (!*table_ptr) *table_ptr = create_table(0);
315     if (*table_ptr)
316     {
317         const WCHAR *name = copy_request_name();
318         if (name) reply->atom = add_atom( *table_ptr, name );
319     }
320 }
321
322 /* delete a global atom */
323 DECL_HANDLER(delete_atom)
324 {
325     delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
326 }
327
328 /* find a global atom */
329 DECL_HANDLER(find_atom)
330 {
331     const WCHAR *name = copy_request_name();
332     if (name)
333         reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
334 }
335
336 /* get global atom name */
337 DECL_HANDLER(get_atom_name)
338 {
339     struct atom_entry *entry;
340     size_t len = 0;
341
342     reply->count = -1;
343     if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
344                                  req->atom )))
345     {
346         reply->count = entry->count;
347         len = strlenW( entry->str ) * sizeof(WCHAR);
348         if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
349         else set_error( STATUS_BUFFER_OVERFLOW );
350     }
351 }
352
353 /* init the process atom table */
354 DECL_HANDLER(init_atom_table)
355 {
356     if (!current->process->atom_table)
357         current->process->atom_table = create_table( req->entries );
358 }