slabhash.h revision 1.1.1.4.4.1 1 1.1 christos /*
2 1.1 christos * util/storage/slabhash.h - hashtable consisting of several smaller tables.
3 1.1 christos *
4 1.1 christos * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 1.1 christos *
6 1.1 christos * This software is open source.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * Redistributions of source code must retain the above copyright notice,
13 1.1 christos * this list of conditions and the following disclaimer.
14 1.1 christos *
15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice,
16 1.1 christos * this list of conditions and the following disclaimer in the documentation
17 1.1 christos * and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1 christos * be used to endorse or promote products derived from this software without
21 1.1 christos * specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos /**
37 1.1 christos * \file
38 1.1 christos *
39 1.1 christos * Hash table that consists of smaller hash tables.
40 1.1 christos * It cannot grow, but that gives it the ability to have multiple
41 1.1 christos * locks. Also this means there are multiple LRU lists.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #ifndef UTIL_STORAGE_SLABHASH_H
45 1.1 christos #define UTIL_STORAGE_SLABHASH_H
46 1.1 christos #include "util/storage/lruhash.h"
47 1.1 christos
48 1.1 christos /** default number of slabs */
49 1.1 christos #define HASH_DEFAULT_SLABS 4
50 1.1 christos
51 1.1 christos /**
52 1.1 christos * Hash table formed from several smaller ones.
53 1.1 christos * This results in a partitioned lruhash table, a 'slashtable'.
54 1.1 christos * None of the data inside the slabhash may be altered.
55 1.1 christos * Therefore, no locks are needed to access the structure.
56 1.1 christos */
57 1.1 christos struct slabhash {
58 1.1 christos /** the size of the array - must be power of 2 */
59 1.1 christos size_t size;
60 1.1 christos /** size bitmask - uses high bits. */
61 1.1 christos uint32_t mask;
62 1.1 christos /** shift right this many bits to get index into array. */
63 1.1 christos unsigned int shift;
64 1.1 christos /** lookup array of hash tables */
65 1.1 christos struct lruhash** array;
66 1.1 christos };
67 1.1 christos
68 1.1 christos /**
69 1.1 christos * Create new slabbed hash table.
70 1.1 christos * @param numtables: number of hash tables to use, other parameters used to
71 1.1 christos * initialize these smaller hashtables.
72 1.1 christos * @param start_size: size of hashtable array at start, must be power of 2.
73 1.1 christos * @param maxmem: maximum amount of memory this table is allowed to use.
74 1.1 christos * so every table gets maxmem/numtables to use for itself.
75 1.1 christos * @param sizefunc: calculates memory usage of entries.
76 1.1 christos * @param compfunc: compares entries, 0 on equality.
77 1.1 christos * @param delkeyfunc: deletes key.
78 1.1 christos * @param deldatafunc: deletes data.
79 1.1 christos * @param arg: user argument that is passed to user function calls.
80 1.1 christos * @return: new hash table or NULL on malloc failure.
81 1.1 christos */
82 1.1 christos struct slabhash* slabhash_create(size_t numtables, size_t start_size,
83 1.1.1.2 christos size_t maxmem, lruhash_sizefunc_type sizefunc,
84 1.1.1.2 christos lruhash_compfunc_type compfunc, lruhash_delkeyfunc_type delkeyfunc,
85 1.1.1.2 christos lruhash_deldatafunc_type deldatafunc, void* arg);
86 1.1 christos
87 1.1 christos /**
88 1.1 christos * Delete hash table. Entries are all deleted.
89 1.1 christos * @param table: to delete.
90 1.1 christos */
91 1.1 christos void slabhash_delete(struct slabhash* table);
92 1.1 christos
93 1.1 christos /**
94 1.1 christos * Clear hash table. Entries are all deleted.
95 1.1 christos * @param table: to make empty.
96 1.1 christos */
97 1.1 christos void slabhash_clear(struct slabhash* table);
98 1.1 christos
99 1.1 christos /**
100 1.1 christos * Insert a new element into the hashtable, uses lruhash_insert.
101 1.1 christos * If key is already present data pointer in that entry is updated.
102 1.1 christos *
103 1.1 christos * @param table: hash table.
104 1.1 christos * @param hash: hash value. User calculates the hash.
105 1.1 christos * @param entry: identifies the entry.
106 1.1 christos * If key already present, this entry->key is deleted immediately.
107 1.1 christos * But entry->data is set to NULL before deletion, and put into
108 1.1 christos * the existing entry. The data is then freed.
109 1.1 christos * @param data: the data.
110 1.1.1.2 christos * @param cb_override: if not NULL overrides the cb_arg for deletefunc.
111 1.1 christos */
112 1.1.1.2 christos void slabhash_insert(struct slabhash* table, hashvalue_type hash,
113 1.1 christos struct lruhash_entry* entry, void* data, void* cb_override);
114 1.1 christos
115 1.1 christos /**
116 1.1 christos * Lookup an entry in the hashtable. Uses lruhash_lookup.
117 1.1 christos * At the end of the function you hold a (read/write)lock on the entry.
118 1.1 christos * The LRU is updated for the entry (if found).
119 1.1 christos * @param table: hash table.
120 1.1 christos * @param hash: hash of key.
121 1.1 christos * @param key: what to look for, compared against entries in overflow chain.
122 1.1 christos * the hash value must be set, and must work with compare function.
123 1.1 christos * @param wr: set to true if you desire a writelock on the entry.
124 1.1 christos * with a writelock you can update the data part.
125 1.1 christos * @return: pointer to the entry or NULL. The entry is locked.
126 1.1 christos * The user must unlock the entry when done.
127 1.1 christos */
128 1.1 christos struct lruhash_entry* slabhash_lookup(struct slabhash* table,
129 1.1.1.2 christos hashvalue_type hash, void* key, int wr);
130 1.1 christos
131 1.1 christos /**
132 1.1 christos * Remove entry from hashtable. Does nothing if not found in hashtable.
133 1.1 christos * Delfunc is called for the entry. Uses lruhash_remove.
134 1.1 christos * @param table: hash table.
135 1.1 christos * @param hash: hash of key.
136 1.1 christos * @param key: what to look for.
137 1.1 christos */
138 1.1.1.2 christos void slabhash_remove(struct slabhash* table, hashvalue_type hash, void* key);
139 1.1 christos
140 1.1 christos /**
141 1.1 christos * Output debug info to the log as to state of the hash table.
142 1.1 christos * @param table: hash table.
143 1.1 christos * @param id: string printed with table to identify the hash table.
144 1.1 christos * @param extended: set to true to print statistics on overflow bin lengths.
145 1.1 christos */
146 1.1 christos void slabhash_status(struct slabhash* table, const char* id, int extended);
147 1.1 christos
148 1.1 christos /**
149 1.1 christos * Retrieve slab hash total size.
150 1.1 christos * @param table: hash table.
151 1.1 christos * @return size configured as max.
152 1.1 christos */
153 1.1 christos size_t slabhash_get_size(struct slabhash* table);
154 1.1 christos
155 1.1 christos /**
156 1.1.1.3 christos * See if slabhash is of given (size, slabs) configuration.
157 1.1.1.3 christos * @param table: hash table
158 1.1.1.3 christos * @param size: max size to test for
159 1.1.1.3 christos * @param slabs: slab count to test for.
160 1.1.1.3 christos * @return true if equal
161 1.1.1.3 christos */
162 1.1.1.3 christos int slabhash_is_size(struct slabhash* table, size_t size, size_t slabs);
163 1.1.1.3 christos
164 1.1.1.3 christos /**
165 1.1.1.4.4.1 martin * Update the size of an element in the hashtable, uses
166 1.1.1.4.4.1 martin * lruhash_update_space_used.
167 1.1.1.4.4.1 martin *
168 1.1.1.4.4.1 martin * @param table: hash table.
169 1.1.1.4.4.1 martin * @param hash: hash value. User calculates the hash.
170 1.1.1.4.4.1 martin * @param cb_override: if not NULL overrides the cb_arg for deletefunc.
171 1.1.1.4.4.1 martin * @param diff_size: difference in size to the hash table storage.
172 1.1.1.4.4.1 martin */
173 1.1.1.4.4.1 martin void slabhash_update_space_used(struct slabhash* table, hashvalue_type hash,
174 1.1.1.4.4.1 martin void* cb_override, int diff_size);
175 1.1.1.4.4.1 martin
176 1.1.1.4.4.1 martin /**
177 1.1 christos * Retrieve slab hash current memory use.
178 1.1 christos * @param table: hash table.
179 1.1 christos * @return memory in use.
180 1.1 christos */
181 1.1 christos size_t slabhash_get_mem(struct slabhash* table);
182 1.1 christos
183 1.1 christos /**
184 1.1 christos * Get lruhash table for a given hash value
185 1.1 christos * @param table: slabbed hash table.
186 1.1 christos * @param hash: hash value.
187 1.1 christos * @return the lru hash table.
188 1.1 christos */
189 1.1.1.2 christos struct lruhash* slabhash_gettable(struct slabhash* table, hashvalue_type hash);
190 1.1 christos
191 1.1 christos /**
192 1.1 christos * Set markdel function
193 1.1 christos * @param table: slabbed hash table.
194 1.1 christos * @param md: markdel function ptr.
195 1.1 christos */
196 1.1.1.2 christos void slabhash_setmarkdel(struct slabhash* table, lruhash_markdelfunc_type md);
197 1.1 christos
198 1.1 christos /**
199 1.1 christos * Traverse a slabhash.
200 1.1 christos * @param table: slabbed hash table.
201 1.1 christos * @param wr: if true, writelock is obtained, otherwise readlock.
202 1.1 christos * @param func: function to call for every element.
203 1.1 christos * @param arg: user argument to function.
204 1.1 christos */
205 1.1 christos void slabhash_traverse(struct slabhash* table, int wr,
206 1.1 christos void (*func)(struct lruhash_entry*, void*), void* arg);
207 1.1 christos
208 1.1 christos /*
209 1.1 christos * Count entries in slabhash.
210 1.1 christos * @param table: slabbed hash table;
211 1.1 christos * @return the number of items
212 1.1 christos */
213 1.1 christos size_t count_slabhash_entries(struct slabhash* table);
214 1.1 christos
215 1.1.1.4 christos /**
216 1.1.1.4 christos * Retrieves number of items in slabhash and the current max collision level
217 1.1.1.4 christos * @param table: slabbed hash table.
218 1.1.1.4 christos * @param entries_count: where to save the current number of elements.
219 1.1.1.4 christos * @param max_collisions: where to save the current max collisions level.
220 1.1.1.4 christos */
221 1.1.1.4 christos void get_slabhash_stats(struct slabhash* table,
222 1.1.1.4 christos long long* entries_count, long long* max_collisions);
223 1.1.1.4 christos
224 1.1.1.4.4.1 martin /**
225 1.1.1.4.4.1 martin * Adjust size of slabhash memory max
226 1.1.1.4.4.1 martin * @param table: slabbed hash table
227 1.1.1.4.4.1 martin * @param max: new max memory
228 1.1.1.4.4.1 martin */
229 1.1.1.4.4.1 martin void slabhash_adjust_size(struct slabhash* table, size_t max);
230 1.1.1.4.4.1 martin
231 1.1 christos /* --- test representation --- */
232 1.1 christos /** test structure contains test key */
233 1.1 christos struct slabhash_testkey {
234 1.1 christos /** the key id */
235 1.1 christos int id;
236 1.1 christos /** the entry */
237 1.1 christos struct lruhash_entry entry;
238 1.1 christos };
239 1.1 christos /** test structure contains test data */
240 1.1 christos struct slabhash_testdata {
241 1.1 christos /** data value */
242 1.1 christos int data;
243 1.1 christos };
244 1.1 christos
245 1.1 christos /** test sizefunc for lruhash */
246 1.1 christos size_t test_slabhash_sizefunc(void*, void*);
247 1.1 christos /** test comparefunc for lruhash */
248 1.1 christos int test_slabhash_compfunc(void*, void*);
249 1.1 christos /** test delkey for lruhash */
250 1.1 christos void test_slabhash_delkey(void*, void*);
251 1.1 christos /** test deldata for lruhash */
252 1.1 christos void test_slabhash_deldata(void*, void*);
253 1.1 christos /* --- end test representation --- */
254 1.1 christos
255 1.1 christos #endif /* UTIL_STORAGE_SLABHASH_H */
256