static inline void *
mem_getunlocked(isc__mem_t *ctx, size_t size) {
- size_t new_size = quantize(size);
- void *ret;
-
- if (new_size >= ctx->max_size) {
- /*
- * memget() was called on something beyond our upper limit.
- */
- if (ctx->quota != 0U && ctx->total + size > ctx->quota) {
- ret = NULL;
- goto done;
- }
- ret = (ctx->memalloc)(ctx->arg, size);
- if (ret == NULL) {
- ctx->memalloc_failures++;
- goto done;
- }
- ctx->total += size;
- ctx->inuse += size;
- ctx->stats[ctx->max_size].gets++;
- ctx->stats[ctx->max_size].totalgets++;
- ctx->malloced += size;
- if (ctx->malloced > ctx->maxmalloced)
- ctx->maxmalloced = ctx->malloced;
- /*
- * If we don't set new_size to size, then the
- * ISC_MEMFLAG_FILL code might write over bytes we don't
- * own.
- */
- new_size = size;
- goto done;
- }
- /*
- * If there are no blocks in the free list for this size, get a chunk
- * of memory and then break it up into "new_size"-sized blocks, adding
- * them to the free list.
- */
- if (ctx->freelists[new_size] == NULL && !more_frags(ctx, new_size))
- return (NULL);
-
- /*
- * The free list uses the "rounded-up" size "new_size".
- */
-
- ret = ctx->freelists[new_size];
- ctx->freelists[new_size] = ctx->freelists[new_size]->next;
-
-
- /*
- * The stats[] uses the _actual_ "size" requested by the
- * caller, with the caveat (in the code above) that "size" >= the
- * max. size (max_size) ends up getting recorded as a call to
- * max_size.
- */
- ctx->stats[size].gets++;
- ctx->stats[size].totalgets++;
- ctx->stats[new_size].freefrags--;
- ctx->inuse += new_size;
-
- done:
- if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0) &&
- ISC_LIKELY(ret != NULL))
- memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
-
- return (ret);
+ UNUSED(ctx);
+ return (malloc(size));
}
#if ISC_MEM_CHECKOVERRUN
/* coverity[+free : arg-1] */
static inline void
mem_putunlocked(isc__mem_t *ctx, void *mem, size_t size) {
- size_t new_size = quantize(size);
-
- if (new_size >= ctx->max_size) {
- /*
- * memput() called on something beyond our upper limit.
- */
- if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0))
- memset(mem, 0xde, size); /* Mnemonic for "dead". */
-
- (ctx->memfree)(ctx->arg, mem);
- INSIST(ctx->stats[ctx->max_size].gets != 0U);
- ctx->stats[ctx->max_size].gets--;
- INSIST(size <= ctx->inuse);
- ctx->inuse -= size;
- ctx->malloced -= size;
- return;
- }
-
- if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
-#if ISC_MEM_CHECKOVERRUN
- check_overrun(mem, size, new_size);
-#endif
- memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
- }
-
- /*
- * The free list uses the "rounded-up" size "new_size".
- */
- ((element *)mem)->next = ctx->freelists[new_size];
- ctx->freelists[new_size] = (element *)mem;
-
- /*
- * The stats[] uses the _actual_ "size" requested by the
- * caller, with the caveat (in the code above) that "size" >= the
- * max. size (max_size) ends up getting recorded as a call to
- * max_size.
- */
- INSIST(ctx->stats[size].gets != 0U);
- ctx->stats[size].gets--;
- ctx->stats[new_size].freefrags++;
- ctx->inuse -= new_size;
+ UNUSED(ctx);
+ UNUSED(size);
+ free(mem);
}
/*!
void *
isc__mem_get(isc_mem_t *ctx0, size_t size FLARG) {
- isc__mem_t *ctx = (isc__mem_t *)ctx0;
- void *ptr;
- isc_boolean_t call_water = ISC_FALSE;
-
- REQUIRE(VALID_CONTEXT(ctx));
-
- if (ISC_UNLIKELY((isc_mem_debugging &
- (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0))
- return (isc__mem_allocate(ctx0, size FLARG_PASS));
-
- 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->inuse > ctx->hi_water) {
- ctx->is_overmem = ISC_TRUE;
- if (!ctx->hi_called)
- call_water = ISC_TRUE;
- }
- if (ctx->inuse > ctx->maxinuse) {
- ctx->maxinuse = ctx->inuse;
- if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
- (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
- fprintf(stderr, "maxinuse = %lu\n",
- (unsigned long)ctx->inuse);
- }
- MCTXUNLOCK(ctx, &ctx->lock);
-
- if (call_water && (ctx->water != NULL))
- (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
-
- return (ptr);
+ UNUSED(ctx0);
+ return (malloc(size));
}
void
isc__mem_put(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
- isc__mem_t *ctx = (isc__mem_t *)ctx0;
- isc_boolean_t call_water = ISC_FALSE;
- size_info *si;
- size_t oldsize;
-
- REQUIRE(VALID_CONTEXT(ctx));
- REQUIRE(ptr != NULL);
-
- if (ISC_UNLIKELY((isc_mem_debugging &
- (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0))
- {
- if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
- si = &(((size_info *)ptr)[-1]);
- oldsize = si->u.size - ALIGNMENT_SIZE;
- if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
- oldsize -= ALIGNMENT_SIZE;
- INSIST(oldsize == size);
- }
- isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
- return;
- }
-
- MCTXLOCK(ctx, &ctx->lock);
-
- DELETE_TRACE(ctx, ptr, size, file, line);
-
- if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
- mem_putunlocked(ctx, ptr, size);
- } else {
- mem_putstats(ctx, ptr, size);
- mem_put(ctx, ptr, size);
- }
-
- /*
- * The check against ctx->lo_water == 0 is for the condition
- * when the context was pushed over hi_water but then had
- * isc_mem_setwater() called with 0 for hi_water and lo_water.
- */
- if ((ctx->inuse < ctx->lo_water) || (ctx->lo_water == 0U)) {
- ctx->is_overmem = ISC_FALSE;
- if (ctx->hi_called)
- call_water = ISC_TRUE;
- }
-
- MCTXUNLOCK(ctx, &ctx->lock);
-
- if (call_water && (ctx->water != NULL))
- (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
+ UNUSED(ctx0);
+ UNUSED(size);
+ free(ptr);
}
void
void *
isc__mem_allocate(isc_mem_t *ctx0, size_t size FLARG) {
- isc__mem_t *ctx = (isc__mem_t *)ctx0;
- size_info *si;
- isc_boolean_t call_water = ISC_FALSE;
-
- REQUIRE(VALID_CONTEXT(ctx));
-
- MCTXLOCK(ctx, &ctx->lock);
- si = mem_allocateunlocked((isc_mem_t *)ctx, size);
- if (((ctx->flags & ISC_MEMFLAG_INTERNAL) == 0) && (si != NULL))
- mem_getstats(ctx, si[-1].u.size);
-
- ADD_TRACE(ctx, si, si[-1].u.size, file, line);
- if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
- !ctx->is_overmem) {
- ctx->is_overmem = ISC_TRUE;
- }
-
- if (ctx->hi_water != 0U && !ctx->hi_called &&
- ctx->inuse > ctx->hi_water) {
- ctx->hi_called = ISC_TRUE;
- call_water = ISC_TRUE;
- }
- if (ctx->inuse > ctx->maxinuse) {
- ctx->maxinuse = ctx->inuse;
- if (ISC_UNLIKELY(ctx->hi_water != 0U &&
- ctx->inuse > ctx->hi_water &&
- (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0))
- fprintf(stderr, "maxinuse = %lu\n",
- (unsigned long)ctx->inuse);
- }
- MCTXUNLOCK(ctx, &ctx->lock);
-
- if (call_water)
- (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
-
- return (si);
+ UNUSED(ctx0);
+ return (malloc(size));
}
void *
isc__mem_reallocate(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
- isc__mem_t *ctx = (isc__mem_t *)ctx0;
- void *new_ptr = NULL;
- size_t oldsize, copysize;
-
- REQUIRE(VALID_CONTEXT(ctx));
-
- /*
- * This function emulates the realloc(3) standard library function:
- * - if size > 0, allocate new memory; and if ptr is non NULL, copy
- * as much of the old contents to the new buffer and free the old one.
- * Note that when allocation fails the original pointer is intact;
- * the caller must free it.
- * - if size is 0 and ptr is non NULL, simply free the given ptr.
- * - this function returns:
- * pointer to the newly allocated memory, or
- * NULL if allocation fails or doesn't happen.
- */
- if (size > 0U) {
- new_ptr = isc__mem_allocate(ctx0, size FLARG_PASS);
- if (new_ptr != NULL && ptr != NULL) {
- oldsize = (((size_info *)ptr)[-1]).u.size;
- INSIST(oldsize >= ALIGNMENT_SIZE);
- oldsize -= ALIGNMENT_SIZE;
- if (ISC_UNLIKELY((isc_mem_debugging &
- ISC_MEM_DEBUGCTX) != 0))
- {
- INSIST(oldsize >= ALIGNMENT_SIZE);
- oldsize -= ALIGNMENT_SIZE;
- }
- copysize = (oldsize > size) ? size : oldsize;
- memmove(new_ptr, ptr, copysize);
- isc__mem_free(ctx0, ptr FLARG_PASS);
- }
- } else if (ptr != NULL)
- isc__mem_free(ctx0, ptr FLARG_PASS);
-
- return (new_ptr);
+ UNUSED(ctx0);
+ return (realloc(ptr, size));
}
void
isc__mem_free(isc_mem_t *ctx0, void *ptr FLARG) {
- isc__mem_t *ctx = (isc__mem_t *)ctx0;
- size_info *si;
- size_t size;
- isc_boolean_t call_water= ISC_FALSE;
-
- REQUIRE(VALID_CONTEXT(ctx));
- REQUIRE(ptr != NULL);
-
- if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
- si = &(((size_info *)ptr)[-2]);
- REQUIRE(si->u.ctx == ctx);
- size = si[1].u.size;
- } else {
- si = &(((size_info *)ptr)[-1]);
- size = si->u.size;
- }
-
- MCTXLOCK(ctx, &ctx->lock);
-
- DELETE_TRACE(ctx, ptr, size, file, line);
-
- if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
- mem_putunlocked(ctx, si, size);
- } else {
- mem_putstats(ctx, si, size);
- mem_put(ctx, si, size);
- }
-
- /*
- * The check against ctx->lo_water == 0 is for the condition
- * when the context was pushed over hi_water but then had
- * isc_mem_setwater() called with 0 for hi_water and lo_water.
- */
- if (ctx->is_overmem &&
- (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
- ctx->is_overmem = ISC_FALSE;
- }
-
- if (ctx->hi_called &&
- (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
- ctx->hi_called = ISC_FALSE;
-
- if (ctx->water != NULL)
- call_water = ISC_TRUE;
- }
- MCTXUNLOCK(ctx, &ctx->lock);
-
- if (call_water)
- (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
+ UNUSED(ctx0);
+ free(ptr);
}
char *
isc__mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
- isc__mem_t *mctx = (isc__mem_t *)mctx0;
- size_t len;
- char *ns;
-
- REQUIRE(VALID_CONTEXT(mctx));
- REQUIRE(s != NULL);
-
- len = strlen(s) + 1;
-
- ns = isc__mem_allocate((isc_mem_t *)mctx, len FLARG_PASS);
-
- if (ns != NULL)
- strlcpy(ns, s, len);
-
- return (ns);
+ UNUSED(mctx0);
+ return (strdup(s));
}
void