Use a better location than HKCU\Wine for saving the temporary
[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     no_close_handle,              /* close_handle */
80     atom_table_destroy            /* destroy */
81 };
82
83 static struct atom_table *global_table;
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 )))
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 unsigned short atom_hash( struct atom_table *table, const WCHAR *str, size_t len )
153 {
154     unsigned int i;
155     unsigned short hash = 0;
156     for (i = 0; i < len; 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 pinned=%c hash=%d \"",
175                  entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
176         dump_strW( entry->str, entry->len, stderr, "\"\"");
177         fprintf( stderr, "\"\n" );
178     }
179 }
180
181 /* destroy the atom table */
182 static void atom_table_destroy( struct object *obj )
183 {
184     int i;
185     struct atom_table *table = (struct atom_table *)obj;
186     assert( obj->ops == &atom_table_ops );
187     if (table->handles)
188     {
189         for (i = 0; i <= table->last; i++) free( table->handles[i] );
190         free( table->handles );
191     }
192     if (table->entries) free( table->entries );
193 }
194
195 /* find an atom entry in its hash list */
196 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
197                                            size_t len, unsigned short hash )
198 {
199     struct atom_entry *entry = table->entries[hash];
200     while (entry)
201     {
202         if (entry->len == len && !memicmpW( entry->str, str, len )) break;
203         entry = entry->next;
204     }
205     return entry;
206 }
207
208 /* close the atom table; used on server exit */
209 void close_atom_table(void)
210 {
211     if (global_table) release_object( global_table );
212 }
213
214 /* add an atom to the table */
215 static atom_t add_atom( struct atom_table *table, const WCHAR *str, size_t len )
216 {
217     struct atom_entry *entry;
218     unsigned short hash = atom_hash( table, str, len );
219     atom_t atom = 0;
220
221     if (!len)
222     {
223         set_error( STATUS_OBJECT_NAME_INVALID );
224         return 0;
225     }
226     if (len > MAX_ATOM_LEN)
227     {
228         set_error( STATUS_INVALID_PARAMETER );
229         return 0;
230     }
231     if ((entry = find_atom_entry( table, str, len, hash )))  /* exists already */
232     {
233         entry->count++;
234         return entry->atom;
235     }
236
237     if ((entry = mem_alloc( sizeof(*entry) + (len - 1) * sizeof(WCHAR) )))
238     {
239         if ((atom = add_atom_entry( table, entry )))
240         {
241             entry->prev  = NULL;
242             if ((entry->next = table->entries[hash])) entry->next->prev = entry;
243             table->entries[hash] = entry;
244             entry->count  = 1;
245             entry->pinned = 0;
246             entry->hash   = hash;
247             entry->len    = len;
248             memcpy( entry->str, str, len * sizeof(WCHAR) );
249         }
250         else free( entry );
251     }
252     else set_error( STATUS_NO_MEMORY );
253     return atom;
254 }
255
256 /* delete an atom from the table */
257 static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
258 {
259     struct atom_entry *entry = get_atom_entry( table, atom );
260     if (!entry) return;
261     if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
262     else if (!--entry->count)
263     {
264         if (entry->next) entry->next->prev = entry->prev;
265         if (entry->prev) entry->prev->next = entry->next;
266         else table->entries[entry->hash] = entry->next;
267         table->handles[atom - MIN_STR_ATOM] = NULL;
268         free( entry );
269     }
270 }
271
272 /* find an atom in the table */
273 static atom_t find_atom( struct atom_table *table, const WCHAR *str, size_t len )
274 {
275     struct atom_entry *entry;
276
277     if (!len)
278     {
279         set_error( STATUS_OBJECT_NAME_INVALID );
280         return 0;
281     }
282     if (len > MAX_ATOM_LEN)
283     {
284         set_error( STATUS_INVALID_PARAMETER );
285         return 0;
286     }
287     if (table && (entry = find_atom_entry( table, str, len, atom_hash(table, str, len) )))
288         return entry->atom;
289     set_error( STATUS_OBJECT_NAME_NOT_FOUND );
290     return 0;
291 }
292
293 static struct atom_table* get_table( obj_handle_t h )
294 {
295     if (h) return (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
296
297     if (!global_table && !(global_table = create_table( HASH_SIZE ))) return NULL;
298     return (struct atom_table *)grab_object( global_table );
299 }
300
301 /* add an atom in the global table; used for window properties */
302 atom_t add_global_atom( const WCHAR *str, size_t len )
303 {
304     if (!global_table && !(global_table = create_table( HASH_SIZE ))) return 0;
305     return add_atom( global_table, str, len );
306 }
307
308 /* find an atom in the global table; used for window properties */
309 atom_t find_global_atom( const WCHAR *str, size_t len )
310 {
311     struct atom_entry *entry;
312
313     if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
314     if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
315         return entry->atom;
316     return 0;
317 }
318
319 /* increment the ref count of a global atom; used for window properties */
320 int grab_global_atom( atom_t atom )
321 {
322     if (atom >= MIN_STR_ATOM)
323     {
324         struct atom_entry *entry = get_atom_entry( global_table, atom );
325         if (entry) entry->count++;
326         return (entry != NULL);
327     }
328     else return 1;
329 }
330
331 /* decrement the ref count of a global atom; used for window properties */
332 void release_global_atom( atom_t atom )
333 {
334     if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom, 1 );
335 }
336
337 /* add a global atom */
338 DECL_HANDLER(add_atom)
339 {
340     struct atom_table *table = get_table( req->table );
341     if (table)
342     {
343         reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
344         release_object( table );
345     }
346 }
347
348 /* delete a global atom */
349 DECL_HANDLER(delete_atom)
350 {
351     struct atom_table *table = get_table( req->table );
352     if (table)
353     {
354         delete_atom( table, req->atom, 0 );
355         release_object( table );
356     }
357 }
358
359 /* find a global atom */
360 DECL_HANDLER(find_atom)
361 {
362     struct atom_table *table = get_table( req->table );
363     if (table)
364     {
365         reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
366         release_object( table );
367     }
368 }
369
370 /* get global atom name */
371 DECL_HANDLER(get_atom_information)
372 {
373     struct atom_table *table = get_table( req->table );
374     if (table)
375     {
376         struct atom_entry *entry;
377
378         if ((entry = get_atom_entry( table, req->atom )))
379         {
380             size_t len = entry->len * sizeof(WCHAR);
381             if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
382             else if (get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
383             reply->count = entry->count;
384             reply->pinned = entry->pinned;
385         }
386         else reply->count = -1;
387         release_object( table );
388     }
389 }
390
391 /* set global atom name */
392 DECL_HANDLER(set_atom_information)
393 {
394     struct atom_table *table = get_table( req->table );
395     if (table)
396     {
397         struct atom_entry *entry;
398
399         if ((entry = get_atom_entry( table, req->atom )))
400         {
401             if (req->pinned) entry->pinned = 1;
402         }
403         release_object( table );
404     }
405 }
406
407 /* init a (local) atom table */
408 DECL_HANDLER(init_atom_table)
409 {
410     struct atom_table* table = create_table( req->entries );
411
412     if (table)
413     {
414         reply->table = alloc_handle( current->process, table, 0, FALSE);
415         release_object( table );
416     }
417 }
418
419 /* set global atom name */
420 DECL_HANDLER(empty_atom_table)
421 {
422     struct atom_table *table = get_table( req->table );
423     if (table)
424     {
425         int i;
426         struct atom_entry *entry;
427
428         for (i = 0; i <= table->last; i++)
429         {
430             entry = table->handles[i];
431             if (entry && (!entry->pinned || req->if_pinned))
432             {
433                 if (entry->next) entry->next->prev = entry->prev;
434                 if (entry->prev) entry->prev->next = entry->next;
435                 else table->entries[entry->hash] = entry->next;
436                 table->handles[i] = NULL;
437                 free( entry );
438             }
439         }
440         release_object( table );
441     }
442 }