atom.c 14.2 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
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
#define WIN32_NO_STATUS

33 34 35
#include "unicode.h"
#include "request.h"
#include "object.h"
36
#include "process.h"
37
#include "handle.h"
38 39
#include "user.h"
#include "winuser.h"
40
#include "winternl.h"
41 42

#define HASH_SIZE     37
43 44 45
#define MIN_HASH_SIZE 4
#define MAX_HASH_SIZE 0x200

46
#define MAX_ATOM_LEN  (255 * sizeof(WCHAR))
47
#define MIN_STR_ATOM  0xc000
48 49 50 51 52 53 54
#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 */
55
    short              pinned; /* whether the atom is pinned or not */
56
    atom_t             atom;   /* atom handle */
57 58
    unsigned short     hash;   /* string hash */
    unsigned short     len;    /* string len */
59 60 61 62 63 64 65 66 67
    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 */
68
    int                 entries_count;       /* number of hash entries */
69
    struct atom_entry **entries;             /* hash table entries */
70 71 72 73 74 75 76 77 78
};

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 */
79
    no_get_type,                  /* get_type */
80 81 82
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
83 84
    NULL,                         /* satisfied */
    no_signal,                    /* signal */
85
    no_get_fd,                    /* get_fd */
86
    no_map_access,                /* map_access */
87 88
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
89
    no_lookup_name,               /* lookup_name */
90
    no_open_file,                 /* open_file */
91
    no_close_handle,              /* close_handle */
92 93 94
    atom_table_destroy            /* destroy */
};

95
static struct atom_table *global_table;
96 97

/* create an atom table */
98
static struct atom_table *create_table(int entries_count)
99 100 101
{
    struct atom_table *table;

102
    if ((table = alloc_object( &atom_table_ops )))
103
    {
104 105 106 107 108 109 110 111 112
        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 );
113 114 115 116
        table->count = 64;
        table->last  = -1;
        if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
            return table;
117
fail:
118 119 120 121 122 123 124
        release_object( table );
        table = NULL;
    }
    return table;
}

/* retrieve an entry pointer from its atom */
125
static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
126 127
{
    struct atom_entry *entry = NULL;
128 129
    if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
        entry = table->handles[atom - MIN_STR_ATOM];
130 131 132 133 134
    if (!entry) set_error( STATUS_INVALID_HANDLE );
    return entry;
}

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

/* compute the hash code for a string */
163
static unsigned short atom_hash( struct atom_table *table, const struct unicode_str *str )
164
{
165 166
    unsigned int i;
    unsigned short hash = 0;
167
    for (i = 0; i < str->len / sizeof(WCHAR); i++) hash ^= toupperW(str->str[i]) + i;
168
    return hash % table->entries_count;
169 170 171 172 173 174 175 176 177
}

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

178 179
    fprintf( stderr, "Atom table size=%d entries=%d\n",
             table->last + 1, table->entries_count );
180 181 182 183 184
    if (!verbose) return;
    for (i = 0; i <= table->last; i++)
    {
        struct atom_entry *entry = table->handles[i];
        if (!entry) continue;
185
        fprintf( stderr, "  %04x: ref=%d pinned=%c hash=%d \"",
186
                 entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
187
        dump_strW( entry->str, entry->len / sizeof(WCHAR), stderr, "\"\"");
188 189 190 191 192 193 194 195 196 197
        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 );
198 199 200 201 202
    if (table->handles)
    {
        for (i = 0; i <= table->last; i++) free( table->handles[i] );
        free( table->handles );
    }
203
    free( table->entries );
204 205 206
}

/* find an atom entry in its hash list */
207 208
static struct atom_entry *find_atom_entry( struct atom_table *table, const struct unicode_str *str,
                                           unsigned short hash )
209 210 211 212
{
    struct atom_entry *entry = table->entries[hash];
    while (entry)
    {
213
        if (entry->len == str->len && !memicmpW( entry->str, str->str, str->len/sizeof(WCHAR) )) break;
214 215 216 217 218 219
        entry = entry->next;
    }
    return entry;
}

