fwrite implemented
[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 #ifdef STON_HT_FREAD
49 #include <stdio.h>
50 #include <errno.h>
51 #include <alloca.h>
52 STON_FUNC_STATIC
53 STON_FUNC_NOINLINE
54 ston_ht ston_ht32_fread(FILE*,long,void*(*)(size_t));
55 size_t ston_ht32_fwrite(ston_ht,FILE*,long);
56 #else
57 #include <stddef.h>
58 #endif //STON_HT_FREAD
59 #include <stdint.h>
60 #include <string.h> //mem*
61 /* STON Hashtable Structure
62 Hashtables are stored as dynamically sized two dimensional arrays
63 */
64 typedef struct ston_ht_header_t
65 { uint16_t ht_columns;
66 uint8_t ht_2pow, ht_flags;
67 }ston_ht_h,* ston_ht;
68
69 STON_FUNC
70 uint32_t ston_up2pow(uint32_t);
71 STON_FUNC
72 uint8_t ston_trailing0(uint32_t);
73 STON_FUNC
74 ston_ht ston_ht32_create(uint16_t,uint8_t,uint8_t,void*(*)(size_t));
75 STON_FUNC
76 uint32_t* ston_ht32_row(ston_ht,uint32_t);
77 STON_FUNC
78 uint32_t ston_ht32_insert(ston_ht,uint32_t,uint16_t,uint32_t);
79 STON_FUNC
80 size_t ston_ht32_insertx(ston_ht,uint32_t,uint32_t*,size_t,size_t);
81
82 #define ston_ht32_new(_COL,_N,_F,_FN) (ston_ht32_create(_COL,ston_trailing0(ston_up2pow(_N << 1)),_F,_FN))
83 #define ston_ht32_entry(_HT,_KEY,_COL) (ston_ht32_row(_HT,_KEY) + _COL)
84 #define ston_ht_size(_HT) ((_HT)->ht_columns << (_HT)->ht_2pow)
85 #define ston_ht_rows(_HT) (0x1 << (_HT)->ht_2pow)
86 #define ston_ht_cols(_HT) ((_HT)->ht_columns)
87 #define ston_ht_start(_HT) ((uint8_t*)((_HT) + 1))
88 #define ston_ht_keyrow(_HT,_KEY) ((_KEY) & (ston_ht_rows(ht) - 1))
89 #define ston_ht32_start(_HT) ((uint32_t*)ston_ht_start(_HT))
90 #define ston_ht32_end(_HT) (ston_ht32_start(_HT) + ston_ht_size(_HT))
91 #define ston_ht32_size(_HT) (ston_ht_size(_HT) * sizeof(uint32_t))
92
93 /** @see http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */
94 STON_FUNC
95 uint32_t ston_up2pow
96 ( uint32_t val )
97 { val = (val << 1) - 1;
98 val |= val >> 1;
99 val |= val >> 2;
100 val |= val >> 4;
101 val |= val >> 8;
102 val |= val >> 16;
103 return ++val;
104 }
105
106 /** @see https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightParallel */
107 STON_FUNC
108 uint8_t ston_trailing0
109 ( uint32_t v )
110 { uint8_t c = 32;
111 v &= -(int32_t)v;
112 if (v) c--;
113 if (v & 0x0000FFFF) c -= 16;
114 if (v & 0x00FF00FF) c -= 8;
115 if (v & 0x0F0F0F0F) c -= 4;
116 if (v & 0x33333333) c -= 2;
117 if (v & 0x55555555) c -= 1;
118 return c;
119 }
120
121 /* Creates a new hash table, provided a memory allocation function that takes a
122 single size_t bytes, a column count, and a row count which determines the
123 size of the table.
124
125 use ston_ht32_new to specify the exact or estimated number of unique keys
126 held in the table. With ston_ht32_new, the provided ht_rows is doubled, and
127 rounded up to the nearest power of two to create a hash table with minimal
128 collisions.
129 */
130 STON_FUNC
131 ston_ht ston_ht32_create
132 ( uint16_t ht_columns,
133 uint8_t ht_2pow,
134 uint8_t ht_flags,
135 void* (*alloc_fn)(size_t)
136 )
137 { size_t ht_bytes = (ht_columns << ht_2pow) * sizeof(uint32_t);
138 ston_ht ht = (ston_ht) alloc_fn(sizeof(ston_ht_h) + ht_bytes);
139 if (ht != NULL)
140 { ht->ht_columns = ht_columns;
141 ht->ht_2pow = ht_2pow;
142 ht->ht_flags = ht_flags;
143 memset(ht + 1, 0, ht_bytes);
144 }
145 return ht;
146 }
147
148 #ifdef STON_HT_FREAD
149 /* Reads a 32-bit hash table out of the provided file at the provide fpos, into
150 a buffer allocated by alloc_fn. Memory is allocated to the stack until the
151 entire structure is verified, and all file operations are finished.
152 Returns NULL with properly set errno on failure.
153 */
154 ston_ht ston_ht32_fread
155 ( FILE* file,
156 long fpos,
157 void* (*alloc_fn)(size_t)
158 )
159 { struct ston_ht_header_t header;
160 ston_ht stack_ht, ht;
161 long fpos_start;
162 size_t table_size, alloc_size;
163 int errno_local;
164 if ((fpos_start = ftell(file)) == -1)
165 return NULL;
166 if (fread(&header, sizeof(header), 1, file) != 1)
167 goto fail_seekback;
168 table_size = ston_ht32_size(&header);
169 alloc_size = sizeof(header) + table_size;
170 stack_ht = (ston_ht) alloca(alloc_size);
171 memcpy(stack_ht, &header, sizeof(header));
172 if (fread(stack_ht + sizeof(header), table_size, 1, file) != 1)
173 goto fail_seekback;
174 if (fseek(file, fpos_start, SEEK_SET) != 0)
175 return NULL;
176 ht = (ston_ht) alloc_fn(alloc_size);
177 if (ht != NULL)
178 memcpy(ht, stack_ht, alloc_size);
179 return ht;
180 fail_seekback:
181 /* Try to seek the file back to origin without clobbering errno */
182 errno_local = errno;
183 fseek(file, fpos_start, SEEK_SET);
184 errno = errno_local;
185 return NULL;
186 }
187
188 /* Writes a 32-bit hash table from memory into a file at fpos. Returns the
189 number of bytes written to the file, errno is set on error. */
190 size_t ston_ht32_fwrite
191 ( struct ston_ht_header_t* ht,
192 FILE* file,
193 long fpos
194 )
195 { size_t bytes_written;
196 long fpos_start;
197 if ((fpos_start = ftell(file)) == NULL
198 || (bytes_written = fwrite(file, 1, sizeof(ston_ht_h), file)) < sizeof(ston_ht_h)
199 || (bytes_written += fwrite(file, 1, ston_ht32_bytes(ht), file)) < (sizeof(ston_ht_h) + ston_ht32_bytes(ht)))
200 return 0;
201 return bytes_written;
202 }
203 #endif
204
205 /* Returns a pointer to the row of data in the hashtable containing the provided
206 key, inserts if not found. Returns NULL on overflow.
207 */
208 STON_FUNC
209 uint32_t* ston_ht32_row
210 ( struct ston_ht_header_t* ht,
211 uint32_t key
212 )
213 { uint32_t* row;
214 uint32_t* row_start = ston_ht32_start(ht);
215 uint32_t* row_end = ston_ht32_end(ht);
216 uint16_t ht_cols = ston_ht_cols(ht);
217 size_t row_number = ston_ht_keyrow(ht,key);
218 uint8_t looped = 0;
219 row = row_start + (row_number * ht_cols);
220 next_row:
221 if (row[0] != 0)
222 goto populated;
223 write_position:
224 row[0] = key;
225 return row;
226 populated:
227 if (row[0] == key)
228 goto write_position;
229 if (row + ht_cols < row_end)
230 row += ht_cols;
231 else if (looped)
232 return NULL;
233 else
234 { looped++;
235 row = row_start;
236 }
237 goto next_row;
238 }
239
240 /* Inserts a value into a hashtable at the specified column, returning the
241 previous value */
242 STON_FUNC
243 uint32_t ston_ht32_insert
244 ( struct ston_ht_header_t* ht,
245 uint32_t key,
246 uint16_t column,
247 uint32_t value
248 )
249 { uint32_t* value_location, old_value;
250 value_location = ston_ht32_entry(ht,key,column);
251 old_value = *value_location;
252 *value_location = value;
253 return old_value;
254 }
255
256 /* Inserts a row of units into a hashtable, starting with the specified column.
257 Returns the number of elements that were written. This function will not
258 overflow internal buffers, but will return a short count (lower than the
259 provided 'units') when truncation of source data occurs. */
260 STON_FUNC
261 size_t
262 ston_ht32_insertx
263 ( struct ston_ht_header_t* ht,
264 uint32_t key,
265 uint32_t* data_src,
266 size_t start_column,
267 size_t units
268 )
269 { uint32_t* data_row = ston_ht32_row(ht,key);
270 uint32_t* data_limit = data_row + ston_ht_cols(ht);
271 uint32_t* data_trg = data_row + start_column;
272 if (data_row == NULL)
273 return 0;
274 while (units-- && data_trg < data_limit)
275 *data_trg++ = *data_src++;
276 return (size_t)(data_trg - data_row);
277 }
278
279
280 #ifndef STON_DHT_SIZE
281 #define STON_DHT_SIZE 4096
282 #endif
283
284 /* STON Dynamic Hashtable Structure
285 A dynamic form of the generic hashtable implementation above which uses
286 external allocation.
287 */
288 typedef struct ston_dht_header_t
289 { uint16_t columns;
290 uint8_t unit_bytes;
291 uint8_t start_depth;
292 }ston_dht_h;
293
294 typedef struct ston_dht_t
295 { ston_dht_h header;
296 void* pages[sizeof(void*) * 8];
297 void* (*ht_alloc)(size_t);
298 void (*ht_free)(void*);
299 }* ston_dht;
300
301 STON_FUNC
302 ston_dht ston_dht_create(uint16_t,uint8_t,uint8_t,void*(*)(size_t),void(*)(void*));
303 STON_FUNC
304 uint32_t* ston_dht32_row(ston_dht,uint32_t);
305 STON_FUNC
306 uint32_t ston_dht32_insert(ston_dht,uint32_t,uint16_t,uint32_t);
307 STON_FUNC
308 size_t ston_dht32_insertx(ston_dht,uint32_t,uint32_t*,uint16_t,size_t);
309 STON_FUNC
310 ston_dht ston_dht_free(ston_dht);
311
312 #define ston_dht_units(_HT,_DEPTH) ((_HT)->header.columns << _DEPTH)
313 #define ston_dht_bytes(_HT,_DEPTH) (ston_dht_units(_HT,_DEPTH) * (_HT)->header.unit_bytes)
314 #define ston_dht_new(_COL,_ALOC,_FRE) (ston_dht_create(_COL,3,sizeof(int),_ALOC,_FRE))
315 #define ston_dht_sized(_COL,_N,_ALOC,_FRE) (ston_dht_create(_COL,ston_trailing0(ston_up2pow(_N),sizeof(int),_ALOC,_FRE)))
316 #define ston_dht32_entry(_HT,_KEY,_COL) (ston_dht32_row(_HT,_KEY) + _COL)
317 #define ston_dht32_new(_COL,_ALOC,_FRE) (ston_dht_create(_COL,0,sizeof(uint32_t),_ALOC,_FRE))
318 #define ston_dht32_sized(_COL,_N,_ALOC,_FRE) (ston_dht_create(_COL,ston_trailing0(ston_up2pow(_N)),sizeof(uint32_t),_ALOC,_FRE))
319
320
321 /* Creates a new bucketted hash table, provided a memory allocation function
322 that takes a single size_t bytes, a memory free function, a column count, and
323 a row count which determines the size of the buckets.
324 */
325 STON_FUNC
326 ston_dht ston_dht_create
327 ( uint16_t columns,
328 uint8_t start_depth,
329 uint8_t unit_bytes,
330 void* (*ht_alloc)(size_t),
331 void (*ht_free)(void*)
332 )
333 { ston_dht ht = (ston_dht) ht_alloc(sizeof(struct ston_dht_t));
334 if (ht != NULL)
335 { ht->header.columns = columns;
336 ht->header.start_depth = start_depth;
337 ht->header.unit_bytes = unit_bytes;
338 memset(ht->pages, 0, sizeof(void*) * sizeof(void*) * 8);
339 ht->pages[start_depth] = ht_alloc(ston_dht_bytes(ht, start_depth));
340 ht->ht_alloc = ht_alloc;
341 ht->ht_free = ht_free;
342 if (ht->pages[start_depth] == NULL && ht_free != NULL)
343 ht_free(ht);
344 else
345 memset(ht->pages[start_depth], 0, ston_dht_bytes(ht, start_depth));
346 }
347 return ht;
348 }
349
350 /* Returns a pointer to the row of data in the hashtable containing the provided
351 key, inserts if not found. Returns NULL on overflow.
352 */
353 STON_FUNC
354 uint32_t* ston_dht32_row
355 ( struct ston_dht_t* ht,
356 uint32_t key
357 )
358 { uint16_t columns = ht->header.columns;
359 uint8_t depth = ht->header.start_depth;
360 uint32_t mask = ((0x1 << depth) - 1) >> 1;
361 void* page;
362 uint32_t* row;
363 uint32_t row_key;
364 next_page:
365 if (ht->pages[depth] == NULL)
366 { ht->pages[depth] = ht->ht_alloc(ston_dht_bytes(ht, depth));
367 if (ht->pages[depth] == NULL)
368 return NULL;
369 memset(ht->pages[depth], 0, ston_dht_bytes(ht, depth));
370 }
371 page = ht->pages[depth];
372 row = (uint32_t*)page + ((key & mask) * columns);
373 row_key = *row;
374 if (row_key == key || row_key == 0)
375 { row[0] = key;
376 return row;
377 }
378 depth++;
379 mask = (mask << 1) | 0x1;
380 goto next_page;
381 }
382
383 /* Inserts a value into a hashtable at the specified column, returning the
384 previous value */
385 STON_FUNC
386 uint32_t ston_dht32_insert
387 ( struct ston_dht_t* ht,
388 uint32_t key,
389 uint16_t column,
390 uint32_t value
391 )
392 { uint32_t* value_location, old_value;
393 value_location = ston_dht32_entry(ht,key,column);
394 old_value = *value_location;
395 *value_location = value;
396 return old_value;
397 }
398
399 /* Insert multiple values, returning the number of bytes written */
400 STON_FUNC
401 size_t
402 ston_dht32_insertx
403 ( struct ston_dht_t* ht,
404 uint32_t key,
405 uint32_t* data_src,
406 uint16_t start_column,
407 size_t units
408 )
409 { uint32_t* data_row = ston_dht32_row(ht,key);
410 uint32_t* data_limit = data_row + ht->header.columns;
411 uint32_t* data_trg = data_row + start_column;
412 if (data_row == NULL)
413 return 0;
414 while (units-- && data_trg < data_limit)
415 *data_trg++ = *data_src++;
416 return (size_t)(data_trg - data_row);
417 }
418
419 /* Free the dynamic hash table */
420 STON_FUNC
421 struct ston_dht_t* ston_dht_free
422 ( struct ston_dht_t* ht )
423 { void (*ht_free)(void*) = ht->ht_free;
424 uint8_t depth = ht->header.start_depth;
425 void** pages = ht->pages;
426 if (ht_free != NULL)
427 { while (pages[depth] != NULL)
428 ht_free(pages[depth++]);
429 ht_free(ht);
430 return NULL;
431 }
432 return ht;
433 }
434
435 /********************************************************************************
436 *********************************************************************************
437 *********************************************************************************
438 ********************************************************************************/
439 typedef struct ston_dht2_header_t
440 { uint16_t val_bytes;
441 uint8_t key_bytes;
442 uint8_t flags;
443 }ston_dht2_h;
444
445 typedef struct ston_dht2_bucket_t
446 { uint8_t depth;
447 void* page;
448 uint32_t count;
449 }ston_dht2_bucket_h,* ston_dht2_bucket;
450
451 #define STON_DHT_BUCKETS_SIZE (sizeof(void*) * 8)
452 typedef struct ston_dht2_t
453 { ston_dht2_h header;
454 ston_dht2_bucket_h buckets[1 + STON_DHT_BUCKETS_SIZE];
455 ston_dht2_bucket bsp;
456 uint32_t row_bytes;
457 uint8_t buckets_len;
458 void* (*ht_alloc)(size_t);
459 void (*ht_free)(void*);
460 }* ston_dht2;
461
462 STON_FUNC
463 ston_dht2 ston_dht2_create(uint16_t,uint8_t,void*(*)(size_t),void(*)(void*));
464 STON_FUNC
465 uint32_t* ston_dht232_row(ston_dht2,uint32_t);
466 STON_FUNC
467 uint32_t ston_dht232_insert(ston_dht2,uint32_t,uint16_t,uint32_t);
468 STON_FUNC
469 size_t ston_dht232_insertx(ston_dht2,uint32_t,uint32_t*,uint16_t,size_t);
470 STON_FUNC
471 ston_dht2 ston_dht2_free(ston_dht2);
472
473 #define ston_dht2_bytes(_HT,_DEPTH) ((_HT)->row_bytes << (_DEPTH))
474 #define ston_dht2_new(_COL,_ALOC,_FRE) (ston_dht2_create(_COL,sizeof(int),_ALOC,_FRE))
475 #define ston_dht232_new(_COL,_ALOC,_FRE) (ston_dht2_create(_COL,sizeof(uint32_t),_ALOC,_FRE))
476 #define ston_dht232_entry(_HT,_KEY,_COL) (ston_dht232_row(_HT,_KEY) + _COL)
477
478
479 /* Creates a new bucketted hash table, provided a memory allocation function
480 that takes a single size_t bytes, a memory free function, a column count, and
481 a row count which determines the size of the buckets.
482 */
483 static ston_dht2_bucket_h dummy_bucket = { (uint8_t)-1, NULL, (uint32_t)-1 };
484
485 STON_FUNC
486 ston_dht2 ston_dht2_create
487 ( uint16_t val_bytes,
488 uint8_t key_bytes,
489 void* (*ht_alloc)(size_t),
490 void (*ht_free)(void*)
491 )
492 { ston_dht2 ht = (ston_dht2) ht_alloc(sizeof(struct ston_dht2_t));
493 if (ht != NULL)
494 { ht->header.val_bytes = val_bytes;
495 ht->header.key_bytes = key_bytes;
496 ht->row_bytes = val_bytes + key_bytes;
497 ht->ht_alloc = ht_alloc;
498 ht->ht_free = ht_free;
499 size_t i;
500 for (i = 0; i <= STON_DHT_BUCKETS_SIZE; i++)
501 ht->buckets[i] = dummy_bucket;
502 ht->bsp = ht->buckets + STON_DHT_BUCKETS_SIZE - 1;
503 ht->bsp->page = ht_alloc(ston_dht2_bytes(ht, 4));
504 if (ht->bsp->page == NULL && ht_free != NULL)
505 ht_free(ht);
506 else
507 { memset((ht->bsp->page), 0, ston_dht2_bytes(ht,4));
508 ht->bsp->depth = 4;
509 ht->bsp->count = 0;
510 ht->buckets_len = 1;
511 }
512 }
513 return ht;
514 }
515
516
517 /* Returns a pointer to the row of data in the hashtable containing the provided
518 key, inserting if not found, or NULL if a memory error occurs */
519 STON_FUNC
520 uint32_t* ston_dht232_row
521 ( struct ston_dht2_t* ht,
522 uint32_t key
523 )
524 { int8_t bucket_no = (int8_t)ht->buckets_len - 1;
525 uint32_t* row, row_key, mask;
526 size_t bytes;
527 int8_t zero_bucket = (int8_t)-1;
528 ston_dht2_bucket bsp = ht->bsp;
529 ston_dht2_bucket_h bucket;
530 next_bucket:
531 bucket = bsp[bucket_no];
532 /* Find until out of allocated pages, then insert at last empty bucket position */
533 if (bucket.page == NULL)
534 { if (zero_bucket != (int8_t)-1)
535 { bucket = bsp[zero_bucket];
536 mask = (0x1 << bucket.depth) - 1;
537 row = (uint32_t*)bucket.page + (key & mask);
538 *row = key;
539 bucket.count++;
540 /* Swap the buckets up a level if the count has exceeded its parent's count */
541 if (bucket.count > bsp[zero_bucket + 1].count)
542 { bsp[zero_bucket] = bsp[zero_bucket + 1];
543 bsp[zero_bucket + 1] = bucket;
544 }
545 else
546 bsp[zero_bucket].count = bucket.count;
547 return row;
548 }
549 /* No buckets with a slot, shift the key right by depth, try again add a new bucket */
550 bsp--;
551 bucket.depth = 4 + (ht->buckets_len >> 1);
552 bucket.count = 1;
553 bytes = ston_dht2_bytes(ht,bucket.depth);
554 if ((bucket.page = ht->ht_alloc(bytes)) == NULL)
555 { printf("Failed to allocate %lu bytes, bucket %i at with len %i\n",
556 bytes, bucket_no, ht->buckets_len);
557 return NULL;
558 }
559 memset(bucket.page,0,bytes);
560 *bsp = bucket;
561 ht->bsp = bsp;
562 ht->buckets_len++;
563 mask = (0x1 << bucket.depth) - 1;
564 row = (uint32_t*)bucket.page + (key & mask);
565 *row = key;
566 return row;
567 }
568 /* Compute mask, and use it to find the row in the page */
569 mask = (0x1 << bucket.depth) - 1;
570 row = (uint32_t*)bucket.page + (key & mask);
571 /* Look at the key at row[0], branch */
572 row_key = *row;
573 if (row_key == key)
574 return row;
575 if (row_key == 0)
576 zero_bucket = bucket_no;
577 bucket_no--;
578 goto next_bucket;
579 }
580
581 /* Inserts a value into a hashtable at the specified column, returning the
582 previous value */
583 STON_FUNC
584 uint32_t ston_dht232_insert
585 ( struct ston_dht2_t* ht,
586 uint32_t key,
587 uint16_t column,
588 uint32_t value
589 )
590 { uint32_t* value_location, old_value;
591 value_location = ston_dht232_entry(ht,key,column);
592 old_value = *value_location;
593 *value_location = value;
594 return old_value;
595 }
596
597 /* Insert multiple values, returning the number of bytes written */
598 STON_FUNC
599 size_t
600 ston_dht232_insertx
601 ( struct ston_dht2_t* ht,
602 uint32_t key,
603 uint32_t* data_src,
604 uint16_t start_column,
605 size_t units
606 )
607 { uint32_t* data_row = ston_dht232_row(ht,key);
608 uint32_t* data_limit = data_row + ht->row_bytes;
609 uint32_t* data_trg = data_row + start_column;
610 if (data_row == NULL)
611 return 0;
612 while (units-- && data_trg < data_limit)
613 *data_trg++ = *data_src++;
614 return (size_t)(data_trg - data_row);
615 }
616
617 /* Free the dynamic hash table */
618 STON_FUNC
619 struct ston_dht2_t* ston_dht2_free
620 ( struct ston_dht2_t* ht )
621 { void (*ht_free)(void*) = ht->ht_free;
622 uint8_t bucket = ht->buckets_len;
623 if (ht_free != NULL)
624 { while (bucket--)
625 ht_free(ht->buckets[bucket].page);
626 ht_free(ht);
627 return NULL;
628 }
629 return ht;
630 }
631
632
633 #endif //_STON_HT_H_