server: Make alloc_handle use attributes instead of inherit flag.
[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 "ntstatus.h"
31 #define WIN32_NO_STATUS
32
33 #include "unicode.h"
34 #include "request.h"
35 #include "object.h"
36 #include "process.h"
37 #include "handle.h"
38 #include "user.h"
39 #include "winuser.h"
40 #include "winternl.h"
41
42 #define HASH_SIZE     37
43 #define MIN_HASH_SIZE 4
44 #define MAX_HASH_SIZE 0x200
45
46 #define MAX_ATOM_LEN  255
47 #define MIN_STR_ATOM  0xc000
48 #define MAX_ATOMS     0x4000
49
50 struct atom_entry
51 {
52     struct atom_entry *next;   /* hash table list */
53     struct atom_entry *prev;   /* hash table list */
54     int                count;  /* reference count */
55     short              pinned; /* whether the atom is pinned or not */
56     atom_t             atom;   /* atom handle */
57     unsigned short     hash;   /* string hash */
58     unsigned short     len;    /* string len */
59     WCHAR              str[1]; /* atom string */
60 };
61
62 struct atom_table
63 {
64     struct object       obj;                 /* object header */
65     int                 count;               /* count of atom handles */
66     int                 last;                /* last handle in-use */
67     struct atom_entry **handles;             /* atom handles */
68     int                 entries_count;       /* humber of hash entries */
69     struct atom_entry **entries;             /* hash table entries */
70 };
71
72 static void atom_table_dump( struct object *obj, int verbose );
73 static void atom_table_destroy( struct object *obj );
74
75 static const struct object_ops atom_table_ops =
76 {
77     sizeof(struct atom_table),    /* size */
78     atom_table_dump,              /* dump */
79     no_add_queue,                 /* add_queue */
80     NULL,                         /* remove_queue */
81     NULL,                         /* signaled */
82     NULL,                         /* satisfied */
83     no_signal,                    /* signal */
84     no_get_fd,                    /* get_fd */
85     no_lookup_name,               /* lookup_name */
86     no_close_handle,              /* close_handle */
87     atom_table_destroy            /* destroy */
88 };
89
90
91 /* create an atom table */
92 static struct atom_table *create_table(int entries_count)
93 {
94     struct atom_table *table;
95
96     if ((table = alloc_object( &atom_table_ops )))
97     {
98         if ((entries_count < MIN_HASH_SIZE) ||
99             (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
100         table->entries_count = entries_count;
101         if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
102         {
103             set_error( STATUS_NO_MEMORY );
104             goto fail;
105         }
106         memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
107         table->count = 64;
108         table->last  = -1;
109         if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
110             return table;
111 fail:
112         release_object( table );
113         table = NULL;
114     }
115     return table;
116 }
117
118 /* retrieve an entry pointer from its atom */
119 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
120 {
121     struct atom_entry *entry = NULL;
122     if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
123         entry = table->handles[atom - MIN_STR_ATOM];
124     if (!entry) set_error( STATUS_INVALID_HANDLE );
125     return entry;
126 }
127
128 /* add an atom entry in the table and return its handle */
129 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
130 {
131     int i;
132     for (i = 0; i <= table->last; i++)
133         if (!table->handles[i]) goto found;
134     if (i == table->count)
135     {
136         struct atom_entry **new_table = NULL;
137         int new_size = table->count + table->count / 2;
138         if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
139         if (new_size > table->count)
140             new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
141         if (!new_table)
142         {
143             set_error( STATUS_NO_MEMORY );
144             return 0;
145         }
146         table->count = new_size;
147         table->handles = new_table;
148     }
149     table->last = i;
150  found:
151     table->handles[i] = entry;
152     entry->atom = i + MIN_STR_ATOM;
153     return entry->atom;
154 }
155
156 /* compute the hash code for a string */
157 static unsigned short atom_hash( struct atom_table *table, const WCHAR *str, size_t len )
158 {
159     unsigned int i;
160     unsigned short hash = 0;
161     for (i = 0; i < len; i++) hash ^= toupperW(str[i]) + i;
162     return hash % table->entries_count;
163 }
164
165 /* dump an atom table */
166 static void atom_table_dump( struct object *obj, int verbose )
167 {
168     int i;
169     struct atom_table *table = (struct atom_table *)obj;
170     assert( obj->ops == &atom_table_ops );
171
172     fprintf( stderr, "Atom table size=%d entries=%d\n",
173              table->last + 1, table->entries_count );
174     if (!verbose) return;
175     for (i = 0; i <= table->last; i++)
176     {
177         struct atom_entry *entry = table->handles[i];
178         if (!entry) continue;
179         fprintf( stderr, "  %04x: ref=%d pinned=%c hash=%d \"",
180                  entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
181         dump_strW( entry->str, entry->len, stderr, "\"\"");
182         fprintf( stderr, "\"\n" );
183     }
184 }
185
186 /* destroy the atom table */
187 static void atom_table_destroy( struct object *obj )
188 {
189     int i;
190     struct atom_table *table = (struct atom_table *)obj;
191     assert( obj->ops == &atom_table_ops );
192     if (table->handles)
193     {
194         for (i = 0; i <= table->last; i++) free( table->handles[i] );
195         free( table->handles );
196     }
197     if (table->entries) free( table->entries );
198 }
199
200 /* find an atom entry in its hash list */
201 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
202                                            size_t len, unsigned short hash )
203 {
204     struct atom_entry *entry = table->entries[hash];
205     while (entry)
206     {
207         if (entry->len == len && !memicmpW( entry->str, str, len )) break;
208         entry = entry->next;
209     }
210     return entry;
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_global_table( struct winstation *winstation, int create )
293 {
294     if (!winstation->atom_table)
295     {
296         if (create) winstation->atom_table = create_table( HASH_SIZE );
297         else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
298     }
299     return winstation->atom_table;
300 }
301
302 static struct atom_table *get_table( obj_handle_t h, int create )
303 {
304     struct atom_table *table = NULL;
305
306     if (h)
307     {
308         table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
309     }
310     else
311     {
312         struct winstation *winstation = get_process_winstation( current->process,
313                                                                 WINSTA_ACCESSGLOBALATOMS );
314         if (winstation)
315         {
316             table = get_global_table( winstation, 1 );
317             if (table) grab_object( table );
318             release_object( winstation );
319         }
320     }
321     return table;
322 }
323
324 /* add an atom in the global table; used for window properties */
325 atom_t add_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
326 {
327     struct atom_table *global_table = get_global_table( winstation, 1 );
328     if (!global_table) return 0;
329     return add_atom( global_table, str, len );
330 }
331
332 /* find an atom in the global table; used for window properties */
333 atom_t find_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
334 {
335     struct atom_table *global_table = get_global_table( winstation, 0 );
336     struct atom_entry *entry;
337
338     if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
339     if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
340         return entry->atom;
341     return 0;
342 }
343
344 /* increment the ref count of a global atom; used for window properties */
345 int grab_global_atom( struct winstation *winstation, atom_t atom )
346 {
347     if (atom >= MIN_STR_ATOM)
348     {
349         struct atom_table *global_table = get_global_table( winstation, 0 );
350         if (global_table)
351         {
352             struct atom_entry *entry = get_atom_entry( global_table, atom );
353             if (entry) entry->count++;
354             return (entry != NULL);
355         }
356         else return 0;
357     }
358     else return 1;
359 }
360
361 /* decrement the ref count of a global atom; used for window properties */
362 void release_global_atom( struct winstation *winstation, atom_t atom )
363 {
364     if (atom >= MIN_STR_ATOM)
365     {
366         struct atom_table *global_table = get_global_table( winstation, 0 );
367         if (global_table) delete_atom( global_table, atom, 1 );
368     }
369 }
370
371 /* add a global atom */
372 DECL_HANDLER(add_atom)
373 {
374     struct atom_table *table = get_table( req->table, 1 );
375     if (table)
376     {
377         reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
378         release_object( table );
379     }
380 }
381
382 /* delete a global atom */
383 DECL_HANDLER(delete_atom)
384 {
385     struct atom_table *table = get_table( req->table, 0 );
386     if (table)
387     {
388         delete_atom( table, req->atom, 0 );
389         release_object( table );
390     }
391 }
392
393 /* find a global atom */
394 DECL_HANDLER(find_atom)
395 {
396     struct atom_table *table = get_table( req->table, 0 );
397     if (table)
398     {
399         reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
400         release_object( table );
401     }
402 }
403
404 /* get global atom name */
405 DECL_HANDLER(get_atom_information)
406 {
407     struct atom_table *table = get_table( req->table, 0 );
408     if (table)
409     {
410         struct atom_entry *entry;
411
412         if ((entry = get_atom_entry( table, req->atom )))
413         {
414             size_t len = entry->len * sizeof(WCHAR);
415             if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
416             else if (get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
417             reply->count = entry->count;
418             reply->pinned = entry->pinned;
419         }
420         else reply->count = -1;
421         release_object( table );
422     }
423 }
424
425 /* set global atom name */
426 DECL_HANDLER(set_atom_information)
427 {
428     struct atom_table *table = get_table( req->table, 0 );
429     if (table)
430     {
431         struct atom_entry *entry;
432
433         if ((entry = get_atom_entry( table, req->atom )))
434         {
435             if (req->pinned) entry->pinned = 1;
436         }
437         release_object( table );
438     }
439 }
440
441 /* init a (local) atom table */
442 DECL_HANDLER(init_atom_table)
443 {
444     struct atom_table* table = create_table( req->entries );
445
446     if (table)
447     {
448         reply->table = alloc_handle( current->process, table, 0, 0 );
449         release_object( table );
450     }
451 }
452
453 /* set global atom name */
454 DECL_HANDLER(empty_atom_table)
455 {
456     struct atom_table *table = get_table( req->table, 1 );
457     if (table)
458     {
459         int i;
460         struct atom_entry *entry;
461
462         for (i = 0; i <= table->last; i++)
463         {
464             entry = table->handles[i];
465             if (entry && (!entry->pinned || req->if_pinned))
466             {
467                 if (entry->next) entry->next->prev = entry->prev;
468                 if (entry->prev) entry->prev->next = entry->next;
469                 else table->entries[entry->hash] = entry->next;
470                 table->handles[i] = NULL;
471                 free( entry );
472             }
473         }
474         release_object( table );
475     }
476 }