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