#include "inc/priv.h" #include "inc/htable.h" #include "inc/hashing.h" sl_htable * htable_new(sl_htable *h, usize numpairs) { usize size; if(numpairs <= HT_N_INLINE_PAIRS){ size = HT_N_INLINE; h->table = &h->local[0]; }else{ size = nextipow2(numpairs); size *= 2; // 2 pointers per key/value pair size *= 2; // aim for 50% occupancy if((h->table = MEM_ALLOC(size * sizeof(*h->table))) == nil) return nil; slvm.malloc_pressure += size * sizeof(*h->table); } h->size = size; h->i = 0; h->num = 0; h->live = 0; memset(h->table, 0xff, size*sizeof(*h->table)); return h; } void htable_free(sl_htable *h) { if(h != nil && h->table != &h->local[0]) MEM_FREE(h->table); } // empty and reduce size void htable_reset(sl_htable *h, usize sz) { sz = nextipow2(sz); if(h->size > sz*4 && h->table != &h->local[0]){ MEM_FREE(h->table); htable_new(h, sz); }else if(h->num > 0 || h->live > 0){ memset(h->table, 0xff, h->size*sizeof(*h->table)); h->num = 0; h->live = 0; } }