mem.c revision 58f32ac26ea330054f7b85579cd93a6376168fe7
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 1997-2003 Internet Software Consortium.
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * Permission to use, copy, modify, and distribute this software for any
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PERFORMANCE OF THIS SOFTWARE.
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson/* $Id: mem.c,v 1.132 2007/02/26 22:57:03 marka Exp $ */
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#define MCTXLOCK(m, l) if (((m)->flags & ISC_MEMFLAG_NOLOCK) == 0) LOCK(l)
e419f613d8591885df608cb73065921be07dd12eBob Halley#define MCTXUNLOCK(m, l) if (((m)->flags & ISC_MEMFLAG_NOLOCK) == 0) UNLOCK(l)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob HalleyLIBISC_EXTERNAL_DATA unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Constants.
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define ALIGNMENT_SIZE 8 /*%< must be a power of 2 */
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define NUM_BASIC_BLOCKS 64 /*%< must be > 1 */
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence unsigned int count;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtontypedef struct {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff * This structure must be ALIGNMENT_SIZE bytes.
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington unsigned long gets;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington unsigned long blocks;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define MEM_MAGIC ISC_MAGIC('M', 'e', 'm', 'C')
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define VALID_CONTEXT(c) ISC_MAGIC_VALID(c, MEM_MAGIC)
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington/* List of all active memory contexts. */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington /* ISC_MEMFLAG_INTERNAL */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington unsigned char ** basic_table;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson unsigned char * lowest;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned char * highest;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define MEMPOOL_MAGIC ISC_MAGIC('M', 'E', 'M', 'p')
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#define VALID_MEMPOOL(c) ISC_MAGIC_VALID(c, MEMPOOL_MAGIC)
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington /* always unlocked */
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington isc_mem_t *mctx; /*%< our memory context */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson /*%< locked via the memory context's lock */
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson ISC_LINK(isc_mempool_t) link; /*%< next pool in this mem context */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*%< optionally locked from here down */
01446841be2b73f9a2ead74056df2d5342414041Andreas Gustafsson element *items; /*%< low water item list */
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington size_t size; /*%< size of each item on this pool */
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington unsigned int maxalloc; /*%< max number of items allowed */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int allocated; /*%< # of items currently given out */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int freecount; /*%< # of items on reserved list */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence unsigned int freemax; /*%< # of items allowed on free list */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence unsigned int fillcount; /*%< # of items to fetch on each fill */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*%< Stats only. */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int gets; /*%< # of requests to this pool */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*%< Debugging only. */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington char name[16]; /*%< printed name in stats reports */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * Private Inline-able.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#define ADD_TRACE(a, b, c, d, e)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define DELETE_TRACE(a, b, c, d, e)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#define ADD_TRACE(a, b, c, d, e) \
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if ((isc_mem_debugging & (ISC_MEM_DEBUGTRACE | \
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington add_trace_entry(a, b, c, d, e); \
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington#define DELETE_TRACE(a, b, c, d, e) delete_trace_entry(a, b, c, d, e)
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * mctx must be locked.
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic inline void
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonadd_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington unsigned int i;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington "add %p size %u "
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington "file %s line %u mctx %p\n"),
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington for (i = 0; i < DEBUGLIST_COUNT; i++) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ISC_LIST_PREPEND(mctx->debuglist[size], dl, link);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline void
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtondelete_trace_entry(isc_mem_t *mctx, const void *ptr, unsigned int size,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington unsigned int i;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington "del %p size %u "
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "file %s line %u mctx %p\n"),
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson dl = ISC_LIST_HEAD(mctx->debuglist[size]);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington for (i = 0; i < DEBUGLIST_COUNT; i++) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington * If we get here, we didn't find the item on the list. We're
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington#endif /* ISC_MEM_TRACKLINES */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * round down to ALIGNMENT_SIZE
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * Round up the result in order to get a size big
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * byte boundaries.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return ((size + ALIGNMENT_SIZE - 1) & (~(ALIGNMENT_SIZE - 1)));
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington unsigned char **table;
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington /* Require: we hold the context lock. */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * Did we hit the quota for this context?
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence increment = NUM_BASIC_BLOCKS * ctx->mem_target;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->quota != 0U && ctx->total + increment > ctx->quota)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington INSIST(ctx->basic_table_count <= ctx->basic_table_size);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->basic_table_count == ctx->basic_table_size) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington table_size = ctx->basic_table_size + TABLE_INCREMENT;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington table_size * sizeof(unsigned char *));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington sizeof(unsigned char *));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington (ctx->memfree)(ctx->arg, ctx->basic_table);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington new = (ctx->memalloc)(ctx->arg, NUM_BASIC_BLOCKS * ctx->mem_target);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->basic_table[ctx->basic_table_count] = new;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * curr is now pointing at the last block in the
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington if (first < ctx->lowest || ctx->lowest == NULL)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonmore_frags(isc_mem_t *ctx, size_t new_size) {
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington * Try to get more fragments by chopping up a basic block.
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington * We can't get more memory from the OS, or we've
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington * hit the quota for this context.
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington * XXXRTH "At quota" notification here.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ctx->basic_blocks = ctx->basic_blocks->next;
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington * Set up a linked-list of blocks of size
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington * Add the remaining fragment of the basic block to a free list.
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington ((element *)next)->next = ctx->freelists[total_size];
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington ctx->freelists[total_size] = (element *)next;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * curr is now pointing at the last block in the
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonstatic inline void *
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonmem_getunlocked(isc_mem_t *ctx, size_t size) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (size >= ctx->max_size || new_size >= ctx->max_size) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * memget() was called on something beyond our upper limit.
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (ctx->quota != 0U && ctx->total + size > ctx->quota) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * If we don't set new_size to size, then the
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * ISC_MEM_FILL code might write over bytes we
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * If there are no blocks in the free list for this size, get a chunk
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * of memory and then break it up into "new_size"-sized blocks, adding
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * them to the free list.
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (ctx->freelists[new_size] == NULL && !more_frags(ctx, new_size))
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * The free list uses the "rounded-up" size "new_size".
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington ctx->freelists[new_size] = ctx->freelists[new_size]->next;
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * The stats[] uses the _actual_ "size" requested by the
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * caller, with the caveat (in the code above) that "size" >= the
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * max. size (max_size) ends up getting recorded as a call to
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtoncheck_overrun(void *mem, size_t size, size_t new_size) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson unsigned char *cp;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic inline void
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonmem_putunlocked(isc_mem_t *ctx, void *mem, size_t size) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (size == ctx->max_size || new_size >= ctx->max_size) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * memput() called on something beyond our upper limit.
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington memset(mem, 0xde, size); /* Mnemonic for "dead". */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington INSIST(ctx->stats[ctx->max_size].gets != 0U);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington * The free list uses the "rounded-up" size "new_size".
f439363eeb4052fddc0e3ec648658548daa10506Brian Wellington ((element *)mem)->next = ctx->freelists[new_size];
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * The stats[] uses the _actual_ "size" requested by the
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington * caller, with the caveat (in the code above) that "size" >= the
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * max. size (max_size) ends up getting recorded as a call to
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Perform a malloc, doing memory filling and overrun detection as necessary.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void *
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews memset(ret, 0xbe, size); /* Mnemonic for "beef". */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * Perform a free, doing memory filling and overrun detection as necessary.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline void
5c29047792191d6141f69b2684314d0b762fedebBrian Wellingtonmem_put(isc_mem_t *ctx, void *mem, size_t size) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington INSIST(((unsigned char *)mem)[size] == 0xbe);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington memset(mem, 0xde, size); /* Mnemonic for "dead". */
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson * Update internal counters after a memory get.
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafssonstatic inline void
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafssonmem_getstats(isc_mem_t *ctx, size_t size) {
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington * Update internal counters after a memory put.
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonstatic inline void
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonmem_putstats(isc_mem_t *ctx, void *ptr, size_t size) {
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellingtonisc_mem_createx(size_t init_max_size, size_t target_size,
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (isc_mem_createx2(init_max_size, target_size, memalloc, memfree,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyisc_mem_createx2(size_t init_max_size, size_t target_size,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0);
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington unsigned int i;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington ISC_LIST_INITANDAPPEND(contexts, ctx, link);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#endif /* ISC_MEM_TRACKLINES */
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtonisc_mem_create(size_t init_max_size, size_t target_size,
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington return (isc_mem_createx2(init_max_size, target_size,
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtonisc_mem_create2(size_t init_max_size, size_t target_size,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (isc_mem_createx2(init_max_size, target_size,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley unsigned int i;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington for (dl = ISC_LIST_HEAD(ctx->debuglist[i]);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington for (i = 0; i < ctx->basic_table_count; i++)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington (ctx->memfree)(ctx->arg, ctx->basic_table);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonisc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington REQUIRE(targetp != NULL && *targetp == NULL);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_putanddetach() is the equivalent of:
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * mctx = NULL;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_attach(ptr->mctx, &mctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_detach(&ptr->mctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_put(mctx, ptr, sizeof(*ptr);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * isc_mem_detach(&mctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonisc__mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Must be before mem_putunlocked() as ctxp is usually within
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson * [ptr..ptr+size).
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0) {
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington * This routine provides legacy support for callers who use mctxs
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonisc_mem_ondestroy(isc_mem_t *ctx, isc_task_t *task, isc_event_t **event) {
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington res = isc_ondestroy_register(&ctx->ondestroy, task, event);
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellingtonisc__mem_get(isc_mem_t *ctx, size_t size FLARG) {
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0)
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington return (isc__mem_allocate(ctx, size FLARG_PASS));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington if (ctx->hi_water != 0U && !ctx->hi_called &&
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellingtonisc__mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG)
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington * The check against ctx->lo_water == 0 is for the condition
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * when the context was pushed over hi_water but then had
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * isc_mem_setwater() called with 0 for hi_water and lo_water.
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington unsigned int i, j;
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington "Dump of all outstanding "
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington "memory allocations:\n"));
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington format = isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington "\tptr %p size %u file %s line %u\n");
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington for (j = 0; j < DEBUGLIST_COUNT; j++)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * Print the stats[] on the stream "out" with suitable formatting.
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington const struct stats *s;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * Note that since a pool can be locked now, these stats might be
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * somewhat off if the pool is in active use at the time the stats
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * are dumped. The link fields are protected by the isc_mem_t's
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * lock, however, so walking this list and extracting integers from
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * stats fields is always safe.
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington "[Pool statistics]\n"));
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington pool->name, (unsigned long) pool->size, pool->maxalloc,
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington pool->allocated, pool->freecount, pool->freemax,
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington * Replacements for malloc() and free() -- they implicitly remember the
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * size of the object allocated (with some additional overhead).
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonisc__mem_allocateunlocked(isc_mem_t *ctx, size_t size) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonisc__mem_allocate(isc_mem_t *ctx, size_t size FLARG) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson si = isc__mem_allocateunlocked(ctx, size);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson ADD_TRACE(ctx, si, si[-1].u.size, file, line);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (ctx->hi_water != 0U && !ctx->hi_called &&
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonisc__mem_free(isc_mem_t *ctx, void *ptr FLARG) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * The check against ctx->lo_water == 0 is for the condition
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * when the context was pushed over hi_water but then had
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * isc_mem_setwater() called with 0 for hi_water and lo_water.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington * Other useful things.
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonisc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ns = isc__mem_allocate(mctx, len + 1 FLARG_PASS);
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellingtonisc_mem_setdestroycheck(isc_mem_t *ctx, isc_boolean_t flag) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafssonisc_mem_setquota(isc_mem_t *ctx, size_t quota) {
e419f613d8591885df608cb73065921be07dd12eBob Halleyisc_mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
e419f613d8591885df608cb73065921be07dd12eBob Halley (ctx->water != water || ctx->water_arg != water_arg ||
e419f613d8591885df608cb73065921be07dd12eBob Halley * Memory pool stuff
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyisc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) {
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington REQUIRE(mpctxp != NULL && *mpctxp == NULL);
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington * Allocate space for this pool, initialize values, and if all works
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * well, attach to the memory context.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington mpctx = isc_mem_get(mctx, sizeof(isc_mempool_t));
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
e419f613d8591885df608cb73065921be07dd12eBob Halleyisc_mempool_setname(isc_mempool_t *mpctx, const char *name) {
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington mpctx->name[sizeof(mpctx->name) - 1] = '\0';
1872808932603066d401d3de97db11af8ffee78aAndreas Gustafsson "isc_mempool_destroy(): mempool %s "
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington "leaked memory",
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Return any items on the free list
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Remove our linked list entry from the memory context.
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson ISC_LIST_UNLINK(mctx->pools, mpctx, link);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson isc_mem_put(mpctx->mctx, mpctx, sizeof(isc_mempool_t));
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafssonisc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrenceisc__mempool_get(isc_mempool_t *mpctx FLARG) {
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson unsigned int i;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson * Don't let the caller go over quota
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson if (mpctx->allocated >= mpctx->maxalloc) {
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington * if we have a free list item, return the first here
goto out;
out:
return (item);
unsigned int freemax;
return (freemax);
unsigned int freecount;
return (freecount);
unsigned int maxalloc;
return (maxalloc);
unsigned int allocated;
return (allocated);
unsigned int fillcount;
return (fillcount);
#if !ISC_MEM_TRACKLINES
#if !ISC_MEM_TRACKLINES
INSIST(0);
#ifdef HAVE_LIBXML2
size_t i;
const struct stats *s;
s->blocks);
s->freefrags);