mem.c revision 58f32ac26ea330054f7b85579cd93a6376168fe7
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 1997-2003 Internet Software Consortium.
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley *
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * Permission to use, copy, modify, and distribute this software for any
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PERFORMANCE OF THIS SOFTWARE.
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley */
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson/* $Id: mem.c,v 1.132 2007/02/26 22:57:03 marka Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
e419f613d8591885df608cb73065921be07dd12eBob Halley/*! \file */
e419f613d8591885df608cb73065921be07dd12eBob Halley
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <config.h>
fca5f81ad69098ea8abba130c7f841c951ef91c2Bob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley#include <stdio.h>
e419f613d8591885df608cb73065921be07dd12eBob Halley#include <stdlib.h>
9695ae1c24b168996e3a267855dc754971ccb32cBob Halley#include <stddef.h>
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
1c776a2909632bc755f3fddd3b53addd792ab4d0Brian Wellington#include <limits.h>
e419f613d8591885df608cb73065921be07dd12eBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#include <isc/magic.h>
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson#include <isc/mem.h>
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington#include <isc/msgs.h>
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington#include <isc/once.h>
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#include <isc/ondestroy.h>
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#include <isc/string.h>
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#include <isc/mutex.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <isc/util.h>
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <isc/xml.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#define MCTXLOCK(m, l) if (((m)->flags & ISC_MEMFLAG_NOLOCK) == 0) LOCK(l)
e419f613d8591885df608cb73065921be07dd12eBob Halley#define MCTXUNLOCK(m, l) if (((m)->flags & ISC_MEMFLAG_NOLOCK) == 0) UNLOCK(l)
e419f613d8591885df608cb73065921be07dd12eBob Halley
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence#ifndef ISC_MEM_DEBUGGING
e419f613d8591885df608cb73065921be07dd12eBob Halley#define ISC_MEM_DEBUGGING 0
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley#endif
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob HalleyLIBISC_EXTERNAL_DATA unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington/*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Constants.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DEF_MAX_SIZE 1100
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DEF_MEM_TARGET 4096
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define ALIGNMENT_SIZE 8 /*%< must be a power of 2 */
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define NUM_BASIC_BLOCKS 64 /*%< must be > 1 */
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define TABLE_INCREMENT 1024
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DEBUGLIST_COUNT 1024
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence/*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * Types.
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence */
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#if ISC_MEM_TRACKLINES
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrencetypedef struct debuglink debuglink_t;
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrencestruct debuglink {
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence ISC_LINK(debuglink_t) link;
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence const void *ptr[DEBUGLIST_COUNT];
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence unsigned int size[DEBUGLIST_COUNT];
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence const char *file[DEBUGLIST_COUNT];
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence unsigned int line[DEBUGLIST_COUNT];
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence unsigned int count;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson};
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson#define FLARG_PASS , file, line
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#define FLARG , const char *file, int line
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#else
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#define FLARG_PASS
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#define FLARG
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington#endif
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleytypedef struct element element;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleystruct element {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley element * next;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley};
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtontypedef struct {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff /*!
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff * This structure must be ALIGNMENT_SIZE bytes.
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff */
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff union {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff size_t size;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_mem_t *ctx;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson char bytes[ALIGNMENT_SIZE];
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley } u;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley} size_info;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtonstruct stats {
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington unsigned long gets;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington unsigned long totalgets;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington unsigned long blocks;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington unsigned long freefrags;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington};
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define MEM_MAGIC ISC_MAGIC('M', 'e', 'm', 'C')
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define VALID_CONTEXT(c) ISC_MAGIC_VALID(c, MEM_MAGIC)
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#if ISC_MEM_TRACKLINES
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtontypedef ISC_LIST(debuglink_t) debuglist_t;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#endif
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington/* List of all active memory contexts. */
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtonstatic ISC_LIST(isc_mem_t) contexts;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtonstatic isc_once_t once = ISC_ONCE_INIT;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtonstatic isc_mutex_t lock;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtonstruct isc_mem {
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington unsigned int magic;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_ondestroy_t ondestroy;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington unsigned int flags;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_mutex_t lock;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_memalloc_t memalloc;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_memfree_t memfree;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington void * arg;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington size_t max_size;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_boolean_t checkfree;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff struct stats * stats;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington unsigned int references;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff size_t quota;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size_t total;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size_t inuse;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size_t maxinuse;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size_t hi_water;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size_t lo_water;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_boolean_t hi_called;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_mem_water_t water;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington void * water_arg;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ISC_LIST(isc_mempool_t) pools;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington /* ISC_MEMFLAG_INTERNAL */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington size_t mem_target;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson element ** freelists;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington element * basic_blocks;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington unsigned char ** basic_table;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington unsigned int basic_table_count;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington unsigned int basic_table_size;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson unsigned char * lowest;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned char * highest;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson#if ISC_MEM_TRACKLINES
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington debuglist_t * debuglist;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#endif
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int memalloc_failures;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_LINK(isc_mem_t) link;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington};
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define MEMPOOL_MAGIC ISC_MAGIC('M', 'E', 'M', 'p')
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#define VALID_MEMPOOL(c) ISC_MAGIC_VALID(c, MEMPOOL_MAGIC)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellingtonstruct isc_mempool {
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington /* always unlocked */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int magic; /*%< magic number */
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington isc_mutex_t *lock; /*%< optional lock */
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington isc_mem_t *mctx; /*%< our memory context */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson /*%< locked via the memory context's lock */
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson ISC_LINK(isc_mempool_t) link; /*%< next pool in this mem context */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*%< optionally locked from here down */
01446841be2b73f9a2ead74056df2d5342414041Andreas Gustafsson element *items; /*%< low water item list */
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington size_t size; /*%< size of each item on this pool */
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington unsigned int maxalloc; /*%< max number of items allowed */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int allocated; /*%< # of items currently given out */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int freecount; /*%< # of items on reserved list */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence unsigned int freemax; /*%< # of items allowed on free list */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence unsigned int fillcount; /*%< # of items to fetch on each fill */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*%< Stats only. */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int gets; /*%< # of requests to this pool */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*%< Debugging only. */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#if ISC_MEMPOOL_NAMES
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington char name[16]; /*%< printed name in stats reports */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#endif
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington};
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington/*
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * Private Inline-able.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#if ! ISC_MEM_TRACKLINES
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#define ADD_TRACE(a, b, c, d, e)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define DELETE_TRACE(a, b, c, d, e)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#else
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#define ADD_TRACE(a, b, c, d, e) \
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington do { \
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if ((isc_mem_debugging & (ISC_MEM_DEBUGTRACE | \
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ISC_MEM_DEBUGRECORD)) != 0 && \
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington b != NULL) \
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington add_trace_entry(a, b, c, d, e); \
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington } while (0)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#define DELETE_TRACE(a, b, c, d, e) delete_trace_entry(a, b, c, d, e)
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic void
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonprint_active(isc_mem_t *ctx, FILE *out);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington/*!
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * mctx must be locked.
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic inline void
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonadd_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson FLARG)
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington{
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington debuglink_t *dl;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington unsigned int i;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ISC_MSG_ADDTRACE,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington "add %p size %u "
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington "file %s line %u mctx %p\n"),
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ptr, size, file, line, mctx);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (mctx->debuglist == NULL)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence return;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (size > mctx->max_size)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington size = mctx->max_size;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington dl = ISC_LIST_HEAD(mctx->debuglist[size]);
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington while (dl != NULL) {
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington if (dl->count == DEBUGLIST_COUNT)
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington goto next;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington for (i = 0; i < DEBUGLIST_COUNT; i++) {
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington if (dl->ptr[i] == NULL) {
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington dl->ptr[i] = ptr;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington dl->size[i] = size;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington dl->file[i] = file;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington dl->line[i] = line;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dl->count++;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson return;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington }
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington }
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson next:
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington dl = ISC_LIST_NEXT(dl, link);
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington }
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington dl = malloc(sizeof(debuglink_t));
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson INSIST(dl != NULL);
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington ISC_LINK_INIT(dl, link);
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington for (i = 1; i < DEBUGLIST_COUNT; i++) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dl->ptr[i] = NULL;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dl->size[i] = 0;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dl->file[i] = NULL;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dl->line[i] = 0;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dl->ptr[0] = ptr;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dl->size[0] = size;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dl->file[0] = file;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dl->line[0] = line;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dl->count = 1;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ISC_LIST_PREPEND(mctx->debuglist[size], dl, link);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington}
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline void
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtondelete_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington const char *file, unsigned int line)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington{
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington debuglink_t *dl;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington unsigned int i;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_MSG_DELTRACE,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington "del %p size %u "
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "file %s line %u mctx %p\n"),
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ptr, size, file, line, mctx);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if (mctx->debuglist == NULL)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington return;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (size > mctx->max_size)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington size = mctx->max_size;
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson dl = ISC_LIST_HEAD(mctx->debuglist[size]);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington while (dl != NULL) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington for (i = 0; i < DEBUGLIST_COUNT; i++) {
01446841be2b73f9a2ead74056df2d5342414041Andreas Gustafsson if (dl->ptr[i] == ptr) {
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington dl->ptr[i] = NULL;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dl->size[i] = 0;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dl->file[i] = NULL;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dl->line[i] = 0;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence INSIST(dl->count > 0);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dl->count--;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (dl->count == 0) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_LIST_UNLINK(mctx->debuglist[size],
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dl, link);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington free(dl);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington }
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington return;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington }
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington }
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington dl = ISC_LIST_NEXT(dl, link);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington }
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington * If we get here, we didn't find the item on the list. We're
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington * screwed.
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington INSIST(dl != NULL);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington}
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington#endif /* ISC_MEM_TRACKLINES */
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonstatic inline size_t
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonrmsize(size_t size) {
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington /*
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * round down to ALIGNMENT_SIZE
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington return (size & (~(ALIGNMENT_SIZE - 1)));
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington}
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic inline size_t
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonquantize(size_t size) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington /*!
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * Round up the result in order to get a size big
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * byte boundaries.
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (size == 0U)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return (ALIGNMENT_SIZE);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return ((size + ALIGNMENT_SIZE - 1) & (~(ALIGNMENT_SIZE - 1)));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington}
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic inline isc_boolean_t
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellingtonmore_basic_blocks(isc_mem_t *ctx) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington void *new;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned char *curr, *next;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington unsigned char *first, *last;
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington unsigned char **table;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson unsigned int table_size;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson size_t increment;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington int i;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington /* Require: we hold the context lock. */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington /*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * Did we hit the quota for this context?
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence increment = NUM_BASIC_BLOCKS * ctx->mem_target;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->quota != 0U && ctx->total + increment > ctx->quota)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence return (ISC_FALSE);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington INSIST(ctx->basic_table_count <= ctx->basic_table_size);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->basic_table_count == ctx->basic_table_size) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington table_size = ctx->basic_table_size + TABLE_INCREMENT;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington table = (ctx->memalloc)(ctx->arg,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington table_size * sizeof(unsigned char *));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (table == NULL) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->memalloc_failures++;
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington return (ISC_FALSE);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->basic_table_size != 0) {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews memcpy(table, ctx->basic_table,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->basic_table_size *
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington sizeof(unsigned char *));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington (ctx->memfree)(ctx->arg, ctx->basic_table);
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington }
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ctx->basic_table = table;
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington ctx->basic_table_size = table_size;
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington }
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington new = (ctx->memalloc)(ctx->arg, NUM_BASIC_BLOCKS * ctx->mem_target);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (new == NULL) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ctx->memalloc_failures++;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return (ISC_FALSE);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->total += increment;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->basic_table[ctx->basic_table_count] = new;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->basic_table_count++;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington curr = new;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson next = curr + ctx->mem_target;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ((element *)curr)->next = (element *)next;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington curr = next;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington next += ctx->mem_target;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson /*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * curr is now pointing at the last block in the
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * array.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ((element *)curr)->next = NULL;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington first = new;
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington if (first < ctx->lowest || ctx->lowest == NULL)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->lowest = first;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (last > ctx->highest)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->highest = last;
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington ctx->basic_blocks = new;
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington return (ISC_TRUE);
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews}
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline isc_boolean_t
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonmore_frags(isc_mem_t *ctx, size_t new_size) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington int i, frags;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size_t total_size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington void *new;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned char *curr, *next;
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington /*!
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington * Try to get more fragments by chopping up a basic block.
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington if (ctx->basic_blocks == NULL) {
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington if (!more_basic_blocks(ctx)) {
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington /*
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington * We can't get more memory from the OS, or we've
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington * hit the quota for this context.
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson /*
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington * XXXRTH "At quota" notification here.
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington */
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington return (ISC_FALSE);
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington }
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington }
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington total_size = ctx->mem_target;
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington new = ctx->basic_blocks;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->basic_blocks = ctx->basic_blocks->next;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington frags = total_size / new_size;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ctx->stats[new_size].blocks++;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->stats[new_size].freefrags += frags;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington * Set up a linked-list of blocks of size
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * "new_size".
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington curr = new;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson next = curr + new_size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington total_size -= new_size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington for (i = 0; i < (frags - 1); i++) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ((element *)curr)->next = (element *)next;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington curr = next;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington next += new_size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington total_size -= new_size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington * Add the remaining fragment of the basic block to a free list.
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington total_size = rmsize(total_size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (total_size > 0U) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington ((element *)next)->next = ctx->freelists[total_size];
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington ctx->freelists[total_size] = (element *)next;
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington ctx->stats[total_size].freefrags++;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * curr is now pointing at the last block in the
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * array.
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ((element *)curr)->next = NULL;
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington ctx->freelists[new_size] = new;
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington return (ISC_TRUE);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington}
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonstatic inline void *
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonmem_getunlocked(isc_mem_t *ctx, size_t size) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington size_t new_size = quantize(size);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington void *ret;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (size >= ctx->max_size || new_size >= ctx->max_size) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington /*
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * memget() was called on something beyond our upper limit.
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (ctx->quota != 0U && ctx->total + size > ctx->quota) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ret = NULL;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington goto done;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ret = (ctx->memalloc)(ctx->arg, size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (ret == NULL) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->memalloc_failures++;
f15af68028adc665d3bdddf955fc52bad83f0514Brian Wellington goto done;
f15af68028adc665d3bdddf955fc52bad83f0514Brian Wellington }
f15af68028adc665d3bdddf955fc52bad83f0514Brian Wellington ctx->total += size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->inuse += size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->stats[ctx->max_size].gets++;
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington ctx->stats[ctx->max_size].totalgets++;
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington /*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * If we don't set new_size to size, then the
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * ISC_MEM_FILL code might write over bytes we
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * don't own.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington new_size = size;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington goto done;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington /*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * If there are no blocks in the free list for this size, get a chunk
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * of memory and then break it up into "new_size"-sized blocks, adding
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * them to the free list.
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington */
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (ctx->freelists[new_size] == NULL && !more_frags(ctx, new_size))
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington return (NULL);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington /*
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * The free list uses the "rounded-up" size "new_size".
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington ret = ctx->freelists[new_size];
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington ctx->freelists[new_size] = ctx->freelists[new_size]->next;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington /*
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * The stats[] uses the _actual_ "size" requested by the
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * caller, with the caveat (in the code above) that "size" >= the
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * max. size (max_size) ends up getting recorded as a call to
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * max_size.
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington */
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington ctx->stats[size].gets++;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ctx->stats[size].totalgets++;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ctx->stats[new_size].freefrags--;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ctx->inuse += new_size;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington done:
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington#if ISC_MEM_FILL
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (ret != NULL)
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#endif
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return (ret);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson}
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#if ISC_MEM_FILL && ISC_MEM_CHECKOVERRUN
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtoncheck_overrun(void *mem, size_t size, size_t new_size) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson unsigned char *cp;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington cp = (unsigned char *)mem;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington cp += size;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington while (size < new_size) {
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington INSIST(*cp == 0xbe);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington cp++;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington size++;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington}
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#endif
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic inline void
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonmem_putunlocked(isc_mem_t *ctx, void *mem, size_t size) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington size_t new_size = quantize(size);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (size == ctx->max_size || new_size >= ctx->max_size) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington /*
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * memput() called on something beyond our upper limit.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington#if ISC_MEM_FILL
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington memset(mem, 0xde, size); /* Mnemonic for "dead". */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington#endif
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington (ctx->memfree)(ctx->arg, mem);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington INSIST(ctx->stats[ctx->max_size].gets != 0U);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ctx->stats[ctx->max_size].gets--;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington INSIST(size <= ctx->total);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->inuse -= size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->total -= size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return;
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington#if ISC_MEM_FILL
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington#if ISC_MEM_CHECKOVERRUN
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington check_overrun(mem, size, new_size);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington#endif
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington#endif
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington /*
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * The free list uses the "rounded-up" size "new_size".
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
f439363eeb4052fddc0e3ec648658548daa10506Brian Wellington ((element *)mem)->next = ctx->freelists[new_size];
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->freelists[new_size] = (element *)mem;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington /*
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * The stats[] uses the _actual_ "size" requested by the
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington * caller, with the caveat (in the code above) that "size" >= the
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * max. size (max_size) ends up getting recorded as a call to
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * max_size.
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington INSIST(ctx->stats[size].gets != 0U);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ctx->stats[size].gets--;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ctx->stats[new_size].freefrags++;
f439363eeb4052fddc0e3ec648658548daa10506Brian Wellington ctx->inuse -= new_size;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington/*!
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Perform a malloc, doing memory filling and overrun detection as necessary.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void *
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonmem_get(isc_mem_t *ctx, size_t size) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington char *ret;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington#if ISC_MEM_CHECKOVERRUN
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington size += 1;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#endif
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ret = (ctx->memalloc)(ctx->arg, size);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (ret == NULL)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ctx->memalloc_failures++;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#if ISC_MEM_FILL
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (ret != NULL)
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews memset(ret, 0xbe, size); /* Mnemonic for "beef". */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#else
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington# if ISC_MEM_CHECKOVERRUN
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (ret != NULL)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ret[size-1] = 0xbe;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington# endif
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#endif
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence return (ret);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington}
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington/*!
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * Perform a free, doing memory filling and overrun detection as necessary.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline void
5c29047792191d6141f69b2684314d0b762fedebBrian Wellingtonmem_put(isc_mem_t *ctx, void *mem, size_t size) {
5c29047792191d6141f69b2684314d0b762fedebBrian Wellington#if ISC_MEM_CHECKOVERRUN
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington INSIST(((unsigned char *)mem)[size] == 0xbe);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#endif
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#if ISC_MEM_FILL
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington memset(mem, 0xde, size); /* Mnemonic for "dead". */
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington#else
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews UNUSED(size);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#endif
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington (ctx->memfree)(ctx->arg, mem);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington}
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington/*!
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson * Update internal counters after a memory get.
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson */
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafssonstatic inline void
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafssonmem_getstats(isc_mem_t *ctx, size_t size) {
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson ctx->total += size;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington ctx->inuse += size;
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (size > ctx->max_size) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->stats[ctx->max_size].gets++;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->stats[ctx->max_size].totalgets++;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley } else {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->stats[size].gets++;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->stats[size].totalgets++;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews }
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington}
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington/*!
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington * Update internal counters after a memory put.
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington */
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonstatic inline void
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonmem_putstats(isc_mem_t *ctx, void *ptr, size_t size) {
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington UNUSED(ptr);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(ctx->inuse >= size);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->inuse -= size;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington if (size > ctx->max_size) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(ctx->stats[ctx->max_size].gets > 0U);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->stats[ctx->max_size].gets--;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley } else {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence INSIST(ctx->stats[size].gets > 0U);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->stats[size].gets--;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
5c29047792191d6141f69b2684314d0b762fedebBrian Wellington}
5c29047792191d6141f69b2684314d0b762fedebBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington * Private.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic void *
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleydefault_memalloc(void *arg, size_t size) {
63bf060be4ff2a7ade02fd86abb98694a5afc250Brian Wellington UNUSED(arg);
63bf060be4ff2a7ade02fd86abb98694a5afc250Brian Wellington if (size == 0U)
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington size = 1;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington return (malloc(size));
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington}
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonstatic void
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtondefault_memfree(void *arg, void *ptr) {
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington UNUSED(arg);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington free(ptr);
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington}
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleystatic void
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellingtoninitialize_action(void) {
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Public.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonisc_result_t
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellingtonisc_mem_createx(size_t init_max_size, size_t target_size,
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_mem_t **ctxp)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley{
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (isc_mem_createx2(init_max_size, target_size, memalloc, memfree,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley arg, ctxp, ISC_MEMFLAG_DEFAULT));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyisc_result_t
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyisc_mem_createx2(size_t init_max_size, size_t target_size,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_mem_t **ctxp, unsigned int flags)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley{
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_mem_t *ctx;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_result_t result;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington REQUIRE(ctxp != NULL && *ctxp == NULL);
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington REQUIRE(memalloc != NULL);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley REQUIRE(memfree != NULL);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0);
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx = (memalloc)(arg, sizeof(*ctx));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ctx == NULL)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (ISC_R_NOMEMORY);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if ((flags & ISC_MEMFLAG_NOLOCK) == 0) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result = isc_mutex_init(&ctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (result != ISC_R_SUCCESS) {
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson (memfree)(arg, ctx);
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson return (result);
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson }
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson }
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson if (init_max_size == 0U)
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->max_size = DEF_MAX_SIZE;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington else
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->max_size = init_max_size;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->flags = flags;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->references = 1;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->quota = 0;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->total = 0;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->inuse = 0;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->maxinuse = 0;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->hi_water = 0;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson ctx->lo_water = 0;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->hi_called = ISC_FALSE;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->water = NULL;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->water_arg = NULL;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->magic = MEM_MAGIC;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington isc_ondestroy_init(&ctx->ondestroy);
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->memalloc = memalloc;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->memfree = memfree;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->arg = arg;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->stats = NULL;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ctx->checkfree = ISC_TRUE;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington#if ISC_MEM_TRACKLINES
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ctx->debuglist = NULL;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington#endif
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ISC_LIST_INIT(ctx->pools);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->freelists = NULL;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->basic_blocks = NULL;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->basic_table = NULL;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->basic_table_count = 0;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->basic_table_size = 0;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->lowest = NULL;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->highest = NULL;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->stats = (memalloc)(arg,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley (ctx->max_size+1) * sizeof(struct stats));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->stats == NULL) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result = ISC_R_NOMEMORY;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley goto error;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
77c67dfb2607618f5e7940486daebafd42a502abBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (target_size == 0U)
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington ctx->mem_target = DEF_MEM_TARGET;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley else
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->mem_target = target_size;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->freelists = (memalloc)(arg, ctx->max_size *
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington sizeof(element *));
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington if (ctx->freelists == NULL) {
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington result = ISC_R_NOMEMORY;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington goto error;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington memset(ctx->freelists, 0,
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington ctx->max_size * sizeof(element *));
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington }
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington#if ISC_MEM_TRACKLINES
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington unsigned int i;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington ctx->debuglist = (memalloc)(arg,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington (ctx->max_size+1) * sizeof(debuglist_t));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ctx->debuglist == NULL) {
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington result = ISC_R_NOMEMORY;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington goto error;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington for (i = 0; i <= ctx->max_size; i++)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_LIST_INIT(ctx->debuglist[i]);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington }
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington#endif
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington ctx->memalloc_failures = 0;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington LOCK(&lock);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington ISC_LIST_INITANDAPPEND(contexts, ctx, link);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley UNLOCK(&lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley *ctxp = ctx;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson return (ISC_R_SUCCESS);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington error:
77c67dfb2607618f5e7940486daebafd42a502abBrian Wellington if (ctx != NULL) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ctx->stats != NULL)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley (memfree)(arg, ctx->stats);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ctx->freelists != NULL)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley (memfree)(arg, ctx->freelists);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#if ISC_MEM_TRACKLINES
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ctx->debuglist != NULL)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley (ctx->memfree)(ctx->arg, ctx->debuglist);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#endif /* ISC_MEM_TRACKLINES */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley DESTROYLOCK(&ctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley (memfree)(arg, ctx);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington return (result);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington}
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtonisc_result_t
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtonisc_mem_create(size_t init_max_size, size_t target_size,
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff isc_mem_t **ctxp)
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington{
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington return (isc_mem_createx2(init_max_size, target_size,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington default_memalloc, default_memfree, NULL,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctxp, ISC_MEMFLAG_DEFAULT));
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington}
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtonisc_result_t
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtonisc_mem_create2(size_t init_max_size, size_t target_size,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_mem_t **ctxp, unsigned int flags)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley{
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (isc_mem_createx2(init_max_size, target_size,
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington default_memalloc, default_memfree, NULL,
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington ctxp, flags));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleystatic void
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleydestroy(isc_mem_t *ctx) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley unsigned int i;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_ondestroy_t ondest;
77c67dfb2607618f5e7940486daebafd42a502abBrian Wellington
77c67dfb2607618f5e7940486daebafd42a502abBrian Wellington ctx->magic = 0;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington LOCK(&lock);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_LIST_UNLINK(contexts, ctx, link);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley UNLOCK(&lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(ISC_LIST_EMPTY(ctx->pools));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington#if ISC_MEM_TRACKLINES
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (ctx->debuglist != NULL) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (ctx->checkfree) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington for (i = 0; i <= ctx->max_size; i++) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (!ISC_LIST_EMPTY(ctx->debuglist[i]))
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington print_active(ctx, stderr);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington INSIST(ISC_LIST_EMPTY(ctx->debuglist[i]));
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington } else {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews debuglink_t *dl;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington for (i = 0; i <= ctx->max_size; i++)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington for (dl = ISC_LIST_HEAD(ctx->debuglist[i]);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dl != NULL;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dl = ISC_LIST_HEAD(ctx->debuglist[i])) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington ISC_LIST_UNLINK(ctx->debuglist[i],
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dl, link);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington free(dl);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington (ctx->memfree)(ctx->arg, ctx->debuglist);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington#endif
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington INSIST(ctx->references == 0);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (ctx->checkfree) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington for (i = 0; i <= ctx->max_size; i++) {
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews#if ISC_MEM_TRACKLINES
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (ctx->stats[i].gets != 0U)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington print_active(ctx, stderr);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington#endif
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington INSIST(ctx->stats[i].gets == 0U);
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington }
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington }
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington (ctx->memfree)(ctx->arg, ctx->stats);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington for (i = 0; i < ctx->basic_table_count; i++)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington (ctx->memfree)(ctx->arg, ctx->freelists);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington (ctx->memfree)(ctx->arg, ctx->basic_table);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington ondest = ctx->ondestroy;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington DESTROYLOCK(&ctx->lock);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington (ctx->memfree)(ctx->arg, ctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_ondestroy_notify(&ondest, ctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington}
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonvoid
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonisc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington REQUIRE(VALID_CONTEXT(source));
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington REQUIRE(targetp != NULL && *targetp == NULL);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington MCTXLOCK(source, &source->lock);
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson source->references++;
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson MCTXUNLOCK(source, &source->lock);
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson *targetp = source;
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson}
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafssonvoid
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafssonisc_mem_detach(isc_mem_t **ctxp) {
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson isc_mem_t *ctx;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_boolean_t want_destroy = ISC_FALSE;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley REQUIRE(ctxp != NULL);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx = *ctxp;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews REQUIRE(VALID_CONTEXT(ctx));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley MCTXLOCK(ctx, &ctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(ctx->references > 0);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ctx->references--;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ctx->references == 0)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley want_destroy = ISC_TRUE;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley MCTXUNLOCK(ctx, &ctx->lock);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (want_destroy)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington destroy(ctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington *ctxp = NULL;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington}
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington/*
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_putanddetach() is the equivalent of:
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington *
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * mctx = NULL;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_attach(ptr->mctx, &mctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_detach(&ptr->mctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_put(mctx, ptr, sizeof(*ptr);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_detach(&mctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington */
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonvoid
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonisc__mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_mem_t *ctx;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_boolean_t want_destroy = ISC_FALSE;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington size_info *si;
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson size_t oldsize;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson REQUIRE(ctxp != NULL);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ctx = *ctxp;
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson REQUIRE(ptr != NULL);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Must be before mem_putunlocked() as ctxp is usually within
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson * [ptr..ptr+size).
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson */
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson *ctxp = NULL;
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0) {
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley si = &(((size_info *)ptr)[-1]);
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington oldsize = si->u.size - ALIGNMENT_SIZE;
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson oldsize -= ALIGNMENT_SIZE;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington INSIST(oldsize == size);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson }
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc__mem_free(ctx, ptr FLARG_PASS);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson MCTXLOCK(ctx, &ctx->lock);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson ctx->references--;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson if (ctx->references == 0)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence want_destroy = ISC_TRUE;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley MCTXUNLOCK(ctx, &ctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (want_destroy)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley destroy(ctx);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson MCTXLOCK(ctx, &ctx->lock);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson mem_putunlocked(ctx, ptr, size);
0a3e2e1d590dac7fb011e72bd3a4982c179d8e68Brian Wellington } else {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mem_put(ctx, ptr, size);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley MCTXLOCK(ctx, &ctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mem_putstats(ctx, ptr, size);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington }
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington DELETE_TRACE(ctx, ptr, size, file, line);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson INSIST(ctx->references > 0);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington ctx->references--;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington if (ctx->references == 0)
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington want_destroy = ISC_TRUE;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington MCTXUNLOCK(ctx, &ctx->lock);
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington if (want_destroy)
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington destroy(ctx);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington}
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonvoid
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafssonisc_mem_destroy(isc_mem_t **ctxp) {
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington isc_mem_t *ctx;
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington /*
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington * This routine provides legacy support for callers who use mctxs
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington * without attaching/detaching.
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington */
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington REQUIRE(ctxp != NULL);
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington ctx = *ctxp;
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington MCTXLOCK(ctx, &ctx->lock);
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington#if ISC_MEM_TRACKLINES
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington if (ctx->references != 1)
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington print_active(ctx, stderr);
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington#endif
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington REQUIRE(ctx->references == 1);
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington ctx->references--;
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington MCTXUNLOCK(ctx, &ctx->lock);
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington destroy(ctx);
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington *ctxp = NULL;
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington}
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafssonisc_result_t
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonisc_mem_ondestroy(isc_mem_t *ctx, isc_task_t *task, isc_event_t **event) {
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington isc_result_t res;
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington MCTXLOCK(ctx, &ctx->lock);
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington res = isc_ondestroy_register(&ctx->ondestroy, task, event);
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington MCTXUNLOCK(ctx, &ctx->lock);
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington return (res);
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington}
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellingtonvoid *
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellingtonisc__mem_get(isc_mem_t *ctx, size_t size FLARG) {
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington void *ptr;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington isc_boolean_t call_water = ISC_FALSE;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0)
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington return (isc__mem_allocate(ctx, size FLARG_PASS));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington MCTXLOCK(ctx, &ctx->lock);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ptr = mem_getunlocked(ctx, size);
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington } else {
187604c1adfe841d909d4a8453b6900e652f7f6dBrian Wellington ptr = mem_get(ctx, size);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington MCTXLOCK(ctx, &ctx->lock);
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington if (ptr != NULL)
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington mem_getstats(ctx, size);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson }
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington ADD_TRACE(ctx, ptr, size, file, line);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington if (ctx->hi_water != 0U && !ctx->hi_called &&
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington ctx->inuse > ctx->hi_water) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ctx->hi_called = ISC_TRUE;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington call_water = ISC_TRUE;
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington }
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson if (ctx->inuse > ctx->maxinuse) {
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington ctx->maxinuse = ctx->inuse;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington fprintf(stderr, "maxinuse = %lu\n",
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington (unsigned long)ctx->inuse);
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington }
e1f16346db02486f751c6db683fffe53c866c186Andreas Gustafsson MCTXUNLOCK(ctx, &ctx->lock);
e1f16346db02486f751c6db683fffe53c866c186Andreas Gustafsson
e1f16346db02486f751c6db683fffe53c866c186Andreas Gustafsson if (call_water)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington return (ptr);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington}
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellingtonvoid
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellingtonisc__mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG)
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington{
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington isc_boolean_t call_water = ISC_FALSE;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington size_info *si;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington size_t oldsize;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington REQUIRE(ptr != NULL);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington si = &(((size_info *)ptr)[-1]);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington oldsize = si->u.size - ALIGNMENT_SIZE;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington oldsize -= ALIGNMENT_SIZE;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington INSIST(oldsize == size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington isc__mem_free(ctx, ptr FLARG_PASS);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington return;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington MCTXLOCK(ctx, &ctx->lock);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington mem_putunlocked(ctx, ptr, size);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington } else {
f15af68028adc665d3bdddf955fc52bad83f0514Brian Wellington mem_put(ctx, ptr, size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington MCTXLOCK(ctx, &ctx->lock);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington mem_putstats(ctx, ptr, size);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington DELETE_TRACE(ctx, ptr, size, file, line);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington /*
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington * The check against ctx->lo_water == 0 is for the condition
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * when the context was pushed over hi_water but then had
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * isc_mem_setwater() called with 0 for hi_water and lo_water.
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (ctx->hi_called &&
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington ctx->hi_called = ISC_FALSE;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (ctx->water != NULL)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington call_water = ISC_TRUE;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington MCTXUNLOCK(ctx, &ctx->lock);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (call_water)
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington}
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington#if ISC_MEM_TRACKLINES
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellingtonstatic void
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellingtonprint_active(isc_mem_t *mctx, FILE *out) {
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington if (mctx->debuglist != NULL) {
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington debuglink_t *dl;
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington unsigned int i, j;
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington const char *format;
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington isc_boolean_t found;
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews ISC_MSG_DUMPALLOC,
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington "Dump of all outstanding "
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington "memory allocations:\n"));
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington found = ISC_FALSE;
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington format = isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington ISC_MSG_PTRFILELINE,
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington "\tptr %p size %u file %s line %u\n");
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington for (i = 0; i <= mctx->max_size; i++) {
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington dl = ISC_LIST_HEAD(mctx->debuglist[i]);
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington if (dl != NULL)
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington found = ISC_TRUE;
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington while (dl != NULL) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington for (j = 0; j < DEBUGLIST_COUNT; j++)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (dl->ptr[j] != NULL)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fprintf(out, format,
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dl->ptr[j],
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dl->size[j],
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dl->file[j],
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dl->line[j]);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dl = ISC_LIST_NEXT(dl, link);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (!found)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington ISC_MSG_NONE, "\tNone.\n"));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington}
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington#endif
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington/*
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * Print the stats[] on the stream "out" with suitable formatting.
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington */
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellingtonvoid
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellingtonisc_mem_stats(isc_mem_t *ctx, FILE *out) {
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington size_t i;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington const struct stats *s;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington const isc_mempool_t *pool;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington MCTXLOCK(ctx, &ctx->lock);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington for (i = 0; i <= ctx->max_size; i++) {
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington s = &ctx->stats[i];
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (s->totalgets == 0U && s->gets == 0U)
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington continue;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson (i == ctx->max_size) ? ">=" : " ",
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington (unsigned long) i, s->totalgets, s->gets);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington (s->blocks != 0U || s->freefrags != 0U))
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson fprintf(out, " (%lu bl, %lu ff)",
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington s->blocks, s->freefrags);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington fputc('\n', out);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington }
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington /*
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * Note that since a pool can be locked now, these stats might be
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * somewhat off if the pool is in active use at the time the stats
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * are dumped. The link fields are protected by the isc_mem_t's
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * lock, however, so walking this list and extracting integers from
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * stats fields is always safe.
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington pool = ISC_LIST_HEAD(ctx->pools);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (pool != NULL) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ISC_MSG_POOLSTATS,
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington "[Pool statistics]\n"));
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington ISC_MSG_POOLNAME, "name"),
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington ISC_MSG_POOLSIZE, "size"),
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington ISC_MSG_POOLMAXALLOC, "maxalloc"),
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ISC_MSG_POOLALLOCATED, "allocated"),
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington ISC_MSG_POOLFREECOUNT, "freecount"),
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington ISC_MSG_POOLFREEMAX, "freemax"),
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington ISC_MSG_POOLFILLCOUNT, "fillcount"),
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington ISC_MSG_POOLGETS, "gets"),
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington "L");
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington while (pool != NULL) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington pool->name, (unsigned long) pool->size, pool->maxalloc,
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington pool->allocated, pool->freecount, pool->freemax,
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson pool->fillcount, pool->gets,
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington (pool->lock == NULL ? "N" : "Y"));
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington pool = ISC_LIST_NEXT(pool, link);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington#if ISC_MEM_TRACKLINES
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington print_active(ctx, out);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington#endif
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson MCTXUNLOCK(ctx, &ctx->lock);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington}
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington/*
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * Replacements for malloc() and free() -- they implicitly remember the
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * size of the object allocated (with some additional overhead).
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonstatic void *
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonisc__mem_allocateunlocked(isc_mem_t *ctx, size_t size) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington size_info *si;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington size += ALIGNMENT_SIZE;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson size += ALIGNMENT_SIZE;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0)
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson si = mem_getunlocked(ctx, size);
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson else
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington si = mem_get(ctx, size);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (si == NULL)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return (NULL);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington si->u.ctx = ctx;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington si++;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington si->u.size = size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return (&si[1]);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington}
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonvoid *
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonisc__mem_allocate(isc_mem_t *ctx, size_t size FLARG) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size_info *si;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_boolean_t call_water = ISC_FALSE;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson REQUIRE(VALID_CONTEXT(ctx));
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington MCTXLOCK(ctx, &ctx->lock);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence si = isc__mem_allocateunlocked(ctx, size);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington } else {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson si = isc__mem_allocateunlocked(ctx, size);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington MCTXLOCK(ctx, &ctx->lock);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (si != NULL)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington mem_getstats(ctx, si[-1].u.size);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#if ISC_MEM_TRACKLINES
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson ADD_TRACE(ctx, si, si[-1].u.size, file, line);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#endif
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (ctx->hi_water != 0U && !ctx->hi_called &&
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ctx->inuse > ctx->hi_water) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ctx->hi_called = ISC_TRUE;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington call_water = ISC_TRUE;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington }
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson if (ctx->inuse > ctx->maxinuse) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->maxinuse = ctx->inuse;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fprintf(stderr, "maxinuse = %lu\n",
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington (unsigned long)ctx->inuse);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington }
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington MCTXUNLOCK(ctx, &ctx->lock);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (call_water)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington return (si);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington}
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonvoid
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonisc__mem_free(isc_mem_t *ctx, void *ptr FLARG) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size_info *si;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington size_t size;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington isc_boolean_t call_water= ISC_FALSE;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington REQUIRE(VALID_CONTEXT(ctx));
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence REQUIRE(ptr != NULL);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence si = &(((size_info *)ptr)[-2]);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence REQUIRE(si->u.ctx == ctx);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington size = si[1].u.size;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington } else {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington si = &(((size_info *)ptr)[-1]);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence size = si->u.size;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington MCTXLOCK(ctx, &ctx->lock);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington mem_putunlocked(ctx, si, size);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington } else {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington mem_put(ctx, si, size);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington MCTXLOCK(ctx, &ctx->lock);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington mem_putstats(ctx, si, size);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington DELETE_TRACE(ctx, ptr, size, file, line);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington /*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * The check against ctx->lo_water == 0 is for the condition
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * when the context was pushed over hi_water but then had
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * isc_mem_setwater() called with 0 for hi_water and lo_water.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->hi_called &&
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->hi_called = ISC_FALSE;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (ctx->water != NULL)
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington call_water = ISC_TRUE;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington }
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington MCTXUNLOCK(ctx, &ctx->lock);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (call_water)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington}
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
115635379a2baf2100695018109ad39e0dac349dAndreas Gustafsson
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington/*
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington * Other useful things.
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonchar *
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonisc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson size_t len;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence char *ns;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley REQUIRE(VALID_CONTEXT(mctx));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley REQUIRE(s != NULL);
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington len = strlen(s);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ns = isc__mem_allocate(mctx, len + 1 FLARG_PASS);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (ns != NULL)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley strncpy(ns, s, len + 1);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington return (ns);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellingtonvoid
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellingtonisc_mem_setdestroycheck(isc_mem_t *ctx, isc_boolean_t flag) {
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington MCTXLOCK(ctx, &ctx->lock);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington ctx->checkfree = flag;
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington MCTXUNLOCK(ctx, &ctx->lock);
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington}
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington/*
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * Quotas
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonvoid
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafssonisc_mem_setquota(isc_mem_t *ctx, size_t quota) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence MCTXLOCK(ctx, &ctx->lock);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ctx->quota = quota;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington MCTXUNLOCK(ctx, &ctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleysize_t
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafssonisc_mem_getquota(isc_mem_t *ctx) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington size_t quota;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington REQUIRE(VALID_CONTEXT(ctx));
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington MCTXLOCK(ctx, &ctx->lock);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence quota = ctx->quota;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington MCTXUNLOCK(ctx, &ctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (quota);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleysize_t
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyisc_mem_inuse(isc_mem_t *ctx) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley size_t inuse;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley REQUIRE(VALID_CONTEXT(ctx));
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington MCTXLOCK(ctx, &ctx->lock);
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley inuse = ctx->inuse;
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley MCTXUNLOCK(ctx, &ctx->lock);
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley return (inuse);
e419f613d8591885df608cb73065921be07dd12eBob Halley}
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halleyvoid
e419f613d8591885df608cb73065921be07dd12eBob Halleyisc_mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington size_t hiwater, size_t lowater)
59e99793307eed0914f8467243d1c4ac761b1d9cAndreas Gustafsson{
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington isc_boolean_t callwater = ISC_FALSE;
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington isc_mem_water_t oldwater;
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson void *oldwater_arg;
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley REQUIRE(VALID_CONTEXT(ctx));
e419f613d8591885df608cb73065921be07dd12eBob Halley REQUIRE(hiwater >= lowater);
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley MCTXLOCK(ctx, &ctx->lock);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson oldwater = ctx->water;
e419f613d8591885df608cb73065921be07dd12eBob Halley oldwater_arg = ctx->water_arg;
e419f613d8591885df608cb73065921be07dd12eBob Halley if (water == NULL) {
62a84c4a27033bb0e7316256964a6950b1e230bdAndreas Gustafsson callwater = ctx->hi_called;
ef97e09e20da2133adc731cf7e29e72d04dfc93fAndreas Gustafsson ctx->water = NULL;
e419f613d8591885df608cb73065921be07dd12eBob Halley ctx->water_arg = NULL;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington ctx->hi_water = 0;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington ctx->lo_water = 0;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington ctx->hi_called = ISC_FALSE;
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson } else {
e419f613d8591885df608cb73065921be07dd12eBob Halley if (ctx->hi_called &&
e419f613d8591885df608cb73065921be07dd12eBob Halley (ctx->water != water || ctx->water_arg != water_arg ||
e419f613d8591885df608cb73065921be07dd12eBob Halley ctx->inuse < lowater || lowater == 0U))
e419f613d8591885df608cb73065921be07dd12eBob Halley callwater = ISC_TRUE;
e419f613d8591885df608cb73065921be07dd12eBob Halley ctx->water = water;
e419f613d8591885df608cb73065921be07dd12eBob Halley ctx->water_arg = water_arg;
e419f613d8591885df608cb73065921be07dd12eBob Halley ctx->hi_water = hiwater;
e419f613d8591885df608cb73065921be07dd12eBob Halley ctx->lo_water = lowater;
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington ctx->hi_called = ISC_FALSE;
e419f613d8591885df608cb73065921be07dd12eBob Halley }
e419f613d8591885df608cb73065921be07dd12eBob Halley MCTXUNLOCK(ctx, &ctx->lock);
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley if (callwater && oldwater != NULL)
e419f613d8591885df608cb73065921be07dd12eBob Halley (oldwater)(oldwater_arg, ISC_MEM_LOWATER);
e419f613d8591885df608cb73065921be07dd12eBob Halley}
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley/*
e419f613d8591885df608cb73065921be07dd12eBob Halley * Memory pool stuff
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonisc_result_t
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyisc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington isc_mempool_t *mpctx;
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington REQUIRE(VALID_CONTEXT(mctx));
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington REQUIRE(size > 0U);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington REQUIRE(mpctxp != NULL && *mpctxp == NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington /*
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington * Allocate space for this pool, initialize values, and if all works
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * well, attach to the memory context.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington mpctx = isc_mem_get(mctx, sizeof(isc_mempool_t));
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (mpctx == NULL)
e419f613d8591885df608cb73065921be07dd12eBob Halley return (ISC_R_NOMEMORY);
e419f613d8591885df608cb73065921be07dd12eBob Halley
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington mpctx->magic = MEMPOOL_MAGIC;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mpctx->lock = NULL;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mpctx->mctx = mctx;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mpctx->size = size;
e419f613d8591885df608cb73065921be07dd12eBob Halley mpctx->maxalloc = UINT_MAX;
e419f613d8591885df608cb73065921be07dd12eBob Halley mpctx->allocated = 0;
e419f613d8591885df608cb73065921be07dd12eBob Halley mpctx->freecount = 0;
e419f613d8591885df608cb73065921be07dd12eBob Halley mpctx->freemax = 1;
e419f613d8591885df608cb73065921be07dd12eBob Halley mpctx->fillcount = 1;
e419f613d8591885df608cb73065921be07dd12eBob Halley mpctx->gets = 0;
e419f613d8591885df608cb73065921be07dd12eBob Halley#if ISC_MEMPOOL_NAMES
ef97e09e20da2133adc731cf7e29e72d04dfc93fAndreas Gustafsson mpctx->name[0] = 0;
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson#endif
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence mpctx->items = NULL;
e419f613d8591885df608cb73065921be07dd12eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley *mpctxp = mpctx;
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley MCTXLOCK(mctx, &mctx->lock);
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
e419f613d8591885df608cb73065921be07dd12eBob Halley MCTXUNLOCK(mctx, &mctx->lock);
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley return (ISC_R_SUCCESS);
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson}
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonvoid
e419f613d8591885df608cb73065921be07dd12eBob Halleyisc_mempool_setname(isc_mempool_t *mpctx, const char *name) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington REQUIRE(name != NULL);
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson#if ISC_MEMPOOL_NAMES
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson if (mpctx->lock != NULL)
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson LOCK(mpctx->lock);
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington mpctx->name[sizeof(mpctx->name) - 1] = '\0';
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (mpctx->lock != NULL)
e419f613d8591885df608cb73065921be07dd12eBob Halley UNLOCK(mpctx->lock);
e419f613d8591885df608cb73065921be07dd12eBob Halley#else
e419f613d8591885df608cb73065921be07dd12eBob Halley UNUSED(mpctx);
e419f613d8591885df608cb73065921be07dd12eBob Halley UNUSED(name);
e419f613d8591885df608cb73065921be07dd12eBob Halley#endif
e419f613d8591885df608cb73065921be07dd12eBob Halley}
1872808932603066d401d3de97db11af8ffee78aAndreas Gustafsson
e419f613d8591885df608cb73065921be07dd12eBob Halleyvoid
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyisc_mempool_destroy(isc_mempool_t **mpctxp) {
e419f613d8591885df608cb73065921be07dd12eBob Halley isc_mempool_t *mpctx;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_mem_t *mctx;
e419f613d8591885df608cb73065921be07dd12eBob Halley isc_mutex_t *lock;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley element *item;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington REQUIRE(mpctxp != NULL);
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington mpctx = *mpctxp;
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington REQUIRE(VALID_MEMPOOL(mpctx));
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington#if ISC_MEMPOOL_NAMES
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington if (mpctx->allocated > 0)
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
1872808932603066d401d3de97db11af8ffee78aAndreas Gustafsson "isc_mempool_destroy(): mempool %s "
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington "leaked memory",
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson mpctx->name);
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington#endif
ef97e09e20da2133adc731cf7e29e72d04dfc93fAndreas Gustafsson REQUIRE(mpctx->allocated == 0);
1872808932603066d401d3de97db11af8ffee78aAndreas Gustafsson
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson mctx = mpctx->mctx;
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley lock = mpctx->lock;
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley if (lock != NULL)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley LOCK(lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Return any items on the free list
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley MCTXLOCK(mctx, &mctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley while (mpctx->items != NULL) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(mpctx->freecount > 0);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mpctx->freecount--;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley item = mpctx->items;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mpctx->items = item->next;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mem_putunlocked(mctx, item, mpctx->size);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington } else {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington mem_put(mctx, item, mpctx->size);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley mem_putstats(mctx, item, mpctx->size);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley MCTXUNLOCK(mctx, &mctx->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Remove our linked list entry from the memory context.
e419f613d8591885df608cb73065921be07dd12eBob Halley */
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley MCTXLOCK(mctx, &mctx->lock);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson ISC_LIST_UNLINK(mctx->pools, mpctx, link);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson MCTXUNLOCK(mctx, &mctx->lock);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson mpctx->magic = 0;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson isc_mem_put(mpctx->mctx, mpctx, sizeof(isc_mempool_t));
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson if (lock != NULL)
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson UNLOCK(lock);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson *mpctxp = NULL;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson}
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafssonvoid
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafssonisc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington REQUIRE(VALID_MEMPOOL(mpctx));
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson REQUIRE(mpctx->lock == NULL);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson REQUIRE(lock != NULL);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
ecfe4a349073b60eeaf7f1362d7dd48695514205Andreas Gustafsson mpctx->lock = lock;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafssonvoid *
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrenceisc__mempool_get(isc_mempool_t *mpctx FLARG) {
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington element *item;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson isc_mem_t *mctx;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson unsigned int i;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
8db70f36bee634ad850bc5ab60afab5c6123bc01Andreas Gustafsson REQUIRE(VALID_MEMPOOL(mpctx));
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence mctx = mpctx->mctx;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson if (mpctx->lock != NULL)
115635379a2baf2100695018109ad39e0dac349dAndreas Gustafsson LOCK(mpctx->lock);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson /*
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson * Don't let the caller go over quota
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson if (mpctx->allocated >= mpctx->maxalloc) {
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson item = NULL;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson goto out;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson }
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson /*
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * if we have a free list item, return the first here
5b0413f993b1c1ed837d23641e9f696cda1ee293Brian Wellington */
5b0413f993b1c1ed837d23641e9f696cda1ee293Brian Wellington item = mpctx->items;
5b0413f993b1c1ed837d23641e9f696cda1ee293Brian Wellington if (item != NULL) {
5b0413f993b1c1ed837d23641e9f696cda1ee293Brian Wellington mpctx->items = item->next;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson INSIST(mpctx->freecount > 0);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson mpctx->freecount--;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson mpctx->gets++;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson mpctx->allocated++;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson goto out;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson }
/*
* We need to dip into the well. Lock the memory context here and
* fill up our free list.
*/
MCTXLOCK(mctx, &mctx->lock);
for (i = 0; i < mpctx->fillcount; i++) {
if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
item = mem_getunlocked(mctx, mpctx->size);
} else {
item = mem_get(mctx, mpctx->size);
if (item != NULL)
mem_getstats(mctx, mpctx->size);
}
if (item == NULL)
break;
item->next = mpctx->items;
mpctx->items = item;
mpctx->freecount++;
}
MCTXUNLOCK(mctx, &mctx->lock);
/*
* If we didn't get any items, return NULL.
*/
item = mpctx->items;
if (item == NULL)
goto out;
mpctx->items = item->next;
mpctx->freecount--;
mpctx->gets++;
mpctx->allocated++;
out:
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
#if ISC_MEM_TRACKLINES
if (item != NULL) {
MCTXLOCK(mctx, &mctx->lock);
ADD_TRACE(mctx, item, mpctx->size, file, line);
MCTXUNLOCK(mctx, &mctx->lock);
}
#endif /* ISC_MEM_TRACKLINES */
return (item);
}
void
isc__mempool_put(isc_mempool_t *mpctx, void *mem FLARG) {
isc_mem_t *mctx;
element *item;
REQUIRE(VALID_MEMPOOL(mpctx));
REQUIRE(mem != NULL);
mctx = mpctx->mctx;
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
INSIST(mpctx->allocated > 0);
mpctx->allocated--;
#if ISC_MEM_TRACKLINES
MCTXLOCK(mctx, &mctx->lock);
DELETE_TRACE(mctx, mem, mpctx->size, file, line);
MCTXUNLOCK(mctx, &mctx->lock);
#endif /* ISC_MEM_TRACKLINES */
/*
* If our free list is full, return this to the mctx directly.
*/
if (mpctx->freecount >= mpctx->freemax) {
if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
MCTXLOCK(mctx, &mctx->lock);
mem_putunlocked(mctx, mem, mpctx->size);
MCTXUNLOCK(mctx, &mctx->lock);
} else {
mem_put(mctx, mem, mpctx->size);
MCTXLOCK(mctx, &mctx->lock);
mem_putstats(mctx, mem, mpctx->size);
MCTXUNLOCK(mctx, &mctx->lock);
}
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return;
}
/*
* Otherwise, attach it to our free list and bump the counter.
*/
mpctx->freecount++;
item = (element *)mem;
item->next = mpctx->items;
mpctx->items = item;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
}
/*
* Quotas
*/
void
isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit) {
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
mpctx->freemax = limit;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
}
unsigned int
isc_mempool_getfreemax(isc_mempool_t *mpctx) {
unsigned int freemax;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
freemax = mpctx->freemax;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (freemax);
}
unsigned int
isc_mempool_getfreecount(isc_mempool_t *mpctx) {
unsigned int freecount;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
freecount = mpctx->freecount;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (freecount);
}
void
isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit) {
REQUIRE(limit > 0);
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
mpctx->maxalloc = limit;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
}
unsigned int
isc_mempool_getmaxalloc(isc_mempool_t *mpctx) {
unsigned int maxalloc;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
maxalloc = mpctx->maxalloc;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (maxalloc);
}
unsigned int
isc_mempool_getallocated(isc_mempool_t *mpctx) {
unsigned int allocated;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
allocated = mpctx->allocated;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (allocated);
}
void
isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit) {
REQUIRE(limit > 0);
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
mpctx->fillcount = limit;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
}
unsigned int
isc_mempool_getfillcount(isc_mempool_t *mpctx) {
unsigned int fillcount;
REQUIRE(VALID_MEMPOOL(mpctx));
if (mpctx->lock != NULL)
LOCK(mpctx->lock);
fillcount = mpctx->fillcount;
if (mpctx->lock != NULL)
UNLOCK(mpctx->lock);
return (fillcount);
}
void
isc_mem_printactive(isc_mem_t *ctx, FILE *file) {
REQUIRE(VALID_CONTEXT(ctx));
REQUIRE(file != NULL);
#if !ISC_MEM_TRACKLINES
UNUSED(ctx);
UNUSED(file);
#else
print_active(ctx, file);
#endif
}
void
isc_mem_printallactive(FILE *file) {
#if !ISC_MEM_TRACKLINES
UNUSED(file);
#else
isc_mem_t *ctx;
RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
LOCK(&lock);
for (ctx = ISC_LIST_HEAD(contexts);
ctx != NULL;
ctx = ISC_LIST_NEXT(ctx, link)) {
fprintf(file, "context: %p\n", ctx);
print_active(ctx, file);
}
UNLOCK(&lock);
#endif
}
void
isc_mem_checkdestroyed(FILE *file) {
RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
LOCK(&lock);
if (!ISC_LIST_EMPTY(contexts)) {
#if ISC_MEM_TRACKLINES
isc_mem_t *ctx;
for (ctx = ISC_LIST_HEAD(contexts);
ctx != NULL;
ctx = ISC_LIST_NEXT(ctx, link)) {
fprintf(file, "context: %p\n", ctx);
print_active(ctx, file);
}
fflush(file);
#endif
INSIST(0);
}
UNLOCK(&lock);
}
#ifdef HAVE_LIBXML2
void
isc_mem_renderxml(isc_mem_t *ctx, xmlTextWriterPtr writer)
{
size_t i;
const struct stats *s;
const isc_mempool_t *pool;
REQUIRE(VALID_CONTEXT(ctx));
MCTXLOCK(ctx, &ctx->lock);
xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
xmlTextWriterWriteFormatString(writer, "%d", ctx->references);
xmlTextWriterEndElement(writer);
xmlTextWriterStartElement(writer, ISC_XMLCHAR "buckets");
for (i = 0; i <= ctx->max_size; i++) {
s = &ctx->stats[i];
if (s->totalgets == 0U && s->gets == 0U)
continue;
xmlTextWriterStartElement(writer, ISC_XMLCHAR "bucket");
xmlTextWriterStartElement(writer, ISC_XMLCHAR "size");
xmlTextWriterWriteFormatString(writer, "%ld", (long)i);
xmlTextWriterEndElement(writer); /* size */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "totalgets");
xmlTextWriterWriteFormatString(writer, "%lu", s->totalgets);
xmlTextWriterEndElement(writer); /* totalgets */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "gets");
xmlTextWriterWriteFormatString(writer, "%lu", s->gets);
xmlTextWriterEndElement(writer); /* gets */
if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
(s->blocks != 0U || s->freefrags != 0U)) {
xmlTextWriterStartElement(writer,
ISC_XMLCHAR "blocks");
xmlTextWriterWriteFormatString(writer, "%lu",
s->blocks);
xmlTextWriterEndElement(writer); /* blocks */
xmlTextWriterStartElement(writer,
ISC_XMLCHAR "freefrags");
xmlTextWriterWriteFormatString(writer, "%lu",
s->freefrags);
xmlTextWriterEndElement(writer); /* freefrags */
}
xmlTextWriterEndElement(writer); /* bucket */
}
xmlTextWriterEndElement(writer); /* buckets */
/*
* Note that since a pool can be locked now, these stats might be
* somewhat off if the pool is in active use at the time the stats
* are dumped. The link fields are protected by the isc_mem_t's
* lock, however, so walking this list and extracting integers from
* stats fields is always safe.
*/
xmlTextWriterStartElement(writer, ISC_XMLCHAR "pools");
pool = ISC_LIST_HEAD(ctx->pools);
while (pool != NULL) {
xmlTextWriterStartElement(writer, ISC_XMLCHAR "pool");
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "name",
ISC_XMLCHAR pool->name);
xmlTextWriterStartElement(writer, ISC_XMLCHAR "size");
xmlTextWriterWriteFormatString(writer, "%ld", (long)pool->size);
xmlTextWriterEndElement(writer); /* size */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxalloc");
xmlTextWriterWriteFormatString(writer, "%u", pool->maxalloc);
xmlTextWriterEndElement(writer); /* maxalloc */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "allocated");
xmlTextWriterWriteFormatString(writer, "%u", pool->allocated);
xmlTextWriterEndElement(writer); /* allocated */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "freecount");
xmlTextWriterWriteFormatString(writer, "%u", pool->freecount);
xmlTextWriterEndElement(writer); /* freecount */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "freemax");
xmlTextWriterWriteFormatString(writer, "%u", pool->freemax);
xmlTextWriterEndElement(writer); /* freemax */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "fillcount");
xmlTextWriterWriteFormatString(writer, "%u", pool->fillcount);
xmlTextWriterEndElement(writer); /* fillcount */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "gets");
xmlTextWriterWriteFormatString(writer, "%u", pool->gets);
xmlTextWriterEndElement(writer); /* gets */
xmlTextWriterStartElement(writer, ISC_XMLCHAR "locked");
xmlTextWriterWriteFormatString(writer, "%s",
((pool->lock == NULL) ? "No" : "Yes"));
xmlTextWriterEndElement(writer); /* locked */
xmlTextWriterEndElement(writer); /* pool */
pool = ISC_LIST_NEXT(pool, link);
}
xmlTextWriterEndElement(writer); /* pools */
MCTXUNLOCK(ctx, &ctx->lock);
}
#endif /* HAVE_LIBXML2 */