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