mem.c revision 6fda1577669dca9e0d8e4832e407bac34cc12de6
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Copyright (C) 1997-2000 Internet Software Consortium.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/* $Id: mem.c,v 1.73 2000/12/07 20:15:57 marka Exp $ */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <config.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <stdio.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <stdlib.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <stddef.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <limits.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <isc/mem.h>
999ae80184e3df1016ac74514124b0459ace4d01Andreas Gustafsson#include <isc/msgs.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <isc/ondestroy.h>
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley#include <isc/string.h>
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley#include <isc/mutex.h>
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley#include <isc/util.h>
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonunsigned int isc_mem_debugging = 0;
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley/*
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley * Constants.
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define DEF_MAX_SIZE 1100
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define DEF_MEM_TARGET 4096
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define ALIGNMENT_SIZE 8
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define NUM_BASIC_BLOCKS 64 /* must be > 1 */
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley#define TABLE_INCREMENT 1024
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley#define DEBUGLIST_COUNT 1024
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley/*
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley * Types.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#if ISC_MEM_TRACKLINES
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssontypedef struct debuglink debuglink_t;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstruct debuglink {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson ISC_LINK(debuglink_t) link;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson const void *ptr[DEBUGLIST_COUNT];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson const char *file[DEBUGLIST_COUNT];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int line[DEBUGLIST_COUNT];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int count;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson};
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define FLARG_PASS , file, line
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define FLARG , const char *file, int line
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#else
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define FLARG_PASS
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define FLARG
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson#endif
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssontypedef struct element element;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonstruct element {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson element * next;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson};
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssontypedef struct {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson /*
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson * This structure must be ALIGNMENT_SIZE bytes.
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson */
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson union {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson size_t size;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson char bytes[ALIGNMENT_SIZE];
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson } u;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson} size_info;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonstruct stats {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned long gets;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned long totalgets;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned long blocks;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned long freefrags;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson};
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson#define MEM_MAGIC 0x4D656d43U /* MemC. */
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson#define VALID_CONTEXT(c) ((c) != NULL && (c)->magic == MEM_MAGIC)
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafssonstruct isc_mem {
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson unsigned int magic;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson isc_ondestroy_t ondestroy;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson isc_mutex_t lock;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_memalloc_t memalloc;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_memfree_t memfree;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson void * arg;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson size_t max_size;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson size_t mem_target;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson element ** freelists;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson element * basic_blocks;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned char ** basic_table;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned int basic_table_count;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned int basic_table_size;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned char * lowest;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned char * highest;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_boolean_t checkfree;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_boolean_t trysplit;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson struct stats * stats;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned int references;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson size_t quota;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson size_t total;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson size_t inuse;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson size_t hi_water;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson size_t lo_water;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_boolean_t hi_called;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_mem_water_t water;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson void * water_arg;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson ISC_LIST(isc_mempool_t) pools;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson#if ISC_MEM_TRACKLINES
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson ISC_LIST(debuglink_t) debuglist;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson#endif
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson};
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson#define MEMPOOL_MAGIC 0x4D454d70U /* MEMp. */
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson#define VALID_MEMPOOL(c) ((c) != NULL && (c)->magic == MEMPOOL_MAGIC)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonstruct isc_mempool {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson /* always unlocked */
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson unsigned int magic; /* magic number */
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_mutex_t *lock; /* optional lock */
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_mem_t *mctx; /* our memory context */
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson /* locked via the memory context's lock */
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson ISC_LINK(isc_mempool_t) link; /* next pool in this mem context */
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson /* optionally locked from here down */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson element *items; /* low water item list */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size_t size; /* size of each item on this pool */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int maxalloc; /* max number of items allowed */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int allocated; /* # of items currently given out */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson unsigned int freecount; /* # of items on reserved list */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson unsigned int freemax; /* # of items allowed on free list */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson unsigned int fillcount; /* # of items to fetch on each fill */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson /* Stats only. */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson unsigned int gets; /* # of requests to this pool */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson /* Debugging only. */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson#if ISC_MEMPOOL_NAMES
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson char name[16]; /* printed name in stats reports */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson#endif
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson};
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Private Inline-able.
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson */
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson#if ! ISC_MEM_TRACKLINES
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson#define ADD_TRACE(a, b, c, d, e)
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson#define DELETE_TRACE(a, b, c, d, e)
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson#else
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson#define ADD_TRACE(a, b, c, d, e) add_trace_entry(a, b, c, d, e)
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson#define DELETE_TRACE(a, b, c, d, e) delete_trace_entry(a, b, c, d, e)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define MEM_TRACE ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#define MEM_RECORD ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * mctx must be locked.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline void
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonadd_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson FLARG)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson{
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson debuglink_t *dl;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int i;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (MEM_TRACE)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_ADDTRACE,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "add %p size %u "
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "file %s line %u mctx %p\n"),
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ptr, size, file, line, mctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (!MEM_RECORD)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl = ISC_LIST_HEAD(mctx->debuglist);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson while (dl != NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (dl->count == DEBUGLIST_COUNT)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson goto next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0 ; i < DEBUGLIST_COUNT ; i++) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (dl->ptr[i] == NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->ptr[i] = ptr;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->file[i] = file;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->line[i] = line;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->count++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson next:
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl = ISC_LIST_NEXT(dl, link);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl = malloc(sizeof(debuglink_t));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(dl != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_LINK_INIT(dl, link);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 1 ; i < DEBUGLIST_COUNT ; i++) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->ptr[i] = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->file[i] = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->line[i] = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->ptr[0] = ptr;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->file[0] = file;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->line[0] = line;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->count = 1;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_LIST_PREPEND(mctx->debuglist, dl, link);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline void
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssondelete_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson const char *file, unsigned int line)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson{
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson debuglink_t *dl;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int i;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (MEM_TRACE)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_DELTRACE,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "del %p size %u "
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "file %s line %u mctx %p\n"),
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ptr, size, file, line, mctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (!MEM_RECORD)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl = ISC_LIST_HEAD(mctx->debuglist);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson while (dl != NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0 ; i < DEBUGLIST_COUNT ; i++) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (dl->ptr[i] == ptr) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->ptr[i] = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->file[i] = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->line[i] = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(dl->count > 0);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl->count--;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (dl->count == 0) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_LIST_UNLINK(mctx->debuglist,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl, link);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson free(dl);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl = ISC_LIST_NEXT(dl, link);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * If we get here, we didn't find the item on the list. We're
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * screwed.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(dl != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif /* ISC_MEM_TRACKLINES */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline size_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonrmsize(size_t size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * round down to ALIGNMENT_SIZE
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size -= (size % ALIGNMENT_SIZE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline size_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonquantize(size_t size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson int temp;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Round up the result in order to get a size big
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * byte boundaries.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (size == 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ALIGNMENT_SIZE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson temp = size + (ALIGNMENT_SIZE - 1);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (temp - temp % ALIGNMENT_SIZE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline void
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonsplit(isc_mem_t *ctx, size_t size, size_t new_size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned char *ptr;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size_t remaining_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Unlink a frag of size 'size'.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ptr = (unsigned char *)ctx->freelists[size];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->freelists[size] = ctx->freelists[size]->next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[size].freefrags--;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Create a frag of size 'new_size' and link it in.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ((element *)ptr)->next = ctx->freelists[new_size];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->freelists[new_size] = (element *)ptr;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[new_size].freefrags++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Create a frag of size 'size - new_size' and link it in.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson remaining_size = size - new_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ptr += new_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ((element *)ptr)->next = ctx->freelists[remaining_size];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->freelists[remaining_size] = (element *)ptr;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[remaining_size].freefrags++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline isc_boolean_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssontry_split(isc_mem_t *ctx, size_t new_size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size_t i, doubled_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (!ctx->trysplit)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_FALSE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Try splitting a frag that's at least twice as big as the size
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * we want.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson doubled_size = new_size * 2;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = doubled_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson i < ctx->max_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson i += ALIGNMENT_SIZE) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->freelists[i] != NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson split(ctx, i, new_size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_TRUE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * No luck. Try splitting any frag bigger than the size we need.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = new_size + ALIGNMENT_SIZE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson i < doubled_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson i += ALIGNMENT_SIZE) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->freelists[i] != NULL) {
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley split(ctx, i, new_size);
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley return (ISC_TRUE);
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_FALSE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline isc_boolean_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonmore_basic_blocks(isc_mem_t *ctx) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson void *new;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned char *curr, *next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned char *first, *last;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley unsigned char **table;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int table_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size_t increment;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson int i;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /* Require: we hold the context lock. */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Did we hit the quota for this context?
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson increment = NUM_BASIC_BLOCKS * ctx->mem_target;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->quota != 0 && ctx->total + increment > ctx->quota)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_FALSE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(ctx->basic_table_count <= ctx->basic_table_size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->basic_table_count == ctx->basic_table_size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson table_size = ctx->basic_table_size + TABLE_INCREMENT;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson table = (ctx->memalloc)(ctx->arg,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson table_size * sizeof (unsigned char *));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (table == NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_FALSE);
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley if (ctx->basic_table_size != 0) {
9637357a7765cc63ae401e02c727b1202f20bc08Bob Halley memcpy(table, ctx->basic_table,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_table_size *
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson sizeof (unsigned char *));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->memfree)(ctx->arg, ctx->basic_table);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_table = table;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_table_size = table_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson new = (ctx->memalloc)(ctx->arg, NUM_BASIC_BLOCKS * ctx->mem_target);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (new == NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_FALSE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->total += increment;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_table[ctx->basic_table_count] = new;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_table_count++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson curr = new;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson next = curr + ctx->mem_target;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ((element *)curr)->next = (element *)next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson curr = next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson next += ctx->mem_target;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * curr is now pointing at the last block in the
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * array.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ((element *)curr)->next = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson first = new;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (first < ctx->lowest || ctx->lowest == NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->lowest = first;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (last > ctx->highest)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->highest = last;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_blocks = new;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_TRUE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline isc_boolean_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonmore_frags(isc_mem_t *ctx, size_t new_size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson int i, frags;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size_t total_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson void *new;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned char *curr, *next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Try to get more fragments by chopping up a basic block.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->basic_blocks == NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (!more_basic_blocks(ctx)) {
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson /*
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson * We can't get more memory from the OS, or we've
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson * hit the quota for this context.
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson */
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson /*
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson * XXXRTH "At quota" notification here.
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson */
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson /*
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson * Maybe we can split one of our existing
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson * list frags.
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson */
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson return (try_split(ctx, new_size));
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson }
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson }
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson total_size = ctx->mem_target;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson new = ctx->basic_blocks;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson ctx->basic_blocks = ctx->basic_blocks->next;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson frags = total_size / new_size;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson ctx->stats[new_size].blocks++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[new_size].freefrags += frags;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Set up a linked-list of blocks of size
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * "new_size".
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson curr = new;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley next = curr + new_size;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley total_size -= new_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0; i < (frags - 1); i++) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ((element *)curr)->next = (element *)next;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley curr = next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson next += new_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson total_size -= new_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Add the remaining fragment of the basic block to a free list.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson total_size = rmsize(total_size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (total_size > 0) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ((element *)next)->next = ctx->freelists[total_size];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->freelists[total_size] = (element *)next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[total_size].freefrags++;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * curr is now pointing at the last block in the
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * array.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ((element *)curr)->next = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->freelists[new_size] = new;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_TRUE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline void *
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonmem_getunlocked(isc_mem_t *ctx, size_t size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size_t new_size = quantize(size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson void *ret;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (size >= ctx->max_size || new_size >= ctx->max_size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * memget() was called on something beyond our upper limit.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->quota != 0 && ctx->total + size > ctx->quota) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ret = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson goto done;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ret = (ctx->memalloc)(ctx->arg, size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ret != NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->total += size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->inuse += size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[ctx->max_size].gets++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[ctx->max_size].totalgets++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * If we don't set new_size to size, then the
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * ISC_MEM_FILL code might write over bytes we
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * don't own.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson new_size = size;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson goto done;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * If there are no blocks in the free list for this size, get a chunk
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * of memory and then break it up into "new_size"-sized blocks, adding
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * them to the free list.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->freelists[new_size] == NULL && !more_frags(ctx, new_size))
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * The free list uses the "rounded-up" size "new_size".
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ret = ctx->freelists[new_size];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->freelists[new_size] = ctx->freelists[new_size]->next;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * The stats[] uses the _actual_ "size" requested by the
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * caller, with the caveat (in the code above) that "size" >= the
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * max. size (max_size) ends up getting recorded as a call to
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * max_size.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[size].gets++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[size].totalgets++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[new_size].freefrags--;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->inuse += new_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson done:
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#if ISC_MEM_FILL
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ret != NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ret);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley#if ISC_MEM_FILL && ISC_MEM_CHECKOVERRUN
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline void
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssoncheck_overrun(void *mem, size_t size, size_t new_size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned char *cp;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson cp = (unsigned char *)mem;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson cp += size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson while (size < new_size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(*cp == 0xbe);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson cp++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size++;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic inline void
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonmem_putunlocked(isc_mem_t *ctx, void *mem, size_t size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size_t new_size = quantize(size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (size == ctx->max_size || new_size >= ctx->max_size) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * memput() called on something beyond our upper limit.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#if ISC_MEM_FILL
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson memset(mem, 0xde, size); /* Mnemonic for "dead". */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->memfree)(ctx->arg, mem);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(ctx->stats[ctx->max_size].gets != 0);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[ctx->max_size].gets--;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(size <= ctx->total);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->inuse -= size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->total -= size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#if ISC_MEM_FILL
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#if ISC_MEM_CHECKOVERRUN
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson check_overrun(mem, size, new_size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * The free list uses the "rounded-up" size "new_size".
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ((element *)mem)->next = ctx->freelists[new_size];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->freelists[new_size] = (element *)mem;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * The stats[] uses the _actual_ "size" requested by the
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * caller, with the caveat (in the code above) that "size" >= the
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * max. size (max_size) ends up getting recorded as a call to
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * max_size.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(ctx->stats[size].gets != 0);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[size].gets--;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats[new_size].freefrags++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->inuse -= new_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Private.
297377a5029e71566d4158bae67f127a3e4f3fc3Bob Halley */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonstatic void *
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssondefault_memalloc(void *arg, size_t size) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNUSED(arg);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (malloc(size));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic void
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssondefault_memfree(void *arg, void *ptr) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNUSED(arg);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson free(ptr);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Public.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_result_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_mem_createx(size_t init_max_size, size_t target_size,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_t **ctxp)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson{
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_t *ctx;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(ctxp != NULL && *ctxp == NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(memalloc != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(memfree != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx = (memalloc)(arg, sizeof *ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx == NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_NOMEMORY);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (init_max_size == 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->max_size = DEF_MAX_SIZE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson else
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->max_size = init_max_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (target_size == 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->mem_target = DEF_MEM_TARGET;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson else
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->mem_target = target_size;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->memalloc = memalloc;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->memfree = memfree;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->arg = arg;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->freelists = (memalloc)(arg, ctx->max_size * sizeof (element *));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->freelists == NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (memfree)(arg, ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_NOMEMORY);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->checkfree = ISC_TRUE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->trysplit = ISC_FALSE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson memset(ctx->freelists, 0,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->max_size * sizeof (element *));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->stats = (memalloc)(arg,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->max_size+1) * sizeof (struct stats));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->stats == NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (memfree)(arg, ctx->freelists);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (memfree)(arg, ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_NOMEMORY);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof (struct stats));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_blocks = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_table = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_table_count = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->basic_table_size = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->lowest = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->highest = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (isc_mutex_init(&ctx->lock) != ISC_R_SUCCESS) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (memfree)(arg, ctx->stats);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (memfree)(arg, ctx->freelists);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (memfree)(arg, ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "isc_mutex_init() %s",
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_FAILED, "failed"));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_UNEXPECTED);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->references = 1;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->quota = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->total = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->inuse = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->hi_water = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->lo_water = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->hi_called = ISC_FALSE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->water = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->water_arg = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->magic = MEM_MAGIC;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_ondestroy_init(&ctx->ondestroy);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_LIST_INIT(ctx->pools);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#if ISC_MEM_TRACKLINES
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_LIST_INIT(ctx->debuglist);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *ctxp = ctx;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_SUCCESS);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_result_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_mem_create(size_t init_max_size, size_t target_size,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_t **ctxp)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson{
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (isc_mem_createx(init_max_size, target_size,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson default_memalloc, default_memfree, NULL,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctxp));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halleystatic void
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssondestroy(isc_mem_t *ctx) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int i;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_ondestroy_t ondest;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->magic = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(ISC_LIST_EMPTY(ctx->pools));
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley#if ISC_MEM_TRACKLINES
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley if (ctx->checkfree)
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley INSIST(ISC_LIST_EMPTY(ctx->debuglist));
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley else {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson debuglink_t *dl;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (dl = ISC_LIST_HEAD(ctx->debuglist);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl != NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dl = ISC_LIST_HEAD(ctx->debuglist)) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_LIST_UNLINK(ctx->debuglist, dl, link);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson free(dl);
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley }
9637357a7765cc63ae401e02c727b1202f20bc08Bob Halley }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(ctx->references == 0);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->checkfree) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0; i <= ctx->max_size; i++)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(ctx->stats[i].gets == 0);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#if 0 /* XXX brister debugging */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0; i < ctx->basic_table_count; i++)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson memset(ctx->basic_table[i], 0x0,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson NUM_BASIC_BLOCKS * ctx->mem_target);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#endif
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0; i < ctx->basic_table_count; i++)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->memfree)(ctx->arg, ctx->freelists);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->memfree)(ctx->arg, ctx->stats);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->memfree)(ctx->arg, ctx->basic_table);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ondest = ctx->ondestroy;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson DESTROYLOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->memfree)(ctx->arg, ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_ondestroy_notify(&ondest, ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonvoid
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(VALID_CONTEXT(source));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(targetp != NULL && *targetp == NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&source->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson source->references++;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNLOCK(&source->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *targetp = source;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonvoid
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_mem_detach(isc_mem_t **ctxp) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_t *ctx;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_boolean_t want_destroy = ISC_FALSE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(ctxp != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx = *ctxp;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(ctx->references > 0);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->references--;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->references == 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson want_destroy = ISC_TRUE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNLOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (want_destroy)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson destroy(ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *ctxp = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * isc_mem_putanddetach() is the equivalent of:
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * mctx = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * isc_mem_attach(ptr->mctx, &mctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * isc_mem_detach(&ptr->mctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * isc_mem_put(mctx, ptr, sizeof(*ptr);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * isc_mem_detach(&mctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonvoid
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc__mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_t *ctx;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_boolean_t want_destroy = ISC_FALSE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(ctxp != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx = *ctxp;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(ptr != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Must be before mem_putunlocked() as ctxp is usually within
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * [ptr..ptr+size).
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *ctxp = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson DELETE_TRACE(ctx, ptr, size, file, line);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson mem_putunlocked(ctx, ptr, size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson INSIST(ctx->references > 0);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->references--;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->references == 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson want_destroy = ISC_TRUE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNLOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (want_destroy)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson destroy(ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonvoid
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_mem_destroy(isc_mem_t **ctxp) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_t *ctx;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * This routine provides legacy support for callers who use mctxs
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * without attaching/detaching.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(ctxp != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx = *ctxp;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(ctx->references == 1);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->references--;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNLOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson destroy(ctx);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *ctxp = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_result_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_mem_ondestroy(isc_mem_t *ctx, isc_task_t *task, isc_event_t **event) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_result_t res;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson res = isc_ondestroy_register(&ctx->ondestroy, task, event);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNLOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (res);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_result_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_mem_restore(isc_mem_t *ctx) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_result_t result;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson result = isc_mutex_init(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (result != ISC_R_SUCCESS)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->magic = 0;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (result);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonvoid *
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc__mem_get(isc_mem_t *ctx, size_t size FLARG) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson void *ptr;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_boolean_t call_water = ISC_FALSE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ptr = mem_getunlocked(ctx, size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ADD_TRACE(ctx, ptr, size, file, line);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->hi_water != 0 && !ctx->hi_called &&
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->inuse > ctx->hi_water) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->hi_called = ISC_TRUE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson call_water = ISC_TRUE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNLOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /* XXX remove */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->hi_water)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(stderr,"inuse %u, total %u\n", ctx->inuse, ctx->total);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (call_water) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /* XXX remove */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(stderr, "%s water(%p, ISC_MEM_HIWATER)\n",
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_CALLING, "calling"),
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->water_arg);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ptr);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonvoid
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc__mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG)
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley{
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_boolean_t call_water = ISC_FALSE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(ptr != NULL);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson DELETE_TRACE(ctx, ptr, size, file, line);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson mem_putunlocked(ctx, ptr, size);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->hi_called && ctx->inuse < ctx->lo_water) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->hi_called = ISC_FALSE;
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley call_water = ISC_TRUE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNLOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /* XXX remove */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->hi_water)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(stderr,"inuse %u, total %u\n", ctx->inuse, ctx->total);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (call_water) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /* XXX remove */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(stderr, "%s water(%p,ISC_MEM_LOWATER)\n",
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_CALLING, "calling"),
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ctx->water_arg);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_result_t
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halleyisc_mem_preallocate(isc_mem_t *ctx) {
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley size_t i;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_result_t result = ISC_R_SUCCESS;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson void *ptr;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0; i < ctx->max_size; i += ALIGNMENT_SIZE) {
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley ptr = mem_getunlocked(ctx, i);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ptr == NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson result = ISC_R_NOMEMORY;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson break;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson mem_putunlocked(ctx, ptr, i);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNLOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (result);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Print the stats[] on the stream "out" with suitable formatting.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonvoid
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_mem_stats(isc_mem_t *ctx, FILE *out) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson size_t i;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson const struct stats *s;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson const isc_mempool_t *pool;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LOCK(&ctx->lock);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ctx->freelists != NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0; i <= ctx->max_size; i++) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson s = &ctx->stats[i];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (s->totalgets == 0 && s->gets == 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson continue;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (i == ctx->max_size) ? ">=" : " ",
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (unsigned long) i, s->totalgets, s->gets);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (s->blocks != 0 || s->freefrags != 0)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(out, " (%lu bl, %lu ff)",
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson s->blocks, s->freefrags);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fputc('\n', out);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson /*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Note that since a pool can be locked now, these stats might be
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * somewhat off if the pool is in active use at the time the stats
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * are dumped. The link fields are protected by the isc_mem_t's
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * lock, however, so walking this list and extracting integers from
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * stats fields is always safe.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson pool = ISC_LIST_HEAD(ctx->pools);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (pool != NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_POOLSTATS,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "[Pool statistics]\n"));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_POOLNAME, "name"),
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson ISC_MSG_POOLSIZE, "size"),
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_POOLMAXALLOC, "maxalloc"),
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson ISC_MSG_POOLALLOCATED, "allocated"),
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson ISC_MSG_POOLFREECOUNT, "freecount"),
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson ISC_MSG_POOLFREEMAX, "freemax"),
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson ISC_MSG_POOLFILLCOUNT, "fillcount"),
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson ISC_MSG_POOLGETS, "gets"),
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson "L");
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson }
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson while (pool != NULL) {
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson pool->name, (unsigned long) pool->size, pool->maxalloc,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson pool->allocated, pool->freecount, pool->freemax,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson pool->fillcount, pool->gets,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson (pool->lock == NULL ? "N" : "Y"));
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson pool = ISC_LIST_NEXT(pool, link);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#if ISC_MEM_TRACKLINES
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (isc_mem_debugging > 1) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson debuglink_t *dl;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int i;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_DUMPALLOC,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "DUMP OF ALL OUTSTANDING "
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "MEMORY ALLOCATIONS\n"));
c6e66777242752532a094561fc728233cab9bc2fAndreas Gustafsson dl = ISC_LIST_HEAD(ctx->debuglist);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (dl == NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ISC_MSG_NONE,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson "\tNone.\n"));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson while (dl != NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i = 0 ; i < DEBUGLIST_COUNT ; i++)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (dl->ptr[i] != NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson fprintf(out,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_msgcat_get(isc_msgcat,
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ISC_MSGSET_MEM,
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ISC_MSG_PTRFILELINE,
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson "\tptr %p "
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson "file %s "
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson "line %u\n"),
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson dl->ptr[i], dl->file[i],
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson dl->line[i]);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson dl = ISC_LIST_NEXT(dl, link);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#endif
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_boolean_t
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mem_valid(isc_mem_t *ctx, void *ptr) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson unsigned char *cp = ptr;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_boolean_t result = ISC_FALSE;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (ctx->lowest != NULL && cp >= ctx->lowest && cp <= ctx->highest)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson result = ISC_TRUE;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (result);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson/*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Replacements for malloc() and free() -- they implicitly remember the
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * size of the object allocated (with some additional overhead).
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonstatic void *
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc__mem_allocateunlocked(isc_mem_t *ctx, size_t size) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson size_info *si;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson size += ALIGNMENT_SIZE;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson si = mem_getunlocked(ctx, size);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (si == NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson si->u.size = size;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (&si[1]);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid *
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc__mem_allocate(isc_mem_t *ctx, size_t size FLARG) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson size_info *si;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson si = isc__mem_allocateunlocked(ctx, size);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#if ISC_MEM_TRACKLINES
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (si != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ADD_TRACE(ctx, si, si[-1].u.size, file, line);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#endif
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (si);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc__mem_free(isc_mem_t *ctx, void *ptr FLARG) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson size_info *si;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(ptr != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson si = &(((size_info *)ptr)[-1]);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson DELETE_TRACE(ctx, ptr, si->u.size, file, line);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mem_putunlocked(ctx, si, si->u.size);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson/*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Other useful things.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonchar *
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson size_t len;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson char *ns;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(mctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(s != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson len = strlen(s);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ns = isc__mem_allocate(mctx, len + 1 FLARG_PASS);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (ns != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson strncpy(ns, s, len + 1);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (ns);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mem_setdestroycheck(isc_mem_t *ctx, isc_boolean_t flag) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->checkfree = flag;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mem_setsplit(isc_mem_t *ctx, isc_boolean_t flag) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->trysplit = flag;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson/*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Quotas
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mem_setquota(isc_mem_t *ctx, size_t quota) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->quota = quota;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonsize_t
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mem_getquota(isc_mem_t *ctx) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson size_t quota;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson quota = ctx->quota;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (quota);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonsize_t
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mem_inuse(isc_mem_t *ctx) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson size_t inuse;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson inuse = ctx->inuse;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (inuse);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson size_t hiwater, size_t lowater)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson{
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (water != NULL) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(hiwater > lowater);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(hiwater > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(lowater > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (water == NULL) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->water = NULL;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->water_arg = NULL;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->hi_water = 0;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->lo_water = 0;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->hi_called = ISC_FALSE;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson } else {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->water = water;
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson ctx->water_arg = water_arg;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->hi_water = hiwater;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->lo_water = lowater;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ctx->hi_called = ISC_FALSE;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&ctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson/*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Memory pool stuff
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#if 0
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson/*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Free all but "n" items from the pool's free list. If n == 0, all items
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * will be returned to the mctx.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonstatic void
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonmempool_release(isc_mempool_t *mpctx, unsigned int n) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_mem_t *mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson element *item;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson element *next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson unsigned int count;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mctx = mpctx->mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->freecount <= n)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson INSIST(mpctx->items != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = mpctx->items;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson for (count = 0 ; count < n ; count++) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = item->next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson INSIST(item != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * All remaining items are to be freed. Lock the context once,
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * free them all, and unlock the context.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson do {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson next = item->next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mem_putunlocked(mctx, item, mpctx->size);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson INSIST(mpctx->freecount > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freecount--;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson } while (item != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#endif
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson/*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Release all items on the free list. No locking is done, the memory
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * context must be locked, and the pool if needed.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonstatic void
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonmempool_releaseall(isc_mempool_t *mpctx) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_mem_t *mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson element *item;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson element *next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mctx = mpctx->mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->freecount == 0)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson INSIST(mpctx->items != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = mpctx->items;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson do {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson next = item->next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mem_putunlocked(mctx, item, mpctx->size);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson INSIST(mpctx->freecount > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freecount--;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson } while (item != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_result_t
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_mempool_t *mpctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_CONTEXT(mctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(size > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(mpctxp != NULL && *mpctxp == NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Allocate space for this pool, initialize values, and if all works
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * well, attach to the memory context.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx = mem_getunlocked(mctx, sizeof(isc_mempool_t));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx == NULL) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (ISC_R_NOMEMORY);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->magic = MEMPOOL_MAGIC;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->lock = NULL;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->mctx = mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->size = size;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->maxalloc = UINT_MAX;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->allocated = 0;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freecount = 0;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freemax = 1;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->fillcount = 1;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->gets = 0;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#if ISC_MEMPOOL_NAMES
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->name[0] = 0;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#endif
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->items = NULL;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson *mpctxp = mpctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (ISC_R_SUCCESS);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_setname(isc_mempool_t *mpctx, const char *name) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(name != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#if ISC_MEMPOOL_NAMES
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson memset(mpctx->name, 0, sizeof(mpctx->name));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#else
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNUSED(mpctx);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNUSED(name);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson#endif
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_destroy(isc_mempool_t **mpctxp) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_mempool_t *mpctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_mem_t *mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_mutex_t *lock;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(mpctxp != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx = *mpctxp;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(mpctx->allocated == 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mctx = mpctx->mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson lock = mpctx->lock;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Return any items on the free list
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mempool_releaseall(mpctx);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Remove our linked list entry from the memory context.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ISC_LIST_UNLINK(mctx->pools, mpctx, link);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson mpctx->magic = 0;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson mem_putunlocked(mpctx->mctx, mpctx, sizeof(isc_mempool_t));
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson UNLOCK(&mctx->lock);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (lock != NULL)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson UNLOCK(lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson *mpctxp = NULL;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(mpctx->lock == NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(lock != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->lock = lock;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid *
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc__mempool_get(isc_mempool_t *mpctx FLARG) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson element *item;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_mem_t *mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson unsigned int i;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mctx = mpctx->mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Don't let the caller go over quota
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->allocated >= mpctx->maxalloc) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = NULL;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson goto out;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * if we have a free list item, return the first here
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = mpctx->items;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (item != NULL) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->items = item->next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson INSIST(mpctx->freecount > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freecount--;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->gets++;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->allocated++;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson goto out;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * We need to dip into the well. Lock the memory context here and
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * fill up our free list.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson for (i = 0 ; i < mpctx->fillcount ; i++) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = mem_getunlocked(mctx, mpctx->size);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (item == NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson break;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item->next = mpctx->items;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->items = item;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freecount++;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * If we didn't get any items, return NULL.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = mpctx->items;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (item == NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson goto out;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->items = item->next;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freecount--;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->gets++;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->allocated++;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson out:
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (item != NULL) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson ADD_TRACE(mctx, item, mpctx->size, file, line);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (item);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc__mempool_put(isc_mempool_t *mpctx, void *mem FLARG) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson isc_mem_t *mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson element *item;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(mem != NULL);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mctx = mpctx->mctx;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson INSIST(mpctx->allocated > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->allocated--;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson DELETE_TRACE(mctx, mem, mpctx->size, file, line);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * If our free list is full, return this to the mctx directly.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->freecount >= mpctx->freemax) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mem_putunlocked(mctx, mem, mpctx->size);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(&mctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson }
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson /*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Otherwise, attach it to our free list and bump the counter.
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freecount++;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item = (element *)mem;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson item->next = mpctx->items;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->items = item;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson/*
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson * Quotas
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson */
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->freemax = limit;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonunsigned int
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_getfreemax(isc_mempool_t *mpctx) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson unsigned int freemax;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson freemax = mpctx->freemax;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (freemax);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonunsigned int
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_getfreecount(isc_mempool_t *mpctx) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson unsigned int freecount;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson freecount = mpctx->freecount;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (freecount);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(limit > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->maxalloc = limit;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonunsigned int
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_getmaxalloc(isc_mempool_t *mpctx) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson unsigned int maxalloc;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson maxalloc = mpctx->maxalloc;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (maxalloc);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonunsigned int
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_getallocated(isc_mempool_t *mpctx) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson unsigned int allocated;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson allocated = mpctx->allocated;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson return (allocated);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonvoid
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(limit > 0);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson mpctx->fillcount = limit;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson}
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonunsigned int
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafssonisc_mempool_getfillcount(isc_mempool_t *mpctx) {
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson unsigned int fillcount;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson LOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson fillcount = mpctx->fillcount;
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if (mpctx->lock != NULL)
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson UNLOCK(mpctx->lock);
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (fillcount);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson