From 719a441ac92f7e1bdea7377a90b29981edd0e82e Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 10 Jan 2004 12:41:52 +0000 Subject: [PATCH] Added HASH_AUTO_POOL option. --- lib/hash-test.c | 3 ++- lib/hashtable.h | 42 +++++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/hash-test.c b/lib/hash-test.c index 2f835613..66a07faa 100644 --- a/lib/hash-test.c +++ b/lib/hash-test.c @@ -69,7 +69,7 @@ static void test(void) log(L_INFO, "OK"); } -#elif 0 +#elif 1 /* TEST 2: external strings */ @@ -82,6 +82,7 @@ struct node { #define HASH_PREFIX(x) test_##x #define HASH_KEY_STRING key #define HASH_NOCASE +#define HASH_AUTO_POOL 4096 #define HASH_WANT_FIND #define HASH_WANT_NEW diff --git a/lib/hashtable.h b/lib/hashtable.h index e12f3772..49d45ba7 100644 --- a/lib/hashtable.h +++ b/lib/hashtable.h @@ -1,7 +1,7 @@ /* * Sherlock Library -- Universal Hash Table * - * (c) 2002 Martin Mares + * (c) 2002--2004 Martin Mares * (c) 2002 Robert Spalek * * This software may be freely distributed and used according to the terms @@ -88,6 +88,7 @@ * HASH_ATOMIC_TYPE=t Atomic values are of type `t' instead of int. * HASH_USE_POOL=pool Allocate all nodes from given mempool. * Collides with delete/remove functions. + * HASH_AUTO_POOL=size Create a pool of the given block size automatically. * * You also get a iterator macro at no extra charge: * @@ -260,21 +261,33 @@ static inline void P(init_data) (P(node) *n UNUSED) #include -#ifndef HASH_GIVE_ALLOC -#ifdef HASH_USE_POOL - -static inline void * P(alloc) (unsigned int size) -{ return mp_alloc_fast(HASH_USE_POOL, size); } +#ifdef HASH_GIVE_ALLOC +/* If the caller has requested to use his own allocation functions, do so */ +static inline void * P(init_alloc) (void) { } +static inline void * P(cleanup_alloc) (void) { } + +#elif defined(HASH_USE_POOL) +/* If the caller has requested to use his mempool, do so */ +#include "lib/pools.h" +static inline void * P(alloc) (unsigned int size) { return mp_alloc_fast(HASH_USE_POOL, size); } +static inline void * P(init_alloc) (void) { } +static inline void * P(cleanup_alloc) (void) { } + +#elif defined(HASH_AUTO_POOL) +/* Use our own pools */ +#include "lib/pools.h" +static struct mempool *P(pool); +static inline void * P(alloc) (unsigned int size) { return mp_alloc_fast(P(pool), size); } +static inline void P(init_alloc) (void) { P(pool) = mp_new(HASH_AUTO_POOL); } +static inline void P(cleanup_alloc) (void) { mp_delete(P(pool)); } #else +/* The default allocation method */ +static inline void * P(alloc) (unsigned int size) { return xmalloc(size); } +static inline void P(free) (void *x) { xfree(x); } +static inline void * P(init_alloc) (void) { } +static inline void * P(cleanup_alloc) (void) { } -static inline void * P(alloc) (unsigned int size) -{ return xmalloc(size); } - -static inline void P(free) (void *x) -{ xfree(x); } - -#endif #endif #ifndef HASH_DEFAULT_SIZE @@ -312,6 +325,7 @@ static void P(init) (void) T.hash_hard_max = 1 << 28; #endif P(alloc_table)(); + P(init_alloc)(); } #ifdef HASH_WANT_CLEANUP @@ -328,6 +342,7 @@ static void P(cleanup) (void) P(free)(b); } #endif + P(cleanup_alloc)(); xfree(T.ht); } #endif @@ -550,6 +565,7 @@ do { \ #undef HASH_NODE #undef HASH_PREFIX #undef HASH_USE_POOL +#undef HASH_AUTO_POOL #undef HASH_WANT_CLEANUP #undef HASH_WANT_DELETE #undef HASH_WANT_FIND -- 2.39.5