2 * Server-side atom management
4 * Copyright (C) 1999, 2000 Alexandre Julliard
5 * Copyright (C) 2000 Turchanov Sergei
19 #define MIN_HASH_SIZE 4
20 #define MAX_HASH_SIZE 0x200
22 #define MAX_ATOM_LEN 255
23 #define MIN_STR_ATOM 0xc000
24 #define MAX_ATOMS 0x4000
28 struct atom_entry *next; /* hash table list */
29 struct atom_entry *prev; /* hash table list */
30 int count; /* reference count */
31 int hash; /* string hash */
32 atom_t atom; /* atom handle */
33 WCHAR str[1]; /* atom string */
38 struct object obj; /* object header */
39 int count; /* count of atom handles */
40 int last; /* last handle in-use */
41 struct atom_entry **handles; /* atom handles */
42 int entries_count; /* humber of hash entries */
43 struct atom_entry **entries; /* hash table entries */
46 static void atom_table_dump( struct object *obj, int verbose );
47 static void atom_table_destroy( struct object *obj );
49 static const struct object_ops atom_table_ops =
51 sizeof(struct atom_table), /* size */
52 atom_table_dump, /* dump */
53 no_add_queue, /* add_queue */
54 NULL, /* remove_queue */
57 NULL, /* get_poll_events */
58 NULL, /* poll_event */
59 no_get_fd, /* get_fd */
61 no_get_file_info, /* get_file_info */
62 atom_table_destroy /* destroy */
65 static struct atom_table *global_table;
68 /* copy an atom name to a temporary area */
69 static const WCHAR *copy_name( const WCHAR *str, size_t len )
71 static WCHAR buffer[MAX_ATOM_LEN+1];
73 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
75 set_error( STATUS_INVALID_PARAMETER );
78 memcpy( buffer, str, len );
79 buffer[len / sizeof(WCHAR)] = 0;
83 /* create an atom table */
84 static struct atom_table *create_table(int entries_count)
86 struct atom_table *table;
88 if ((table = alloc_object( &atom_table_ops, -1 )))
90 if ((entries_count < MIN_HASH_SIZE) ||
91 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
92 table->entries_count = entries_count;
93 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
95 set_error( STATUS_NO_MEMORY );
98 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
101 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
104 release_object( table );
110 /* retrieve an entry pointer from its atom */
111 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
113 struct atom_entry *entry = NULL;
114 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
115 entry = table->handles[atom - MIN_STR_ATOM];
116 if (!entry) set_error( STATUS_INVALID_HANDLE );
120 /* add an atom entry in the table and return its handle */
121 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
124 for (i = 0; i <= table->last; i++)
125 if (!table->handles[i]) goto found;
126 if (i == table->count)
128 struct atom_entry **new_table = NULL;
129 int new_size = table->count + table->count / 2;
130 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
131 if (new_size > table->count)
132 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
135 set_error( STATUS_NO_MEMORY );
138 table->count = new_size;
139 table->handles = new_table;
143 table->handles[i] = entry;
144 entry->atom = i + MIN_STR_ATOM;
148 /* compute the hash code for a string */
149 static int atom_hash( struct atom_table *table, const WCHAR *str )
153 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
154 return hash % table->entries_count;
157 /* dump an atom table */
158 static void atom_table_dump( struct object *obj, int verbose )
161 struct atom_table *table = (struct atom_table *)obj;
162 assert( obj->ops == &atom_table_ops );
164 fprintf( stderr, "Atom table size=%d entries=%d\n",
165 table->last + 1, table->entries_count );
166 if (!verbose) return;
167 for (i = 0; i <= table->last; i++)
169 struct atom_entry *entry = table->handles[i];
170 if (!entry) continue;
171 fprintf( stderr, " %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
172 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
173 fprintf( stderr, "\"\n" );
177 /* destroy the atom table */
178 static void atom_table_destroy( struct object *obj )
181 struct atom_table *table = (struct atom_table *)obj;
182 assert( obj->ops == &atom_table_ops );
185 for (i = 0; i <= table->last; i++) free( table->handles[i] );
186 free( table->handles );
188 if (table->entries) free( table->entries );
191 /* find an atom entry in its hash list */
192 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
194 struct atom_entry *entry = table->entries[hash];
197 if (!strcmpiW( entry->str, str )) break;
203 /* close the atom table; used on server exit */
204 void close_atom_table(void)
206 if (global_table) release_object( global_table );
209 /* add an atom to the table */
210 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
212 struct atom_entry *entry;
213 int hash = atom_hash( table, str );
218 set_error( STATUS_OBJECT_NAME_INVALID );
221 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
227 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
229 if ((atom = add_atom_entry( table, entry )))
232 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
233 table->entries[hash] = entry;
236 strcpyW( entry->str, str );
240 else set_error( STATUS_NO_MEMORY );
244 /* delete an atom from the table */
245 static void delete_atom( struct atom_table *table, atom_t atom )
247 struct atom_entry *entry = get_atom_entry( table, atom );
248 if (entry && !--entry->count)
250 if (entry->next) entry->next->prev = entry->prev;
251 if (entry->prev) entry->prev->next = entry->next;
252 else table->entries[entry->hash] = entry->next;
253 table->handles[atom - MIN_STR_ATOM] = NULL;
258 /* find an atom in the table */
259 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
261 struct atom_entry *entry;
263 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
265 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
266 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
270 /* get an atom name and refcount*/
271 static size_t get_atom_name( struct atom_table *table, atom_t atom,
272 WCHAR *str, size_t maxsize, int *count )
275 struct atom_entry *entry = get_atom_entry( table, atom );
279 *count = entry->count;
280 len = strlenW( entry->str ) * sizeof(WCHAR);
281 if (len <= maxsize) memcpy( str, entry->str, len );
284 set_error( STATUS_BUFFER_OVERFLOW );
291 /* increment the ref count of a global atom; used for window properties */
292 int grab_global_atom( atom_t atom )
294 struct atom_entry *entry = get_atom_entry( global_table, atom );
295 if (entry) entry->count++;
296 return (entry != NULL);
299 /* decrement the ref count of a global atom; used for window properties */
300 void release_global_atom( atom_t atom )
302 delete_atom( global_table, atom );
305 /* add a global atom */
306 DECL_HANDLER(add_atom)
308 struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
310 if (!*table_ptr) *table_ptr = create_table(0);
313 const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
314 if (name) req->atom = add_atom( *table_ptr, name );
318 /* delete a global atom */
319 DECL_HANDLER(delete_atom)
321 delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
324 /* find a global atom */
325 DECL_HANDLER(find_atom)
327 const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
329 req->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
332 /* get global atom name */
333 DECL_HANDLER(get_atom_name)
335 WCHAR *name = get_req_data(req);
336 size_t size = get_atom_name( req->local ? current->process->atom_table : global_table,
337 req->atom, name, get_req_data_size(req), &req->count );
338 set_req_data_size( req, size );
341 /* init the process atom table */
342 DECL_HANDLER(init_atom_table)
344 if (!current->process->atom_table)
345 current->process->atom_table = create_table( req->entries );