atom.c 10.6 KB
Newer Older
1 2 3 4 5
/*
 * Server-side atom management
 *
 * Copyright (C) 1999, 2000 Alexandre Julliard
 * Copyright (C) 2000 Turchanov Sergei
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 21
 */

22 23 24
#include "config.h"
#include "wine/port.h"

25 26 27
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
28
#include <string.h>
29 30 31 32

#include "unicode.h"
#include "request.h"
#include "object.h"
33
#include "process.h"
34 35

#define HASH_SIZE     37
36 37 38
#define MIN_HASH_SIZE 4
#define MAX_HASH_SIZE 0x200

39
#define MAX_ATOM_LEN  255
40
#define MIN_STR_ATOM  0xc000
41 42 43 44 45 46 47 48
#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 */
49
    atom_t             atom;   /* atom handle */
50 51 52 53 54 55 56 57 58
    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 */
59 60
    int                 entries_count;       /* humber of hash entries */
    struct atom_entry **entries;             /* hash table entries */
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
};

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 */
76
    no_get_fd,                    /* get_fd */
77 78
    no_flush,                     /* flush */
    no_get_file_info,             /* get_file_info */
79
    NULL,                         /* queue_async */
80 81 82 83 84 85
    atom_table_destroy            /* destroy */
};

static struct atom_table *global_table;


86 87
/* copy an atom name from the request to a temporary area */
static const WCHAR *copy_request_name(void)
88 89 90
{
    static WCHAR buffer[MAX_ATOM_LEN+1];

91 92 93
    const WCHAR *str = get_req_data();
    size_t len = get_req_data_size();

94 95 96 97 98 99 100
    if (len > MAX_ATOM_LEN*sizeof(WCHAR))
    {
        set_error( STATUS_INVALID_PARAMETER );
        return NULL;
    }
    memcpy( buffer, str, len );
    buffer[len / sizeof(WCHAR)] = 0;
101 102 103 104
    return buffer;
}

/* create an atom table */
105
static struct atom_table *create_table(int entries_count)
106 107 108 109 110
{
    struct atom_table *table;

    if ((table = alloc_object( &atom_table_ops, -1 )))
    {
111 112 113 114 115 116 117 118 119
        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 );
120 121 122 123
        table->count = 64;
        table->last  = -1;
        if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
            return table;
124
fail:
125 126 127 128 129 130 131
        release_object( table );
        table = NULL;
    }
    return table;
}

/* retrieve an entry pointer from its atom */
132
static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
133 134
{
    struct atom_entry *entry = NULL;
135 136
    if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
        entry = table->handles[atom - MIN_STR_ATOM];
137 138 139 140 141
    if (!entry) set_error( STATUS_INVALID_HANDLE );
    return entry;
}

/* add an atom entry in the table and return its handle */
142
static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
143 144 145 146 147 148 149 150 151 152 153 154 155 156
{
    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 );
157
            return 0;
158 159 160 161 162 163 164
        }
        table->count = new_size;
        table->handles = new_table;
    }
    table->last = i;
 found:
    table->handles[i] = entry;
165 166
    entry->atom = i + MIN_STR_ATOM;
    return entry->atom;
167 168 169
}

/* compute the hash code for a string */
170
static int atom_hash( struct atom_table *table, const WCHAR *str )
171 172 173
{
    int i;
    WCHAR hash = 0;
174
    for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
175
    return hash % table->entries_count;
176 177 178 179 180 181 182 183 184
}

/* 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 );

185 186
    fprintf( stderr, "Atom table size=%d entries=%d\n",
             table->last + 1, table->entries_count );
187 188 189 190 191
    if (!verbose) return;
    for (i = 0; i <= table->last; i++)
    {
        struct atom_entry *entry = table->handles[i];
        if (!entry) continue;
192
        fprintf( stderr, "  %04x: ref=%d hash=%d \"", entry->atom, entry->count, entry->hash );
193 194 195 196 197 198 199 200 201 202 203
        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 );
204 205 206 207 208 209
    if (table->handles)
    {
        for (i = 0; i <= table->last; i++) free( table->handles[i] );
        free( table->handles );
    }
    if (table->entries) free( table->entries );
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

/* 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 */
231
static atom_t add_atom( struct atom_table *table, const WCHAR *str )
232 233
{
    struct atom_entry *entry;
234
    int hash = atom_hash( table, str );
235
    atom_t atom = 0;
236 237 238 239

    if (!*str)
    {
        set_error( STATUS_OBJECT_NAME_INVALID );
240
        return 0;
241 242 243 244 245 246 247 248 249
    }
    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) )))
    {
250
        if ((atom = add_atom_entry( table, entry )))
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        {
            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 */
266
static void delete_atom( struct atom_table *table, atom_t atom )
267 268 269 270 271 272 273
{
    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;
274
        table->handles[atom - MIN_STR_ATOM] = NULL;
275 276 277 278 279
        free( entry );
    }
}

/* find an atom in the table */
280
static atom_t find_atom( struct atom_table *table, const WCHAR *str )
281 282 283
{
    struct atom_entry *entry;

284 285
    if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) ))))
        return entry->atom;
286 287
    if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
    else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
288
    return 0;
289 290
}

291 292 293
/* increment the ref count of a global atom; used for window properties */
int grab_global_atom( atom_t atom )
{
294 295 296 297 298 299 300
    if (atom >= MIN_STR_ATOM)
    {
        struct atom_entry *entry = get_atom_entry( global_table, atom );
        if (entry) entry->count++;
        return (entry != NULL);
    }
    else return 1;
301 302 303 304 305
}

/* decrement the ref count of a global atom; used for window properties */
void release_global_atom( atom_t atom )
{
306
    if (atom >= MIN_STR_ATOM) delete_atom( global_table, atom );
307 308
}

309 310 311
/* add a global atom */
DECL_HANDLER(add_atom)
{
312 313 314
    struct atom_table **table_ptr = req->local ? &current->process->atom_table : &global_table;

    if (!*table_ptr) *table_ptr = create_table(0);
315 316
    if (*table_ptr)
    {
317 318
        const WCHAR *name = copy_request_name();
        if (name) reply->atom = add_atom( *table_ptr, name );
319
    }
320 321 322 323 324
}

/* delete a global atom */
DECL_HANDLER(delete_atom)
{
325
    delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
326 327 328 329 330
}

/* find a global atom */
DECL_HANDLER(find_atom)
{
331
    const WCHAR *name = copy_request_name();
332
    if (name)
333
        reply->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
334 335 336 337 338
}

/* get global atom name */
DECL_HANDLER(get_atom_name)
{
339 340 341 342 343 344 345 346 347 348 349 350
    struct atom_entry *entry;
    size_t len = 0;

    reply->count = -1;
    if ((entry = get_atom_entry( req->local ? current->process->atom_table : global_table,
                                 req->atom )))
    {
        reply->count = entry->count;
        len = strlenW( entry->str ) * sizeof(WCHAR);
        if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
        else set_error( STATUS_BUFFER_OVERFLOW );
    }
351 352 353 354 355 356 357
}

/* init the process atom table */
DECL_HANDLER(init_atom_table)
{
    if (!current->process->atom_table)
        current->process->atom_table = create_table( req->entries );
358
}