0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1997-2001, 2004-2013, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * allocation and freeing by file and line number.
c52e5c8ed1e2a3a90b912d3e4cb5da9dc32d85f7Andreas Gustafsson * 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.
b292230ab8dd33480dabad2b3615dcce5dd70c35Mark Andrews * If we are performing a Coverity static analysis then ISC_MEM_CHECKOVERRUN
b292230ab8dd33480dabad2b3615dcce5dd70c35Mark Andrews * can hide bugs that would otherwise discovered so force to zero.
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * 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.
b292230ab8dd33480dabad2b3615dcce5dd70c35Mark Andrews * If we are performing a Coverity static analysis then ISC_MEM_FILL
b292230ab8dd33480dabad2b3615dcce5dd70c35Mark Andrews * can hide bugs that would otherwise discovered so force to zero.
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
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * case of a memory leak.
c4958494a98a59ce25e9fecad76a9ab0e36cc59fDanny MayerLIBISC_EXTERNAL_DATA extern unsigned int isc_mem_debugging;
5a505fc4c2e99842052d9409790c7da0b5663bceMukund SivaramanLIBISC_EXTERNAL_DATA extern unsigned int isc_mem_defaultflags;
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * The variable isc_mem_debugging holds a set of flags for
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * turning certain memory debugging options on or off at
0ef59d22882fc5d28f4683fdbe88aefd8eaad6f3Francis Dupont * 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 * \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
0ef59d22882fc5d28f4683fdbe88aefd8eaad6f3Francis Dupont * If a hi_water mark is set, print the maximum inuse memory
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson * every time it is raised once it exceeds the hi_water mark.
d4d68515d23668cfe804ab2a174a7e427decedd8Mark Andrews * \li #ISC_MEM_DEBUGSIZE
1425217e5c3a2cbab7f8344e600e0c16047289ffMark Andrews * Check the size argument being passed to isc_mem_put() matches
d4d68515d23668cfe804ab2a174a7e427decedd8Mark Andrews * that passed to isc_mem_get().
d4d68515d23668cfe804ab2a174a7e427decedd8Mark Andrews * \li #ISC_MEM_DEBUGCTX
1425217e5c3a2cbab7f8344e600e0c16047289ffMark Andrews * Check the mctx argument being passed to isc_mem_put() matches
d4d68515d23668cfe804ab2a174a7e427decedd8Mark Andrews * that passed to isc_mem_get().
5388178e8aa12c263286caa394491903cbf9806eMark Andrews#define _ISC_MEM_FLARG , const char *, unsigned int
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * Define ISC_MEM_USE_INTERNAL_MALLOC=1 to use the internal malloc()
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * implementation in preference to the system one. The internal malloc()
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * is very space-efficient, and quite fast on uniprocessor systems. It
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * performs poorly on multiprocessor machines.
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * JT: we can overcome the performance issue on multiprocessor machines
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * by carefully separating memory contexts.
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * Flags for isc_mem_create2()calls.
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#define ISC_MEMFLAG_NOLOCK 0x00000001 /* no lock is necessary */
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews#define ISC_MEMFLAG_INTERNAL 0x00000002 /* use internal malloc */
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews#define ISC_MEMFLAG_DEFAULT ISC_MEMFLAG_INTERNAL
457144a0c69e0aa242386f35498e41dc8bfc1610Tatuya JINMEI 神明達哉 * We use either isc___mem (three underscores) or isc__mem (two) depending on
457144a0c69e0aa242386f35498e41dc8bfc1610Tatuya JINMEI 神明達哉 * whether it's for BIND9's internal purpose (with -DBIND9) or generic export
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan Hunt#define isc_mem_get(c, s) ISCMEMFUNC(get)((c), (s) _ISC_MEM_FILELINE)
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan Hunt#define isc_mem_allocate(c, s) ISCMEMFUNC(allocate)((c), (s) _ISC_MEM_FILELINE)
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan Hunt#define isc_mem_reallocate(c, p, s) ISCMEMFUNC(reallocate)((c), (p), (s) _ISC_MEM_FILELINE)
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan Hunt#define isc_mem_strdup(c, p) ISCMEMFUNC(strdup)((c), (p) _ISC_MEM_FILELINE)
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan Hunt#define isc_mempool_get(c) ISCMEMPOOLFUNC(get)((c) _ISC_MEM_FILELINE)
0ef59d22882fc5d28f4683fdbe88aefd8eaad6f3Francis Dupont * isc_mem_putanddetach() is a convenience function for use where you
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * have a structure with an attached memory context.
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * isc_mem_t *mctx;
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * isc_mem_t *mctx;
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr));
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * is the equivalent of:
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * mctx = NULL;
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * isc_mem_attach(ptr->mctx, &mctx);
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * isc_mem_detach(&ptr->mctx);
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * isc_mem_put(mctx, ptr, sizeof(*ptr));
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * isc_mem_detach(&mctx);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉/*% memory and memory pool methods */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*attach)(isc_mem_t *source, isc_mem_t **targetp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void *(*memget)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*memput)(isc_mem_t *mctx, void *ptr, size_t size _ISC_MEM_FLARG);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*memputanddetach)(isc_mem_t **mctxp, void *ptr,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void *(*memallocate)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG);
e7c38ca9635e73c9a928bbab9c73c2abbd499f8bEvan Hunt void *(*memreallocate)(isc_mem_t *mctx, void *ptr,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 char *(*memstrdup)(isc_mem_t *mctx, const char *s _ISC_MEM_FLARG);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*memfree)(isc_mem_t *mctx, void *ptr _ISC_MEM_FLARG);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*setdestroycheck)(isc_mem_t *mctx, isc_boolean_t flag);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*setwater)(isc_mem_t *ctx, isc_mem_water_t water,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void *water_arg, size_t hiwater, size_t lowater);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*waterack)(isc_mem_t *ctx, int flag);
27fe1966c948ba0c1c9d0d831ea3d8bf32d052acTatuya JINMEI 神明達哉 isc_boolean_t (*isovermem)(isc_mem_t *mctx);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_result_t (*mpcreate)(isc_mem_t *mctx, size_t size,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void *(*get)(isc_mempool_t *mpctx _ISC_MEM_FLARG);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*put)(isc_mempool_t *mpctx, void *mem _ISC_MEM_FLARG);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 unsigned int (*getallocated)(isc_mempool_t *mpctx);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*setmaxalloc)(isc_mempool_t *mpctx, unsigned int limit);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*setfreemax)(isc_mempool_t *mpctx, unsigned int limit);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*setname)(isc_mempool_t *mpctx, const char *name);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*associatelock)(isc_mempool_t *mpctx, isc_mutex_t *lock);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 void (*setfillcount)(isc_mempool_t *mpctx, unsigned int limit);
823ca3c14f1596341bdd50707c01f3b529b75b4aAutomatic Updater * This structure is actually just the common prefix of a memory context
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * implementation's version of an isc_mem_t.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Direct use of this structure by clients is forbidden. mctx implementations
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * may change the structure. 'magic' must be ISCAPI_MCTX_MAGIC for any of the
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * isc_mem_ routines to work. mctx implementations must maintain all mctx
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#define ISCAPI_MCTX_MAGIC ISC_MAGIC('A','m','c','x')
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#define ISCAPI_MCTX_VALID(m) ((m) != NULL && \
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * This is the common prefix of a memory pool context. The same note as
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * that for the mem structure applies.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#define ISCAPI_MPOOL_MAGIC ISC_MAGIC('A','m','p','l')
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#define ISCAPI_MPOOL_VALID(mp) ((mp) != NULL && \
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan Hunt ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE); \
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan Hunt ISCMEMFUNC(putanddetach)((c), (p), (s) _ISC_MEM_FILELINE); \
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan Hunt ISCMEMPOOLFUNC(put)((c), (p) _ISC_MEM_FILELINE); \
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc_mem_create(size_t max_size, size_t target_size,
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉isc_mem_create2(size_t max_size, size_t target_size,
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc_mem_createx(size_t max_size, size_t target_size,
6d858e75d4f411e89485ec702ce1e118e1ead53cAndreas Gustafsson isc_memalloc_t memalloc, isc_memfree_t memfree,
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉isc_mem_createx2(size_t max_size, size_t target_size,
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 isc_memalloc_t memalloc, isc_memfree_t memfree,
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 void *arg, isc_mem_t **mctxp, unsigned int flags);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \brief Create a memory context.
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * 'max_size' and 'target_size' are tuning parameters. When
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * ISC_MEMFLAG_INTERNAL is set, allocations smaller than 'max_size'
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * will be satisfied by getting blocks of size 'target_size' from the
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * system allocator and breaking them up into pieces; larger allocations
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * will use the system allocator directly. If 'max_size' and/or
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * 'target_size' are zero, default values will be * used. When
016c0a82f1ce3fe4d362d7c9cf8de4377ffaf5a9Mark Andrews * ISC_MEMFLAG_INTERNAL is not set, 'target_size' is ignored.
e0806ab5eda9add79044ccf17e55c49bae35d2e2Mark Andrews * 'max_size' is also used to size the statistics arrays and the array
f558c8988e776466f8ff3edc6a288d34dff75dc7Francis Dupont * used to record active memory when ISC_MEM_DEBUGRECORD is set. Setting
e0806ab5eda9add79044ccf17e55c49bae35d2e2Mark Andrews * 'max_size' too low can have detrimental effects on performance.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * A memory context created using isc_mem_createx() will obtain
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * memory from the system by calling 'memalloc' and 'memfree',
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * passing them the argument 'arg'. A memory context created
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * using isc_mem_create() will use the standard library malloc()
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * If ISC_MEMFLAG_NOLOCK is set in 'flags', the corresponding memory context
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * will be accessed without locking. The user who creates the context must
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * ensure there be no race. Since this can be a source of bug, it is generally
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * inadvisable to use this flag unless the user is very sure about the race
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉 * condition and the access to the object is highly performance sensitive.
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * mctxp != NULL && *mctxp == NULL */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \brief Attach to / detach from a memory context.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * This is intended for applications that use multiple memory contexts
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * in such a way that it is not obvious when the last allocations from
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * a given context has been freed and destroying the context is safe.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Most applications do not need to call these functions as they can
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * simply create a single memory context at the beginning of main()
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * and destroy it at the end of main(), thereby guaranteeing that it
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * is not destroyed while there are outstanding allocations.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Destroy a memory context.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Request to be notified with an event when a memory context has
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * been successfully destroyed.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Print memory usage statistics for 'mctx' on the stream 'out'.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * If 'on' is ISC_TRUE, 'mctx' will check for memory leaks when
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * destroyed and abort the program if any are present.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Set/get the memory quota of 'mctx'. This is a hard limit
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * on the amount of memory that may be allocated from mctx;
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * if it is exceeded, allocations will fail.
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * Get an estimate of the amount of memory in use in 'mctx', in bytes.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * This includes quantization overhead, but does not include memory
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * allocated from the system but not yet used.
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * Get an estimate of the largest amount of memory that has been in
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * use in 'mctx' at any time.
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * Get the total amount of memory in 'mctx', in bytes, including memory
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * not yet used.
27fe1966c948ba0c1c9d0d831ea3d8bf32d052acTatuya JINMEI 神明達哉 * Return true iff the memory context is in "over memory" state, i.e.,
27fe1966c948ba0c1c9d0d831ea3d8bf32d052acTatuya JINMEI 神明達哉 * a hiwater mark has been set and the used amount of memory has exceeds
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsisc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg,
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater * Set high and low water marks for this memory context.
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * When the memory usage of 'mctx' exceeds 'hiwater',
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * '(water)(water_arg, #ISC_MEM_HIWATER)' will be called. 'water' needs to
0ef59d22882fc5d28f4683fdbe88aefd8eaad6f3Francis Dupont * call isc_mem_waterack() with #ISC_MEM_HIWATER to acknowledge the state
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * change. 'water' may be called multiple times.
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * When the usage drops below 'lowater', 'water' will again be called, this
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * time with #ISC_MEM_LOWATER. 'water' need to calls isc_mem_waterack() with
0ef59d22882fc5d28f4683fdbe88aefd8eaad6f3Francis Dupont * #ISC_MEM_LOWATER to acknowledge the change.
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * static void
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * water(void *arg, int mark) {
43581a71ab34a68227c4fc78b263cb7e09168f81Mark Andrews * struct foo *foo = arg;
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * LOCK(&foo->marklock);
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * if (foo->mark != mark) {
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * foo->mark = mark;
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * isc_mem_waterack(foo->mctx, mark);
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews * UNLOCK(&foo->marklock);
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence * If 'water' is NULL then 'water_arg', 'hi_water' and 'lo_water' are
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence * ignored and the state is reset.
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence * 'water' is not NULL.
65640f401a22971de16c01e9a9547d95c55ed45eDavid Lawrence * hi_water >= lo_water
0ef59d22882fc5d28f4683fdbe88aefd8eaad6f3Francis Dupont * Called to acknowledge changes in signaled by calls to 'water'.
e9733bc599f30033ffe7af69f5fca254fb59d46eMark Andrewsisc_mem_printactive(isc_mem_t *mctx, FILE *file);
e9733bc599f30033ffe7af69f5fca254fb59d46eMark Andrews * Print to 'file' all active memory in 'mctx'.
e9733bc599f30033ffe7af69f5fca254fb59d46eMark Andrews * Requires ISC_MEM_DEBUGRECORD to have been set.
e9733bc599f30033ffe7af69f5fca254fb59d46eMark Andrews * Print to 'file' all active memory in all contexts.
e9733bc599f30033ffe7af69f5fca254fb59d46eMark Andrews * Requires ISC_MEM_DEBUGRECORD to have been set.
e9733bc599f30033ffe7af69f5fca254fb59d46eMark Andrews * Check that all memory contexts have been destroyed.
e9733bc599f30033ffe7af69f5fca254fb59d46eMark Andrews * Prints out those that have not been.
e9733bc599f30033ffe7af69f5fca254fb59d46eMark Andrews * Fatally fails if there are still active contexts.
f5d0f495847eb4eb9f0058e73051f855800bee0bMark Andrews * Return the current reference count.
cffe96e26744abcf33494837b234219046a631d8Mark Andrewsisc_mem_setname(isc_mem_t *ctx, const char *name, void *tag);
cffe96e26744abcf33494837b234219046a631d8Mark Andrews * Name 'ctx'.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews *\li Only the first 15 characters of 'name' will be copied.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews *\li 'tag' is for debugging purposes only.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews *\li 'ctx' is a valid ctx.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews * Get the name of 'ctx', as previously set using isc_mem_setname().
cffe96e26744abcf33494837b234219046a631d8Mark Andrews *\li 'ctx' is a valid ctx.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews *\li A non-NULL pointer to a null-terminated string.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews * If the ctx has not been named, the string is
cffe96e26744abcf33494837b234219046a631d8Mark Andrews * Get the tag value for 'task', as previously set using isc_mem_setname().
cffe96e26744abcf33494837b234219046a631d8Mark Andrews *\li 'ctx' is a valid ctx.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews *\li This function is for debugging purposes only.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews *\li 'ctx' is a valid task.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews * Render all contexts' statistics and status in XML for writer.
cffe96e26744abcf33494837b234219046a631d8Mark Andrews#endif /* HAVE_LIBXML2 */
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt * Render all contexts' statistics and status in JSON.
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt#endif /* HAVE_JSON */
eb7ef395d27b1104f684e21836f200c052736d07Michael Graff * Memory pools
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graffisc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Create a memory pool.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li mctx is a valid memory context.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li size > 0
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li mpctxp != NULL and *mpctxp == NULL
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li maxalloc = UINT_MAX
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li freemax = 1
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li fillcount = 1
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li #ISC_R_NOMEMORY -- not enough memory to create pool
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li #ISC_R_SUCCESS -- all is well.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Destroy a memory pool.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li mpctxp != NULL && *mpctxp is a valid pool.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li The pool has no un"put" allocations outstanding
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrenceisc_mempool_setname(isc_mempool_t *mpctx, const char *name);
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * Associate a name with a memory pool. At most 15 characters may be used.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li mpctx is a valid pool.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li name != NULL;
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graffisc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * Associate a lock with this memory pool.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * This lock is used when getting or putting items using this memory pool,
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * and it is also used to set or get internal state via the isc_mempool_get*()
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * and isc_mempool_set*() set of functions.
0ef59d22882fc5d28f4683fdbe88aefd8eaad6f3Francis Dupont * Multiple pools can each share a single lock. For instance, if "manager"
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * type object contained pools for various sizes of events, and each of
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * these pools used a common lock. Note that this lock must NEVER be used
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * by other than mempool routines once it is given to a pool, since that can
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * easily cause double locking.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li mpctpx is a valid pool.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li lock != NULL.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li No previous lock is assigned to this pool.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li The lock is initialized before calling this function via the normal
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * means of doing that.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * The following functions get/set various parameters. Note that due to
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * the unlocked nature of pools these are potentially random values unless
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * the imposed externally provided locking protocols are followed.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Also note that the quota limits will not always take immediate effect.
0fd03d2a66102f8d9bd7acea4e7db73ecaa0cb52Michael Graff * For instance, setting "maxalloc" to a number smaller than the currently
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * allocated count is permitted. New allocations will be refused until
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * the count drops below this threshold.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * All functions require (in addition to other requirements):
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * mpctx is a valid memory pool
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Returns the maximum allowed size of the free list.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Sets the maximum allowed size of the free list.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mempool_getfreecount(isc_mempool_t *mpctx);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Returns current size of the free list.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mempool_getmaxalloc(isc_mempool_t *mpctx);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Returns the maximum allowed number of allocations.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Sets the maximum allowed number of allocations.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Additional requirements:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li limit > 0
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mempool_getallocated(isc_mempool_t *mpctx);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Returns the number of items allocated from this pool.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mempool_getfillcount(isc_mempool_t *mpctx);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Returns the number of items allocated as a block from the parent memory
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * context when the free list is empty.
c7f22f83aac9e61dafee191cad040e9c42652cc8David Lawrenceisc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Sets the fillcount.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Additional requirements:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein *\li limit > 0
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Pseudo-private functions for use via macros. Do not call directly.
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMFUNC(get)(isc_mem_t *, size_t _ISC_MEM_FLARG);
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMFUNC(putanddetach)(isc_mem_t **, void *, size_t _ISC_MEM_FLARG);
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMFUNC(put)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMFUNC(allocate)(isc_mem_t *, size_t _ISC_MEM_FLARG);
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMFUNC(reallocate)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMFUNC(free)(isc_mem_t *, void * _ISC_MEM_FLARG);
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMFUNC(strdup)(isc_mem_t *, const char *_ISC_MEM_FLARG);
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMPOOLFUNC(get)(isc_mempool_t * _ISC_MEM_FLARG);
82629edf81d6ce977440eb5ce3314cdeb0fa14daEvan HuntISCMEMPOOLFUNC(put)(isc_mempool_t *, void * _ISC_MEM_FLARG);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * See isc_mem_create2() above.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉(*isc_memcreatefunc_t)(size_t init_max_size, size_t target_size,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉isc_mem_register(isc_memcreatefunc_t createfunc);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Register a new memory management implementation and add it to the list of
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * supported implementations. This function must be called when a different
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * memory management library is used than the one contained in the ISC library.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * A short cut function that specifies the memory management module in the ISC
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * library for isc_mem_register(). An application that uses the ISC library
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * usually do not have to care about this function: it would call
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * isc_lib_register(), which internally calls this function.
54a2e7e8a21ee765f41bd995101995613bff9e8cDavid Lawrence#endif /* ISC_MEM_H */