void lruhash_test(void) { struct lruhash *table; printf("test lruhash functions\n"); table = lruhash_create(2, 8192, sizefunc, compfunc, delkey, deldata); test_bucket_find_entry(table); test_lru(table); test_short_table(table); test_long_table(table); lruhash_delete(table); table = lruhash_create(2, 8192, sizefunc, compfunc, delkey, deldata); test_threaded_table(table); lruhash_delete(table); }
void lruhash_test(void) { /* start very very small array, so it can do lots of table_grow() */ /* also small in size so that reclaim has to be done quickly. */ struct lruhash* table ; unit_show_feature("lruhash"); table = lruhash_create(2, 8192, test_slabhash_sizefunc, test_slabhash_compfunc, test_slabhash_delkey, test_slabhash_deldata, NULL); test_bin_find_entry(table); test_lru(table); test_short_table(table); test_long_table(table); lruhash_delete(table); table = lruhash_create(2, 8192, test_slabhash_sizefunc, test_slabhash_compfunc, test_slabhash_delkey, test_slabhash_deldata, NULL); test_threaded_table(table); lruhash_delete(table); }
struct slabhash* slabhash_create(size_t numtables, size_t start_size, size_t maxmem, lruhash_sizefunc_type sizefunc, lruhash_compfunc_type compfunc, lruhash_delkeyfunc_type delkeyfunc, lruhash_deldatafunc_type deldatafunc, void* arg) { size_t i; struct slabhash* sl = (struct slabhash*)calloc(1, sizeof(struct slabhash)); if(!sl) return NULL; sl->size = numtables; log_assert(sl->size > 0); sl->array = (struct lruhash**)calloc(sl->size, sizeof(struct lruhash*)); if(!sl->array) { free(sl); return NULL; } sl->mask = (uint32_t)(sl->size - 1); if(sl->mask == 0) { sl->shift = 0; } else { log_assert( (sl->size & sl->mask) == 0 /* size must be power of 2 */ ); sl->shift = 0; while(!(sl->mask & 0x80000000)) { sl->mask <<= 1; sl->shift ++; } } for(i=0; i<sl->size; i++) { sl->array[i] = lruhash_create(start_size, maxmem / sl->size, sizefunc, compfunc, delkeyfunc, deldatafunc, arg); if(!sl->array[i]) { slabhash_delete(sl); return NULL; } } return sl; }