Let the console renderer be defined as a thread.
[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 from the request to a temporary area */
69 static const WCHAR *copy_request_name(void)
70 {
71     static WCHAR buffer[MAX_ATOM_LEN+1];
72
73     const WCHAR *str = get_req_data();
74     size_t len = get_req_data_size();
75
76     if (len > MAX_ATOM_LEN*sizeof(WCHAR))
77     {
78         set_error( STATUS_INVALID_PARAMETER );
79         return NULL;
80     }
81     memcpy( buffer, str, len );
82     buffer[len / sizeof(WCHAR)] = 0;
83     return buffer;
84 }
85
86 /* create an atom table */
87 static struct atom_table *create_table(int entries_count)
88 {
89     struct atom_table *table;
90
91     if ((table = alloc_object( &atom_table_ops, -1 )))
92     {
93         if ((entries_count < MIN_HASH_SIZE) ||
94             (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
95         table->entries_count = entries_count;
96         if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
97         {
98             set_error( STATUS_NO_MEMORY );
99             goto fail;
100         }
101         memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
102         table->count = 64;
103         table->last  = -1;
104         if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
105             return table;
106 fail:
107         release_object( table );
108         table = NULL;
109     }
110     return table;
111 }
112
113 /* retrieve an entry pointer from its atom */
114 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
115 {
116     struct atom_entry *entry = NULL;
117     if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
118         entry = table->handles[atom - MIN_STR_ATOM];
119     if (!entry) set_error( STATUS_INVALID_HANDLE );
120     return entry;
121 }
122
123 /* add an atom entry in the table and return its handle */
124 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
125 {
126     int i;
127     for (i = 0; i <= table->last; i++)
128         if (!table->handles[i]) goto found;
129     if (i == table->count)
130     {
131         struct atom_entry **new_table = NULL;
132         int new_size = table->count + table->count / 2;
133         if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
134         if (new_size > table->count)
135             new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
136         if (!new_table)
137         {
138             set_error( STATUS_NO_MEMORY );
139             return 0;
140         }
141         table->count = new_size;
142         table->handles = new_table;
143     }
144     table->last = i;
145  found:
146     table->handles[i] = entry;
147     entry->atom = i + MIN_STR_ATOM;
148     return entry->atom;
149 }
150
151 /* compute the hash code for a string */
152 static int atom_hash( struct atom_table *table, const WCHAR *str )
153 {
154     int i;
155     WCHAR hash = 0;
156     for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
157     return hash % table->entries_count;
158 }
159
160 /* dump an atom table */
161 static void atom_table_dump( struct object *obj, int verbose )
162 {
163     int i;
164     struct atom_table *table = (struct atom_table *)obj;
165     assert( obj->ops == &atom_table_ops );
166
167     fprintf( stderr, "Atom table size=%d entries=%d\n",
168              table->last + 1, table->entries_count );
169     if (!verbose) return;
170     for (i = 0; i <= table->last; i++)
171     {
172         struct atom_entry *entry = table->handles[i];
173         if (!entry) continue;
174         fprintf( stderr, "  %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
175         dump_strW( entry->str, strlenW(entry->str), 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, int hash )
196 {
197     struct atom_entry *entry = table->entries[hash];
198     while (entry)
199     {
200         if (!strcmpiW( entry->str, str )) break;
201         entry = entry->next;
202     }
203     return entry;
204 }
205
206 /* close the atom table; used on server exit */
207 void close_atom_table(void)
208 {
209     if (global_table) release_object( global_table );
210 }
211
212 /* add an atom to the table */
213 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
214 {
215     struct atom_entry *entry;
216     int hash = atom_hash( table, str );
217     atom_t atom = 0;
218
219     if (!*str)
220     {
221         set_error( STATUS_OBJECT_NAME_INVALID );
222         return 0;
223     }
224     if ((entry = find_atom_entry( table, str, hash )))  /* exists already */
225     {
226         entry->count++;
227         return entry->atom;
228     }
229
230     if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
231     {
232         if ((atom = add_atom_entry( table, entry )))
233         {
234             entry->prev  = NULL;
235             if ((entry->next = table->entries[hash])) entry->next->prev = entry;
236             table->entries[hash] = entry;
237             entry->count = 1;
238             entry->hash  = hash;
239             strcpyW( entry->str, str );
240         }
241         else free( entry );
242     }
243     else set_error( STATUS_NO_MEMORY );
244     return atom;
245 }
246
247 /* delete an atom from the table */
248 static void delete_atom( struct atom_table *table, atom_t atom )
249 {
250     struct atom_entry *entry = get_atom_entry( table, atom );
251     if (entry && !--entry->count)
252     {
253         if (entry->next) entry->next->prev = entry->prev;
254         if (entry->prev) entry->prev->next = entry->next;
255         else table->entries[entry->hash] = entry->next;
256         table->handles[atom - MIN_STR_ATOM] = NULL;
257         free( entry );
258     }
259 }
260
261 /* find an atom in the table */
262 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
263 {
264     struct atom_entry *entry;
265
266     if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
267         return entry->atom;
268     if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
269     else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
270     return 0;
271 }
272
273 /* increment the ref count of a global atom; used for window properties */
274 int grab_global_atom( atom_t atom )
275 {
276     struct atom_entry *entry = get_atom_entry( global_table, atom );
277     if (entry) entry->count++;
278     return (entry != NULL);
279 }
280
281 /* decrement the ref count of a global atom; used for window properties */
282 void release_global_atom( atom_t atom )
283 {
284     delete_atom( global_table, atom );
285 }
286
287 /* add a global atom */
288 DECL_HANDLER(add_atom)
289 {
290     struct atom_table **table_ptr = req->local ? &current->process->atom_table : &global_table;
291
292     if (!*table_ptr) *table_ptr = create_table(0);
293     if (*table_ptr)
294     {
295         const WCHAR *name = copy_request_name();
296         if (name) reply->atom = add_atom( *table_ptr, name );
297     }
298 }
299
300 /* delete a global atom */
301 DECL_HANDLER(delete_atom)
302 {
303     delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
304 }
305
306 /* find a global atom */
307 DECL_HANDLER(find_atom)
308 {
309     const WCHAR *name = copy_request_name();
310     if (name)
311         reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
312 }
313
314 /* get global atom name */
315 DECL_HANDLER(get_atom_name)
316 {
317     struct atom_entry *entry;
318     size_t len = 0;
319
320     reply->count = -1;
321     if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
322                                  req->atom )))
323     {
324         reply->count = entry->count;
325         len = strlenW( entry->str ) * sizeof(WCHAR);
326         if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
327         else set_error( STATUS_BUFFER_OVERFLOW );
328     }
329 }
330
331 /* init the process atom table */
332 DECL_HANDLER(init_atom_table)
333 {
334     if (!current->process->atom_table)
335         current->process->atom_table = create_table( req->entries );
336 }