* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: mem.c,v 1.121 2005/06/10 07:00:19 marka Exp $ */
+/* $Id: mem.c,v 1.122 2005/06/17 02:22:44 marka Exp $ */
/*! \file */
#endif
LIBISC_EXTERNAL_DATA unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING;
-/*!
- * Define ISC_MEM_USE_INTERNAL_MALLOC=1 to use the internal malloc()
- * implementation in preference to the system one. The internal malloc()
- * is very space-efficient, and quite fast on uniprocessor systems. It
- * performs poorly on multiprocessor machines.
- * JT: we can overcome the performance issue on multiprocessor machines
- * by carefully separating memory contexts.
- */
-#ifndef ISC_MEM_USE_INTERNAL_MALLOC
-#define ISC_MEM_USE_INTERNAL_MALLOC 1
-#endif
-
/*
* Constants.
*/
struct stats {
unsigned long gets;
unsigned long totalgets;
-#if ISC_MEM_USE_INTERNAL_MALLOC
unsigned long blocks;
unsigned long freefrags;
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
};
#define MEM_MAGIC ISC_MAGIC('M', 'e', 'm', 'C')
void * water_arg;
ISC_LIST(isc_mempool_t) pools;
-#if ISC_MEM_USE_INTERNAL_MALLOC
+ /* ISC_MEMFLAG_INTERNAL */
size_t mem_target;
element ** freelists;
element * basic_blocks;
unsigned int basic_table_size;
unsigned char * lowest;
unsigned char * highest;
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
#if ISC_MEM_TRACKLINES
debuglist_t * debuglist;
}
#endif /* ISC_MEM_TRACKLINES */
-#if ISC_MEM_USE_INTERNAL_MALLOC
static inline size_t
rmsize(size_t size) {
/*
ctx->inuse -= new_size;
}
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
-
/*!
* Perform a malloc, doing memory filling and overrun detection as necessary.
*/
}
}
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
-
/*
* Private.
*/
isc_mem_t **ctxp)
{
return (isc_mem_createx2(init_max_size, target_size, memalloc, memfree,
- arg, ctxp, 0));
+ arg, ctxp, ISC_MEMFLAG_DEFAULT));
}
INSIST((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0);
-#if !ISC_MEM_USE_INTERNAL_MALLOC
- UNUSED(target_size);
-#endif
-
ctx = (memalloc)(arg, sizeof(*ctx));
if (ctx == NULL)
return (ISC_R_NOMEMORY);
ctx->debuglist = NULL;
#endif
ISC_LIST_INIT(ctx->pools);
-
-#if ISC_MEM_USE_INTERNAL_MALLOC
ctx->freelists = NULL;
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+ ctx->basic_blocks = NULL;
+ ctx->basic_table = NULL;
+ ctx->basic_table_count = 0;
+ ctx->basic_table_size = 0;
+ ctx->lowest = NULL;
+ ctx->highest = NULL;
ctx->stats = (memalloc)(arg,
(ctx->max_size+1) * sizeof(struct stats));
}
memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
-#if ISC_MEM_USE_INTERNAL_MALLOC
- if (target_size == 0)
- ctx->mem_target = DEF_MEM_TARGET;
- else
- ctx->mem_target = target_size;
- ctx->freelists = (memalloc)(arg, ctx->max_size * sizeof(element *));
- if (ctx->freelists == NULL) {
- result = ISC_R_NOMEMORY;
- goto error;
+ if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ if (target_size == 0)
+ ctx->mem_target = DEF_MEM_TARGET;
+ else
+ ctx->mem_target = target_size;
+ ctx->freelists = (memalloc)(arg, ctx->max_size *
+ sizeof(element *));
+ if (ctx->freelists == NULL) {
+ result = ISC_R_NOMEMORY;
+ goto error;
+ }
+ memset(ctx->freelists, 0,
+ ctx->max_size * sizeof(element *));
}
- memset(ctx->freelists, 0,
- ctx->max_size * sizeof(element *));
- ctx->basic_blocks = NULL;
- ctx->basic_table = NULL;
- ctx->basic_table_count = 0;
- ctx->basic_table_size = 0;
- ctx->lowest = NULL;
- ctx->highest = NULL;
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
#if ISC_MEM_TRACKLINES
if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) {
if (ctx != NULL) {
if (ctx->stats != NULL)
(memfree)(arg, ctx->stats);
-#if ISC_MEM_USE_INTERNAL_MALLOC
if (ctx->freelists != NULL)
(memfree)(arg, ctx->freelists);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
#if ISC_MEM_TRACKLINES
if (ctx->debuglist != NULL)
(ctx->memfree)(ctx->arg, ctx->debuglist);
{
return (isc_mem_createx2(init_max_size, target_size,
default_memalloc, default_memfree, NULL,
- ctxp, 0));
+ ctxp, ISC_MEMFLAG_DEFAULT));
}
isc_result_t
ctx->magic = 0;
-#if ISC_MEM_USE_INTERNAL_MALLOC
INSIST(ISC_LIST_EMPTY(ctx->pools));
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
#if ISC_MEM_TRACKLINES
if (ctx->debuglist != NULL) {
(ctx->memfree)(ctx->arg, ctx->stats);
-#if ISC_MEM_USE_INTERNAL_MALLOC
- for (i = 0; i < ctx->basic_table_count; i++)
- (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
- (ctx->memfree)(ctx->arg, ctx->freelists);
- (ctx->memfree)(ctx->arg, ctx->basic_table);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+ 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->arg, ctx->freelists);
+ (ctx->memfree)(ctx->arg, ctx->basic_table);
+ }
ondest = ctx->ondestroy;
return;
}
-#if ISC_MEM_USE_INTERNAL_MALLOC
- MCTXLOCK(ctx, &ctx->lock);
- mem_putunlocked(ctx, ptr, size);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- mem_put(ctx, ptr, size);
- MCTXLOCK(ctx, &ctx->lock);
- mem_putstats(ctx, ptr, size);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+
+ if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ MCTXLOCK(ctx, &ctx->lock);
+ mem_putunlocked(ctx, ptr, size);
+ } else {
+ mem_put(ctx, ptr, size);
+ MCTXLOCK(ctx, &ctx->lock);
+ mem_putstats(ctx, ptr, size);
+ }
DELETE_TRACE(ctx, ptr, size, file, line);
INSIST(ctx->references > 0);
if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0)
return (isc__mem_allocate(ctx, size FLARG_PASS));
-#if ISC_MEM_USE_INTERNAL_MALLOC
- MCTXLOCK(ctx, &ctx->lock);
- ptr = mem_getunlocked(ctx, size);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- ptr = mem_get(ctx, size);
- MCTXLOCK(ctx, &ctx->lock);
- if (ptr != NULL)
- mem_getstats(ctx, size);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+
+ if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ MCTXLOCK(ctx, &ctx->lock);
+ ptr = mem_getunlocked(ctx, size);
+ } else {
+ ptr = mem_get(ctx, size);
+ MCTXLOCK(ctx, &ctx->lock);
+ if (ptr != NULL)
+ mem_getstats(ctx, size);
+ }
ADD_TRACE(ctx, ptr, size, file, line);
if (ctx->hi_water != 0U && !ctx->hi_called &&
return;
}
-#if ISC_MEM_USE_INTERNAL_MALLOC
- MCTXLOCK(ctx, &ctx->lock);
- mem_putunlocked(ctx, ptr, size);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- mem_put(ctx, ptr, size);
- MCTXLOCK(ctx, &ctx->lock);
- mem_putstats(ctx, ptr, size);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+ if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ MCTXLOCK(ctx, &ctx->lock);
+ mem_putunlocked(ctx, ptr, size);
+ } else {
+ mem_put(ctx, ptr, size);
+ MCTXLOCK(ctx, &ctx->lock);
+ mem_putstats(ctx, ptr, size);
+ }
DELETE_TRACE(ctx, ptr, size, file, line);
fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
(i == ctx->max_size) ? ">=" : " ",
(unsigned long) i, s->totalgets, s->gets);
-#if ISC_MEM_USE_INTERNAL_MALLOC
- if (s->blocks != 0 || s->freefrags != 0)
+ if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
+ (s->blocks != 0 || s->freefrags != 0))
fprintf(out, " (%lu bl, %lu ff)",
s->blocks, s->freefrags);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
fputc('\n', out);
}
size += ALIGNMENT_SIZE;
if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
size += ALIGNMENT_SIZE;
-#if ISC_MEM_USE_INTERNAL_MALLOC
- si = mem_getunlocked(ctx, size);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- si = mem_get(ctx, size);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+
+ if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0)
+ si = mem_getunlocked(ctx, size);
+ else
+ si = mem_get(ctx, size);
+
if (si == NULL)
return (NULL);
if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
REQUIRE(VALID_CONTEXT(ctx));
-#if ISC_MEM_USE_INTERNAL_MALLOC
- MCTXLOCK(ctx, &ctx->lock);
- si = isc__mem_allocateunlocked(ctx, size);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- si = isc__mem_allocateunlocked(ctx, size);
- MCTXLOCK(ctx, &ctx->lock);
- if (si != NULL)
- mem_getstats(ctx, si[-1].u.size);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+ if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ MCTXLOCK(ctx, &ctx->lock);
+ si = isc__mem_allocateunlocked(ctx, size);
+ } else {
+ si = isc__mem_allocateunlocked(ctx, size);
+ MCTXLOCK(ctx, &ctx->lock);
+ if (si != NULL)
+ mem_getstats(ctx, si[-1].u.size);
+ }
#if ISC_MEM_TRACKLINES
ADD_TRACE(ctx, si, si[-1].u.size, file, line);
size = si->u.size;
}
-#if ISC_MEM_USE_INTERNAL_MALLOC
- MCTXLOCK(ctx, &ctx->lock);
- mem_putunlocked(ctx, si, size);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- mem_put(ctx, si, size);
- MCTXLOCK(ctx, &ctx->lock);
- mem_putstats(ctx, si, size);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+ if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ MCTXLOCK(ctx, &ctx->lock);
+ mem_putunlocked(ctx, si, size);
+ } else {
+ mem_put(ctx, si, size);
+ MCTXLOCK(ctx, &ctx->lock);
+ mem_putstats(ctx, si, size);
+ }
DELETE_TRACE(ctx, ptr, size, file, line);
item = mpctx->items;
mpctx->items = item->next;
-#if ISC_MEM_USE_INTERNAL_MALLOC
- mem_putunlocked(mctx, item, mpctx->size);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- mem_put(mctx, item, mpctx->size);
- mem_putstats(mctx, item, mpctx->size);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+ if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ mem_putunlocked(mctx, item, mpctx->size);
+ } else {
+ mem_put(mctx, item, mpctx->size);
+ mem_putstats(mctx, item, mpctx->size);
+ }
}
MCTXUNLOCK(mctx, &mctx->lock);
*/
MCTXLOCK(mctx, &mctx->lock);
for (i = 0; i < mpctx->fillcount; i++) {
-#if ISC_MEM_USE_INTERNAL_MALLOC
- item = mem_getunlocked(mctx, mpctx->size);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- item = mem_get(mctx, mpctx->size);
- if (item != NULL)
- mem_getstats(mctx, mpctx->size);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+ if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ item = mem_getunlocked(mctx, mpctx->size);
+ } else {
+ item = mem_get(mctx, mpctx->size);
+ if (item != NULL)
+ mem_getstats(mctx, mpctx->size);
+ }
if (item == NULL)
break;
item->next = mpctx->items;
* If our free list is full, return this to the mctx directly.
*/
if (mpctx->freecount >= mpctx->freemax) {
-#if ISC_MEM_USE_INTERNAL_MALLOC
- MCTXLOCK(mctx, &mctx->lock);
- mem_putunlocked(mctx, mem, mpctx->size);
- MCTXUNLOCK(mctx, &mctx->lock);
-#else /* ISC_MEM_USE_INTERNAL_MALLOC */
- mem_put(mctx, mem, mpctx->size);
- MCTXLOCK(mctx, &mctx->lock);
- mem_putstats(mctx, mem, mpctx->size);
- MCTXUNLOCK(mctx, &mctx->lock);
-#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
+ if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
+ MCTXLOCK(mctx, &mctx->lock);
+ mem_putunlocked(mctx, mem, mpctx->size);
+ MCTXUNLOCK(mctx, &mctx->lock);
+ } else {
+ mem_put(mctx, mem, mpctx->size);
+ MCTXLOCK(mctx, &mctx->lock);
+ mem_putstats(mctx, mem, mpctx->size);
+ MCTXUNLOCK(mctx, &mctx->lock);
+ }
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return;