mem.c revision 499b34cea04a46823d003d4c0520c8b03e8513cb
/*
* Copyright (C) 1997-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: mem.c,v 1.78 2001/01/09 21:56:16 bwelling Exp $ */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <limits.h>
#include <isc/ondestroy.h>
unsigned int isc_mem_debugging = 0;
/*
* Constants.
*/
#define DEF_MAX_SIZE 1100
#define DEF_MEM_TARGET 4096
#define TABLE_INCREMENT 1024
#define DEBUGLIST_COUNT 1024
/*
* Types.
*/
typedef struct debuglink debuglink_t;
struct debuglink {
const void *ptr[DEBUGLIST_COUNT];
const char *file[DEBUGLIST_COUNT];
unsigned int line[DEBUGLIST_COUNT];
unsigned int count;
};
#else
#define FLARG_PASS
#define FLARG
#endif
struct element {
};
typedef struct {
/*
* This structure must be ALIGNMENT_SIZE bytes.
*/
union {
char bytes[ALIGNMENT_SIZE];
} u;
} size_info;
struct stats {
unsigned long gets;
unsigned long totalgets;
unsigned long blocks;
unsigned long freefrags;
};
struct isc_mem {
unsigned int magic;
void * arg;
unsigned char ** basic_table;
unsigned int basic_table_count;
unsigned int basic_table_size;
unsigned char * lowest;
unsigned char * highest;
unsigned int references;
void * water_arg;
#endif
};
struct isc_mempool {
/* always unlocked */
unsigned int magic; /* magic number */
/* locked via the memory context's lock */
/* optionally locked from here down */
unsigned int maxalloc; /* max number of items allowed */
unsigned int allocated; /* # of items currently given out */
unsigned int freecount; /* # of items on reserved list */
unsigned int freemax; /* # of items allowed on free list */
unsigned int fillcount; /* # of items to fetch on each fill */
/* Stats only. */
unsigned int gets; /* # of requests to this pool */
/* Debugging only. */
#endif
};
/*
* Private Inline-able.
*/
#if ! ISC_MEM_TRACKLINES
#define ADD_TRACE(a, b, c, d, e)
#define DELETE_TRACE(a, b, c, d, e)
#else
#define ADD_TRACE(a, b, c, d, e) add_trace_entry(a, b, c, d, e)
#define DELETE_TRACE(a, b, c, d, e) delete_trace_entry(a, b, c, d, e)
/*
* mctx must be locked.
*/
static inline void
{
unsigned int i;
if (MEM_TRACE)
"add %p size %u "
"file %s line %u mctx %p\n"),
if (!MEM_RECORD)
return;
goto next;
for (i = 0 ; i < DEBUGLIST_COUNT ; i++) {
return;
}
}
next:
}
for (i = 1 ; i < DEBUGLIST_COUNT ; i++) {
}
}
static inline void
{
unsigned int i;
if (MEM_TRACE)
"del %p size %u "
"file %s line %u mctx %p\n"),
if (!MEM_RECORD)
return;
for (i = 0 ; i < DEBUGLIST_COUNT ; i++) {
}
return;
}
}
}
/*
* If we get here, we didn't find the item on the list. We're
* screwed.
*/
}
#endif /* ISC_MEM_TRACKLINES */
static inline size_t
/*
* round down to ALIGNMENT_SIZE
*/
}
static inline size_t
/*
* Round up the result in order to get a size big
* enough to satisfy the request and be aligned on ALIGNMENT_SIZE
* byte boundaries.
*/
if (size == 0)
return (ALIGNMENT_SIZE);
}
static inline void
unsigned char *ptr;
/*
* Unlink a frag of size 'size'.
*/
/*
* Create a frag of size 'new_size' and link it in.
*/
/*
* Create a frag of size 'size - new_size' and link it in.
*/
}
static inline isc_boolean_t
size_t i, doubled_size;
return (ISC_FALSE);
/*
* Try splitting a frag that's at least twice as big as the size
* we want.
*/
for (i = doubled_size;
i += ALIGNMENT_SIZE) {
return (ISC_TRUE);
}
}
/*
* No luck. Try splitting any frag bigger than the size we need.
*/
for (i = new_size + ALIGNMENT_SIZE;
i < doubled_size;
i += ALIGNMENT_SIZE) {
return (ISC_TRUE);
}
}
return (ISC_FALSE);
}
static inline isc_boolean_t
void *new;
unsigned char **table;
unsigned int table_size;
int i;
/* Require: we hold the context lock. */
/*
* Did we hit the quota for this context?
*/
return (ISC_FALSE);
table_size * sizeof (unsigned char *));
return (ISC_FALSE);
if (ctx->basic_table_size != 0) {
sizeof (unsigned char *));
}
}
return (ISC_FALSE);
ctx->basic_table_count++;
for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
}
/*
* curr is now pointing at the last block in the
* array.
*/
return (ISC_TRUE);
}
static inline isc_boolean_t
int i, frags;
void *new;
/*
* Try to get more fragments by chopping up a basic block.
*/
if (!more_basic_blocks(ctx)) {
/*
* We can't get more memory from the OS, or we've
* hit the quota for this context.
*/
/*
* XXXRTH "At quota" notification here.
*/
/*
* Maybe we can split one of our existing
* list frags.
*/
}
}
/*
* Set up a linked-list of blocks of size
* "new_size".
*/
total_size -= new_size;
for (i = 0; i < (frags - 1); i++) {
total_size -= new_size;
}
/*
* Add the remaining fragment of the basic block to a free list.
*/
if (total_size > 0) {
}
/*
* curr is now pointing at the last block in the
* array.
*/
return (ISC_TRUE);
}
static inline void *
void *ret;
/*
* memget() was called on something beyond our upper limit.
*/
goto done;
}
/*
* If we don't set new_size to size, then the
* ISC_MEM_FILL code might write over bytes we
* don't own.
*/
}
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.
*/
return (NULL);
/*
* The free list uses the "rounded-up" size "new_size".
*/
/*
* 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.
*/
done:
#if ISC_MEM_FILL
#endif
return (ret);
}
#if ISC_MEM_FILL && ISC_MEM_CHECKOVERRUN
static inline void
unsigned char *cp;
cp++;
size++;
}
}
#endif
static inline void
/*
* memput() called on something beyond our upper limit.
*/
#if ISC_MEM_FILL
#endif
return;
}
#if ISC_MEM_FILL
#endif
#endif
/*
* The free list uses the "rounded-up" size "new_size".
*/
/*
* 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.
*/
}
/*
* Private.
*/
static void *
}
static void
}
/*
* Public.
*/
{
return (ISC_R_NOMEMORY);
if (init_max_size == 0)
else
if (target_size == 0)
else
return (ISC_R_NOMEMORY);
}
return (ISC_R_NOMEMORY);
}
ctx->basic_table_count = 0;
ctx->basic_table_size = 0;
"isc_mutex_init() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
#endif
return (ISC_R_SUCCESS);
}
{
ctxp));
}
static void
unsigned int i;
else {
}
}
#endif
}
#if 0 /* XXX brister debugging */
for (i = 0; i < ctx->basic_table_count; i++)
#endif
for (i = 0; i < ctx->basic_table_count; i++)
}
void
source->references++;
}
void
ctx->references--;
if (ctx->references == 0)
if (want_destroy)
}
/*
* isc_mem_putanddetach() is the equivalent of:
*
* mctx = NULL;
* isc_mem_attach(ptr->mctx, &mctx);
* isc_mem_detach(&ptr->mctx);
* isc_mem_put(mctx, ptr, sizeof(*ptr);
* isc_mem_detach(&mctx);
*/
void
/*
* Must be before mem_putunlocked() as ctxp is usually within
* [ptr..ptr+size).
*/
ctx->references--;
if (ctx->references == 0)
if (want_destroy)
}
void
/*
* This routine provides legacy support for callers who use mctxs
*/
ctx->references--;
}
return (res);
}
if (result != ISC_R_SUCCESS)
return (result);
}
void *
void *ptr;
}
/* XXX remove */
if (call_water) {
/* XXX remove */
ISC_MSG_CALLING, "calling"),
}
return (ptr);
}
void
{
}
/* XXX remove */
if (call_water) {
/* XXX remove */
ISC_MSG_CALLING, "calling"),
}
}
size_t i;
void *ptr;
break;
}
}
return (result);
}
/*
* Print the stats[] on the stream "out" with suitable formatting.
*/
void
size_t i;
const struct stats *s;
const isc_mempool_t *pool;
continue;
}
}
/*
* Note that since a pool can be locked now, these stats might be
* somewhat off if the pool is in active use at the time the stats
* are dumped. The link fields are protected by the isc_mem_t's
* lock, however, so walking this list and extracting integers from
* stats fields is always safe.
*/
"[Pool statistics]\n"));
ISC_MSG_POOLNAME, "name"),
ISC_MSG_POOLSIZE, "size"),
ISC_MSG_POOLMAXALLOC, "maxalloc"),
ISC_MSG_POOLALLOCATED, "allocated"),
ISC_MSG_POOLFREECOUNT, "freecount"),
ISC_MSG_POOLFREEMAX, "freemax"),
ISC_MSG_POOLFILLCOUNT, "fillcount"),
ISC_MSG_POOLGETS, "gets"),
"L");
}
}
if (MEM_RECORD) {
unsigned int i;
"DUMP OF ALL OUTSTANDING "
"MEMORY ALLOCATIONS\n"));
"\tNone.\n"));
for (i = 0 ; i < DEBUGLIST_COUNT ; i++)
"\tptr %p "
"file %s "
"line %u\n"),
}
}
#endif
}
return (result);
}
/*
* Replacements for malloc() and free() -- they implicitly remember the
* size of the object allocated (with some additional overhead).
*/
static void *
size += ALIGNMENT_SIZE;
return (NULL);
return (&si[1]);
}
void *
#endif
return (si);
}
void
}
/*
* Other useful things.
*/
char *
char *ns;
return (ns);
}
void
}
void
}
/*
* Quotas
*/
void
}
return (quota);
}
return (inuse);
}
void
{
}
} else {
}
}
/*
* Memory pool stuff
*/
#if 0
/*
* Free all but "n" items from the pool's free list. If n == 0, all items
* will be returned to the mctx.
*/
static void
unsigned int count;
return;
}
/*
* All remaining items are to be freed. Lock the context once,
* free them all, and unlock the context.
*/
do {
}
#endif
/*
* Release all items on the free list. No locking is done, the memory
* context must be locked, and the pool if needed.
*/
static void
return;
do {
}
/*
* Allocate space for this pool, initialize values, and if all works
* well, attach to the memory context.
*/
return (ISC_R_NOMEMORY);
}
#endif
return (ISC_R_SUCCESS);
}
void
#else
#endif
}
void
/*
* Return any items on the free list
*/
/*
* Remove our linked list entry from the memory context.
*/
}
void
}
void *
unsigned int i;
/*
* Don't let the caller go over quota
*/
goto out;
}
/*
* if we have a free list item, return the first here
*/
goto out;
}
/*
* We need to dip into the well. Lock the memory context here and
* fill up our free list.
*/
break;
}
/*
* If we didn't get any items, return NULL.
*/
goto out;
out:
}
return (item);
}
void
/*
* If our free list is full, return this to the mctx directly.
*/
return;
}
/*
* Otherwise, attach it to our free list and bump the counter.
*/
}
/*
* Quotas
*/
void
}
unsigned int
unsigned int freemax;
return (freemax);
}
unsigned int
unsigned int freecount;
return (freecount);
}
void
}
unsigned int
unsigned int maxalloc;
return (maxalloc);
}
unsigned int
unsigned int allocated;
return (allocated);
}
void
}
unsigned int
unsigned int fillcount;
return (fillcount);
}