a0bf05f680eb2169a168bcab6136f69f41443950
[henge/apc.git] / ston / ston_ht.h
1 /*!@file
2 \brief STON Hash Tables
3 \details Aligned general purpose hash functions and memory definitions
4 whose columns are provided, and whose rows, and sizes, are derived.
5
6 ht_size = header.ht_columns << header.ht_2pow;
7 ht_rows = 0x1 << header.ht_2pow;
8
9 All generic hashtables in henge must have a power-of-two number of
10 rows. An ht_columns value that is also a power-of-two will result in
11 a power-of-two sized memory imprint for the structure, making it easy
12 to page align.
13
14 Elements in the columns may be of any arbitrary size.
15
16 typedef uint32_t my_ht_type;
17 ht_bytes = ht_size * sizeof(my_ht_type);
18
19 implementation covers only 32-bit unit sizes.
20
21 \author Ken Grimes
22 \date Feb 2017
23 ----------------------------------------------------------------------------*/
24 #ifndef _STON_HT_T_
25 #define _STON_HT_T_
26 /* Define STON_NOSTATIC to expose included function symbols */
27 #ifndef STON_NOSTATIC
28 #define STON_FUNC_STATIC static
29 #else
30 #define STON_FUNC_STATIC
31 #endif //STON_NOSTATIC
32 /* If GNUC is detected, uses attributes to stop inlining */
33 #ifdef __GNUC__
34 #define STON_FUNC_NOINLINE __attribute__ ((noinline))
35 #else
36 #define STON_FUNC_NOINLINE
37 #endif //__GNUC__
38 /* Define STON_NOINLINE to prevent inline compiler hints */
39 #ifndef STON_NOINLINE
40 #define STON_FUNC_INLINE inline
41 #else
42 #define STON_FUNC_INLINE
43 #endif //STON_NOINLINE
44 /* Define STON_FUNC to override the default STON Function attributes */
45 #ifndef STON_FUNC
46 #define STON_FUNC STON_FUNC_STATIC STON_FUNC_INLINE
47 #endif //STON_FUNC
48 #ifndef STON_NOSTDIO
49 #include <stdio.h>
50 #include <string.h> //memcpy
51 #include <alloca.h>
52 #endif //STON_NOSTDIO
53 #include <stdint.h>
54 /* STON Hashtable Structure
55 Hashtables are stored as dynamically sized two dimensional arrays
56 */
57 typedef struct ston_ht_header_t
58 { uint16_t ht_columns;
59 uint8_t ht_2pow, ht_flags;
60 }* ston_ht;
61
62 STON_FUNC
63 size_t ston_up2pow(size_t);
64 STON_FUNC_STATIC
65 STON_FUNC_NOINLINE
66 ston_ht ston_ht32_fread(FILE*,long,void*(*)(size_t));
67 STON_FUNC
68 ston_ht ston_ht32_create(uint16_t,size_t,uint8_t,void*(*)(size_t));
69 STON_FUNC
70 uint32_t* ston_ht32_row(ston_ht,uint32_t);
71 STON_FUNC
72 uint32_t ston_ht32_insert(ston_ht,uint32_t,uint16_t,uint32_t);
73
74 #define ston_ht32_new(_COL,_N,_F,_FN) ston_ht32_create(_COLS,ston_up2pow(_N << 1),_F,_FN)
75 #define ston_ht32_entry(_HT,_KEY,_COL) (ston_ht32_row(_HT,_KEY) + _COL)
76 #define ston_ht32_insertx(_HT,_KEY,_COL,_VAL) *ston_ht32_entry(_HT,_KEY,_COL) = _VAL
77 #define ston_ht_size(_HT) ((_HT)->ht_columns << (_HT)->ht_2pow)
78 #define ston_ht_rows(_HT) (0x1 << (_HT)->ht_2pow)
79 #define ston_ht_cols(_HT) ((_HT)->ht_columns)
80 #define ston_ht_start(_HT) (((uint8_t*)(_HT)) + sizeof(*(_HT)))
81 #define ston_ht_keyrow(_HT,_KEY) ((_KEY) & (ston_ht_rows(ht) - 1))
82 #define ston_ht32_start(_HT) ((uint32_t*)ston_ht_start(_HT))
83 #define ston_ht32_end(_HT) (ston_ht32_start(_HT) + ston_ht_size(_HT))
84 #define ston_ht32_size(_HT) (ston_ht_size(_HT) * sizeof(uint32_t))
85
86 /** @see http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */
87 STON_FUNC
88 size_t ston_up2pow
89 ( size_t val )
90 { val = (val << 1) - 1;
91 val |= val >> 1;
92 val |= val >> 2;
93 val |= val >> 4;
94 val |= val >> 8;
95 val |= val >> 16;
96 return ++val;
97 }
98
99 /* Creates a new hash table, provided a memory allocation function that takes a
100 single size_t bytes, a column count, and a row count which determines the
101 size of the table.
102
103 use ston_ht32_new to specify the exact or estimated number of unique keys
104 held in the table. With ston_ht32_new, the provided ht_rows is doubled, and
105 rounded up to the nearest power of two to create a hash table with minimal
106 collisions.
107 */
108 STON_FUNC
109 ston_ht ston_ht32_create
110 ( uint16_t ht_columns,
111 size_t ht_rows,
112 uint8_t ht_flags,
113 void* (*alloc_fn)(size_t)
114 )
115 { size_t ht_size = ht_rows * ht_columns * sizeof(uint32_t);
116 ston_ht ht = (ston_ht) alloc_fn(sizeof(struct ston_ht_header_t) + ht_size);
117 if (ht != NULL)
118 { for (ht->ht_2pow = 0; ht_size; ht->ht_2pow++)
119 ht_size = ht_size >> 1;
120 ht->ht_columns = ht_columns;
121 ht->ht_flags = ht_flags;
122 }
123 return ht;
124 }
125
126 #ifndef STON_NO_STDIO
127 /* Reads a 32-bit hash table out of the provided file at the provide fpos, into
128 a buffer allocated by alloc_fn. Memory is allocated to the stack until the
129 entire structure is verified, and all file operations are finished.
130 Returns NULL with properly set errno on failure.
131 */
132 STON_FUNC_STATIC
133 STON_FUNC_NOINLINE
134 ston_ht ston_ht32_fread
135 ( FILE* file,
136 long fpos,
137 void* (*alloc_fn)(size_t)
138 )
139 { struct ston_ht_header_t header;
140 ston_ht stack_ht, ht;
141 long fpos_start;
142 size_t table_size, alloc_size;
143 int errno_local;
144 if ((fpos_start = ftell(file)) == -1)
145 return NULL;
146 if (fread(&header, sizeof(header), 1, file) != 1)
147 goto fail_seekback;
148 table_size = ston_ht32_size(&header);
149 alloc_size = sizeof(header) + table_size;
150 stack_ht = (ston_ht) alloca(alloc_size);
151 memcpy(stack_ht, &header, sizeof(header));
152 if (fread(stack_ht + sizeof(header), table_size, 1, file) != 1)
153 goto fail_seekback;
154 if (fseek(file, fpos_start, SEEK_SET) != 0)
155 return NULL;
156 ht = (ston_ht) alloc_fn(alloc_size);
157 if (ht != NULL)
158 memcpy(ht, stack_ht, alloc_size);
159 return ht;
160 fail_seekback:
161 /* Try to seek the file back to origin without clobbering errno */
162 errno_local = errno;
163 fseek(file, fpos_start, SEEK_SET);
164 errno = errno_local;
165 return NULL;
166 }
167 #endif
168
169 /* Returns a pointer to the row of data in the hashtable containing the provided
170 key, inserts if not found. Returns NULL on overflow.
171 */
172 STON_FUNC
173 uint32_t* ston_ht32_row
174 ( struct ston_ht_header_t* ht,
175 uint32_t key
176 )
177 { uint32_t* row;
178 uint32_t* row_start = ston_ht32_start(ht);
179 uint32_t* row_end = ston_ht32_end(ht);
180 uint16_t ht_cols = ston_ht_cols(ht);
181 size_t row_number = ston_ht_keyrow(ht,key);
182 uint8_t looped = 0;
183 row = row_start + (row_number * ht_cols);
184 next_row:
185 if (row[0] != 0)
186 goto populated;
187 write_position:
188 row[0] = key;
189 return row;
190 populated:
191 if (row[0] == key)
192 goto write_position;
193 if (row < row_end)
194 row += ht_cols;
195 else if (looped)
196 return NULL;
197 else
198 { looped++;
199 row = row_start;
200 }
201 goto next_row;
202 }
203
204 /* Inserts a value into a hashtable at the specified column, returning the
205 previous value */
206 STON_FUNC
207 uint32_t ston_ht32_insert
208 ( struct ston_ht_header_t* ht,
209 uint32_t key,
210 uint16_t column,
211 uint32_t value
212 )
213 { uint32_t* value_location, old_value;
214 value_location = ston_ht32_entry(ht,key,column);
215 old_value = *value_location;
216 *value_location = value;
217 return old_value;
218 }
219
220 #ifndef STON_DHT_SIZE
221 #define STON_DHT_SIZE 4096
222 #endif
223
224 /* STON Dynamic Hashtable Structure
225 A dynamic form of the generic hashtable implementation above which uses
226 external allocation.
227 */
228 typedef struct ston_dht_header_t
229 { uint16_t ht_columns;
230 uint8_t ht_2pow, ht_flags;
231 void* (*ht_alloc)(size_t);
232 void (*ht_free)(void*);
233 void** page_head;
234 }* ston_dht;
235 #define STON_DHT_HEADERSIZE (sizeof(struct ston_dht_header_t))
236
237 STON_FUNC
238 ston_dht ston_dht32_create(uint16_t,size_t,uint8_t,void*(*)(size_t),void(*)(void*));
239 STON_FUNC
240 uint32_t* ston_dht32_row(ston_dht,uint32_t);
241 STON_FUNC
242 uint32_t ston_dht32_insert(ston_dht,uint32_t,uint16_t,uint32_t);
243 STON_FUNC
244 void ston_dht32_free(ston_dht);
245
246 #define ston_dht32_new(_COL,_N,_F,_FN) ston_dht32_create(_COLS,ston_up2pow(_N << 1),_F,_FN)
247 #define ston_dht32_entry(_HT,_KEY,_COL) (ston_dht32_row(_HT,_KEY) + _COL)
248 #define ston_dht32_insertx(_HT,_KEY,_COL,_VAL) *ston_dht32_col(_HT,_KEY,_COL) = _VAL
249 #define ston_dht_size(_HT) (ston_ht_size(_HT))
250 #define ston_dht_rows(_HT) (ston_ht_rows(_HT))
251 #define ston_dht_cols(_HT) (ston_ht_cols(_HT))
252 #define ston_dht_keyrow(_HT,_KEY) (ston_ht_keyrow(_HT,_KEY))
253 #define ston_dht_pagestart(_HT) ((void**)(((uint8_t*)(_HT)) + STON_DHT_HEADERSIZE))
254 #define ston_dht_pagehead(_HT) ((_HT)->page_head)
255 #define ston_dht_pagemax(_HT) ((void**)((uint8_t*)(_HT) + STON_DHT_SIZE - sizeof(void**)))
256 #define ston_dht_start(_HT,_DEPTH) ((uint8_t*)*(ston_dht_pagestart(_HT) + _DEPTH))
257 #define ston_dht32_start(_HT,_DEPTH) ((uint32_t*)ston_dht_start(_HT,_DEPTH))
258 #define ston_dht32_end(_HT,_DEPTH) (ston_ht32_start(_HT,_DEPTH) + ston_ht_size(_HT))
259 #define ston_dht32_size(_HT) (ston_dht_size(_HT) * sizeof(uint32_t))
260 #define ston_dht32_pagepush(_HT) ((*(++((_HT)->page_head)) = (_HT)->ht_alloc(ston_dht32_size(_HT))))
261 #define ston_dht32_pagepop(_HT) ((_HT)->ht_free((_HT)->page_head--))
262
263 /* Creates a new bucketted hash table, provided a memory allocation function
264 that takes a single size_t bytes, a memory free function, a column count, and
265 a row count which determines the size of the buckets.
266 */
267 STON_FUNC
268 ston_dht ston_dht32_create
269 ( uint16_t ht_columns,
270 size_t ht_rows,
271 uint8_t ht_flags,
272 void* (*ht_alloc)(size_t),
273 void (*ht_free)(void*)
274 )
275 { size_t ht_size = ht_rows * ht_columns * sizeof(uint32_t);
276 ston_dht ht = (ston_dht) ht_alloc(STON_DHT_SIZE);
277 if (ht != NULL)
278 { for (ht->ht_2pow = 0; ht_size; ht->ht_2pow++)
279 ht_size = ht_size >> 1;
280 ht->ht_columns = ht_columns;
281 ht->ht_flags = ht_flags;
282 ht->ht_alloc = ht_alloc;
283 ht->ht_free = ht_free;
284 ht->page_head = ston_dht_pagestart(ht);
285 if ((*(ht->page_head) = ht->ht_alloc(ston_dht_size(ht))) == NULL)
286 ht_free(ht);
287 }
288 return ht;
289 }
290
291 /* Returns a pointer to the row of data in the hashtable containing the provided
292 key, inserts if not found. Returns NULL on overflow.
293 */
294 STON_FUNC
295 uint32_t* ston_dht32_row
296 ( struct ston_dht_header_t* ht,
297 uint32_t key
298 )
299 { uint16_t ht_cols = ston_dht_cols(ht);
300 size_t row_number = ston_dht_keyrow(ht,key);
301 uint32_t** page = (uint32_t**)ston_dht_pagestart(ht);
302 uint32_t** pagemax = (uint32_t**)ston_dht_pagemax(ht);
303 uint8_t loop_x = 0;
304 uint8_t loop_y = 0;
305 uint32_t* row,* row_end;
306 next_page:
307 row = *page + (row_number * ht_cols);
308 row_end = *page + (ston_dht_size(ht) - 1);
309 next_row:
310 if (row[0] != 0)
311 goto populated;
312 write_position:
313 row[0] = key;
314 return row;
315 populated:
316 if (row[0] == key)
317 goto write_position;
318 if (!loop_x)
319 { if (page < pagemax)
320 { if (page == (uint32_t**)ston_dht_pagehead(ht))
321 if (ston_dht32_pagepush(ht) == NULL)
322 { ston_dht32_free(ht);
323 return NULL;
324 }
325 ++page;
326 goto next_row;
327 }
328 loop_x = 1;
329 row_number = (row_number + 1) % ston_dht_rows(ht);
330 page = (uint32_t**)ston_dht_pagestart(ht);
331 goto next_row;
332 }
333 if (row < row_end)
334 { row += ht_cols;
335 goto next_row;
336 }
337 else if (!loop_y)
338 { loop_y = 1;
339 row = *page;
340 goto next_row;
341 }
342 if (page < pagemax)
343 { loop_y = 0;
344 page++;
345 goto next_page;
346 }
347 return NULL;
348 }
349
350 /* Inserts a value into a hashtable at the specified column, returning the
351 previous value */
352 STON_FUNC
353 uint32_t ston_dht32_insert
354 ( struct ston_dht_header_t* ht,
355 uint32_t key,
356 uint16_t column,
357 uint32_t value
358 )
359 { uint32_t* value_location, old_value;
360 value_location = ston_dht32_entry(ht,key,column);
361 old_value = *value_location;
362 *value_location = value;
363 return old_value;
364 }
365
366 /* Free the dynamic hash table */
367 STON_FUNC
368 void ston_dht32_free
369 ( struct ston_dht_header_t* ht )
370 { void (*ht_free)(void*) = ht->ht_free;
371 if (ht_free != NULL)
372 { while (ht->page_head >= ston_dht_pagestart(ht))
373 ht_free(ht->page_head--);
374 ht_free(ht);
375 }
376 }
377
378 #endif //_STON_HT_H_