diff options
author | Vijay Bellur <vijay@gluster.com> | 2009-12-02 06:08:56 +0000 |
---|---|---|
committer | Anand V. Avati <avati@dev.gluster.com> | 2009-12-02 11:00:03 -0800 |
commit | 85b587f07c754bd2399fb227c6ea79509502d38c (patch) | |
tree | ac36ed833b51a3b22d0a574c434aa8cf730610d1 /libglusterfs/src | |
parent | 3f8320c34ad07ec93956ac0211fb2ef658893747 (diff) |
Add support in rbthash to make use of user provided mempool.
Signed-off-by: Vijay Bellur <vijay@gluster.com>
Signed-off-by: Anand V. Avati <avati@dev.gluster.com>
BUG: 221 (stat prefetch implementation)
URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=221
Diffstat (limited to 'libglusterfs/src')
-rw-r--r-- | libglusterfs/src/rbthash.c | 52 | ||||
-rw-r--r-- | libglusterfs/src/rbthash.h | 5 |
2 files changed, 47 insertions, 10 deletions
diff --git a/libglusterfs/src/rbthash.c b/libglusterfs/src/rbthash.c index 3b0f4ed4f..1c6294872 100644 --- a/libglusterfs/src/rbthash.c +++ b/libglusterfs/src/rbthash.c @@ -78,9 +78,20 @@ err: return ret; } +/* + * rbthash_table_init - Initialize a RBT based hash table + * @buckets - Number of buckets in the hash table + * @hfunc - hashing function + * @dfunc - destroyer for data in the RBT + * @expected_entries - Number of entries expected in RBT. Mutually exclusive + * with entrypool. + * @entrypool - Memory pool in lieu of expected_entries. + */ rbthash_table_t * -rbthash_table_init (int buckets, rbt_hasher_t hfunc, rbt_data_destroyer_t dfunc) +rbthash_table_init (int buckets, rbt_hasher_t hfunc, + rbt_data_destroyer_t dfunc, unsigned long expected_entries, + struct mem_pool *entrypool) { rbthash_table_t *newtab = NULL; int ret = -1; @@ -90,6 +101,19 @@ rbthash_table_init (int buckets, rbt_hasher_t hfunc, rbt_data_destroyer_t dfunc) return NULL; } + if (!entrypool && !expected_entries) { + gf_log (GF_RBTHASH, GF_LOG_ERROR, + "Both mem-pool and expected entries not provided"); + return NULL; + } + + if (entrypool && expected_entries) { + gf_log (GF_RBTHASH, GF_LOG_ERROR, + "Both mem-pool and expected entries are provided"); + return NULL; + } + + newtab = CALLOC (1, sizeof (*newtab)); if (!newtab) return NULL; @@ -100,10 +124,17 @@ rbthash_table_init (int buckets, rbt_hasher_t hfunc, rbt_data_destroyer_t dfunc) goto free_newtab; } - newtab->entrypool = mem_pool_new (rbthash_entry_t, GF_RBTHASH_MEMPOOL); - if (!newtab->entrypool) { - gf_log (GF_RBTHASH, GF_LOG_ERROR,"Failed to allocate mem-pool"); - goto free_buckets; + if (expected_entries) { + newtab->entrypool = + mem_pool_new (rbthash_entry_t, expected_entries); + if (!newtab->entrypool) { + gf_log (GF_RBTHASH, GF_LOG_ERROR, + "Failed to allocate mem-pool"); + goto free_buckets; + } + newtab->pool_alloced = _gf_true; + } else { + newtab->entrypool = entrypool; } LOCK_INIT (&newtab->tablelock); @@ -112,13 +143,16 @@ rbthash_table_init (int buckets, rbt_hasher_t hfunc, rbt_data_destroyer_t dfunc) if (ret == -1) { gf_log (GF_RBTHASH, GF_LOG_ERROR, "Failed to init buckets"); - mem_pool_destroy (newtab->entrypool); - } else + if (newtab->pool_alloced) + mem_pool_destroy (newtab->entrypool); + } else { gf_log (GF_RBTHASH, GF_LOG_TRACE, "Inited hash table: buckets:" " %d", buckets); + } newtab->hashfunc = hfunc; newtab->dfunc = dfunc; + free_buckets: if (ret == -1) FREE (newtab->buckets); @@ -132,7 +166,6 @@ free_newtab: return newtab; } - rbthash_entry_t * rbthash_init_entry (rbthash_table_t *tbl, void *data, void *key, int keylen) { @@ -379,7 +412,8 @@ rbthash_table_destroy (rbthash_table_t *tbl) return; rbthash_table_destroy_buckets (tbl); - mem_pool_destroy (tbl->entrypool); + if (tbl->pool_alloced) + mem_pool_destroy (tbl->entrypool); FREE (tbl->buckets); FREE (tbl); diff --git a/libglusterfs/src/rbthash.h b/libglusterfs/src/rbthash.h index 5bfa6afd0..3f6d788f4 100644 --- a/libglusterfs/src/rbthash.h +++ b/libglusterfs/src/rbthash.h @@ -23,6 +23,7 @@ #include "locking.h" #include "mem-pool.h" #include "logging.h" +#include "common-utils.h" #include <pthread.h> @@ -52,11 +53,13 @@ typedef struct rbthash_table { struct rbthash_bucket *buckets; rbt_hasher_t hashfunc; rbt_data_destroyer_t dfunc; + gf_boolean_t pool_alloced; } rbthash_table_t; extern rbthash_table_t * rbthash_table_init (int buckets, rbt_hasher_t hfunc, - rbt_data_destroyer_t dfunc); + rbt_data_destroyer_t dfunc, unsigned long expected_entries, + struct mem_pool *entrypool); extern int rbthash_insert (rbthash_table_t *tbl, void *data, void *key, int keylen); |