mem.h revision 5388178e8aa12c263286caa394491903cbf9806e
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 1997-2001 Internet Software Consortium.
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley * Permission to use, copy, modify, and/or distribute this software for any
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley * purpose with or without fee is hereby granted, provided that the above
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley * copyright notice and this permission notice appear in all copies.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* $Id: mem.h,v 1.87 2010/03/04 05:45:51 marka Exp $ */
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewstypedef void (*isc_mem_water_t)(void *, int);
ac70da9a2710aa9f343d2f720eb1bdd4191a79caBob Halleytypedef void (*isc_memfree_t)(void *, void *);
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * Define ISC_MEM_DEBUG=1 to make all functions that free memory
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * set the pointer being freed to NULL after being freed.
de88422aec961bcda8b383b4a4327180584ca993David Lawrence * This is the default; set ISC_MEM_DEBUG=0 to disable it.
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * allocation and freeing by file and line number.
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * Define ISC_MEM_CHECKOVERRUN=1 to turn on checks for using memory outside
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * the requested space. This will increase the size of each allocation.
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * Define ISC_MEM_FILL=1 to fill each block of memory returned to the system
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * with the byte string '0xbe'. This helps track down uninitialized pointers
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * and the like. On freeing memory, the space is filled with '0xde' for
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * the same reasons.
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * Define ISC_MEMPOOL_NAMES=1 to make memory pools store a symbolic
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * name so that the leaking pool can be more readily identified in
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * case of a memory leak.
ab023a65562e62b85a824509d829b6fad87e00b1Rob AusteinLIBISC_EXTERNAL_DATA extern unsigned int isc_mem_debugging;
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * The variable isc_mem_debugging holds a set of flags for
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * turning certain memory debugging options on or off at
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * runtime. It is initialized to the value ISC_MEM_DEGBUGGING,
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * which is 0 by default but may be overridden at compile time.
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * The following flags can be specified:
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * \li #ISC_MEM_DEBUGTRACE
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * Log each allocation and free to isc_lctx.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \li #ISC_MEM_DEBUGRECORD
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * Remember each allocation, and match them up on free.
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * Crash if a free doesn't match an allocation.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \li #ISC_MEM_DEBUGUSAGE
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * If a hi_water mark is set, print the maximum inuse memory
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson * every time it is raised once it exceeds the hi_water mark.
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * \li #ISC_MEM_DEBUGSIZE
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * Check the size argument being passed to isc_mem_put() matches
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * that passed to isc_mem_get().
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * \li #ISC_MEM_DEBUGCTX
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * Check the mctx argument being passed to isc_mem_put() matches
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * that passed to isc_mem_get().
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define _ISC_MEM_FLARG , const char *, unsigned int
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * Define ISC_MEM_USE_INTERNAL_MALLOC=1 to use the internal malloc()
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * implementation in preference to the system one. The internal malloc()
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * is very space-efficient, and quite fast on uniprocessor systems. It
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * performs poorly on multiprocessor machines.
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * JT: we can overcome the performance issue on multiprocessor machines
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * by carefully separating memory contexts.
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * Flags for isc_mem_create2()calls.
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define ISC_MEMFLAG_NOLOCK 0x00000001 /* no lock is necessary */
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define ISC_MEMFLAG_INTERNAL 0x00000002 /* use internal malloc */
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * We use either isc___mem (three underscores) or isc__mem (two) depending on
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * whether it's for BIND9's internal purpose (with -DBIND9) or generic export
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews * library. This condition is generally handled in isc/namespace.h, but for
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews * Windows it doesn't work if it involves multiple times of macro expansion
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews * (such as isc_mem to isc__mem then to isc___mem). The following definitions
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews * are used to work around this portability issue. Right now, we don't support
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews * the export library for Windows, so we always use the three-underscore
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISCMEMPOOLFUNC(sfx) isc___mempool_ ## sfx
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISCMEMPOOLFUNC(sfx) isc__mempool_ ## sfx
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley#define isc_mem_get(c, s) ISCMEMFUNC(get)((c), (s) _ISC_MEM_FILELINE)
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mem_allocate(c, s) ISCMEMFUNC(allocate)((c), (s) _ISC_MEM_FILELINE)
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews#define isc_mem_reallocate(c, p, s) ISCMEMFUNC(reallocate)((c), (p), (s) _ISC_MEM_FILELINE)
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews#define isc_mem_strdup(c, p) ISCMEMFUNC(strdup)((c), (p) _ISC_MEM_FILELINE)
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mempool_get(c) ISCMEMPOOLFUNC(get)((c) _ISC_MEM_FILELINE)
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley * isc_mem_putanddetach() is a convenience function for use where you
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * have a structure with an attached memory context.
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * isc_mem_t *mctx;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * isc_mem_t *mctx;
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr));
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * is the equivalent of:
e0806ab5eda9add79044ccf17e55c49bae35d2e2Mark Andrews * mctx = NULL;
e0806ab5eda9add79044ccf17e55c49bae35d2e2Mark Andrews * isc_mem_attach(ptr->mctx, &mctx);
e0806ab5eda9add79044ccf17e55c49bae35d2e2Mark Andrews * isc_mem_detach(&ptr->mctx);
e0806ab5eda9add79044ccf17e55c49bae35d2e2Mark Andrews * isc_mem_put(mctx, ptr, sizeof(*ptr));
e0806ab5eda9add79044ccf17e55c49bae35d2e2Mark Andrews * isc_mem_detach(&mctx);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson/*% memory and memory pool methods */
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*attach)(isc_mem_t *source, isc_mem_t **targetp);
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson void *(*memget)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein void (*memput)(isc_mem_t *mctx, void *ptr, size_t size _ISC_MEM_FLARG);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*memputanddetach)(isc_mem_t **mctxp, void *ptr,
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer void *(*memallocate)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer void *(*memreallocate)(isc_mem_t *mctx, void *ptr,
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer char *(*memstrdup)(isc_mem_t *mctx, const char *s _ISC_MEM_FLARG);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein void (*memfree)(isc_mem_t *mctx, void *ptr _ISC_MEM_FLARG);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein void (*setdestroycheck)(isc_mem_t *mctx, isc_boolean_t flag);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*setwater)(isc_mem_t *ctx, isc_mem_water_t water,
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void *water_arg, size_t hiwater, size_t lowater);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*waterack)(isc_mem_t *ctx, int flag);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson isc_result_t (*mpcreate)(isc_mem_t *mctx, size_t size,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein void *(*get)(isc_mempool_t *mpctx _ISC_MEM_FLARG);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*put)(isc_mempool_t *mpctx, void *mem _ISC_MEM_FLARG);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer unsigned int (*getallocated)(isc_mempool_t *mpctx);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer void (*setmaxalloc)(isc_mempool_t *mpctx, unsigned int limit);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein void (*setfreemax)(isc_mempool_t *mpctx, unsigned int limit);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*setname)(isc_mempool_t *mpctx, const char *name);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*associatelock)(isc_mempool_t *mpctx, isc_mutex_t *lock);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*setfillcount)(isc_mempool_t *mpctx, unsigned int limit);
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * This structure is actually just the common prefix of a memory context
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * implementation's version of an isc_mem_t.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Direct use of this structure by clients is forbidden. mctx implementations
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * may change the structure. 'magic' must be ISCAPI_MCTX_MAGIC for any of the
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * isc_mem_ routines to work. mctx implementations must maintain all mctx
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * invariants.
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson#define ISCAPI_MCTX_MAGIC ISC_MAGIC('A','m','c','x')
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * This is the common prefix of a memory pool context. The same note as
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * that for the mem structure applies.
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer unsigned int impmagic;
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer unsigned int magic;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson#define ISCAPI_MPOOL_MAGIC ISC_MAGIC('A','m','p','l')
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson#define ISCAPI_MPOOL_VALID(mp) ((mp) != NULL && \
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer#define isc_mem_put(c, p, s) \
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE); \
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ISCMEMFUNC(putanddetach)((c), (p), (s) _ISC_MEM_FILELINE); \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define isc_mem_free(c, p) \
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ISCMEMPOOLFUNC(put)((c), (p) _ISC_MEM_FILELINE); \
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#define isc_mem_put(c, p, s) ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE)
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence ISCMEMFUNC(putanddetach)((c), (p), (s) _ISC_MEM_FILELINE)
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence#define isc_mem_free(c, p) ISCMEMFUNC(free)((c), (p) _ISC_MEM_FILELINE)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#define isc_mempool_put(c, p) ISCMEMPOOLFUNC(put)((c), (p) _ISC_MEM_FILELINE)
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffisc_mem_create(size_t max_size, size_t target_size,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffisc_mem_create2(size_t max_size, size_t target_size,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_mem_createx(size_t max_size, size_t target_size,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_mem_createx2(size_t max_size, size_t target_size,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff void *arg, isc_mem_t **mctxp, unsigned int flags);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \brief Create a memory context.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * 'max_size' and 'target_size' are tuning parameters. When
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * ISC_MEMFLAG_INTERNAL is set, allocations smaller than 'max_size'
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * will be satisfied by getting blocks of size 'target_size' from the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * system allocator and breaking them up into pieces; larger allocations
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * will use the system allocator directly. If 'max_size' and/or
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * 'target_size' are zero, default values will be * used. When
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * ISC_MEMFLAG_INTERNAL is not set, 'target_size' is ignored.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * 'max_size' is also used to size the statistics arrays and the array
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * used to record active memory when ISC_MEM_DEBUGRECORD is set. Settin
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * 'max_size' too low can have detrimental effects on performance.
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence * A memory context created using isc_mem_createx() will obtain
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * memory from the system by calling 'memalloc' and 'memfree',
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * passing them the argument 'arg'. A memory context created
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * using isc_mem_create() will use the standard library malloc()
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * and free().
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * If ISC_MEMFLAG_NOLOCK is set in 'flags', the corresponding memory context
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * will be accessed without locking. The user who creates the context must
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * ensure there be no race. Since this can be a source of bug, it is generally
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * inadvisable to use this flag unless the user is very sure about the race
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * condition and the access to the object is highly performance sensitive.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * mctxp != NULL && *mctxp == NULL */
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * \brief Attach to / detach from a memory context.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * This is intended for applications that use multiple memory contexts
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * in such a way that it is not obvious when the last allocations from
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a given context has been freed and destroying the context is safe.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Most applications do not need to call these functions as they can
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * simply create a single memory context at the beginning of main()
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * and destroy it at the end of main(), thereby guaranteeing that it
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * is not destroyed while there are outstanding allocations.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Destroy a memory context.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Request to be notified with an event when a memory context has
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * been successfully destroyed.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Print memory usage statistics for 'mctx' on the stream 'out'.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * If 'on' is ISC_TRUE, 'mctx' will check for memory leaks when
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * destroyed and abort the program if any are present.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Set/get the memory quota of 'mctx'. This is a hard limit
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * on the amount of memory that may be allocated from mctx;
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * if it is exceeded, allocations will fail.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Get an estimate of the number of memory in use in 'mctx', in bytes.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * This includes quantization overhead, but does not include memory
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * allocated from the system but not yet used.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Set high and low water marks for this memory context.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence * When the memory usage of 'mctx' exceeds 'hiwater',
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * '(water)(water_arg, #ISC_MEM_HIWATER)' will be called. 'water' needs to
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * call isc_mem_waterack() with #ISC_MEM_HIWATER to acknowledge the state
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * change. 'water' may be called multiple times.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * When the usage drops below 'lowater', 'water' will again be called, this
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence * time with #ISC_MEM_LOWATER. 'water' need to calls isc_mem_waterack() with
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence * #ISC_MEM_LOWATER to acknowledge the change.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * static void
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * water(void *arg, int mark) {
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * struct foo *foo = arg;
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * LOCK(&foo->marklock);
eb7ef395d27b1104f684e21836f200c052736d07Michael Graff * if (foo->mark != mark) {
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * foo->mark = mark;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * isc_mem_waterack(foo->mctx, mark);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * UNLOCK(&foo->marklock);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * If 'water' is NULL then 'water_arg', 'hi_water' and 'lo_water' are
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * ignored and the state is reset.
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * 'water' is not NULL.
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * hi_water >= lo_water
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * Called to acknowledge changes in signaled by calls to 'water'.
#ifdef HAVE_LIBXML2
#ifdef USE_MEMIMPREGISTER
typedef isc_result_t
isc__mem_register(void);