2
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3
* University Research and Technology
4
* Corporation. All rights reserved.
5
* Copyright (c) 2004-2005 The University of Tennessee and The University
6
* of Tennessee Research Foundation. All rights
8
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9
* University of Stuttgart. All rights reserved.
10
* Copyright (c) 2004-2005 The Regents of the University of California.
11
* All rights reserved.
14
* Additional copyrights may follow
19
#include "opal_config.h"
24
#include "opal/util/output.h"
25
#include "opal/class/opal_list.h"
26
#include "opal/class/opal_hash_table.h"
27
#include "opal/constants.h"
33
#define HASH_MULTIPLIER 31
35
static void opal_hash_table_construct(opal_hash_table_t* ht);
36
static void opal_hash_table_destruct(opal_hash_table_t* ht);
42
opal_hash_table_construct,
43
opal_hash_table_destruct
47
static void opal_hash_table_construct(opal_hash_table_t* ht)
49
OBJ_CONSTRUCT(&ht->ht_nodes, opal_list_t);
51
ht->ht_table_size = 0;
56
static void opal_hash_table_destruct(opal_hash_table_t* ht)
59
opal_hash_table_remove_all(ht);
60
for(i=0; i<ht->ht_table_size; i++) {
61
OBJ_DESTRUCT(ht->ht_table+i);
63
if(NULL != ht->ht_table) {
66
OBJ_DESTRUCT(&ht->ht_nodes);
70
int opal_hash_table_init(opal_hash_table_t* ht, size_t table_size)
74
size_t tmp = table_size;
80
ht->ht_mask = power2-1;
81
ht->ht_table = (opal_list_t *)malloc(power2 * sizeof(opal_list_t));
82
if(NULL == ht->ht_table) {
83
return OPAL_ERR_OUT_OF_RESOURCE;
85
for(i=ht->ht_table_size; i<power2; i++) {
86
opal_list_t* list = ht->ht_table+i;
87
OBJ_CONSTRUCT(list, opal_list_t);
89
ht->ht_table_size = power2;
93
int opal_hash_table_remove_all(opal_hash_table_t* ht)
96
for(i=0; i<ht->ht_table_size; i++) {
97
opal_list_t* list = ht->ht_table+i;
98
while(opal_list_get_size(list)) {
99
opal_list_item_t *item = opal_list_remove_first(list);
104
while(opal_list_get_size(&ht->ht_nodes)) {
105
opal_list_item_t* item = opal_list_remove_first(&ht->ht_nodes);
112
/***************************************************************************/
115
* opal_uint32_hash_node_t
118
struct opal_uint32_hash_node_t
120
opal_list_item_t super;
124
typedef struct opal_uint32_hash_node_t opal_uint32_hash_node_t;
126
static OBJ_CLASS_INSTANCE(opal_uint32_hash_node_t,
132
int opal_hash_table_get_value_uint32(opal_hash_table_t* ht, uint32_t key,
135
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
136
opal_uint32_hash_node_t *node;
138
#if OMPI_ENABLE_DEBUG
139
if(ht->ht_table_size == 0) {
140
opal_output(0, "opal_hash_table_get_value_uint32:"
141
"opal_hash_table_init() has not been called");
145
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
146
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
147
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
148
if (node->hn_key == key) {
149
*ptr = node->hn_value;
153
return OPAL_ERR_NOT_FOUND;
157
int opal_hash_table_set_value_uint32(opal_hash_table_t* ht,
158
uint32_t key, void* value)
160
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
161
opal_uint32_hash_node_t *node;
163
#if OMPI_ENABLE_DEBUG
164
if(ht->ht_table_size == 0) {
165
opal_output(0, "opal_hash_table_set_value_uint32:"
166
"opal_hash_table_init() has not been called");
167
return OPAL_ERR_BAD_PARAM;
170
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
171
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
172
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
173
if (node->hn_key == key) {
174
node->hn_value = value;
179
node = (opal_uint32_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
181
node = OBJ_NEW(opal_uint32_hash_node_t);
183
return OPAL_ERR_OUT_OF_RESOURCE;
186
node->hn_value = value;
187
opal_list_append(list, (opal_list_item_t*)node);
193
int opal_hash_table_remove_value_uint32(opal_hash_table_t* ht, uint32_t key)
195
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
196
opal_uint32_hash_node_t *node;
198
#if OMPI_ENABLE_DEBUG
199
if(ht->ht_table_size == 0) {
200
opal_output(0, "opal_hash_table_remove_value_uint32:"
201
"opal_hash_table_init() has not been called");
202
return OPAL_ERR_BAD_PARAM;
205
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
206
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
207
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
208
if (node->hn_key == key) {
209
opal_list_remove_item(list, (opal_list_item_t*)node);
210
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
215
return OPAL_ERR_NOT_FOUND;
218
/***************************************************************************/
221
* opal_uint64_hash_node_t
224
struct opal_uint64_hash_node_t
226
opal_list_item_t super;
230
typedef struct opal_uint64_hash_node_t opal_uint64_hash_node_t;
232
static OBJ_CLASS_INSTANCE(opal_uint64_hash_node_t,
238
int opal_hash_table_get_value_uint64(opal_hash_table_t* ht, uint64_t key,
241
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
242
opal_uint64_hash_node_t *node;
244
#if OMPI_ENABLE_DEBUG
245
if(ht->ht_table_size == 0) {
246
opal_output(0, "opal_hash_table_get_value_uint64:"
247
"opal_hash_table_init() has not been called");
251
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
252
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
253
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
254
if (node->hn_key == key) {
255
*ptr = node->hn_value;
259
return OPAL_ERR_NOT_FOUND;
263
int opal_hash_table_set_value_uint64(opal_hash_table_t* ht,
264
uint64_t key, void* value)
266
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
267
opal_uint64_hash_node_t *node;
269
#if OMPI_ENABLE_DEBUG
270
if(ht->ht_table_size == 0) {
271
opal_output(0, "opal_hash_table_set_value_uint64:"
272
"opal_hash_table_init() has not been called");
273
return OPAL_ERR_BAD_PARAM;
276
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
277
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
278
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
279
if (node->hn_key == key) {
280
node->hn_value = value;
285
node = (opal_uint64_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
287
node = OBJ_NEW(opal_uint64_hash_node_t);
289
return OPAL_ERR_OUT_OF_RESOURCE;
293
node->hn_value = value;
294
opal_list_append(list, (opal_list_item_t*)node);
300
int opal_hash_table_remove_value_uint64(opal_hash_table_t* ht, uint64_t key)
302
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
303
opal_uint64_hash_node_t *node;
305
#if OMPI_ENABLE_DEBUG
306
if(ht->ht_table_size == 0) {
307
opal_output(0, "opal_hash_table_remove_value_uint64:"
308
"opal_hash_table_init() has not been called");
309
return OPAL_ERR_BAD_PARAM;
312
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
313
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
314
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
315
if (node->hn_key == key) {
316
opal_list_remove_item(list, (opal_list_item_t*)node);
317
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
322
return OPAL_ERR_NOT_FOUND;
325
/***************************************************************************/
328
* opal_ptr_hash_node_t
331
struct opal_ptr_hash_node_t
333
opal_list_item_t super;
338
typedef struct opal_ptr_hash_node_t opal_ptr_hash_node_t;
340
static void opal_ptr_hash_node_construct(opal_ptr_hash_node_t* hn)
347
static void opal_ptr_hash_node_destruct(opal_ptr_hash_node_t* hn)
349
if(NULL != hn->hn_key) {
354
static OBJ_CLASS_INSTANCE(opal_ptr_hash_node_t,
356
opal_ptr_hash_node_construct,
357
opal_ptr_hash_node_destruct);
360
static inline uint32_t opal_hash_value(size_t mask, const void *key,
364
const unsigned char *p;
367
p = (const unsigned char *)key;
368
for (i = 0; i < keysize; i++, p++)
369
h = HASH_MULTIPLIER*h + *p;
373
int opal_hash_table_get_value_ptr(opal_hash_table_t* ht, const void* key,
374
size_t key_size, void **ptr)
376
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask, key,
378
opal_ptr_hash_node_t *node;
380
#if OMPI_ENABLE_DEBUG
381
if(ht->ht_table_size == 0) {
382
opal_output(0, "opal_hash_table_get_value_ptr:"
383
"opal_hash_table_init() has not been called");
387
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
388
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
389
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
390
if (node->hn_key_size == key_size &&
391
memcmp(node->hn_key, key, key_size) == 0) {
392
*ptr = node->hn_value;
396
return OPAL_ERR_NOT_FOUND;
400
int opal_hash_table_set_value_ptr(opal_hash_table_t* ht, const void* key,
401
size_t key_size, void* value)
403
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask, key,
405
opal_ptr_hash_node_t *node;
407
#if OMPI_ENABLE_DEBUG
408
if(ht->ht_table_size == 0) {
409
opal_output(0, "opal_hash_table_set_value_ptr:"
410
"opal_hash_table_init() has not been called");
411
return OPAL_ERR_BAD_PARAM;
414
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
415
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
416
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
417
if (node->hn_key_size == key_size &&
418
memcmp(node->hn_key, key, key_size) == 0) {
419
node->hn_value = value;
424
node = (opal_ptr_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
426
node = OBJ_NEW(opal_ptr_hash_node_t);
428
return OPAL_ERR_OUT_OF_RESOURCE;
431
node->hn_key = malloc(key_size);
432
node->hn_key_size = key_size;
433
node->hn_value = value;
434
memcpy(node->hn_key, key, key_size);
435
opal_list_append(list, (opal_list_item_t*)node);
441
int opal_hash_table_remove_value_ptr(opal_hash_table_t* ht,
442
const void* key, size_t key_size)
444
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask,
446
opal_ptr_hash_node_t *node;
448
#if OMPI_ENABLE_DEBUG
449
if(ht->ht_table_size == 0) {
450
opal_output(0, "opal_hash_table_remove_value_ptr: "
451
"opal_hash_table_init() has not been called");
452
return OPAL_ERR_BAD_PARAM;
455
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
456
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
457
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
458
if (node->hn_key_size == key_size &&
459
memcmp(node->hn_key, key, key_size) == 0) {
462
node->hn_key_size = 0;
463
opal_list_remove_item(list, (opal_list_item_t*)node);
464
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
469
return OPAL_ERR_NOT_FOUND;
474
opal_hash_table_get_first_key_uint32(opal_hash_table_t *ht, uint32_t *key,
475
void **value, void **node)
478
opal_uint32_hash_node_t *list_node;
480
/* Go through all the lists and return the first element off the
481
first non-empty list */
483
for (i = 0; i < ht->ht_table_size; ++i) {
484
if (opal_list_get_size(ht->ht_table + i) > 0) {
485
list_node = (opal_uint32_hash_node_t*)
486
opal_list_get_first(ht->ht_table + i);
488
*key = list_node->hn_key;
489
*value = list_node->hn_value;
494
/* The hash table is empty */
501
opal_hash_table_get_next_key_uint32(opal_hash_table_t *ht, uint32_t *key,
502
void **value, void *in_node,
507
opal_list_item_t *item;
508
opal_uint32_hash_node_t *next;
510
/* Try to simply get the next value in the list. If there isn't
511
one, find the next non-empty list and take the first value */
513
next = (opal_uint32_hash_node_t*) in_node;
514
list = ht->ht_table + (next->hn_key & ht->ht_mask);
515
item = opal_list_get_next(next);
516
if (opal_list_get_end(list) == item) {
518
for (i = (list - ht->ht_table) + 1; i < ht->ht_table_size; ++i) {
519
if (opal_list_get_size(ht->ht_table + i) > 0) {
520
item = opal_list_get_first(ht->ht_table + i);
525
/* If we didn't find another non-empty list after this one,
526
then we're at the end of the hash table */
533
/* We found it. Save the values (use "next" to avoid some
536
*out_node = (void *) item;
537
next = (opal_uint32_hash_node_t *) *out_node;
539
*value = next->hn_value;
546
opal_hash_table_get_first_key_uint64(opal_hash_table_t *ht, uint64_t *key,
547
void **value, void **node)
550
opal_uint64_hash_node_t *list_node;
552
/* Go through all the lists and return the first element off the
553
first non-empty list */
555
for (i = 0; i < ht->ht_table_size; ++i) {
556
if (opal_list_get_size(ht->ht_table + i) > 0) {
557
list_node = (opal_uint64_hash_node_t*)
558
opal_list_get_first(ht->ht_table + i);
560
*key = list_node->hn_key;
561
*value = list_node->hn_value;
566
/* The hash table is empty */
573
opal_hash_table_get_next_key_uint64(opal_hash_table_t *ht, uint64_t *key,
574
void **value, void *in_node,
579
opal_list_item_t *item;
580
opal_uint64_hash_node_t *next;
582
/* Try to simply get the next value in the list. If there isn't
583
one, find the next non-empty list and take the first value */
585
next = (opal_uint64_hash_node_t*) in_node;
586
list = ht->ht_table + (next->hn_key & ht->ht_mask);
587
item = opal_list_get_next(next);
588
if (opal_list_get_end(list) == item) {
590
for (i = (list - ht->ht_table) + 1; i < ht->ht_table_size; ++i) {
591
if (opal_list_get_size(ht->ht_table + i) > 0) {
592
item = opal_list_get_first(ht->ht_table + i);
597
/* If we didn't find another non-empty list after this one,
598
then we're at the end of the hash table */
605
/* We found it. Save the values (use "next" to avoid some
608
*out_node = (void *) item;
609
next = (opal_uint64_hash_node_t *) *out_node;
611
*value = next->hn_value;