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_read_fd, /* get_read_fd */
58 no_write_fd, /* get_write_fd */
60 no_get_file_info, /* get_file_info */
61 atom_table_destroy /* destroy */
64 static struct atom_table *global_table;
67 /* copy an atom name to a temporary area */
68 static const WCHAR *copy_name( const WCHAR *str, size_t len )
70 static WCHAR buffer[MAX_ATOM_LEN+1];
72 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
74 set_error( STATUS_INVALID_PARAMETER );
77 memcpy( buffer, str, len );
78 buffer[len / sizeof(WCHAR)] = 0;
82 /* create an atom table */
83 static struct atom_table *create_table(int entries_count)
85 struct atom_table *table;
87 if ((table = alloc_object( &atom_table_ops, -1 )))
89 if ((entries_count < MIN_HASH_SIZE) ||
90 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
91 table->entries_count = entries_count;
92 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
94 set_error( STATUS_NO_MEMORY );
97 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
100 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
103 release_object( table );
109 /* retrieve an entry pointer from its atom */
110 static struct atom_entry *get_atom_entry( struct atom_table *table, int atom )
112 struct atom_entry *entry = NULL;
113 if (table && (atom >= 0) && (atom <= table->last)) entry = table->handles[atom];
114 if (!entry) set_error( STATUS_INVALID_HANDLE );
118 /* add an atom entry in the table and return its handle */
119 static int add_atom_entry( struct atom_table *table, struct atom_entry *entry )
122 for (i = 0; i <= table->last; i++)
123 if (!table->handles[i]) goto found;
124 if (i == table->count)
126 struct atom_entry **new_table = NULL;
127 int new_size = table->count + table->count / 2;
128 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
129 if (new_size > table->count)
130 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
133 set_error( STATUS_NO_MEMORY );
136 table->count = new_size;
137 table->handles = new_table;
141 table->handles[i] = entry;
146 /* compute the hash code for a string */
147 static int atom_hash( struct atom_table *table, const WCHAR *str )
151 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
152 return hash % table->entries_count;
155 /* dump an atom table */
156 static void atom_table_dump( struct object *obj, int verbose )
159 struct atom_table *table = (struct atom_table *)obj;
160 assert( obj->ops == &atom_table_ops );
162 fprintf( stderr, "Atom table size=%d entries=%d\n",
163 table->last + 1, table->entries_count );
164 if (!verbose) return;
165 for (i = 0; i <= table->last; i++)
167 struct atom_entry *entry = table->handles[i];
168 if (!entry) continue;
169 fprintf( stderr, " %5d: ref=%d hash=%d \"", i, entry->count, entry->hash );
170 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
171 fprintf( stderr, "\"\n" );
175 /* destroy the atom table */
176 static void atom_table_destroy( struct object *obj )
179 struct atom_table *table = (struct atom_table *)obj;
180 assert( obj->ops == &atom_table_ops );
183 for (i = 0; i <= table->last; i++) free( table->handles[i] );
184 free( table->handles );
186 if (table->entries) free( table->entries );
189 /* find an atom entry in its hash list */
190 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
192 struct atom_entry *entry = table->entries[hash];
195 if (!strcmpiW( entry->str, str )) break;
201 /* close the atom table; used on server exit */
202 void close_atom_table(void)
204 if (global_table) release_object( global_table );
207 /* add an atom to the table */
208 static int add_atom( struct atom_table *table, const WCHAR *str )
210 struct atom_entry *entry;
211 int hash = atom_hash( table, str );
216 set_error( STATUS_OBJECT_NAME_INVALID );
219 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
225 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
227 if ((atom = add_atom_entry( table, entry )) != -1)
230 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
231 table->entries[hash] = entry;
234 strcpyW( entry->str, str );
238 else set_error( STATUS_NO_MEMORY );
242 /* delete an atom from the table */
243 static void delete_atom( struct atom_table *table, int atom )
245 struct atom_entry *entry = get_atom_entry( table, atom );
246 if (entry && !--entry->count)
248 if (entry->next) entry->next->prev = entry->prev;
249 if (entry->prev) entry->prev->next = entry->next;
250 else table->entries[entry->hash] = entry->next;
251 table->handles[atom] = NULL;
256 /* find an atom in the table */
257 static int find_atom( struct atom_table *table, const WCHAR *str )
259 struct atom_entry *entry;
261 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) )))) return entry->atom;
262 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
263 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
267 /* get an atom name and refcount*/
268 static size_t get_atom_name( struct atom_table *table, int atom,
269 WCHAR *str, size_t maxsize, int *count )
272 struct atom_entry *entry = get_atom_entry( table, atom );
276 *count = entry->count;
277 len = strlenW( entry->str ) * sizeof(WCHAR);
278 if (len <= maxsize) memcpy( str, entry->str, len );
281 set_error( STATUS_BUFFER_OVERFLOW );
288 /* add a global atom */
289 DECL_HANDLER(add_atom)
291 struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
293 if (!*table_ptr) *table_ptr = create_table(0);
296 const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
297 if (name) req->atom = add_atom( *table_ptr, name );
301 /* delete a global atom */
302 DECL_HANDLER(delete_atom)
304 delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
307 /* find a global atom */
308 DECL_HANDLER(find_atom)
310 const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
312 req->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
315 /* get global atom name */
316 DECL_HANDLER(get_atom_name)
318 WCHAR *name = get_req_data(req);
319 size_t size = get_atom_name( req->local ? current->process->atom_table : global_table,
320 req->atom, name, get_req_data_size(req), &req->count );
321 set_req_data_size( req, size );
324 /* init the process atom table */
325 DECL_HANDLER(init_atom_table)
327 if (!current->process->atom_table)
328 current->process->atom_table = create_table( req->entries );