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);
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);
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 copysize = oldsize > size ? size : oldsize;
1596 memcpy(new_ptr, ptr, copysize);
1597 isc__mem_free(ctx0, ptr FLARG_PASS);
1599 } else if (ptr != NULL)
1600 isc__mem_free(ctx0, ptr FLARG_PASS);
1605 ISC_MEMFUNC_SCOPE void
1606 isc___mem_free(isc_mem_t *ctx0, void *ptr FLARG) {
1607 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1610 isc_boolean_t call_water= ISC_FALSE;
1612 REQUIRE(VALID_CONTEXT(ctx));
1613 REQUIRE(ptr != NULL);
1615 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
1616 si = &(((size_info *)ptr)[-2]);
1617 REQUIRE(si->u.ctx == ctx);
1618 size = si[1].u.size;
1620 si = &(((size_info *)ptr)[-1]);
1624 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1625 MCTXLOCK(ctx, &ctx->lock);
1626 mem_putunlocked(ctx, si, size);
1628 mem_put(ctx, si, size);
1629 MCTXLOCK(ctx, &ctx->lock);
1630 mem_putstats(ctx, si, size);
1633 DELETE_TRACE(ctx, ptr, size, file, line);
1636 * The check against ctx->lo_water == 0 is for the condition
1637 * when the context was pushed over hi_water but then had
1638 * isc_mem_setwater() called with 0 for hi_water and lo_water.
1640 if (ctx->is_overmem &&
1641 (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1642 ctx->is_overmem = ISC_FALSE;
1645 if (ctx->hi_called &&
1646 (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1647 ctx->hi_called = ISC_FALSE;
1649 if (ctx->water != NULL)
1650 call_water = ISC_TRUE;
1652 MCTXUNLOCK(ctx, &ctx->lock);
1655 (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1660 * Other useful things.
1663 ISC_MEMFUNC_SCOPE char *
1664 isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
1665 isc__mem_t *mctx = (isc__mem_t *)mctx0;
1669 REQUIRE(VALID_CONTEXT(mctx));
1674 ns = isc___mem_allocate((isc_mem_t *)mctx, len + 1 FLARG_PASS);
1677 strncpy(ns, s, len + 1);
1682 ISC_MEMFUNC_SCOPE void
1683 isc__mem_setdestroycheck(isc_mem_t *ctx0, isc_boolean_t flag) {
1684 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1686 REQUIRE(VALID_CONTEXT(ctx));
1687 MCTXLOCK(ctx, &ctx->lock);
1689 ctx->checkfree = flag;
1691 MCTXUNLOCK(ctx, &ctx->lock);
1698 ISC_MEMFUNC_SCOPE void
1699 isc__mem_setquota(isc_mem_t *ctx0, size_t quota) {
1700 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1702 REQUIRE(VALID_CONTEXT(ctx));
1703 MCTXLOCK(ctx, &ctx->lock);
1707 MCTXUNLOCK(ctx, &ctx->lock);
1710 ISC_MEMFUNC_SCOPE size_t
1711 isc__mem_getquota(isc_mem_t *ctx0) {
1712 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1715 REQUIRE(VALID_CONTEXT(ctx));
1716 MCTXLOCK(ctx, &ctx->lock);
1720 MCTXUNLOCK(ctx, &ctx->lock);
1725 ISC_MEMFUNC_SCOPE size_t
1726 isc__mem_inuse(isc_mem_t *ctx0) {
1727 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1730 REQUIRE(VALID_CONTEXT(ctx));
1731 MCTXLOCK(ctx, &ctx->lock);
1735 MCTXUNLOCK(ctx, &ctx->lock);
1740 ISC_MEMFUNC_SCOPE void
1741 isc__mem_setwater(isc_mem_t *ctx0, isc_mem_water_t water, void *water_arg,
1742 size_t hiwater, size_t lowater)
1744 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1745 isc_boolean_t callwater = ISC_FALSE;
1746 isc_mem_water_t oldwater;
1749 REQUIRE(VALID_CONTEXT(ctx));
1750 REQUIRE(hiwater >= lowater);
1752 MCTXLOCK(ctx, &ctx->lock);
1753 oldwater = ctx->water;
1754 oldwater_arg = ctx->water_arg;
1755 if (water == NULL) {
1756 callwater = ctx->hi_called;
1758 ctx->water_arg = NULL;
1761 ctx->hi_called = ISC_FALSE;
1763 if (ctx->hi_called &&
1764 (ctx->water != water || ctx->water_arg != water_arg ||
1765 ctx->inuse < lowater || lowater == 0U))
1766 callwater = ISC_TRUE;
1768 ctx->water_arg = water_arg;
1769 ctx->hi_water = hiwater;
1770 ctx->lo_water = lowater;
1771 ctx->hi_called = ISC_FALSE;
1773 MCTXUNLOCK(ctx, &ctx->lock);
1775 if (callwater && oldwater != NULL)
1776 (oldwater)(oldwater_arg, ISC_MEM_LOWATER);
1779 ISC_MEMFUNC_SCOPE isc_boolean_t
1780 isc__mem_isovermem(isc_mem_t *ctx0) {
1781 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1783 REQUIRE(VALID_CONTEXT(ctx));
1786 * We don't bother to lock the context because 100% accuracy isn't
1787 * necessary (and even if we locked the context the returned value
1788 * could be different from the actual state when it's used anyway)
1790 return (ctx->is_overmem);
1793 ISC_MEMFUNC_SCOPE void
1794 isc__mem_setname(isc_mem_t *ctx0, const char *name, void *tag) {
1795 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1797 REQUIRE(VALID_CONTEXT(ctx));
1800 memset(ctx->name, 0, sizeof(ctx->name));
1801 strncpy(ctx->name, name, sizeof(ctx->name) - 1);
1806 ISC_MEMFUNC_SCOPE const char *
1807 isc__mem_getname(isc_mem_t *ctx0) {
1808 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1810 REQUIRE(VALID_CONTEXT(ctx));
1815 ISC_MEMFUNC_SCOPE void *
1816 isc__mem_gettag(isc_mem_t *ctx0) {
1817 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1819 REQUIRE(VALID_CONTEXT(ctx));
1828 ISC_MEMFUNC_SCOPE isc_result_t
1829 isc__mempool_create(isc_mem_t *mctx0, size_t size, isc_mempool_t **mpctxp) {
1830 isc__mem_t *mctx = (isc__mem_t *)mctx0;
1831 isc__mempool_t *mpctx;
1833 REQUIRE(VALID_CONTEXT(mctx));
1835 REQUIRE(mpctxp != NULL && *mpctxp == NULL);
1838 * Allocate space for this pool, initialize values, and if all works
1839 * well, attach to the memory context.
1841 mpctx = isc_mem_get((isc_mem_t *)mctx, sizeof(isc__mempool_t));
1843 return (ISC_R_NOMEMORY);
1845 mpctx->common.methods = (isc_mempoolmethods_t *)&mempoolmethods;
1846 mpctx->common.impmagic = MEMPOOL_MAGIC;
1847 mpctx->common.magic = ISCAPI_MPOOL_MAGIC;
1851 mpctx->maxalloc = UINT_MAX;
1852 mpctx->allocated = 0;
1853 mpctx->freecount = 0;
1855 mpctx->fillcount = 1;
1857 #if ISC_MEMPOOL_NAMES
1860 mpctx->items = NULL;
1862 *mpctxp = (isc_mempool_t *)mpctx;
1864 MCTXLOCK(mctx, &mctx->lock);
1865 ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
1867 MCTXUNLOCK(mctx, &mctx->lock);
1869 return (ISC_R_SUCCESS);
1872 ISC_MEMFUNC_SCOPE void
1873 isc__mempool_setname(isc_mempool_t *mpctx0, const char *name) {
1874 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1876 REQUIRE(name != NULL);
1877 REQUIRE(VALID_MEMPOOL(mpctx));
1879 #if ISC_MEMPOOL_NAMES
1880 if (mpctx->lock != NULL)
1883 strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
1884 mpctx->name[sizeof(mpctx->name) - 1] = '\0';
1886 if (mpctx->lock != NULL)
1887 UNLOCK(mpctx->lock);
1894 ISC_MEMFUNC_SCOPE void
1895 isc__mempool_destroy(isc_mempool_t **mpctxp) {
1896 isc__mempool_t *mpctx;
1901 REQUIRE(mpctxp != NULL);
1902 mpctx = (isc__mempool_t *)*mpctxp;
1903 REQUIRE(VALID_MEMPOOL(mpctx));
1904 #if ISC_MEMPOOL_NAMES
1905 if (mpctx->allocated > 0)
1906 UNEXPECTED_ERROR(__FILE__, __LINE__,
1907 "isc__mempool_destroy(): mempool %s "
1911 REQUIRE(mpctx->allocated == 0);
1921 * Return any items on the free list
1923 MCTXLOCK(mctx, &mctx->lock);
1924 while (mpctx->items != NULL) {
1925 INSIST(mpctx->freecount > 0);
1927 item = mpctx->items;
1928 mpctx->items = item->next;
1930 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1931 mem_putunlocked(mctx, item, mpctx->size);
1933 mem_put(mctx, item, mpctx->size);
1934 mem_putstats(mctx, item, mpctx->size);
1937 MCTXUNLOCK(mctx, &mctx->lock);
1940 * Remove our linked list entry from the memory context.
1942 MCTXLOCK(mctx, &mctx->lock);
1943 ISC_LIST_UNLINK(mctx->pools, mpctx, link);
1945 MCTXUNLOCK(mctx, &mctx->lock);
1947 mpctx->common.impmagic = 0;
1948 mpctx->common.magic = 0;
1950 isc_mem_put((isc_mem_t *)mpctx->mctx, mpctx, sizeof(isc__mempool_t));
1958 ISC_MEMFUNC_SCOPE void
1959 isc__mempool_associatelock(isc_mempool_t *mpctx0, isc_mutex_t *lock) {
1960 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1962 REQUIRE(VALID_MEMPOOL(mpctx));
1963 REQUIRE(mpctx->lock == NULL);
1964 REQUIRE(lock != NULL);
1969 ISC_MEMFUNC_SCOPE void *
1970 isc___mempool_get(isc_mempool_t *mpctx0 FLARG) {
1971 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1976 REQUIRE(VALID_MEMPOOL(mpctx));
1980 if (mpctx->lock != NULL)
1984 * Don't let the caller go over quota
1986 if (mpctx->allocated >= mpctx->maxalloc) {
1992 * if we have a free list item, return the first here
1994 item = mpctx->items;
1996 mpctx->items = item->next;
1997 INSIST(mpctx->freecount > 0);
2005 * We need to dip into the well. Lock the memory context here and
2006 * fill up our free list.
2008 MCTXLOCK(mctx, &mctx->lock);
2009 for (i = 0; i < mpctx->fillcount; i++) {
2010 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2011 item = mem_getunlocked(mctx, mpctx->size);
2013 item = mem_get(mctx, mpctx->size);
2015 mem_getstats(mctx, mpctx->size);
2019 item->next = mpctx->items;
2020 mpctx->items = item;
2023 MCTXUNLOCK(mctx, &mctx->lock);
2026 * If we didn't get any items, return NULL.
2028 item = mpctx->items;
2032 mpctx->items = item->next;
2038 if (mpctx->lock != NULL)
2039 UNLOCK(mpctx->lock);
2041 #if ISC_MEM_TRACKLINES
2043 MCTXLOCK(mctx, &mctx->lock);
2044 ADD_TRACE(mctx, item, mpctx->size, file, line);
2045 MCTXUNLOCK(mctx, &mctx->lock);
2047 #endif /* ISC_MEM_TRACKLINES */
2052 ISC_MEMFUNC_SCOPE void
2053 isc___mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
2054 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2058 REQUIRE(VALID_MEMPOOL(mpctx));
2059 REQUIRE(mem != NULL);
2063 if (mpctx->lock != NULL)
2066 INSIST(mpctx->allocated > 0);
2069 #if ISC_MEM_TRACKLINES
2070 MCTXLOCK(mctx, &mctx->lock);
2071 DELETE_TRACE(mctx, mem, mpctx->size, file, line);
2072 MCTXUNLOCK(mctx, &mctx->lock);
2073 #endif /* ISC_MEM_TRACKLINES */
2076 * If our free list is full, return this to the mctx directly.
2078 if (mpctx->freecount >= mpctx->freemax) {
2079 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2080 MCTXLOCK(mctx, &mctx->lock);
2081 mem_putunlocked(mctx, mem, mpctx->size);
2082 MCTXUNLOCK(mctx, &mctx->lock);
2084 mem_put(mctx, mem, mpctx->size);
2085 MCTXLOCK(mctx, &mctx->lock);
2086 mem_putstats(mctx, mem, mpctx->size);
2087 MCTXUNLOCK(mctx, &mctx->lock);
2089 if (mpctx->lock != NULL)
2090 UNLOCK(mpctx->lock);
2095 * Otherwise, attach it to our free list and bump the counter.
2098 item = (element *)mem;
2099 item->next = mpctx->items;
2100 mpctx->items = item;
2102 if (mpctx->lock != NULL)
2103 UNLOCK(mpctx->lock);
2110 ISC_MEMFUNC_SCOPE void
2111 isc__mempool_setfreemax(isc_mempool_t *mpctx0, unsigned int limit) {
2112 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2114 REQUIRE(VALID_MEMPOOL(mpctx));
2116 if (mpctx->lock != NULL)
2119 mpctx->freemax = limit;
2121 if (mpctx->lock != NULL)
2122 UNLOCK(mpctx->lock);
2125 ISC_MEMFUNC_SCOPE unsigned int
2126 isc__mempool_getfreemax(isc_mempool_t *mpctx0) {
2127 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2128 unsigned int freemax;
2130 REQUIRE(VALID_MEMPOOL(mpctx));
2132 if (mpctx->lock != NULL)
2135 freemax = mpctx->freemax;
2137 if (mpctx->lock != NULL)
2138 UNLOCK(mpctx->lock);
2143 ISC_MEMFUNC_SCOPE unsigned int
2144 isc__mempool_getfreecount(isc_mempool_t *mpctx0) {
2145 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2146 unsigned int freecount;
2148 REQUIRE(VALID_MEMPOOL(mpctx));
2150 if (mpctx->lock != NULL)
2153 freecount = mpctx->freecount;
2155 if (mpctx->lock != NULL)
2156 UNLOCK(mpctx->lock);
2161 ISC_MEMFUNC_SCOPE void
2162 isc__mempool_setmaxalloc(isc_mempool_t *mpctx0, unsigned int limit) {
2163 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2167 REQUIRE(VALID_MEMPOOL(mpctx));
2169 if (mpctx->lock != NULL)
2172 mpctx->maxalloc = limit;
2174 if (mpctx->lock != NULL)
2175 UNLOCK(mpctx->lock);
2178 ISC_MEMFUNC_SCOPE unsigned int
2179 isc__mempool_getmaxalloc(isc_mempool_t *mpctx0) {
2180 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2181 unsigned int maxalloc;
2183 REQUIRE(VALID_MEMPOOL(mpctx));
2185 if (mpctx->lock != NULL)
2188 maxalloc = mpctx->maxalloc;
2190 if (mpctx->lock != NULL)
2191 UNLOCK(mpctx->lock);
2196 ISC_MEMFUNC_SCOPE unsigned int
2197 isc__mempool_getallocated(isc_mempool_t *mpctx0) {
2198 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2199 unsigned int allocated;
2201 REQUIRE(VALID_MEMPOOL(mpctx));
2203 if (mpctx->lock != NULL)
2206 allocated = mpctx->allocated;
2208 if (mpctx->lock != NULL)
2209 UNLOCK(mpctx->lock);
2214 ISC_MEMFUNC_SCOPE void
2215 isc__mempool_setfillcount(isc_mempool_t *mpctx0, unsigned int limit) {
2216 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2219 REQUIRE(VALID_MEMPOOL(mpctx));
2221 if (mpctx->lock != NULL)
2224 mpctx->fillcount = limit;
2226 if (mpctx->lock != NULL)
2227 UNLOCK(mpctx->lock);
2230 ISC_MEMFUNC_SCOPE unsigned int
2231 isc__mempool_getfillcount(isc_mempool_t *mpctx0) {
2232 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2234 unsigned int fillcount;
2236 REQUIRE(VALID_MEMPOOL(mpctx));
2238 if (mpctx->lock != NULL)
2241 fillcount = mpctx->fillcount;
2243 if (mpctx->lock != NULL)
2244 UNLOCK(mpctx->lock);
2249 #ifdef USE_MEMIMPREGISTER
2251 isc__mem_register() {
2252 return (isc_mem_register(isc__mem_create2));
2257 ISC_MEMFUNC_SCOPE void
2258 isc__mem_printactive(isc_mem_t *ctx0, FILE *file) {
2259 isc__mem_t *ctx = (isc__mem_t *)ctx0;
2261 REQUIRE(VALID_CONTEXT(ctx));
2262 REQUIRE(file != NULL);
2264 #if !ISC_MEM_TRACKLINES
2268 print_active(ctx, file);
2272 ISC_MEMFUNC_SCOPE void
2273 isc__mem_printallactive(FILE *file) {
2274 #if !ISC_MEM_TRACKLINES
2279 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2282 for (ctx = ISC_LIST_HEAD(contexts);
2284 ctx = ISC_LIST_NEXT(ctx, link)) {
2285 fprintf(file, "context: %p\n", ctx);
2286 print_active(ctx, file);
2292 ISC_MEMFUNC_SCOPE void
2293 isc__mem_checkdestroyed(FILE *file) {
2295 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2298 if (!ISC_LIST_EMPTY(contexts)) {
2299 #if ISC_MEM_TRACKLINES
2302 for (ctx = ISC_LIST_HEAD(contexts);
2304 ctx = ISC_LIST_NEXT(ctx, link)) {
2305 fprintf(file, "context: %p\n", ctx);
2306 print_active(ctx, file);
2315 ISC_MEMFUNC_SCOPE unsigned int
2316 isc_mem_references(isc_mem_t *ctx0) {
2317 isc__mem_t *ctx = (isc__mem_t *)ctx0;
2318 unsigned int references;
2320 REQUIRE(VALID_CONTEXT(ctx));
2322 MCTXLOCK(ctx, &ctx->lock);
2323 references = ctx->references;
2324 MCTXUNLOCK(ctx, &ctx->lock);
2326 return (references);
2331 typedef struct summarystat {
2334 isc_uint64_t blocksize;
2335 isc_uint64_t contextsize;
2339 renderctx(isc__mem_t *ctx, summarystat_t *summary, xmlTextWriterPtr writer) {
2340 REQUIRE(VALID_CONTEXT(ctx));
2342 xmlTextWriterStartElement(writer, ISC_XMLCHAR "context");
2344 xmlTextWriterStartElement(writer, ISC_XMLCHAR "id");
2345 xmlTextWriterWriteFormatString(writer, "%p", ctx);
2346 xmlTextWriterEndElement(writer); /* id */
2348 if (ctx->name[0] != 0) {
2349 xmlTextWriterStartElement(writer, ISC_XMLCHAR "name");
2350 xmlTextWriterWriteFormatString(writer, "%s", ctx->name);
2351 xmlTextWriterEndElement(writer); /* name */
2354 REQUIRE(VALID_CONTEXT(ctx));
2355 MCTXLOCK(ctx, &ctx->lock);
2357 summary->contextsize += sizeof(*ctx) +
2358 (ctx->max_size + 1) * sizeof(struct stats) +
2359 ctx->max_size * sizeof(element *) +
2360 ctx->basic_table_count * sizeof(char *);
2361 #if ISC_MEM_TRACKLINES
2362 if (ctx->debuglist != NULL) {
2363 summary->contextsize +=
2364 (ctx->max_size + 1) * sizeof(debuglist_t) +
2365 ctx->debuglistcnt * sizeof(debuglink_t);
2368 xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
2369 xmlTextWriterWriteFormatString(writer, "%d", ctx->references);
2370 xmlTextWriterEndElement(writer); /* references */
2372 summary->total += ctx->total;
2373 xmlTextWriterStartElement(writer, ISC_XMLCHAR "total");
2374 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2375 (isc_uint64_t)ctx->total);
2376 xmlTextWriterEndElement(writer); /* total */
2378 summary->inuse += ctx->inuse;
2379 xmlTextWriterStartElement(writer, ISC_XMLCHAR "inuse");
2380 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2381 (isc_uint64_t)ctx->inuse);
2382 xmlTextWriterEndElement(writer); /* inuse */
2384 xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxinuse");
2385 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2386 (isc_uint64_t)ctx->maxinuse);
2387 xmlTextWriterEndElement(writer); /* maxinuse */
2389 xmlTextWriterStartElement(writer, ISC_XMLCHAR "blocksize");
2390 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2391 summary->blocksize += ctx->basic_table_count *
2392 NUM_BASIC_BLOCKS * ctx->mem_target;
2393 xmlTextWriterWriteFormatString(writer,
2394 "%" ISC_PRINT_QUADFORMAT "u",
2396 ctx->basic_table_count *
2400 xmlTextWriterWriteFormatString(writer, "%s", "-");
2401 xmlTextWriterEndElement(writer); /* blocksize */
2403 xmlTextWriterStartElement(writer, ISC_XMLCHAR "pools");
2404 xmlTextWriterWriteFormatString(writer, "%u", ctx->poolcnt);
2405 xmlTextWriterEndElement(writer); /* pools */
2406 summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
2408 xmlTextWriterStartElement(writer, ISC_XMLCHAR "hiwater");
2409 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2410 (isc_uint64_t)ctx->hi_water);
2411 xmlTextWriterEndElement(writer); /* hiwater */
2413 xmlTextWriterStartElement(writer, ISC_XMLCHAR "lowater");
2414 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2415 (isc_uint64_t)ctx->lo_water);
2416 xmlTextWriterEndElement(writer); /* lowater */
2418 MCTXUNLOCK(ctx, &ctx->lock);
2420 xmlTextWriterEndElement(writer); /* context */
2424 isc_mem_renderxml(xmlTextWriterPtr writer) {
2426 summarystat_t summary;
2429 memset(&summary, 0, sizeof(summary));
2431 xmlTextWriterStartElement(writer, ISC_XMLCHAR "contexts");
2433 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2437 for (ctx = ISC_LIST_HEAD(contexts);
2439 ctx = ISC_LIST_NEXT(ctx, link)) {
2440 renderctx(ctx, &summary, writer);
2444 xmlTextWriterEndElement(writer); /* contexts */
2446 xmlTextWriterStartElement(writer, ISC_XMLCHAR "summary");
2448 xmlTextWriterStartElement(writer, ISC_XMLCHAR "TotalUse");
2449 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2451 xmlTextWriterEndElement(writer); /* TotalUse */
2453 xmlTextWriterStartElement(writer, ISC_XMLCHAR "InUse");
2454 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2456 xmlTextWriterEndElement(writer); /* InUse */
2458 xmlTextWriterStartElement(writer, ISC_XMLCHAR "BlockSize");
2459 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2461 xmlTextWriterEndElement(writer); /* BlockSize */
2463 xmlTextWriterStartElement(writer, ISC_XMLCHAR "ContextSize");
2464 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2465 summary.contextsize);
2466 xmlTextWriterEndElement(writer); /* ContextSize */
2468 xmlTextWriterStartElement(writer, ISC_XMLCHAR "Lost");
2469 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2471 xmlTextWriterEndElement(writer); /* Lost */
2473 xmlTextWriterEndElement(writer); /* summary */
2476 #endif /* HAVE_LIBXML2 */