]> mj.ucw.cz Git - libucw.git/blobdiff - lib/hashtable.h
Fixed parsing of bin/config output (by TomHol in rel-3-6-1).
[libucw.git] / lib / hashtable.h
index 75276fcef562d07ae2fd6ab4aeed805d88bda888..486fe30bb83e5a00b01183c7866a78c2b6794778 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *     Sherlock Library -- Universal Hash Table
+ *     UCW Library -- Universal Hash Table
  *
  *     (c) 2002--2004 Martin Mares <mj@ucw.cz>
  *     (c) 2002 Robert Spalek <robert@ucw.cz>
@@ -90,6 +90,7 @@
  *                     deallocation is not supported by mempools, so delete/remove
  *                     will leak pool memory.
  *  HASH_AUTO_POOL=size        Create a pool of the given block size automatically.
+ *  HASH_ZERO_FILL     New entries should be initialized to all zeroes.
  *  HASH_TABLE_ALLOC   The hash table itself will be allocated and freed using
  *                     the same allocation functions as the nodes instead of
  *                     the default xmalloc().
  *  undef'd.
  */
 
-#ifndef _SHERLOCK_HASHFUNC_H
+#ifndef _UCW_HASHFUNC_H
 #include "lib/hashfunc.h"
 #endif
 
@@ -150,6 +151,9 @@ struct P(table) {
   uns hash_size;
   uns hash_count, hash_max, hash_min, hash_hard_max;
   P(bucket) **ht;
+#ifdef HASH_AUTO_POOL
+  struct mempool *pool;
+#endif
 };
 
 #ifdef HASH_TABLE_DYNAMIC
@@ -185,7 +189,7 @@ struct P(table) P(table);
 #ifndef HASH_GIVE_HASHFN
 #  define HASH_GIVE_HASHFN
    static inline int P(hash) (TAUC HASH_ATOMIC_TYPE x)
-   { return hash_int(x); }
+   { return ((sizeof(x) <= 4) ? hash_u32(x) : hash_u64(x)); }
 #endif
 
 #ifndef HASH_GIVE_EQ
@@ -304,11 +308,11 @@ static inline void P(cleanup_alloc) (TAU) { }
 #elif defined(HASH_AUTO_POOL)
 /* Use our own pools */
 #include "lib/mempool.h"
-static struct mempool *P(pool);
-static inline void * P(alloc) (TAUC unsigned int size) { return mp_alloc_fast(P(pool), size); }
+static inline void * P(alloc) (TAUC unsigned int size) { return mp_alloc_fast(T.pool, size); }
 static inline void P(free) (TAUC void *x UNUSED) { }
-static inline void P(init_alloc) (TAU) { P(pool) = mp_new(HASH_AUTO_POOL); }
-static inline void P(cleanup_alloc) (TAU) { mp_delete(P(pool)); }
+static inline void P(init_alloc) (TAU) { T.pool = mp_new(HASH_AUTO_POOL); }
+static inline void P(cleanup_alloc) (TAU) { mp_delete(T.pool); }
+#define HASH_USE_POOL
 
 #else
 /* The default allocation method */
@@ -335,6 +339,17 @@ static inline void P(table_free) (TAUC void *x) { xfree(x); }
 #define HASH_FN_BITS 32
 #endif
 
+#ifdef HASH_ZERO_FILL
+static inline void * P(new_bucket)(TAUC uns size)
+{
+  byte *buck = P(alloc)(TTC size);
+  bzero(buck, size);
+  return buck;
+}
+#else
+static inline void * P(new_bucket)(TAUC uns size) { return P(alloc)(TTC size); }
+#endif
+
 /* Now the operations */
 
 static void P(alloc_table) (TAU)
@@ -468,7 +483,7 @@ static P(node) * P(new) (TAC HASH_KEY_DECL)
 
   h0 = P(hash) (TTC HASH_KEY( ));
   h = h0 % T.hash_size;
-  b = P(alloc) (TTC sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
+  b = P(new_bucket) (TTC sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
   b->next = T.ht[h];
   T.ht[h] = b;
 #ifndef HASH_CONSERVE_SPACE
@@ -499,7 +514,7 @@ static P(node) * P(lookup) (TAC HASH_KEY_DECL)
        return &b->n;
     }
 
-  b = P(alloc) (TTC sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
+  b = P(new_bucket) (TTC sizeof(struct P(bucket)) + HASH_EXTRA_SIZE(HASH_KEY( )));
   b->next = T.ht[h];
   T.ht[h] = b;
 #ifndef HASH_CONSERVE_SPACE
@@ -583,7 +598,7 @@ do {                                                                                        \
 #undef TA
 #undef TAC
 #undef TAU
-#undef TACU
+#undef TAUC
 #undef TT
 #undef TTC
 
@@ -618,3 +633,4 @@ do {                                                                                        \
 #undef HASH_WANT_REMOVE
 #undef HASH_TABLE_ALLOC
 #undef HASH_TABLE_DYNAMIC
+#undef HASH_ZERO_FILL