Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side objects |
| 3 | * These are the server equivalent of K32OBJ |
| 4 | * |
| 5 | * Copyright (C) 1998 Alexandre Julliard |
| 6 | */ |
| 7 | |
| 8 | #include <assert.h> |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 9 | #include <limits.h> |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 13 | #include "winerror.h" |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 14 | #include "server.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 15 | #include "server/thread.h" |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 16 | |
| 17 | int debug_level = 0; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 18 | |
| 19 | struct object_name |
| 20 | { |
| 21 | struct object_name *next; |
| 22 | struct object *obj; |
| 23 | int len; |
| 24 | char name[1]; |
| 25 | }; |
| 26 | |
| 27 | #define NAME_HASH_SIZE 37 |
| 28 | |
| 29 | static struct object_name *names[NAME_HASH_SIZE]; |
| 30 | |
| 31 | /*****************************************************************/ |
| 32 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 33 | void *mem_alloc( size_t size ) |
| 34 | { |
| 35 | void *ptr = malloc( size ); |
| 36 | if (ptr) memset( ptr, 0x55, size ); |
| 37 | else if (current) SET_ERROR( ERROR_OUTOFMEMORY ); |
| 38 | return ptr; |
| 39 | } |
| 40 | |
| 41 | /*****************************************************************/ |
| 42 | |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 43 | static int get_name_hash( const char *name ) |
| 44 | { |
| 45 | int hash = 0; |
| 46 | while (*name) hash ^= *name++; |
| 47 | return hash % NAME_HASH_SIZE; |
| 48 | } |
| 49 | |
| 50 | static struct object_name *add_name( struct object *obj, const char *name ) |
| 51 | { |
| 52 | struct object_name *ptr; |
| 53 | int hash = get_name_hash( name ); |
| 54 | int len = strlen( name ); |
| 55 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 56 | if (!(ptr = (struct object_name *)mem_alloc( sizeof(*ptr) + len ))) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 57 | return NULL; |
| 58 | ptr->next = names[hash]; |
| 59 | ptr->obj = obj; |
| 60 | ptr->len = len; |
| 61 | strcpy( ptr->name, name ); |
| 62 | names[hash] = ptr; |
| 63 | return ptr; |
| 64 | } |
| 65 | |
| 66 | static void free_name( struct object *obj ) |
| 67 | { |
| 68 | int hash = get_name_hash( obj->name->name ); |
| 69 | struct object_name **pptr = &names[hash]; |
| 70 | while (*pptr && *pptr != obj->name) pptr = &(*pptr)->next; |
| 71 | assert( *pptr ); |
| 72 | *pptr = (*pptr)->next; |
| 73 | free( obj->name ); |
| 74 | } |
| 75 | |
| 76 | /* initialize an already allocated object */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 77 | /* return 1 if OK, 0 on error */ |
| 78 | int init_object( struct object *obj, const struct object_ops *ops, |
| 79 | const char *name ) |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 80 | { |
| 81 | obj->refcount = 1; |
| 82 | obj->ops = ops; |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 83 | obj->head = NULL; |
| 84 | obj->tail = NULL; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 85 | if (!name) obj->name = NULL; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 86 | else if (!(obj->name = add_name( obj, name ))) return 0; |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | struct object *create_named_object( const char *name, const struct object_ops *ops, size_t size ) |
| 91 | { |
| 92 | struct object *obj; |
| 93 | if ((obj = find_object( name ))) |
| 94 | { |
| 95 | if (obj->ops == ops) |
| 96 | { |
| 97 | SET_ERROR( ERROR_ALREADY_EXISTS ); |
| 98 | return obj; |
| 99 | } |
| 100 | SET_ERROR( ERROR_INVALID_HANDLE ); |
| 101 | return NULL; |
| 102 | } |
| 103 | if (!(obj = mem_alloc( size ))) return NULL; |
| 104 | if (!init_object( obj, ops, name )) |
| 105 | { |
| 106 | free( obj ); |
| 107 | return NULL; |
| 108 | } |
| 109 | CLEAR_ERROR(); |
| 110 | return obj; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 113 | /* grab an object (i.e. increment its refcount) and return the object */ |
| 114 | struct object *grab_object( void *ptr ) |
| 115 | { |
| 116 | struct object *obj = (struct object *)ptr; |
| 117 | assert( obj->refcount < INT_MAX ); |
| 118 | obj->refcount++; |
| 119 | return obj; |
| 120 | } |
| 121 | |
| 122 | /* release an object (i.e. decrement its refcount) */ |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 123 | void release_object( void *ptr ) |
| 124 | { |
| 125 | struct object *obj = (struct object *)ptr; |
| 126 | assert( obj->refcount ); |
| 127 | if (!--obj->refcount) |
| 128 | { |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 129 | /* if the refcount is 0, nobody can be in the wait queue */ |
| 130 | assert( !obj->head ); |
| 131 | assert( !obj->tail ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 132 | if (obj->name) free_name( obj ); |
| 133 | obj->ops->destroy( obj ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* find an object by its name; the refcount is incremented */ |
| 138 | struct object *find_object( const char *name ) |
| 139 | { |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 140 | struct object_name *ptr; |
| 141 | if (!name) return NULL; |
| 142 | ptr = names[ get_name_hash( name ) ]; |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 143 | while (ptr && strcmp( ptr->name, name )) ptr = ptr->next; |
| 144 | if (!ptr) return NULL; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 145 | return grab_object( ptr->obj ); |
Alexandre Julliard | 642d313 | 1998-07-12 19:29:36 +0000 | [diff] [blame] | 146 | } |
Alexandre Julliard | aa0ebd0 | 1998-12-30 12:06:45 +0000 | [diff] [blame^] | 147 | |
| 148 | /* functions for unimplemented object operations */ |
| 149 | |
| 150 | int no_satisfied( struct object *obj, struct thread *thread ) |
| 151 | { |
| 152 | return 0; /* not abandoned */ |
| 153 | } |
| 154 | |
| 155 | int no_read_fd( struct object *obj ) |
| 156 | { |
| 157 | SET_ERROR( ERROR_INVALID_HANDLE ); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | int no_write_fd( struct object *obj ) |
| 162 | { |
| 163 | SET_ERROR( ERROR_INVALID_HANDLE ); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | int no_flush( struct object *obj ) |
| 168 | { |
| 169 | SET_ERROR( ERROR_INVALID_HANDLE ); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | void default_select_event( int fd, int event, void *private ) |
| 174 | { |
| 175 | struct object *obj = (struct object *)private; |
| 176 | assert( obj ); |
| 177 | wake_up( obj, 0 ); |
| 178 | } |