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 )
70 static WCHAR buffer[MAX_ATOM_LEN+1];
73 while (p < buffer + sizeof(buffer) - 1) if (!(*p++ = *str++)) break;
78 /* create an atom table */
79 static struct atom_table *create_table(int entries_count)
81 struct atom_table *table;
83 if ((table = alloc_object( &atom_table_ops, -1 )))
85 if ((entries_count < MIN_HASH_SIZE) ||
86 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
87 table->entries_count = entries_count;
88 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
90 set_error( STATUS_NO_MEMORY );
93 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
96 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
99 release_object( table );
105 /* retrieve an entry pointer from its atom */
106 static struct atom_entry *get_atom_entry( struct atom_table *table, int atom )
108 struct atom_entry *entry = NULL;
109 if (table && (atom >= 0) && (atom <= table->last)) entry = table->handles[atom];
110 if (!entry) set_error( STATUS_INVALID_HANDLE );
114 /* add an atom entry in the table and return its handle */
115 static int add_atom_entry( struct atom_table *table, struct atom_entry *entry )
118 for (i = 0; i <= table->last; i++)
119 if (!table->handles[i]) goto found;
120 if (i == table->count)
122 struct atom_entry **new_table = NULL;
123 int new_size = table->count + table->count / 2;
124 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
125 if (new_size > table->count)
126 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
129 set_error( STATUS_NO_MEMORY );
132 table->count = new_size;
133 table->handles = new_table;
137 table->handles[i] = entry;
142 /* compute the hash code for a string */
143 static int atom_hash( struct atom_table *table, const WCHAR *str )
147 for (i = 0; str[i]; i++) hash ^= towupper(str[i]) + i;
148 return hash % table->entries_count;
151 /* dump an atom table */
152 static void atom_table_dump( struct object *obj, int verbose )
155 struct atom_table *table = (struct atom_table *)obj;
156 assert( obj->ops == &atom_table_ops );
158 fprintf( stderr, "Atom table size=%d entries=%d\n",
159 table->last + 1, table->entries_count );
160 if (!verbose) return;
161 for (i = 0; i <= table->last; i++)
163 struct atom_entry *entry = table->handles[i];
164 if (!entry) continue;
165 fprintf( stderr, " %5d: ref=%d hash=%d \"", i, entry->count, entry->hash );
166 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
167 fprintf( stderr, "\"\n" );
171 /* destroy the atom table */
172 static void atom_table_destroy( struct object *obj )
175 struct atom_table *table = (struct atom_table *)obj;
176 assert( obj->ops == &atom_table_ops );
179 for (i = 0; i <= table->last; i++) free( table->handles[i] );
180 free( table->handles );
182 if (table->entries) free( table->entries );
185 /* find an atom entry in its hash list */
186 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
188 struct atom_entry *entry = table->entries[hash];
191 if (!strcmpiW( entry->str, str )) break;
197 /* close the atom table; used on server exit */
198 void close_atom_table(void)
200 if (global_table) release_object( global_table );
203 /* add an atom to the table */
204 static int add_atom( struct atom_table *table, const WCHAR *str )
206 struct atom_entry *entry;
207 int hash = atom_hash( table, str );
212 set_error( STATUS_OBJECT_NAME_INVALID );
215 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
221 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
223 if ((atom = add_atom_entry( table, entry )) != -1)
226 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
227 table->entries[hash] = entry;
230 strcpyW( entry->str, str );
234 else set_error( STATUS_NO_MEMORY );
238 /* delete an atom from the table */
239 static void delete_atom( struct atom_table *table, int atom )
241 struct atom_entry *entry = get_atom_entry( table, atom );
242 if (entry && !--entry->count)
244 if (entry->next) entry->next->prev = entry->prev;
245 if (entry->prev) entry->prev->next = entry->next;
246 else table->entries[entry->hash] = entry->next;
247 table->handles[atom] = NULL;
252 /* find an atom in the table */
253 static int find_atom( struct atom_table *table, const WCHAR *str )
255 struct atom_entry *entry;
257 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) )))) return entry->atom;
258 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
259 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
263 /* get an atom name and refcount*/
264 static int get_atom_name( struct atom_table *table, int atom, WCHAR *str )
267 struct atom_entry *entry = get_atom_entry( table, atom );
270 strcpyW( str, entry->str );
271 count = entry->count;
276 /* add a global atom */
277 DECL_HANDLER(add_atom)
279 struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
281 if (!*table_ptr) *table_ptr = create_table(0);
282 if (*table_ptr) req->atom = add_atom( *table_ptr, copy_name( req->name ) );
285 /* delete a global atom */
286 DECL_HANDLER(delete_atom)
288 delete_atom( req->local ? current->process->atom_table : global_table,
292 /* find a global atom */
293 DECL_HANDLER(find_atom)
295 req->atom = find_atom( req->local ? current->process->atom_table : global_table,
296 copy_name( req->name ) );
299 /* get global atom name */
300 DECL_HANDLER(get_atom_name)
303 req->count = get_atom_name( req->local ? current->process->atom_table : global_table,
304 req->atom, req->name );
307 /* init the process atom table */
308 DECL_HANDLER(init_atom_table)
310 if (!current->process->atom_table)
311 current->process->atom_table = create_table( req->entries );