mem.c revision 92f9189aeec4ee6385700dbd7c6bed9cabd2e182
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1997-2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Permission to use, copy, modify, and distribute this software for any
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * purpose with or without fee is hereby granted, provided that the above
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * copyright notice and this permission notice appear in all copies.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian 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.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* $Id: mem.c,v 1.71 2000/12/06 20:34:34 tale Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <stdio.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <stdlib.h>
f4b4e7c16211137332e50bcad3fef0d15639a4f1Brian Wellington#include <stddef.h>
f4b4e7c16211137332e50bcad3fef0d15639a4f1Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <limits.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#include <isc/mem.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/msgs.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/ondestroy.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/string.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/mutex.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/util.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellingtonunsigned int isc_mem_debugging = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Constants.
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DEF_MAX_SIZE 1100
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DEF_MEM_TARGET 4096
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define ALIGNMENT_SIZE 8
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define NUM_BASIC_BLOCKS 64 /* must be > 1 */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define TABLE_INCREMENT 1024
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DEBUGLIST_COUNT 1024
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Types.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#if ISC_MEM_TRACKLINES
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtontypedef struct debuglink debuglink_t;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstruct debuglink {
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington ISC_LINK(debuglink_t) link;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington const void *ptr[DEBUGLIST_COUNT];
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington const char *file[DEBUGLIST_COUNT];
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int line[DEBUGLIST_COUNT];
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int count;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington};
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define FLARG_PASS , file, line
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define FLARG , const char *file, int line
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#else
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#define FLARG_PASS
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define FLARG
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtontypedef struct element element;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstruct element {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington element * next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington};
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtontypedef struct {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * This structure must be ALIGNMENT_SIZE bytes.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington union {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington size_t size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char bytes[ALIGNMENT_SIZE];
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington } u;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington} size_info;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstruct stats {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned long gets;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned long totalgets;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned long blocks;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned long freefrags;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington};
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#define MEM_MAGIC 0x4D656d43U /* MemC. */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#define VALID_CONTEXT(c) ((c) != NULL && (c)->magic == MEM_MAGIC)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstruct isc_mem {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int magic;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_ondestroy_t ondestroy;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mutex_t lock;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_memalloc_t memalloc;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_memfree_t memfree;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington void * arg;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t max_size;
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence size_t mem_target;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington element ** freelists;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein element * basic_blocks;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington unsigned char ** basic_table;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington unsigned int basic_table_count;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington unsigned int basic_table_size;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington unsigned char * lowest;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington unsigned char * highest;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t checkfree;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t trysplit;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington struct stats * stats;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int references;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t quota;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein size_t total;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t inuse;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t hi_water;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t lo_water;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t hi_called;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_water_t water;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington void * water_arg;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST(isc_mempool_t) pools;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEM_TRACKLINES
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington ISC_LIST(debuglink_t) debuglist;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington#endif
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington};
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington#define MEMPOOL_MAGIC 0x4D454d70U /* MEMp. */
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington#define VALID_MEMPOOL(c) ((c) != NULL && (c)->magic == MEMPOOL_MAGIC)
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellingtonstruct isc_mempool {
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington /* always unlocked */
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington unsigned int magic; /* magic number */
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington isc_mutex_t *lock; /* optional lock */
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington isc_mem_t *mctx; /* our memory context */
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington /* locked via the memory context's lock */
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington ISC_LINK(isc_mempool_t) link; /* next pool in this mem context */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* optionally locked from here down */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington element *items; /* low water item list */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington size_t size; /* size of each item on this pool */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int maxalloc; /* max number of items allowed */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int allocated; /* # of items currently given out */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int freecount; /* # of items on reserved list */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int freemax; /* # of items allowed on free list */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int fillcount; /* # of items to fetch on each fill */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Stats only. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int gets; /* # of requests to this pool */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Debugging only. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEMPOOL_NAMES
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char name[16]; /* printed name in stats reports */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#endif
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington};
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Private Inline-able.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ! ISC_MEM_TRACKLINES
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define ADD_TRACE(a, b, c, d, e)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DELETE_TRACE(a, b, c, d, e)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#else
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#define ADD_TRACE(a, b, c, d, e) add_trace_entry(a, b, c, d, e)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#define DELETE_TRACE(a, b, c, d, e) delete_trace_entry(a, b, c, d, e)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#define MEM_TRACE ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#define MEM_RECORD ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * mctx must be locked.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic inline void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonadd_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington FLARG)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington{
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debuglink_t *dl;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int i;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (MEM_TRACE)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_MSG_ADDTRACE,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "add %p size %u "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "file %s line %u mctx %p\n"),
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ptr, size, file, line, mctx);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (!MEM_RECORD)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dl = ISC_LIST_HEAD(mctx->debuglist);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (dl != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (dl->count == DEBUGLIST_COUNT)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington goto next;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington for (i = 0 ; i < DEBUGLIST_COUNT ; i++) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (dl->ptr[i] == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->ptr[i] = ptr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->file[i] = file;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->line[i] = line;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->count++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington next:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl = ISC_LIST_NEXT(dl, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington dl = malloc(sizeof(debuglink_t));
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington INSIST(dl != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LINK_INIT(dl, link);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington for (i = 1 ; i < DEBUGLIST_COUNT ; i++) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington dl->ptr[i] = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->file[i] = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->line[i] = 0;
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington }
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington dl->ptr[0] = ptr;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington dl->file[0] = file;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington dl->line[0] = line;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington dl->count = 1;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_PREPEND(mctx->debuglist, dl, link);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline void
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtondelete_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington const char *file, unsigned int line)
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington{
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington debuglink_t *dl;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int i;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington if (MEM_TRACE)
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington ISC_MSG_DELTRACE,
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington "del %p size %u "
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington "file %s line %u mctx %p\n"),
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington ptr, size, file, line, mctx);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (!MEM_RECORD)
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington return;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington dl = ISC_LIST_HEAD(mctx->debuglist);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington while (dl != NULL) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington for (i = 0 ; i < DEBUGLIST_COUNT ; i++) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (dl->ptr[i] == ptr) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->ptr[i] = NULL;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington dl->file[i] = NULL;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington dl->line[i] = 0;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington INSIST(dl->count > 0);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington dl->count--;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington if (dl->count == 0) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_UNLINK(mctx->debuglist,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free(dl);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington }
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington return;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington dl = ISC_LIST_NEXT(dl, link);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington }
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington /*
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington * If we get here, we didn't find the item on the list. We're
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington * screwed.
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington INSIST(dl != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#endif /* ISC_MEM_TRACKLINES */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline size_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonrmsize(size_t size) {
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington /*
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * round down to ALIGNMENT_SIZE
de32cbd34e78bdd276e69cff239846760d4ee16eBrian Wellington */
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington size -= (size % ALIGNMENT_SIZE);
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington return (size);
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington}
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellingtonstatic inline size_t
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellingtonquantize(size_t size) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int temp;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Round up the result in order to get a size big
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * byte boundaries.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (size == 0)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (ALIGNMENT_SIZE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington temp = size + (ALIGNMENT_SIZE - 1);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (temp - temp % ALIGNMENT_SIZE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonsplit(isc_mem_t *ctx, size_t size, size_t new_size) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned char *ptr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t remaining_size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Unlink a frag of size 'size'.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ptr = (unsigned char *)ctx->freelists[size];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->freelists[size] = ctx->freelists[size]->next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[size].freefrags--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Create a frag of size 'new_size' and link it in.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ((element *)ptr)->next = ctx->freelists[new_size];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->freelists[new_size] = (element *)ptr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[new_size].freefrags++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Create a frag of size 'size - new_size' and link it in.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington remaining_size = size - new_size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ptr += new_size;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ((element *)ptr)->next = ctx->freelists[remaining_size];
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ctx->freelists[remaining_size] = (element *)ptr;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ctx->stats[remaining_size].freefrags++;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson}
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic inline isc_boolean_t
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssontry_split(isc_mem_t *ctx, size_t new_size) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson size_t i, doubled_size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (!ctx->trysplit)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (ISC_FALSE);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson /*
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * Try splitting a frag that's at least twice as big as the size
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * we want.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson doubled_size = new_size * 2;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson for (i = doubled_size;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson i < ctx->max_size;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson i += ALIGNMENT_SIZE) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (ctx->freelists[i] != NULL) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson split(ctx, i, new_size);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (ISC_TRUE);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson /*
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * No luck. Try splitting any frag bigger than the size we need.
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson for (i = new_size + ALIGNMENT_SIZE;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson i < doubled_size;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson i += ALIGNMENT_SIZE) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (ctx->freelists[i] != NULL) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson split(ctx, i, new_size);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (ISC_TRUE);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (ISC_FALSE);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson}
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic inline isc_boolean_t
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonmore_basic_blocks(isc_mem_t *ctx) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson void *new;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson unsigned char *curr, *next;
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews unsigned char *first, *last;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson unsigned char **table;
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews unsigned int table_size;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson size_t increment;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson int i;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington /* Require: we hold the context lock. */
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington /*
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington * Did we hit the quota for this context?
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington */
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington increment = NUM_BASIC_BLOCKS * ctx->mem_target;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->quota != 0 && ctx->total + increment > ctx->quota)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ctx->basic_table_count <= ctx->basic_table_size);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (ctx->basic_table_count == ctx->basic_table_size) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson table_size = ctx->basic_table_size + TABLE_INCREMENT;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson table = (ctx->memalloc)(ctx->arg,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson table_size * sizeof (unsigned char *));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (table == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->basic_table_size != 0) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington memcpy(table, ctx->basic_table,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->basic_table_size *
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson sizeof (unsigned char *));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson (ctx->memfree)(ctx->arg, ctx->basic_table);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->basic_table = table;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ctx->basic_table_size = table_size;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson new = (ctx->memalloc)(ctx->arg, NUM_BASIC_BLOCKS * ctx->mem_target);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (new == NULL)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (ISC_FALSE);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ctx->total += increment;
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington ctx->basic_table[ctx->basic_table_count] = new;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ctx->basic_table_count++;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson curr = new;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson next = curr + ctx->mem_target;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ((element *)curr)->next = (element *)next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington curr = next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington next += ctx->mem_target;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * curr is now pointing at the last block in the
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * array.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ((element *)curr)->next = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington first = new;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (first < ctx->lowest || ctx->lowest == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->lowest = first;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (last > ctx->highest)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->highest = last;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->basic_blocks = new;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_TRUE);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson}
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic inline isc_boolean_t
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonmore_frags(isc_mem_t *ctx, size_t new_size) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington int i, frags;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington size_t total_size;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington void *new;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned char *curr, *next;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Try to get more fragments by chopping up a basic block.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (ctx->basic_blocks == NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (!more_basic_blocks(ctx)) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * We can't get more memory from the OS, or we've
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * hit the quota for this context.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * XXXRTH "At quota" notification here.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Maybe we can split one of our existing
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * list frags.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (try_split(ctx, new_size));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington total_size = ctx->mem_target;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington new = ctx->basic_blocks;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ctx->basic_blocks = ctx->basic_blocks->next;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington frags = total_size / new_size;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ctx->stats[new_size].blocks++;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ctx->stats[new_size].freefrags += frags;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Set up a linked-list of blocks of size
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * "new_size".
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington curr = new;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington next = curr + new_size;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington total_size -= new_size;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington for (i = 0; i < (frags - 1); i++) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ((element *)curr)->next = (element *)next;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington curr = next;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington next += new_size;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington total_size -= new_size;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Add the remaining fragment of the basic block to a free list.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington total_size = rmsize(total_size);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (total_size > 0) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ((element *)next)->next = ctx->freelists[total_size];
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ctx->freelists[total_size] = (element *)next;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ctx->stats[total_size].freefrags++;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson /*
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * curr is now pointing at the last block in the
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * array.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ((element *)curr)->next = NULL;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ctx->freelists[new_size] = new;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (ISC_TRUE);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic inline void *
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonmem_getunlocked(isc_mem_t *ctx, size_t size) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson size_t new_size = quantize(size);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson void *ret;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (size >= ctx->max_size || new_size >= ctx->max_size) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson /*
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * memget() was called on something beyond our upper limit.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (ctx->quota != 0 && ctx->total + size > ctx->quota) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ret = NULL;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson goto done;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ret = (ctx->memalloc)(ctx->arg, size);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (ret != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->total += size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->inuse += size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[ctx->max_size].gets++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[ctx->max_size].totalgets++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * If we don't set new_size to size, then the
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * ISC_MEM_FILL code might write over bytes we
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * don't own.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington new_size = size;
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto done;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * If there are no blocks in the free list for this size, get a chunk
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * of memory and then break it up into "new_size"-sized blocks, adding
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * them to the free list.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->freelists[new_size] == NULL && !more_frags(ctx, new_size))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The free list uses the "rounded-up" size "new_size".
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ret = ctx->freelists[new_size];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->freelists[new_size] = ctx->freelists[new_size]->next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The stats[] uses the _actual_ "size" requested by the
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * caller, with the caveat (in the code above) that "size" >= the
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * max. size (max_size) ends up getting recorded as a call to
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * max_size.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[size].gets++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[size].totalgets++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[new_size].freefrags--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->inuse += new_size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington done:
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEM_FILL
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ret != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington#endif
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington return (ret);
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington}
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington#if ISC_MEM_FILL && ISC_MEM_CHECKOVERRUN
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncheck_overrun(void *mem, size_t size, size_t new_size) {
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington unsigned char *cp;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cp = (unsigned char *)mem;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cp += size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington while (size < new_size) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(*cp == 0xbe);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cp++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonmem_putunlocked(isc_mem_t *ctx, void *mem, size_t size) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t new_size = quantize(size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (size == ctx->max_size || new_size >= ctx->max_size) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * memput() called on something beyond our upper limit.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEM_FILL
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington memset(mem, 0xde, size); /* Mnemonic for "dead". */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (ctx->memfree)(ctx->arg, mem);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ctx->stats[ctx->max_size].gets != 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[ctx->max_size].gets--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(size <= ctx->total);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->inuse -= size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->total -= size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEM_FILL
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEM_CHECKOVERRUN
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_overrun(mem, size, new_size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The free list uses the "rounded-up" size "new_size".
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ((element *)mem)->next = ctx->freelists[new_size];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->freelists[new_size] = (element *)mem;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The stats[] uses the _actual_ "size" requested by the
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * caller, with the caveat (in the code above) that "size" >= the
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * max. size (max_size) ends up getting recorded as a call to
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * max_size.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ctx->stats[size].gets != 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[size].gets--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats[new_size].freefrags++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->inuse -= new_size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Private.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondefault_memalloc(void *arg, size_t size) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNUSED(arg);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (malloc(size));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondefault_memfree(void *arg, void *ptr) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNUSED(arg);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free(ptr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Public.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
c0d2891f6e08fcf5379dfb9a1bf8fbbb63f1952aMark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_result_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_createx(size_t init_max_size, size_t target_size,
c0d2891f6e08fcf5379dfb9a1bf8fbbb63f1952aMark Andrews isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t **ctxp)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t *ctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(ctxp != NULL && *ctxp == NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(memalloc != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(memfree != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx = (memalloc)(arg, sizeof *ctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_R_NOMEMORY);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (init_max_size == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->max_size = DEF_MAX_SIZE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->max_size = init_max_size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (target_size == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->mem_target = DEF_MEM_TARGET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->mem_target = target_size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->memalloc = memalloc;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->memfree = memfree;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->arg = arg;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->freelists = (memalloc)(arg, ctx->max_size * sizeof (element *));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->freelists == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (memfree)(arg, ctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_R_NOMEMORY);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ctx->checkfree = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->trysplit = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington memset(ctx->freelists, 0,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->max_size * sizeof (element *));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->stats = (memalloc)(arg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (ctx->max_size+1) * sizeof (struct stats));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->stats == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (memfree)(arg, ctx->freelists);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (memfree)(arg, ctx);
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington return (ISC_R_NOMEMORY);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof (struct stats));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->basic_blocks = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->basic_table = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->basic_table_count = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->basic_table_size = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->lowest = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->highest = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (isc_mutex_init(&ctx->lock) != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (memfree)(arg, ctx->stats);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (memfree)(arg, ctx->freelists);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (memfree)(arg, ctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "isc_mutex_init() %s",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_MSG_FAILED, "failed"));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (ISC_R_UNEXPECTED);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->references = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->quota = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->total = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->inuse = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->hi_water = 0;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ctx->lo_water = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->hi_called = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->water = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ctx->water_arg = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->magic = MEM_MAGIC;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_ondestroy_init(&ctx->ondestroy);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(ctx->pools);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEM_TRACKLINES
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(ctx->debuglist);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *ctxp = ctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (ISC_R_SUCCESS);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_result_t
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellingtonisc_mem_create(size_t init_max_size, size_t target_size,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t **ctxp)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington{
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews return (isc_mem_createx(init_max_size, target_size,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington default_memalloc, default_memfree, NULL,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctxp));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondestroy(isc_mem_t *ctx) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int i;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_ondestroy_t ondest;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ISC_LIST_EMPTY(ctx->pools));
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#if ISC_MEM_TRACKLINES
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->checkfree)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ISC_LIST_EMPTY(ctx->debuglist));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debuglink_t *dl;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (dl = ISC_LIST_HEAD(ctx->debuglist);
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson dl != NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl = ISC_LIST_HEAD(ctx->debuglist)) {
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington ISC_LIST_UNLINK(ctx->debuglist, dl, link);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington free(dl);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington }
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington#endif
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington INSIST(ctx->references == 0);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington if (ctx->checkfree) {
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington for (i = 0; i <= ctx->max_size; i++)
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington INSIST(ctx->stats[i].gets == 0);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington }
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington#if 0 /* XXX brister debugging */
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington for (i = 0; i < ctx->basic_table_count; i++)
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington memset(ctx->basic_table[i], 0x0,
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington NUM_BASIC_BLOCKS * ctx->mem_target);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington#endif
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (i = 0; i < ctx->basic_table_count; i++)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (ctx->memfree)(ctx->arg, ctx->freelists);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (ctx->memfree)(ctx->arg, ctx->stats);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (ctx->memfree)(ctx->arg, ctx->basic_table);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ondest = ctx->ondestroy;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington DESTROYLOCK(&ctx->lock);
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington (ctx->memfree)(ctx->arg, ctx);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
6e93e6ea4557291e847aced6a88adcdf39f06843Andreas Gustafsson isc_ondestroy_notify(&ondest, ctx);
6e93e6ea4557291e847aced6a88adcdf39f06843Andreas Gustafsson}
6e93e6ea4557291e847aced6a88adcdf39f06843Andreas Gustafsson
6e93e6ea4557291e847aced6a88adcdf39f06843Andreas Gustafssonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(source));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(targetp != NULL && *targetp == NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington LOCK(&source->lock);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington source->references++;
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington UNLOCK(&source->lock);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *targetp = source;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_detach(isc_mem_t **ctxp) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t *ctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t want_destroy = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(ctxp != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx = *ctxp;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ctx->references > 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->references--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->references == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington want_destroy = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (want_destroy)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington destroy(ctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *ctxp = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * isc_mem_putanddetach() is the equivalent of:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * mctx = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * isc_mem_attach(ptr->mctx, &mctx);
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson * isc_mem_detach(&ptr->mctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * isc_mem_put(mctx, ptr, sizeof(*ptr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * isc_mem_detach(&mctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc__mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t *ctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t want_destroy = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(ctxp != NULL);
fefe1106d96d3a89b21315f665b36a67cdea3840Mark Andrews ctx = *ctxp;
fefe1106d96d3a89b21315f665b36a67cdea3840Mark Andrews REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(ptr != NULL);
fefe1106d96d3a89b21315f665b36a67cdea3840Mark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Must be before mem_putunlocked() as ctxp is usually within
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * [ptr..ptr+size).
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *ctxp = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DELETE_TRACE(ctx, ptr, size, file, line);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mem_putunlocked(ctx, ptr, size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ctx->references > 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->references--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->references == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington want_destroy = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (want_destroy)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington destroy(ctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_destroy(isc_mem_t **ctxp) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t *ctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t want_destroy = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * This routine provides legacy support for callers who use mctxs
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * without attaching/detaching.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(ctxp != NULL);
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington ctx = *ctxp;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(ctx->references == 1);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->references--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->references == 0)
0a45f098e446a7f41c2a71d9dd0fd8f459b49c34Brian Wellington want_destroy = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
0a45f098e446a7f41c2a71d9dd0fd8f459b49c34Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (want_destroy)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington destroy(ctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *ctxp = NULL;
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington}
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellingtonisc_result_t
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellingtonisc_mem_ondestroy(isc_mem_t *ctx, isc_task_t *task, isc_event_t **event) {
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington isc_result_t res;
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington LOCK(&ctx->lock);
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington res = isc_ondestroy_register(&ctx->ondestroy, task, event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (res);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_result_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_restore(isc_mem_t *ctx) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_mutex_init(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (result);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc__mem_get(isc_mem_t *ctx, size_t size FLARG) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington void *ptr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t call_water = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ptr = mem_getunlocked(ctx, size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ADD_TRACE(ctx, ptr, size, file, line);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->hi_water != 0 && !ctx->hi_called &&
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->inuse > ctx->hi_water) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->hi_called = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington call_water = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXX remove */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->hi_water)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(stderr,"inuse %u, total %u\n", ctx->inuse, ctx->total);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (call_water) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXX remove */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(stderr, "%s water(%p, ISC_MEM_HIWATER)\n",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_MSG_CALLING, "calling"),
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->water_arg);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ptr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc__mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t call_water = ISC_FALSE;
b7cd261f2fca2c7138cdc6ae8ee434e9c0031303Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(ptr != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DELETE_TRACE(ctx, ptr, size, file, line);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mem_putunlocked(ctx, ptr, size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->hi_called && ctx->inuse < ctx->lo_water) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->hi_called = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington call_water = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXX remove */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->hi_water)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(stderr,"inuse %u, total %u\n", ctx->inuse, ctx->total);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (call_water) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXX remove */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(stderr, "%s water(%p,ISC_MEM_LOWATER)\n",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_MSG_CALLING, "calling"),
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->water_arg);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_result_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_preallocate(isc_mem_t *ctx) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t i;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result = ISC_R_SUCCESS;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington void *ptr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (i = 0; i < ctx->max_size; i += ALIGNMENT_SIZE) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ptr = mem_getunlocked(ctx, i);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ptr == NULL) {
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson result = ISC_R_NOMEMORY;
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson break;
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mem_putunlocked(ctx, ptr, i);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (result);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Print the stats[] on the stream "out" with suitable formatting.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_stats(isc_mem_t *ctx, FILE *out) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t i;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington const struct stats *s;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington const isc_mempool_t *pool;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson LOCK(&ctx->lock);
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->freelists != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (i = 0; i <= ctx->max_size; i++) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington s = &ctx->stats[i];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (s->totalgets == 0 && s->gets == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington continue;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (i == ctx->max_size) ? ">=" : " ",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (unsigned long) i, s->totalgets, s->gets);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (s->blocks != 0 || s->freefrags != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(out, " (%lu bl, %lu ff)",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington s->blocks, s->freefrags);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fputc('\n', out);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Note that since a pool can be locked now, these stats might be
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * somewhat off if the pool is in active use at the time the stats
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * are dumped. The link fields are protected by the isc_mem_t's
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * lock, however, so walking this list and extracting integers from
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * stats fields is always safe.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington pool = ISC_LIST_HEAD(ctx->pools);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (pool != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_MSG_POOLSTATS,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "[Pool statistics]\n"));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_MSG_POOLNAME, "name"),
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_MSG_POOLSIZE, "size"),
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_MSG_POOLMAXALLOC, "maxalloc"),
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_MSG_POOLALLOCATED, "allocated"),
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ISC_MSG_POOLFREECOUNT, "freecount"),
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_MSG_POOLFREEMAX, "freemax"),
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_MSG_POOLFILLCOUNT, "fillcount"),
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_MSG_POOLGETS, "gets"),
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "L");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (pool != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington pool->name, (unsigned long) pool->size, pool->maxalloc,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington pool->allocated, pool->freecount, pool->freemax,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington pool->fillcount, pool->gets,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington (pool->lock == NULL ? "N" : "Y"));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington pool = ISC_LIST_NEXT(pool, link);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#if ISC_MEM_TRACKLINES
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (isc_mem_debugging > 1) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debuglink_t *dl;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington unsigned int i;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_MSG_DUMPALLOC,
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews "DUMP OF ALL OUTSTANDING "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "MEMORY ALLOCATIONS\n"));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dl = ISC_LIST_HEAD(ctx->debuglist);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (dl == NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_MSG_NONE,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "\tNone.\n"));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (dl != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington for (i = 0 ; i < DEBUGLIST_COUNT ; i++)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (dl->ptr[i] != NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington fprintf(out,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_msgcat_get(isc_msgcat,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_MSGSET_MEM,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_MSG_PTRFILELINE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "\tptr %p "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "file %s "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "line %u\n"),
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->ptr[i], dl->file[i],
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl->line[i]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dl = ISC_LIST_NEXT(dl, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_boolean_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_valid(isc_mem_t *ctx, void *ptr) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned char *cp = ptr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t result = ISC_FALSE;
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ctx->lowest != NULL && cp >= ctx->lowest && cp <= ctx->highest)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (result);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Replacements for malloc() and free() -- they implicitly remember the
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * size of the object allocated (with some additional overhead).
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc__mem_allocateunlocked(isc_mem_t *ctx, size_t size) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_info *si;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size += ALIGNMENT_SIZE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington si = mem_getunlocked(ctx, size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (si == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington si->u.size = size;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (&si[1]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc__mem_allocate(isc_mem_t *ctx, size_t size FLARG) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_info *si;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington si = isc__mem_allocateunlocked(ctx, size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEM_TRACKLINES
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (si != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ADD_TRACE(ctx, si, si[-1].u.size, file, line);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (si);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc__mem_free(isc_mem_t *ctx, void *ptr FLARG) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_info *si;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(ptr != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington si = &(((size_info *)ptr)[-1]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DELETE_TRACE(ctx, ptr, si->u.size, file, line);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mem_putunlocked(ctx, si, si->u.size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Other useful things.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
f8abaa0fae7f75d9601c10b6a4af8dd907494d45Mark Andrews
b55c30f2de6e1baaa3a9ba69b92f428f2c255ac3Mark Andrewschar *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t len;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char *ns;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(mctx));
f8abaa0fae7f75d9601c10b6a4af8dd907494d45Mark Andrews REQUIRE(s != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington len = strlen(s);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ns = isc__mem_allocate(mctx, len + 1 FLARG_PASS);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ns != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strncpy(ns, s, len + 1);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ns);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_setdestroycheck(isc_mem_t *ctx, isc_boolean_t flag) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->checkfree = flag;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_setsplit(isc_mem_t *ctx, isc_boolean_t flag) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->trysplit = flag;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Quotas
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_setquota(isc_mem_t *ctx, size_t quota) {
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews REQUIRE(VALID_CONTEXT(ctx));
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews LOCK(&ctx->lock);
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews ctx->quota = quota;
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews UNLOCK(&ctx->lock);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews}
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrewssize_t
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrewsisc_mem_getquota(isc_mem_t *ctx) {
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews size_t quota;
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington quota = ctx->quota;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (quota);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonsize_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_inuse(isc_mem_t *ctx) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington size_t inuse;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington inuse = ctx->inuse;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (inuse);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews size_t hiwater, size_t lowater)
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews{
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(VALID_CONTEXT(ctx));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (water != NULL) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(hiwater > lowater);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(hiwater > 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(lowater > 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
6fd4ab217ef8e37703b029eac5ab7ae4f7a663c9Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (water == NULL) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ctx->water = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->water_arg = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->hi_water = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->lo_water = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->hi_called = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ctx->water = water;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->water_arg = water_arg;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ctx->hi_water = hiwater;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ctx->lo_water = lowater;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ctx->hi_called = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&ctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Memory pool stuff
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if 0
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Free all but "n" items from the pool's free list. If n == 0, all items
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * will be returned to the mctx.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonmempool_release(isc_mempool_t *mpctx, unsigned int n) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t *mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington element *item;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington element *next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int count;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mctx = mpctx->mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington if (mpctx->freecount <= n)
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(mpctx->items != NULL);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington item = mpctx->items;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington for (count = 0 ; count < n ; count++) {
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington item = item->next;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington INSIST(item != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington /*
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington * All remaining items are to be freed. Lock the context once,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * free them all, and unlock the context.
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&mctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington do {
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington next = item->next;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington mem_putunlocked(mctx, item, mpctx->size);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(mpctx->freecount > 0);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington mpctx->freecount--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington item = next;
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington } while (item != NULL);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington UNLOCK(&mctx->lock);
34f991028382079af7c2b53bac6768803ff28f8cBrian Wellington}
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington#endif
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington/*
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington * Release all items on the free list. No locking is done, the memory
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * context must be locked, and the pool if needed.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonmempool_releaseall(isc_mempool_t *mpctx) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t *mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington element *item;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington element *next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mctx = mpctx->mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington if (mpctx->freecount == 0)
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington return;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington INSIST(mpctx->items != NULL);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington item = mpctx->items;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington do {
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington next = item->next;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington mem_putunlocked(mctx, item, mpctx->size);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington INSIST(mpctx->freecount > 0);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington mpctx->freecount--;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington item = next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } while (item != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_result_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_t *mpctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CONTEXT(mctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(size > 0);
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington REQUIRE(mpctxp != NULL && *mpctxp == NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Allocate space for this pool, initialize values, and if all works
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * well, attach to the memory context.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&mctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington mpctx = mem_getunlocked(mctx, sizeof(isc_mempool_t));
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington if (mpctx == NULL) {
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington UNLOCK(&mctx->lock);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington return (ISC_R_NOMEMORY);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington }
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington mpctx->magic = MEMPOOL_MAGIC;
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington mpctx->lock = NULL;
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington mpctx->mctx = mctx;
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington mpctx->size = size;
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington mpctx->maxalloc = UINT_MAX;
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington mpctx->allocated = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mpctx->freecount = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mpctx->freemax = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mpctx->fillcount = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mpctx->gets = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if ISC_MEMPOOL_NAMES
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington mpctx->name[0] = 0;
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews#endif
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews mpctx->items = NULL;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 *mpctxp = mpctx;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_APPENDUNSAFE(mctx->pools, mpctx, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&mctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_R_SUCCESS);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellingtonisc_mempool_setname(isc_mempool_t *mpctx, const char *name) {
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington REQUIRE(name != NULL);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington#if ISC_MEMPOOL_NAMES
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington if (mpctx->lock != NULL)
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington LOCK(mpctx->lock);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington memset(mpctx->name, 0, sizeof(mpctx->name));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (mpctx->lock != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(mpctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNUSED(mpctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNUSED(name);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#endif
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonvoid
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_mempool_destroy(isc_mempool_t **mpctxp) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mempool_t *mpctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mem_t *mctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mutex_t *lock;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(mpctxp != NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mpctx = *mpctxp;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(VALID_MEMPOOL(mpctx));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(mpctx->allocated == 0);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mctx = mpctx->mctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington lock = mpctx->lock;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (lock != NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington LOCK(lock);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington LOCK(&mctx->lock);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Return any items on the free list
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mempool_releaseall(mpctx);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Remove our linked list entry from the memory context.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LIST_UNLINK(mctx->pools, mpctx, link);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mpctx->magic = 0;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mem_putunlocked(mpctx->mctx, mpctx, sizeof(isc_mempool_t));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNLOCK(&mctx->lock);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (lock != NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNLOCK(lock);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington *mpctxp = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonvoid
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(VALID_MEMPOOL(mpctx));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(mpctx->lock == NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(lock != NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mpctx->lock = lock;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonvoid *
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc__mempool_get(isc_mempool_t *mpctx FLARG) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington element *item;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mem_t *mctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int i;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(VALID_MEMPOOL(mpctx));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mctx = mpctx->mctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (mpctx->lock != NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington LOCK(mpctx->lock);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Don't let the caller go over quota
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (mpctx->allocated >= mpctx->maxalloc) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington item = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington goto out;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * if we have a free list item, return the first here
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington item = mpctx->items;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (item != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mpctx->items = item->next;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(mpctx->freecount > 0);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mpctx->freecount--;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mpctx->gets++;
b7cd261f2fca2c7138cdc6ae8ee434e9c0031303Brian Wellington mpctx->allocated++;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington goto out;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * We need to dip into the well. Lock the memory context here and
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * fill up our free list.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington LOCK(&mctx->lock);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington for (i = 0 ; i < mpctx->fillcount ; i++) {
5d85bf183f957f9ea0902be1a6e3405b90bc7c0fBrian Wellington item = mem_getunlocked(mctx, mpctx->size);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (item == NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington break;
b7cd261f2fca2c7138cdc6ae8ee434e9c0031303Brian Wellington item->next = mpctx->items;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mpctx->items = item;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mpctx->freecount++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&mctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * If we didn't get any items, return NULL.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington item = mpctx->items;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (item == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto out;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mpctx->items = item->next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mpctx->freecount--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mpctx->gets++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mpctx->allocated++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington out:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (mpctx->lock != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(mpctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (item != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&mctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ADD_TRACE(mctx, item, mpctx->size, file, line);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&mctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (item);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc__mempool_put(isc_mempool_t *mpctx, void *mem FLARG) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t *mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington element *item;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_MEMPOOL(mpctx));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(mem != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mctx = mpctx->mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (mpctx->lock != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(mpctx->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(mpctx->allocated > 0);
mpctx->allocated--;
DELETE_TRACE(mctx, mem, mpctx->size, file, line);
/*
* If our free list is full, return this to the mctx directly.
*/
if (mpctx->freecount >= mpctx->freemax) {
LOCK(&mctx->lock);
mem_putunlocked(mctx, mem, mpctx->size);
UNLOCK(&mctx->lock);
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return;
}
/*
* Otherwise, attach it to our free list and bump the counter.
*/
mpctx->freecount++;
item = (element *)mem;
item->next = mpctx->items;
mpctx->items = item;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
}
/*
* Quotas
*/
void
isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit) {
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
mpctx->freemax = limit;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
}
unsigned int
isc_mempool_getfreemax(isc_mempool_t *mpctx) {
unsigned int freemax;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
freemax = mpctx->freemax;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (freemax);
}
unsigned int
isc_mempool_getfreecount(isc_mempool_t *mpctx) {
unsigned int freecount;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
freecount = mpctx->freecount;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (freecount);
}
void
isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit) {
REQUIRE(limit > 0);
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
mpctx->maxalloc = limit;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
}
unsigned int
isc_mempool_getmaxalloc(isc_mempool_t *mpctx) {
unsigned int maxalloc;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
maxalloc = mpctx->maxalloc;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (maxalloc);
}
unsigned int
isc_mempool_getallocated(isc_mempool_t *mpctx) {
unsigned int allocated;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
allocated = mpctx->allocated;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (allocated);
}
void
isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit) {
REQUIRE(limit > 0);
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
mpctx->fillcount = limit;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
}
unsigned int
isc_mempool_getfillcount(isc_mempool_t *mpctx) {
unsigned int fillcount;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
fillcount = mpctx->fillcount;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (fillcount);
}