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