2 * Server-side atom management
4 * Copyright (C) 1999, 2000 Alexandre Julliard
5 * Copyright (C) 2000 Turchanov Sergei
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.
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.
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
23 #include "wine/port.h"
36 #define MIN_HASH_SIZE 4
37 #define MAX_HASH_SIZE 0x200
39 #define MAX_ATOM_LEN 255
40 #define MIN_STR_ATOM 0xc000
41 #define MAX_ATOMS 0x4000
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 */
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 */
63 static void atom_table_dump( struct object *obj, int verbose );
64 static void atom_table_destroy( struct object *obj );
66 static const struct object_ops atom_table_ops =
68 sizeof(struct atom_table), /* size */
69 atom_table_dump, /* dump */
70 no_add_queue, /* add_queue */
71 NULL, /* remove_queue */
74 NULL, /* get_poll_events */
75 NULL, /* poll_event */
76 no_get_fd, /* get_fd */
78 no_get_file_info, /* get_file_info */
79 NULL, /* queue_async */
80 atom_table_destroy /* destroy */
83 static struct atom_table *global_table;
86 /* copy an atom name from the request to a temporary area */
87 static const WCHAR *copy_request_name(void)
89 static WCHAR buffer[MAX_ATOM_LEN+1];
91 const WCHAR *str = get_req_data();
92 size_t len = get_req_data_size();
94 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
96 set_error( STATUS_INVALID_PARAMETER );
99 memcpy( buffer, str, len );
100 buffer[len / sizeof(WCHAR)] = 0;
104 /* create an atom table */
105 static struct atom_table *create_table(int entries_count)
107 struct atom_table *table;
109 if ((table = alloc_object( &atom_table_ops, -1 )))
111 if ((entries_count < MIN_HASH_SIZE) ||
112 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
113 table->entries_count = entries_count;
114 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
116 set_error( STATUS_NO_MEMORY );
119 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
122 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
125 release_object( table );
131 /* retrieve an entry pointer from its atom */
132 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
134 struct atom_entry *entry = NULL;
135 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
136 entry = table->handles[atom - MIN_STR_ATOM];
137 if (!entry) set_error( STATUS_INVALID_HANDLE );
141 /* add an atom entry in the table and return its handle */
142 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
145 for (i = 0; i <= table->last; i++)
146 if (!table->handles[i]) goto found;
147 if (i == table->count)
149 struct atom_entry **new_table = NULL;
150 int new_size = table->count + table->count / 2;
151 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
152 if (new_size > table->count)
153 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
156 set_error( STATUS_NO_MEMORY );
159 table->count = new_size;
160 table->handles = new_table;
164 table->handles[i] = entry;
165 entry->atom = i + MIN_STR_ATOM;
169 /* compute the hash code for a string */
170 static int atom_hash( struct atom_table *table, const WCHAR *str )
174 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
175 return hash % table->entries_count;
178 /* dump an atom table */
179 static void atom_table_dump( struct object *obj, int verbose )
182 struct atom_table *table = (struct atom_table *)obj;
183 assert( obj->ops == &atom_table_ops );
185 fprintf( stderr, "Atom table size=%d entries=%d\n",
186 table->last + 1, table->entries_count );
187 if (!verbose) return;
188 for (i = 0; i <= table->last; i++)
190 struct atom_entry *entry = table->handles[i];
191 if (!entry) continue;
192 fprintf( stderr, " %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
193 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
194 fprintf( stderr, "\"\n" );
198 /* destroy the atom table */
199 static void atom_table_destroy( struct object *obj )
202 struct atom_table *table = (struct atom_table *)obj;
203 assert( obj->ops == &atom_table_ops );
206 for (i = 0; i <= table->last; i++) free( table->handles[i] );
207 free( table->handles );
209 if (table->entries) free( table->entries );
212 /* find an atom entry in its hash list */
213 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
215 struct atom_entry *entry = table->entries[hash];
218 if (!strcmpiW( entry->str, str )) break;
224 /* close the atom table; used on server exit */
225 void close_atom_table(void)
227 if (global_table) release_object( global_table );
230 /* add an atom to the table */
231 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
233 struct atom_entry *entry;
234 int hash = atom_hash( table, str );
239 set_error( STATUS_OBJECT_NAME_INVALID );
242 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
248 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
250 if ((atom = add_atom_entry( table, entry )))
253 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
254 table->entries[hash] = entry;
257 strcpyW( entry->str, str );
261 else set_error( STATUS_NO_MEMORY );
265 /* delete an atom from the table */
266 static void delete_atom( struct atom_table *table, atom_t atom )
268 struct atom_entry *entry = get_atom_entry( table, atom );
269 if (entry && !--entry->count)
271 if (entry->next) entry->next->prev = entry->prev;
272 if (entry->prev) entry->prev->next = entry->next;
273 else table->entries[entry->hash] = entry->next;
274 table->handles[atom - MIN_STR_ATOM] = NULL;
279 /* find an atom in the table */
280 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
282 struct atom_entry *entry;
284 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
286 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
287 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
291 /* increment the ref count of a global atom; used for window properties */
292 int grab_global_atom( atom_t atom )
294 if (atom >= MIN_STR_ATOM)
296 struct atom_entry *entry = get_atom_entry( global_table, atom );
297 if (entry) entry->count++;
298 return (entry != NULL);
303 /* decrement the ref count of a global atom; used for window properties */
304 void release_global_atom( atom_t atom )
306 if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom );
309 /* add a global atom */
310 DECL_HANDLER(add_atom)
312 struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
314 if (!*table_ptr) *table_ptr = create_table(0);
317 const WCHAR *name = copy_request_name();
318 if (name) reply->atom = add_atom( *table_ptr, name );
322 /* delete a global atom */
323 DECL_HANDLER(delete_atom)
325 delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
328 /* find a global atom */
329 DECL_HANDLER(find_atom)
331 const WCHAR *name = copy_request_name();
333 reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
336 /* get global atom name */
337 DECL_HANDLER(get_atom_name)
339 struct atom_entry *entry;
343 if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
346 reply->count = entry->count;
347 len = strlenW( entry->str ) * sizeof(WCHAR);
348 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
349 else set_error( STATUS_BUFFER_OVERFLOW );
353 /* init the process atom table */
354 DECL_HANDLER(init_atom_table)
356 if (!current->process->atom_table)
357 current->process->atom_table = create_table( req->entries );