mem.c revision 449f4411c9d6a06ba3c8a7a525cefbdbd443117f
19c7b1a0293498a3e36692c59646ed6e15ffc8d0Tinderbox User * Copyright (C) 1997-2001 Internet Software Consortium.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * copyright notice and this permission notice appear in all copies.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt/* $Id: mem.c,v 1.89 2001/02/14 23:05:14 gson Exp $ */
94bd918b63001277f1b28ae4581645f8a835688fBob Halleyunsigned int isc_mem_debugging = 0;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * Define ISC_MEM_USE_INTERNAL_MALLOC=1 to use the internal malloc()
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * implementation in preference to the system one. The internal malloc()
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * is very space-efficient, and quite fast on uniprocessor systems. It
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * performs poorly on multiprocessor machines.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * Constants.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#define ALIGNMENT_SIZE 8 /* must be a power of 2 */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned int count;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeintypedef struct {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * This structure must be ALIGNMENT_SIZE bytes.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned long gets;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned long totalgets;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned long blocks;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned long freefrags;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#define VALID_CONTEXT(c) ((c) != NULL && (c)->magic == MEM_MAGIC)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned int magic;
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt unsigned char ** basic_table;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned char * lowest;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned char * highest;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#endif /* ISC_MEM_USE_INTERNAL_MALLOC */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#define VALID_MEMPOOL(c) ((c) != NULL && (c)->magic == MEMPOOL_MAGIC)
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt /* always unlocked */
94bd918b63001277f1b28ae4581645f8a835688fBob Halley /* locked via the memory context's lock */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein ISC_LINK(isc_mempool_t) link; /* next pool in this mem context */
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews /* optionally locked from here down */
94bd918b63001277f1b28ae4581645f8a835688fBob Halley size_t size; /* size of each item on this pool */
94bd918b63001277f1b28ae4581645f8a835688fBob Halley unsigned int maxalloc; /* max number of items allowed */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned int allocated; /* # of items currently given out */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned int freecount; /* # of items on reserved list */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein unsigned int freemax; /* # of items allowed on free list */
94bd918b63001277f1b28ae4581645f8a835688fBob Halley unsigned int fillcount; /* # of items to fetch on each fill */
94bd918b63001277f1b28ae4581645f8a835688fBob Halley /* Stats only. */
94bd918b63001277f1b28ae4581645f8a835688fBob Halley unsigned int gets; /* # of requests to this pool */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein /* Debugging only. */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein char name[16]; /* printed name in stats reports */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * Private Inline-able.
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt#define ADD_TRACE(a, b, c, d, e)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews#define DELETE_TRACE(a, b, c, d, e)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#define ADD_TRACE(a, b, c, d, e) add_trace_entry(a, b, c, d, e)
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#define DELETE_TRACE(a, b, c, d, e) delete_trace_entry(a, b, c, d, e)
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews#define MEM_TRACE ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt#define MEM_RECORD ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0)
94bd918b63001277f1b28ae4581645f8a835688fBob Halley * mctx must be locked.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic inline void
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinadd_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size
4eb998928b9aef0ceda42d7529980d658138698aEvan Hunt unsigned int i;
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein "add %p size %u "
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews "file %s line %u mctx %p\n"),
561a29af8c54a216e7d30b5b4f6e0d21661654ecMark Andrews for (i = 0 ; i < DEBUGLIST_COUNT ; i++) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic inline void
94bd918b63001277f1b28ae4581645f8a835688fBob Halleydelete_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size,
94bd918b63001277f1b28ae4581645f8a835688fBob Halley unsigned int i;
if (MEM_TRACE)
if (!MEM_RECORD)
for (i = 0 ; i < DEBUGLIST_COUNT ; i++) {
static inline size_t
static inline size_t
if (size == 0)
return (ALIGNMENT_SIZE);
static inline isc_boolean_t
void *new;
unsigned char **table;
unsigned int table_size;
return (ISC_FALSE);
table_size * sizeof (unsigned char *));
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_TRUE);
static inline isc_boolean_t
int i, frags;
void *new;
return (ISC_FALSE);
if (total_size > 0) {
return (ISC_TRUE);
void *ret;
goto done;
goto done;
return (NULL);
done:
#if ISC_MEM_FILL
return (ret);
unsigned char *cp;
cp++;
size++;
#if ISC_MEM_FILL
#if ISC_MEM_FILL
char *ret;
#if ISC_MEM_FILL
# if ISC_MEM_CHECKOVERRUN
return (ret);
#if ISC_MEM_FILL
return (ISC_R_NOMEMORY);
if (init_max_size == 0)
goto error;
if (target_size == 0)
goto error;
goto error;
return (ISC_R_SUCCESS);
if (ctx) {
return (result);
ctxp));
if (want_destroy)
if (want_destroy)
return (res);
void *ptr;
if (call_water) {
return (ptr);
if (call_water) {
for (i = 0 ; i < DEBUGLIST_COUNT ; i++)
size_t i;
const struct stats *s;
return (NULL);
return (si);
char *ns;
return (ns);
return (quota);
return (inuse);
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
goto out;
goto out;
goto out;
out:
return (item);
unsigned int freemax;
return (freemax);
unsigned int freecount;
return (freecount);
unsigned int maxalloc;
return (maxalloc);
unsigned int allocated;
return (allocated);
unsigned int fillcount;
return (fillcount);