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