#define ISC_MEM_HIWATER 1
typedef void (*isc_mem_water_t)(void *, int);
-typedef void * (*isc_memalloc_t)(void *, size_t);
-typedef void (*isc_memfree_t)(void *, void *);
-
/*%
* Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
* allocation and freeing by file and line number.
#endif
/*
- * Flags for isc_mem_createx() calls.
+ * Flags for isc_mem_create() calls.
*/
#define ISC_MEMFLAG_NOLOCK 0x00000001 /* no lock is necessary */
#define ISC_MEMFLAG_INTERNAL 0x00000002 /* use internal malloc */
#define ISC_MEMFLAG_FILL 0x00000004 /* fill with pattern after alloc and frees */
#if !ISC_MEM_USE_INTERNAL_MALLOC
-#define ISC_MEMFLAG_DEFAULT 0
+#define ISC_MEMFLAG_DEFAULT 0
#else
#define ISC_MEMFLAG_DEFAULT ISC_MEMFLAG_INTERNAL|ISC_MEMFLAG_FILL
#endif
void
isc_mem_create(isc_mem_t **mctxp);
-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.
*
- * 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
- * using isc_mem_create() will use the standard library malloc()
- * and free().
- *
- * If ISC_MEMFLAG_NOLOCK is set in 'flags', the corresponding memory context
- * will be accessed without locking. The user who creates the context must
- * ensure there be no race. Since this can be a source of bug, it is generally
- * inadvisable to use this flag unless the user is very sure about the race
- * condition and the access to the object is highly performance sensitive.
- *
* Requires:
* mctxp != NULL && *mctxp == NULL */
/*@}*/
*/
static uint64_t totallost;
+typedef void * (*isc__memalloc_t)(size_t);
+typedef void (*isc__memfree_t)(void *);
+
struct isc__mem {
isc_mem_t common;
unsigned int flags;
isc_mutex_t lock;
- isc_memalloc_t memalloc;
- isc_memfree_t memfree;
- void * arg;
+ isc__memalloc_t memalloc;
+ isc__memfree_t memfree;
size_t max_size;
- bool checkfree;
+ bool checkfree;
struct stats * stats;
isc_refcount_t references;
char name[16];
size_t maxmalloced;
size_t hi_water;
size_t lo_water;
- bool hi_called;
- bool is_overmem;
+ bool hi_called;
+ bool is_overmem;
isc_mem_water_t water;
void * water_arg;
ISC_LIST(isc__mempool_t) pools;
INSIST(ctx->basic_table_count <= ctx->basic_table_size);
if (ctx->basic_table_count == ctx->basic_table_size) {
table_size = ctx->basic_table_size + TABLE_INCREMENT;
- table = (ctx->memalloc)(ctx->arg,
- table_size * sizeof(unsigned char *));
+ table = (ctx->memalloc)(table_size * sizeof(unsigned char *));
RUNTIME_CHECK(table != NULL);
ctx->malloced += table_size * sizeof(unsigned char *);
if (ctx->malloced > ctx->maxmalloced)
memmove(table, ctx->basic_table,
ctx->basic_table_size *
sizeof(unsigned char *));
- (ctx->memfree)(ctx->arg, ctx->basic_table);
+ (ctx->memfree)(ctx->basic_table);
ctx->malloced -= ctx->basic_table_size *
sizeof(unsigned char *);
}
ctx->basic_table_size = table_size;
}
- tmp = (ctx->memalloc)(ctx->arg, NUM_BASIC_BLOCKS * ctx->mem_target);
+ tmp = (ctx->memalloc)(NUM_BASIC_BLOCKS * ctx->mem_target);
RUNTIME_CHECK(tmp != NULL);
ctx->total += NUM_BASIC_BLOCKS * ctx->mem_target;;
ctx->basic_table[ctx->basic_table_count] = tmp;
/*
* memget() was called on something beyond our upper limit.
*/
- ret = (ctx->memalloc)(ctx->arg, size);
+ ret = (ctx->memalloc)(size);
RUNTIME_CHECK(ret != NULL);
ctx->total += size;
ctx->inuse += size;
if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0))
memset(mem, 0xde, size); /* Mnemonic for "dead". */
- (ctx->memfree)(ctx->arg, mem);
+ (ctx->memfree)(mem);
INSIST(ctx->stats[ctx->max_size].gets != 0U);
ctx->stats[ctx->max_size].gets--;
INSIST(size <= ctx->inuse);
#if ISC_MEM_CHECKOVERRUN
size += 1;
#endif
- ret = (ctx->memalloc)(ctx->arg, size);
+ ret = (ctx->memalloc)(size);
RUNTIME_CHECK(ret != NULL);
if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
#endif
if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0))
memset(mem, 0xde, size); /* Mnemonic for "dead". */
- (ctx->memfree)(ctx->arg, mem);
+ (ctx->memfree)(mem);
}
/*!
*/
static void *
-default_memalloc(void *arg, size_t size) {
+default_memalloc(size_t size) {
void *ptr;
- UNUSED(arg);
ptr = malloc(size);
}
static void
-default_memfree(void *arg, void *ptr) {
- UNUSED(arg);
+default_memfree(void *ptr) {
free(ptr);
}
totallost = 0;
}
-/*
- * Public.
- */
-
-void
-isc_mem_createx(isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
- isc_mem_t **ctxp, unsigned int flags)
+static void
+mem_create(isc_mem_t **ctxp, unsigned int flags)
{
isc__mem_t *ctx;
REQUIRE(ctxp != NULL && *ctxp == NULL);
- REQUIRE(memalloc != NULL);
- REQUIRE(memfree != NULL);
STATIC_ASSERT((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0,
"wrong alignment size");
RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
- ctx = (memalloc)(arg, sizeof(*ctx));
- RUNTIME_CHECK(ctx != NULL);
+ ctx = (default_memalloc)(sizeof(*ctx));
if ((flags & ISC_MEMFLAG_NOLOCK) == 0) {
isc_mutex_init(&ctx->lock);
ctx->common.impmagic = MEM_MAGIC;
ctx->common.magic = ISCAPI_MCTX_MAGIC;
ctx->common.methods = (isc_memmethods_t *)&memmethods;
- ctx->memalloc = memalloc;
- ctx->memfree = memfree;
- ctx->arg = arg;
+ ctx->memalloc = default_memalloc;
+ ctx->memfree = default_memfree;
ctx->stats = NULL;
ctx->checkfree = true;
#if ISC_MEM_TRACKLINES
ctx->lowest = NULL;
ctx->highest = NULL;
- ctx->stats = (memalloc)(arg,
- (ctx->max_size+1) * sizeof(struct stats));
+ ctx->stats = (ctx->memalloc)((ctx->max_size+1) * sizeof(struct stats));
RUNTIME_CHECK(ctx->stats != NULL);
memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
ctx->mem_target = DEF_MEM_TARGET;
- ctx->freelists = (memalloc)(arg, ctx->max_size *
+ ctx->freelists = (ctx->memalloc)(ctx->max_size *
sizeof(element *));
RUNTIME_CHECK(ctx->freelists != NULL);
memset(ctx->freelists, 0,
if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0)) {
unsigned int i;
- ctx->debuglist = (memalloc)(arg, (DEBUG_TABLE_COUNT *
+ ctx->debuglist = (ctx->memalloc)((DEBUG_TABLE_COUNT *
sizeof(debuglist_t)));
RUNTIME_CHECK(ctx->debuglist != NULL);
for (i = 0; i < DEBUG_TABLE_COUNT; i++)
*ctxp = (isc_mem_t *)ctx;
}
+/*
+ * Public.
+ */
+
static void
destroy(isc__mem_t *ctx) {
unsigned int i;
ctx->malloced -= sizeof(*dl);
}
- (ctx->memfree)(ctx->arg, ctx->debuglist);
+ (ctx->memfree)(ctx->debuglist);
ctx->malloced -= DEBUG_TABLE_COUNT * sizeof(debuglist_t);
}
#endif
}
}
- (ctx->memfree)(ctx->arg, ctx->stats);
+ (ctx->memfree)(ctx->stats);
ctx->malloced -= (ctx->max_size+1) * sizeof(struct stats);
if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
for (i = 0; i < ctx->basic_table_count; i++) {
- (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
+ (ctx->memfree)(ctx->basic_table[i]);
ctx->malloced -= NUM_BASIC_BLOCKS * ctx->mem_target;
}
- (ctx->memfree)(ctx->arg, ctx->freelists);
+ (ctx->memfree)(ctx->freelists);
ctx->malloced -= ctx->max_size * sizeof(element *);
if (ctx->basic_table != NULL) {
- (ctx->memfree)(ctx->arg, ctx->basic_table);
+ (ctx->memfree)(ctx->basic_table);
ctx->malloced -= ctx->basic_table_size *
sizeof(unsigned char *);
}
ctx->malloced -= sizeof(*ctx);
if (ctx->checkfree)
INSIST(ctx->malloced == 0);
- (ctx->memfree)(ctx->arg, ctx);
+ (ctx->memfree)(ctx);
}
void
void
isc_mem_create(isc_mem_t **mctxp) {
- isc_mem_createx(default_memalloc, default_memfree,
- NULL, mctxp, isc_mem_defaultflags);
+ mem_create(mctxp, isc_mem_defaultflags);
}
void *
return (0);
}
-static void *
-default_memalloc(void *arg, size_t size) {
- UNUSED(arg);
- if (size == 0U) {
- size = 1;
- }
- return (malloc(size));
-}
-
-static void
-default_memfree(void *arg, void *ptr) {
- UNUSED(arg);
- free(ptr);
-}
-
static int
_teardown(void **state) {
UNUSED(state);
isc_mem_destroy(&localmctx);
- isc_mem_createx(default_memalloc, default_memfree,
- NULL, &localmctx,
- ISC_MEMFLAG_FILL | ISC_MEMFLAG_INTERNAL);
+ isc_mem_create(&localmctx);
result = isc_mempool_create(localmctx, 2, &mp1);
assert_int_equal(result, ISC_R_SUCCESS);
/* Local alloc, free */
mctx2 = NULL;
- isc_mem_createx(default_memalloc, default_memfree,
- NULL, &mctx2, 0);
+ isc_mem_create(&mctx2);
before = isc_mem_total(mctx2);
UNUSED(state);
mctx2 = NULL;
- isc_mem_createx(default_memalloc, default_memfree,
- NULL, &mctx2, 0);
+ isc_mem_create(&mctx2);
before = isc_mem_inuse(mctx2);
ptr = isc_mem_allocate(mctx2, 1024000);
UNUSED(state);
- isc_mem_createx(default_memalloc, default_memfree,
- NULL, &mctx2, 0);
+ isc_mem_create(&mctx2);
isc_mem_debugging = 0;
ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr);
UNUSED(state);
- isc_mem_createx(default_memalloc, default_memfree,
- NULL, &mctx2, 0);
+ isc_mem_create(&mctx2);
ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr);
isc__mem_printactive(mctx2, f);
UNUSED(state);
- isc_mem_createx(default_memalloc, default_memfree,
- NULL, &mctx2, 0);
+ isc_mem_create(&mctx2);
isc_mem_debugging = ISC_MEM_DEBUGTRACE;
ptr = isc_mem_get(mctx2, 2048);
assert_non_null(ptr);