1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/*
*
* Copyright (c) International Business Machines Corp., 2000
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Module: handlemMgr.c
*
*/
#include <stdlib.h>
#include <fullengine.h>
#include "handlemgr.h"
#include "engine.h"
/* Structure to hold information about a handle */
typedef struct handle_entry_s {
object_handle_t handle;
void * object;
object_type_t type;
struct handle_entry_s * next;
} handle_entry_t;
/* Structure for an entry in the hash table */
typedef struct hash_table_entry_s {
u_int32_t serial;
handle_entry_t * head;
} hash_table_entry_t;
/*
* The hash table size should be prime number. A prim number promotes
* a more even distribution of entries in the hash table.
* Other "prime" candidates that are near a power of 10 or power of 2):
* 257, 499, 503, 509, 521, 997, 1009, 1021, 1031
*/
#define HASH_TABLE_SIZE 127
/* A handle consists of an index into the hash table and a serial number */
#define HANDLE_INDEX_BITS 8
#define HANDLE_INDEX_MASK ((1 << HANDLE_INDEX_BITS) - 1)
#define HANDLE_SERIAL_BITS (32 - INDEX_BITS)
#define MAKE_HANDLE(index, serial) (index + 1 + (serial << HANDLE_INDEX_BITS))
#define GET_HASH_INDEX(handle) ((handle & HANDLE_INDEX_MASK) - 1)
static hash_table_entry_t * hash_table = NULL;
/*
* Initialize the handle manager.
* The main task here is to allocate the hash table.
*
* Return TRUE if the allocation as successful, else return FALSE.
*/
BOOLEAN initialize_handle_manager(void) {
BOOLEAN result = TRUE;
LOG_PROC_ENTRY();
/* Has the Handle Manager already been initialized? */
if (hash_table == NULL) {
hash_table = (hash_table_entry_t *) calloc(HASH_TABLE_SIZE, sizeof(hash_table_entry_t));
if (hash_table == NULL)
result = FALSE;
}
LOG_PROC_EXIT_BOOLEAN(result);
return result;
}
/*
* Hash value computer. Modeled after ElfHash().
*/
static u_int32_t hash_value_for_address(void * address) {
u_int32_t h = 0;
u_int32_t g;
int i;
u_char * pByte = (u_char *) &address;
for (i = 0; i < sizeof(address); i++) {
h = (h << 4) + pByte[i];
g = h & 0xF0000000;
if (g != 0) {
h ^= g >> 24;
}
h &= ~g;
}
return h;
}
/*
* Create a user handle for an object. The object TAG is
* associated with the handle.
*/
int create_handle(void * object, object_type_t object_type, object_handle_t * handle) {
int rc = HANDLE_MANAGER_NO_ERROR;
handle_entry_t * handle_entry;
u_int32_t hash_index;
LOG_PROC_ENTRY();
/* Initialize return handle in case we bail out. */
*handle = 0;
/* Has the hash_table been created yet? */
if (hash_table != NULL) {
/* Allocate a new handle entry. */
handle_entry = malloc(sizeof(handle_entry_t));
if (handle_entry != NULL) {
/* Get the hash index for this new handle entry. */
hash_index = hash_value_for_address(handle_entry) % HASH_TABLE_SIZE;
handle_entry->object = object;
handle_entry->type = object_type;
handle_entry->next = NULL;
/*
* The handle is a combination of the hash index and the current
* serial number at that index. Increment the serial number for
* the next handle entry that will go in the same hash slot.
*/
handle_entry->handle = MAKE_HANDLE(hash_index, hash_table[hash_index].serial++);
/* Put the handle entry into the hash table. */
handle_entry->next = hash_table[hash_index].head;
hash_table[hash_index].head = handle_entry;
/* Return the new handle. */
*handle = handle_entry->handle;
} else {
rc = ENOMEM;
}
} else {
/*
* There is no hash_table, which means that this module has not been
* initialized yet!
*/
rc = HANDLE_MANAGER_NOT_INITIALIZED;
}
LOG_PROC_EXIT_INT(rc);
return rc;
}
/*
* Destroy a user handle. Remove it from the hash table so it will never be
* found again.
*/
int destroy_handle(object_handle_t handle) {
int rc = HANDLE_MANAGER_NO_ERROR;
u_int32_t hash_index;
handle_entry_t ** pp_handle_entry;
LOG_PROC_ENTRY();
/* Has the hash_table been created yet? */
if (hash_table != NULL) {
/* Get the index of the hash table entry that should have this handle.*/
hash_index = GET_HASH_INDEX(handle);
if (hash_index < HASH_TABLE_SIZE) {
/*
* Look for a handle_entry with a handle that matches the one
* given.
*/
pp_handle_entry = &hash_table[hash_index].head;
while ((*pp_handle_entry != NULL) && ((*pp_handle_entry)->handle != handle)) {
pp_handle_entry = &((*pp_handle_entry)->next);
}
/*
* If we found a matching handle_entry, remove it from the hash table,
* and free the handle_entry.
*/
if (*pp_handle_entry != NULL) {
handle_entry_t * p_handle_entry = *pp_handle_entry;
*pp_handle_entry = (*pp_handle_entry)->next;
free(p_handle_entry);
} else {
/* Bad handle. It was not found in the table. */
rc = HANDLE_MANAGER_BAD_HANDLE;
}
} else {
/*
* The handle is not valid. Its index goes beyond the end of the
* hash table.
*/
rc = HANDLE_MANAGER_BAD_HANDLE;
}
} else {
/*
* There is no hash_table, which means that this module has not been
* initialized yet!
*/
rc = HANDLE_MANAGER_NOT_INITIALIZED;
}
LOG_PROC_EXIT_INT(rc);
return rc;
}
/*
* Destroy all the handle entries in the hash table.
*/
int destroy_all_handles(void) {
int rc = HANDLE_MANAGER_NO_ERROR;
int i;
handle_entry_t * handle_entry;
LOG_PROC_ENTRY();
/* Has the hash_table list been created yet? */
if (hash_table != NULL) {
/*
* Loop through all the hash_table entries and free any handle entries
* that are found. The serial numbers are left alone, just in case
* new handle entries will be added later. Leaving the serial numbers
* will help ensure that future handles are different from past handles.
*/
for (i = 0; i < HASH_TABLE_SIZE; i ++) {
while (hash_table[i].head != NULL) {
handle_entry = hash_table[i].head;
hash_table[i].head = handle_entry->next;
free(handle_entry);
}
}
} else {
/*
* There is no hash_table, which means that this module has not been
* initialized yet!
*/
rc = HANDLE_MANAGER_NOT_INITIALIZED;
}
LOG_PROC_EXIT_INT(rc);
return rc;
}
/*
* Given a handle, get the object and its tag.
*/
int translate_handle(object_handle_t handle, void * * object, object_type_t * object_type) {
int rc = HANDLE_MANAGER_NO_ERROR;
u_int32_t hash_index;
handle_entry_t * handle_entry;
LOG_PROC_ENTRY();
/* Has the hash_table list been created yet? */
if ( hash_table != NULL ) {
/* Get the index of the hash table entry that should have this handle.*/
hash_index = GET_HASH_INDEX(handle);
if (hash_index < HASH_TABLE_SIZE) {
/*
* Look for a handle_entry with a handle that matches the one
* given. */
handle_entry = hash_table[hash_index].head;
while ((handle_entry != NULL) && (handle_entry->handle != handle)) {
handle_entry = handle_entry->next;
}
/*
* If a matching handle entry was found, return the information
* stored in the handle entry.
*/
if (handle_entry != NULL) {
*object = handle_entry->object;
*object_type = handle_entry->type;
} else {
/* Bad handle. It was not found in the table. */
rc = HANDLE_MANAGER_BAD_HANDLE;
}
} else {
/*
* The handle is not valid. Its index goes beyond the end of the
* hash table.
*/
rc = HANDLE_MANAGER_BAD_HANDLE;
}
} else {
/*
* There is no hash_table, which means that this module has not been
* initialized yet!
*/
rc = HANDLE_MANAGER_NOT_INITIALIZED;
}
LOG_PROC_EXIT_INT(rc);
return rc;
}
|