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