123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- /*
- * Server-side atom management
- *
- * Copyright (C) 1999, 2000 Alexandre Julliard
- * Copyright (C) 2000 Turchanov Sergei
- *
- * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
- */
- #include "config.h"
- #include "wine/port.h"
- #include <assert.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include "ntstatus.h"
- #define WIN32_NO_STATUS
- #include "unicode.h"
- #include "request.h"
- #include "object.h"
- #include "process.h"
- #include "handle.h"
- #include "user.h"
- #include "winuser.h"
- #include "winternl.h"
- #define HASH_SIZE 37
- #define MIN_HASH_SIZE 4
- #define MAX_HASH_SIZE 0x200
- #define MAX_ATOM_LEN (255 * sizeof(WCHAR))
- #define MIN_STR_ATOM 0xc000
- #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 */
- short pinned; /* whether the atom is pinned or not */
- atom_t atom; /* atom handle */
- unsigned short hash; /* string hash */
- unsigned short len; /* string len */
- 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 */
- int entries_count; /* number of hash entries */
- struct atom_entry **entries; /* hash table entries */
- };
- 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_get_type, /* get_type */
- no_add_queue, /* add_queue */
- NULL, /* remove_queue */
- NULL, /* signaled */
- NULL, /* get_esync_fd */
- NULL, /* satisfied */
- no_signal, /* signal */
- no_get_fd, /* get_fd */
- no_map_access, /* map_access */
- default_get_sd, /* get_sd */
- default_set_sd, /* set_sd */
- no_lookup_name, /* lookup_name */
- no_link_name, /* link_name */
- NULL, /* unlink_name */
- no_open_file, /* open_file */
- no_kernel_obj_list, /* get_kernel_obj_list */
- no_alloc_handle, /* alloc_handle */
- no_close_handle, /* close_handle */
- atom_table_destroy /* destroy */
- };
- static struct atom_table *global_table;
- /* create an atom table */
- static struct atom_table *create_table(int entries_count)
- {
- struct atom_table *table;
- if ((table = alloc_object( &atom_table_ops )))
- {
- if ((entries_count < MIN_HASH_SIZE) ||
- (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
- table->handles = NULL;
- 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 );
- table->count = 64;
- table->last = -1;
- if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
- return table;
- fail:
- release_object( table );
- table = NULL;
- }
- return table;
- }
- /* retrieve an entry pointer from its atom */
- static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
- {
- struct atom_entry *entry = NULL;
- if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
- entry = table->handles[atom - MIN_STR_ATOM];
- if (!entry) set_error( STATUS_INVALID_HANDLE );
- return entry;
- }
- /* add an atom entry in the table and return its handle */
- static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
- {
- 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 );
- return 0;
- }
- table->count = new_size;
- table->handles = new_table;
- }
- table->last = i;
- found:
- table->handles[i] = entry;
- entry->atom = i + MIN_STR_ATOM;
- return entry->atom;
- }
- /* 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 );
- fprintf( stderr, "Atom table size=%d entries=%d\n",
- table->last + 1, table->entries_count );
- if (!verbose) return;
- for (i = 0; i <= table->last; i++)
- {
- struct atom_entry *entry = table->handles[i];
- if (!entry) continue;
- fprintf( stderr, " %04x: ref=%d pinned=%c hash=%d \"",
- entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
- dump_strW( entry->str, entry->len, 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 );
- if (table->handles)
- {
- for (i = 0; i <= table->last; i++) free( table->handles[i] );
- free( table->handles );
- }
- free( table->entries );
- }
- /* find an atom entry in its hash list */
- static struct atom_entry *find_atom_entry( struct atom_table *table, const struct unicode_str *str,
- unsigned short hash )
- {
- struct atom_entry *entry = table->entries[hash];
- while (entry)
- {
- if (entry->len == str->len && !memicmp_strW( entry->str, str->str, str->len )) break;
- entry = entry->next;
- }
- return entry;
- }
- /* add an atom to the table */
- static atom_t add_atom( struct atom_table *table, const struct unicode_str *str )
- {
- struct atom_entry *entry;
- unsigned short hash = hash_strW( str->str, str->len, table->entries_count );
- atom_t atom = 0;
- if (!str->len)
- {
- set_error( STATUS_OBJECT_NAME_INVALID );
- return 0;
- }
- if (str->len > MAX_ATOM_LEN)
- {
- set_error( STATUS_INVALID_PARAMETER );
- return 0;
- }
- if ((entry = find_atom_entry( table, str, hash ))) /* exists already */
- {
- entry->count++;
- return entry->atom;
- }
- if ((entry = mem_alloc( FIELD_OFFSET( struct atom_entry, str[str->len / sizeof(WCHAR)] ) )))
- {
- if ((atom = add_atom_entry( table, entry )))
- {
- entry->prev = NULL;
- if ((entry->next = table->entries[hash])) entry->next->prev = entry;
- table->entries[hash] = entry;
- entry->count = 1;
- entry->pinned = 0;
- entry->hash = hash;
- entry->len = str->len;
- memcpy( entry->str, str->str, str->len );
- }
- else free( entry );
- }
- else set_error( STATUS_NO_MEMORY );
- return atom;
- }
- /* delete an atom from the table */
- static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
- {
- struct atom_entry *entry = get_atom_entry( table, atom );
- if (!entry) return;
- if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
- else if (!--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;
- table->handles[atom - MIN_STR_ATOM] = NULL;
- free( entry );
- }
- }
- /* find an atom in the table */
- static atom_t find_atom( struct atom_table *table, const struct unicode_str *str )
- {
- struct atom_entry *entry;
- if (!str->len)
- {
- set_error( STATUS_OBJECT_NAME_INVALID );
- return 0;
- }
- if (str->len > MAX_ATOM_LEN)
- {
- set_error( STATUS_INVALID_PARAMETER );
- return 0;
- }
- if (table && (entry = find_atom_entry( table, str,
- hash_strW( str->str, str->len, table->entries_count ))))
- return entry->atom;
- set_error( STATUS_OBJECT_NAME_NOT_FOUND );
- return 0;
- }
- static struct atom_table *get_global_table( struct winstation *winstation, int create )
- {
- struct atom_table *table = winstation ? winstation->atom_table : global_table;
- if (!table)
- {
- if (create)
- {
- table = create_table( HASH_SIZE );
- if (winstation) winstation->atom_table = table;
- else
- {
- global_table = table;
- make_object_static( &global_table->obj );
- }
- }
- else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
- }
- return table;
- }
- static struct atom_table *get_table( obj_handle_t h, int create )
- {
- struct atom_table *table = NULL;
- if (h)
- {
- table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
- }
- else
- {
- table = get_global_table( NULL, 1 );
- if (table) grab_object( table );
- }
- return table;
- }
- /* add an atom in the global table; used for window properties */
- atom_t add_global_atom( struct winstation *winstation, const struct unicode_str *str )
- {
- struct atom_table *table = get_global_table( winstation, 1 );
- if (!table) return 0;
- return add_atom( table, str );
- }
- /* find an atom in the global table; used for window properties */
- atom_t find_global_atom( struct winstation *winstation, const struct unicode_str *str )
- {
- struct atom_table *table = get_global_table( winstation, 0 );
- struct atom_entry *entry;
- if (!str->len || str->len > MAX_ATOM_LEN || !table) return 0;
- if ((entry = find_atom_entry( table, str, hash_strW( str->str, str->len, table->entries_count ))))
- return entry->atom;
- return 0;
- }
- /* increment the ref count of a global atom; used for window properties */
- int grab_global_atom( struct winstation *winstation, atom_t atom )
- {
- if (atom >= MIN_STR_ATOM)
- {
- struct atom_table *table = get_global_table( winstation, 0 );
- if (table)
- {
- struct atom_entry *entry = get_atom_entry( table, atom );
- if (entry) entry->count++;
- return (entry != NULL);
- }
- else return 0;
- }
- else return 1;
- }
- /* decrement the ref count of a global atom; used for window properties */
- void release_global_atom( struct winstation *winstation, atom_t atom )
- {
- if (atom >= MIN_STR_ATOM)
- {
- struct atom_table *table = get_global_table( winstation, 0 );
- if (table) delete_atom( table, atom, 1 );
- }
- }
- /* add a global atom */
- DECL_HANDLER(add_atom)
- {
- struct unicode_str name = get_req_unicode_str();
- struct atom_table *table = get_table( req->table, 1 );
- if (table)
- {
- reply->atom = add_atom( table, &name );
- release_object( table );
- }
- }
- /* delete a global atom */
- DECL_HANDLER(delete_atom)
- {
- struct atom_table *table = get_table( req->table, 0 );
- if (table)
- {
- delete_atom( table, req->atom, 0 );
- release_object( table );
- }
- }
- /* find a global atom */
- DECL_HANDLER(find_atom)
- {
- struct unicode_str name = get_req_unicode_str();
- struct atom_table *table = get_table( req->table, 0 );
- if (table)
- {
- reply->atom = find_atom( table, &name );
- release_object( table );
- }
- }
- /* get global atom name */
- DECL_HANDLER(get_atom_information)
- {
- struct atom_table *table = get_table( req->table, 0 );
- if (table)
- {
- struct atom_entry *entry;
- if ((entry = get_atom_entry( table, req->atom )))
- {
- set_reply_data( entry->str, min( entry->len, get_reply_max_size() ));
- reply->count = entry->count;
- reply->pinned = entry->pinned;
- reply->total = entry->len;
- }
- else reply->count = -1;
- release_object( table );
- }
- }
- /* set global atom name */
- DECL_HANDLER(set_atom_information)
- {
- struct atom_table *table = get_table( req->table, 0 );
- if (table)
- {
- struct atom_entry *entry;
- if ((entry = get_atom_entry( table, req->atom )))
- {
- if (req->pinned) entry->pinned = 1;
- }
- release_object( table );
- }
- }
- /* init a (local) atom table */
- DECL_HANDLER(init_atom_table)
- {
- struct atom_table* table = create_table( req->entries );
- if (table)
- {
- reply->table = alloc_handle( current->process, table, 0, 0 );
- release_object( table );
- }
- }
- /* set global atom name */
- DECL_HANDLER(empty_atom_table)
- {
- struct atom_table *table = get_table( req->table, 1 );
- 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 );
- }
- }
|