mem.h revision 18159f6f6053de21e433f2194ca4f89b96926c09
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 1997-2001 Internet Software Consortium.
138d22b316c7cd10eaa9df2ee0e3ec712a077153Bob Halley * Permission to use, copy, modify, and 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.
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
15a44745412679c30a6d022733925af70a38b715David Lawrence * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15a44745412679c30a6d022733925af70a38b715David Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15a44745412679c30a6d022733925af70a38b715David Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson/* $Id: mem.h,v 1.55 2001/09/06 18:23:35 gson 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.
18159f6f6053de21e433f2194ca4f89b96926c09Andreas Gustafsson * 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
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.
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.
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.
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * _DEBUGTRACE
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * log (to isc_lctx) each allocation and free.
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * _DEBUGRECORD
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * remember each allocation, and match them up on free. Crash if
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff * a free doesn't match an allocation
950d71eee32cb7c6ef9f659154f77bbf5a2cdce6Mark Andrews * _DEBUGUSAGE
950d71eee32cb7c6ef9f659154f77bbf5a2cdce6Mark Andrews * if a hi_water mark is set print the maximium inuse memory every
950d71eee32cb7c6ef9f659154f77bbf5a2cdce6Mark Andrews * time it is raised once it exceeds the hi_water mark
c4958494a98a59ce25e9fecad76a9ab0e36cc59fDanny MayerLIBISC_EXTERNAL_DATA extern unsigned int isc_mem_debugging;
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define _ISC_MEM_FLARG , const char *, int
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mem_get(c, s) isc__mem_get((c), (s) _ISC_MEM_FILELINE)
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mem_allocate(c, s) isc__mem_allocate((c), (s) _ISC_MEM_FILELINE)
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mem_strdup(c, p) isc__mem_strdup((c), (p) _ISC_MEM_FILELINE)
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mempool_get(c) isc__mempool_get((c) _ISC_MEM_FILELINE)
6c6bed90cb5b51a90530b7d488b2696626a8fafcMark Andrews * isc_mem_putanddetach() is a convienence 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);
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mem_put(c, p, s) \
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff isc__mem_put((c), (p), (s) _ISC_MEM_FILELINE); \
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews#define isc_mem_putanddetach(c, p, s) \
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews isc__mem_putanddetach((c), (p), (s) _ISC_MEM_FILELINE); \
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff isc__mempool_put((c), (p) _ISC_MEM_FILELINE); \
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mem_put(c, p, s) isc__mem_put((c), (p), (s) _ISC_MEM_FILELINE)
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews#define isc_mem_putanddetach(c, p, s) \
1162a4e02a6594dbb4f57fd288a5d20ab467e4d7Mark Andrews isc__mem_putanddetach((c), (p), (s) _ISC_MEM_FILELINE)
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mem_free(c, p) isc__mem_free((c), (p) _ISC_MEM_FILELINE)
bfab56849df65a2d4295b256808a66985cfa9d98Michael Graff#define isc_mempool_put(c, p) isc__mempool_put((c), (p) _ISC_MEM_FILELINE)
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc_mem_create(size_t max_size, size_t target_size,
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc_mem_createx(size_t max_size, size_t target_size,
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson isc_memalloc_t memalloc, isc_memfree_t memfree,
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Create a memory context.
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * 'max_size' and 'target_size' are tuning parameters. When
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * ISC_MEM_USE_INTERNAL_MALLOC is true, allocations smaller than
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * 'max_size' will be satisfied by getting blocks of size
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * 'target_size' from the system allocator and breaking them up into
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * pieces; larger allocations will use the system allocator directly.
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * If 'max_size' and/or 'target_size' are zero, default values will be
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * used. When ISC_MEM_USE_INTERNAL_MALLOC is false, 'max_size' and
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * 'target_size' are ignored.
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()
1d625775ee265ec4d5237ae3a315789ad0957bd5Andreas Gustafsson * mctxp != NULL && *mctxp == NULL */
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * 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'.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Iff '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.
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Get an estimate of the number 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.
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsisc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews * Set high and low water marks for this memory context. When the memory
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews * usage of 'mctx' exceeds 'hiwater', '(water)(water_arg, ISC_MEM_HIWATER)'
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews * will be called. When the usage drops below 'lowater', 'water' will
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews * again be called, this time with ISC_MEM_LOWATER.
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
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.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * mctx is a valid memory context.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * mpctxp != NULL and *mpctxp == NULL
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * maxalloc = UINT_MAX
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * freemax = 1
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * fillcount = 1
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * ISC_R_NOMEMORY -- not enough memory to create pool
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * ISC_R_SUCCESS -- all is well.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * Destroy a memory pool.
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * mpctxp != NULL && *mpctxp is a valid pool.
f28a94af2179ff7592d732e409d006582e9af7c3Michael Graff * 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.
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * mpctx is a valid pool.
78b2f25c9afee0d16f2e75882d9763abcb0872e5Michael Graff * 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.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * Mutiple 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.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * mpctpx is a valid pool.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * lock != NULL.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * No previous lock is assigned to this pool.
50dfb7ee4ada4c9aa30bd18c1b5d9b04be765669Michael Graff * 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:
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:
c674a8d5e7d202ada2bb617e5bb98b1859dc5a62Andreas Gustafsson * Pseudo-private functions for use via macros. Do not call directly.
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc__mem_get(isc_mem_t *, size_t _ISC_MEM_FLARG);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc__mem_put(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc__mem_allocate(isc_mem_t *, size_t _ISC_MEM_FLARG);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc__mem_free(isc_mem_t *, void * _ISC_MEM_FLARG);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc__mem_strdup(isc_mem_t *, const char *_ISC_MEM_FLARG);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc__mempool_get(isc_mempool_t * _ISC_MEM_FLARG);
8096fe1df5abfef957b934a1d1c5e97eaa246434Danny Mayerisc__mempool_put(isc_mempool_t *, void * _ISC_MEM_FLARG);
54a2e7e8a21ee765f41bd995101995613bff9e8cDavid Lawrence#endif /* ISC_MEM_H */