mem.h revision 5388178e8aa12c263286caa394491903cbf9806e
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley/*
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 1997-2001 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
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.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
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.
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley */
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* $Id: mem.h,v 1.87 2010/03/04 05:45:51 marka Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
4cafb28abcaa2a485c7aac1696213435538b92edBob Halley#ifndef ISC_MEM_H
4cafb28abcaa2a485c7aac1696213435538b92edBob Halley#define ISC_MEM_H 1
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file isc/mem.h */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley#include <stdio.h>
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff#include <isc/lang.h>
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff#include <isc/mutex.h>
c4958494a98a59ce25e9fecad76a9ab0e36cc59fDanny Mayer#include <isc/platform.h>
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff#include <isc/types.h>
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley#include <isc/xml.h>
7aacbd685b2107670e4179689abec9cb82d972abBob Halley
7aacbd685b2107670e4179689abec9cb82d972abBob HalleyISC_LANG_BEGINDECLS
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#define ISC_MEM_LOWATER 0
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#define ISC_MEM_HIWATER 1
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewstypedef void (*isc_mem_water_t)(void *, int);
ac70da9a2710aa9f343d2f720eb1bdd4191a79caBob Halley
ac70da9a2710aa9f343d2f720eb1bdd4191a79caBob Halleytypedef void * (*isc_memalloc_t)(void *, size_t);
ac70da9a2710aa9f343d2f720eb1bdd4191a79caBob Halleytypedef void (*isc_memfree_t)(void *, void *);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson/*%
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.
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson */
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson#ifndef ISC_MEM_DEBUG
de88422aec961bcda8b383b4a4327180584ca993David Lawrence#define ISC_MEM_DEBUG 1
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson/*%
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * allocation and freeing by file and line number.
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson */
50a8312c9f682004e21ffb2d58ec2064a244a9c7Andreas Gustafsson#ifndef ISC_MEM_TRACKLINES
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISC_MEM_TRACKLINES 1
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson/*%
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.
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson */
073bd4c4bcc2f2597521578dc6ae0f49a065626cMark Andrews#ifndef ISC_MEM_CHECKOVERRUN
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISC_MEM_CHECKOVERRUN 1
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson/*%
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.
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson */
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson#ifndef ISC_MEM_FILL
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson#define ISC_MEM_FILL 1
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson/*%
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.
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson */
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson#ifndef ISC_MEMPOOL_NAMES
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson#define ISC_MEMPOOL_NAMES 1
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#endif
c4958494a98a59ce25e9fecad76a9ab0e36cc59fDanny Mayer
ab023a65562e62b85a824509d829b6fad87e00b1Rob AusteinLIBISC_EXTERNAL_DATA extern unsigned int isc_mem_debugging;
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff/*@{*/
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISC_MEM_DEBUGTRACE 0x00000001U
950d71eee32cb7c6ef9f659154f77bbf5a2cdce6Mark Andrews#define ISC_MEM_DEBUGRECORD 0x00000002U
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define ISC_MEM_DEBUGUSAGE 0x00000004U
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson#define ISC_MEM_DEBUGSIZE 0x00000008U
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson#define ISC_MEM_DEBUGCTX 0x00000010U
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson#define ISC_MEM_DEBUGALL 0x0000001FU
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson/*!<
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:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * \li #ISC_MEM_DEBUGTRACE
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * Log each allocation and free to isc_lctx.
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson *
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.
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson *
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 *
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 *
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().
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff */
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff/*@}*/
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff
54ef500660da1f4e8d4e14e737fa81c4c881ac0aBrian Wellington#if ISC_MEM_TRACKLINES
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define _ISC_MEM_FILELINE , __FILE__, __LINE__
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define _ISC_MEM_FLARG , const char *, unsigned int
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#else
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define _ISC_MEM_FILELINE
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define _ISC_MEM_FLARG
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews/*!
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 */
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#ifndef ISC_MEM_USE_INTERNAL_MALLOC
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define ISC_MEM_USE_INTERNAL_MALLOC 1
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#endif
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * Flags for isc_mem_create2()calls.
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews */
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define ISC_MEMFLAG_NOLOCK 0x00000001 /* no lock is necessary */
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define ISC_MEMFLAG_INTERNAL 0x00000002 /* use internal malloc */
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#if ISC_MEM_USE_INTERNAL_MALLOC
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define ISC_MEMFLAG_DEFAULT ISC_MEMFLAG_INTERNAL
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#else
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews#define ISC_MEMFLAG_DEFAULT 0
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson#endif
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff/*%<
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 * version.
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff */
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#ifdef WIN32
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISCMEMFUNC(sfx) isc___mem_ ## sfx
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISCMEMPOOLFUNC(sfx) isc___mempool_ ## sfx
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#else
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISCMEMFUNC(sfx) isc__mem_ ## sfx
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define ISCMEMPOOLFUNC(sfx) isc__mempool_ ## sfx
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#endif
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff
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)
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff/*%
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley * isc_mem_putanddetach() is a convenience function for use where you
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * have a structure with an attached memory context.
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer *
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * Given:
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson *
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * \code
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * struct {
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * ...
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * isc_mem_t *mctx;
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * ...
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * } *ptr;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * isc_mem_t *mctx;
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson *
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr));
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * \endcode
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson *
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * is the equivalent of:
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson *
e0806ab5eda9add79044ccf17e55c49bae35d2e2Mark Andrews * \code
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 * \endcode
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson */
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson/*% memory and memory pool methods */
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafssontypedef struct isc_memmethods {
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*attach)(isc_mem_t *source, isc_mem_t **targetp);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*detach)(isc_mem_t **mctxp);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*destroy)(isc_mem_t **mctxp);
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,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein size_t size _ISC_MEM_FLARG);
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 size_t size _ISC_MEM_FLARG);
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 size_t (*inuse)(isc_mem_t *mctx);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson isc_result_t (*mpcreate)(isc_mem_t *mctx, size_t size,
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson isc_mempool_t **mpctxp);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson} isc_memmethods_t;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafssontypedef struct isc_mempoolmethods {
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson void (*destroy)(isc_mempool_t **mpctxp);
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);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer} isc_mempoolmethods_t;
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson/*%
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 * \brief
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.
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstruct isc_mem {
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson unsigned int impmagic;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson unsigned int magic;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson isc_memmethods_t *methods;
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer};
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson#define ISCAPI_MCTX_MAGIC ISC_MAGIC('A','m','c','x')
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define ISCAPI_MCTX_VALID(m) ((m) != NULL && \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein (m)->magic == ISCAPI_MCTX_MAGIC)
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson/*%
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 */
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerstruct isc_mempool {
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer unsigned int impmagic;
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer unsigned int magic;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mempoolmethods_t *methods;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson};
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson#define ISCAPI_MPOOL_MAGIC ISC_MAGIC('A','m','p','l')
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson#define ISCAPI_MPOOL_VALID(mp) ((mp) != NULL && \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein (mp)->magic == ISCAPI_MPOOL_MAGIC)
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer#if ISC_MEM_DEBUG
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer#define isc_mem_put(c, p, s) \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein do { \
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE); \
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson (p) = NULL; \
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson } while (0)
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson#define isc_mem_putanddetach(c, p, s) \
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson do { \
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ISCMEMFUNC(putanddetach)((c), (p), (s) _ISC_MEM_FILELINE); \
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (p) = NULL; \
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews } while (0)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define isc_mem_free(c, p) \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein do { \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ISCMEMFUNC(free)((c), (p) _ISC_MEM_FILELINE); \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein (p) = NULL; \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein } while (0)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#define isc_mempool_put(c, p) \
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein do { \
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ISCMEMPOOLFUNC(put)((c), (p) _ISC_MEM_FILELINE); \
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence (p) = NULL; \
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence } while (0)
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence#else
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#define isc_mem_put(c, p, s) ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE)
50a3152b7088546418c8edc0c89eeaf48560b035Andreas Gustafsson#define isc_mem_putanddetach(c, p, s) \
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)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#endif
eb7ef395d27b1104f684e21836f200c052736d07Michael Graff
eb7ef395d27b1104f684e21836f200c052736d07Michael Graff/*@{*/
eb7ef395d27b1104f684e21836f200c052736d07Michael Graffisc_result_t
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffisc_mem_create(size_t max_size, size_t target_size,
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff isc_mem_t **mctxp);
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_result_t
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffisc_mem_create2(size_t max_size, size_t target_size,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff isc_mem_t **mctxp, unsigned int flags);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_result_t
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_mem_createx(size_t max_size, size_t target_size,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_memalloc_t memalloc, isc_memfree_t memfree,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff void *arg, isc_mem_t **mctxp);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_result_t
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_mem_createx2(size_t max_size, size_t target_size,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_memalloc_t memalloc, isc_memfree_t memfree,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff void *arg, isc_mem_t **mctxp, unsigned int flags);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*!<
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \brief Create a memory context.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff *
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 *
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.
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff *
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 *
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.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * Requires:
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * mctxp != NULL && *mctxp == NULL */
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff/*@}*/
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff/*@{*/
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graffvoid
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graffisc_mem_attach(isc_mem_t *, isc_mem_t **);
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graffvoid
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graffisc_mem_detach(isc_mem_t **);
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff/*!<
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * \brief Attach to / detach from a memory context.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff *
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.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff *
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.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff/*@}*/
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffvoid
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffisc_mem_destroy(isc_mem_t **);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff/*%<
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Destroy a memory context.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff */
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffisc_result_t
0fd03d2a66102f8d9bd7acea4e7db73ecaa0cb52Michael Graffisc_mem_ondestroy(isc_mem_t *ctx,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff isc_task_t *task,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff isc_event_t **event);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff/*%<
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Request to be notified with an event when a memory context has
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * been successfully destroyed.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff */
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrencevoid
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mem_stats(isc_mem_t *mctx, FILE *out);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%<
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Print memory usage statistics for 'mctx' on the stream 'out'.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff */
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrencevoid
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mem_setdestroycheck(isc_mem_t *mctx,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_boolean_t on);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff/*%<
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.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence */
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*@{*/
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffvoid
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffisc_mem_setquota(isc_mem_t *, size_t);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffsize_t
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mem_getquota(isc_mem_t *);
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence/*%<
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 */
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence/*@}*/
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinsize_t
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graffisc_mem_inuse(isc_mem_t *mctx);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff/*%<
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.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff */
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrencevoid
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg,
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff size_t hiwater, size_t lowater);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff/*%<
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Set high and low water marks for this memory context.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrence *
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 *
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.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * static void
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * water(void *arg, int mark) {
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * struct foo *foo = arg;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * LOCK(&foo->marklock);
eb7ef395d27b1104f684e21836f200c052736d07Michael Graff * if (foo->mark != mark) {
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * foo->mark = mark;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * ....
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * isc_mem_waterack(foo->mctx, mark);
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * }
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * UNLOCK(&foo->marklock);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * }
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer *
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 *
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * Requires:
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer *
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * 'water' is not NULL.
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * hi_water >= lo_water
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer */
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayervoid
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc_mem_waterack(isc_mem_t *ctx, int mark);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer/*%<
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer * Called to acknowledge changes in signaled by calls to 'water'.
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayer */
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson
7aacbd685b2107670e4179689abec9cb82d972abBob Halleyvoid
7aacbd685b2107670e4179689abec9cb82d972abBob Halleyisc_mem_printactive(isc_mem_t *mctx, FILE *file);
54a2e7e8a21ee765f41bd995101995613bff9e8cDavid Lawrence/*%<
* Print to 'file' all active memory in 'mctx'.
*
* Requires ISC_MEM_DEBUGRECORD to have been set.
*/
void
isc_mem_printallactive(FILE *file);
/*%<
* Print to 'file' all active memory in all contexts.
*
* Requires ISC_MEM_DEBUGRECORD to have been set.
*/
void
isc_mem_checkdestroyed(FILE *file);
/*%<
* Check that all memory contexts have been destroyed.
* Prints out those that have not been.
* Fatally fails if there are still active contexts.
*/
unsigned int
isc_mem_references(isc_mem_t *ctx);
/*%<
* Return the current reference count.
*/
void
isc_mem_setname(isc_mem_t *ctx, const char *name, void *tag);
/*%<
* Name 'ctx'.
*
* Notes:
*
*\li Only the first 15 characters of 'name' will be copied.
*
*\li 'tag' is for debugging purposes only.
*
* Requires:
*
*\li 'ctx' is a valid ctx.
*/
const char *
isc_mem_getname(isc_mem_t *ctx);
/*%<
* Get the name of 'ctx', as previously set using isc_mem_setname().
*
* Requires:
*\li 'ctx' is a valid ctx.
*
* Returns:
*\li A non-NULL pointer to a null-terminated string.
* If the ctx has not been named, the string is
* empty.
*/
void *
isc_mem_gettag(isc_mem_t *ctx);
/*%<
* Get the tag value for 'task', as previously set using isc_mem_setname().
*
* Requires:
*\li 'ctx' is a valid ctx.
*
* Notes:
*\li This function is for debugging purposes only.
*
* Requires:
*\li 'ctx' is a valid task.
*/
#ifdef HAVE_LIBXML2
void
isc_mem_renderxml(xmlTextWriterPtr writer);
/*%<
* Render all contexts' statistics and status in XML for writer.
*/
#endif /* HAVE_LIBXML2 */
/*
* Memory pools
*/
isc_result_t
isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
/*%<
* Create a memory pool.
*
* Requires:
*\li mctx is a valid memory context.
*\li size > 0
*\li mpctxp != NULL and *mpctxp == NULL
*
* Defaults:
*\li maxalloc = UINT_MAX
*\li freemax = 1
*\li fillcount = 1
*
* Returns:
*\li #ISC_R_NOMEMORY -- not enough memory to create pool
*\li #ISC_R_SUCCESS -- all is well.
*/
void
isc_mempool_destroy(isc_mempool_t **mpctxp);
/*%<
* Destroy a memory pool.
*
* Requires:
*\li mpctxp != NULL && *mpctxp is a valid pool.
*\li The pool has no un"put" allocations outstanding
*/
void
isc_mempool_setname(isc_mempool_t *mpctx, const char *name);
/*%<
* Associate a name with a memory pool. At most 15 characters may be used.
*
* Requires:
*\li mpctx is a valid pool.
*\li name != NULL;
*/
void
isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
/*%<
* Associate a lock with this memory pool.
*
* This lock is used when getting or putting items using this memory pool,
* and it is also used to set or get internal state via the isc_mempool_get*()
* and isc_mempool_set*() set of functions.
*
* Multiple pools can each share a single lock. For instance, if "manager"
* type object contained pools for various sizes of events, and each of
* these pools used a common lock. Note that this lock must NEVER be used
* by other than mempool routines once it is given to a pool, since that can
* easily cause double locking.
*
* Requires:
*
*\li mpctpx is a valid pool.
*
*\li lock != NULL.
*
*\li No previous lock is assigned to this pool.
*
*\li The lock is initialized before calling this function via the normal
* means of doing that.
*/
/*
* The following functions get/set various parameters. Note that due to
* the unlocked nature of pools these are potentially random values unless
* the imposed externally provided locking protocols are followed.
*
* Also note that the quota limits will not always take immediate effect.
* For instance, setting "maxalloc" to a number smaller than the currently
* allocated count is permitted. New allocations will be refused until
* the count drops below this threshold.
*
* All functions require (in addition to other requirements):
* mpctx is a valid memory pool
*/
unsigned int
isc_mempool_getfreemax(isc_mempool_t *mpctx);
/*%<
* Returns the maximum allowed size of the free list.
*/
void
isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
/*%<
* Sets the maximum allowed size of the free list.
*/
unsigned int
isc_mempool_getfreecount(isc_mempool_t *mpctx);
/*%<
* Returns current size of the free list.
*/
unsigned int
isc_mempool_getmaxalloc(isc_mempool_t *mpctx);
/*!<
* Returns the maximum allowed number of allocations.
*/
void
isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
/*%<
* Sets the maximum allowed number of allocations.
*
* Additional requirements:
*\li limit > 0
*/
unsigned int
isc_mempool_getallocated(isc_mempool_t *mpctx);
/*%<
* Returns the number of items allocated from this pool.
*/
unsigned int
isc_mempool_getfillcount(isc_mempool_t *mpctx);
/*%<
* Returns the number of items allocated as a block from the parent memory
* context when the free list is empty.
*/
void
isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
/*%<
* Sets the fillcount.
*
* Additional requirements:
*\li limit > 0
*/
/*
* Pseudo-private functions for use via macros. Do not call directly.
*/
void *
ISCMEMFUNC(get)(isc_mem_t *, size_t _ISC_MEM_FLARG);
void
ISCMEMFUNC(putanddetach)(isc_mem_t **, void *, size_t _ISC_MEM_FLARG);
void
ISCMEMFUNC(put)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
void *
ISCMEMFUNC(allocate)(isc_mem_t *, size_t _ISC_MEM_FLARG);
void *
ISCMEMFUNC(reallocate)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
void
ISCMEMFUNC(free)(isc_mem_t *, void * _ISC_MEM_FLARG);
char *
ISCMEMFUNC(strdup)(isc_mem_t *, const char *_ISC_MEM_FLARG);
void *
ISCMEMPOOLFUNC(get)(isc_mempool_t * _ISC_MEM_FLARG);
void
ISCMEMPOOLFUNC(put)(isc_mempool_t *, void * _ISC_MEM_FLARG);
#ifdef USE_MEMIMPREGISTER
/*%<
* See isc_mem_create2() above.
*/
typedef isc_result_t
(*isc_memcreatefunc_t)(size_t init_max_size, size_t target_size,
isc_mem_t **ctxp, unsigned int flags);
isc_result_t
isc_mem_register(isc_memcreatefunc_t createfunc);
/*%<
* Register a new memory management implementation and add it to the list of
* supported implementations. This function must be called when a different
* memory management library is used than the one contained in the ISC library.
*/
isc_result_t
isc__mem_register(void);
/*%<
* A short cut function that specifies the memory management module in the ISC
* library for isc_mem_register(). An application that uses the ISC library
* usually do not have to care about this function: it would call
* isc_lib_register(), which internally calls this function.
*/
#endif /* USE_MEMIMPREGISTER */
ISC_LANG_ENDDECLS
#endif /* ISC_MEM_H */