Home | History | Annotate | Line # | Download | only in isc
      1  1.12  christos /*	$NetBSD: mem.h,v 1.12 2025/01/26 16:25:41 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.9  christos  * SPDX-License-Identifier: MPL-2.0
      7   1.9  christos  *
      8   1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      9   1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10   1.5  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11   1.1  christos  *
     12   1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     13   1.1  christos  * information regarding copyright ownership.
     14   1.1  christos  */
     15   1.1  christos 
     16  1.10  christos #pragma once
     17   1.1  christos 
     18   1.1  christos /*! \file isc/mem.h */
     19   1.1  christos 
     20   1.3  christos #include <stdbool.h>
     21   1.1  christos #include <stdio.h>
     22   1.1  christos 
     23  1.10  christos #include <isc/attributes.h>
     24   1.1  christos #include <isc/lang.h>
     25   1.1  christos #include <isc/mutex.h>
     26  1.12  christos #include <isc/overflow.h>
     27   1.1  christos #include <isc/types.h>
     28  1.12  christos #include <isc/urcu.h>
     29   1.1  christos 
     30   1.1  christos ISC_LANG_BEGINDECLS
     31   1.1  christos 
     32   1.1  christos /*%
     33   1.1  christos  * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
     34   1.1  christos  * allocation and freeing by file and line number.
     35   1.1  christos  */
     36   1.1  christos #ifndef ISC_MEM_TRACKLINES
     37  1.10  christos #define ISC_MEM_TRACKLINES 0
     38   1.4  christos #endif /* ifndef ISC_MEM_TRACKLINES */
     39   1.1  christos 
     40  1.10  christos extern unsigned int isc_mem_debugging;
     41  1.10  christos extern unsigned int isc_mem_defaultflags;
     42   1.1  christos 
     43   1.1  christos /*@{*/
     44   1.4  christos #define ISC_MEM_DEBUGTRACE  0x00000001U
     45   1.4  christos #define ISC_MEM_DEBUGRECORD 0x00000002U
     46   1.4  christos #define ISC_MEM_DEBUGUSAGE  0x00000004U
     47  1.12  christos #define ISC_MEM_DEBUGALL \
     48  1.12  christos 	(ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD | ISC_MEM_DEBUGUSAGE)
     49   1.1  christos /*!<
     50   1.1  christos  * The variable isc_mem_debugging holds a set of flags for
     51   1.1  christos  * turning certain memory debugging options on or off at
     52   1.1  christos  * runtime.  It is initialized to the value ISC_MEM_DEGBUGGING,
     53   1.1  christos  * which is 0 by default but may be overridden at compile time.
     54   1.1  christos  * The following flags can be specified:
     55   1.1  christos  *
     56   1.1  christos  * \li #ISC_MEM_DEBUGTRACE
     57   1.1  christos  *	Log each allocation and free to isc_lctx.
     58   1.1  christos  *
     59   1.1  christos  * \li #ISC_MEM_DEBUGRECORD
     60   1.1  christos  *	Remember each allocation, and match them up on free.
     61   1.1  christos  *	Crash if a free doesn't match an allocation.
     62   1.1  christos  *
     63   1.1  christos  * \li #ISC_MEM_DEBUGUSAGE
     64  1.12  christos  *	Every time the memory usage is greater (lower) than hi_water
     65  1.12  christos  *	(lo_water) mark, print the current inuse memory.
     66   1.1  christos  */
     67   1.1  christos /*@}*/
     68   1.1  christos 
     69   1.1  christos #if ISC_MEM_TRACKLINES
     70   1.4  christos #define _ISC_MEM_FILELINE , __FILE__, __LINE__
     71   1.4  christos #define _ISC_MEM_FLARG	  , const char *, unsigned int
     72   1.4  christos #else /* if ISC_MEM_TRACKLINES */
     73   1.1  christos #define _ISC_MEM_FILELINE
     74   1.1  christos #define _ISC_MEM_FLARG
     75   1.4  christos #endif /* if ISC_MEM_TRACKLINES */
     76   1.1  christos 
     77   1.1  christos /*
     78   1.4  christos  * Flags for isc_mem_create() calls.
     79   1.1  christos  */
     80  1.10  christos #define ISC_MEMFLAG_RESERVED1 0x00000001 /* reserved, obsoleted, don't use */
     81  1.10  christos #define ISC_MEMFLAG_RESERVED2 0x00000002 /* reserved, obsoleted, don't use */
     82   1.4  christos #define ISC_MEMFLAG_FILL \
     83   1.4  christos 	0x00000004 /* fill with pattern after alloc and frees */
     84   1.1  christos 
     85   1.9  christos /*%
     86   1.9  christos  * Define ISC_MEM_DEFAULTFILL=1 to turn filling the memory with pattern
     87   1.9  christos  * after alloc and free.
     88   1.9  christos  */
     89   1.9  christos #if ISC_MEM_DEFAULTFILL
     90   1.9  christos #define ISC_MEMFLAG_DEFAULT ISC_MEMFLAG_FILL
     91  1.10  christos #else /* if !ISC_MEM_USE_INTERNAL_MALLOC */
     92   1.4  christos #define ISC_MEMFLAG_DEFAULT 0
     93   1.4  christos #endif /* if !ISC_MEM_USE_INTERNAL_MALLOC */
     94   1.1  christos 
     95   1.1  christos /*%
     96   1.1  christos  * isc_mem_putanddetach() is a convenience function for use where you
     97   1.1  christos  * have a structure with an attached memory context.
     98   1.1  christos  *
     99   1.1  christos  * Given:
    100   1.1  christos  *
    101   1.1  christos  * \code
    102   1.1  christos  * struct {
    103   1.1  christos  *	...
    104   1.1  christos  *	isc_mem_t *mctx;
    105   1.1  christos  *	...
    106   1.1  christos  * } *ptr;
    107   1.1  christos  *
    108   1.1  christos  * isc_mem_t *mctx;
    109   1.1  christos  *
    110   1.1  christos  * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr));
    111   1.1  christos  * \endcode
    112   1.1  christos  *
    113   1.1  christos  * is the equivalent of:
    114   1.1  christos  *
    115   1.1  christos  * \code
    116   1.1  christos  * mctx = NULL;
    117   1.1  christos  * isc_mem_attach(ptr->mctx, &mctx);
    118   1.1  christos  * isc_mem_detach(&ptr->mctx);
    119   1.1  christos  * isc_mem_put(mctx, ptr, sizeof(*ptr));
    120   1.1  christos  * isc_mem_detach(&mctx);
    121   1.1  christos  * \endcode
    122   1.1  christos  */
    123   1.1  christos 
    124   1.3  christos /*%
    125   1.3  christos  * These functions are actually implemented in isc__mem_<function>
    126   1.3  christos  * (two underscores). The single-underscore macros are used to pass
    127   1.3  christos  * __FILE__ and __LINE__, and in the case of the put functions, to
    128   1.3  christos  * set the pointer being freed to NULL in the calling function.
    129  1.12  christos  */
    130  1.12  christos 
    131  1.12  christos /*%
    132  1.12  christos  * The definitions of the macros have been pulled directly from jemalloc.h
    133  1.12  christos  * and checked for consistency in mem.c.
    134   1.3  christos  *
    135  1.12  christos  *\li	ISC__MEM_ZERO - fill the memory with zeroes before returning
    136   1.3  christos  */
    137   1.3  christos 
    138  1.12  christos #define ISC__MEM_ZERO ((int)0x40)
    139  1.12  christos 
    140  1.12  christos #define isc_mem_get(c, s) isc__mem_get((c), (s), 0 _ISC_MEM_FILELINE)
    141  1.12  christos #define isc_mem_cget(c, n, s)                        \
    142  1.12  christos 	isc__mem_get((c), ISC_CHECKED_MUL((n), (s)), \
    143  1.12  christos 		     ISC__MEM_ZERO _ISC_MEM_FILELINE)
    144  1.10  christos #define isc_mem_reget(c, p, o, n) \
    145  1.12  christos 	isc__mem_reget((c), (p), (o), (n), 0 _ISC_MEM_FILELINE)
    146  1.12  christos #define isc_mem_creget(c, p, o, n, s)                       \
    147  1.12  christos 	isc__mem_reget((c), (p), ISC_CHECKED_MUL((o), (s)), \
    148  1.12  christos 		       ISC_CHECKED_MUL((n), (s)),           \
    149  1.12  christos 		       ISC__MEM_ZERO _ISC_MEM_FILELINE)
    150  1.12  christos #define isc_mem_allocate(c, s) isc__mem_allocate((c), (s), 0 _ISC_MEM_FILELINE)
    151  1.12  christos #define isc_mem_callocate(c, n, s)                        \
    152  1.12  christos 	isc__mem_allocate((c), ISC_CHECKED_MUL((n), (s)), \
    153  1.12  christos 			  ISC__MEM_ZERO _ISC_MEM_FILELINE)
    154   1.4  christos #define isc_mem_reallocate(c, p, s) \
    155  1.12  christos 	isc__mem_reallocate((c), (p), (s), 0 _ISC_MEM_FILELINE)
    156  1.12  christos #define isc_mem_strdup(c, p) isc__mem_strdup((c), (p)_ISC_MEM_FILELINE)
    157  1.10  christos #define isc_mem_strndup(c, p, l) \
    158  1.12  christos 	isc__mem_strndup((c), (p), (l)_ISC_MEM_FILELINE)
    159  1.12  christos #define isc_mempool_get(c) isc__mempool_get((c)_ISC_MEM_FILELINE)
    160   1.4  christos 
    161  1.12  christos #define isc_mem_put(c, p, s)                                      \
    162  1.12  christos 	do {                                                      \
    163  1.12  christos 		isc__mem_put((c), (p), (s), 0 _ISC_MEM_FILELINE); \
    164  1.12  christos 		(p) = NULL;                                       \
    165  1.10  christos 	} while (0)
    166  1.12  christos #define isc_mem_cput(c, p, n, s)                                  \
    167  1.12  christos 	do {                                                      \
    168  1.12  christos 		isc__mem_put((c), (p), ISC_CHECKED_MUL((n), (s)), \
    169  1.12  christos 			     ISC__MEM_ZERO _ISC_MEM_FILELINE);    \
    170  1.12  christos 		(p) = NULL;                                       \
    171   1.6    rillig 	} while (0)
    172  1.12  christos #define isc_mem_putanddetach(c, p, s)                                      \
    173  1.12  christos 	do {                                                               \
    174  1.12  christos 		isc__mem_putanddetach((c), (p), (s), 0 _ISC_MEM_FILELINE); \
    175  1.12  christos 		(p) = NULL;                                                \
    176  1.10  christos 	} while (0)
    177  1.12  christos #define isc_mem_free(c, p)                                    \
    178  1.12  christos 	do {                                                  \
    179  1.12  christos 		isc__mem_free((c), (p), 0 _ISC_MEM_FILELINE); \
    180  1.12  christos 		(p) = NULL;                                   \
    181   1.6    rillig 	} while (0)
    182  1.12  christos #define isc_mempool_put(c, p)                                \
    183   1.4  christos 	do {                                                 \
    184  1.12  christos 		isc__mempool_put((c), (p)_ISC_MEM_FILELINE); \
    185   1.4  christos 		(p) = NULL;                                  \
    186   1.6    rillig 	} while (0)
    187   1.1  christos 
    188   1.1  christos /*@{*/
    189  1.11  christos /*
    190  1.11  christos  * This is a little hack to help with dynamic link order,
    191  1.11  christos  * see https://github.com/jemalloc/jemalloc/issues/2566
    192  1.11  christos  * for more information.
    193  1.11  christos  */
    194  1.11  christos #if HAVE_JEMALLOC
    195  1.11  christos 
    196  1.11  christos /*
    197  1.11  christos  * cmocka.h has confliction definitions with the jemalloc header but we only
    198  1.11  christos  * need the mallocx symbol from jemalloc.
    199  1.11  christos  */
    200  1.11  christos void *
    201  1.11  christos mallocx(size_t size, int flags);
    202  1.11  christos 
    203  1.11  christos extern volatile void *isc__mem_malloc;
    204  1.11  christos 
    205  1.11  christos #define isc_mem_create(cp)                                            \
    206  1.11  christos 	{                                                             \
    207  1.12  christos 		isc__mem_create((cp)_ISC_MEM_FILELINE);               \
    208  1.11  christos 		isc__mem_malloc = mallocx;                            \
    209  1.11  christos 		ISC_INSIST(CMM_ACCESS_ONCE(isc__mem_malloc) != NULL); \
    210  1.11  christos 	}
    211  1.11  christos #else
    212  1.12  christos #define isc_mem_create(cp) isc__mem_create((cp)_ISC_MEM_FILELINE)
    213  1.11  christos #endif
    214  1.12  christos void
    215  1.12  christos isc__mem_create(isc_mem_t **_ISC_MEM_FLARG);
    216   1.1  christos 
    217   1.1  christos /*!<
    218   1.1  christos  * \brief Create a memory context.
    219   1.1  christos  *
    220   1.1  christos  * Requires:
    221   1.1  christos  * mctxp != NULL && *mctxp == NULL */
    222   1.1  christos /*@}*/
    223   1.1  christos 
    224  1.10  christos #define isc_mem_create_arena(cp) isc__mem_create_arena((cp)_ISC_MEM_FILELINE)
    225  1.10  christos void
    226  1.10  christos isc__mem_create_arena(isc_mem_t **_ISC_MEM_FLARG);
    227  1.10  christos /*!<
    228  1.10  christos  * \brief Create a memory context that routs all its operations to a
    229  1.10  christos  * dedicated jemalloc arena (when available). When jemalloc is not
    230  1.10  christos  * available, the function is, effectively, an alias to
    231  1.10  christos  * isc_mem_create().
    232  1.10  christos  *
    233  1.10  christos  * Requires:
    234  1.10  christos  * mctxp != NULL && *mctxp == NULL */
    235  1.10  christos /*@}*/
    236  1.10  christos 
    237  1.10  christos isc_result_t
    238  1.10  christos isc_mem_arena_set_muzzy_decay_ms(isc_mem_t *mctx, const ssize_t decay_ms);
    239  1.10  christos 
    240  1.10  christos isc_result_t
    241  1.10  christos isc_mem_arena_set_dirty_decay_ms(isc_mem_t *mctx, const ssize_t decay_ms);
    242  1.10  christos /*!<
    243  1.10  christos  * \brief These two functions set the given parameters on the
    244  1.10  christos  * jemalloc arena associated with the memory context (if there is
    245  1.10  christos  * one). When jemalloc is not available, these are no-op.
    246  1.10  christos  *
    247  1.10  christos  * NOTE: The "muzzy_decay_ms" and "dirty_decay_ms" are the most common
    248  1.10  christos  * parameters to adjust when the defaults do not work well (per the
    249  1.10  christos  * official jemalloc tuning guide:
    250  1.10  christos  * https://github.com/jemalloc/jemalloc/blob/dev/TUNING.md).
    251  1.10  christos  *
    252  1.10  christos  * Requires:
    253  1.10  christos  * mctx - a valid memory context.
    254  1.10  christos  */
    255  1.10  christos /*@}*/
    256  1.10  christos 
    257  1.10  christos void
    258  1.10  christos isc_mem_attach(isc_mem_t *, isc_mem_t **);
    259  1.10  christos 
    260   1.1  christos /*@{*/
    261   1.1  christos void
    262   1.1  christos isc_mem_attach(isc_mem_t *, isc_mem_t **);
    263  1.12  christos #define isc_mem_detach(cp) isc__mem_detach((cp)_ISC_MEM_FILELINE)
    264  1.12  christos void
    265  1.12  christos isc__mem_detach(isc_mem_t **_ISC_MEM_FLARG);
    266   1.1  christos /*!<
    267   1.1  christos  * \brief Attach to / detach from a memory context.
    268   1.1  christos  *
    269   1.1  christos  * This is intended for applications that use multiple memory contexts
    270   1.1  christos  * in such a way that it is not obvious when the last allocations from
    271   1.1  christos  * a given context has been freed and destroying the context is safe.
    272   1.1  christos  *
    273   1.1  christos  * Most applications do not need to call these functions as they can
    274   1.1  christos  * simply create a single memory context at the beginning of main()
    275   1.1  christos  * and destroy it at the end of main(), thereby guaranteeing that it
    276   1.1  christos  * is not destroyed while there are outstanding allocations.
    277   1.1  christos  */
    278   1.1  christos /*@}*/
    279   1.1  christos 
    280  1.12  christos #define isc_mem_destroy(cp) isc__mem_destroy((cp)_ISC_MEM_FILELINE)
    281  1.12  christos void
    282  1.12  christos isc__mem_destroy(isc_mem_t **_ISC_MEM_FLARG);
    283   1.1  christos /*%<
    284   1.1  christos  * Destroy a memory context.
    285   1.1  christos  */
    286   1.1  christos 
    287   1.1  christos void
    288   1.1  christos isc_mem_stats(isc_mem_t *mctx, FILE *out);
    289   1.1  christos /*%<
    290   1.1  christos  * Print memory usage statistics for 'mctx' on the stream 'out'.
    291   1.1  christos  */
    292   1.1  christos 
    293   1.1  christos void
    294   1.4  christos isc_mem_setdestroycheck(isc_mem_t *mctx, bool on);
    295   1.1  christos /*%<
    296   1.3  christos  * If 'on' is true, 'mctx' will check for memory leaks when
    297   1.1  christos  * destroyed and abort the program if any are present.
    298   1.1  christos  */
    299   1.1  christos 
    300   1.1  christos size_t
    301   1.1  christos isc_mem_inuse(isc_mem_t *mctx);
    302   1.1  christos /*%<
    303   1.1  christos  * Get an estimate of the amount of memory in use in 'mctx', in bytes.
    304   1.1  christos  * This includes quantization overhead, but does not include memory
    305   1.1  christos  * allocated from the system but not yet used.
    306   1.1  christos  */
    307   1.1  christos 
    308   1.3  christos bool
    309   1.1  christos isc_mem_isovermem(isc_mem_t *mctx);
    310   1.1  christos /*%<
    311   1.1  christos  * Return true iff the memory context is in "over memory" state, i.e.,
    312   1.1  christos  * a hiwater mark has been set and the used amount of memory has exceeds
    313   1.1  christos  * the mark.
    314   1.1  christos  */
    315   1.1  christos 
    316   1.1  christos void
    317  1.10  christos isc_mem_clearwater(isc_mem_t *mctx);
    318  1.10  christos void
    319  1.12  christos isc_mem_setwater(isc_mem_t *mctx, size_t hiwater, size_t lowater);
    320   1.1  christos /*%<
    321   1.1  christos  * Set high and low water marks for this memory context.
    322   1.1  christos  *
    323  1.12  christos  * When the memory usage of 'mctx' exceeds 'hiwater', the overmem condition
    324  1.12  christos  * will be met and isc_mem_isovermem() will return true.
    325  1.12  christos  *
    326  1.12  christos  * If the 'hiwater' and 'lowater' is set to 0, the high- and low-water
    327  1.12  christos  * processing are disabled for this memory context.
    328  1.12  christos  *
    329  1.12  christos  * There's a convenient function isc_mem_clearwater().
    330   1.1  christos  *
    331   1.1  christos  * Requires:
    332  1.10  christos  *\li	'hiwater' >= 'lowater'
    333   1.1  christos  */
    334   1.1  christos 
    335   1.1  christos void
    336   1.1  christos isc_mem_checkdestroyed(FILE *file);
    337   1.1  christos /*%<
    338   1.1  christos  * Check that all memory contexts have been destroyed.
    339   1.1  christos  * Prints out those that have not been.
    340   1.1  christos  * Fatally fails if there are still active contexts.
    341   1.1  christos  */
    342   1.1  christos 
    343   1.1  christos unsigned int
    344   1.1  christos isc_mem_references(isc_mem_t *ctx);
    345   1.1  christos /*%<
    346   1.1  christos  * Return the current reference count.
    347   1.1  christos  */
    348   1.1  christos 
    349   1.1  christos void
    350  1.10  christos isc_mem_setname(isc_mem_t *ctx, const char *name);
    351   1.1  christos /*%<
    352   1.1  christos  * Name 'ctx'.
    353   1.1  christos  *
    354   1.1  christos  * Notes:
    355   1.1  christos  *
    356   1.1  christos  *\li	Only the first 15 characters of 'name' will be copied.
    357   1.1  christos  *
    358   1.1  christos  * Requires:
    359   1.1  christos  *
    360   1.1  christos  *\li	'ctx' is a valid ctx.
    361   1.1  christos  */
    362   1.1  christos 
    363   1.1  christos const char *
    364   1.1  christos isc_mem_getname(isc_mem_t *ctx);
    365   1.1  christos /*%<
    366   1.1  christos  * Get the name of 'ctx', as previously set using isc_mem_setname().
    367   1.1  christos  *
    368   1.1  christos  * Requires:
    369   1.1  christos  *\li	'ctx' is a valid ctx.
    370   1.1  christos  *
    371   1.1  christos  * Returns:
    372   1.1  christos  *\li	A non-NULL pointer to a null-terminated string.
    373   1.1  christos  * 	If the ctx has not been named, the string is
    374   1.1  christos  * 	empty.
    375   1.1  christos  */
    376   1.1  christos 
    377   1.1  christos #ifdef HAVE_LIBXML2
    378   1.1  christos int
    379   1.4  christos isc_mem_renderxml(void *writer0);
    380   1.1  christos /*%<
    381   1.1  christos  * Render all contexts' statistics and status in XML for writer.
    382   1.1  christos  */
    383   1.1  christos #endif /* HAVE_LIBXML2 */
    384   1.1  christos 
    385   1.4  christos #ifdef HAVE_JSON_C
    386   1.1  christos isc_result_t
    387   1.4  christos isc_mem_renderjson(void *memobj0);
    388   1.1  christos /*%<
    389   1.1  christos  * Render all contexts' statistics and status in JSON.
    390   1.1  christos  */
    391   1.4  christos #endif /* HAVE_JSON_C */
    392   1.1  christos 
    393   1.1  christos /*
    394   1.1  christos  * Memory pools
    395   1.1  christos  */
    396   1.1  christos 
    397  1.10  christos #define isc_mempool_create(c, s, mp) \
    398  1.10  christos 	isc__mempool_create((c), (s), (mp)_ISC_MEM_FILELINE)
    399   1.4  christos void
    400  1.10  christos isc__mempool_create(isc_mem_t *restrict mctx, const size_t element_size,
    401  1.10  christos 		    isc_mempool_t **mpctxp _ISC_MEM_FLARG);
    402   1.1  christos /*%<
    403   1.1  christos  * Create a memory pool.
    404   1.1  christos  *
    405   1.1  christos  * Requires:
    406   1.1  christos  *\li	mctx is a valid memory context.
    407   1.1  christos  *\li	size > 0
    408   1.1  christos  *\li	mpctxp != NULL and *mpctxp == NULL
    409   1.1  christos  *
    410   1.1  christos  * Defaults:
    411   1.1  christos  *\li	freemax = 1
    412   1.1  christos  *\li	fillcount = 1
    413   1.1  christos  *
    414   1.1  christos  * Returns:
    415   1.1  christos  *\li	#ISC_R_NOMEMORY		-- not enough memory to create pool
    416   1.1  christos  *\li	#ISC_R_SUCCESS		-- all is well.
    417   1.1  christos  */
    418   1.1  christos 
    419  1.10  christos #define isc_mempool_destroy(mp) isc__mempool_destroy((mp)_ISC_MEM_FILELINE)
    420   1.1  christos void
    421  1.10  christos isc__mempool_destroy(isc_mempool_t **restrict mpctxp _ISC_MEM_FLARG);
    422   1.1  christos /*%<
    423   1.1  christos  * Destroy a memory pool.
    424   1.1  christos  *
    425   1.1  christos  * Requires:
    426   1.1  christos  *\li	mpctxp != NULL && *mpctxp is a valid pool.
    427   1.1  christos  *\li	The pool has no un"put" allocations outstanding
    428   1.1  christos  */
    429   1.1  christos 
    430   1.1  christos void
    431  1.10  christos isc_mempool_setname(isc_mempool_t *restrict mpctx, const char *name);
    432   1.1  christos /*%<
    433  1.10  christos  * Associate a name with a memory pool.  At most 15 characters may be
    434  1.10  christos  *used.
    435   1.1  christos  *
    436   1.1  christos  * Requires:
    437   1.1  christos  *\li	mpctx is a valid pool.
    438   1.1  christos  *\li	name != NULL;
    439   1.1  christos  */
    440   1.1  christos 
    441   1.1  christos /*
    442   1.1  christos  * The following functions get/set various parameters.  Note that due to
    443  1.10  christos  * the unlocked nature of pools these are potentially random values
    444  1.10  christos  *unless the imposed externally provided locking protocols are followed.
    445   1.1  christos  *
    446  1.10  christos  * Also note that the quota limits will not always take immediate
    447  1.10  christos  * effect.
    448   1.1  christos  *
    449   1.1  christos  * All functions require (in addition to other requirements):
    450   1.1  christos  *	mpctx is a valid memory pool
    451   1.1  christos  */
    452   1.1  christos 
    453   1.1  christos unsigned int
    454  1.10  christos isc_mempool_getfreemax(isc_mempool_t *restrict mpctx);
    455   1.1  christos /*%<
    456   1.1  christos  * Returns the maximum allowed size of the free list.
    457   1.1  christos  */
    458   1.1  christos 
    459   1.1  christos void
    460  1.10  christos isc_mempool_setfreemax(isc_mempool_t *restrict mpctx, const unsigned int limit);
    461   1.1  christos /*%<
    462   1.1  christos  * Sets the maximum allowed size of the free list.
    463   1.1  christos  */
    464   1.1  christos 
    465   1.1  christos unsigned int
    466  1.10  christos isc_mempool_getfreecount(isc_mempool_t *restrict mpctx);
    467   1.1  christos /*%<
    468   1.1  christos  * Returns current size of the free list.
    469   1.1  christos  */
    470   1.1  christos 
    471   1.1  christos unsigned int
    472  1.10  christos isc_mempool_getallocated(isc_mempool_t *restrict mpctx);
    473   1.1  christos /*%<
    474   1.1  christos  * Returns the number of items allocated from this pool.
    475   1.1  christos  */
    476   1.1  christos 
    477   1.1  christos unsigned int
    478  1.10  christos isc_mempool_getfillcount(isc_mempool_t *restrict mpctx);
    479   1.1  christos /*%<
    480  1.10  christos  * Returns the number of items allocated as a block from the parent
    481  1.10  christos  * memory context when the free list is empty.
    482   1.1  christos  */
    483   1.1  christos 
    484   1.1  christos void
    485  1.10  christos isc_mempool_setfillcount(isc_mempool_t *restrict mpctx,
    486  1.10  christos 			 const unsigned int limit);
    487   1.1  christos /*%<
    488   1.1  christos  * Sets the fillcount.
    489   1.1  christos  *
    490   1.1  christos  * Additional requirements:
    491   1.1  christos  *\li	limit > 0
    492   1.1  christos  */
    493   1.1  christos 
    494   1.8  christos #if defined(UNIT_TESTING) && defined(malloc)
    495   1.8  christos /*
    496   1.8  christos  * cmocka.h redefined malloc as a macro, we #undef it
    497   1.8  christos  * to avoid replacing ISC_ATTR_MALLOC with garbage.
    498   1.8  christos  */
    499   1.8  christos #pragma push_macro("malloc")
    500   1.8  christos #undef malloc
    501   1.8  christos #define POP_MALLOC_MACRO 1
    502   1.8  christos #endif
    503   1.8  christos 
    504   1.1  christos /*
    505   1.1  christos  * Pseudo-private functions for use via macros.  Do not call directly.
    506   1.1  christos  */
    507  1.12  christos void
    508  1.12  christos isc__mem_putanddetach(isc_mem_t **, void *, size_t, int _ISC_MEM_FLARG);
    509  1.12  christos void
    510  1.12  christos isc__mem_put(isc_mem_t *, void *, size_t, int _ISC_MEM_FLARG);
    511  1.12  christos void
    512  1.12  christos isc__mem_free(isc_mem_t *, void *, int _ISC_MEM_FLARG);
    513   1.8  christos 
    514  1.12  christos ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_put, 2)
    515  1.12  christos void *
    516  1.12  christos isc__mem_get(isc_mem_t *, size_t, int _ISC_MEM_FLARG);
    517   1.8  christos 
    518  1.12  christos ISC_ATTR_DEALLOCATOR_IDX(isc__mem_put, 2)
    519  1.12  christos void *
    520  1.12  christos isc__mem_reget(isc_mem_t *, void *, size_t, size_t, int _ISC_MEM_FLARG);
    521  1.12  christos 
    522  1.12  christos ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_free, 2)
    523  1.12  christos void *
    524  1.12  christos isc__mem_allocate(isc_mem_t *, size_t, int _ISC_MEM_FLARG);
    525  1.12  christos 
    526  1.12  christos ISC_ATTR_DEALLOCATOR_IDX(isc__mem_free, 2)
    527  1.12  christos void *
    528  1.12  christos isc__mem_reallocate(isc_mem_t *, void *, size_t, int _ISC_MEM_FLARG);
    529   1.8  christos 
    530   1.8  christos ISC_ATTR_RETURNS_NONNULL
    531  1.12  christos ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_free, 2)
    532  1.12  christos char *
    533  1.12  christos isc__mem_strdup(isc_mem_t *, const char *_ISC_MEM_FLARG);
    534  1.10  christos 
    535  1.10  christos ISC_ATTR_RETURNS_NONNULL
    536  1.12  christos ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mem_free, 2)
    537  1.12  christos char *
    538  1.12  christos isc__mem_strndup(isc_mem_t *, const char *, size_t _ISC_MEM_FLARG);
    539   1.8  christos 
    540  1.12  christos ISC_ATTR_MALLOC_DEALLOCATOR_IDX(isc__mempool_put, 2)
    541  1.12  christos void *
    542  1.12  christos isc__mempool_get(isc_mempool_t *_ISC_MEM_FLARG);
    543   1.8  christos 
    544  1.12  christos void
    545  1.12  christos isc__mempool_put(isc_mempool_t *, void *_ISC_MEM_FLARG);
    546   1.8  christos 
    547   1.8  christos #ifdef POP_MALLOC_MACRO
    548   1.8  christos /*
    549   1.8  christos  * Restore cmocka.h macro for malloc.
    550   1.8  christos  */
    551   1.8  christos #pragma pop_macro("malloc")
    552   1.8  christos #endif
    553   1.1  christos 
    554   1.1  christos ISC_LANG_ENDDECLS
    555