/* add an atom to the table */
220
static atom_t add_atom( struct atom_table *table, const struct unicode_str *str )
221 222
{
    struct atom_entry *entry;
223
    unsigned short hash = atom_hash( table, str );
224
    atom_t atom = 0;
225

226
    if (!str->len)
227 228
    {
        set_error( STATUS_OBJECT_NAME_INVALID );
229
        return 0;
230
    }
231
    if (str->len > MAX_ATOM_LEN)
232 233 234 235
    {
        set_error( STATUS_INVALID_PARAMETER );
        return 0;
    }
236
    if ((entry = find_atom_entry( table, str, hash )))  /* exists already */
237 238 239 240 241
    {
        entry->count++;
        return entry->atom;
    }

242
    if ((entry = mem_alloc( FIELD_OFFSET( struct atom_entry, str[str->len / sizeof(WCHAR)] ) )))
243
    {
244
        if ((atom = add_atom_entry( table, entry )))
245 246 247 248
        {
            entry->prev  = NULL;
            if ((entry->next = table->entries[hash])) entry->next->prev = entry;
            table->entries[hash] = entry;
249
            entry->count  = 1;
250
            entry->pinned = 0;
251
            entry->hash   = hash;
252 253
            entry->len    = str->len;
            memcpy( entry->str, str->str, str->len );
254 255 256 257 258 259 260 261
        }
        else free( entry );
    }
    else set_error( STATUS_NO_MEMORY );
    return atom;
}

/* delete an atom from the table */
262
static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
263 264
{
    struct atom_entry *entry = get_atom_entry( table, atom );
265 266 267
    if (!entry) return;
    if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
    else if (!--entry->count)
268 269 270 271
    {
        if (entry->next) entry->next->prev = entry->prev;
        if (entry->prev) entry->prev->next = entry->next;
        else table->entries[entry->hash] = entry->next;
272
        table->handles[atom - MIN_STR_ATOM] = NULL;
273 274 275 276 277
        free( entry );
    }
}

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

282
    if (!str->len)
283 284 285 286
    {
        set_error( STATUS_OBJECT_NAME_INVALID );
        return 0;
    }
287
    if (str->len > MAX_ATOM_LEN)
288 289 290 291
    {
        set_error( STATUS_INVALID_PARAMETER );
        return 0;
    }
292
    if (table && (entry = find_atom_entry( table, str, atom_hash(table, str) )))
293 294 295 296 297
        return entry->atom;
    set_error( STATUS_OBJECT_NAME_NOT_FOUND );
    return 0;
}

298
static struct atom_table *get_global_table( struct winstation *winstation, int create )
299
{
300 301
    struct atom_table *table = winstation ? winstation->atom_table : global_table;
    if (!table)
302
    {
303 304 305 306 307 308 309 310 311 312
        if (create)
        {
            table = create_table( HASH_SIZE );
            if (winstation) winstation->atom_table = table;
            else
            {
                global_table = table;
                make_object_static( &global_table->obj );
            }
        }
313 314
        else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
    }
315
    return table;
316 317 318
}

static struct atom_table *get_table( obj_handle_t h, int create )
319
{
320
    struct atom_table *table = NULL;
321

322 323 324 325 326 327
    if (h)
    {
        table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
    }
    else
    {
328 329
        table = get_global_table( NULL, 1 );
        if (table) grab_object( table );
330 331
    }
    return table;
332 333 334
}

/* add an atom in the global table; used for window properties */
335
atom_t add_global_atom( struct winstation *winstation, const struct unicode_str *str )
336
{
337 338 339
    struct atom_table *table = get_global_table( winstation, 1 );
    if (!table) return 0;
    return add_atom( table, str );
340 341 342
}

/* find an atom in the global table; used for window properties */
343
atom_t find_global_atom( struct winstation *winstation, const struct unicode_str *str )
344
{
345
    struct atom_table *table = get_global_table( winstation, 0 );
346 347
    struct atom_entry *entry;

348 349
    if (!str->len || str->len > MAX_ATOM_LEN || !table) return 0;
    if ((entry = find_atom_entry( table, str, atom_hash(table, str) )))
350 351
        return entry->atom;
    return 0;
352 353
}

