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