Home | History | Annotate | Line # | Download | only in isc
mem.c revision 1.6
      1 /*	$NetBSD: mem.c,v 1.6 2020/05/24 19:46:26 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * This Source Code Form is subject to the terms of the Mozilla Public
      7  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  *
     10  * See the COPYRIGHT file distributed with this work for additional
     11  * information regarding copyright ownership.
     12  */
     13 
     14 /*! \file */
     15 
     16 #include <errno.h>
     17 #include <inttypes.h>
     18 #include <limits.h>
     19 #include <stdbool.h>
     20 #include <stddef.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 
     24 #include <isc/bind9.h>
     25 #include <isc/hash.h>
     26 #include <isc/magic.h>
     27 #include <isc/mem.h>
     28 #include <isc/mutex.h>
     29 #include <isc/once.h>
     30 #include <isc/print.h>
     31 #include <isc/refcount.h>
     32 #include <isc/strerr.h>
     33 #include <isc/string.h>
     34 #include <isc/util.h>
     35 
     36 #ifdef HAVE_LIBXML2
     37 #include <libxml/xmlwriter.h>
     38 #define ISC_XMLCHAR (const xmlChar *)
     39 #endif /* HAVE_LIBXML2 */
     40 
     41 #ifdef HAVE_JSON_C
     42 #include <json_object.h>
     43 #endif /* HAVE_JSON_C */
     44 
     45 #include "mem_p.h"
     46 
     47 #define MCTXLOCK(m)   LOCK(&m->lock)
     48 #define MCTXUNLOCK(m) UNLOCK(&m->lock)
     49 
     50 #ifndef ISC_MEM_DEBUGGING
     51 #define ISC_MEM_DEBUGGING 0
     52 #endif /* ifndef ISC_MEM_DEBUGGING */
     53 LIBISC_EXTERNAL_DATA unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING;
     54 LIBISC_EXTERNAL_DATA unsigned int isc_mem_defaultflags = ISC_MEMFLAG_DEFAULT;
     55 
     56 /*
     57  * Constants.
     58  */
     59 
     60 #define DEF_MAX_SIZE	  1100
     61 #define DEF_MEM_TARGET	  4096
     62 #define ALIGNMENT_SIZE	  8U /*%< must be a power of 2 */
     63 #define NUM_BASIC_BLOCKS  64 /*%< must be > 1 */
     64 #define TABLE_INCREMENT	  1024
     65 #define DEBUG_TABLE_COUNT 512U
     66 
     67 /*
     68  * Types.
     69  */
     70 typedef struct isc__mem isc__mem_t;
     71 typedef struct isc__mempool isc__mempool_t;
     72 
     73 #if ISC_MEM_TRACKLINES
     74 typedef struct debuglink debuglink_t;
     75 struct debuglink {
     76 	ISC_LINK(debuglink_t) link;
     77 	const void *ptr;
     78 	size_t size;
     79 	const char *file;
     80 	unsigned int line;
     81 };
     82 
     83 typedef ISC_LIST(debuglink_t) debuglist_t;
     84 
     85 #define FLARG_PASS , file, line
     86 #define FLARG	   , const char *file, unsigned int line
     87 #else /* if ISC_MEM_TRACKLINES */
     88 #define FLARG_PASS
     89 #define FLARG
     90 #endif /* if ISC_MEM_TRACKLINES */
     91 
     92 typedef struct element element;
     93 struct element {
     94 	element *next;
     95 };
     96 
     97 typedef struct {
     98 	/*!
     99 	 * This structure must be ALIGNMENT_SIZE bytes.
    100 	 */
    101 	union {
    102 		size_t size;
    103 		isc__mem_t *ctx;
    104 		char bytes[ALIGNMENT_SIZE];
    105 	} u;
    106 } size_info;
    107 
    108 struct stats {
    109 	unsigned long gets;
    110 	unsigned long totalgets;
    111 	unsigned long blocks;
    112 	unsigned long freefrags;
    113 };
    114 
    115 #define MEM_MAGIC	 ISC_MAGIC('M', 'e', 'm', 'C')
    116 #define VALID_CONTEXT(c) ISC_MAGIC_VALID(c, MEM_MAGIC)
    117 
    118 /* List of all active memory contexts. */
    119 
    120 static ISC_LIST(isc__mem_t) contexts;
    121 
    122 static isc_once_t once = ISC_ONCE_INIT;
    123 static isc_mutex_t contextslock;
    124 
    125 /*%
    126  * Total size of lost memory due to a bug of external library.
    127  * Locked by the global lock.
    128  */
    129 static uint64_t totallost;
    130 
    131 /*%
    132  * Memory allocation and free function definitions.
    133  * isc__memalloc_t must deal with memory allocation failure
    134  * and must never return NULL.
    135  */
    136 typedef void *(*isc__memalloc_t)(size_t);
    137 typedef void (*isc__memfree_t)(void *);
    138 
    139 struct isc__mem {
    140 	isc_mem_t common;
    141 	unsigned int flags;
    142 	isc_mutex_t lock;
    143 	isc__memalloc_t memalloc;
    144 	isc__memfree_t memfree;
    145 	size_t max_size;
    146 	bool checkfree;
    147 	struct stats *stats;
    148 	isc_refcount_t references;
    149 	char name[16];
    150 	void *tag;
    151 	size_t total;
    152 	size_t inuse;
    153 	size_t maxinuse;
    154 	size_t malloced;
    155 	size_t maxmalloced;
    156 	size_t hi_water;
    157 	size_t lo_water;
    158 	bool hi_called;
    159 	bool is_overmem;
    160 	isc_mem_water_t water;
    161 	void *water_arg;
    162 	ISC_LIST(isc__mempool_t) pools;
    163 	unsigned int poolcnt;
    164 
    165 	/*  ISC_MEMFLAG_INTERNAL */
    166 	size_t mem_target;
    167 	element **freelists;
    168 	element *basic_blocks;
    169 	unsigned char **basic_table;
    170 	unsigned int basic_table_count;
    171 	unsigned int basic_table_size;
    172 	unsigned char *lowest;
    173 	unsigned char *highest;
    174 
    175 #if ISC_MEM_TRACKLINES
    176 	debuglist_t *debuglist;
    177 	size_t debuglistcnt;
    178 #endif /* if ISC_MEM_TRACKLINES */
    179 
    180 	ISC_LINK(isc__mem_t) link;
    181 };
    182 
    183 #define MEMPOOL_MAGIC	 ISC_MAGIC('M', 'E', 'M', 'p')
    184 #define VALID_MEMPOOL(c) ISC_MAGIC_VALID(c, MEMPOOL_MAGIC)
    185 
    186 struct isc__mempool {
    187 	/* always unlocked */
    188 	isc_mempool_t common; /*%< common header of mempool's */
    189 	isc_mutex_t *lock;    /*%< optional lock */
    190 	isc__mem_t *mctx;     /*%< our memory context */
    191 	/*%< locked via the memory context's lock */
    192 	ISC_LINK(isc__mempool_t) link; /*%< next pool in this mem context */
    193 	/*%< optionally locked from here down */
    194 	element *items;		/*%< low water item list */
    195 	size_t size;		/*%< size of each item on this pool */
    196 	unsigned int maxalloc;	/*%< max number of items allowed */
    197 	unsigned int allocated; /*%< # of items currently given out */
    198 	unsigned int freecount; /*%< # of items on reserved list */
    199 	unsigned int freemax;	/*%< # of items allowed on free list */
    200 	unsigned int fillcount; /*%< # of items to fetch on each fill */
    201 	/*%< Stats only. */
    202 	unsigned int gets; /*%< # of requests to this pool */
    203 			   /*%< Debugging only. */
    204 #if ISC_MEMPOOL_NAMES
    205 	char name[16]; /*%< printed name in stats reports */
    206 #endif		       /* if ISC_MEMPOOL_NAMES */
    207 };
    208 
    209 /*
    210  * Private Inline-able.
    211  */
    212 
    213 #if !ISC_MEM_TRACKLINES
    214 #define ADD_TRACE(a, b, c, d, e)
    215 #define DELETE_TRACE(a, b, c, d, e)
    216 #define ISC_MEMFUNC_SCOPE
    217 #else /* if !ISC_MEM_TRACKLINES */
    218 #define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD)
    219 #define ADD_TRACE(a, b, c, d, e)                                               \
    220 	do {                                                                   \
    221 		if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0 && \
    222 				 b != NULL))                                   \
    223 			add_trace_entry(a, b, c, d, e);                        \
    224 	} while (/*CONSTCOND*/0)
    225 #define DELETE_TRACE(a, b, c, d, e)                                            \
    226 	do {                                                                   \
    227 		if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0 && \
    228 				 b != NULL))                                   \
    229 			delete_trace_entry(a, b, c, d, e);                     \
    230 	} while (/*CONSTCOND*/0)
    231 
    232 static void
    233 print_active(isc__mem_t *ctx, FILE *out);
    234 
    235 #endif /* ISC_MEM_TRACKLINES */
    236 
    237 static void *
    238 isc___mem_get(isc_mem_t *ctx, size_t size FLARG);
    239 static void
    240 isc___mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG);
    241 static void
    242 isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG);
    243 static void *
    244 isc___mem_allocate(isc_mem_t *ctx, size_t size FLARG);
    245 static void *
    246 isc___mem_reallocate(isc_mem_t *ctx, void *ptr, size_t size FLARG);
    247 static char *
    248 isc___mem_strdup(isc_mem_t *mctx, const char *s FLARG);
    249 static void
    250 isc___mem_free(isc_mem_t *ctx, void *ptr FLARG);
    251 
    252 static isc_memmethods_t memmethods = {
    253 	isc___mem_get,	    isc___mem_put,	  isc___mem_putanddetach,
    254 	isc___mem_allocate, isc___mem_reallocate, isc___mem_strdup,
    255 	isc___mem_free,
    256 };
    257 
    258 #if ISC_MEM_TRACKLINES
    259 /*!
    260  * mctx must be locked.
    261  */
    262 static void
    263 add_trace_entry(isc__mem_t *mctx, const void *ptr, size_t size FLARG) {
    264 	debuglink_t *dl;
    265 	uint32_t hash;
    266 	uint32_t idx;
    267 
    268 	if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
    269 		fprintf(stderr, "add %p size %zu file %s line %u mctx %p\n",
    270 			ptr, size, file, line, mctx);
    271 	}
    272 
    273 	if (mctx->debuglist == NULL) {
    274 		return;
    275 	}
    276 
    277 #ifdef __COVERITY__
    278 	/*
    279 	 * Use simple conversion from pointer to hash to avoid
    280 	 * tainting 'ptr' due to byte swap in isc_hash_function.
    281 	 */
    282 	hash = (uintptr_t)ptr >> 3;
    283 #else
    284 	hash = isc_hash_function(&ptr, sizeof(ptr), true);
    285 #endif
    286 	idx = hash % DEBUG_TABLE_COUNT;
    287 
    288 	dl = malloc(sizeof(debuglink_t));
    289 	INSIST(dl != NULL);
    290 	mctx->malloced += sizeof(debuglink_t);
    291 	if (mctx->malloced > mctx->maxmalloced) {
    292 		mctx->maxmalloced = mctx->malloced;
    293 	}
    294 
    295 	ISC_LINK_INIT(dl, link);
    296 	dl->ptr = ptr;
    297 	dl->size = size;
    298 	dl->file = file;
    299 	dl->line = line;
    300 
    301 	ISC_LIST_PREPEND(mctx->debuglist[idx], dl, link);
    302 	mctx->debuglistcnt++;
    303 }
    304 
    305 static void
    306 delete_trace_entry(isc__mem_t *mctx, const void *ptr, size_t size,
    307 		   const char *file, unsigned int line) {
    308 	debuglink_t *dl;
    309 	uint32_t hash;
    310 	uint32_t idx;
    311 
    312 	if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
    313 		fprintf(stderr, "del %p size %zu file %s line %u mctx %p\n",
    314 			ptr, size, file, line, mctx);
    315 	}
    316 
    317 	if (mctx->debuglist == NULL) {
    318 		return;
    319 	}
    320 
    321 #ifdef __COVERITY__
    322 	/*
    323 	 * Use simple conversion from pointer to hash to avoid
    324 	 * tainting 'ptr' due to byte swap in isc_hash_function.
    325 	 */
    326 	hash = (uintptr_t)ptr >> 3;
    327 #else
    328 	hash = isc_hash_function(&ptr, sizeof(ptr), true);
    329 #endif
    330 	idx = hash % DEBUG_TABLE_COUNT;
    331 
    332 	dl = ISC_LIST_HEAD(mctx->debuglist[idx]);
    333 	while (ISC_LIKELY(dl != NULL)) {
    334 		if (ISC_UNLIKELY(dl->ptr == ptr)) {
    335 			ISC_LIST_UNLINK(mctx->debuglist[idx], dl, link);
    336 			mctx->malloced -= sizeof(*dl);
    337 			free(dl);
    338 			return;
    339 		}
    340 		dl = ISC_LIST_NEXT(dl, link);
    341 	}
    342 
    343 	/*
    344 	 * If we get here, we didn't find the item on the list.  We're
    345 	 * screwed.
    346 	 */
    347 	INSIST(0);
    348 	ISC_UNREACHABLE();
    349 }
    350 #endif /* ISC_MEM_TRACKLINES */
    351 
    352 static inline size_t
    353 rmsize(size_t size) {
    354 	/*
    355 	 * round down to ALIGNMENT_SIZE
    356 	 */
    357 	return (size & (~(ALIGNMENT_SIZE - 1)));
    358 }
    359 
    360 static inline size_t
    361 quantize(size_t size) {
    362 	/*!
    363 	 * Round up the result in order to get a size big
    364 	 * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
    365 	 * byte boundaries.
    366 	 */
    367 
    368 	if (size == 0U) {
    369 		return (ALIGNMENT_SIZE);
    370 	}
    371 	return ((size + ALIGNMENT_SIZE - 1) & (~(ALIGNMENT_SIZE - 1)));
    372 }
    373 
    374 static inline void
    375 more_basic_blocks(isc__mem_t *ctx) {
    376 	void *tmp;
    377 	unsigned char *curr, *next;
    378 	unsigned char *first, *last;
    379 	unsigned char **table;
    380 	unsigned int table_size;
    381 
    382 	/* Require: we hold the context lock. */
    383 
    384 	INSIST(ctx->basic_table_count <= ctx->basic_table_size);
    385 	if (ctx->basic_table_count == ctx->basic_table_size) {
    386 		table_size = ctx->basic_table_size + TABLE_INCREMENT;
    387 		table = (ctx->memalloc)(table_size * sizeof(unsigned char *));
    388 		ctx->malloced += table_size * sizeof(unsigned char *);
    389 		if (ctx->malloced > ctx->maxmalloced) {
    390 			ctx->maxmalloced = ctx->malloced;
    391 		}
    392 		if (ctx->basic_table_size != 0) {
    393 			memmove(table, ctx->basic_table,
    394 				ctx->basic_table_size *
    395 					sizeof(unsigned char *));
    396 			(ctx->memfree)(ctx->basic_table);
    397 			ctx->malloced -= ctx->basic_table_size *
    398 					 sizeof(unsigned char *);
    399 		}
    400 		ctx->basic_table = table;
    401 		ctx->basic_table_size = table_size;
    402 	}
    403 
    404 	tmp = (ctx->memalloc)(NUM_BASIC_BLOCKS * ctx->mem_target);
    405 	ctx->total += NUM_BASIC_BLOCKS * ctx->mem_target;
    406 	ctx->basic_table[ctx->basic_table_count] = tmp;
    407 	ctx->basic_table_count++;
    408 	ctx->malloced += NUM_BASIC_BLOCKS * ctx->mem_target;
    409 	if (ctx->malloced > ctx->maxmalloced) {
    410 		ctx->maxmalloced = ctx->malloced;
    411 	}
    412 
    413 	curr = tmp;
    414 	next = curr + ctx->mem_target;
    415 	for (int i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
    416 		((element *)curr)->next = (element *)next;
    417 		curr = next;
    418 		next += ctx->mem_target;
    419 	}
    420 	/*
    421 	 * curr is now pointing at the last block in the
    422 	 * array.
    423 	 */
    424 	((element *)curr)->next = NULL;
    425 	first = tmp;
    426 	last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
    427 	if (first < ctx->lowest || ctx->lowest == NULL) {
    428 		ctx->lowest = first;
    429 	}
    430 	if (last > ctx->highest) {
    431 		ctx->highest = last;
    432 	}
    433 	ctx->basic_blocks = tmp;
    434 }
    435 
    436 static inline void
    437 more_frags(isc__mem_t *ctx, size_t new_size) {
    438 	int frags;
    439 	size_t total_size;
    440 	void *tmp;
    441 	unsigned char *curr, *next;
    442 
    443 	/*!
    444 	 * Try to get more fragments by chopping up a basic block.
    445 	 */
    446 
    447 	if (ctx->basic_blocks == NULL) {
    448 		more_basic_blocks(ctx);
    449 	}
    450 
    451 	total_size = ctx->mem_target;
    452 	tmp = ctx->basic_blocks;
    453 	ctx->basic_blocks = ctx->basic_blocks->next;
    454 	frags = (int)(total_size / new_size);
    455 	ctx->stats[new_size].blocks++;
    456 	ctx->stats[new_size].freefrags += frags;
    457 	/*
    458 	 * Set up a linked-list of blocks of size
    459 	 * "new_size".
    460 	 */
    461 	curr = tmp;
    462 	next = curr + new_size;
    463 	total_size -= new_size;
    464 	for (int i = 0; i < (frags - 1); i++) {
    465 		((element *)curr)->next = (element *)next;
    466 		curr = next;
    467 		next += new_size;
    468 		total_size -= new_size;
    469 	}
    470 	/*
    471 	 * Add the remaining fragment of the basic block to a free list.
    472 	 */
    473 	total_size = rmsize(total_size);
    474 	if (total_size > 0U) {
    475 		((element *)next)->next = ctx->freelists[total_size];
    476 		ctx->freelists[total_size] = (element *)next;
    477 		ctx->stats[total_size].freefrags++;
    478 	}
    479 	/*
    480 	 * curr is now pointing at the last block in the
    481 	 * array.
    482 	 */
    483 	((element *)curr)->next = NULL;
    484 	ctx->freelists[new_size] = tmp;
    485 }
    486 
    487 static inline void *
    488 mem_getunlocked(isc__mem_t *ctx, size_t size) {
    489 	size_t new_size = quantize(size);
    490 	void *ret;
    491 
    492 	if (new_size >= ctx->max_size) {
    493 		/*
    494 		 * memget() was called on something beyond our upper limit.
    495 		 */
    496 		ret = (ctx->memalloc)(size);
    497 		ctx->total += size;
    498 		ctx->inuse += size;
    499 		ctx->stats[ctx->max_size].gets++;
    500 		ctx->stats[ctx->max_size].totalgets++;
    501 		ctx->malloced += size;
    502 		if (ctx->malloced > ctx->maxmalloced) {
    503 			ctx->maxmalloced = ctx->malloced;
    504 		}
    505 		/*
    506 		 * If we don't set new_size to size, then the
    507 		 * ISC_MEMFLAG_FILL code might write over bytes we don't
    508 		 * own.
    509 		 */
    510 		new_size = size;
    511 		goto done;
    512 	}
    513 	/*
    514 	 * If there are no blocks in the free list for this size, get a chunk
    515 	 * of memory and then break it up into "new_size"-sized blocks, adding
    516 	 * them to the free list.
    517 	 */
    518 	if (ctx->freelists[new_size] == NULL) {
    519 		more_frags(ctx, new_size);
    520 	}
    521 
    522 	/*
    523 	 * The free list uses the "rounded-up" size "new_size".
    524 	 */
    525 
    526 	ret = ctx->freelists[new_size];
    527 	ctx->freelists[new_size] = ctx->freelists[new_size]->next;
    528 
    529 	/*
    530 	 * The stats[] uses the _actual_ "size" requested by the
    531 	 * caller, with the caveat (in the code above) that "size" >= the
    532 	 * max. size (max_size) ends up getting recorded as a call to
    533 	 * max_size.
    534 	 */
    535 	ctx->stats[size].gets++;
    536 	ctx->stats[size].totalgets++;
    537 	ctx->stats[new_size].freefrags--;
    538 	ctx->inuse += new_size;
    539 
    540 done:
    541 	if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0) &&
    542 	    ISC_LIKELY(ret != NULL))
    543 	{
    544 		memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
    545 	}
    546 
    547 	return (ret);
    548 }
    549 
    550 #if ISC_MEM_CHECKOVERRUN
    551 static inline void
    552 check_overrun(void *mem, size_t size, size_t new_size) {
    553 	unsigned char *cp;
    554 
    555 	cp = (unsigned char *)mem;
    556 	cp += size;
    557 	while (size < new_size) {
    558 		INSIST(*cp == 0xbe);
    559 		cp++;
    560 		size++;
    561 	}
    562 }
    563 #endif /* if ISC_MEM_CHECKOVERRUN */
    564 
    565 /* coverity[+free : arg-1] */
    566 static inline void
    567 mem_putunlocked(isc__mem_t *ctx, void *mem, size_t size) {
    568 	size_t new_size = quantize(size);
    569 
    570 	if (new_size >= ctx->max_size) {
    571 		/*
    572 		 * memput() called on something beyond our upper limit.
    573 		 */
    574 		if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
    575 			memset(mem, 0xde, size); /* Mnemonic for "dead". */
    576 		}
    577 
    578 		(ctx->memfree)(mem);
    579 		INSIST(ctx->stats[ctx->max_size].gets != 0U);
    580 		ctx->stats[ctx->max_size].gets--;
    581 		INSIST(size <= ctx->inuse);
    582 		ctx->inuse -= size;
    583 		ctx->malloced -= size;
    584 		return;
    585 	}
    586 
    587 	if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
    588 #if ISC_MEM_CHECKOVERRUN
    589 		check_overrun(mem, size, new_size);
    590 #endif					     /* if ISC_MEM_CHECKOVERRUN */
    591 		memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
    592 	}
    593 
    594 	/*
    595 	 * The free list uses the "rounded-up" size "new_size".
    596 	 */
    597 	((element *)mem)->next = ctx->freelists[new_size];
    598 	ctx->freelists[new_size] = (element *)mem;
    599 
    600 	/*
    601 	 * The stats[] uses the _actual_ "size" requested by the
    602 	 * caller, with the caveat (in the code above) that "size" >= the
    603 	 * max. size (max_size) ends up getting recorded as a call to
    604 	 * max_size.
    605 	 */
    606 	INSIST(ctx->stats[size].gets != 0U);
    607 	ctx->stats[size].gets--;
    608 	ctx->stats[new_size].freefrags++;
    609 	ctx->inuse -= new_size;
    610 }
    611 
    612 /*!
    613  * Perform a malloc, doing memory filling and overrun detection as necessary.
    614  */
    615 static inline void *
    616 mem_get(isc__mem_t *ctx, size_t size) {
    617 	char *ret;
    618 
    619 #if ISC_MEM_CHECKOVERRUN
    620 	size += 1;
    621 #endif /* if ISC_MEM_CHECKOVERRUN */
    622 	ret = (ctx->memalloc)(size);
    623 
    624 	if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
    625 		if (ISC_LIKELY(ret != NULL)) {
    626 			memset(ret, 0xbe, size); /* Mnemonic for "beef". */
    627 		}
    628 	}
    629 #if ISC_MEM_CHECKOVERRUN
    630 	else
    631 	{
    632 		if (ISC_LIKELY(ret != NULL)) {
    633 			ret[size - 1] = 0xbe;
    634 		}
    635 	}
    636 #endif /* if ISC_MEM_CHECKOVERRUN */
    637 
    638 	return (ret);
    639 }
    640 
    641 /*!
    642  * Perform a free, doing memory filling and overrun detection as necessary.
    643  */
    644 /* coverity[+free : arg-1] */
    645 static inline void
    646 mem_put(isc__mem_t *ctx, void *mem, size_t size) {
    647 #if ISC_MEM_CHECKOVERRUN
    648 	INSIST(((unsigned char *)mem)[size] == 0xbe);
    649 	size += 1;
    650 #endif /* if ISC_MEM_CHECKOVERRUN */
    651 	if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
    652 		memset(mem, 0xde, size); /* Mnemonic for "dead". */
    653 	}
    654 	(ctx->memfree)(mem);
    655 }
    656 
    657 /*!
    658  * Update internal counters after a memory get.
    659  */
    660 static inline void
    661 mem_getstats(isc__mem_t *ctx, size_t size) {
    662 	ctx->total += size;
    663 	ctx->inuse += size;
    664 
    665 	if (size > ctx->max_size) {
    666 		ctx->stats[ctx->max_size].gets++;
    667 		ctx->stats[ctx->max_size].totalgets++;
    668 	} else {
    669 		ctx->stats[size].gets++;
    670 		ctx->stats[size].totalgets++;
    671 	}
    672 
    673 #if ISC_MEM_CHECKOVERRUN
    674 	size += 1;
    675 #endif /* if ISC_MEM_CHECKOVERRUN */
    676 	ctx->malloced += size;
    677 	if (ctx->malloced > ctx->maxmalloced) {
    678 		ctx->maxmalloced = ctx->malloced;
    679 	}
    680 }
    681 
    682 /*!
    683  * Update internal counters after a memory put.
    684  */
    685 static inline void
    686 mem_putstats(isc__mem_t *ctx, void *ptr, size_t size) {
    687 	UNUSED(ptr);
    688 
    689 	INSIST(ctx->inuse >= size);
    690 	ctx->inuse -= size;
    691 
    692 	if (size > ctx->max_size) {
    693 		INSIST(ctx->stats[ctx->max_size].gets > 0U);
    694 		ctx->stats[ctx->max_size].gets--;
    695 	} else {
    696 		INSIST(ctx->stats[size].gets > 0U);
    697 		ctx->stats[size].gets--;
    698 	}
    699 #if ISC_MEM_CHECKOVERRUN
    700 	size += 1;
    701 #endif /* if ISC_MEM_CHECKOVERRUN */
    702 	ctx->malloced -= size;
    703 }
    704 
    705 /*
    706  * Private.
    707  */
    708 
    709 static void *
    710 default_memalloc(size_t size) {
    711 	void *ptr;
    712 
    713 	ptr = malloc(size);
    714 
    715 	/*
    716 	 * If the space cannot be allocated, a null pointer is returned. If the
    717 	 * size of the space requested is zero, the behavior is
    718 	 * implementation-defined: either a null pointer is returned, or the
    719 	 * behavior is as if the size were some nonzero value, except that the
    720 	 * returned pointer shall not be used to access an object.
    721 	 * [ISO9899  7.22.3]
    722 	 *
    723 	 * [ISO9899]
    724 	 *   ISO/IEC WG 9899:2011: Programming languages - C.
    725 	 *   International Organization for Standardization, Geneva,
    726 	 * Switzerland.
    727 	 *   http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf
    728 	 */
    729 
    730 	if (ptr == NULL && size != 0) {
    731 		char strbuf[ISC_STRERRORSIZE];
    732 		strerror_r(errno, strbuf, sizeof(strbuf));
    733 		isc_error_fatal(__FILE__, __LINE__, "malloc failed: %s",
    734 				strbuf);
    735 	}
    736 
    737 	return (ptr);
    738 }
    739 
    740 static void
    741 default_memfree(void *ptr) {
    742 	free(ptr);
    743 }
    744 
    745 static void
    746 initialize_action(void) {
    747 	isc_mutex_init(&contextslock);
    748 	ISC_LIST_INIT(contexts);
    749 	totallost = 0;
    750 }
    751 
    752 static void
    753 mem_create(isc_mem_t **ctxp, unsigned int flags) {
    754 	REQUIRE(ctxp != NULL && *ctxp == NULL);
    755 
    756 	isc__mem_t *ctx;
    757 
    758 	STATIC_ASSERT((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0,
    759 		      "wrong alignment size");
    760 
    761 	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
    762 
    763 	ctx = (default_memalloc)(sizeof(*ctx));
    764 
    765 	isc_mutex_init(&ctx->lock);
    766 
    767 	ctx->max_size = DEF_MAX_SIZE;
    768 	ctx->flags = flags;
    769 	isc_refcount_init(&ctx->references, 1);
    770 	memset(ctx->name, 0, sizeof(ctx->name));
    771 	ctx->tag = NULL;
    772 	ctx->total = 0;
    773 	ctx->inuse = 0;
    774 	ctx->maxinuse = 0;
    775 	ctx->malloced = sizeof(*ctx);
    776 	ctx->maxmalloced = sizeof(*ctx);
    777 	ctx->hi_water = 0;
    778 	ctx->lo_water = 0;
    779 	ctx->hi_called = false;
    780 	ctx->is_overmem = false;
    781 	ctx->water = NULL;
    782 	ctx->water_arg = NULL;
    783 	ctx->common.impmagic = MEM_MAGIC;
    784 	ctx->common.magic = ISCAPI_MCTX_MAGIC;
    785 	ctx->common.methods = (isc_memmethods_t *)&memmethods;
    786 	ctx->memalloc = default_memalloc;
    787 	ctx->memfree = default_memfree;
    788 	ctx->stats = NULL;
    789 	ctx->checkfree = true;
    790 #if ISC_MEM_TRACKLINES
    791 	ctx->debuglist = NULL;
    792 	ctx->debuglistcnt = 0;
    793 #endif /* if ISC_MEM_TRACKLINES */
    794 	ISC_LIST_INIT(ctx->pools);
    795 	ctx->poolcnt = 0;
    796 	ctx->freelists = NULL;
    797 	ctx->basic_blocks = NULL;
    798 	ctx->basic_table = NULL;
    799 	ctx->basic_table_count = 0;
    800 	ctx->basic_table_size = 0;
    801 	ctx->lowest = NULL;
    802 	ctx->highest = NULL;
    803 
    804 	ctx->stats =
    805 		(ctx->memalloc)((ctx->max_size + 1) * sizeof(struct stats));
    806 
    807 	memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
    808 	ctx->malloced += (ctx->max_size + 1) * sizeof(struct stats);
    809 	ctx->maxmalloced += (ctx->max_size + 1) * sizeof(struct stats);
    810 
    811 	if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
    812 		ctx->mem_target = DEF_MEM_TARGET;
    813 		ctx->freelists =
    814 			(ctx->memalloc)(ctx->max_size * sizeof(element *));
    815 		memset(ctx->freelists, 0, ctx->max_size * sizeof(element *));
    816 		ctx->malloced += ctx->max_size * sizeof(element *);
    817 		ctx->maxmalloced += ctx->max_size * sizeof(element *);
    818 	}
    819 
    820 #if ISC_MEM_TRACKLINES
    821 	if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0)) {
    822 		unsigned int i;
    823 
    824 		ctx->debuglist = (ctx->memalloc)(
    825 			(DEBUG_TABLE_COUNT * sizeof(debuglist_t)));
    826 		for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
    827 			ISC_LIST_INIT(ctx->debuglist[i]);
    828 		}
    829 		ctx->malloced += DEBUG_TABLE_COUNT * sizeof(debuglist_t);
    830 		ctx->maxmalloced += DEBUG_TABLE_COUNT * sizeof(debuglist_t);
    831 	}
    832 #endif /* if ISC_MEM_TRACKLINES */
    833 
    834 	LOCK(&contextslock);
    835 	ISC_LIST_INITANDAPPEND(contexts, ctx, link);
    836 	UNLOCK(&contextslock);
    837 
    838 	*ctxp = (isc_mem_t *)ctx;
    839 }
    840 
    841 /*
    842  * Public.
    843  */
    844 
    845 static void
    846 destroy(isc__mem_t *ctx) {
    847 	unsigned int i;
    848 
    849 	LOCK(&contextslock);
    850 	ISC_LIST_UNLINK(contexts, ctx, link);
    851 	totallost += ctx->inuse;
    852 	UNLOCK(&contextslock);
    853 
    854 	ctx->common.impmagic = 0;
    855 	ctx->common.magic = 0;
    856 
    857 	INSIST(ISC_LIST_EMPTY(ctx->pools));
    858 
    859 #if ISC_MEM_TRACKLINES
    860 	if (ISC_UNLIKELY(ctx->debuglist != NULL)) {
    861 		debuglink_t *dl;
    862 		for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
    863 			for (dl = ISC_LIST_HEAD(ctx->debuglist[i]); dl != NULL;
    864 			     dl = ISC_LIST_HEAD(ctx->debuglist[i]))
    865 			{
    866 				if (ctx->checkfree && dl->ptr != NULL) {
    867 					print_active(ctx, stderr);
    868 				}
    869 				INSIST(!ctx->checkfree || dl->ptr == NULL);
    870 
    871 				ISC_LIST_UNLINK(ctx->debuglist[i], dl, link);
    872 				free(dl);
    873 				ctx->malloced -= sizeof(*dl);
    874 			}
    875 		}
    876 
    877 		(ctx->memfree)(ctx->debuglist);
    878 		ctx->malloced -= DEBUG_TABLE_COUNT * sizeof(debuglist_t);
    879 	}
    880 #endif /* if ISC_MEM_TRACKLINES */
    881 
    882 	if (ctx->checkfree) {
    883 		for (i = 0; i <= ctx->max_size; i++) {
    884 			if (ctx->stats[i].gets != 0U) {
    885 				fprintf(stderr,
    886 					"Failing assertion due to probable "
    887 					"leaked memory in context %p (\"%s\") "
    888 					"(stats[%u].gets == %lu).\n",
    889 					ctx, ctx->name, i, ctx->stats[i].gets);
    890 #if ISC_MEM_TRACKLINES
    891 				print_active(ctx, stderr);
    892 #endif /* if ISC_MEM_TRACKLINES */
    893 				INSIST(ctx->stats[i].gets == 0U);
    894 			}
    895 		}
    896 	}
    897 
    898 	(ctx->memfree)(ctx->stats);
    899 	ctx->malloced -= (ctx->max_size + 1) * sizeof(struct stats);
    900 
    901 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
    902 		for (i = 0; i < ctx->basic_table_count; i++) {
    903 			(ctx->memfree)(ctx->basic_table[i]);
    904 			ctx->malloced -= NUM_BASIC_BLOCKS * ctx->mem_target;
    905 		}
    906 		(ctx->memfree)(ctx->freelists);
    907 		ctx->malloced -= ctx->max_size * sizeof(element *);
    908 		if (ctx->basic_table != NULL) {
    909 			(ctx->memfree)(ctx->basic_table);
    910 			ctx->malloced -= ctx->basic_table_size *
    911 					 sizeof(unsigned char *);
    912 		}
    913 	}
    914 
    915 	isc_mutex_destroy(&ctx->lock);
    916 
    917 	ctx->malloced -= sizeof(*ctx);
    918 	if (ctx->checkfree) {
    919 		INSIST(ctx->malloced == 0);
    920 	}
    921 	(ctx->memfree)(ctx);
    922 }
    923 
    924 void
    925 isc_mem_attach(isc_mem_t *source0, isc_mem_t **targetp) {
    926 	REQUIRE(VALID_CONTEXT(source0));
    927 	REQUIRE(targetp != NULL && *targetp == NULL);
    928 
    929 	isc__mem_t *source = (isc__mem_t *)source0;
    930 
    931 	isc_refcount_increment(&source->references);
    932 
    933 	*targetp = (isc_mem_t *)source;
    934 }
    935 
    936 void
    937 isc_mem_detach(isc_mem_t **ctxp) {
    938 	REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
    939 
    940 	isc__mem_t *ctx = (isc__mem_t *)*ctxp;
    941 	*ctxp = NULL;
    942 
    943 	if (isc_refcount_decrement(&ctx->references) == 1) {
    944 		isc_refcount_destroy(&ctx->references);
    945 		destroy(ctx);
    946 	}
    947 }
    948 
    949 /*
    950  * isc_mem_putanddetach() is the equivalent of:
    951  *
    952  * mctx = NULL;
    953  * isc_mem_attach(ptr->mctx, &mctx);
    954  * isc_mem_detach(&ptr->mctx);
    955  * isc_mem_put(mctx, ptr, sizeof(*ptr);
    956  * isc_mem_detach(&mctx);
    957  */
    958 
    959 void
    960 isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
    961 	REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
    962 	REQUIRE(ptr != NULL);
    963 
    964 	isc__mem_t *ctx = (isc__mem_t *)*ctxp;
    965 	*ctxp = NULL;
    966 
    967 	if (ISC_UNLIKELY((isc_mem_debugging &
    968 			  (ISC_MEM_DEBUGSIZE | ISC_MEM_DEBUGCTX)) != 0))
    969 	{
    970 		if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
    971 			size_info *si = &(((size_info *)ptr)[-1]);
    972 			size_t oldsize = si->u.size - ALIGNMENT_SIZE;
    973 			if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
    974 				oldsize -= ALIGNMENT_SIZE;
    975 			}
    976 			INSIST(oldsize == size);
    977 		}
    978 		isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
    979 
    980 		goto destroy;
    981 	}
    982 
    983 	MCTXLOCK(ctx);
    984 
    985 	DELETE_TRACE(ctx, ptr, size, file, line);
    986 
    987 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
    988 		mem_putunlocked(ctx, ptr, size);
    989 	} else {
    990 		mem_putstats(ctx, ptr, size);
    991 		mem_put(ctx, ptr, size);
    992 	}
    993 	MCTXUNLOCK(ctx);
    994 
    995 destroy:
    996 	if (isc_refcount_decrement(&ctx->references) == 1) {
    997 		isc_refcount_destroy(&ctx->references);
    998 		destroy(ctx);
    999 	}
   1000 }
   1001 
   1002 void
   1003 isc_mem_destroy(isc_mem_t **ctxp) {
   1004 	/*
   1005 	 * This routine provides legacy support for callers who use mctxs
   1006 	 * without attaching/detaching.
   1007 	 */
   1008 
   1009 	REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
   1010 
   1011 	isc__mem_t *ctx = (isc__mem_t *)*ctxp;
   1012 
   1013 #if ISC_MEM_TRACKLINES
   1014 	if (isc_refcount_decrement(&ctx->references) > 1) {
   1015 		print_active(ctx, stderr);
   1016 	}
   1017 #else  /* if ISC_MEM_TRACKLINES */
   1018 	isc_refcount_decrement(&ctx->references);
   1019 #endif /* if ISC_MEM_TRACKLINES */
   1020 	isc_refcount_destroy(&ctx->references);
   1021 	destroy(ctx);
   1022 
   1023 	*ctxp = NULL;
   1024 }
   1025 
   1026 void *
   1027 isc___mem_get(isc_mem_t *ctx0, size_t size FLARG) {
   1028 	REQUIRE(VALID_CONTEXT(ctx0));
   1029 
   1030 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1031 	void *ptr;
   1032 	bool call_water = false;
   1033 
   1034 	if (ISC_UNLIKELY((isc_mem_debugging &
   1035 			  (ISC_MEM_DEBUGSIZE | ISC_MEM_DEBUGCTX)) != 0))
   1036 	{
   1037 		return (isc__mem_allocate(ctx0, size FLARG_PASS));
   1038 	}
   1039 
   1040 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   1041 		MCTXLOCK(ctx);
   1042 		ptr = mem_getunlocked(ctx, size);
   1043 	} else {
   1044 		ptr = mem_get(ctx, size);
   1045 		MCTXLOCK(ctx);
   1046 		if (ptr != NULL) {
   1047 			mem_getstats(ctx, size);
   1048 		}
   1049 	}
   1050 
   1051 	ADD_TRACE(ctx, ptr, size, file, line);
   1052 
   1053 	if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water) {
   1054 		ctx->is_overmem = true;
   1055 		if (!ctx->hi_called) {
   1056 			call_water = true;
   1057 		}
   1058 	}
   1059 	if (ctx->inuse > ctx->maxinuse) {
   1060 		ctx->maxinuse = ctx->inuse;
   1061 		if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
   1062 		    (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
   1063 		{
   1064 			fprintf(stderr, "maxinuse = %lu\n",
   1065 				(unsigned long)ctx->inuse);
   1066 		}
   1067 	}
   1068 	MCTXUNLOCK(ctx);
   1069 
   1070 	if (call_water && (ctx->water != NULL)) {
   1071 		(ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
   1072 	}
   1073 
   1074 	return (ptr);
   1075 }
   1076 
   1077 void
   1078 isc___mem_put(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
   1079 	REQUIRE(VALID_CONTEXT(ctx0));
   1080 	REQUIRE(ptr != NULL);
   1081 
   1082 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1083 	bool call_water = false;
   1084 	size_info *si;
   1085 	size_t oldsize;
   1086 
   1087 	if (ISC_UNLIKELY((isc_mem_debugging &
   1088 			  (ISC_MEM_DEBUGSIZE | ISC_MEM_DEBUGCTX)) != 0))
   1089 	{
   1090 		if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
   1091 			si = &(((size_info *)ptr)[-1]);
   1092 			oldsize = si->u.size - ALIGNMENT_SIZE;
   1093 			if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
   1094 				oldsize -= ALIGNMENT_SIZE;
   1095 			}
   1096 			INSIST(oldsize == size);
   1097 		}
   1098 		isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
   1099 		return;
   1100 	}
   1101 
   1102 	MCTXLOCK(ctx);
   1103 
   1104 	DELETE_TRACE(ctx, ptr, size, file, line);
   1105 
   1106 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   1107 		mem_putunlocked(ctx, ptr, size);
   1108 	} else {
   1109 		mem_putstats(ctx, ptr, size);
   1110 		mem_put(ctx, ptr, size);
   1111 	}
   1112 
   1113 	/*
   1114 	 * The check against ctx->lo_water == 0 is for the condition
   1115 	 * when the context was pushed over hi_water but then had
   1116 	 * isc_mem_setwater() called with 0 for hi_water and lo_water.
   1117 	 */
   1118 	if ((ctx->inuse < ctx->lo_water) || (ctx->lo_water == 0U)) {
   1119 		ctx->is_overmem = false;
   1120 		if (ctx->hi_called) {
   1121 			call_water = true;
   1122 		}
   1123 	}
   1124 
   1125 	MCTXUNLOCK(ctx);
   1126 
   1127 	if (call_water && (ctx->water != NULL)) {
   1128 		(ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
   1129 	}
   1130 }
   1131 
   1132 void
   1133 isc_mem_waterack(isc_mem_t *ctx0, int flag) {
   1134 	REQUIRE(VALID_CONTEXT(ctx0));
   1135 
   1136 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1137 
   1138 	MCTXLOCK(ctx);
   1139 	if (flag == ISC_MEM_LOWATER) {
   1140 		ctx->hi_called = false;
   1141 	} else if (flag == ISC_MEM_HIWATER) {
   1142 		ctx->hi_called = true;
   1143 	}
   1144 	MCTXUNLOCK(ctx);
   1145 }
   1146 
   1147 #if ISC_MEM_TRACKLINES
   1148 static void
   1149 print_active(isc__mem_t *mctx, FILE *out) {
   1150 	if (mctx->debuglist != NULL) {
   1151 		debuglink_t *dl;
   1152 		unsigned int i;
   1153 		bool found;
   1154 
   1155 		fputs("Dump of all outstanding memory allocations:\n", out);
   1156 		found = false;
   1157 		for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
   1158 			dl = ISC_LIST_HEAD(mctx->debuglist[i]);
   1159 
   1160 			if (dl != NULL) {
   1161 				found = true;
   1162 			}
   1163 
   1164 			while (dl != NULL) {
   1165 				if (dl->ptr != NULL) {
   1166 					fprintf(out,
   1167 						"\tptr %p size %zu file %s "
   1168 						"line %u\n",
   1169 						dl->ptr, dl->size, dl->file,
   1170 						dl->line);
   1171 				}
   1172 				dl = ISC_LIST_NEXT(dl, link);
   1173 			}
   1174 		}
   1175 
   1176 		if (!found) {
   1177 			fputs("\tNone.\n", out);
   1178 		}
   1179 	}
   1180 }
   1181 #endif /* if ISC_MEM_TRACKLINES */
   1182 
   1183 /*
   1184  * Print the stats[] on the stream "out" with suitable formatting.
   1185  */
   1186 void
   1187 isc_mem_stats(isc_mem_t *ctx0, FILE *out) {
   1188 	REQUIRE(VALID_CONTEXT(ctx0));
   1189 
   1190 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1191 	size_t i;
   1192 	const struct stats *s;
   1193 	const isc__mempool_t *pool;
   1194 
   1195 	MCTXLOCK(ctx);
   1196 
   1197 	for (i = 0; i <= ctx->max_size; i++) {
   1198 		s = &ctx->stats[i];
   1199 
   1200 		if (s->totalgets == 0U && s->gets == 0U) {
   1201 			continue;
   1202 		}
   1203 		fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
   1204 			(i == ctx->max_size) ? ">=" : "  ", (unsigned long)i,
   1205 			s->totalgets, s->gets);
   1206 		if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
   1207 		    (s->blocks != 0U || s->freefrags != 0U))
   1208 		{
   1209 			fprintf(out, " (%lu bl, %lu ff)", s->blocks,
   1210 				s->freefrags);
   1211 		}
   1212 		fputc('\n', out);
   1213 	}
   1214 
   1215 	/*
   1216 	 * Note that since a pool can be locked now, these stats might be
   1217 	 * somewhat off if the pool is in active use at the time the stats
   1218 	 * are dumped.  The link fields are protected by the isc_mem_t's
   1219 	 * lock, however, so walking this list and extracting integers from
   1220 	 * stats fields is always safe.
   1221 	 */
   1222 	pool = ISC_LIST_HEAD(ctx->pools);
   1223 	if (pool != NULL) {
   1224 		fputs("[Pool statistics]\n", out);
   1225 		fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
   1226 			"name", "size", "maxalloc", "allocated", "freecount",
   1227 			"freemax", "fillcount", "gets", "L");
   1228 	}
   1229 	while (pool != NULL) {
   1230 		fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
   1231 #if ISC_MEMPOOL_NAMES
   1232 			pool->name,
   1233 #else  /* if ISC_MEMPOOL_NAMES */
   1234 			"(not tracked)",
   1235 #endif /* if ISC_MEMPOOL_NAMES */
   1236 			(unsigned long)pool->size, pool->maxalloc,
   1237 			pool->allocated, pool->freecount, pool->freemax,
   1238 			pool->fillcount, pool->gets,
   1239 			(pool->lock == NULL ? "N" : "Y"));
   1240 		pool = ISC_LIST_NEXT(pool, link);
   1241 	}
   1242 
   1243 #if ISC_MEM_TRACKLINES
   1244 	print_active(ctx, out);
   1245 #endif /* if ISC_MEM_TRACKLINES */
   1246 
   1247 	MCTXUNLOCK(ctx);
   1248 }
   1249 
   1250 /*
   1251  * Replacements for malloc() and free() -- they implicitly remember the
   1252  * size of the object allocated (with some additional overhead).
   1253  */
   1254 
   1255 static void *
   1256 mem_allocateunlocked(isc_mem_t *ctx0, size_t size) {
   1257 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1258 	size_info *si;
   1259 
   1260 	size += ALIGNMENT_SIZE;
   1261 	if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
   1262 		size += ALIGNMENT_SIZE;
   1263 	}
   1264 
   1265 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   1266 		si = mem_getunlocked(ctx, size);
   1267 	} else {
   1268 		si = mem_get(ctx, size);
   1269 	}
   1270 
   1271 	if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
   1272 		si->u.ctx = ctx;
   1273 		si++;
   1274 	}
   1275 	si->u.size = size;
   1276 	return (&si[1]);
   1277 }
   1278 
   1279 void *
   1280 isc___mem_allocate(isc_mem_t *ctx0, size_t size FLARG) {
   1281 	REQUIRE(VALID_CONTEXT(ctx0));
   1282 
   1283 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1284 	size_info *si;
   1285 	bool call_water = false;
   1286 
   1287 	MCTXLOCK(ctx);
   1288 	si = mem_allocateunlocked((isc_mem_t *)ctx, size);
   1289 	if (((ctx->flags & ISC_MEMFLAG_INTERNAL) == 0)) {
   1290 		mem_getstats(ctx, si[-1].u.size);
   1291 	}
   1292 
   1293 	ADD_TRACE(ctx, si, si[-1].u.size, file, line);
   1294 	if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
   1295 	    !ctx->is_overmem) {
   1296 		ctx->is_overmem = true;
   1297 	}
   1298 
   1299 	if (ctx->hi_water != 0U && !ctx->hi_called &&
   1300 	    ctx->inuse > ctx->hi_water) {
   1301 		ctx->hi_called = true;
   1302 		call_water = true;
   1303 	}
   1304 	if (ctx->inuse > ctx->maxinuse) {
   1305 		ctx->maxinuse = ctx->inuse;
   1306 		if (ISC_UNLIKELY(ctx->hi_water != 0U &&
   1307 				 ctx->inuse > ctx->hi_water &&
   1308 				 (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0))
   1309 		{
   1310 			fprintf(stderr, "maxinuse = %lu\n",
   1311 				(unsigned long)ctx->inuse);
   1312 		}
   1313 	}
   1314 	MCTXUNLOCK(ctx);
   1315 
   1316 	if (call_water) {
   1317 		(ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
   1318 	}
   1319 
   1320 	return (si);
   1321 }
   1322 
   1323 void *
   1324 isc___mem_reallocate(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
   1325 	REQUIRE(VALID_CONTEXT(ctx0));
   1326 
   1327 	void *new_ptr = NULL;
   1328 	size_t oldsize, copysize;
   1329 
   1330 	/*
   1331 	 * This function emulates the realloc(3) standard library function:
   1332 	 * - if size > 0, allocate new memory; and if ptr is non NULL, copy
   1333 	 *   as much of the old contents to the new buffer and free the old one.
   1334 	 *   Note that when allocation fails the original pointer is intact;
   1335 	 *   the caller must free it.
   1336 	 * - if size is 0 and ptr is non NULL, simply free the given ptr.
   1337 	 * - this function returns:
   1338 	 *     pointer to the newly allocated memory, or
   1339 	 *     NULL if allocation fails or doesn't happen.
   1340 	 */
   1341 	if (size > 0U) {
   1342 		new_ptr = isc__mem_allocate(ctx0, size FLARG_PASS);
   1343 		if (new_ptr != NULL && ptr != NULL) {
   1344 			oldsize = (((size_info *)ptr)[-1]).u.size;
   1345 			INSIST(oldsize >= ALIGNMENT_SIZE);
   1346 			oldsize -= ALIGNMENT_SIZE;
   1347 			if (ISC_UNLIKELY((isc_mem_debugging &
   1348 					  ISC_MEM_DEBUGCTX) != 0)) {
   1349 				INSIST(oldsize >= ALIGNMENT_SIZE);
   1350 				oldsize -= ALIGNMENT_SIZE;
   1351 			}
   1352 			copysize = (oldsize > size) ? size : oldsize;
   1353 			memmove(new_ptr, ptr, copysize);
   1354 			isc__mem_free(ctx0, ptr FLARG_PASS);
   1355 		}
   1356 	} else if (ptr != NULL) {
   1357 		isc__mem_free(ctx0, ptr FLARG_PASS);
   1358 	}
   1359 
   1360 	return (new_ptr);
   1361 }
   1362 
   1363 void
   1364 isc___mem_free(isc_mem_t *ctx0, void *ptr FLARG) {
   1365 	REQUIRE(VALID_CONTEXT(ctx0));
   1366 	REQUIRE(ptr != NULL);
   1367 
   1368 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1369 	size_info *si;
   1370 	size_t size;
   1371 	bool call_water = false;
   1372 
   1373 	if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
   1374 		si = &(((size_info *)ptr)[-2]);
   1375 		REQUIRE(si->u.ctx == ctx);
   1376 		size = si[1].u.size;
   1377 	} else {
   1378 		si = &(((size_info *)ptr)[-1]);
   1379 		size = si->u.size;
   1380 	}
   1381 
   1382 	MCTXLOCK(ctx);
   1383 
   1384 	DELETE_TRACE(ctx, ptr, size, file, line);
   1385 
   1386 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   1387 		mem_putunlocked(ctx, si, size);
   1388 	} else {
   1389 		mem_putstats(ctx, si, size);
   1390 		mem_put(ctx, si, size);
   1391 	}
   1392 
   1393 	/*
   1394 	 * The check against ctx->lo_water == 0 is for the condition
   1395 	 * when the context was pushed over hi_water but then had
   1396 	 * isc_mem_setwater() called with 0 for hi_water and lo_water.
   1397 	 */
   1398 	if (ctx->is_overmem &&
   1399 	    (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
   1400 		ctx->is_overmem = false;
   1401 	}
   1402 
   1403 	if (ctx->hi_called &&
   1404 	    (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
   1405 		ctx->hi_called = false;
   1406 
   1407 		if (ctx->water != NULL) {
   1408 			call_water = true;
   1409 		}
   1410 	}
   1411 	MCTXUNLOCK(ctx);
   1412 
   1413 	if (call_water) {
   1414 		(ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
   1415 	}
   1416 }
   1417 
   1418 /*
   1419  * Other useful things.
   1420  */
   1421 
   1422 char *
   1423 isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
   1424 	REQUIRE(VALID_CONTEXT(mctx0));
   1425 	REQUIRE(s != NULL);
   1426 
   1427 	isc__mem_t *mctx = (isc__mem_t *)mctx0;
   1428 	size_t len;
   1429 	char *ns;
   1430 
   1431 	len = strlen(s) + 1;
   1432 
   1433 	ns = isc__mem_allocate((isc_mem_t *)mctx, len FLARG_PASS);
   1434 
   1435 	if (ns != NULL) {
   1436 		strlcpy(ns, s, len);
   1437 	}
   1438 
   1439 	return (ns);
   1440 }
   1441 
   1442 void
   1443 isc_mem_setdestroycheck(isc_mem_t *ctx0, bool flag) {
   1444 	REQUIRE(VALID_CONTEXT(ctx0));
   1445 
   1446 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1447 
   1448 	MCTXLOCK(ctx);
   1449 
   1450 	ctx->checkfree = flag;
   1451 
   1452 	MCTXUNLOCK(ctx);
   1453 }
   1454 
   1455 size_t
   1456 isc_mem_inuse(isc_mem_t *ctx0) {
   1457 	REQUIRE(VALID_CONTEXT(ctx0));
   1458 
   1459 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1460 	size_t inuse;
   1461 
   1462 	MCTXLOCK(ctx);
   1463 
   1464 	inuse = ctx->inuse;
   1465 
   1466 	MCTXUNLOCK(ctx);
   1467 
   1468 	return (inuse);
   1469 }
   1470 
   1471 size_t
   1472 isc_mem_maxinuse(isc_mem_t *ctx0) {
   1473 	REQUIRE(VALID_CONTEXT(ctx0));
   1474 
   1475 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1476 	size_t maxinuse;
   1477 
   1478 	MCTXLOCK(ctx);
   1479 
   1480 	maxinuse = ctx->maxinuse;
   1481 
   1482 	MCTXUNLOCK(ctx);
   1483 
   1484 	return (maxinuse);
   1485 }
   1486 
   1487 size_t
   1488 isc_mem_total(isc_mem_t *ctx0) {
   1489 	REQUIRE(VALID_CONTEXT(ctx0));
   1490 
   1491 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1492 	size_t total;
   1493 
   1494 	MCTXLOCK(ctx);
   1495 
   1496 	total = ctx->total;
   1497 
   1498 	MCTXUNLOCK(ctx);
   1499 
   1500 	return (total);
   1501 }
   1502 
   1503 void
   1504 isc_mem_setwater(isc_mem_t *ctx0, isc_mem_water_t water, void *water_arg,
   1505 		 size_t hiwater, size_t lowater) {
   1506 	REQUIRE(VALID_CONTEXT(ctx0));
   1507 	REQUIRE(hiwater >= lowater);
   1508 
   1509 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1510 	bool callwater = false;
   1511 	isc_mem_water_t oldwater;
   1512 	void *oldwater_arg;
   1513 
   1514 	MCTXLOCK(ctx);
   1515 	oldwater = ctx->water;
   1516 	oldwater_arg = ctx->water_arg;
   1517 	if (water == NULL) {
   1518 		callwater = ctx->hi_called;
   1519 		ctx->water = NULL;
   1520 		ctx->water_arg = NULL;
   1521 		ctx->hi_water = 0;
   1522 		ctx->lo_water = 0;
   1523 	} else {
   1524 		if (ctx->hi_called &&
   1525 		    (ctx->water != water || ctx->water_arg != water_arg ||
   1526 		     ctx->inuse < lowater || lowater == 0U))
   1527 		{
   1528 			callwater = true;
   1529 		}
   1530 		ctx->water = water;
   1531 		ctx->water_arg = water_arg;
   1532 		ctx->hi_water = hiwater;
   1533 		ctx->lo_water = lowater;
   1534 	}
   1535 	MCTXUNLOCK(ctx);
   1536 
   1537 	if (callwater && oldwater != NULL) {
   1538 		(oldwater)(oldwater_arg, ISC_MEM_LOWATER);
   1539 	}
   1540 }
   1541 
   1542 bool
   1543 isc_mem_isovermem(isc_mem_t *ctx0) {
   1544 	REQUIRE(VALID_CONTEXT(ctx0));
   1545 
   1546 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1547 
   1548 	/*
   1549 	 * We don't bother to lock the context because 100% accuracy isn't
   1550 	 * necessary (and even if we locked the context the returned value
   1551 	 * could be different from the actual state when it's used anyway)
   1552 	 */
   1553 	return (ctx->is_overmem);
   1554 }
   1555 
   1556 void
   1557 isc_mem_setname(isc_mem_t *ctx0, const char *name, void *tag) {
   1558 	REQUIRE(VALID_CONTEXT(ctx0));
   1559 
   1560 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1561 
   1562 	LOCK(&ctx->lock);
   1563 	strlcpy(ctx->name, name, sizeof(ctx->name));
   1564 	ctx->tag = tag;
   1565 	UNLOCK(&ctx->lock);
   1566 }
   1567 
   1568 const char *
   1569 isc_mem_getname(isc_mem_t *ctx0) {
   1570 	REQUIRE(VALID_CONTEXT(ctx0));
   1571 
   1572 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1573 
   1574 	if (ctx->name[0] == 0) {
   1575 		return ("");
   1576 	}
   1577 
   1578 	return (ctx->name);
   1579 }
   1580 
   1581 void *
   1582 isc_mem_gettag(isc_mem_t *ctx0) {
   1583 	REQUIRE(VALID_CONTEXT(ctx0));
   1584 
   1585 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   1586 
   1587 	return (ctx->tag);
   1588 }
   1589 
   1590 /*
   1591  * Memory pool stuff
   1592  */
   1593 
   1594 void
   1595 isc_mempool_create(isc_mem_t *mctx0, size_t size, isc_mempool_t **mpctxp) {
   1596 	REQUIRE(VALID_CONTEXT(mctx0));
   1597 	REQUIRE(size > 0U);
   1598 	REQUIRE(mpctxp != NULL && *mpctxp == NULL);
   1599 
   1600 	isc__mem_t *mctx = (isc__mem_t *)mctx0;
   1601 	isc__mempool_t *mpctx;
   1602 
   1603 	/*
   1604 	 * Allocate space for this pool, initialize values, and if all works
   1605 	 * well, attach to the memory context.
   1606 	 */
   1607 	mpctx = isc_mem_get((isc_mem_t *)mctx, sizeof(isc__mempool_t));
   1608 
   1609 	mpctx->common.impmagic = MEMPOOL_MAGIC;
   1610 	mpctx->common.magic = ISCAPI_MPOOL_MAGIC;
   1611 	mpctx->lock = NULL;
   1612 	mpctx->mctx = mctx;
   1613 	/*
   1614 	 * Mempools are stored as a linked list of element.
   1615 	 */
   1616 	if (size < sizeof(element)) {
   1617 		size = sizeof(element);
   1618 	}
   1619 	mpctx->size = size;
   1620 	mpctx->maxalloc = UINT_MAX;
   1621 	mpctx->allocated = 0;
   1622 	mpctx->freecount = 0;
   1623 	mpctx->freemax = 1;
   1624 	mpctx->fillcount = 1;
   1625 	mpctx->gets = 0;
   1626 #if ISC_MEMPOOL_NAMES
   1627 	mpctx->name[0] = 0;
   1628 #endif /* if ISC_MEMPOOL_NAMES */
   1629 	mpctx->items = NULL;
   1630 
   1631 	*mpctxp = (isc_mempool_t *)mpctx;
   1632 
   1633 	MCTXLOCK(mctx);
   1634 	ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
   1635 	mctx->poolcnt++;
   1636 	MCTXUNLOCK(mctx);
   1637 }
   1638 
   1639 void
   1640 isc_mempool_setname(isc_mempool_t *mpctx0, const char *name) {
   1641 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1642 	REQUIRE(name != NULL);
   1643 
   1644 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1645 
   1646 #if ISC_MEMPOOL_NAMES
   1647 	if (mpctx->lock != NULL) {
   1648 		LOCK(mpctx->lock);
   1649 	}
   1650 
   1651 	strlcpy(mpctx->name, name, sizeof(mpctx->name));
   1652 
   1653 	if (mpctx->lock != NULL) {
   1654 		UNLOCK(mpctx->lock);
   1655 	}
   1656 #else  /* if ISC_MEMPOOL_NAMES */
   1657 	UNUSED(mpctx);
   1658 	UNUSED(name);
   1659 #endif /* if ISC_MEMPOOL_NAMES */
   1660 }
   1661 
   1662 void
   1663 isc_mempool_destroy(isc_mempool_t **mpctxp) {
   1664 	REQUIRE(mpctxp != NULL);
   1665 	REQUIRE(VALID_MEMPOOL(*mpctxp));
   1666 
   1667 	isc__mempool_t *mpctx;
   1668 	isc__mem_t *mctx;
   1669 	isc_mutex_t *lock;
   1670 	element *item;
   1671 
   1672 	mpctx = (isc__mempool_t *)*mpctxp;
   1673 #if ISC_MEMPOOL_NAMES
   1674 	if (mpctx->allocated > 0) {
   1675 		UNEXPECTED_ERROR(__FILE__, __LINE__,
   1676 				 "isc_mempool_destroy(): mempool %s "
   1677 				 "leaked memory",
   1678 				 mpctx->name);
   1679 	}
   1680 #endif /* if ISC_MEMPOOL_NAMES */
   1681 	REQUIRE(mpctx->allocated == 0);
   1682 
   1683 	mctx = mpctx->mctx;
   1684 
   1685 	lock = mpctx->lock;
   1686 
   1687 	if (lock != NULL) {
   1688 		LOCK(lock);
   1689 	}
   1690 
   1691 	/*
   1692 	 * Return any items on the free list
   1693 	 */
   1694 	MCTXLOCK(mctx);
   1695 	while (mpctx->items != NULL) {
   1696 		INSIST(mpctx->freecount > 0);
   1697 		mpctx->freecount--;
   1698 		item = mpctx->items;
   1699 		mpctx->items = item->next;
   1700 
   1701 		if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   1702 			mem_putunlocked(mctx, item, mpctx->size);
   1703 		} else {
   1704 			mem_putstats(mctx, item, mpctx->size);
   1705 			mem_put(mctx, item, mpctx->size);
   1706 		}
   1707 	}
   1708 	MCTXUNLOCK(mctx);
   1709 
   1710 	/*
   1711 	 * Remove our linked list entry from the memory context.
   1712 	 */
   1713 	MCTXLOCK(mctx);
   1714 	ISC_LIST_UNLINK(mctx->pools, mpctx, link);
   1715 	mctx->poolcnt--;
   1716 	MCTXUNLOCK(mctx);
   1717 
   1718 	mpctx->common.impmagic = 0;
   1719 	mpctx->common.magic = 0;
   1720 
   1721 	isc_mem_put((isc_mem_t *)mpctx->mctx, mpctx, sizeof(isc__mempool_t));
   1722 
   1723 	if (lock != NULL) {
   1724 		UNLOCK(lock);
   1725 	}
   1726 
   1727 	*mpctxp = NULL;
   1728 }
   1729 
   1730 void
   1731 isc_mempool_associatelock(isc_mempool_t *mpctx0, isc_mutex_t *lock) {
   1732 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1733 	REQUIRE(lock != NULL);
   1734 
   1735 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1736 
   1737 	REQUIRE(mpctx->lock == NULL);
   1738 
   1739 	mpctx->lock = lock;
   1740 }
   1741 
   1742 void *
   1743 isc__mempool_get(isc_mempool_t *mpctx0 FLARG) {
   1744 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1745 
   1746 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1747 	element *item;
   1748 	isc__mem_t *mctx;
   1749 	unsigned int i;
   1750 
   1751 	mctx = mpctx->mctx;
   1752 
   1753 	if (mpctx->lock != NULL) {
   1754 		LOCK(mpctx->lock);
   1755 	}
   1756 
   1757 	/*
   1758 	 * Don't let the caller go over quota
   1759 	 */
   1760 	if (ISC_UNLIKELY(mpctx->allocated >= mpctx->maxalloc)) {
   1761 		item = NULL;
   1762 		goto out;
   1763 	}
   1764 
   1765 	if (ISC_UNLIKELY(mpctx->items == NULL)) {
   1766 		/*
   1767 		 * We need to dip into the well.  Lock the memory context
   1768 		 * here and fill up our free list.
   1769 		 */
   1770 		MCTXLOCK(mctx);
   1771 		for (i = 0; i < mpctx->fillcount; i++) {
   1772 			if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   1773 				item = mem_getunlocked(mctx, mpctx->size);
   1774 			} else {
   1775 				item = mem_get(mctx, mpctx->size);
   1776 				if (item != NULL) {
   1777 					mem_getstats(mctx, mpctx->size);
   1778 				}
   1779 			}
   1780 			if (ISC_UNLIKELY(item == NULL)) {
   1781 				break;
   1782 			}
   1783 			item->next = mpctx->items;
   1784 			mpctx->items = item;
   1785 			mpctx->freecount++;
   1786 		}
   1787 		MCTXUNLOCK(mctx);
   1788 	}
   1789 
   1790 	/*
   1791 	 * If we didn't get any items, return NULL.
   1792 	 */
   1793 	item = mpctx->items;
   1794 	if (ISC_UNLIKELY(item == NULL)) {
   1795 		goto out;
   1796 	}
   1797 
   1798 	mpctx->items = item->next;
   1799 	INSIST(mpctx->freecount > 0);
   1800 	mpctx->freecount--;
   1801 	mpctx->gets++;
   1802 	mpctx->allocated++;
   1803 
   1804 out:
   1805 	if (mpctx->lock != NULL) {
   1806 		UNLOCK(mpctx->lock);
   1807 	}
   1808 
   1809 #if ISC_MEM_TRACKLINES
   1810 	if (ISC_UNLIKELY(((isc_mem_debugging & TRACE_OR_RECORD) != 0) &&
   1811 			 item != NULL)) {
   1812 		MCTXLOCK(mctx);
   1813 		ADD_TRACE(mctx, item, mpctx->size, file, line);
   1814 		MCTXUNLOCK(mctx);
   1815 	}
   1816 #endif /* ISC_MEM_TRACKLINES */
   1817 
   1818 	return (item);
   1819 }
   1820 
   1821 /* coverity[+free : arg-1] */
   1822 void
   1823 isc__mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
   1824 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1825 	REQUIRE(mem != NULL);
   1826 
   1827 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1828 	isc__mem_t *mctx = mpctx->mctx;
   1829 	element *item;
   1830 
   1831 	if (mpctx->lock != NULL) {
   1832 		LOCK(mpctx->lock);
   1833 	}
   1834 
   1835 	INSIST(mpctx->allocated > 0);
   1836 	mpctx->allocated--;
   1837 
   1838 #if ISC_MEM_TRACKLINES
   1839 	if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0)) {
   1840 		MCTXLOCK(mctx);
   1841 		DELETE_TRACE(mctx, mem, mpctx->size, file, line);
   1842 		MCTXUNLOCK(mctx);
   1843 	}
   1844 #endif /* ISC_MEM_TRACKLINES */
   1845 
   1846 	/*
   1847 	 * If our free list is full, return this to the mctx directly.
   1848 	 */
   1849 	if (mpctx->freecount >= mpctx->freemax) {
   1850 		MCTXLOCK(mctx);
   1851 		if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   1852 			mem_putunlocked(mctx, mem, mpctx->size);
   1853 		} else {
   1854 			mem_putstats(mctx, mem, mpctx->size);
   1855 			mem_put(mctx, mem, mpctx->size);
   1856 		}
   1857 		MCTXUNLOCK(mctx);
   1858 		if (mpctx->lock != NULL) {
   1859 			UNLOCK(mpctx->lock);
   1860 		}
   1861 		return;
   1862 	}
   1863 
   1864 	/*
   1865 	 * Otherwise, attach it to our free list and bump the counter.
   1866 	 */
   1867 	mpctx->freecount++;
   1868 	item = (element *)mem;
   1869 	item->next = mpctx->items;
   1870 	mpctx->items = item;
   1871 
   1872 	if (mpctx->lock != NULL) {
   1873 		UNLOCK(mpctx->lock);
   1874 	}
   1875 }
   1876 
   1877 /*
   1878  * Quotas
   1879  */
   1880 
   1881 void
   1882 isc_mempool_setfreemax(isc_mempool_t *mpctx0, unsigned int limit) {
   1883 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1884 
   1885 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1886 
   1887 	if (mpctx->lock != NULL) {
   1888 		LOCK(mpctx->lock);
   1889 	}
   1890 
   1891 	mpctx->freemax = limit;
   1892 
   1893 	if (mpctx->lock != NULL) {
   1894 		UNLOCK(mpctx->lock);
   1895 	}
   1896 }
   1897 
   1898 unsigned int
   1899 isc_mempool_getfreemax(isc_mempool_t *mpctx0) {
   1900 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1901 
   1902 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1903 	unsigned int freemax;
   1904 
   1905 	if (mpctx->lock != NULL) {
   1906 		LOCK(mpctx->lock);
   1907 	}
   1908 
   1909 	freemax = mpctx->freemax;
   1910 
   1911 	if (mpctx->lock != NULL) {
   1912 		UNLOCK(mpctx->lock);
   1913 	}
   1914 
   1915 	return (freemax);
   1916 }
   1917 
   1918 unsigned int
   1919 isc_mempool_getfreecount(isc_mempool_t *mpctx0) {
   1920 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1921 
   1922 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1923 	unsigned int freecount;
   1924 
   1925 	if (mpctx->lock != NULL) {
   1926 		LOCK(mpctx->lock);
   1927 	}
   1928 
   1929 	freecount = mpctx->freecount;
   1930 
   1931 	if (mpctx->lock != NULL) {
   1932 		UNLOCK(mpctx->lock);
   1933 	}
   1934 
   1935 	return (freecount);
   1936 }
   1937 
   1938 void
   1939 isc_mempool_setmaxalloc(isc_mempool_t *mpctx0, unsigned int limit) {
   1940 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1941 	REQUIRE(limit > 0);
   1942 
   1943 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1944 
   1945 	if (mpctx->lock != NULL) {
   1946 		LOCK(mpctx->lock);
   1947 	}
   1948 
   1949 	mpctx->maxalloc = limit;
   1950 
   1951 	if (mpctx->lock != NULL) {
   1952 		UNLOCK(mpctx->lock);
   1953 	}
   1954 }
   1955 
   1956 unsigned int
   1957 isc_mempool_getmaxalloc(isc_mempool_t *mpctx0) {
   1958 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1959 
   1960 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1961 	unsigned int maxalloc;
   1962 
   1963 	if (mpctx->lock != NULL) {
   1964 		LOCK(mpctx->lock);
   1965 	}
   1966 
   1967 	maxalloc = mpctx->maxalloc;
   1968 
   1969 	if (mpctx->lock != NULL) {
   1970 		UNLOCK(mpctx->lock);
   1971 	}
   1972 
   1973 	return (maxalloc);
   1974 }
   1975 
   1976 unsigned int
   1977 isc_mempool_getallocated(isc_mempool_t *mpctx0) {
   1978 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1979 
   1980 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   1981 	unsigned int allocated;
   1982 
   1983 	if (mpctx->lock != NULL) {
   1984 		LOCK(mpctx->lock);
   1985 	}
   1986 
   1987 	allocated = mpctx->allocated;
   1988 
   1989 	if (mpctx->lock != NULL) {
   1990 		UNLOCK(mpctx->lock);
   1991 	}
   1992 
   1993 	return (allocated);
   1994 }
   1995 
   1996 void
   1997 isc_mempool_setfillcount(isc_mempool_t *mpctx0, unsigned int limit) {
   1998 	REQUIRE(VALID_MEMPOOL(mpctx0));
   1999 	REQUIRE(limit > 0);
   2000 
   2001 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   2002 
   2003 	if (mpctx->lock != NULL) {
   2004 		LOCK(mpctx->lock);
   2005 	}
   2006 
   2007 	mpctx->fillcount = limit;
   2008 
   2009 	if (mpctx->lock != NULL) {
   2010 		UNLOCK(mpctx->lock);
   2011 	}
   2012 }
   2013 
   2014 unsigned int
   2015 isc_mempool_getfillcount(isc_mempool_t *mpctx0) {
   2016 	REQUIRE(VALID_MEMPOOL(mpctx0));
   2017 
   2018 	isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
   2019 
   2020 	unsigned int fillcount;
   2021 
   2022 	if (mpctx->lock != NULL) {
   2023 		LOCK(mpctx->lock);
   2024 	}
   2025 
   2026 	fillcount = mpctx->fillcount;
   2027 
   2028 	if (mpctx->lock != NULL) {
   2029 		UNLOCK(mpctx->lock);
   2030 	}
   2031 
   2032 	return (fillcount);
   2033 }
   2034 
   2035 /*
   2036  * Requires contextslock to be held by caller.
   2037  */
   2038 static void
   2039 print_contexts(FILE *file) {
   2040 	isc__mem_t *ctx;
   2041 
   2042 	for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
   2043 	     ctx = ISC_LIST_NEXT(ctx, link)) {
   2044 		fprintf(file, "context: %p (%s): %" PRIuFAST32 " references\n",
   2045 			ctx, ctx->name[0] == 0 ? "<unknown>" : ctx->name,
   2046 			isc_refcount_current(&ctx->references));
   2047 		print_active(ctx, file);
   2048 	}
   2049 	fflush(file);
   2050 }
   2051 
   2052 void
   2053 isc_mem_checkdestroyed(FILE *file) {
   2054 #if !ISC_MEM_TRACKLINES
   2055 	UNUSED(file);
   2056 #endif /* if !ISC_MEM_TRACKLINES */
   2057 
   2058 	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
   2059 
   2060 	LOCK(&contextslock);
   2061 	if (!ISC_LIST_EMPTY(contexts)) {
   2062 #if ISC_MEM_TRACKLINES
   2063 		if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0)) {
   2064 			print_contexts(file);
   2065 		}
   2066 #endif /* if ISC_MEM_TRACKLINES */
   2067 		INSIST(0);
   2068 		ISC_UNREACHABLE();
   2069 	}
   2070 	UNLOCK(&contextslock);
   2071 }
   2072 
   2073 unsigned int
   2074 isc_mem_references(isc_mem_t *ctx0) {
   2075 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   2076 	return (isc_refcount_current(&ctx->references));
   2077 }
   2078 
   2079 typedef struct summarystat {
   2080 	uint64_t total;
   2081 	uint64_t inuse;
   2082 	uint64_t malloced;
   2083 	uint64_t blocksize;
   2084 	uint64_t contextsize;
   2085 } summarystat_t;
   2086 
   2087 #ifdef HAVE_LIBXML2
   2088 #define TRY0(a)                     \
   2089 	do {                        \
   2090 		xmlrc = (a);        \
   2091 		if (xmlrc < 0)      \
   2092 			goto error; \
   2093 	} while (/*CONSTCOND*/0)
   2094 static int
   2095 xml_renderctx(isc__mem_t *ctx, summarystat_t *summary,
   2096 	      xmlTextWriterPtr writer) {
   2097 	REQUIRE(VALID_CONTEXT(ctx));
   2098 
   2099 	int xmlrc;
   2100 
   2101 	MCTXLOCK(ctx);
   2102 
   2103 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "context"));
   2104 
   2105 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "id"));
   2106 	TRY0(xmlTextWriterWriteFormatString(writer, "%p", ctx));
   2107 	TRY0(xmlTextWriterEndElement(writer)); /* id */
   2108 
   2109 	if (ctx->name[0] != 0) {
   2110 		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
   2111 		TRY0(xmlTextWriterWriteFormatString(writer, "%s", ctx->name));
   2112 		TRY0(xmlTextWriterEndElement(writer)); /* name */
   2113 	}
   2114 
   2115 	summary->contextsize += sizeof(*ctx) +
   2116 				(ctx->max_size + 1) * sizeof(struct stats) +
   2117 				ctx->max_size * sizeof(element *) +
   2118 				ctx->basic_table_count * sizeof(char *);
   2119 #if ISC_MEM_TRACKLINES
   2120 	if (ctx->debuglist != NULL) {
   2121 		summary->contextsize += DEBUG_TABLE_COUNT *
   2122 						sizeof(debuglist_t) +
   2123 					ctx->debuglistcnt * sizeof(debuglink_t);
   2124 	}
   2125 #endif /* if ISC_MEM_TRACKLINES */
   2126 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "references"));
   2127 	TRY0(xmlTextWriterWriteFormatString(
   2128 		writer, "%" PRIuFAST32,
   2129 		isc_refcount_current(&ctx->references)));
   2130 	TRY0(xmlTextWriterEndElement(writer)); /* references */
   2131 
   2132 	summary->total += ctx->total;
   2133 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "total"));
   2134 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2135 					    (uint64_t)ctx->total));
   2136 	TRY0(xmlTextWriterEndElement(writer)); /* total */
   2137 
   2138 	summary->inuse += ctx->inuse;
   2139 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "inuse"));
   2140 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2141 					    (uint64_t)ctx->inuse));
   2142 	TRY0(xmlTextWriterEndElement(writer)); /* inuse */
   2143 
   2144 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxinuse"));
   2145 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2146 					    (uint64_t)ctx->maxinuse));
   2147 	TRY0(xmlTextWriterEndElement(writer)); /* maxinuse */
   2148 
   2149 	summary->malloced += ctx->malloced;
   2150 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "malloced"));
   2151 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2152 					    (uint64_t)ctx->malloced));
   2153 	TRY0(xmlTextWriterEndElement(writer)); /* malloced */
   2154 
   2155 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxmalloced"));
   2156 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2157 					    (uint64_t)ctx->maxmalloced));
   2158 	TRY0(xmlTextWriterEndElement(writer)); /* maxmalloced */
   2159 
   2160 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "blocksize"));
   2161 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   2162 		summary->blocksize += ctx->basic_table_count *
   2163 				      NUM_BASIC_BLOCKS * ctx->mem_target;
   2164 		TRY0(xmlTextWriterWriteFormatString(
   2165 			writer, "%" PRIu64 "",
   2166 			(uint64_t)ctx->basic_table_count * NUM_BASIC_BLOCKS *
   2167 				ctx->mem_target));
   2168 	} else {
   2169 		TRY0(xmlTextWriterWriteFormatString(writer, "%s", "-"));
   2170 	}
   2171 	TRY0(xmlTextWriterEndElement(writer)); /* blocksize */
   2172 
   2173 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "pools"));
   2174 	TRY0(xmlTextWriterWriteFormatString(writer, "%u", ctx->poolcnt));
   2175 	TRY0(xmlTextWriterEndElement(writer)); /* pools */
   2176 	summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
   2177 
   2178 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "hiwater"));
   2179 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2180 					    (uint64_t)ctx->hi_water));
   2181 	TRY0(xmlTextWriterEndElement(writer)); /* hiwater */
   2182 
   2183 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "lowater"));
   2184 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2185 					    (uint64_t)ctx->lo_water));
   2186 	TRY0(xmlTextWriterEndElement(writer)); /* lowater */
   2187 
   2188 	TRY0(xmlTextWriterEndElement(writer)); /* context */
   2189 
   2190 error:
   2191 	MCTXUNLOCK(ctx);
   2192 
   2193 	return (xmlrc);
   2194 }
   2195 
   2196 int
   2197 isc_mem_renderxml(void *writer0) {
   2198 	isc__mem_t *ctx;
   2199 	summarystat_t summary;
   2200 	uint64_t lost;
   2201 	int xmlrc;
   2202 	xmlTextWriterPtr writer = (xmlTextWriterPtr)writer0;
   2203 
   2204 	memset(&summary, 0, sizeof(summary));
   2205 
   2206 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "contexts"));
   2207 
   2208 	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
   2209 
   2210 	LOCK(&contextslock);
   2211 	lost = totallost;
   2212 	for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
   2213 	     ctx = ISC_LIST_NEXT(ctx, link)) {
   2214 		xmlrc = xml_renderctx(ctx, &summary, writer);
   2215 		if (xmlrc < 0) {
   2216 			UNLOCK(&contextslock);
   2217 			goto error;
   2218 		}
   2219 	}
   2220 	UNLOCK(&contextslock);
   2221 
   2222 	TRY0(xmlTextWriterEndElement(writer)); /* contexts */
   2223 
   2224 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "summary"));
   2225 
   2226 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "TotalUse"));
   2227 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2228 					    summary.total));
   2229 	TRY0(xmlTextWriterEndElement(writer)); /* TotalUse */
   2230 
   2231 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "InUse"));
   2232 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2233 					    summary.inuse));
   2234 	TRY0(xmlTextWriterEndElement(writer)); /* InUse */
   2235 
   2236 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "Malloced"));
   2237 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2238 					    summary.malloced));
   2239 	TRY0(xmlTextWriterEndElement(writer)); /* InUse */
   2240 
   2241 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "BlockSize"));
   2242 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2243 					    summary.blocksize));
   2244 	TRY0(xmlTextWriterEndElement(writer)); /* BlockSize */
   2245 
   2246 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "ContextSize"));
   2247 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
   2248 					    summary.contextsize));
   2249 	TRY0(xmlTextWriterEndElement(writer)); /* ContextSize */
   2250 
   2251 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "Lost"));
   2252 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "", lost));
   2253 	TRY0(xmlTextWriterEndElement(writer)); /* Lost */
   2254 
   2255 	TRY0(xmlTextWriterEndElement(writer)); /* summary */
   2256 error:
   2257 	return (xmlrc);
   2258 }
   2259 
   2260 #endif /* HAVE_LIBXML2 */
   2261 
   2262 #ifdef HAVE_JSON_C
   2263 #define CHECKMEM(m) RUNTIME_CHECK(m != NULL)
   2264 
   2265 static isc_result_t
   2266 json_renderctx(isc__mem_t *ctx, summarystat_t *summary, json_object *array) {
   2267 	REQUIRE(VALID_CONTEXT(ctx));
   2268 	REQUIRE(summary != NULL);
   2269 	REQUIRE(array != NULL);
   2270 
   2271 	json_object *ctxobj, *obj;
   2272 	char buf[1024];
   2273 
   2274 	MCTXLOCK(ctx);
   2275 
   2276 	summary->contextsize += sizeof(*ctx) +
   2277 				(ctx->max_size + 1) * sizeof(struct stats) +
   2278 				ctx->max_size * sizeof(element *) +
   2279 				ctx->basic_table_count * sizeof(char *);
   2280 	summary->total += ctx->total;
   2281 	summary->inuse += ctx->inuse;
   2282 	summary->malloced += ctx->malloced;
   2283 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   2284 		summary->blocksize += ctx->basic_table_count *
   2285 				      NUM_BASIC_BLOCKS * ctx->mem_target;
   2286 	}
   2287 #if ISC_MEM_TRACKLINES
   2288 	if (ctx->debuglist != NULL) {
   2289 		summary->contextsize += DEBUG_TABLE_COUNT *
   2290 						sizeof(debuglist_t) +
   2291 					ctx->debuglistcnt * sizeof(debuglink_t);
   2292 	}
   2293 #endif /* if ISC_MEM_TRACKLINES */
   2294 
   2295 	ctxobj = json_object_new_object();
   2296 	CHECKMEM(ctxobj);
   2297 
   2298 	snprintf(buf, sizeof(buf), "%p", ctx);
   2299 	obj = json_object_new_string(buf);
   2300 	CHECKMEM(obj);
   2301 	json_object_object_add(ctxobj, "id", obj);
   2302 
   2303 	if (ctx->name[0] != 0) {
   2304 		obj = json_object_new_string(ctx->name);
   2305 		CHECKMEM(obj);
   2306 		json_object_object_add(ctxobj, "name", obj);
   2307 	}
   2308 
   2309 	obj = json_object_new_int64(isc_refcount_current(&ctx->references));
   2310 	CHECKMEM(obj);
   2311 	json_object_object_add(ctxobj, "references", obj);
   2312 
   2313 	obj = json_object_new_int64(ctx->total);
   2314 	CHECKMEM(obj);
   2315 	json_object_object_add(ctxobj, "total", obj);
   2316 
   2317 	obj = json_object_new_int64(ctx->inuse);
   2318 	CHECKMEM(obj);
   2319 	json_object_object_add(ctxobj, "inuse", obj);
   2320 
   2321 	obj = json_object_new_int64(ctx->maxinuse);
   2322 	CHECKMEM(obj);
   2323 	json_object_object_add(ctxobj, "maxinuse", obj);
   2324 
   2325 	obj = json_object_new_int64(ctx->malloced);
   2326 	CHECKMEM(obj);
   2327 	json_object_object_add(ctxobj, "malloced", obj);
   2328 
   2329 	obj = json_object_new_int64(ctx->maxmalloced);
   2330 	CHECKMEM(obj);
   2331 	json_object_object_add(ctxobj, "maxmalloced", obj);
   2332 
   2333 	if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
   2334 		uint64_t blocksize;
   2335 		blocksize = ctx->basic_table_count * NUM_BASIC_BLOCKS *
   2336 			    ctx->mem_target;
   2337 		obj = json_object_new_int64(blocksize);
   2338 		CHECKMEM(obj);
   2339 		json_object_object_add(ctxobj, "blocksize", obj);
   2340 	}
   2341 
   2342 	obj = json_object_new_int64(ctx->poolcnt);
   2343 	CHECKMEM(obj);
   2344 	json_object_object_add(ctxobj, "pools", obj);
   2345 
   2346 	summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
   2347 
   2348 	obj = json_object_new_int64(ctx->hi_water);
   2349 	CHECKMEM(obj);
   2350 	json_object_object_add(ctxobj, "hiwater", obj);
   2351 
   2352 	obj = json_object_new_int64(ctx->lo_water);
   2353 	CHECKMEM(obj);
   2354 	json_object_object_add(ctxobj, "lowater", obj);
   2355 
   2356 	MCTXUNLOCK(ctx);
   2357 	json_object_array_add(array, ctxobj);
   2358 	return (ISC_R_SUCCESS);
   2359 }
   2360 
   2361 isc_result_t
   2362 isc_mem_renderjson(void *memobj0) {
   2363 	isc_result_t result = ISC_R_SUCCESS;
   2364 	isc__mem_t *ctx;
   2365 	summarystat_t summary;
   2366 	uint64_t lost;
   2367 	json_object *ctxarray, *obj;
   2368 	json_object *memobj = (json_object *)memobj0;
   2369 
   2370 	memset(&summary, 0, sizeof(summary));
   2371 	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
   2372 
   2373 	ctxarray = json_object_new_array();
   2374 	CHECKMEM(ctxarray);
   2375 
   2376 	LOCK(&contextslock);
   2377 	lost = totallost;
   2378 	for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
   2379 	     ctx = ISC_LIST_NEXT(ctx, link)) {
   2380 		result = json_renderctx(ctx, &summary, ctxarray);
   2381 		if (result != ISC_R_SUCCESS) {
   2382 			UNLOCK(&contextslock);
   2383 			goto error;
   2384 		}
   2385 	}
   2386 	UNLOCK(&contextslock);
   2387 
   2388 	obj = json_object_new_int64(summary.total);
   2389 	CHECKMEM(obj);
   2390 	json_object_object_add(memobj, "TotalUse", obj);
   2391 
   2392 	obj = json_object_new_int64(summary.inuse);
   2393 	CHECKMEM(obj);
   2394 	json_object_object_add(memobj, "InUse", obj);
   2395 
   2396 	obj = json_object_new_int64(summary.malloced);
   2397 	CHECKMEM(obj);
   2398 	json_object_object_add(memobj, "Malloced", obj);
   2399 
   2400 	obj = json_object_new_int64(summary.blocksize);
   2401 	CHECKMEM(obj);
   2402 	json_object_object_add(memobj, "BlockSize", obj);
   2403 
   2404 	obj = json_object_new_int64(summary.contextsize);
   2405 	CHECKMEM(obj);
   2406 	json_object_object_add(memobj, "ContextSize", obj);
   2407 
   2408 	obj = json_object_new_int64(lost);
   2409 	CHECKMEM(obj);
   2410 	json_object_object_add(memobj, "Lost", obj);
   2411 
   2412 	json_object_object_add(memobj, "contexts", ctxarray);
   2413 	return (ISC_R_SUCCESS);
   2414 
   2415 error:
   2416 	if (ctxarray != NULL) {
   2417 		json_object_put(ctxarray);
   2418 	}
   2419 	return (result);
   2420 }
   2421 #endif /* HAVE_JSON_C */
   2422 
   2423 void
   2424 isc_mem_create(isc_mem_t **mctxp) {
   2425 	mem_create(mctxp, isc_mem_defaultflags);
   2426 }
   2427 
   2428 void *
   2429 isc__mem_get(isc_mem_t *mctx, size_t size FLARG) {
   2430 	REQUIRE(ISCAPI_MCTX_VALID(mctx));
   2431 
   2432 	return (mctx->methods->memget(mctx, size FLARG_PASS));
   2433 }
   2434 
   2435 void
   2436 isc__mem_put(isc_mem_t *mctx, void *ptr, size_t size FLARG) {
   2437 	REQUIRE(ISCAPI_MCTX_VALID(mctx));
   2438 
   2439 	mctx->methods->memput(mctx, ptr, size FLARG_PASS);
   2440 }
   2441 
   2442 void
   2443 isc__mem_putanddetach(isc_mem_t **mctxp, void *ptr, size_t size FLARG) {
   2444 	REQUIRE(mctxp != NULL && ISCAPI_MCTX_VALID(*mctxp));
   2445 
   2446 	(*mctxp)->methods->memputanddetach(mctxp, ptr, size FLARG_PASS);
   2447 }
   2448 
   2449 void *
   2450 isc__mem_allocate(isc_mem_t *mctx, size_t size FLARG) {
   2451 	REQUIRE(ISCAPI_MCTX_VALID(mctx));
   2452 
   2453 	return (mctx->methods->memallocate(mctx, size FLARG_PASS));
   2454 }
   2455 
   2456 void *
   2457 isc__mem_reallocate(isc_mem_t *mctx, void *ptr, size_t size FLARG) {
   2458 	REQUIRE(ISCAPI_MCTX_VALID(mctx));
   2459 
   2460 	return (mctx->methods->memreallocate(mctx, ptr, size FLARG_PASS));
   2461 }
   2462 
   2463 char *
   2464 isc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
   2465 	REQUIRE(ISCAPI_MCTX_VALID(mctx));
   2466 
   2467 	return (mctx->methods->memstrdup(mctx, s FLARG_PASS));
   2468 }
   2469 
   2470 void
   2471 isc__mem_free(isc_mem_t *mctx, void *ptr FLARG) {
   2472 	REQUIRE(ISCAPI_MCTX_VALID(mctx));
   2473 
   2474 	mctx->methods->memfree(mctx, ptr FLARG_PASS);
   2475 }
   2476 
   2477 void
   2478 isc__mem_printactive(isc_mem_t *ctx0, FILE *file) {
   2479 #if ISC_MEM_TRACKLINES
   2480 	REQUIRE(VALID_CONTEXT(ctx0));
   2481 	REQUIRE(file != NULL);
   2482 
   2483 	isc__mem_t *ctx = (isc__mem_t *)ctx0;
   2484 
   2485 	print_active(ctx, file);
   2486 #else  /* if ISC_MEM_TRACKLINES */
   2487 	UNUSED(ctx0);
   2488 	UNUSED(file);
   2489 #endif /* if ISC_MEM_TRACKLINES */
   2490 }
   2491