2 * Server-side atom management
4 * Copyright (C) 1999, 2000 Alexandre Julliard
5 * Copyright (C) 2000 Turchanov Sergei
18 #define MIN_HASH_SIZE 4
19 #define MAX_HASH_SIZE 0x200
21 #define MAX_ATOM_LEN 255
22 #define MAX_ATOMS 0x4000
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 */
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 */
44 static void atom_table_dump( struct object *obj, int verbose );
45 static void atom_table_destroy( struct object *obj );
47 static const struct object_ops atom_table_ops =
49 sizeof(struct atom_table), /* size */
50 atom_table_dump, /* dump */
51 no_add_queue, /* add_queue */
52 NULL, /* remove_queue */
55 NULL, /* get_poll_events */
56 NULL, /* poll_event */
57 no_get_fd, /* get_fd */
59 no_get_file_info, /* get_file_info */
60 atom_table_destroy /* destroy */
63 static struct atom_table *global_table;
66 /* copy an atom name to a temporary area */
67 static const WCHAR *copy_name( const WCHAR *str, size_t len )
69 static WCHAR buffer[MAX_ATOM_LEN+1];
71 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
73 set_error( STATUS_INVALID_PARAMETER );
76 memcpy( buffer, str, len );
77 buffer[len / sizeof(WCHAR)] = 0;
81 /* create an atom table */
82 static struct atom_table *create_table(int entries_count)
84 struct atom_table *table;
86 if ((table = alloc_object( &atom_table_ops, -1 )))
88 if ((entries_count < MIN_HASH_SIZE) ||
89 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
90 table->entries_count = entries_count;
91 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
93 set_error( STATUS_NO_MEMORY );
96 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
99 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
102 release_object( table );
108 /* retrieve an entry pointer from its atom */
109 static struct atom_entry *get_atom_entry( struct atom_table *table, int atom )
111 struct atom_entry *entry = NULL;
112 if (table && (atom >= 0) && (atom <= table->last)) entry = table->handles[atom];
113 if (!entry) set_error( STATUS_INVALID_HANDLE );
117 /* add an atom entry in the table and return its handle */
118 static int add_atom_entry( struct atom_table *table, struct atom_entry *entry )
121 for (i = 0; i <= table->last; i++)
122 if (!table->handles[i]) goto found;
123 if (i == table->count)
125 struct atom_entry **new_table = NULL;
126 int new_size = table->count + table->count / 2;
127 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
128 if (new_size > table->count)
129 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
132 set_error( STATUS_NO_MEMORY );
135 table->count = new_size;
136 table->handles = new_table;
140 table->handles[i] = entry;
145 /* compute the hash code for a string */
146 static int atom_hash( struct atom_table *table, const WCHAR *str )
150 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
151 return hash % table->entries_count;
154 /* dump an atom table */
155 static void atom_table_dump( struct object *obj, int verbose )
158 struct atom_table *table = (struct atom_table *)obj;
159 assert( obj->ops == &atom_table_ops );
161 fprintf( stderr, "Atom table size=%d entries=%d\n",
162 table->last + 1, table->entries_count );
163 if (!verbose) return;
164 for (i = 0; i <= table->last; i++)
166 struct atom_entry *entry = table->handles[i];
167 if (!entry) continue;
168 fprintf( stderr, " %5d: ref=%d hash=%d \"", i, entry->count, entry->hash );
169 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
170 fprintf( stderr, "\"\n" );
174 /* destroy the atom table */
175 static void atom_table_destroy( struct object *obj )
178 struct atom_table *table = (struct atom_table *)obj;
179 assert( obj->ops == &atom_table_ops );
182 for (i = 0; i <= table->last; i++) free( table->handles[i] );
183 free( table->handles );
185 if (table->entries) free( table->entries );
188 /* find an atom entry in its hash list */
189 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
191 struct atom_entry *entry = table->entries[hash];
194 if (!strcmpiW( entry->str, str )) break;
200 /* close the atom table; used on server exit */
201 void close_atom_table(void)
203 if (global_table) release_object( global_table );
206 /* add an atom to the table */
207 static int add_atom( struct atom_table *table, const WCHAR *str )
209 struct atom_entry *entry;
210 int hash = atom_hash( table, str );
215 set_error( STATUS_OBJECT_NAME_INVALID );
218 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
224 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
226 if ((atom = add_atom_entry( table, entry )) != -1)
229 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
230 table->entries[hash] = entry;
233 strcpyW( entry->str, str );
237 else set_error( STATUS_NO_MEMORY );
241 /* delete an atom from the table */
242 static void delete_atom( struct atom_table *table, int atom )
244 struct atom_entry *entry = get_atom_entry( table, atom );
245 if (entry && !--entry->count)
247 if (entry->next) entry->next->prev = entry->prev;
248 if (entry->prev) entry->prev->next = entry->next;
249 else table->entries[entry->hash] = entry->next;
250 table->handles[atom] = NULL;
255 /* find an atom in the table */
256 static int find_atom( struct atom_table *table, const WCHAR *str )
258 struct atom_entry *entry;
260 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) )))) return entry->atom;
261 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
262 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
266 /* get an atom name and refcount*/
267 static size_t get_atom_name( struct atom_table *table, int atom,
268 WCHAR *str, size_t maxsize, int *count )
271 struct atom_entry *entry = get_atom_entry( table, atom );
275 *count = entry->count;
276 len = strlenW( entry->str ) * sizeof(WCHAR);
277 if (len <= maxsize) memcpy( str, entry->str, len );
280 set_error( STATUS_BUFFER_OVERFLOW );
287 /* add a global atom */
288 DECL_HANDLER(add_atom)
290 struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
292 if (!*table_ptr) *table_ptr = create_table(0);
295 const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
296 if (name) req->atom = add_atom( *table_ptr, name );
300 /* delete a global atom */
301 DECL_HANDLER(delete_atom)
303 delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
306 /* find a global atom */
307 DECL_HANDLER(find_atom)
309 const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
311 req->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
314 /* get global atom name */
315 DECL_HANDLER(get_atom_name)
317 WCHAR *name = get_req_data(req);
318 size_t size = get_atom_name( req->local ? current->process->atom_table : global_table,
319 req->atom, name, get_req_data_size(req), &req->count );
320 set_req_data_size( req, size );
323 /* init the process atom table */
324 DECL_HANDLER(init_atom_table)
326 if (!current->process->atom_table)
327 current->process->atom_table = create_table( req->entries );