2004-02-11 01:15:55 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-02-11 01:15:55 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2004-02-11 01:15:55 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2005-07-04 03:31:27 +04:00
|
|
|
#include "opal/util/output.h"
|
2005-07-03 20:22:16 +04:00
|
|
|
#include "opal/class/opal_list.h"
|
2005-08-13 00:46:25 +04:00
|
|
|
#include "opal/class/opal_hash_table.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
- Check, whether the compiler supports __builtin_clz (count leading
zeroes);
if so, use it for bit-operations like opal_cube_dim and opal_hibit.
Implement two versions of power-of-two.
In case of opal_next_poweroftwo, this reduces the average execution
time from 83 cycles to 4 cycles (Intel Nehalem, icc, -O2, inlining,
measured rdtsc, with loop over 2^27 values).
Numbers for other functions are similar (but of course heavily depend
on the usage, e.g. opal_hibit() with a start of 4 does not save
much). The bsr instruction on AMD Opteron is also not as fast.
- Replace various places where the next power-of-two is computed.
Tested on Intel Nehalem Cluster with openib, compilers GNU-4.6.1 and
Intel-12.0.4 using mpi_testsuite -t "Collective" with 128 processes.
This commit was SVN r25270.
2011-10-12 02:49:01 +04:00
|
|
|
#include "opal/util/bit_ops.h"
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
/*
|
2005-07-03 20:52:32 +04:00
|
|
|
* opal_hash_table_t
|
2004-02-11 01:15:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define HASH_MULTIPLIER 31
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static void opal_hash_table_construct(opal_hash_table_t* ht);
|
|
|
|
static void opal_hash_table_destruct(opal_hash_table_t* ht);
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
|
2004-03-03 19:44:41 +03:00
|
|
|
OBJ_CLASS_INSTANCE(
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_t,
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t,
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_construct,
|
|
|
|
opal_hash_table_destruct
|
2004-03-03 19:44:41 +03:00
|
|
|
);
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static void opal_hash_table_construct(opal_hash_table_t* ht)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
OBJ_CONSTRUCT(&ht->ht_nodes, opal_list_t);
|
2004-02-11 01:15:55 +03:00
|
|
|
ht->ht_table = NULL;
|
|
|
|
ht->ht_table_size = 0;
|
|
|
|
ht->ht_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static void opal_hash_table_destruct(opal_hash_table_t* ht)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_remove_all(ht);
|
2004-09-17 03:51:30 +04:00
|
|
|
for(i=0; i<ht->ht_table_size; i++) {
|
2004-02-11 01:15:55 +03:00
|
|
|
OBJ_DESTRUCT(ht->ht_table+i);
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
|
|
|
if(NULL != ht->ht_table) {
|
2004-02-11 01:15:55 +03:00
|
|
|
free(ht->ht_table);
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-02-11 01:15:55 +03:00
|
|
|
OBJ_DESTRUCT(&ht->ht_nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_init(opal_hash_table_t* ht, size_t table_size)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
- Check, whether the compiler supports __builtin_clz (count leading
zeroes);
if so, use it for bit-operations like opal_cube_dim and opal_hibit.
Implement two versions of power-of-two.
In case of opal_next_poweroftwo, this reduces the average execution
time from 83 cycles to 4 cycles (Intel Nehalem, icc, -O2, inlining,
measured rdtsc, with loop over 2^27 values).
Numbers for other functions are similar (but of course heavily depend
on the usage, e.g. opal_hibit() with a start of 4 does not save
much). The bsr instruction on AMD Opteron is also not as fast.
- Replace various places where the next power-of-two is computed.
Tested on Intel Nehalem Cluster with openib, compilers GNU-4.6.1 and
Intel-12.0.4 using mpi_testsuite -t "Collective" with 128 processes.
This commit was SVN r25270.
2011-10-12 02:49:01 +04:00
|
|
|
size_t power2 = opal_next_poweroftwo (table_size);
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
ht->ht_mask = power2-1;
|
2005-07-03 20:22:16 +04:00
|
|
|
ht->ht_table = (opal_list_t *)malloc(power2 * sizeof(opal_list_t));
|
2004-09-17 03:51:30 +04:00
|
|
|
if(NULL == ht->ht_table) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-03-03 19:44:41 +03:00
|
|
|
for(i=ht->ht_table_size; i<power2; i++) {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t* list = ht->ht_table+i;
|
|
|
|
OBJ_CONSTRUCT(list, opal_list_t);
|
2004-03-03 19:44:41 +03:00
|
|
|
}
|
2004-02-11 01:15:55 +03:00
|
|
|
ht->ht_table_size = power2;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_remove_all(opal_hash_table_t* ht)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for(i=0; i<ht->ht_table_size; i++) {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t* list = ht->ht_table+i;
|
|
|
|
while(opal_list_get_size(list)) {
|
|
|
|
opal_list_item_t *item = opal_list_remove_first(list);
|
2004-02-11 01:15:55 +03:00
|
|
|
OBJ_RELEASE(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
while(opal_list_get_size(&ht->ht_nodes)) {
|
|
|
|
opal_list_item_t* item = opal_list_remove_first(&ht->ht_nodes);
|
2004-02-11 01:15:55 +03:00
|
|
|
OBJ_RELEASE(item);
|
|
|
|
}
|
|
|
|
ht->ht_size = 0;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
2004-09-17 03:51:30 +04:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2004-02-11 01:15:55 +03:00
|
|
|
/*
|
2005-07-03 20:52:32 +04:00
|
|
|
* opal_uint32_hash_node_t
|
2004-02-11 01:15:55 +03:00
|
|
|
*/
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
struct opal_uint32_hash_node_t
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t super;
|
2004-02-11 01:15:55 +03:00
|
|
|
uint32_t hn_key;
|
|
|
|
void *hn_value;
|
|
|
|
};
|
2005-07-03 20:52:32 +04:00
|
|
|
typedef struct opal_uint32_hash_node_t opal_uint32_hash_node_t;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(opal_uint32_hash_node_t,
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t,
|
2004-09-17 03:51:30 +04:00
|
|
|
NULL,
|
|
|
|
NULL);
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_get_value_uint32(opal_hash_table_t* ht, uint32_t key,
|
2004-10-29 22:38:10 +04:00
|
|
|
void **ptr)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint32_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_get_value_uint32:"
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key == key) {
|
2004-10-29 22:38:10 +04:00
|
|
|
*ptr = node->hn_value;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_set_value_uint32(opal_hash_table_t* ht,
|
2004-03-24 04:54:59 +03:00
|
|
|
uint32_t key, void* value)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint32_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_set_value_uint32:"
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key == key) {
|
|
|
|
node->hn_value = value;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
node = (opal_uint32_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
|
2004-02-11 01:15:55 +03:00
|
|
|
if(NULL == node) {
|
2005-07-03 20:52:32 +04:00
|
|
|
node = OBJ_NEW(opal_uint32_hash_node_t);
|
2004-02-11 01:15:55 +03:00
|
|
|
if(NULL == node)
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
node->hn_key = key;
|
|
|
|
node->hn_value = value;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_append(list, (opal_list_item_t*)node);
|
2004-02-11 01:15:55 +03:00
|
|
|
ht->ht_size++;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_remove_value_uint32(opal_hash_table_t* ht, uint32_t key)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint32_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_remove_value_uint32:"
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_uint32_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_uint32_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_uint32_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key == key) {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_remove_item(list, (opal_list_item_t*)node);
|
|
|
|
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
|
2004-02-11 01:15:55 +03:00
|
|
|
ht->ht_size--;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
2004-09-17 03:51:30 +04:00
|
|
|
/***************************************************************************/
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
/*
|
2005-07-03 20:52:32 +04:00
|
|
|
* opal_uint64_hash_node_t
|
2004-02-11 01:15:55 +03:00
|
|
|
*/
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
struct opal_uint64_hash_node_t
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t super;
|
2004-02-11 01:15:55 +03:00
|
|
|
uint64_t hn_key;
|
|
|
|
void* hn_value;
|
|
|
|
};
|
2005-07-03 20:52:32 +04:00
|
|
|
typedef struct opal_uint64_hash_node_t opal_uint64_hash_node_t;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(opal_uint64_hash_node_t,
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t,
|
2004-09-17 03:51:30 +04:00
|
|
|
NULL,
|
|
|
|
NULL);
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_get_value_uint64(opal_hash_table_t* ht, uint64_t key,
|
2004-10-29 22:38:10 +04:00
|
|
|
void **ptr)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint64_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_get_value_uint64:"
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key == key) {
|
2004-10-29 22:38:10 +04:00
|
|
|
*ptr = node->hn_value;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_set_value_uint64(opal_hash_table_t* ht,
|
2004-03-24 04:54:59 +03:00
|
|
|
uint64_t key, void* value)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint64_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_set_value_uint64:"
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key == key) {
|
|
|
|
node->hn_value = value;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
node = (opal_uint64_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
|
2004-02-11 01:15:55 +03:00
|
|
|
if(NULL == node) {
|
2005-07-03 20:52:32 +04:00
|
|
|
node = OBJ_NEW(opal_uint64_hash_node_t);
|
2004-09-17 03:51:30 +04:00
|
|
|
if(NULL == node) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
node->hn_key = key;
|
|
|
|
node->hn_value = value;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_append(list, (opal_list_item_t*)node);
|
2004-02-11 01:15:55 +03:00
|
|
|
ht->ht_size++;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_remove_value_uint64(opal_hash_table_t* ht, uint64_t key)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t* list = ht->ht_table + (key & ht->ht_mask);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint64_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_remove_value_uint64:"
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_uint64_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_uint64_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_uint64_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key == key) {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_remove_item(list, (opal_list_item_t*)node);
|
|
|
|
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
|
2004-02-11 01:15:55 +03:00
|
|
|
ht->ht_size--;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
2004-09-17 03:51:30 +04:00
|
|
|
/***************************************************************************/
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
/*
|
2005-07-03 20:52:32 +04:00
|
|
|
* opal_ptr_hash_node_t
|
2004-02-11 01:15:55 +03:00
|
|
|
*/
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
struct opal_ptr_hash_node_t
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t super;
|
2004-02-11 01:15:55 +03:00
|
|
|
void* hn_key;
|
|
|
|
size_t hn_key_size;
|
|
|
|
void* hn_value;
|
|
|
|
};
|
2005-07-03 20:52:32 +04:00
|
|
|
typedef struct opal_ptr_hash_node_t opal_ptr_hash_node_t;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static void opal_ptr_hash_node_construct(opal_ptr_hash_node_t* hn)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
|
|
|
hn->hn_key_size = 0;
|
|
|
|
hn->hn_key = NULL;
|
|
|
|
hn->hn_value = NULL;
|
|
|
|
}
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static void opal_ptr_hash_node_destruct(opal_ptr_hash_node_t* hn)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2004-09-17 03:51:30 +04:00
|
|
|
if(NULL != hn->hn_key) {
|
2004-02-11 01:15:55 +03:00
|
|
|
free(hn->hn_key);
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(opal_ptr_hash_node_t,
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t,
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_ptr_hash_node_construct,
|
|
|
|
opal_ptr_hash_node_destruct);
|
2004-02-11 01:15:55 +03:00
|
|
|
|
|
|
|
|
2005-07-04 04:16:24 +04:00
|
|
|
static inline uint32_t opal_hash_value(size_t mask, const void *key,
|
2006-08-22 00:11:12 +04:00
|
|
|
size_t keysize)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2006-08-22 00:11:12 +04:00
|
|
|
size_t h, i;
|
2004-02-11 01:15:55 +03:00
|
|
|
const unsigned char *p;
|
|
|
|
|
|
|
|
h = 0;
|
2004-10-18 20:55:20 +04:00
|
|
|
p = (const unsigned char *)key;
|
2004-02-11 01:15:55 +03:00
|
|
|
for (i = 0; i < keysize; i++, p++)
|
|
|
|
h = HASH_MULTIPLIER*h + *p;
|
2006-08-22 00:11:12 +04:00
|
|
|
return (uint32_t)(h & mask);
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_get_value_ptr(opal_hash_table_t* ht, const void* key,
|
2004-10-29 22:38:10 +04:00
|
|
|
size_t key_size, void **ptr)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-04 04:16:24 +04:00
|
|
|
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask, key,
|
2004-09-17 03:51:30 +04:00
|
|
|
key_size);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_ptr_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_get_value_ptr:"
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key_size == key_size &&
|
|
|
|
memcmp(node->hn_key, key, key_size) == 0) {
|
2004-10-29 22:38:10 +04:00
|
|
|
*ptr = node->hn_value;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_set_value_ptr(opal_hash_table_t* ht, const void* key,
|
2004-09-17 03:51:30 +04:00
|
|
|
size_t key_size, void* value)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-04 04:16:24 +04:00
|
|
|
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask, key,
|
2004-09-17 03:51:30 +04:00
|
|
|
key_size);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_ptr_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_set_value_ptr:"
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key_size == key_size &&
|
|
|
|
memcmp(node->hn_key, key, key_size) == 0) {
|
|
|
|
node->hn_value = value;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
node = (opal_ptr_hash_node_t*)opal_list_remove_first(&ht->ht_nodes);
|
2004-02-11 01:15:55 +03:00
|
|
|
if(NULL == node) {
|
2005-07-03 20:52:32 +04:00
|
|
|
node = OBJ_NEW(opal_ptr_hash_node_t);
|
2004-09-17 03:51:30 +04:00
|
|
|
if(NULL == node) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
node->hn_key = malloc(key_size);
|
|
|
|
node->hn_key_size = key_size;
|
|
|
|
node->hn_value = value;
|
|
|
|
memcpy(node->hn_key, key, key_size);
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_append(list, (opal_list_item_t*)node);
|
2004-02-11 01:15:55 +03:00
|
|
|
ht->ht_size++;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
int opal_hash_table_remove_value_ptr(opal_hash_table_t* ht,
|
2004-09-17 03:51:30 +04:00
|
|
|
const void* key, size_t key_size)
|
2004-02-11 01:15:55 +03:00
|
|
|
{
|
2005-07-04 04:16:24 +04:00
|
|
|
opal_list_t* list = ht->ht_table + opal_hash_value(ht->ht_mask,
|
2004-09-17 03:51:30 +04:00
|
|
|
key, key_size);
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_ptr_hash_node_t *node;
|
2004-02-11 01:15:55 +03:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2004-02-11 01:15:55 +03:00
|
|
|
if(ht->ht_table_size == 0) {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "opal_hash_table_remove_value_ptr: "
|
2005-07-03 20:52:32 +04:00
|
|
|
"opal_hash_table_init() has not been called");
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-03 20:52:32 +04:00
|
|
|
for(node = (opal_ptr_hash_node_t*)opal_list_get_first(list);
|
|
|
|
node != (opal_ptr_hash_node_t*)opal_list_get_end(list);
|
|
|
|
node = (opal_ptr_hash_node_t*)opal_list_get_next(node)) {
|
2004-02-11 01:15:55 +03:00
|
|
|
if (node->hn_key_size == key_size &&
|
|
|
|
memcmp(node->hn_key, key, key_size) == 0) {
|
|
|
|
free(node->hn_key);
|
|
|
|
node->hn_key = NULL;
|
|
|
|
node->hn_key_size = 0;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_remove_item(list, (opal_list_item_t*)node);
|
|
|
|
opal_list_append(&ht->ht_nodes, (opal_list_item_t*)node);
|
2004-02-11 01:15:55 +03:00
|
|
|
ht->ht_size--;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2004-03-24 04:54:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_get_first_key_uint32(opal_hash_table_t *ht, uint32_t *key,
|
2004-09-17 03:51:30 +04:00
|
|
|
void **value, void **node)
|
2004-03-24 04:54:59 +03:00
|
|
|
{
|
2004-09-17 03:51:30 +04:00
|
|
|
size_t i;
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint32_hash_node_t *list_node;
|
2004-05-30 20:21:04 +04:00
|
|
|
|
2004-09-17 03:51:30 +04:00
|
|
|
/* Go through all the lists and return the first element off the
|
|
|
|
first non-empty list */
|
|
|
|
|
|
|
|
for (i = 0; i < ht->ht_table_size; ++i) {
|
2005-07-03 20:22:16 +04:00
|
|
|
if (opal_list_get_size(ht->ht_table + i) > 0) {
|
2005-07-03 20:52:32 +04:00
|
|
|
list_node = (opal_uint32_hash_node_t*)
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_get_first(ht->ht_table + i);
|
2004-09-17 03:51:30 +04:00
|
|
|
*node = list_node;
|
|
|
|
*key = list_node->hn_key;
|
|
|
|
*value = list_node->hn_value;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-05-30 20:21:04 +04:00
|
|
|
}
|
|
|
|
|
2004-09-17 03:51:30 +04:00
|
|
|
/* The hash table is empty */
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-03-24 04:54:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_get_next_key_uint32(opal_hash_table_t *ht, uint32_t *key,
|
2004-09-17 03:51:30 +04:00
|
|
|
void **value, void *in_node,
|
|
|
|
void **out_node)
|
2004-03-24 04:54:59 +03:00
|
|
|
{
|
2004-09-17 03:51:30 +04:00
|
|
|
size_t i;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t *list;
|
|
|
|
opal_list_item_t *item;
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint32_hash_node_t *next;
|
2004-09-17 03:51:30 +04:00
|
|
|
|
|
|
|
/* Try to simply get the next value in the list. If there isn't
|
|
|
|
one, find the next non-empty list and take the first value */
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
next = (opal_uint32_hash_node_t*) in_node;
|
2004-09-17 03:51:30 +04:00
|
|
|
list = ht->ht_table + (next->hn_key & ht->ht_mask);
|
2005-07-03 20:22:16 +04:00
|
|
|
item = opal_list_get_next(next);
|
|
|
|
if (opal_list_get_end(list) == item) {
|
2004-09-17 03:51:30 +04:00
|
|
|
item = NULL;
|
|
|
|
for (i = (list - ht->ht_table) + 1; i < ht->ht_table_size; ++i) {
|
2005-07-03 20:22:16 +04:00
|
|
|
if (opal_list_get_size(ht->ht_table + i) > 0) {
|
|
|
|
item = opal_list_get_first(ht->ht_table + i);
|
2004-09-17 03:51:30 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we didn't find another non-empty list after this one,
|
|
|
|
then we're at the end of the hash table */
|
|
|
|
|
|
|
|
if (NULL == item) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-03-24 04:54:59 +03:00
|
|
|
}
|
2004-09-17 03:51:30 +04:00
|
|
|
|
|
|
|
/* We found it. Save the values (use "next" to avoid some
|
|
|
|
typecasting) */
|
|
|
|
|
2004-10-21 02:56:33 +04:00
|
|
|
*out_node = (void *) item;
|
2005-07-03 20:52:32 +04:00
|
|
|
next = (opal_uint32_hash_node_t *) *out_node;
|
2004-09-17 03:51:30 +04:00
|
|
|
*key = next->hn_key;
|
|
|
|
*value = next->hn_value;
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-02-11 01:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-24 04:54:59 +03:00
|
|
|
int
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_get_first_key_uint64(opal_hash_table_t *ht, uint64_t *key,
|
2004-09-17 03:51:30 +04:00
|
|
|
void **value, void **node)
|
2004-03-24 04:54:59 +03:00
|
|
|
{
|
2004-09-17 03:51:30 +04:00
|
|
|
size_t i;
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint64_hash_node_t *list_node;
|
2004-05-30 20:21:04 +04:00
|
|
|
|
2004-09-17 03:51:30 +04:00
|
|
|
/* Go through all the lists and return the first element off the
|
|
|
|
first non-empty list */
|
|
|
|
|
|
|
|
for (i = 0; i < ht->ht_table_size; ++i) {
|
2005-07-03 20:22:16 +04:00
|
|
|
if (opal_list_get_size(ht->ht_table + i) > 0) {
|
2005-07-03 20:52:32 +04:00
|
|
|
list_node = (opal_uint64_hash_node_t*)
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_get_first(ht->ht_table + i);
|
2004-09-17 03:51:30 +04:00
|
|
|
*node = list_node;
|
|
|
|
*key = list_node->hn_key;
|
|
|
|
*value = list_node->hn_value;
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-05-30 20:21:04 +04:00
|
|
|
}
|
|
|
|
|
2004-09-17 03:51:30 +04:00
|
|
|
/* The hash table is empty */
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-03-24 04:54:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_hash_table_get_next_key_uint64(opal_hash_table_t *ht, uint64_t *key,
|
2004-09-17 03:51:30 +04:00
|
|
|
void **value, void *in_node,
|
|
|
|
void **out_node)
|
2004-03-24 04:54:59 +03:00
|
|
|
{
|
2004-09-17 03:51:30 +04:00
|
|
|
size_t i;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t *list;
|
|
|
|
opal_list_item_t *item;
|
2005-07-03 20:52:32 +04:00
|
|
|
opal_uint64_hash_node_t *next;
|
2004-09-17 03:51:30 +04:00
|
|
|
|
|
|
|
/* Try to simply get the next value in the list. If there isn't
|
|
|
|
one, find the next non-empty list and take the first value */
|
|
|
|
|
2005-07-03 20:52:32 +04:00
|
|
|
next = (opal_uint64_hash_node_t*) in_node;
|
2004-09-17 03:51:30 +04:00
|
|
|
list = ht->ht_table + (next->hn_key & ht->ht_mask);
|
2005-07-03 20:22:16 +04:00
|
|
|
item = opal_list_get_next(next);
|
|
|
|
if (opal_list_get_end(list) == item) {
|
2004-09-17 03:51:30 +04:00
|
|
|
item = NULL;
|
|
|
|
for (i = (list - ht->ht_table) + 1; i < ht->ht_table_size; ++i) {
|
2005-07-03 20:22:16 +04:00
|
|
|
if (opal_list_get_size(ht->ht_table + i) > 0) {
|
|
|
|
item = opal_list_get_first(ht->ht_table + i);
|
2004-09-17 03:51:30 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we didn't find another non-empty list after this one,
|
|
|
|
then we're at the end of the hash table */
|
|
|
|
|
|
|
|
if (NULL == item) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-09-17 03:51:30 +04:00
|
|
|
}
|
2004-03-24 04:54:59 +03:00
|
|
|
}
|
|
|
|
|
2004-09-17 03:51:30 +04:00
|
|
|
/* We found it. Save the values (use "next" to avoid some
|
|
|
|
typecasting) */
|
|
|
|
|
2004-10-21 02:56:33 +04:00
|
|
|
*out_node = (void *) item;
|
2005-07-03 20:52:32 +04:00
|
|
|
next = (opal_uint64_hash_node_t *) *out_node;
|
2004-09-17 03:51:30 +04:00
|
|
|
*key = next->hn_key;
|
|
|
|
*value = next->hn_value;
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-03-24 04:54:59 +03:00
|
|
|
}
|