2000-02-13 14:56:13 +01:00
|
|
|
/*
|
|
|
|
* Server-side atom management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999, 2000 Alexandre Julliard
|
|
|
|
* Copyright (C) 2000 Turchanov Sergei
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2001-03-21 21:30:46 +01:00
|
|
|
#include <string.h>
|
2000-02-13 14:56:13 +01:00
|
|
|
|
|
|
|
#include "unicode.h"
|
|
|
|
#include "request.h"
|
|
|
|
#include "object.h"
|
2000-05-30 22:32:06 +02:00
|
|
|
#include "process.h"
|
2000-02-13 14:56:13 +01:00
|
|
|
|
|
|
|
#define HASH_SIZE 37
|
2000-05-30 22:32:06 +02:00
|
|
|
#define MIN_HASH_SIZE 4
|
|
|
|
#define MAX_HASH_SIZE 0x200
|
|
|
|
|
2000-02-13 14:56:13 +01:00
|
|
|
#define MAX_ATOM_LEN 255
|
2001-10-12 20:45:29 +02:00
|
|
|
#define MIN_STR_ATOM 0xc000
|
2000-02-13 14:56:13 +01:00
|
|
|
#define MAX_ATOMS 0x4000
|
|
|
|
|
|
|
|
struct atom_entry
|
|
|
|
{
|
|
|
|
struct atom_entry *next; /* hash table list */
|
|
|
|
struct atom_entry *prev; /* hash table list */
|
|
|
|
int count; /* reference count */
|
|
|
|
int hash; /* string hash */
|
2001-10-12 20:45:29 +02:00
|
|
|
atom_t atom; /* atom handle */
|
2000-02-13 14:56:13 +01:00
|
|
|
WCHAR str[1]; /* atom string */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct atom_table
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
int count; /* count of atom handles */
|
|
|
|
int last; /* last handle in-use */
|
|
|
|
struct atom_entry **handles; /* atom handles */
|
2000-05-30 22:32:06 +02:00
|
|
|
int entries_count; /* humber of hash entries */
|
|
|
|
struct atom_entry **entries; /* hash table entries */
|
2000-02-13 14:56:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void atom_table_dump( struct object *obj, int verbose );
|
|
|
|
static void atom_table_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops atom_table_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct atom_table), /* size */
|
|
|
|
atom_table_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satified */
|
|
|
|
NULL, /* get_poll_events */
|
|
|
|
NULL, /* poll_event */
|
2000-12-19 03:12:45 +01:00
|
|
|
no_get_fd, /* get_fd */
|
2000-02-13 14:56:13 +01:00
|
|
|
no_flush, /* flush */
|
|
|
|
no_get_file_info, /* get_file_info */
|
|
|
|
atom_table_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct atom_table *global_table;
|
|
|
|
|
|
|
|
|
|
|
|
/* copy an atom name to a temporary area */
|
2000-08-30 02:00:48 +02:00
|
|
|
static const WCHAR *copy_name( const WCHAR *str, size_t len )
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
static WCHAR buffer[MAX_ATOM_LEN+1];
|
|
|
|
|
2000-08-30 02:00:48 +02:00
|
|
|
if (len > MAX_ATOM_LEN*sizeof(WCHAR))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy( buffer, str, len );
|
|
|
|
buffer[len / sizeof(WCHAR)] = 0;
|
2000-02-13 14:56:13 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create an atom table */
|
2000-05-30 22:32:06 +02:00
|
|
|
static struct atom_table *create_table(int entries_count)
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
struct atom_table *table;
|
|
|
|
|
|
|
|
if ((table = alloc_object( &atom_table_ops, -1 )))
|
|
|
|
{
|
2000-05-30 22:32:06 +02:00
|
|
|
if ((entries_count < MIN_HASH_SIZE) ||
|
|
|
|
(entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
|
|
|
|
table->entries_count = entries_count;
|
|
|
|
if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
|
|
|
|
{
|
|
|
|
set_error( STATUS_NO_MEMORY );
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
|
2000-02-13 14:56:13 +01:00
|
|
|
table->count = 64;
|
|
|
|
table->last = -1;
|
|
|
|
if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
|
|
|
|
return table;
|
2000-05-30 22:32:06 +02:00
|
|
|
fail:
|
2000-02-13 14:56:13 +01:00
|
|
|
release_object( table );
|
|
|
|
table = NULL;
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* retrieve an entry pointer from its atom */
|
2001-10-12 20:45:29 +02:00
|
|
|
static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
struct atom_entry *entry = NULL;
|
2001-10-12 20:45:29 +02:00
|
|
|
if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
|
|
|
|
entry = table->handles[atom - MIN_STR_ATOM];
|
2000-02-13 14:56:13 +01:00
|
|
|
if (!entry) set_error( STATUS_INVALID_HANDLE );
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add an atom entry in the table and return its handle */
|
2001-10-12 20:45:29 +02:00
|
|
|
static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i <= table->last; i++)
|
|
|
|
if (!table->handles[i]) goto found;
|
|
|
|
if (i == table->count)
|
|
|
|
{
|
|
|
|
struct atom_entry **new_table = NULL;
|
|
|
|
int new_size = table->count + table->count / 2;
|
|
|
|
if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
|
|
|
|
if (new_size > table->count)
|
|
|
|
new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
|
|
|
|
if (!new_table)
|
|
|
|
{
|
|
|
|
set_error( STATUS_NO_MEMORY );
|
2001-10-12 20:45:29 +02:00
|
|
|
return 0;
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
table->count = new_size;
|
|
|
|
table->handles = new_table;
|
|
|
|
}
|
|
|
|
table->last = i;
|
|
|
|
found:
|
|
|
|
table->handles[i] = entry;
|
2001-10-12 20:45:29 +02:00
|
|
|
entry->atom = i + MIN_STR_ATOM;
|
|
|
|
return entry->atom;
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compute the hash code for a string */
|
2000-05-30 22:32:06 +02:00
|
|
|
static int atom_hash( struct atom_table *table, const WCHAR *str )
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
WCHAR hash = 0;
|
2000-07-25 23:01:59 +02:00
|
|
|
for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
|
2000-05-30 22:32:06 +02:00
|
|
|
return hash % table->entries_count;
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* dump an atom table */
|
|
|
|
static void atom_table_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct atom_table *table = (struct atom_table *)obj;
|
|
|
|
assert( obj->ops == &atom_table_ops );
|
|
|
|
|
2000-05-30 22:32:06 +02:00
|
|
|
fprintf( stderr, "Atom table size=%d entries=%d\n",
|
|
|
|
table->last + 1, table->entries_count );
|
2000-02-13 14:56:13 +01:00
|
|
|
if (!verbose) return;
|
|
|
|
for (i = 0; i <= table->last; i++)
|
|
|
|
{
|
|
|
|
struct atom_entry *entry = table->handles[i];
|
|
|
|
if (!entry) continue;
|
2001-10-12 20:45:29 +02:00
|
|
|
fprintf( stderr, " %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
|
2000-02-13 14:56:13 +01:00
|
|
|
dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
|
|
|
|
fprintf( stderr, "\"\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy the atom table */
|
|
|
|
static void atom_table_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct atom_table *table = (struct atom_table *)obj;
|
|
|
|
assert( obj->ops == &atom_table_ops );
|
2000-05-30 22:32:06 +02:00
|
|
|
if (table->handles)
|
|
|
|
{
|
|
|
|
for (i = 0; i <= table->last; i++) free( table->handles[i] );
|
|
|
|
free( table->handles );
|
|
|
|
}
|
|
|
|
if (table->entries) free( table->entries );
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find an atom entry in its hash list */
|
|
|
|
static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
|
|
|
|
{
|
|
|
|
struct atom_entry *entry = table->entries[hash];
|
|
|
|
while (entry)
|
|
|
|
{
|
|
|
|
if (!strcmpiW( entry->str, str )) break;
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close the atom table; used on server exit */
|
|
|
|
void close_atom_table(void)
|
|
|
|
{
|
|
|
|
if (global_table) release_object( global_table );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add an atom to the table */
|
2001-10-12 20:45:29 +02:00
|
|
|
static atom_t add_atom( struct atom_table *table, const WCHAR *str )
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
struct atom_entry *entry;
|
2000-05-30 22:32:06 +02:00
|
|
|
int hash = atom_hash( table, str );
|
2001-10-12 20:45:29 +02:00
|
|
|
atom_t atom = 0;
|
2000-02-13 14:56:13 +01:00
|
|
|
|
|
|
|
if (!*str)
|
|
|
|
{
|
|
|
|
set_error( STATUS_OBJECT_NAME_INVALID );
|
2001-10-12 20:45:29 +02:00
|
|
|
return 0;
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
|
|
|
|
{
|
|
|
|
entry->count++;
|
|
|
|
return entry->atom;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
|
|
|
|
{
|
2001-10-12 20:45:29 +02:00
|
|
|
if ((atom = add_atom_entry( table, entry )))
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
entry->prev = NULL;
|
|
|
|
if ((entry->next = table->entries[hash])) entry->next->prev = entry;
|
|
|
|
table->entries[hash] = entry;
|
|
|
|
entry->count = 1;
|
|
|
|
entry->hash = hash;
|
|
|
|
strcpyW( entry->str, str );
|
|
|
|
}
|
|
|
|
else free( entry );
|
|
|
|
}
|
|
|
|
else set_error( STATUS_NO_MEMORY );
|
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete an atom from the table */
|
2001-10-12 20:45:29 +02:00
|
|
|
static void delete_atom( struct atom_table *table, atom_t atom )
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
struct atom_entry *entry = get_atom_entry( table, atom );
|
|
|
|
if (entry && !--entry->count)
|
|
|
|
{
|
|
|
|
if (entry->next) entry->next->prev = entry->prev;
|
|
|
|
if (entry->prev) entry->prev->next = entry->next;
|
|
|
|
else table->entries[entry->hash] = entry->next;
|
2001-10-12 20:45:29 +02:00
|
|
|
table->handles[atom - MIN_STR_ATOM] = NULL;
|
2000-02-13 14:56:13 +01:00
|
|
|
free( entry );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find an atom in the table */
|
2001-10-12 20:45:29 +02:00
|
|
|
static atom_t find_atom( struct atom_table *table, const WCHAR *str )
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
|
|
|
struct atom_entry *entry;
|
|
|
|
|
2001-10-12 20:45:29 +02:00
|
|
|
if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
|
|
|
|
return entry->atom;
|
2000-02-13 14:56:13 +01:00
|
|
|
if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
|
|
|
|
else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
|
2001-10-12 20:45:29 +02:00
|
|
|
return 0;
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get an atom name and refcount*/
|
2001-10-12 20:45:29 +02:00
|
|
|
static size_t get_atom_name( struct atom_table *table, atom_t atom,
|
2000-08-30 02:00:48 +02:00
|
|
|
WCHAR *str, size_t maxsize, int *count )
|
2000-02-13 14:56:13 +01:00
|
|
|
{
|
2000-11-01 04:11:12 +01:00
|
|
|
size_t len = 0;
|
2000-02-13 14:56:13 +01:00
|
|
|
struct atom_entry *entry = get_atom_entry( table, atom );
|
2000-08-30 02:00:48 +02:00
|
|
|
*count = -1;
|
2000-02-13 14:56:13 +01:00
|
|
|
if (entry)
|
|
|
|
{
|
2000-08-30 02:00:48 +02:00
|
|
|
*count = entry->count;
|
|
|
|
len = strlenW( entry->str ) * sizeof(WCHAR);
|
|
|
|
if (len <= maxsize) memcpy( str, entry->str, len );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_OVERFLOW );
|
|
|
|
len = 0;
|
|
|
|
}
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
2000-08-30 02:00:48 +02:00
|
|
|
return len;
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
|
2001-10-12 20:45:29 +02:00
|
|
|
/* increment the ref count of a global atom; used for window properties */
|
|
|
|
int grab_global_atom( atom_t atom )
|
|
|
|
{
|
|
|
|
struct atom_entry *entry = get_atom_entry( global_table, atom );
|
|
|
|
if (entry) entry->count++;
|
|
|
|
return (entry != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decrement the ref count of a global atom; used for window properties */
|
|
|
|
void release_global_atom( atom_t atom )
|
|
|
|
{
|
|
|
|
delete_atom( global_table, atom );
|
|
|
|
}
|
|
|
|
|
2000-02-13 14:56:13 +01:00
|
|
|
/* add a global atom */
|
|
|
|
DECL_HANDLER(add_atom)
|
|
|
|
{
|
2000-05-30 22:32:06 +02:00
|
|
|
struct atom_table **table_ptr = req->local ? ¤t->process->atom_table : &global_table;
|
|
|
|
|
|
|
|
if (!*table_ptr) *table_ptr = create_table(0);
|
2000-08-30 02:00:48 +02:00
|
|
|
if (*table_ptr)
|
|
|
|
{
|
|
|
|
const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
|
|
|
|
if (name) req->atom = add_atom( *table_ptr, name );
|
|
|
|
}
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* delete a global atom */
|
|
|
|
DECL_HANDLER(delete_atom)
|
|
|
|
{
|
2000-08-30 02:00:48 +02:00
|
|
|
delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find a global atom */
|
|
|
|
DECL_HANDLER(find_atom)
|
|
|
|
{
|
2000-08-30 02:00:48 +02:00
|
|
|
const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
|
|
|
|
if (name)
|
|
|
|
req->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get global atom name */
|
|
|
|
DECL_HANDLER(get_atom_name)
|
|
|
|
{
|
2000-08-30 02:00:48 +02:00
|
|
|
WCHAR *name = get_req_data(req);
|
|
|
|
size_t size = get_atom_name( req->local ? current->process->atom_table : global_table,
|
|
|
|
req->atom, name, get_req_data_size(req), &req->count );
|
|
|
|
set_req_data_size( req, size );
|
2000-05-30 22:32:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* init the process atom table */
|
|
|
|
DECL_HANDLER(init_atom_table)
|
|
|
|
{
|
|
|
|
if (!current->process->atom_table)
|
|
|
|
current->process->atom_table = create_table( req->entries );
|
2000-02-13 14:56:13 +01:00
|
|
|
}
|