2 * Copyright (C) 2004-2010, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1997-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
30 #include <isc/magic.h>
34 #include <isc/ondestroy.h>
35 #include <isc/string.h>
36 #include <isc/mutex.h>
37 #include <isc/print.h>
41 #define MCTXLOCK(m, l) if (((m)->flags & ISC_MEMFLAG_NOLOCK) == 0) LOCK(l)
42 #define MCTXUNLOCK(m, l) if (((m)->flags & ISC_MEMFLAG_NOLOCK) == 0) UNLOCK(l)
44 #ifndef ISC_MEM_DEBUGGING
45 #define ISC_MEM_DEBUGGING 0
47 LIBISC_EXTERNAL_DATA unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING;
53 #define DEF_MAX_SIZE 1100
54 #define DEF_MEM_TARGET 4096
55 #define ALIGNMENT_SIZE 8U /*%< must be a power of 2 */
56 #define NUM_BASIC_BLOCKS 64 /*%< must be > 1 */
57 #define TABLE_INCREMENT 1024
58 #define DEBUGLIST_COUNT 1024
63 typedef struct isc__mem isc__mem_t;
64 typedef struct isc__mempool isc__mempool_t;
66 #if ISC_MEM_TRACKLINES
67 typedef struct debuglink debuglink_t;
69 ISC_LINK(debuglink_t) link;
70 const void *ptr[DEBUGLIST_COUNT];
71 unsigned int size[DEBUGLIST_COUNT];
72 const char *file[DEBUGLIST_COUNT];
73 unsigned int line[DEBUGLIST_COUNT];
77 #define FLARG_PASS , file, line
78 #define FLARG , const char *file, unsigned int line
84 typedef struct element element;
91 * This structure must be ALIGNMENT_SIZE bytes.
96 char bytes[ALIGNMENT_SIZE];
102 unsigned long totalgets;
103 unsigned long blocks;
104 unsigned long freefrags;
107 #define MEM_MAGIC ISC_MAGIC('M', 'e', 'm', 'C')
108 #define VALID_CONTEXT(c) ISC_MAGIC_VALID(c, MEM_MAGIC)
110 #if ISC_MEM_TRACKLINES
111 typedef ISC_LIST(debuglink_t) debuglist_t;
114 /* List of all active memory contexts. */
116 static ISC_LIST(isc__mem_t) contexts;
117 static isc_once_t once = ISC_ONCE_INIT;
118 static isc_mutex_t lock;
121 * Total size of lost memory due to a bug of external library.
122 * Locked by the global lock.
124 static isc_uint64_t totallost;
128 isc_ondestroy_t ondestroy;
131 isc_memalloc_t memalloc;
132 isc_memfree_t memfree;
135 isc_boolean_t checkfree;
136 struct stats * stats;
137 unsigned int references;
146 isc_boolean_t hi_called;
147 isc_boolean_t is_overmem;
148 isc_mem_water_t water;
150 ISC_LIST(isc__mempool_t) pools;
151 unsigned int poolcnt;
153 /* ISC_MEMFLAG_INTERNAL */
155 element ** freelists;
156 element * basic_blocks;
157 unsigned char ** basic_table;
158 unsigned int basic_table_count;
159 unsigned int basic_table_size;
160 unsigned char * lowest;
161 unsigned char * highest;
163 #if ISC_MEM_TRACKLINES
164 debuglist_t * debuglist;
165 unsigned int debuglistcnt;
168 unsigned int memalloc_failures;
169 ISC_LINK(isc__mem_t) link;
172 #define MEMPOOL_MAGIC ISC_MAGIC('M', 'E', 'M', 'p')
173 #define VALID_MEMPOOL(c) ISC_MAGIC_VALID(c, MEMPOOL_MAGIC)
175 struct isc__mempool {
176 /* always unlocked */
177 isc_mempool_t common; /*%< common header of mempool's */
178 isc_mutex_t *lock; /*%< optional lock */
179 isc__mem_t *mctx; /*%< our memory context */
180 /*%< locked via the memory context's lock */
181 ISC_LINK(isc__mempool_t) link; /*%< next pool in this mem context */
182 /*%< optionally locked from here down */
183 element *items; /*%< low water item list */
184 size_t size; /*%< size of each item on this pool */
185 unsigned int maxalloc; /*%< max number of items allowed */
186 unsigned int allocated; /*%< # of items currently given out */
187 unsigned int freecount; /*%< # of items on reserved list */
188 unsigned int freemax; /*%< # of items allowed on free list */
189 unsigned int fillcount; /*%< # of items to fetch on each fill */
191 unsigned int gets; /*%< # of requests to this pool */
192 /*%< Debugging only. */
193 #if ISC_MEMPOOL_NAMES
194 char name[16]; /*%< printed name in stats reports */
199 * Private Inline-able.
202 #if ! ISC_MEM_TRACKLINES
203 #define ADD_TRACE(a, b, c, d, e)
204 #define DELETE_TRACE(a, b, c, d, e)
206 #define ADD_TRACE(a, b, c, d, e) \
208 if ((isc_mem_debugging & (ISC_MEM_DEBUGTRACE | \
209 ISC_MEM_DEBUGRECORD)) != 0 && \
211 add_trace_entry(a, b, c, d, e); \
213 #define DELETE_TRACE(a, b, c, d, e) delete_trace_entry(a, b, c, d, e)
216 print_active(isc__mem_t *ctx, FILE *out);
219 * The following can be either static or public, depending on build environment.
223 #define ISC_MEMFUNC_SCOPE
225 #define ISC_MEMFUNC_SCOPE static
228 ISC_MEMFUNC_SCOPE isc_result_t
229 isc__mem_createx(size_t init_max_size, size_t target_size,
230 isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
232 ISC_MEMFUNC_SCOPE isc_result_t
233 isc__mem_createx2(size_t init_max_size, size_t target_size,
234 isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
235 isc_mem_t **ctxp, unsigned int flags);
236 ISC_MEMFUNC_SCOPE isc_result_t
237 isc__mem_create(size_t init_max_size, size_t target_size, isc_mem_t **ctxp);
238 ISC_MEMFUNC_SCOPE isc_result_t
239 isc__mem_create2(size_t init_max_size, size_t target_size,
240 isc_mem_t **ctxp, unsigned int flags);
241 ISC_MEMFUNC_SCOPE void
242 isc__mem_attach(isc_mem_t *source, isc_mem_t **targetp);
243 ISC_MEMFUNC_SCOPE void
244 isc__mem_detach(isc_mem_t **ctxp);
245 ISC_MEMFUNC_SCOPE void
246 isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG);
247 ISC_MEMFUNC_SCOPE void
248 isc__mem_destroy(isc_mem_t **ctxp);
249 ISC_MEMFUNC_SCOPE isc_result_t
250 isc__mem_ondestroy(isc_mem_t *ctx, isc_task_t *task, isc_event_t **event);
251 ISC_MEMFUNC_SCOPE void *
252 isc___mem_get(isc_mem_t *ctx, size_t size FLARG);
253 ISC_MEMFUNC_SCOPE void
254 isc___mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG);
255 ISC_MEMFUNC_SCOPE void
256 isc__mem_stats(isc_mem_t *ctx, FILE *out);
257 ISC_MEMFUNC_SCOPE void *
258 isc___mem_allocate(isc_mem_t *ctx, size_t size FLARG);
259 ISC_MEMFUNC_SCOPE void *
260 isc___mem_reallocate(isc_mem_t *ctx, void *ptr, size_t size FLARG);
261 ISC_MEMFUNC_SCOPE void
262 isc___mem_free(isc_mem_t *ctx, void *ptr FLARG);
263 ISC_MEMFUNC_SCOPE char *
264 isc___mem_strdup(isc_mem_t *mctx, const char *s FLARG);
265 ISC_MEMFUNC_SCOPE void
266 isc__mem_setdestroycheck(isc_mem_t *ctx, isc_boolean_t flag);
267 ISC_MEMFUNC_SCOPE void
268 isc__mem_setquota(isc_mem_t *ctx, size_t quota);
269 ISC_MEMFUNC_SCOPE size_t
270 isc__mem_getquota(isc_mem_t *ctx);
271 ISC_MEMFUNC_SCOPE size_t
272 isc__mem_inuse(isc_mem_t *ctx);
273 ISC_MEMFUNC_SCOPE isc_boolean_t
274 isc__mem_isovermem(isc_mem_t *ctx);
275 ISC_MEMFUNC_SCOPE void
276 isc__mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
277 size_t hiwater, size_t lowater);
278 ISC_MEMFUNC_SCOPE void
279 isc__mem_waterack(isc_mem_t *ctx0, int flag);
280 ISC_MEMFUNC_SCOPE void
281 isc__mem_setname(isc_mem_t *ctx, const char *name, void *tag);
282 ISC_MEMFUNC_SCOPE const char *
283 isc__mem_getname(isc_mem_t *ctx);
284 ISC_MEMFUNC_SCOPE void *
285 isc__mem_gettag(isc_mem_t *ctx);
286 ISC_MEMFUNC_SCOPE isc_result_t
287 isc__mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
288 ISC_MEMFUNC_SCOPE void
289 isc__mempool_setname(isc_mempool_t *mpctx, const char *name);
290 ISC_MEMFUNC_SCOPE void
291 isc__mempool_destroy(isc_mempool_t **mpctxp);
292 ISC_MEMFUNC_SCOPE void
293 isc__mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
294 ISC_MEMFUNC_SCOPE void *
295 isc___mempool_get(isc_mempool_t *mpctx FLARG);
296 ISC_MEMFUNC_SCOPE void
297 isc___mempool_put(isc_mempool_t *mpctx, void *mem FLARG);
298 ISC_MEMFUNC_SCOPE void
299 isc__mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
300 ISC_MEMFUNC_SCOPE unsigned int
301 isc__mempool_getfreemax(isc_mempool_t *mpctx);
302 ISC_MEMFUNC_SCOPE unsigned int
303 isc__mempool_getfreecount(isc_mempool_t *mpctx);
304 ISC_MEMFUNC_SCOPE void
305 isc__mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
306 ISC_MEMFUNC_SCOPE unsigned int
307 isc__mempool_getmaxalloc(isc_mempool_t *mpctx);
308 ISC_MEMFUNC_SCOPE unsigned int
309 isc__mempool_getallocated(isc_mempool_t *mpctx);
310 ISC_MEMFUNC_SCOPE void
311 isc__mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
312 ISC_MEMFUNC_SCOPE unsigned int
313 isc__mempool_getfillcount(isc_mempool_t *mpctx);
315 ISC_MEMFUNC_SCOPE void
316 isc__mem_printactive(isc_mem_t *ctx0, FILE *file);
317 ISC_MEMFUNC_SCOPE void
318 isc__mem_printallactive(FILE *file);
319 ISC_MEMFUNC_SCOPE void
320 isc__mem_checkdestroyed(FILE *file);
321 ISC_MEMFUNC_SCOPE unsigned int
322 isc__mem_references(isc_mem_t *ctx0);
325 static struct isc__memmethods {
326 isc_memmethods_t methods;
329 * The following are defined just for avoiding unused static functions.
332 void *createx, *create, *create2, *ondestroy, *stats,
333 *setquota, *getquota, *setname, *getname, *gettag;
342 isc___mem_putanddetach,
344 isc___mem_reallocate,
347 isc__mem_setdestroycheck,
356 (void *)isc__mem_createx, (void *)isc__mem_create,
357 (void *)isc__mem_create2, (void *)isc__mem_ondestroy,
358 (void *)isc__mem_stats, (void *)isc__mem_setquota,
359 (void *)isc__mem_getquota, (void *)isc__mem_setname,
360 (void *)isc__mem_getname, (void *)isc__mem_gettag
364 static struct isc__mempoolmethods {
365 isc_mempoolmethods_t methods;
368 * The following are defined just for avoiding unused static functions.
371 void *getfreemax, *getfreecount, *getmaxalloc, *getfillcount;
375 isc__mempool_destroy,
378 isc__mempool_getallocated,
379 isc__mempool_setmaxalloc,
380 isc__mempool_setfreemax,
381 isc__mempool_setname,
382 isc__mempool_associatelock,
383 isc__mempool_setfillcount
387 (void *)isc__mempool_getfreemax, (void *)isc__mempool_getfreecount,
388 (void *)isc__mempool_getmaxalloc, (void *)isc__mempool_getfillcount
393 * mctx must be locked.
396 add_trace_entry(isc__mem_t *mctx, const void *ptr, unsigned int size
401 unsigned int mysize = size;
403 if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
404 fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
407 "file %s line %u mctx %p\n"),
408 ptr, size, file, line, mctx);
410 if (mctx->debuglist == NULL)
413 if (mysize > mctx->max_size)
414 mysize = mctx->max_size;
416 dl = ISC_LIST_HEAD(mctx->debuglist[mysize]);
418 if (dl->count == DEBUGLIST_COUNT)
420 for (i = 0; i < DEBUGLIST_COUNT; i++) {
421 if (dl->ptr[i] == NULL) {
431 dl = ISC_LIST_NEXT(dl, link);
434 dl = malloc(sizeof(debuglink_t));
437 ISC_LINK_INIT(dl, link);
438 for (i = 1; i < DEBUGLIST_COUNT; i++) {
451 ISC_LIST_PREPEND(mctx->debuglist[mysize], dl, link);
452 mctx->debuglistcnt++;
456 delete_trace_entry(isc__mem_t *mctx, const void *ptr, unsigned int size,
457 const char *file, unsigned int line)
462 if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
463 fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
466 "file %s line %u mctx %p\n"),
467 ptr, size, file, line, mctx);
469 if (mctx->debuglist == NULL)
472 if (size > mctx->max_size)
473 size = mctx->max_size;
475 dl = ISC_LIST_HEAD(mctx->debuglist[size]);
477 for (i = 0; i < DEBUGLIST_COUNT; i++) {
478 if (dl->ptr[i] == ptr) {
484 INSIST(dl->count > 0);
486 if (dl->count == 0) {
487 ISC_LIST_UNLINK(mctx->debuglist[size],
494 dl = ISC_LIST_NEXT(dl, link);
498 * If we get here, we didn't find the item on the list. We're
503 #endif /* ISC_MEM_TRACKLINES */
506 rmsize(size_t size) {
508 * round down to ALIGNMENT_SIZE
510 return (size & (~(ALIGNMENT_SIZE - 1)));
514 quantize(size_t size) {
516 * Round up the result in order to get a size big
517 * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
522 return (ALIGNMENT_SIZE);
523 return ((size + ALIGNMENT_SIZE - 1) & (~(ALIGNMENT_SIZE - 1)));
526 static inline isc_boolean_t
527 more_basic_blocks(isc__mem_t *ctx) {
529 unsigned char *curr, *next;
530 unsigned char *first, *last;
531 unsigned char **table;
532 unsigned int table_size;
536 /* Require: we hold the context lock. */
539 * Did we hit the quota for this context?
541 increment = NUM_BASIC_BLOCKS * ctx->mem_target;
542 if (ctx->quota != 0U && ctx->total + increment > ctx->quota)
545 INSIST(ctx->basic_table_count <= ctx->basic_table_size);
546 if (ctx->basic_table_count == ctx->basic_table_size) {
547 table_size = ctx->basic_table_size + TABLE_INCREMENT;
548 table = (ctx->memalloc)(ctx->arg,
549 table_size * sizeof(unsigned char *));
551 ctx->memalloc_failures++;
554 if (ctx->basic_table_size != 0) {
555 memcpy(table, ctx->basic_table,
556 ctx->basic_table_size *
557 sizeof(unsigned char *));
558 (ctx->memfree)(ctx->arg, ctx->basic_table);
560 ctx->basic_table = table;
561 ctx->basic_table_size = table_size;
564 new = (ctx->memalloc)(ctx->arg, NUM_BASIC_BLOCKS * ctx->mem_target);
566 ctx->memalloc_failures++;
569 ctx->total += increment;
570 ctx->basic_table[ctx->basic_table_count] = new;
571 ctx->basic_table_count++;
574 next = curr + ctx->mem_target;
575 for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
576 ((element *)curr)->next = (element *)next;
578 next += ctx->mem_target;
581 * curr is now pointing at the last block in the
584 ((element *)curr)->next = NULL;
586 last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
587 if (first < ctx->lowest || ctx->lowest == NULL)
589 if (last > ctx->highest)
591 ctx->basic_blocks = new;
596 static inline isc_boolean_t
597 more_frags(isc__mem_t *ctx, size_t new_size) {
601 unsigned char *curr, *next;
604 * Try to get more fragments by chopping up a basic block.
607 if (ctx->basic_blocks == NULL) {
608 if (!more_basic_blocks(ctx)) {
610 * We can't get more memory from the OS, or we've
611 * hit the quota for this context.
614 * XXXRTH "At quota" notification here.
620 total_size = ctx->mem_target;
621 new = ctx->basic_blocks;
622 ctx->basic_blocks = ctx->basic_blocks->next;
623 frags = total_size / new_size;
624 ctx->stats[new_size].blocks++;
625 ctx->stats[new_size].freefrags += frags;
627 * Set up a linked-list of blocks of size
631 next = curr + new_size;
632 total_size -= new_size;
633 for (i = 0; i < (frags - 1); i++) {
634 ((element *)curr)->next = (element *)next;
637 total_size -= new_size;
640 * Add the remaining fragment of the basic block to a free list.
642 total_size = rmsize(total_size);
643 if (total_size > 0U) {
644 ((element *)next)->next = ctx->freelists[total_size];
645 ctx->freelists[total_size] = (element *)next;
646 ctx->stats[total_size].freefrags++;
649 * curr is now pointing at the last block in the
652 ((element *)curr)->next = NULL;
653 ctx->freelists[new_size] = new;
659 mem_getunlocked(isc__mem_t *ctx, size_t size) {
660 size_t new_size = quantize(size);
663 if (size >= ctx->max_size || new_size >= ctx->max_size) {
665 * memget() was called on something beyond our upper limit.
667 if (ctx->quota != 0U && ctx->total + size > ctx->quota) {
671 ret = (ctx->memalloc)(ctx->arg, size);
673 ctx->memalloc_failures++;
678 ctx->stats[ctx->max_size].gets++;
679 ctx->stats[ctx->max_size].totalgets++;
681 * If we don't set new_size to size, then the
682 * ISC_MEM_FILL code might write over bytes we
690 * If there are no blocks in the free list for this size, get a chunk
691 * of memory and then break it up into "new_size"-sized blocks, adding
692 * them to the free list.
694 if (ctx->freelists[new_size] == NULL && !more_frags(ctx, new_size))
698 * The free list uses the "rounded-up" size "new_size".
700 ret = ctx->freelists[new_size];
701 ctx->freelists[new_size] = ctx->freelists[new_size]->next;
704 * The stats[] uses the _actual_ "size" requested by the
705 * caller, with the caveat (in the code above) that "size" >= the
706 * max. size (max_size) ends up getting recorded as a call to
709 ctx->stats[size].gets++;
710 ctx->stats[size].totalgets++;
711 ctx->stats[new_size].freefrags--;
712 ctx->inuse += new_size;
718 memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
724 #if ISC_MEM_FILL && ISC_MEM_CHECKOVERRUN
726 check_overrun(void *mem, size_t size, size_t new_size) {
729 cp = (unsigned char *)mem;
731 while (size < new_size) {
740 mem_putunlocked(isc__mem_t *ctx, void *mem, size_t size) {
741 size_t new_size = quantize(size);
743 if (size == ctx->max_size || new_size >= ctx->max_size) {
745 * memput() called on something beyond our upper limit.
748 memset(mem, 0xde, size); /* Mnemonic for "dead". */
750 (ctx->memfree)(ctx->arg, mem);
751 INSIST(ctx->stats[ctx->max_size].gets != 0U);
752 ctx->stats[ctx->max_size].gets--;
753 INSIST(size <= ctx->total);
760 #if ISC_MEM_CHECKOVERRUN
761 check_overrun(mem, size, new_size);
763 memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
767 * The free list uses the "rounded-up" size "new_size".
769 ((element *)mem)->next = ctx->freelists[new_size];
770 ctx->freelists[new_size] = (element *)mem;
773 * The stats[] uses the _actual_ "size" requested by the
774 * caller, with the caveat (in the code above) that "size" >= the
775 * max. size (max_size) ends up getting recorded as a call to
778 INSIST(ctx->stats[size].gets != 0U);
779 ctx->stats[size].gets--;
780 ctx->stats[new_size].freefrags++;
781 ctx->inuse -= new_size;
785 * Perform a malloc, doing memory filling and overrun detection as necessary.
788 mem_get(isc__mem_t *ctx, size_t size) {
791 #if ISC_MEM_CHECKOVERRUN
795 ret = (ctx->memalloc)(ctx->arg, size);
797 ctx->memalloc_failures++;
801 memset(ret, 0xbe, size); /* Mnemonic for "beef". */
803 # if ISC_MEM_CHECKOVERRUN
813 * Perform a free, doing memory filling and overrun detection as necessary.
816 mem_put(isc__mem_t *ctx, void *mem, size_t size) {
817 #if ISC_MEM_CHECKOVERRUN
818 INSIST(((unsigned char *)mem)[size] == 0xbe);
821 memset(mem, 0xde, size); /* Mnemonic for "dead". */
825 (ctx->memfree)(ctx->arg, mem);
829 * Update internal counters after a memory get.
832 mem_getstats(isc__mem_t *ctx, size_t size) {
836 if (size > ctx->max_size) {
837 ctx->stats[ctx->max_size].gets++;
838 ctx->stats[ctx->max_size].totalgets++;
840 ctx->stats[size].gets++;
841 ctx->stats[size].totalgets++;
846 * Update internal counters after a memory put.
849 mem_putstats(isc__mem_t *ctx, void *ptr, size_t size) {
852 INSIST(ctx->inuse >= size);
855 if (size > ctx->max_size) {
856 INSIST(ctx->stats[ctx->max_size].gets > 0U);
857 ctx->stats[ctx->max_size].gets--;
859 INSIST(ctx->stats[size].gets > 0U);
860 ctx->stats[size].gets--;
869 default_memalloc(void *arg, size_t size) {
873 return (malloc(size));
877 default_memfree(void *arg, void *ptr) {
883 initialize_action(void) {
884 RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
885 ISC_LIST_INIT(contexts);
893 ISC_MEMFUNC_SCOPE isc_result_t
894 isc__mem_createx(size_t init_max_size, size_t target_size,
895 isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
898 return (isc__mem_createx2(init_max_size, target_size, memalloc, memfree,
899 arg, ctxp, ISC_MEMFLAG_DEFAULT));
903 ISC_MEMFUNC_SCOPE isc_result_t
904 isc__mem_createx2(size_t init_max_size, size_t target_size,
905 isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
906 isc_mem_t **ctxp, unsigned int flags)
911 REQUIRE(ctxp != NULL && *ctxp == NULL);
912 REQUIRE(memalloc != NULL);
913 REQUIRE(memfree != NULL);
915 INSIST((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0);
917 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
919 ctx = (memalloc)(arg, sizeof(*ctx));
921 return (ISC_R_NOMEMORY);
923 if ((flags & ISC_MEMFLAG_NOLOCK) == 0) {
924 result = isc_mutex_init(&ctx->lock);
925 if (result != ISC_R_SUCCESS) {
931 if (init_max_size == 0U)
932 ctx->max_size = DEF_MAX_SIZE;
934 ctx->max_size = init_max_size;
937 memset(ctx->name, 0, sizeof(ctx->name));
945 ctx->hi_called = ISC_FALSE;
946 ctx->is_overmem = ISC_FALSE;
948 ctx->water_arg = NULL;
949 ctx->common.impmagic = MEM_MAGIC;
950 ctx->common.magic = ISCAPI_MCTX_MAGIC;
951 ctx->common.methods = (isc_memmethods_t *)&memmethods;
952 isc_ondestroy_init(&ctx->ondestroy);
953 ctx->memalloc = memalloc;
954 ctx->memfree = memfree;
957 ctx->checkfree = ISC_TRUE;
958 #if ISC_MEM_TRACKLINES
959 ctx->debuglist = NULL;
960 ctx->debuglistcnt = 0;
962 ISC_LIST_INIT(ctx->pools);
964 ctx->freelists = NULL;
965 ctx->basic_blocks = NULL;
966 ctx->basic_table = NULL;
967 ctx->basic_table_count = 0;
968 ctx->basic_table_size = 0;
972 ctx->stats = (memalloc)(arg,
973 (ctx->max_size+1) * sizeof(struct stats));
974 if (ctx->stats == NULL) {
975 result = ISC_R_NOMEMORY;
978 memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
980 if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
981 if (target_size == 0U)
982 ctx->mem_target = DEF_MEM_TARGET;
984 ctx->mem_target = target_size;
985 ctx->freelists = (memalloc)(arg, ctx->max_size *
987 if (ctx->freelists == NULL) {
988 result = ISC_R_NOMEMORY;
991 memset(ctx->freelists, 0,
992 ctx->max_size * sizeof(element *));
995 #if ISC_MEM_TRACKLINES
996 if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) {
999 ctx->debuglist = (memalloc)(arg,
1000 (ctx->max_size+1) * sizeof(debuglist_t));
1001 if (ctx->debuglist == NULL) {
1002 result = ISC_R_NOMEMORY;
1005 for (i = 0; i <= ctx->max_size; i++)
1006 ISC_LIST_INIT(ctx->debuglist[i]);
1010 ctx->memalloc_failures = 0;
1013 ISC_LIST_INITANDAPPEND(contexts, ctx, link);
1016 *ctxp = (isc_mem_t *)ctx;
1017 return (ISC_R_SUCCESS);
1021 if (ctx->stats != NULL)
1022 (memfree)(arg, ctx->stats);
1023 if (ctx->freelists != NULL)
1024 (memfree)(arg, ctx->freelists);
1025 #if ISC_MEM_TRACKLINES
1026 if (ctx->debuglist != NULL)
1027 (ctx->memfree)(ctx->arg, ctx->debuglist);
1028 #endif /* ISC_MEM_TRACKLINES */
1029 if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
1030 DESTROYLOCK(&ctx->lock);
1031 (memfree)(arg, ctx);
1037 ISC_MEMFUNC_SCOPE isc_result_t
1038 isc__mem_create(size_t init_max_size, size_t target_size, isc_mem_t **ctxp) {
1039 return (isc__mem_createx2(init_max_size, target_size,
1040 default_memalloc, default_memfree, NULL,
1041 ctxp, ISC_MEMFLAG_DEFAULT));
1044 ISC_MEMFUNC_SCOPE isc_result_t
1045 isc__mem_create2(size_t init_max_size, size_t target_size,
1046 isc_mem_t **ctxp, unsigned int flags)
1048 return (isc__mem_createx2(init_max_size, target_size,
1049 default_memalloc, default_memfree, NULL,
1054 destroy(isc__mem_t *ctx) {
1056 isc_ondestroy_t ondest;
1059 ISC_LIST_UNLINK(contexts, ctx, link);
1060 totallost += ctx->inuse;
1063 ctx->common.impmagic = 0;
1064 ctx->common.magic = 0;
1066 INSIST(ISC_LIST_EMPTY(ctx->pools));
1068 #if ISC_MEM_TRACKLINES
1069 if (ctx->debuglist != NULL) {
1070 if (ctx->checkfree) {
1071 for (i = 0; i <= ctx->max_size; i++) {
1072 if (!ISC_LIST_EMPTY(ctx->debuglist[i]))
1073 print_active(ctx, stderr);
1074 INSIST(ISC_LIST_EMPTY(ctx->debuglist[i]));
1079 for (i = 0; i <= ctx->max_size; i++)
1080 for (dl = ISC_LIST_HEAD(ctx->debuglist[i]);
1082 dl = ISC_LIST_HEAD(ctx->debuglist[i])) {
1083 ISC_LIST_UNLINK(ctx->debuglist[i],
1088 (ctx->memfree)(ctx->arg, ctx->debuglist);
1091 INSIST(ctx->references == 0);
1093 if (ctx->checkfree) {
1094 for (i = 0; i <= ctx->max_size; i++) {
1095 #if ISC_MEM_TRACKLINES
1096 if (ctx->stats[i].gets != 0U)
1097 print_active(ctx, stderr);
1099 INSIST(ctx->stats[i].gets == 0U);
1103 (ctx->memfree)(ctx->arg, ctx->stats);
1105 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1106 for (i = 0; i < ctx->basic_table_count; i++)
1107 (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
1108 (ctx->memfree)(ctx->arg, ctx->freelists);
1109 if (ctx->basic_table != NULL)
1110 (ctx->memfree)(ctx->arg, ctx->basic_table);
1113 ondest = ctx->ondestroy;
1115 if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
1116 DESTROYLOCK(&ctx->lock);
1117 (ctx->memfree)(ctx->arg, ctx);
1119 isc_ondestroy_notify(&ondest, ctx);
1122 ISC_MEMFUNC_SCOPE void
1123 isc__mem_attach(isc_mem_t *source0, isc_mem_t **targetp) {
1124 isc__mem_t *source = (isc__mem_t *)source0;
1126 REQUIRE(VALID_CONTEXT(source));
1127 REQUIRE(targetp != NULL && *targetp == NULL);
1129 MCTXLOCK(source, &source->lock);
1130 source->references++;
1131 MCTXUNLOCK(source, &source->lock);
1133 *targetp = (isc_mem_t *)source;
1136 ISC_MEMFUNC_SCOPE void
1137 isc__mem_detach(isc_mem_t **ctxp) {
1139 isc_boolean_t want_destroy = ISC_FALSE;
1141 REQUIRE(ctxp != NULL);
1142 ctx = (isc__mem_t *)*ctxp;
1143 REQUIRE(VALID_CONTEXT(ctx));
1145 MCTXLOCK(ctx, &ctx->lock);
1146 INSIST(ctx->references > 0);
1148 if (ctx->references == 0)
1149 want_destroy = ISC_TRUE;
1150 MCTXUNLOCK(ctx, &ctx->lock);
1159 * isc_mem_putanddetach() is the equivalent of:
1162 * isc_mem_attach(ptr->mctx, &mctx);
1163 * isc_mem_detach(&ptr->mctx);
1164 * isc_mem_put(mctx, ptr, sizeof(*ptr);
1165 * isc_mem_detach(&mctx);
1168 ISC_MEMFUNC_SCOPE void
1169 isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
1171 isc_boolean_t want_destroy = ISC_FALSE;
1175 REQUIRE(ctxp != NULL);
1176 ctx = (isc__mem_t *)*ctxp;
1177 REQUIRE(VALID_CONTEXT(ctx));
1178 REQUIRE(ptr != NULL);
1181 * Must be before mem_putunlocked() as ctxp is usually within
1186 if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0) {
1187 if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
1188 si = &(((size_info *)ptr)[-1]);
1189 oldsize = si->u.size - ALIGNMENT_SIZE;
1190 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
1191 oldsize -= ALIGNMENT_SIZE;
1192 INSIST(oldsize == size);
1194 isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
1196 MCTXLOCK(ctx, &ctx->lock);
1198 if (ctx->references == 0)
1199 want_destroy = ISC_TRUE;
1200 MCTXUNLOCK(ctx, &ctx->lock);
1207 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1208 MCTXLOCK(ctx, &ctx->lock);
1209 mem_putunlocked(ctx, ptr, size);
1211 mem_put(ctx, ptr, size);
1212 MCTXLOCK(ctx, &ctx->lock);
1213 mem_putstats(ctx, ptr, size);
1216 DELETE_TRACE(ctx, ptr, size, file, line);
1217 INSIST(ctx->references > 0);
1219 if (ctx->references == 0)
1220 want_destroy = ISC_TRUE;
1222 MCTXUNLOCK(ctx, &ctx->lock);
1228 ISC_MEMFUNC_SCOPE void
1229 isc__mem_destroy(isc_mem_t **ctxp) {
1233 * This routine provides legacy support for callers who use mctxs
1234 * without attaching/detaching.
1237 REQUIRE(ctxp != NULL);
1238 ctx = (isc__mem_t *)*ctxp;
1239 REQUIRE(VALID_CONTEXT(ctx));
1241 MCTXLOCK(ctx, &ctx->lock);
1242 #if ISC_MEM_TRACKLINES
1243 if (ctx->references != 1)
1244 print_active(ctx, stderr);
1246 REQUIRE(ctx->references == 1);
1248 MCTXUNLOCK(ctx, &ctx->lock);
1255 ISC_MEMFUNC_SCOPE isc_result_t
1256 isc__mem_ondestroy(isc_mem_t *ctx0, isc_task_t *task, isc_event_t **event) {
1257 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1260 MCTXLOCK(ctx, &ctx->lock);
1261 res = isc_ondestroy_register(&ctx->ondestroy, task, event);
1262 MCTXUNLOCK(ctx, &ctx->lock);
1267 ISC_MEMFUNC_SCOPE void *
1268 isc___mem_get(isc_mem_t *ctx0, size_t size FLARG) {
1269 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1271 isc_boolean_t call_water = ISC_FALSE;
1273 REQUIRE(VALID_CONTEXT(ctx));
1275 if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0)
1276 return (isc__mem_allocate(ctx0, size FLARG_PASS));
1278 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1279 MCTXLOCK(ctx, &ctx->lock);
1280 ptr = mem_getunlocked(ctx, size);
1282 ptr = mem_get(ctx, size);
1283 MCTXLOCK(ctx, &ctx->lock);
1285 mem_getstats(ctx, size);
1288 ADD_TRACE(ctx, ptr, size, file, line);
1289 if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1291 ctx->is_overmem = ISC_TRUE;
1293 if (ctx->hi_water != 0U && !ctx->hi_called &&
1294 ctx->inuse > ctx->hi_water) {
1295 call_water = ISC_TRUE;
1297 if (ctx->inuse > ctx->maxinuse) {
1298 ctx->maxinuse = ctx->inuse;
1299 if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1300 (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
1301 fprintf(stderr, "maxinuse = %lu\n",
1302 (unsigned long)ctx->inuse);
1304 MCTXUNLOCK(ctx, &ctx->lock);
1307 (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
1312 ISC_MEMFUNC_SCOPE void
1313 isc___mem_put(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
1314 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1315 isc_boolean_t call_water = ISC_FALSE;
1319 REQUIRE(VALID_CONTEXT(ctx));
1320 REQUIRE(ptr != NULL);
1322 if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0) {
1323 if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
1324 si = &(((size_info *)ptr)[-1]);
1325 oldsize = si->u.size - ALIGNMENT_SIZE;
1326 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
1327 oldsize -= ALIGNMENT_SIZE;
1328 INSIST(oldsize == size);
1330 isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
1334 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1335 MCTXLOCK(ctx, &ctx->lock);
1336 mem_putunlocked(ctx, ptr, size);
1338 mem_put(ctx, ptr, size);
1339 MCTXLOCK(ctx, &ctx->lock);
1340 mem_putstats(ctx, ptr, size);
1343 DELETE_TRACE(ctx, ptr, size, file, line);
1346 * The check against ctx->lo_water == 0 is for the condition
1347 * when the context was pushed over hi_water but then had
1348 * isc_mem_setwater() called with 0 for hi_water and lo_water.
1350 if (ctx->is_overmem &&
1351 (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1352 ctx->is_overmem = ISC_FALSE;
1354 if (ctx->hi_called &&
1355 (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1356 if (ctx->water != NULL)
1357 call_water = ISC_TRUE;
1359 MCTXUNLOCK(ctx, &ctx->lock);
1362 (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1365 ISC_MEMFUNC_SCOPE void
1366 isc__mem_waterack(isc_mem_t *ctx0, int flag) {
1367 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1369 REQUIRE(VALID_CONTEXT(ctx));
1371 MCTXLOCK(ctx, &ctx->lock);
1372 if (flag == ISC_MEM_LOWATER)
1373 ctx->hi_called = ISC_FALSE;
1374 else if (flag == ISC_MEM_HIWATER)
1375 ctx->hi_called = ISC_TRUE;
1376 MCTXUNLOCK(ctx, &ctx->lock);
1379 #if ISC_MEM_TRACKLINES
1381 print_active(isc__mem_t *mctx, FILE *out) {
1382 if (mctx->debuglist != NULL) {
1386 isc_boolean_t found;
1388 fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1390 "Dump of all outstanding "
1391 "memory allocations:\n"));
1393 format = isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1394 ISC_MSG_PTRFILELINE,
1395 "\tptr %p size %u file %s line %u\n");
1396 for (i = 0; i <= mctx->max_size; i++) {
1397 dl = ISC_LIST_HEAD(mctx->debuglist[i]);
1402 while (dl != NULL) {
1403 for (j = 0; j < DEBUGLIST_COUNT; j++)
1404 if (dl->ptr[j] != NULL)
1405 fprintf(out, format,
1410 dl = ISC_LIST_NEXT(dl, link);
1414 fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1415 ISC_MSG_NONE, "\tNone.\n"));
1421 * Print the stats[] on the stream "out" with suitable formatting.
1423 ISC_MEMFUNC_SCOPE void
1424 isc__mem_stats(isc_mem_t *ctx0, FILE *out) {
1425 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1427 const struct stats *s;
1428 const isc__mempool_t *pool;
1430 REQUIRE(VALID_CONTEXT(ctx));
1431 MCTXLOCK(ctx, &ctx->lock);
1433 for (i = 0; i <= ctx->max_size; i++) {
1436 if (s->totalgets == 0U && s->gets == 0U)
1438 fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
1439 (i == ctx->max_size) ? ">=" : " ",
1440 (unsigned long) i, s->totalgets, s->gets);
1441 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
1442 (s->blocks != 0U || s->freefrags != 0U))
1443 fprintf(out, " (%lu bl, %lu ff)",
1444 s->blocks, s->freefrags);
1449 * Note that since a pool can be locked now, these stats might be
1450 * somewhat off if the pool is in active use at the time the stats
1451 * are dumped. The link fields are protected by the isc_mem_t's
1452 * lock, however, so walking this list and extracting integers from
1453 * stats fields is always safe.
1455 pool = ISC_LIST_HEAD(ctx->pools);
1457 fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1459 "[Pool statistics]\n"));
1460 fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
1461 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1462 ISC_MSG_POOLNAME, "name"),
1463 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1464 ISC_MSG_POOLSIZE, "size"),
1465 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1466 ISC_MSG_POOLMAXALLOC, "maxalloc"),
1467 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1468 ISC_MSG_POOLALLOCATED, "allocated"),
1469 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1470 ISC_MSG_POOLFREECOUNT, "freecount"),
1471 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1472 ISC_MSG_POOLFREEMAX, "freemax"),
1473 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1474 ISC_MSG_POOLFILLCOUNT, "fillcount"),
1475 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1476 ISC_MSG_POOLGETS, "gets"),
1479 while (pool != NULL) {
1480 fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
1481 pool->name, (unsigned long) pool->size, pool->maxalloc,
1482 pool->allocated, pool->freecount, pool->freemax,
1483 pool->fillcount, pool->gets,
1484 (pool->lock == NULL ? "N" : "Y"));
1485 pool = ISC_LIST_NEXT(pool, link);
1488 #if ISC_MEM_TRACKLINES
1489 print_active(ctx, out);
1492 MCTXUNLOCK(ctx, &ctx->lock);
1496 * Replacements for malloc() and free() -- they implicitly remember the
1497 * size of the object allocated (with some additional overhead).
1501 isc__mem_allocateunlocked(isc_mem_t *ctx0, size_t size) {
1502 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1505 size += ALIGNMENT_SIZE;
1506 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
1507 size += ALIGNMENT_SIZE;
1509 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0)
1510 si = mem_getunlocked(ctx, size);
1512 si = mem_get(ctx, size);
1516 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
1524 ISC_MEMFUNC_SCOPE void *
1525 isc___mem_allocate(isc_mem_t *ctx0, size_t size FLARG) {
1526 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1528 isc_boolean_t call_water = ISC_FALSE;
1530 REQUIRE(VALID_CONTEXT(ctx));
1532 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1533 MCTXLOCK(ctx, &ctx->lock);
1534 si = isc__mem_allocateunlocked((isc_mem_t *)ctx, size);
1536 si = isc__mem_allocateunlocked((isc_mem_t *)ctx, size);
1537 MCTXLOCK(ctx, &ctx->lock);
1539 mem_getstats(ctx, si[-1].u.size);
1542 #if ISC_MEM_TRACKLINES
1543 ADD_TRACE(ctx, si, si[-1].u.size, file, line);
1545 if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1547 ctx->is_overmem = ISC_TRUE;
1550 if (ctx->hi_water != 0U && !ctx->hi_called &&
1551 ctx->inuse > ctx->hi_water) {
1552 ctx->hi_called = ISC_TRUE;
1553 call_water = ISC_TRUE;
1555 if (ctx->inuse > ctx->maxinuse) {
1556 ctx->maxinuse = ctx->inuse;
1557 if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1558 (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
1559 fprintf(stderr, "maxinuse = %lu\n",
1560 (unsigned long)ctx->inuse);
1562 MCTXUNLOCK(ctx, &ctx->lock);
1565 (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
1570 ISC_MEMFUNC_SCOPE void *
1571 isc___mem_reallocate(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
1572 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1573 void *new_ptr = NULL;
1574 size_t oldsize, copysize;
1576 REQUIRE(VALID_CONTEXT(ctx));
1579 * This function emulates the realloc(3) standard library function:
1580 * - if size > 0, allocate new memory; and if ptr is non NULL, copy
1581 * as much of the old contents to the new buffer and free the old one.
1582 * Note that when allocation fails the original pointer is intact;
1583 * the caller must free it.
1584 * - if size is 0 and ptr is non NULL, simply free the given ptr.
1585 * - this function returns:
1586 * pointer to the newly allocated memory, or
1587 * NULL if allocation fails or doesn't happen.
1590 new_ptr = isc__mem_allocate(ctx0, size FLARG_PASS);
1591 if (new_ptr != NULL && ptr != NULL) {
1592 oldsize = (((size_info *)ptr)[-1]).u.size;
1593 INSIST(oldsize >= ALIGNMENT_SIZE);
1594 oldsize -= ALIGNMENT_SIZE;
1595 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
1596 INSIST(oldsize >= ALIGNMENT_SIZE);
1597 oldsize -= ALIGNMENT_SIZE;
1599 copysize = (oldsize > size) ? size : oldsize;
1600 memcpy(new_ptr, ptr, copysize);
1601 isc__mem_free(ctx0, ptr FLARG_PASS);
1603 } else if (ptr != NULL)
1604 isc__mem_free(ctx0, ptr FLARG_PASS);
1609 ISC_MEMFUNC_SCOPE void
1610 isc___mem_free(isc_mem_t *ctx0, void *ptr FLARG) {
1611 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1614 isc_boolean_t call_water= ISC_FALSE;
1616 REQUIRE(VALID_CONTEXT(ctx));
1617 REQUIRE(ptr != NULL);
1619 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
1620 si = &(((size_info *)ptr)[-2]);
1621 REQUIRE(si->u.ctx == ctx);
1622 size = si[1].u.size;
1624 si = &(((size_info *)ptr)[-1]);
1628 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1629 MCTXLOCK(ctx, &ctx->lock);
1630 mem_putunlocked(ctx, si, size);
1632 mem_put(ctx, si, size);
1633 MCTXLOCK(ctx, &ctx->lock);
1634 mem_putstats(ctx, si, size);
1637 DELETE_TRACE(ctx, ptr, size, file, line);
1640 * The check against ctx->lo_water == 0 is for the condition
1641 * when the context was pushed over hi_water but then had
1642 * isc_mem_setwater() called with 0 for hi_water and lo_water.
1644 if (ctx->is_overmem &&
1645 (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1646 ctx->is_overmem = ISC_FALSE;
1649 if (ctx->hi_called &&
1650 (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1651 ctx->hi_called = ISC_FALSE;
1653 if (ctx->water != NULL)
1654 call_water = ISC_TRUE;
1656 MCTXUNLOCK(ctx, &ctx->lock);
1659 (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1664 * Other useful things.
1667 ISC_MEMFUNC_SCOPE char *
1668 isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
1669 isc__mem_t *mctx = (isc__mem_t *)mctx0;
1673 REQUIRE(VALID_CONTEXT(mctx));
1678 ns = isc___mem_allocate((isc_mem_t *)mctx, len + 1 FLARG_PASS);
1681 strncpy(ns, s, len + 1);
1686 ISC_MEMFUNC_SCOPE void
1687 isc__mem_setdestroycheck(isc_mem_t *ctx0, isc_boolean_t flag) {
1688 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1690 REQUIRE(VALID_CONTEXT(ctx));
1691 MCTXLOCK(ctx, &ctx->lock);
1693 ctx->checkfree = flag;
1695 MCTXUNLOCK(ctx, &ctx->lock);
1702 ISC_MEMFUNC_SCOPE void
1703 isc__mem_setquota(isc_mem_t *ctx0, size_t quota) {
1704 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1706 REQUIRE(VALID_CONTEXT(ctx));
1707 MCTXLOCK(ctx, &ctx->lock);
1711 MCTXUNLOCK(ctx, &ctx->lock);
1714 ISC_MEMFUNC_SCOPE size_t
1715 isc__mem_getquota(isc_mem_t *ctx0) {
1716 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1719 REQUIRE(VALID_CONTEXT(ctx));
1720 MCTXLOCK(ctx, &ctx->lock);
1724 MCTXUNLOCK(ctx, &ctx->lock);
1729 ISC_MEMFUNC_SCOPE size_t
1730 isc__mem_inuse(isc_mem_t *ctx0) {
1731 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1734 REQUIRE(VALID_CONTEXT(ctx));
1735 MCTXLOCK(ctx, &ctx->lock);
1739 MCTXUNLOCK(ctx, &ctx->lock);
1744 ISC_MEMFUNC_SCOPE void
1745 isc__mem_setwater(isc_mem_t *ctx0, isc_mem_water_t water, void *water_arg,
1746 size_t hiwater, size_t lowater)
1748 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1749 isc_boolean_t callwater = ISC_FALSE;
1750 isc_mem_water_t oldwater;
1753 REQUIRE(VALID_CONTEXT(ctx));
1754 REQUIRE(hiwater >= lowater);
1756 MCTXLOCK(ctx, &ctx->lock);
1757 oldwater = ctx->water;
1758 oldwater_arg = ctx->water_arg;
1759 if (water == NULL) {
1760 callwater = ctx->hi_called;
1762 ctx->water_arg = NULL;
1765 ctx->hi_called = ISC_FALSE;
1767 if (ctx->hi_called &&
1768 (ctx->water != water || ctx->water_arg != water_arg ||
1769 ctx->inuse < lowater || lowater == 0U))
1770 callwater = ISC_TRUE;
1772 ctx->water_arg = water_arg;
1773 ctx->hi_water = hiwater;
1774 ctx->lo_water = lowater;
1775 ctx->hi_called = ISC_FALSE;
1777 MCTXUNLOCK(ctx, &ctx->lock);
1779 if (callwater && oldwater != NULL)
1780 (oldwater)(oldwater_arg, ISC_MEM_LOWATER);
1783 ISC_MEMFUNC_SCOPE isc_boolean_t
1784 isc__mem_isovermem(isc_mem_t *ctx0) {
1785 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1787 REQUIRE(VALID_CONTEXT(ctx));
1790 * We don't bother to lock the context because 100% accuracy isn't
1791 * necessary (and even if we locked the context the returned value
1792 * could be different from the actual state when it's used anyway)
1794 return (ctx->is_overmem);
1797 ISC_MEMFUNC_SCOPE void
1798 isc__mem_setname(isc_mem_t *ctx0, const char *name, void *tag) {
1799 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1801 REQUIRE(VALID_CONTEXT(ctx));
1804 memset(ctx->name, 0, sizeof(ctx->name));
1805 strncpy(ctx->name, name, sizeof(ctx->name) - 1);
1810 ISC_MEMFUNC_SCOPE const char *
1811 isc__mem_getname(isc_mem_t *ctx0) {
1812 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1814 REQUIRE(VALID_CONTEXT(ctx));
1819 ISC_MEMFUNC_SCOPE void *
1820 isc__mem_gettag(isc_mem_t *ctx0) {
1821 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1823 REQUIRE(VALID_CONTEXT(ctx));
1832 ISC_MEMFUNC_SCOPE isc_result_t
1833 isc__mempool_create(isc_mem_t *mctx0, size_t size, isc_mempool_t **mpctxp) {
1834 isc__mem_t *mctx = (isc__mem_t *)mctx0;
1835 isc__mempool_t *mpctx;
1837 REQUIRE(VALID_CONTEXT(mctx));
1839 REQUIRE(mpctxp != NULL && *mpctxp == NULL);
1842 * Allocate space for this pool, initialize values, and if all works
1843 * well, attach to the memory context.
1845 mpctx = isc_mem_get((isc_mem_t *)mctx, sizeof(isc__mempool_t));
1847 return (ISC_R_NOMEMORY);
1849 mpctx->common.methods = (isc_mempoolmethods_t *)&mempoolmethods;
1850 mpctx->common.impmagic = MEMPOOL_MAGIC;
1851 mpctx->common.magic = ISCAPI_MPOOL_MAGIC;
1855 mpctx->maxalloc = UINT_MAX;
1856 mpctx->allocated = 0;
1857 mpctx->freecount = 0;
1859 mpctx->fillcount = 1;
1861 #if ISC_MEMPOOL_NAMES
1864 mpctx->items = NULL;
1866 *mpctxp = (isc_mempool_t *)mpctx;
1868 MCTXLOCK(mctx, &mctx->lock);
1869 ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
1871 MCTXUNLOCK(mctx, &mctx->lock);
1873 return (ISC_R_SUCCESS);
1876 ISC_MEMFUNC_SCOPE void
1877 isc__mempool_setname(isc_mempool_t *mpctx0, const char *name) {
1878 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1880 REQUIRE(name != NULL);
1881 REQUIRE(VALID_MEMPOOL(mpctx));
1883 #if ISC_MEMPOOL_NAMES
1884 if (mpctx->lock != NULL)
1887 strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
1888 mpctx->name[sizeof(mpctx->name) - 1] = '\0';
1890 if (mpctx->lock != NULL)
1891 UNLOCK(mpctx->lock);
1898 ISC_MEMFUNC_SCOPE void
1899 isc__mempool_destroy(isc_mempool_t **mpctxp) {
1900 isc__mempool_t *mpctx;
1905 REQUIRE(mpctxp != NULL);
1906 mpctx = (isc__mempool_t *)*mpctxp;
1907 REQUIRE(VALID_MEMPOOL(mpctx));
1908 #if ISC_MEMPOOL_NAMES
1909 if (mpctx->allocated > 0)
1910 UNEXPECTED_ERROR(__FILE__, __LINE__,
1911 "isc__mempool_destroy(): mempool %s "
1915 REQUIRE(mpctx->allocated == 0);
1925 * Return any items on the free list
1927 MCTXLOCK(mctx, &mctx->lock);
1928 while (mpctx->items != NULL) {
1929 INSIST(mpctx->freecount > 0);
1931 item = mpctx->items;
1932 mpctx->items = item->next;
1934 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1935 mem_putunlocked(mctx, item, mpctx->size);
1937 mem_put(mctx, item, mpctx->size);
1938 mem_putstats(mctx, item, mpctx->size);
1941 MCTXUNLOCK(mctx, &mctx->lock);
1944 * Remove our linked list entry from the memory context.
1946 MCTXLOCK(mctx, &mctx->lock);
1947 ISC_LIST_UNLINK(mctx->pools, mpctx, link);
1949 MCTXUNLOCK(mctx, &mctx->lock);
1951 mpctx->common.impmagic = 0;
1952 mpctx->common.magic = 0;
1954 isc_mem_put((isc_mem_t *)mpctx->mctx, mpctx, sizeof(isc__mempool_t));
1962 ISC_MEMFUNC_SCOPE void
1963 isc__mempool_associatelock(isc_mempool_t *mpctx0, isc_mutex_t *lock) {
1964 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1966 REQUIRE(VALID_MEMPOOL(mpctx));
1967 REQUIRE(mpctx->lock == NULL);
1968 REQUIRE(lock != NULL);
1973 ISC_MEMFUNC_SCOPE void *
1974 isc___mempool_get(isc_mempool_t *mpctx0 FLARG) {
1975 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1980 REQUIRE(VALID_MEMPOOL(mpctx));
1984 if (mpctx->lock != NULL)
1988 * Don't let the caller go over quota
1990 if (mpctx->allocated >= mpctx->maxalloc) {
1996 * if we have a free list item, return the first here
1998 item = mpctx->items;
2000 mpctx->items = item->next;
2001 INSIST(mpctx->freecount > 0);
2009 * We need to dip into the well. Lock the memory context here and
2010 * fill up our free list.
2012 MCTXLOCK(mctx, &mctx->lock);
2013 for (i = 0; i < mpctx->fillcount; i++) {
2014 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2015 item = mem_getunlocked(mctx, mpctx->size);
2017 item = mem_get(mctx, mpctx->size);
2019 mem_getstats(mctx, mpctx->size);
2023 item->next = mpctx->items;
2024 mpctx->items = item;
2027 MCTXUNLOCK(mctx, &mctx->lock);
2030 * If we didn't get any items, return NULL.
2032 item = mpctx->items;
2036 mpctx->items = item->next;
2042 if (mpctx->lock != NULL)
2043 UNLOCK(mpctx->lock);
2045 #if ISC_MEM_TRACKLINES
2047 MCTXLOCK(mctx, &mctx->lock);
2048 ADD_TRACE(mctx, item, mpctx->size, file, line);
2049 MCTXUNLOCK(mctx, &mctx->lock);
2051 #endif /* ISC_MEM_TRACKLINES */
2056 ISC_MEMFUNC_SCOPE void
2057 isc___mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
2058 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2062 REQUIRE(VALID_MEMPOOL(mpctx));
2063 REQUIRE(mem != NULL);
2067 if (mpctx->lock != NULL)
2070 INSIST(mpctx->allocated > 0);
2073 #if ISC_MEM_TRACKLINES
2074 MCTXLOCK(mctx, &mctx->lock);
2075 DELETE_TRACE(mctx, mem, mpctx->size, file, line);
2076 MCTXUNLOCK(mctx, &mctx->lock);
2077 #endif /* ISC_MEM_TRACKLINES */
2080 * If our free list is full, return this to the mctx directly.
2082 if (mpctx->freecount >= mpctx->freemax) {
2083 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2084 MCTXLOCK(mctx, &mctx->lock);
2085 mem_putunlocked(mctx, mem, mpctx->size);
2086 MCTXUNLOCK(mctx, &mctx->lock);
2088 mem_put(mctx, mem, mpctx->size);
2089 MCTXLOCK(mctx, &mctx->lock);
2090 mem_putstats(mctx, mem, mpctx->size);
2091 MCTXUNLOCK(mctx, &mctx->lock);
2093 if (mpctx->lock != NULL)
2094 UNLOCK(mpctx->lock);
2099 * Otherwise, attach it to our free list and bump the counter.
2102 item = (element *)mem;
2103 item->next = mpctx->items;
2104 mpctx->items = item;
2106 if (mpctx->lock != NULL)
2107 UNLOCK(mpctx->lock);
2114 ISC_MEMFUNC_SCOPE void
2115 isc__mempool_setfreemax(isc_mempool_t *mpctx0, unsigned int limit) {
2116 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2118 REQUIRE(VALID_MEMPOOL(mpctx));
2120 if (mpctx->lock != NULL)
2123 mpctx->freemax = limit;
2125 if (mpctx->lock != NULL)
2126 UNLOCK(mpctx->lock);
2129 ISC_MEMFUNC_SCOPE unsigned int
2130 isc__mempool_getfreemax(isc_mempool_t *mpctx0) {
2131 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2132 unsigned int freemax;
2134 REQUIRE(VALID_MEMPOOL(mpctx));
2136 if (mpctx->lock != NULL)
2139 freemax = mpctx->freemax;
2141 if (mpctx->lock != NULL)
2142 UNLOCK(mpctx->lock);
2147 ISC_MEMFUNC_SCOPE unsigned int
2148 isc__mempool_getfreecount(isc_mempool_t *mpctx0) {
2149 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2150 unsigned int freecount;
2152 REQUIRE(VALID_MEMPOOL(mpctx));
2154 if (mpctx->lock != NULL)
2157 freecount = mpctx->freecount;
2159 if (mpctx->lock != NULL)
2160 UNLOCK(mpctx->lock);
2165 ISC_MEMFUNC_SCOPE void
2166 isc__mempool_setmaxalloc(isc_mempool_t *mpctx0, unsigned int limit) {
2167 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2171 REQUIRE(VALID_MEMPOOL(mpctx));
2173 if (mpctx->lock != NULL)
2176 mpctx->maxalloc = limit;
2178 if (mpctx->lock != NULL)
2179 UNLOCK(mpctx->lock);
2182 ISC_MEMFUNC_SCOPE unsigned int
2183 isc__mempool_getmaxalloc(isc_mempool_t *mpctx0) {
2184 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2185 unsigned int maxalloc;
2187 REQUIRE(VALID_MEMPOOL(mpctx));
2189 if (mpctx->lock != NULL)
2192 maxalloc = mpctx->maxalloc;
2194 if (mpctx->lock != NULL)
2195 UNLOCK(mpctx->lock);
2200 ISC_MEMFUNC_SCOPE unsigned int
2201 isc__mempool_getallocated(isc_mempool_t *mpctx0) {
2202 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2203 unsigned int allocated;
2205 REQUIRE(VALID_MEMPOOL(mpctx));
2207 if (mpctx->lock != NULL)
2210 allocated = mpctx->allocated;
2212 if (mpctx->lock != NULL)
2213 UNLOCK(mpctx->lock);
2218 ISC_MEMFUNC_SCOPE void
2219 isc__mempool_setfillcount(isc_mempool_t *mpctx0, unsigned int limit) {
2220 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2223 REQUIRE(VALID_MEMPOOL(mpctx));
2225 if (mpctx->lock != NULL)
2228 mpctx->fillcount = limit;
2230 if (mpctx->lock != NULL)
2231 UNLOCK(mpctx->lock);
2234 ISC_MEMFUNC_SCOPE unsigned int
2235 isc__mempool_getfillcount(isc_mempool_t *mpctx0) {
2236 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2238 unsigned int fillcount;
2240 REQUIRE(VALID_MEMPOOL(mpctx));
2242 if (mpctx->lock != NULL)
2245 fillcount = mpctx->fillcount;
2247 if (mpctx->lock != NULL)
2248 UNLOCK(mpctx->lock);
2253 #ifdef USE_MEMIMPREGISTER
2255 isc__mem_register() {
2256 return (isc_mem_register(isc__mem_create2));
2261 ISC_MEMFUNC_SCOPE void
2262 isc__mem_printactive(isc_mem_t *ctx0, FILE *file) {
2263 isc__mem_t *ctx = (isc__mem_t *)ctx0;
2265 REQUIRE(VALID_CONTEXT(ctx));
2266 REQUIRE(file != NULL);
2268 #if !ISC_MEM_TRACKLINES
2272 print_active(ctx, file);
2276 ISC_MEMFUNC_SCOPE void
2277 isc__mem_printallactive(FILE *file) {
2278 #if !ISC_MEM_TRACKLINES
2283 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2286 for (ctx = ISC_LIST_HEAD(contexts);
2288 ctx = ISC_LIST_NEXT(ctx, link)) {
2289 fprintf(file, "context: %p\n", ctx);
2290 print_active(ctx, file);
2296 ISC_MEMFUNC_SCOPE void
2297 isc__mem_checkdestroyed(FILE *file) {
2299 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2302 if (!ISC_LIST_EMPTY(contexts)) {
2303 #if ISC_MEM_TRACKLINES
2306 for (ctx = ISC_LIST_HEAD(contexts);
2308 ctx = ISC_LIST_NEXT(ctx, link)) {
2309 fprintf(file, "context: %p\n", ctx);
2310 print_active(ctx, file);
2319 ISC_MEMFUNC_SCOPE unsigned int
2320 isc_mem_references(isc_mem_t *ctx0) {
2321 isc__mem_t *ctx = (isc__mem_t *)ctx0;
2322 unsigned int references;
2324 REQUIRE(VALID_CONTEXT(ctx));
2326 MCTXLOCK(ctx, &ctx->lock);
2327 references = ctx->references;
2328 MCTXUNLOCK(ctx, &ctx->lock);
2330 return (references);
2335 typedef struct summarystat {
2338 isc_uint64_t blocksize;
2339 isc_uint64_t contextsize;
2343 renderctx(isc__mem_t *ctx, summarystat_t *summary, xmlTextWriterPtr writer) {
2344 REQUIRE(VALID_CONTEXT(ctx));
2346 xmlTextWriterStartElement(writer, ISC_XMLCHAR "context");
2348 xmlTextWriterStartElement(writer, ISC_XMLCHAR "id");
2349 xmlTextWriterWriteFormatString(writer, "%p", ctx);
2350 xmlTextWriterEndElement(writer); /* id */
2352 if (ctx->name[0] != 0) {
2353 xmlTextWriterStartElement(writer, ISC_XMLCHAR "name");
2354 xmlTextWriterWriteFormatString(writer, "%s", ctx->name);
2355 xmlTextWriterEndElement(writer); /* name */
2358 REQUIRE(VALID_CONTEXT(ctx));
2359 MCTXLOCK(ctx, &ctx->lock);
2361 summary->contextsize += sizeof(*ctx) +
2362 (ctx->max_size + 1) * sizeof(struct stats) +
2363 ctx->max_size * sizeof(element *) +
2364 ctx->basic_table_count * sizeof(char *);
2365 #if ISC_MEM_TRACKLINES
2366 if (ctx->debuglist != NULL) {
2367 summary->contextsize +=
2368 (ctx->max_size + 1) * sizeof(debuglist_t) +
2369 ctx->debuglistcnt * sizeof(debuglink_t);
2372 xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
2373 xmlTextWriterWriteFormatString(writer, "%d", ctx->references);
2374 xmlTextWriterEndElement(writer); /* references */
2376 summary->total += ctx->total;
2377 xmlTextWriterStartElement(writer, ISC_XMLCHAR "total");
2378 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2379 (isc_uint64_t)ctx->total);
2380 xmlTextWriterEndElement(writer); /* total */
2382 summary->inuse += ctx->inuse;
2383 xmlTextWriterStartElement(writer, ISC_XMLCHAR "inuse");
2384 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2385 (isc_uint64_t)ctx->inuse);
2386 xmlTextWriterEndElement(writer); /* inuse */
2388 xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxinuse");
2389 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2390 (isc_uint64_t)ctx->maxinuse);
2391 xmlTextWriterEndElement(writer); /* maxinuse */
2393 xmlTextWriterStartElement(writer, ISC_XMLCHAR "blocksize");
2394 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2395 summary->blocksize += ctx->basic_table_count *
2396 NUM_BASIC_BLOCKS * ctx->mem_target;
2397 xmlTextWriterWriteFormatString(writer,
2398 "%" ISC_PRINT_QUADFORMAT "u",
2400 ctx->basic_table_count *
2404 xmlTextWriterWriteFormatString(writer, "%s", "-");
2405 xmlTextWriterEndElement(writer); /* blocksize */
2407 xmlTextWriterStartElement(writer, ISC_XMLCHAR "pools");
2408 xmlTextWriterWriteFormatString(writer, "%u", ctx->poolcnt);
2409 xmlTextWriterEndElement(writer); /* pools */
2410 summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
2412 xmlTextWriterStartElement(writer, ISC_XMLCHAR "hiwater");
2413 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2414 (isc_uint64_t)ctx->hi_water);
2415 xmlTextWriterEndElement(writer); /* hiwater */
2417 xmlTextWriterStartElement(writer, ISC_XMLCHAR "lowater");
2418 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2419 (isc_uint64_t)ctx->lo_water);
2420 xmlTextWriterEndElement(writer); /* lowater */
2422 MCTXUNLOCK(ctx, &ctx->lock);
2424 xmlTextWriterEndElement(writer); /* context */
2428 isc_mem_renderxml(xmlTextWriterPtr writer) {
2430 summarystat_t summary;
2433 memset(&summary, 0, sizeof(summary));
2435 xmlTextWriterStartElement(writer, ISC_XMLCHAR "contexts");
2437 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2441 for (ctx = ISC_LIST_HEAD(contexts);
2443 ctx = ISC_LIST_NEXT(ctx, link)) {
2444 renderctx(ctx, &summary, writer);
2448 xmlTextWriterEndElement(writer); /* contexts */
2450 xmlTextWriterStartElement(writer, ISC_XMLCHAR "summary");
2452 xmlTextWriterStartElement(writer, ISC_XMLCHAR "TotalUse");
2453 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2455 xmlTextWriterEndElement(writer); /* TotalUse */
2457 xmlTextWriterStartElement(writer, ISC_XMLCHAR "InUse");
2458 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2460 xmlTextWriterEndElement(writer); /* InUse */
2462 xmlTextWriterStartElement(writer, ISC_XMLCHAR "BlockSize");
2463 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2465 xmlTextWriterEndElement(writer); /* BlockSize */
2467 xmlTextWriterStartElement(writer, ISC_XMLCHAR "ContextSize");
2468 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2469 summary.contextsize);
2470 xmlTextWriterEndElement(writer); /* ContextSize */
2472 xmlTextWriterStartElement(writer, ISC_XMLCHAR "Lost");
2473 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2475 xmlTextWriterEndElement(writer); /* Lost */
2477 xmlTextWriterEndElement(writer); /* summary */
2480 #endif /* HAVE_LIBXML2 */