diff --git a/daemon/daemon.c b/daemon/daemon.c index b98f18c0c..e02719f07 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -803,9 +803,8 @@ void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg) { daemon->cfg = cfg; config_apply(cfg); - if(!daemon->env->msg_cache || - cfg->msg_cache_slabs != daemon->env->msg_cache->size || - (cfg->msg_cache_size/cfg->msg_cache_slabs)*cfg->msg_cache_slabs != slabhash_get_size(daemon->env->msg_cache)) { + if(!slabhash_is_size(daemon->env->msg_cache, cfg->msg_cache_size, + cfg->msg_cache_slabs)) { slabhash_delete(daemon->env->msg_cache); daemon->env->msg_cache = slabhash_create(cfg->msg_cache_slabs, HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size, diff --git a/libunbound/context.c b/libunbound/context.c index dbe1cfcb0..20b463f7c 100644 --- a/libunbound/context.c +++ b/libunbound/context.c @@ -71,9 +71,8 @@ context_finalize(struct ub_ctx* ctx) return UB_INITFAIL; if(!auth_zones_apply_cfg(ctx->env->auth_zones, cfg, 1)) return UB_INITFAIL; - if(!ctx->env->msg_cache || - cfg->msg_cache_slabs != ctx->env->msg_cache->size || - (cfg->msg_cache_size/cfg->msg_cache_slabs)*cfg->msg_cache_slabs != slabhash_get_size(ctx->env->msg_cache)) { + if(!slabhash_is_size(ctx->env->msg_cache, cfg->msg_cache_size, + cfg->msg_cache_slabs)) { slabhash_delete(ctx->env->msg_cache); ctx->env->msg_cache = slabhash_create(cfg->msg_cache_slabs, HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size, diff --git a/services/cache/infra.c b/services/cache/infra.c index 2b9b96e87..6f8fea6ad 100644 --- a/services/cache/infra.c +++ b/services/cache/infra.c @@ -302,12 +302,11 @@ infra_adjust(struct infra_cache* infra, struct config_file* cfg) /* divide cachesize by slabs and multiply by slabs, because if the * cachesize is not an even multiple of slabs, that is the resulting * size of the slabhash */ - if((maxmem/cfg->infra_cache_slabs)*cfg->infra_cache_slabs != slabhash_get_size(infra->hosts) || - cfg->infra_cache_slabs != infra->hosts->size || - cfg->ratelimit_slabs != infra->domain_rates->size || - (cfg->ratelimit_size/cfg->ratelimit_slabs)*cfg->ratelimit_slabs != slabhash_get_size(infra->domain_rates) || - cfg->ip_ratelimit_slabs != infra->client_ip_rates->size || - (cfg->ip_ratelimit_size/cfg->ip_ratelimit_slabs)*cfg->ip_ratelimit_slabs != slabhash_get_size(infra->client_ip_rates)) { + if(!slabhash_is_size(infra->hosts, maxmem, cfg->infra_cache_slabs) || + !slabhash_is_size(infra->domain_rates, cfg->ratelimit_size, + cfg->ratelimit_slabs) || + !slabhash_is_size(infra->client_ip_rates, cfg->ip_ratelimit_size, + cfg->ip_ratelimit_slabs)) { infra_delete(infra); infra = infra_create(cfg); } else { diff --git a/services/cache/rrset.c b/services/cache/rrset.c index 26c1aeb91..8c0251bcb 100644 --- a/services/cache/rrset.c +++ b/services/cache/rrset.c @@ -81,8 +81,8 @@ void rrset_cache_delete(struct rrset_cache* r) struct rrset_cache* rrset_cache_adjust(struct rrset_cache *r, struct config_file* cfg, struct alloc_cache* alloc) { - if(!r || !cfg || cfg->rrset_cache_slabs != r->table.size || - cfg->rrset_cache_size != slabhash_get_size(&r->table)) + if(!r || !cfg || !slabhash_is_size(&r->table, cfg->rrset_cache_size, + cfg->rrset_cache_slabs)) { rrset_cache_delete(r); r = rrset_cache_create(cfg, alloc); diff --git a/util/storage/slabhash.c b/util/storage/slabhash.c index ae63b9772..a6c3d0fa6 100644 --- a/util/storage/slabhash.c +++ b/util/storage/slabhash.c @@ -153,6 +153,19 @@ size_t slabhash_get_size(struct slabhash* sl) return total; } +int slabhash_is_size(struct slabhash* sl, size_t size, size_t slabs) +{ + /* divide by slabs and then multiply by the number of slabs, + * because if the size is not an even multiple of slabs, the + * uneven amount needs to be removed for comparison */ + if(!sl) return 0; + if(sl->size != slabs) return 0; + if(slabs == 0) return 0; + if( (size/slabs)*slabs == slabhash_get_size(sl)) + return 1; + return 0; +} + size_t slabhash_get_mem(struct slabhash* sl) { size_t i, total = sizeof(*sl); diff --git a/util/storage/slabhash.h b/util/storage/slabhash.h index 2ecf6fe71..4ecb60421 100644 --- a/util/storage/slabhash.h +++ b/util/storage/slabhash.h @@ -152,6 +152,15 @@ void slabhash_status(struct slabhash* table, const char* id, int extended); */ size_t slabhash_get_size(struct slabhash* table); +/** + * See if slabhash is of given (size, slabs) configuration. + * @param table: hash table + * @param size: max size to test for + * @param slabs: slab count to test for. + * @return true if equal + */ +int slabhash_is_size(struct slabhash* table, size_t size, size_t slabs); + /** * Retrieve slab hash current memory use. * @param table: hash table.