2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Written by Kern Sibbald, MMIV
28 * Written by Kern Sibbald, MMIV
32 /* ========================================================================
34 * Hash table class -- htable
39 * BIG_MALLOC is to provide a large malloc service to htable
44 * Loop var through each member of table
47 #define foreach_htable(var, tbl) \
48 for((var)=(typeof(var))((tbl)->first()); \
50 (var)=(typeof(var))((tbl)->next()))
52 #define foreach_htable(var, tbl) \
53 for((*((void **)&(var))=(void *)((tbl)->first())); \
55 (*((void **)&(var))=(void *)((tbl)->next())))
59 char *key; /* char key */
60 uint64_t ikey; /* integer key */
64 void *next; /* next hash item */
65 uint64_t hash; /* 64 bit hash for this key */
66 union key_val key; /* key value this key */
67 bool is_ikey; /* set if integer key */
71 struct h_mem *next; /* next buffer */
72 char *mem; /* memory pointer */
73 int64_t rem; /* remaining bytes in big_buffer */
74 char first[1]; /* first byte */
77 class htable : public SMARTALLOC {
78 hlink **table; /* hash table */
79 uint64_t hash; /* temp storage */
80 uint64_t total_size; /* total bytes malloced */
81 int loffset; /* link offset in item */
82 uint32_t num_items; /* current number of items */
83 uint32_t max_items; /* maximum items before growing */
84 uint32_t buckets; /* size of hash table */
85 uint32_t index; /* temp storage */
86 uint32_t mask; /* "remainder" mask */
87 uint32_t rshift; /* amount to shift down */
88 hlink *walkptr; /* table walk pointer */
89 uint32_t walk_index; /* table walk index */
90 uint32_t blocks; /* blocks malloced */
92 struct h_mem *mem_block; /* malloc'ed memory block chain */
93 void malloc_big_buf(int size); /* Get a big buffer */
95 void hash_index(char *key); /* produce hash key,index */
96 void hash_index(uint64_t ikey); /* produce hash key,index */
97 void grow_table(); /* grow the table */
100 htable(void *item, void *link, int tsize = 31);
101 ~htable() { destroy(); }
102 void init(void *item, void *link, int tsize = 31);
103 bool insert(char *key, void *item); /* char key */
104 bool insert(uint64_t ikey, void *item); /* 64 bit key */
105 void *lookup(char *key); /* char key */
106 void *lookup(uint64_t ikey); /* 64 bit key */
107 void *first(); /* get first item in table */
108 void *next(); /* get next item in table */
110 void stats(); /* print stats about the table */
111 uint32_t size(); /* return size of table */
112 char *hash_malloc(int size); /* malloc bytes for a hash entry */
114 void hash_big_free(); /* free all hash allocated big buffers */