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 from the request to a temporary area */
69 static const WCHAR *copy_request_name(void)
71 static WCHAR buffer[MAX_ATOM_LEN+1];
73 const WCHAR *str = get_req_data();
74 size_t len = get_req_data_size();
76 if (len > MAX_ATOM_LEN*sizeof(WCHAR))
78 set_error( STATUS_INVALID_PARAMETER );
81 memcpy( buffer, str, len );
82 buffer[len / sizeof(WCHAR)] = 0;
86 /* create an atom table */
87 static struct atom_table *create_table(int entries_count)
89 struct atom_table *table;
91 if ((table = alloc_object( &atom_table_ops, -1 )))
93 if ((entries_count < MIN_HASH_SIZE) ||
94 (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
95 table->entries_count = entries_count;
96 if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
98 set_error( STATUS_NO_MEMORY );
101 memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
104 if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
107 release_object( table );
113 /* retrieve an entry pointer from its atom */
114 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
116 struct atom_entry *entry = NULL;
117 if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
118 entry = table->handles[atom - MIN_STR_ATOM];
119 if (!entry) set_error( STATUS_INVALID_HANDLE );
123 /* add an atom entry in the table and return its handle */
124 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
127 for (i = 0; i <= table->last; i++)
128 if (!table->handles[i]) goto found;
129 if (i == table->count)
131 struct atom_entry **new_table = NULL;
132 int new_size = table->count + table->count / 2;
133 if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
134 if (new_size > table->count)
135 new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
138 set_error( STATUS_NO_MEMORY );
141 table->count = new_size;
142 table->handles = new_table;
146 table->handles[i] = entry;
147 entry->atom = i + MIN_STR_ATOM;
151 /* compute the hash code for a string */
152 static int atom_hash( struct atom_table *table, const WCHAR *str )
156 for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
157 return hash % table->entries_count;
160 /* dump an atom table */
161 static void atom_table_dump( struct object *obj, int verbose )
164 struct atom_table *table = (struct atom_table *)obj;
165 assert( obj->ops == &atom_table_ops );
167 fprintf( stderr, "Atom table size=%d entries=%d\n",
168 table->last + 1, table->entries_count );
169 if (!verbose) return;
170 for (i = 0; i <= table->last; i++)
172 struct atom_entry *entry = table->handles[i];
173 if (!entry) continue;
174 fprintf( stderr, " %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
175 dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
176 fprintf( stderr, "\"\n" );
180 /* destroy the atom table */
181 static void atom_table_destroy( struct object *obj )
184 struct atom_table *table = (struct atom_table *)obj;
185 assert( obj->ops == &atom_table_ops );
188 for (i = 0; i <= table->last; i++) free( table->handles[i] );
189 free( table->handles );
191 if (table->entries) free( table->entries );
194 /* find an atom entry in its hash list */
195 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
197 struct atom_entry *entry = table->entries[hash];
200 if (!strcmpiW( entry->str, str )) break;
206 /* close the atom table; used on server exit */
207 void close_atom_table(void)
209 if (global_table) release_object( global_table );
212 /* add an atom to the table */
213 static atom_t add_atom( struct atom_table *table, const WCHAR *str )
215 struct atom_entry *entry;
216 int hash = atom_hash( table, str );
221 set_error( STATUS_OBJECT_NAME_INVALID );
224 if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
230 if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
232 if ((atom = add_atom_entry( table, entry )))
235 if ((entry->next = table->entries[hash])) entry->next->prev = entry;
236 table->entries[hash] = entry;
239 strcpyW( entry->str, str );
243 else set_error( STATUS_NO_MEMORY );
247 /* delete an atom from the table */
248 static void delete_atom( struct atom_table *table, atom_t atom )
250 struct atom_entry *entry = get_atom_entry( table, atom );
251 if (entry && !--entry->count)
253 if (entry->next) entry->next->prev = entry->prev;
254 if (entry->prev) entry->prev->next = entry->next;
255 else table->entries[entry->hash] = entry->next;
256 table->handles[atom - MIN_STR_ATOM] = NULL;
261 /* find an atom in the table */
262 static atom_t find_atom( struct atom_table *table, const WCHAR *str )
264 struct atom_entry *entry;
266 if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
268 if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
269 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
273 /* increment the ref count of a global atom; used for window properties */
274 int grab_global_atom( atom_t atom )
276 struct atom_entry *entry = get_atom_entry( global_table, atom );
277 if (entry) entry->count++;
278 return (entry != NULL);
281 /* decrement the ref count of a global atom; used for window properties */
282 void release_global_atom( atom_t atom )
284 delete_atom( global_table, atom );
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_request_name();
296 if (name) reply->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_request_name();
311 reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
314 /* get global atom name */
315 DECL_HANDLER(get_atom_name)
317 struct atom_entry *entry;
321 if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
324 reply->count = entry->count;
325 len = strlenW( entry->str ) * sizeof(WCHAR);
326 if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
327 else set_error( STATUS_BUFFER_OVERFLOW );
331 /* init the process atom table */
332 DECL_HANDLER(init_atom_table)
334 if (!current->process->atom_table)
335 current->process->atom_table = create_table( req->entries );