Commit 1b716a39 authored by Ondřej Surý's avatar Ondřej Surý
Browse files

Simplify isc_mem_create() to always use defaults and never fail

Previously, the isc_mem_create() and isc_mem_createx() functions took `max_size`
and `target_size` as first two arguments.  Those values were never used in the
BIND 9 code.  The refactoring removes those arguments and let BIND 9 always use
the default values.

Previously, the isc_mem_create() and isc_mem_createx() functions could have
failed because of failed memory allocation.  As this was no longer true and the
functions have always returned ISC_R_SUCCESS, the have been refactored to return
void.
parent 3be71081
......@@ -60,8 +60,7 @@ int ctx_init(void) {
goto done;
}
if (isc_mem_create(0, 0, &ctx.mem) != ISC_R_SUCCESS)
goto done;
isc_mem_create(&ctx.mem);
if (isc_log_create(ctx.mem, &ctx.log, &ctx.logcfg) != ISC_R_SUCCESS)
goto done;
......@@ -337,6 +336,3 @@ struct DNSConf {
%readonly
dns_c_ctx_t *confctx;
};
......@@ -282,8 +282,8 @@ rudimentary initialization of both.
isc_log_t *lctx;
isc_logconfig_t *lcfg;
if (isc_mem_create(0, 0, &mctx) != ISC_R_SUCCESS) ||
isc_log_create(mctx, &lctx, &lcfg) != ISC_R_SUCCESS))
isc_mem_create(&mctx);
if (isc_log_create(mctx, &lctx, &lcfg) != ISC_R_SUCCESS))
oops_it_didnt_work();
3) Initalize any additional libraries. The convention for the name of
......
......@@ -504,7 +504,7 @@ memory has not been freed when BIND shuts down.
To create a basic memory context, use:
isc_mem_t *mctx = NULL;
result = isc_mem_create(0, 0, &mctx);
isc_mem_create(&mctx);
(The zeroes are tuning parameters, `max_size` and `target_size`: Any
allocations smaller than `max_size` will be satisfied by getting
......@@ -1069,9 +1069,10 @@ the following steps need to be taken to initialize it.
isc_log_t *lctx;
isc_logconfig_t *lcfg;
if (isc_mem_create(0, 0, &mctx) != ISC_R_SUCCESS) ||
isc_log_create(mctx, &lctx, &lcfg) != ISC_R_SUCCESS))
isc_mem_create(&mctx);
if (isc_log_create(mctx, &lctx, &lcfg) != ISC_R_SUCCESS)) {
oops_it_didnt_work();
}
1. Initalize any additional libraries. The convention for the name of
the initialization function is `{library}_log_init()`, with a pointer to
......
......@@ -252,30 +252,16 @@ struct isc_mempool {
} while (0)
/*@{*/
isc_result_t
isc_mem_create(size_t max_size, size_t target_size,
isc_mem_t **mctxp);
void
isc_mem_create(isc_mem_t **mctxp);
isc_result_t
isc_mem_createx(size_t max_size, size_t target_size,
isc_memalloc_t memalloc, isc_memfree_t memfree,
void
isc_mem_createx(isc_memalloc_t memalloc, isc_memfree_t memfree,
void *arg, isc_mem_t **mctxp, unsigned int flags);
/*!<
* \brief Create a memory context.
*
* 'max_size' and 'target_size' are tuning parameters. When
* ISC_MEMFLAG_INTERNAL is set, allocations smaller than 'max_size'
* will be satisfied by getting blocks of size 'target_size' from the
* system allocator and breaking them up into pieces; larger allocations
* will use the system allocator directly. If 'max_size' and/or
* 'target_size' are zero, default values will be * used. When
* ISC_MEMFLAG_INTERNAL is not set, 'target_size' is ignored.
*
* 'max_size' is also used to size the statistics arrays and the array
* used to record active memory when ISC_MEM_DEBUGRECORD is set. Setting
* 'max_size' too low can have detrimental effects on performance.
*
* A memory context created using isc_mem_createx() will obtain
* memory from the system by calling 'memalloc' and 'memfree',
* passing them the argument 'arg'. A memory context created
......
......@@ -725,9 +725,8 @@ initialize_action(void) {
* Public.
*/
isc_result_t
isc_mem_createx(size_t init_max_size, size_t target_size,
isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
void
isc_mem_createx(isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
isc_mem_t **ctxp, unsigned int flags)
{
isc__mem_t *ctx;
......@@ -748,10 +747,7 @@ isc_mem_createx(size_t init_max_size, size_t target_size,
isc_mutex_init(&ctx->lock);
}
if (init_max_size == 0U)
ctx->max_size = DEF_MAX_SIZE;
else
ctx->max_size = init_max_size;
ctx->max_size = DEF_MAX_SIZE;
ctx->flags = flags;
isc_refcount_init(&ctx->references, 1);
memset(ctx->name, 0, sizeof(ctx->name));
......@@ -798,10 +794,7 @@ isc_mem_createx(size_t init_max_size, size_t target_size,
ctx->maxmalloced += (ctx->max_size+1) * sizeof(struct stats);
if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
if (target_size == 0U)
ctx->mem_target = DEF_MEM_TARGET;
else
ctx->mem_target = target_size;
ctx->mem_target = DEF_MEM_TARGET;
ctx->freelists = (memalloc)(arg, ctx->max_size *
sizeof(element *));
RUNTIME_CHECK(ctx->freelists != NULL);
......@@ -830,8 +823,6 @@ isc_mem_createx(size_t init_max_size, size_t target_size,
UNLOCK(&contextslock);
*ctxp = (isc_mem_t *)ctx;
return (ISC_R_SUCCESS);
}
static void
......@@ -2371,11 +2362,10 @@ isc_mem_renderjson(void *memobj0) {
}
#endif /* HAVE_JSON_C */
isc_result_t
isc_mem_create(size_t init_max_size, size_t target_size, isc_mem_t **mctxp) {
return (isc_mem_createx(init_max_size, target_size,
default_memalloc, default_memfree,
NULL, mctxp, isc_mem_defaultflags));
void
isc_mem_create(isc_mem_t **mctxp) {
isc_mem_createx(default_memalloc, default_memfree,
NULL, mctxp, isc_mem_defaultflags);
}
void *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment