Branch data Line data Source code
1 : : // This file is a part of Julia. License is MIT: https://julialang.org/license 2 : : 3 : : /* 4 : : functions common to all hash table instantiations 5 : : */ 6 : : 7 : : #include <stdlib.h> 8 : : #include <stdio.h> 9 : : #include <string.h> 10 : : #include <assert.h> 11 : : #include <limits.h> 12 : : 13 : : #include "dtypes.h" 14 : : #include "htable.h" 15 : : #include "hashing.h" 16 : : 17 : : #ifdef __cplusplus 18 : : extern "C" { 19 : : #endif 20 : : 21 : 6768440 : htable_t *htable_new(htable_t *h, size_t size) 22 : : { 23 [ + + ]: 6768440 : if (size <= HT_N_INLINE / 2) { 24 : 6764560 : h->size = size = HT_N_INLINE; 25 : 6764560 : h->table = &h->_space[0]; 26 : : } 27 : : else { 28 : 3888 : size = next_power_of_two(size); 29 : 3888 : size *= 2; // 2 pointers per key/value pair 30 : 3888 : size *= 2; // aim for 50% occupancy 31 : 3888 : h->size = size; 32 : 3888 : h->table = (void**)LLT_ALLOC(size * sizeof(void*)); 33 : : } 34 [ - + ]: 6768440 : if (h->table == NULL) 35 : 0 : return NULL; 36 : : size_t i; 37 [ + + ]: 230454000 : for (i = 0; i < size; i++) 38 : 223686000 : h->table[i] = HT_NOTFOUND; 39 : 6768440 : return h; 40 : : } 41 : : 42 : 250286 : void htable_free(htable_t *h) 43 : : { 44 [ + + ]: 250286 : if (h->table != &h->_space[0]) 45 : 39856 : LLT_FREE(h->table); 46 : 250286 : } 47 : : 48 : : // empty and reduce size 49 : 220 : void htable_reset(htable_t *h, size_t sz) 50 : : { 51 : 220 : sz = next_power_of_two(sz); 52 [ + + + + ]: 220 : if (h->size > sz * 4 && h->size > HT_N_INLINE) { 53 : 46 : LLT_FREE(h->table); 54 : 46 : h->table = NULL; 55 [ - + ]: 46 : if (htable_new(h, sz) == NULL) 56 : 0 : htable_new(h, 0); 57 : : } 58 : : else { 59 : 174 : size_t i, hsz = h->size; 60 [ + + ]: 9966 : for (i = 0; i < hsz; i++) 61 : 9792 : h->table[i] = HT_NOTFOUND; 62 : : } 63 : 220 : } 64 : : 65 : : #ifdef __cplusplus 66 : : } 67 : : #endif