354
/* increment the ref count of a global atom; used for window properties */
355
int grab_global_atom( struct winstation *winstation, atom_t atom )
356
{
357 358
    if (atom >= MIN_STR_ATOM)
    {
359 360
        struct atom_table *table = get_global_table( winstation, 0 );
        if (table)
361
        {
362
            struct atom_entry *entry = get_atom_entry( table, atom );
363 364 365 366
            if (entry) entry->count++;
            return (entry != NULL);
        }
        else return 0;
367 368
    }
    else return 1;
369 370 371
}

/* decrement the ref count of a global atom; used for window properties */
372
void release_global_atom( struct winstation *winstation, atom_t atom )
373
{
374 375
    if (atom >= MIN_STR_ATOM)
    {
376 377
        struct atom_table *table = get_global_table( winstation, 0 );
        if (table) delete_atom( table, atom, 1 );
378
    }
379 380
}

381 382 383
/* add a global atom */
DECL_HANDLER(add_atom)
{
384
    struct unicode_str name;
385
    struct atom_table *table = get_table( req->table, 1 );
386

387
    if (table)
388
    {
389 390
        get_req_unicode_str( &name );
        reply->atom = add_atom( table, &name );
391
        release_object( table );
392
    }
393 394 395 396 397
}

/* delete a global atom */
DECL_HANDLER(delete_atom)
{
398
    struct atom_table *table = get_table( req->table, 0 );
399 400
    if (table)
    {
401
        delete_atom( table, req->atom, 0 );
402 403
        release_object( table );
    }
404 405 406 407 408
}

/* find a global atom */
DECL_HANDLER(find_atom)
{
409
    struct unicode_str name;
410
    struct atom_table *table = get_table( req->table, 0 );
411

412 413
    if (table)
    {
414 415
        get_req_unicode_str( &name );
        reply->atom = find_atom( table, &name );
416 417
        release_object( table );
    }
418 419 420
}

/* get global atom name */
421
DECL_HANDLER(get_atom_information)
422
{
423
    struct atom_table *table = get_table( req->table, 0 );
424 425 426
    if (table)
    {
        struct atom_entry *entry;
427

428 429
        if ((entry = get_atom_entry( table, req->atom )))
        {
430
            set_reply_data( entry->str, min( entry->len, get_reply_max_size() ));
431 432
            reply->count = entry->count;
            reply->pinned = entry->pinned;
433
            reply->total = entry->len;
434 435 436 437 438 439 440 441 442
        }
        else reply->count = -1;
        release_object( table );
    }
}

/* set global atom name */
DECL_HANDLER(set_atom_information)
{
443
    struct atom_table *table = get_table( req->table, 0 );
444
    if (table)
445
    {
446 447 448 449 450 451 452
        struct atom_entry *entry;

        if ((entry = get_atom_entry( table, req->atom )))
        {
            if (req->pinned) entry->pinned = 1;
        }
        release_object( table );
453
    }
454 455
}

456
/* init a (local) atom table */
457 458
DECL_HANDLER(init_atom_table)
{
459
    struct atom_table* table = create_table( req->entries );
460

461 462
    if (table)
    {
463
        reply->table = alloc_handle( current->process, table, 0, 0 );
464 465
        release_object( table );
    }
466 467 468 469 470
}

/* set global atom name */
DECL_HANDLER(empty_atom_table)
{
471
    struct atom_table *table = get_table( req->table, 1 );
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    if (table)
    {
        int i;
        struct atom_entry *entry;

        for (i = 0; i <= table->last; i++)
        {
            entry = table->handles[i];
            if (entry && (!entry->pinned || req->if_pinned))
            {
                if (entry->next) entry->next->prev = entry->prev;
                if (entry->prev) entry->prev->next = entry->next;
                else table->entries[entry->hash] = entry->next;
                table->handles[i] = NULL;
                free( entry );
            }
        }
        release_object( table );
    }
491
}