2 * Copyright (C) 2004-2010 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.
18 /* $Id: mem.c,v 1.153.104.3 2010/05/12 00:49:31 marka Exp $ */
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_mem_water_t water;
149 ISC_LIST(isc__mempool_t) pools;
150 unsigned int poolcnt;
152 /* ISC_MEMFLAG_INTERNAL */
154 element ** freelists;
155 element * basic_blocks;
156 unsigned char ** basic_table;
157 unsigned int basic_table_count;
158 unsigned int basic_table_size;
159 unsigned char * lowest;
160 unsigned char * highest;
162 #if ISC_MEM_TRACKLINES
163 debuglist_t * debuglist;
164 unsigned int debuglistcnt;
167 unsigned int memalloc_failures;
168 ISC_LINK(isc__mem_t) link;
171 #define MEMPOOL_MAGIC ISC_MAGIC('M', 'E', 'M', 'p')
172 #define VALID_MEMPOOL(c) ISC_MAGIC_VALID(c, MEMPOOL_MAGIC)
174 struct isc__mempool {
175 /* always unlocked */
176 isc_mempool_t common; /*%< common header of mempool's */
177 isc_mutex_t *lock; /*%< optional lock */
178 isc__mem_t *mctx; /*%< our memory context */
179 /*%< locked via the memory context's lock */
180 ISC_LINK(isc__mempool_t) link; /*%< next pool in this mem context */
181 /*%< optionally locked from here down */
182 element *items; /*%< low water item list */
183 size_t size; /*%< size of each item on this pool */
184 unsigned int maxalloc; /*%< max number of items allowed */
185 unsigned int allocated; /*%< # of items currently given out */
186 unsigned int freecount; /*%< # of items on reserved list */
187 unsigned int freemax; /*%< # of items allowed on free list */
188 unsigned int fillcount; /*%< # of items to fetch on each fill */
190 unsigned int gets; /*%< # of requests to this pool */
191 /*%< Debugging only. */
192 #if ISC_MEMPOOL_NAMES
193 char name[16]; /*%< printed name in stats reports */
198 * Private Inline-able.
201 #if ! ISC_MEM_TRACKLINES
202 #define ADD_TRACE(a, b, c, d, e)
203 #define DELETE_TRACE(a, b, c, d, e)
205 #define ADD_TRACE(a, b, c, d, e) \
207 if ((isc_mem_debugging & (ISC_MEM_DEBUGTRACE | \
208 ISC_MEM_DEBUGRECORD)) != 0 && \
210 add_trace_entry(a, b, c, d, e); \
212 #define DELETE_TRACE(a, b, c, d, e) delete_trace_entry(a, b, c, d, e)
215 print_active(isc__mem_t *ctx, FILE *out);
218 * The following can be either static or public, depending on build environment.
222 #define ISC_MEMFUNC_SCOPE
224 #define ISC_MEMFUNC_SCOPE static
227 ISC_MEMFUNC_SCOPE isc_result_t
228 isc__mem_createx(size_t init_max_size, size_t target_size,
229 isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
231 ISC_MEMFUNC_SCOPE isc_result_t
232 isc__mem_createx2(size_t init_max_size, size_t target_size,
233 isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
234 isc_mem_t **ctxp, unsigned int flags);
235 ISC_MEMFUNC_SCOPE isc_result_t
236 isc__mem_create(size_t init_max_size, size_t target_size, isc_mem_t **ctxp);
237 ISC_MEMFUNC_SCOPE isc_result_t
238 isc__mem_create2(size_t init_max_size, size_t target_size,
239 isc_mem_t **ctxp, unsigned int flags);
240 ISC_MEMFUNC_SCOPE void
241 isc__mem_attach(isc_mem_t *source, isc_mem_t **targetp);
242 ISC_MEMFUNC_SCOPE void
243 isc__mem_detach(isc_mem_t **ctxp);
244 ISC_MEMFUNC_SCOPE void
245 isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG);
246 ISC_MEMFUNC_SCOPE void
247 isc__mem_destroy(isc_mem_t **ctxp);
248 ISC_MEMFUNC_SCOPE isc_result_t
249 isc__mem_ondestroy(isc_mem_t *ctx, isc_task_t *task, isc_event_t **event);
250 ISC_MEMFUNC_SCOPE void *
251 isc___mem_get(isc_mem_t *ctx, size_t size FLARG);
252 ISC_MEMFUNC_SCOPE void
253 isc___mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG);
254 ISC_MEMFUNC_SCOPE void
255 isc__mem_stats(isc_mem_t *ctx, FILE *out);
256 ISC_MEMFUNC_SCOPE void *
257 isc___mem_allocate(isc_mem_t *ctx, size_t size FLARG);
258 ISC_MEMFUNC_SCOPE void *
259 isc___mem_reallocate(isc_mem_t *ctx, void *ptr, size_t size FLARG);
260 ISC_MEMFUNC_SCOPE void
261 isc___mem_free(isc_mem_t *ctx, void *ptr FLARG);
262 ISC_MEMFUNC_SCOPE char *
263 isc___mem_strdup(isc_mem_t *mctx, const char *s FLARG);
264 ISC_MEMFUNC_SCOPE void
265 isc__mem_setdestroycheck(isc_mem_t *ctx, isc_boolean_t flag);
266 ISC_MEMFUNC_SCOPE void
267 isc__mem_setquota(isc_mem_t *ctx, size_t quota);
268 ISC_MEMFUNC_SCOPE size_t
269 isc__mem_getquota(isc_mem_t *ctx);
270 ISC_MEMFUNC_SCOPE size_t
271 isc__mem_inuse(isc_mem_t *ctx);
272 ISC_MEMFUNC_SCOPE void
273 isc__mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
274 size_t hiwater, size_t lowater);
275 ISC_MEMFUNC_SCOPE void
276 isc__mem_waterack(isc_mem_t *ctx0, int flag);
277 ISC_MEMFUNC_SCOPE void
278 isc__mem_setname(isc_mem_t *ctx, const char *name, void *tag);
279 ISC_MEMFUNC_SCOPE const char *
280 isc__mem_getname(isc_mem_t *ctx);
281 ISC_MEMFUNC_SCOPE void *
282 isc__mem_gettag(isc_mem_t *ctx);
283 ISC_MEMFUNC_SCOPE isc_result_t
284 isc__mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
285 ISC_MEMFUNC_SCOPE void
286 isc__mempool_setname(isc_mempool_t *mpctx, const char *name);
287 ISC_MEMFUNC_SCOPE void
288 isc__mempool_destroy(isc_mempool_t **mpctxp);
289 ISC_MEMFUNC_SCOPE void
290 isc__mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
291 ISC_MEMFUNC_SCOPE void *
292 isc___mempool_get(isc_mempool_t *mpctx FLARG);
293 ISC_MEMFUNC_SCOPE void
294 isc___mempool_put(isc_mempool_t *mpctx, void *mem FLARG);
295 ISC_MEMFUNC_SCOPE void
296 isc__mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
297 ISC_MEMFUNC_SCOPE unsigned int
298 isc__mempool_getfreemax(isc_mempool_t *mpctx);
299 ISC_MEMFUNC_SCOPE unsigned int
300 isc__mempool_getfreecount(isc_mempool_t *mpctx);
301 ISC_MEMFUNC_SCOPE void
302 isc__mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
303 ISC_MEMFUNC_SCOPE unsigned int
304 isc__mempool_getmaxalloc(isc_mempool_t *mpctx);
305 ISC_MEMFUNC_SCOPE unsigned int
306 isc__mempool_getallocated(isc_mempool_t *mpctx);
307 ISC_MEMFUNC_SCOPE void
308 isc__mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
309 ISC_MEMFUNC_SCOPE unsigned int
310 isc__mempool_getfillcount(isc_mempool_t *mpctx);
312 ISC_MEMFUNC_SCOPE void
313 isc__mem_printactive(isc_mem_t *ctx0, FILE *file);
314 ISC_MEMFUNC_SCOPE void
315 isc__mem_printallactive(FILE *file);
316 ISC_MEMFUNC_SCOPE void
317 isc__mem_checkdestroyed(FILE *file);
318 ISC_MEMFUNC_SCOPE unsigned int
319 isc__mem_references(isc_mem_t *ctx0);
322 static struct isc__memmethods {
323 isc_memmethods_t methods;
326 * The following are defined just for avoiding unused static functions.
329 void *createx, *create, *create2, *ondestroy, *stats,
330 *setquota, *getquota, *setname, *getname, *gettag;
339 isc___mem_putanddetach,
341 isc___mem_reallocate,
344 isc__mem_setdestroycheck,
352 (void *)isc__mem_createx, (void *)isc__mem_create,
353 (void *)isc__mem_create2, (void *)isc__mem_ondestroy,
354 (void *)isc__mem_stats, (void *)isc__mem_setquota,
355 (void *)isc__mem_getquota, (void *)isc__mem_setname,
356 (void *)isc__mem_getname, (void *)isc__mem_gettag
360 static struct isc__mempoolmethods {
361 isc_mempoolmethods_t methods;
364 * The following are defined just for avoiding unused static functions.
367 void *getfreemax, *getfreecount, *getmaxalloc, *getfillcount;
371 isc__mempool_destroy,
374 isc__mempool_getallocated,
375 isc__mempool_setmaxalloc,
376 isc__mempool_setfreemax,
377 isc__mempool_setname,
378 isc__mempool_associatelock,
379 isc__mempool_setfillcount
383 (void *)isc__mempool_getfreemax, (void *)isc__mempool_getfreecount,
384 (void *)isc__mempool_getmaxalloc, (void *)isc__mempool_getfillcount
389 * mctx must be locked.
392 add_trace_entry(isc__mem_t *mctx, const void *ptr, unsigned int size
397 unsigned int mysize = size;
399 if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
400 fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
403 "file %s line %u mctx %p\n"),
404 ptr, size, file, line, mctx);
406 if (mctx->debuglist == NULL)
409 if (mysize > mctx->max_size)
410 mysize = mctx->max_size;
412 dl = ISC_LIST_HEAD(mctx->debuglist[mysize]);
414 if (dl->count == DEBUGLIST_COUNT)
416 for (i = 0; i < DEBUGLIST_COUNT; i++) {
417 if (dl->ptr[i] == NULL) {
427 dl = ISC_LIST_NEXT(dl, link);
430 dl = malloc(sizeof(debuglink_t));
433 ISC_LINK_INIT(dl, link);
434 for (i = 1; i < DEBUGLIST_COUNT; i++) {
447 ISC_LIST_PREPEND(mctx->debuglist[mysize], dl, link);
448 mctx->debuglistcnt++;
452 delete_trace_entry(isc__mem_t *mctx, const void *ptr, unsigned int size,
453 const char *file, unsigned int line)
458 if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
459 fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
462 "file %s line %u mctx %p\n"),
463 ptr, size, file, line, mctx);
465 if (mctx->debuglist == NULL)
468 if (size > mctx->max_size)
469 size = mctx->max_size;
471 dl = ISC_LIST_HEAD(mctx->debuglist[size]);
473 for (i = 0; i < DEBUGLIST_COUNT; i++) {
474 if (dl->ptr[i] == ptr) {
480 INSIST(dl->count > 0);
482 if (dl->count == 0) {
483 ISC_LIST_UNLINK(mctx->debuglist[size],
490 dl = ISC_LIST_NEXT(dl, link);
494 * If we get here, we didn't find the item on the list. We're
499 #endif /* ISC_MEM_TRACKLINES */
502 rmsize(size_t size) {
504 * round down to ALIGNMENT_SIZE
506 return (size & (~(ALIGNMENT_SIZE - 1)));
510 quantize(size_t size) {
512 * Round up the result in order to get a size big
513 * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
518 return (ALIGNMENT_SIZE);
519 return ((size + ALIGNMENT_SIZE - 1) & (~(ALIGNMENT_SIZE - 1)));
522 static inline isc_boolean_t
523 more_basic_blocks(isc__mem_t *ctx) {
525 unsigned char *curr, *next;
526 unsigned char *first, *last;
527 unsigned char **table;
528 unsigned int table_size;
532 /* Require: we hold the context lock. */
535 * Did we hit the quota for this context?
537 increment = NUM_BASIC_BLOCKS * ctx->mem_target;
538 if (ctx->quota != 0U && ctx->total + increment > ctx->quota)
541 INSIST(ctx->basic_table_count <= ctx->basic_table_size);
542 if (ctx->basic_table_count == ctx->basic_table_size) {
543 table_size = ctx->basic_table_size + TABLE_INCREMENT;
544 table = (ctx->memalloc)(ctx->arg,
545 table_size * sizeof(unsigned char *));
547 ctx->memalloc_failures++;
550 if (ctx->basic_table_size != 0) {
551 memcpy(table, ctx->basic_table,
552 ctx->basic_table_size *
553 sizeof(unsigned char *));
554 (ctx->memfree)(ctx->arg, ctx->basic_table);
556 ctx->basic_table = table;
557 ctx->basic_table_size = table_size;
560 new = (ctx->memalloc)(ctx->arg, NUM_BASIC_BLOCKS * ctx->mem_target);
562 ctx->memalloc_failures++;
565 ctx->total += increment;
566 ctx->basic_table[ctx->basic_table_count] = new;
567 ctx->basic_table_count++;
570 next = curr + ctx->mem_target;
571 for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
572 ((element *)curr)->next = (element *)next;
574 next += ctx->mem_target;
577 * curr is now pointing at the last block in the
580 ((element *)curr)->next = NULL;
582 last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
583 if (first < ctx->lowest || ctx->lowest == NULL)
585 if (last > ctx->highest)
587 ctx->basic_blocks = new;
592 static inline isc_boolean_t
593 more_frags(isc__mem_t *ctx, size_t new_size) {
597 unsigned char *curr, *next;
600 * Try to get more fragments by chopping up a basic block.
603 if (ctx->basic_blocks == NULL) {
604 if (!more_basic_blocks(ctx)) {
606 * We can't get more memory from the OS, or we've
607 * hit the quota for this context.
610 * XXXRTH "At quota" notification here.
616 total_size = ctx->mem_target;
617 new = ctx->basic_blocks;
618 ctx->basic_blocks = ctx->basic_blocks->next;
619 frags = total_size / new_size;
620 ctx->stats[new_size].blocks++;
621 ctx->stats[new_size].freefrags += frags;
623 * Set up a linked-list of blocks of size
627 next = curr + new_size;
628 total_size -= new_size;
629 for (i = 0; i < (frags - 1); i++) {
630 ((element *)curr)->next = (element *)next;
633 total_size -= new_size;
636 * Add the remaining fragment of the basic block to a free list.
638 total_size = rmsize(total_size);
639 if (total_size > 0U) {
640 ((element *)next)->next = ctx->freelists[total_size];
641 ctx->freelists[total_size] = (element *)next;
642 ctx->stats[total_size].freefrags++;
645 * curr is now pointing at the last block in the
648 ((element *)curr)->next = NULL;
649 ctx->freelists[new_size] = new;
655 mem_getunlocked(isc__mem_t *ctx, size_t size) {
656 size_t new_size = quantize(size);
659 if (size >= ctx->max_size || new_size >= ctx->max_size) {
661 * memget() was called on something beyond our upper limit.
663 if (ctx->quota != 0U && ctx->total + size > ctx->quota) {
667 ret = (ctx->memalloc)(ctx->arg, size);
669 ctx->memalloc_failures++;
674 ctx->stats[ctx->max_size].gets++;
675 ctx->stats[ctx->max_size].totalgets++;
677 * If we don't set new_size to size, then the
678 * ISC_MEM_FILL code might write over bytes we
686 * If there are no blocks in the free list for this size, get a chunk
687 * of memory and then break it up into "new_size"-sized blocks, adding
688 * them to the free list.
690 if (ctx->freelists[new_size] == NULL && !more_frags(ctx, new_size))
694 * The free list uses the "rounded-up" size "new_size".
696 ret = ctx->freelists[new_size];
697 ctx->freelists[new_size] = ctx->freelists[new_size]->next;
700 * The stats[] uses the _actual_ "size" requested by the
701 * caller, with the caveat (in the code above) that "size" >= the
702 * max. size (max_size) ends up getting recorded as a call to
705 ctx->stats[size].gets++;
706 ctx->stats[size].totalgets++;
707 ctx->stats[new_size].freefrags--;
708 ctx->inuse += new_size;
714 memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
720 #if ISC_MEM_FILL && ISC_MEM_CHECKOVERRUN
722 check_overrun(void *mem, size_t size, size_t new_size) {
725 cp = (unsigned char *)mem;
727 while (size < new_size) {
736 mem_putunlocked(isc__mem_t *ctx, void *mem, size_t size) {
737 size_t new_size = quantize(size);
739 if (size == ctx->max_size || new_size >= ctx->max_size) {
741 * memput() called on something beyond our upper limit.
744 memset(mem, 0xde, size); /* Mnemonic for "dead". */
746 (ctx->memfree)(ctx->arg, mem);
747 INSIST(ctx->stats[ctx->max_size].gets != 0U);
748 ctx->stats[ctx->max_size].gets--;
749 INSIST(size <= ctx->total);
756 #if ISC_MEM_CHECKOVERRUN
757 check_overrun(mem, size, new_size);
759 memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
763 * The free list uses the "rounded-up" size "new_size".
765 ((element *)mem)->next = ctx->freelists[new_size];
766 ctx->freelists[new_size] = (element *)mem;
769 * The stats[] uses the _actual_ "size" requested by the
770 * caller, with the caveat (in the code above) that "size" >= the
771 * max. size (max_size) ends up getting recorded as a call to
774 INSIST(ctx->stats[size].gets != 0U);
775 ctx->stats[size].gets--;
776 ctx->stats[new_size].freefrags++;
777 ctx->inuse -= new_size;
781 * Perform a malloc, doing memory filling and overrun detection as necessary.
784 mem_get(isc__mem_t *ctx, size_t size) {
787 #if ISC_MEM_CHECKOVERRUN
791 ret = (ctx->memalloc)(ctx->arg, size);
793 ctx->memalloc_failures++;
797 memset(ret, 0xbe, size); /* Mnemonic for "beef". */
799 # if ISC_MEM_CHECKOVERRUN
809 * Perform a free, doing memory filling and overrun detection as necessary.
812 mem_put(isc__mem_t *ctx, void *mem, size_t size) {
813 #if ISC_MEM_CHECKOVERRUN
814 INSIST(((unsigned char *)mem)[size] == 0xbe);
817 memset(mem, 0xde, size); /* Mnemonic for "dead". */
821 (ctx->memfree)(ctx->arg, mem);
825 * Update internal counters after a memory get.
828 mem_getstats(isc__mem_t *ctx, size_t size) {
832 if (size > ctx->max_size) {
833 ctx->stats[ctx->max_size].gets++;
834 ctx->stats[ctx->max_size].totalgets++;
836 ctx->stats[size].gets++;
837 ctx->stats[size].totalgets++;
842 * Update internal counters after a memory put.
845 mem_putstats(isc__mem_t *ctx, void *ptr, size_t size) {
848 INSIST(ctx->inuse >= size);
851 if (size > ctx->max_size) {
852 INSIST(ctx->stats[ctx->max_size].gets > 0U);
853 ctx->stats[ctx->max_size].gets--;
855 INSIST(ctx->stats[size].gets > 0U);
856 ctx->stats[size].gets--;
865 default_memalloc(void *arg, size_t size) {
869 return (malloc(size));
873 default_memfree(void *arg, void *ptr) {
879 initialize_action(void) {
880 RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
881 ISC_LIST_INIT(contexts);
889 ISC_MEMFUNC_SCOPE isc_result_t
890 isc__mem_createx(size_t init_max_size, size_t target_size,
891 isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
894 return (isc__mem_createx2(init_max_size, target_size, memalloc, memfree,
895 arg, ctxp, ISC_MEMFLAG_DEFAULT));
899 ISC_MEMFUNC_SCOPE isc_result_t
900 isc__mem_createx2(size_t init_max_size, size_t target_size,
901 isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
902 isc_mem_t **ctxp, unsigned int flags)
907 REQUIRE(ctxp != NULL && *ctxp == NULL);
908 REQUIRE(memalloc != NULL);
909 REQUIRE(memfree != NULL);
911 INSIST((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0);
913 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
915 ctx = (memalloc)(arg, sizeof(*ctx));
917 return (ISC_R_NOMEMORY);
919 if ((flags & ISC_MEMFLAG_NOLOCK) == 0) {
920 result = isc_mutex_init(&ctx->lock);
921 if (result != ISC_R_SUCCESS) {
927 if (init_max_size == 0U)
928 ctx->max_size = DEF_MAX_SIZE;
930 ctx->max_size = init_max_size;
933 memset(ctx->name, 0, sizeof(ctx->name));
941 ctx->hi_called = ISC_FALSE;
943 ctx->water_arg = NULL;
944 ctx->common.impmagic = MEM_MAGIC;
945 ctx->common.magic = ISCAPI_MCTX_MAGIC;
946 ctx->common.methods = (isc_memmethods_t *)&memmethods;
947 isc_ondestroy_init(&ctx->ondestroy);
948 ctx->memalloc = memalloc;
949 ctx->memfree = memfree;
952 ctx->checkfree = ISC_TRUE;
953 #if ISC_MEM_TRACKLINES
954 ctx->debuglist = NULL;
955 ctx->debuglistcnt = 0;
957 ISC_LIST_INIT(ctx->pools);
959 ctx->freelists = NULL;
960 ctx->basic_blocks = NULL;
961 ctx->basic_table = NULL;
962 ctx->basic_table_count = 0;
963 ctx->basic_table_size = 0;
967 ctx->stats = (memalloc)(arg,
968 (ctx->max_size+1) * sizeof(struct stats));
969 if (ctx->stats == NULL) {
970 result = ISC_R_NOMEMORY;
973 memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
975 if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
976 if (target_size == 0U)
977 ctx->mem_target = DEF_MEM_TARGET;
979 ctx->mem_target = target_size;
980 ctx->freelists = (memalloc)(arg, ctx->max_size *
982 if (ctx->freelists == NULL) {
983 result = ISC_R_NOMEMORY;
986 memset(ctx->freelists, 0,
987 ctx->max_size * sizeof(element *));
990 #if ISC_MEM_TRACKLINES
991 if ((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0) {
994 ctx->debuglist = (memalloc)(arg,
995 (ctx->max_size+1) * sizeof(debuglist_t));
996 if (ctx->debuglist == NULL) {
997 result = ISC_R_NOMEMORY;
1000 for (i = 0; i <= ctx->max_size; i++)
1001 ISC_LIST_INIT(ctx->debuglist[i]);
1005 ctx->memalloc_failures = 0;
1008 ISC_LIST_INITANDAPPEND(contexts, ctx, link);
1011 *ctxp = (isc_mem_t *)ctx;
1012 return (ISC_R_SUCCESS);
1016 if (ctx->stats != NULL)
1017 (memfree)(arg, ctx->stats);
1018 if (ctx->freelists != NULL)
1019 (memfree)(arg, ctx->freelists);
1020 #if ISC_MEM_TRACKLINES
1021 if (ctx->debuglist != NULL)
1022 (ctx->memfree)(ctx->arg, ctx->debuglist);
1023 #endif /* ISC_MEM_TRACKLINES */
1024 if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
1025 DESTROYLOCK(&ctx->lock);
1026 (memfree)(arg, ctx);
1032 ISC_MEMFUNC_SCOPE isc_result_t
1033 isc__mem_create(size_t init_max_size, size_t target_size, isc_mem_t **ctxp) {
1034 return (isc__mem_createx2(init_max_size, target_size,
1035 default_memalloc, default_memfree, NULL,
1036 ctxp, ISC_MEMFLAG_DEFAULT));
1039 ISC_MEMFUNC_SCOPE isc_result_t
1040 isc__mem_create2(size_t init_max_size, size_t target_size,
1041 isc_mem_t **ctxp, unsigned int flags)
1043 return (isc__mem_createx2(init_max_size, target_size,
1044 default_memalloc, default_memfree, NULL,
1049 destroy(isc__mem_t *ctx) {
1051 isc_ondestroy_t ondest;
1054 ISC_LIST_UNLINK(contexts, ctx, link);
1055 totallost += ctx->inuse;
1058 ctx->common.impmagic = 0;
1059 ctx->common.magic = 0;
1061 INSIST(ISC_LIST_EMPTY(ctx->pools));
1063 #if ISC_MEM_TRACKLINES
1064 if (ctx->debuglist != NULL) {
1065 if (ctx->checkfree) {
1066 for (i = 0; i <= ctx->max_size; i++) {
1067 if (!ISC_LIST_EMPTY(ctx->debuglist[i]))
1068 print_active(ctx, stderr);
1069 INSIST(ISC_LIST_EMPTY(ctx->debuglist[i]));
1074 for (i = 0; i <= ctx->max_size; i++)
1075 for (dl = ISC_LIST_HEAD(ctx->debuglist[i]);
1077 dl = ISC_LIST_HEAD(ctx->debuglist[i])) {
1078 ISC_LIST_UNLINK(ctx->debuglist[i],
1083 (ctx->memfree)(ctx->arg, ctx->debuglist);
1086 INSIST(ctx->references == 0);
1088 if (ctx->checkfree) {
1089 for (i = 0; i <= ctx->max_size; i++) {
1090 #if ISC_MEM_TRACKLINES
1091 if (ctx->stats[i].gets != 0U)
1092 print_active(ctx, stderr);
1094 INSIST(ctx->stats[i].gets == 0U);
1098 (ctx->memfree)(ctx->arg, ctx->stats);
1100 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1101 for (i = 0; i < ctx->basic_table_count; i++)
1102 (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
1103 (ctx->memfree)(ctx->arg, ctx->freelists);
1104 if (ctx->basic_table != NULL)
1105 (ctx->memfree)(ctx->arg, ctx->basic_table);
1108 ondest = ctx->ondestroy;
1110 if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
1111 DESTROYLOCK(&ctx->lock);
1112 (ctx->memfree)(ctx->arg, ctx);
1114 isc_ondestroy_notify(&ondest, ctx);
1117 ISC_MEMFUNC_SCOPE void
1118 isc__mem_attach(isc_mem_t *source0, isc_mem_t **targetp) {
1119 isc__mem_t *source = (isc__mem_t *)source0;
1121 REQUIRE(VALID_CONTEXT(source));
1122 REQUIRE(targetp != NULL && *targetp == NULL);
1124 MCTXLOCK(source, &source->lock);
1125 source->references++;
1126 MCTXUNLOCK(source, &source->lock);
1128 *targetp = (isc_mem_t *)source;
1131 ISC_MEMFUNC_SCOPE void
1132 isc__mem_detach(isc_mem_t **ctxp) {
1134 isc_boolean_t want_destroy = ISC_FALSE;
1136 REQUIRE(ctxp != NULL);
1137 ctx = (isc__mem_t *)*ctxp;
1138 REQUIRE(VALID_CONTEXT(ctx));
1140 MCTXLOCK(ctx, &ctx->lock);
1141 INSIST(ctx->references > 0);
1143 if (ctx->references == 0)
1144 want_destroy = ISC_TRUE;
1145 MCTXUNLOCK(ctx, &ctx->lock);
1154 * isc_mem_putanddetach() is the equivalent of:
1157 * isc_mem_attach(ptr->mctx, &mctx);
1158 * isc_mem_detach(&ptr->mctx);
1159 * isc_mem_put(mctx, ptr, sizeof(*ptr);
1160 * isc_mem_detach(&mctx);
1163 ISC_MEMFUNC_SCOPE void
1164 isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
1166 isc_boolean_t want_destroy = ISC_FALSE;
1170 REQUIRE(ctxp != NULL);
1171 ctx = (isc__mem_t *)*ctxp;
1172 REQUIRE(VALID_CONTEXT(ctx));
1173 REQUIRE(ptr != NULL);
1176 * Must be before mem_putunlocked() as ctxp is usually within
1181 if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0) {
1182 if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
1183 si = &(((size_info *)ptr)[-1]);
1184 oldsize = si->u.size - ALIGNMENT_SIZE;
1185 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
1186 oldsize -= ALIGNMENT_SIZE;
1187 INSIST(oldsize == size);
1189 isc_mem_free((isc_mem_t *)ctx, ptr);
1191 MCTXLOCK(ctx, &ctx->lock);
1193 if (ctx->references == 0)
1194 want_destroy = ISC_TRUE;
1195 MCTXUNLOCK(ctx, &ctx->lock);
1202 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1203 MCTXLOCK(ctx, &ctx->lock);
1204 mem_putunlocked(ctx, ptr, size);
1206 mem_put(ctx, ptr, size);
1207 MCTXLOCK(ctx, &ctx->lock);
1208 mem_putstats(ctx, ptr, size);
1211 DELETE_TRACE(ctx, ptr, size, file, line);
1212 INSIST(ctx->references > 0);
1214 if (ctx->references == 0)
1215 want_destroy = ISC_TRUE;
1217 MCTXUNLOCK(ctx, &ctx->lock);
1223 ISC_MEMFUNC_SCOPE void
1224 isc__mem_destroy(isc_mem_t **ctxp) {
1228 * This routine provides legacy support for callers who use mctxs
1229 * without attaching/detaching.
1232 REQUIRE(ctxp != NULL);
1233 ctx = (isc__mem_t *)*ctxp;
1234 REQUIRE(VALID_CONTEXT(ctx));
1236 MCTXLOCK(ctx, &ctx->lock);
1237 #if ISC_MEM_TRACKLINES
1238 if (ctx->references != 1)
1239 print_active(ctx, stderr);
1241 REQUIRE(ctx->references == 1);
1243 MCTXUNLOCK(ctx, &ctx->lock);
1250 ISC_MEMFUNC_SCOPE isc_result_t
1251 isc__mem_ondestroy(isc_mem_t *ctx0, isc_task_t *task, isc_event_t **event) {
1252 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1255 MCTXLOCK(ctx, &ctx->lock);
1256 res = isc_ondestroy_register(&ctx->ondestroy, task, event);
1257 MCTXUNLOCK(ctx, &ctx->lock);
1262 ISC_MEMFUNC_SCOPE void *
1263 isc___mem_get(isc_mem_t *ctx0, size_t size FLARG) {
1264 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1266 isc_boolean_t call_water = ISC_FALSE;
1268 REQUIRE(VALID_CONTEXT(ctx));
1270 if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0)
1271 return (isc_mem_allocate((isc_mem_t *)ctx, size));
1273 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1274 MCTXLOCK(ctx, &ctx->lock);
1275 ptr = mem_getunlocked(ctx, size);
1277 ptr = mem_get(ctx, size);
1278 MCTXLOCK(ctx, &ctx->lock);
1280 mem_getstats(ctx, size);
1283 ADD_TRACE(ctx, ptr, size, file, line);
1284 if (ctx->hi_water != 0U && !ctx->hi_called &&
1285 ctx->inuse > ctx->hi_water) {
1286 call_water = ISC_TRUE;
1288 if (ctx->inuse > ctx->maxinuse) {
1289 ctx->maxinuse = ctx->inuse;
1290 if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1291 (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
1292 fprintf(stderr, "maxinuse = %lu\n",
1293 (unsigned long)ctx->inuse);
1295 MCTXUNLOCK(ctx, &ctx->lock);
1298 (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
1303 ISC_MEMFUNC_SCOPE void
1304 isc___mem_put(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
1305 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1306 isc_boolean_t call_water = ISC_FALSE;
1310 REQUIRE(VALID_CONTEXT(ctx));
1311 REQUIRE(ptr != NULL);
1313 if ((isc_mem_debugging & (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0) {
1314 if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
1315 si = &(((size_info *)ptr)[-1]);
1316 oldsize = si->u.size - ALIGNMENT_SIZE;
1317 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
1318 oldsize -= ALIGNMENT_SIZE;
1319 INSIST(oldsize == size);
1321 isc_mem_free((isc_mem_t *)ctx, ptr);
1325 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1326 MCTXLOCK(ctx, &ctx->lock);
1327 mem_putunlocked(ctx, ptr, size);
1329 mem_put(ctx, ptr, size);
1330 MCTXLOCK(ctx, &ctx->lock);
1331 mem_putstats(ctx, ptr, size);
1334 DELETE_TRACE(ctx, ptr, size, file, line);
1337 * The check against ctx->lo_water == 0 is for the condition
1338 * when the context was pushed over hi_water but then had
1339 * isc_mem_setwater() called with 0 for hi_water and lo_water.
1341 if (ctx->hi_called &&
1342 (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1343 if (ctx->water != NULL)
1344 call_water = ISC_TRUE;
1346 MCTXUNLOCK(ctx, &ctx->lock);
1349 (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1352 ISC_MEMFUNC_SCOPE void
1353 isc__mem_waterack(isc_mem_t *ctx0, int flag) {
1354 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1356 REQUIRE(VALID_CONTEXT(ctx));
1358 MCTXLOCK(ctx, &ctx->lock);
1359 if (flag == ISC_MEM_LOWATER)
1360 ctx->hi_called = ISC_FALSE;
1361 else if (flag == ISC_MEM_HIWATER)
1362 ctx->hi_called = ISC_TRUE;
1363 MCTXUNLOCK(ctx, &ctx->lock);
1366 #if ISC_MEM_TRACKLINES
1368 print_active(isc__mem_t *mctx, FILE *out) {
1369 if (mctx->debuglist != NULL) {
1373 isc_boolean_t found;
1375 fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1377 "Dump of all outstanding "
1378 "memory allocations:\n"));
1380 format = isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1381 ISC_MSG_PTRFILELINE,
1382 "\tptr %p size %u file %s line %u\n");
1383 for (i = 0; i <= mctx->max_size; i++) {
1384 dl = ISC_LIST_HEAD(mctx->debuglist[i]);
1389 while (dl != NULL) {
1390 for (j = 0; j < DEBUGLIST_COUNT; j++)
1391 if (dl->ptr[j] != NULL)
1392 fprintf(out, format,
1397 dl = ISC_LIST_NEXT(dl, link);
1401 fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1402 ISC_MSG_NONE, "\tNone.\n"));
1408 * Print the stats[] on the stream "out" with suitable formatting.
1410 ISC_MEMFUNC_SCOPE void
1411 isc__mem_stats(isc_mem_t *ctx0, FILE *out) {
1412 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1414 const struct stats *s;
1415 const isc__mempool_t *pool;
1417 REQUIRE(VALID_CONTEXT(ctx));
1418 MCTXLOCK(ctx, &ctx->lock);
1420 for (i = 0; i <= ctx->max_size; i++) {
1423 if (s->totalgets == 0U && s->gets == 0U)
1425 fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
1426 (i == ctx->max_size) ? ">=" : " ",
1427 (unsigned long) i, s->totalgets, s->gets);
1428 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
1429 (s->blocks != 0U || s->freefrags != 0U))
1430 fprintf(out, " (%lu bl, %lu ff)",
1431 s->blocks, s->freefrags);
1436 * Note that since a pool can be locked now, these stats might be
1437 * somewhat off if the pool is in active use at the time the stats
1438 * are dumped. The link fields are protected by the isc_mem_t's
1439 * lock, however, so walking this list and extracting integers from
1440 * stats fields is always safe.
1442 pool = ISC_LIST_HEAD(ctx->pools);
1444 fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1446 "[Pool statistics]\n"));
1447 fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
1448 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1449 ISC_MSG_POOLNAME, "name"),
1450 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1451 ISC_MSG_POOLSIZE, "size"),
1452 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1453 ISC_MSG_POOLMAXALLOC, "maxalloc"),
1454 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1455 ISC_MSG_POOLALLOCATED, "allocated"),
1456 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1457 ISC_MSG_POOLFREECOUNT, "freecount"),
1458 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1459 ISC_MSG_POOLFREEMAX, "freemax"),
1460 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1461 ISC_MSG_POOLFILLCOUNT, "fillcount"),
1462 isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1463 ISC_MSG_POOLGETS, "gets"),
1466 while (pool != NULL) {
1467 fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
1468 pool->name, (unsigned long) pool->size, pool->maxalloc,
1469 pool->allocated, pool->freecount, pool->freemax,
1470 pool->fillcount, pool->gets,
1471 (pool->lock == NULL ? "N" : "Y"));
1472 pool = ISC_LIST_NEXT(pool, link);
1475 #if ISC_MEM_TRACKLINES
1476 print_active(ctx, out);
1479 MCTXUNLOCK(ctx, &ctx->lock);
1483 * Replacements for malloc() and free() -- they implicitly remember the
1484 * size of the object allocated (with some additional overhead).
1488 isc__mem_allocateunlocked(isc_mem_t *ctx0, size_t size) {
1489 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1492 size += ALIGNMENT_SIZE;
1493 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
1494 size += ALIGNMENT_SIZE;
1496 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0)
1497 si = mem_getunlocked(ctx, size);
1499 si = mem_get(ctx, size);
1503 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
1511 ISC_MEMFUNC_SCOPE void *
1512 isc___mem_allocate(isc_mem_t *ctx0, size_t size FLARG) {
1513 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1515 isc_boolean_t call_water = ISC_FALSE;
1517 REQUIRE(VALID_CONTEXT(ctx));
1519 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1520 MCTXLOCK(ctx, &ctx->lock);
1521 si = isc__mem_allocateunlocked((isc_mem_t *)ctx, size);
1523 si = isc__mem_allocateunlocked((isc_mem_t *)ctx, size);
1524 MCTXLOCK(ctx, &ctx->lock);
1526 mem_getstats(ctx, si[-1].u.size);
1529 #if ISC_MEM_TRACKLINES
1530 ADD_TRACE(ctx, si, si[-1].u.size, file, line);
1532 if (ctx->hi_water != 0U && !ctx->hi_called &&
1533 ctx->inuse > ctx->hi_water) {
1534 ctx->hi_called = ISC_TRUE;
1535 call_water = ISC_TRUE;
1537 if (ctx->inuse > ctx->maxinuse) {
1538 ctx->maxinuse = ctx->inuse;
1539 if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1540 (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
1541 fprintf(stderr, "maxinuse = %lu\n",
1542 (unsigned long)ctx->inuse);
1544 MCTXUNLOCK(ctx, &ctx->lock);
1547 (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
1552 ISC_MEMFUNC_SCOPE void *
1553 isc___mem_reallocate(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
1554 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1555 void *new_ptr = NULL;
1556 size_t oldsize, copysize;
1558 REQUIRE(VALID_CONTEXT(ctx));
1561 * This function emulates the realloc(3) standard library function:
1562 * - if size > 0, allocate new memory; and if ptr is non NULL, copy
1563 * as much of the old contents to the new buffer and free the old one.
1564 * Note that when allocation fails the original pointer is intact;
1565 * the caller must free it.
1566 * - if size is 0 and ptr is non NULL, simply free the given ptr.
1567 * - this function returns:
1568 * pointer to the newly allocated memory, or
1569 * NULL if allocation fails or doesn't happen.
1572 new_ptr = isc__mem_allocate(ctx0, size FLARG_PASS);
1573 if (new_ptr != NULL && ptr != NULL) {
1574 oldsize = (((size_info *)ptr)[-1]).u.size;
1575 INSIST(oldsize >= ALIGNMENT_SIZE);
1576 oldsize -= ALIGNMENT_SIZE;
1577 copysize = oldsize > size ? size : oldsize;
1578 memcpy(new_ptr, ptr, copysize);
1579 isc__mem_free(ctx0, ptr FLARG_PASS);
1581 } else if (ptr != NULL)
1582 isc__mem_free(ctx0, ptr FLARG_PASS);
1587 ISC_MEMFUNC_SCOPE void
1588 isc___mem_free(isc_mem_t *ctx0, void *ptr FLARG) {
1589 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1592 isc_boolean_t call_water= ISC_FALSE;
1594 REQUIRE(VALID_CONTEXT(ctx));
1595 REQUIRE(ptr != NULL);
1597 if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
1598 si = &(((size_info *)ptr)[-2]);
1599 REQUIRE(si->u.ctx == ctx);
1600 size = si[1].u.size;
1602 si = &(((size_info *)ptr)[-1]);
1606 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1607 MCTXLOCK(ctx, &ctx->lock);
1608 mem_putunlocked(ctx, si, size);
1610 mem_put(ctx, si, size);
1611 MCTXLOCK(ctx, &ctx->lock);
1612 mem_putstats(ctx, si, size);
1615 DELETE_TRACE(ctx, ptr, size, file, line);
1618 * The check against ctx->lo_water == 0 is for the condition
1619 * when the context was pushed over hi_water but then had
1620 * isc_mem_setwater() called with 0 for hi_water and lo_water.
1622 if (ctx->hi_called &&
1623 (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1624 ctx->hi_called = ISC_FALSE;
1626 if (ctx->water != NULL)
1627 call_water = ISC_TRUE;
1629 MCTXUNLOCK(ctx, &ctx->lock);
1632 (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1637 * Other useful things.
1640 ISC_MEMFUNC_SCOPE char *
1641 isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
1642 isc__mem_t *mctx = (isc__mem_t *)mctx0;
1646 REQUIRE(VALID_CONTEXT(mctx));
1651 ns = isc___mem_allocate((isc_mem_t *)mctx, len + 1 FLARG_PASS);
1654 strncpy(ns, s, len + 1);
1659 ISC_MEMFUNC_SCOPE void
1660 isc__mem_setdestroycheck(isc_mem_t *ctx0, isc_boolean_t flag) {
1661 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1663 REQUIRE(VALID_CONTEXT(ctx));
1664 MCTXLOCK(ctx, &ctx->lock);
1666 ctx->checkfree = flag;
1668 MCTXUNLOCK(ctx, &ctx->lock);
1675 ISC_MEMFUNC_SCOPE void
1676 isc__mem_setquota(isc_mem_t *ctx0, size_t quota) {
1677 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1679 REQUIRE(VALID_CONTEXT(ctx));
1680 MCTXLOCK(ctx, &ctx->lock);
1684 MCTXUNLOCK(ctx, &ctx->lock);
1687 ISC_MEMFUNC_SCOPE size_t
1688 isc__mem_getquota(isc_mem_t *ctx0) {
1689 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1692 REQUIRE(VALID_CONTEXT(ctx));
1693 MCTXLOCK(ctx, &ctx->lock);
1697 MCTXUNLOCK(ctx, &ctx->lock);
1702 ISC_MEMFUNC_SCOPE size_t
1703 isc__mem_inuse(isc_mem_t *ctx0) {
1704 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1707 REQUIRE(VALID_CONTEXT(ctx));
1708 MCTXLOCK(ctx, &ctx->lock);
1712 MCTXUNLOCK(ctx, &ctx->lock);
1717 ISC_MEMFUNC_SCOPE void
1718 isc__mem_setwater(isc_mem_t *ctx0, isc_mem_water_t water, void *water_arg,
1719 size_t hiwater, size_t lowater)
1721 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1722 isc_boolean_t callwater = ISC_FALSE;
1723 isc_mem_water_t oldwater;
1726 REQUIRE(VALID_CONTEXT(ctx));
1727 REQUIRE(hiwater >= lowater);
1729 MCTXLOCK(ctx, &ctx->lock);
1730 oldwater = ctx->water;
1731 oldwater_arg = ctx->water_arg;
1732 if (water == NULL) {
1733 callwater = ctx->hi_called;
1735 ctx->water_arg = NULL;
1738 ctx->hi_called = ISC_FALSE;
1740 if (ctx->hi_called &&
1741 (ctx->water != water || ctx->water_arg != water_arg ||
1742 ctx->inuse < lowater || lowater == 0U))
1743 callwater = ISC_TRUE;
1745 ctx->water_arg = water_arg;
1746 ctx->hi_water = hiwater;
1747 ctx->lo_water = lowater;
1748 ctx->hi_called = ISC_FALSE;
1750 MCTXUNLOCK(ctx, &ctx->lock);
1752 if (callwater && oldwater != NULL)
1753 (oldwater)(oldwater_arg, ISC_MEM_LOWATER);
1756 ISC_MEMFUNC_SCOPE void
1757 isc__mem_setname(isc_mem_t *ctx0, const char *name, void *tag) {
1758 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1760 REQUIRE(VALID_CONTEXT(ctx));
1763 memset(ctx->name, 0, sizeof(ctx->name));
1764 strncpy(ctx->name, name, sizeof(ctx->name) - 1);
1769 ISC_MEMFUNC_SCOPE const char *
1770 isc__mem_getname(isc_mem_t *ctx0) {
1771 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1773 REQUIRE(VALID_CONTEXT(ctx));
1778 ISC_MEMFUNC_SCOPE void *
1779 isc__mem_gettag(isc_mem_t *ctx0) {
1780 isc__mem_t *ctx = (isc__mem_t *)ctx0;
1782 REQUIRE(VALID_CONTEXT(ctx));
1791 ISC_MEMFUNC_SCOPE isc_result_t
1792 isc__mempool_create(isc_mem_t *mctx0, size_t size, isc_mempool_t **mpctxp) {
1793 isc__mem_t *mctx = (isc__mem_t *)mctx0;
1794 isc__mempool_t *mpctx;
1796 REQUIRE(VALID_CONTEXT(mctx));
1798 REQUIRE(mpctxp != NULL && *mpctxp == NULL);
1801 * Allocate space for this pool, initialize values, and if all works
1802 * well, attach to the memory context.
1804 mpctx = isc_mem_get((isc_mem_t *)mctx, sizeof(isc__mempool_t));
1806 return (ISC_R_NOMEMORY);
1808 mpctx->common.methods = (isc_mempoolmethods_t *)&mempoolmethods;
1809 mpctx->common.impmagic = MEMPOOL_MAGIC;
1810 mpctx->common.magic = ISCAPI_MPOOL_MAGIC;
1814 mpctx->maxalloc = UINT_MAX;
1815 mpctx->allocated = 0;
1816 mpctx->freecount = 0;
1818 mpctx->fillcount = 1;
1820 #if ISC_MEMPOOL_NAMES
1823 mpctx->items = NULL;
1825 *mpctxp = (isc_mempool_t *)mpctx;
1827 MCTXLOCK(mctx, &mctx->lock);
1828 ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
1830 MCTXUNLOCK(mctx, &mctx->lock);
1832 return (ISC_R_SUCCESS);
1835 ISC_MEMFUNC_SCOPE void
1836 isc__mempool_setname(isc_mempool_t *mpctx0, const char *name) {
1837 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1839 REQUIRE(name != NULL);
1840 REQUIRE(VALID_MEMPOOL(mpctx));
1842 #if ISC_MEMPOOL_NAMES
1843 if (mpctx->lock != NULL)
1846 strncpy(mpctx->name, name, sizeof(mpctx->name) - 1);
1847 mpctx->name[sizeof(mpctx->name) - 1] = '\0';
1849 if (mpctx->lock != NULL)
1850 UNLOCK(mpctx->lock);
1857 ISC_MEMFUNC_SCOPE void
1858 isc__mempool_destroy(isc_mempool_t **mpctxp) {
1859 isc__mempool_t *mpctx;
1864 REQUIRE(mpctxp != NULL);
1865 mpctx = (isc__mempool_t *)*mpctxp;
1866 REQUIRE(VALID_MEMPOOL(mpctx));
1867 #if ISC_MEMPOOL_NAMES
1868 if (mpctx->allocated > 0)
1869 UNEXPECTED_ERROR(__FILE__, __LINE__,
1870 "isc__mempool_destroy(): mempool %s "
1874 REQUIRE(mpctx->allocated == 0);
1884 * Return any items on the free list
1886 MCTXLOCK(mctx, &mctx->lock);
1887 while (mpctx->items != NULL) {
1888 INSIST(mpctx->freecount > 0);
1890 item = mpctx->items;
1891 mpctx->items = item->next;
1893 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1894 mem_putunlocked(mctx, item, mpctx->size);
1896 mem_put(mctx, item, mpctx->size);
1897 mem_putstats(mctx, item, mpctx->size);
1900 MCTXUNLOCK(mctx, &mctx->lock);
1903 * Remove our linked list entry from the memory context.
1905 MCTXLOCK(mctx, &mctx->lock);
1906 ISC_LIST_UNLINK(mctx->pools, mpctx, link);
1908 MCTXUNLOCK(mctx, &mctx->lock);
1910 mpctx->common.impmagic = 0;
1911 mpctx->common.magic = 0;
1913 isc_mem_put((isc_mem_t *)mpctx->mctx, mpctx, sizeof(isc__mempool_t));
1921 ISC_MEMFUNC_SCOPE void
1922 isc__mempool_associatelock(isc_mempool_t *mpctx0, isc_mutex_t *lock) {
1923 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1925 REQUIRE(VALID_MEMPOOL(mpctx));
1926 REQUIRE(mpctx->lock == NULL);
1927 REQUIRE(lock != NULL);
1932 ISC_MEMFUNC_SCOPE void *
1933 isc___mempool_get(isc_mempool_t *mpctx0 FLARG) {
1934 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1939 REQUIRE(VALID_MEMPOOL(mpctx));
1943 if (mpctx->lock != NULL)
1947 * Don't let the caller go over quota
1949 if (mpctx->allocated >= mpctx->maxalloc) {
1955 * if we have a free list item, return the first here
1957 item = mpctx->items;
1959 mpctx->items = item->next;
1960 INSIST(mpctx->freecount > 0);
1968 * We need to dip into the well. Lock the memory context here and
1969 * fill up our free list.
1971 MCTXLOCK(mctx, &mctx->lock);
1972 for (i = 0; i < mpctx->fillcount; i++) {
1973 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1974 item = mem_getunlocked(mctx, mpctx->size);
1976 item = mem_get(mctx, mpctx->size);
1978 mem_getstats(mctx, mpctx->size);
1982 item->next = mpctx->items;
1983 mpctx->items = item;
1986 MCTXUNLOCK(mctx, &mctx->lock);
1989 * If we didn't get any items, return NULL.
1991 item = mpctx->items;
1995 mpctx->items = item->next;
2001 if (mpctx->lock != NULL)
2002 UNLOCK(mpctx->lock);
2004 #if ISC_MEM_TRACKLINES
2006 MCTXLOCK(mctx, &mctx->lock);
2007 ADD_TRACE(mctx, item, mpctx->size, file, line);
2008 MCTXUNLOCK(mctx, &mctx->lock);
2010 #endif /* ISC_MEM_TRACKLINES */
2015 ISC_MEMFUNC_SCOPE void
2016 isc___mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
2017 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2021 REQUIRE(VALID_MEMPOOL(mpctx));
2022 REQUIRE(mem != NULL);
2026 if (mpctx->lock != NULL)
2029 INSIST(mpctx->allocated > 0);
2032 #if ISC_MEM_TRACKLINES
2033 MCTXLOCK(mctx, &mctx->lock);
2034 DELETE_TRACE(mctx, mem, mpctx->size, file, line);
2035 MCTXUNLOCK(mctx, &mctx->lock);
2036 #endif /* ISC_MEM_TRACKLINES */
2039 * If our free list is full, return this to the mctx directly.
2041 if (mpctx->freecount >= mpctx->freemax) {
2042 if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2043 MCTXLOCK(mctx, &mctx->lock);
2044 mem_putunlocked(mctx, mem, mpctx->size);
2045 MCTXUNLOCK(mctx, &mctx->lock);
2047 mem_put(mctx, mem, mpctx->size);
2048 MCTXLOCK(mctx, &mctx->lock);
2049 mem_putstats(mctx, mem, mpctx->size);
2050 MCTXUNLOCK(mctx, &mctx->lock);
2052 if (mpctx->lock != NULL)
2053 UNLOCK(mpctx->lock);
2058 * Otherwise, attach it to our free list and bump the counter.
2061 item = (element *)mem;
2062 item->next = mpctx->items;
2063 mpctx->items = item;
2065 if (mpctx->lock != NULL)
2066 UNLOCK(mpctx->lock);
2073 ISC_MEMFUNC_SCOPE void
2074 isc__mempool_setfreemax(isc_mempool_t *mpctx0, unsigned int limit) {
2075 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2077 REQUIRE(VALID_MEMPOOL(mpctx));
2079 if (mpctx->lock != NULL)
2082 mpctx->freemax = limit;
2084 if (mpctx->lock != NULL)
2085 UNLOCK(mpctx->lock);
2088 ISC_MEMFUNC_SCOPE unsigned int
2089 isc__mempool_getfreemax(isc_mempool_t *mpctx0) {
2090 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2091 unsigned int freemax;
2093 REQUIRE(VALID_MEMPOOL(mpctx));
2095 if (mpctx->lock != NULL)
2098 freemax = mpctx->freemax;
2100 if (mpctx->lock != NULL)
2101 UNLOCK(mpctx->lock);
2106 ISC_MEMFUNC_SCOPE unsigned int
2107 isc__mempool_getfreecount(isc_mempool_t *mpctx0) {
2108 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2109 unsigned int freecount;
2111 REQUIRE(VALID_MEMPOOL(mpctx));
2113 if (mpctx->lock != NULL)
2116 freecount = mpctx->freecount;
2118 if (mpctx->lock != NULL)
2119 UNLOCK(mpctx->lock);
2124 ISC_MEMFUNC_SCOPE void
2125 isc__mempool_setmaxalloc(isc_mempool_t *mpctx0, unsigned int limit) {
2126 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2130 REQUIRE(VALID_MEMPOOL(mpctx));
2132 if (mpctx->lock != NULL)
2135 mpctx->maxalloc = limit;
2137 if (mpctx->lock != NULL)
2138 UNLOCK(mpctx->lock);
2141 ISC_MEMFUNC_SCOPE unsigned int
2142 isc__mempool_getmaxalloc(isc_mempool_t *mpctx0) {
2143 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2144 unsigned int maxalloc;
2146 REQUIRE(VALID_MEMPOOL(mpctx));
2148 if (mpctx->lock != NULL)
2151 maxalloc = mpctx->maxalloc;
2153 if (mpctx->lock != NULL)
2154 UNLOCK(mpctx->lock);
2159 ISC_MEMFUNC_SCOPE unsigned int
2160 isc__mempool_getallocated(isc_mempool_t *mpctx0) {
2161 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2162 unsigned int allocated;
2164 REQUIRE(VALID_MEMPOOL(mpctx));
2166 if (mpctx->lock != NULL)
2169 allocated = mpctx->allocated;
2171 if (mpctx->lock != NULL)
2172 UNLOCK(mpctx->lock);
2177 ISC_MEMFUNC_SCOPE void
2178 isc__mempool_setfillcount(isc_mempool_t *mpctx0, unsigned int limit) {
2179 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2182 REQUIRE(VALID_MEMPOOL(mpctx));
2184 if (mpctx->lock != NULL)
2187 mpctx->fillcount = limit;
2189 if (mpctx->lock != NULL)
2190 UNLOCK(mpctx->lock);
2193 ISC_MEMFUNC_SCOPE unsigned int
2194 isc__mempool_getfillcount(isc_mempool_t *mpctx0) {
2195 isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2197 unsigned int fillcount;
2199 REQUIRE(VALID_MEMPOOL(mpctx));
2201 if (mpctx->lock != NULL)
2204 fillcount = mpctx->fillcount;
2206 if (mpctx->lock != NULL)
2207 UNLOCK(mpctx->lock);
2212 #ifdef USE_MEMIMPREGISTER
2214 isc__mem_register() {
2215 return (isc_mem_register(isc__mem_create2));
2220 ISC_MEMFUNC_SCOPE void
2221 isc__mem_printactive(isc_mem_t *ctx0, FILE *file) {
2222 isc__mem_t *ctx = (isc__mem_t *)ctx0;
2224 REQUIRE(VALID_CONTEXT(ctx));
2225 REQUIRE(file != NULL);
2227 #if !ISC_MEM_TRACKLINES
2231 print_active(ctx, file);
2235 ISC_MEMFUNC_SCOPE void
2236 isc__mem_printallactive(FILE *file) {
2237 #if !ISC_MEM_TRACKLINES
2242 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2245 for (ctx = ISC_LIST_HEAD(contexts);
2247 ctx = ISC_LIST_NEXT(ctx, link)) {
2248 fprintf(file, "context: %p\n", ctx);
2249 print_active(ctx, file);
2255 ISC_MEMFUNC_SCOPE void
2256 isc__mem_checkdestroyed(FILE *file) {
2258 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2261 if (!ISC_LIST_EMPTY(contexts)) {
2262 #if ISC_MEM_TRACKLINES
2265 for (ctx = ISC_LIST_HEAD(contexts);
2267 ctx = ISC_LIST_NEXT(ctx, link)) {
2268 fprintf(file, "context: %p\n", ctx);
2269 print_active(ctx, file);
2278 ISC_MEMFUNC_SCOPE unsigned int
2279 isc_mem_references(isc_mem_t *ctx0) {
2280 isc__mem_t *ctx = (isc__mem_t *)ctx0;
2281 unsigned int references;
2283 REQUIRE(VALID_CONTEXT(ctx));
2285 MCTXLOCK(ctx, &ctx->lock);
2286 references = ctx->references;
2287 MCTXUNLOCK(ctx, &ctx->lock);
2289 return (references);
2294 typedef struct summarystat {
2297 isc_uint64_t blocksize;
2298 isc_uint64_t contextsize;
2302 renderctx(isc__mem_t *ctx, summarystat_t *summary, xmlTextWriterPtr writer) {
2303 REQUIRE(VALID_CONTEXT(ctx));
2305 xmlTextWriterStartElement(writer, ISC_XMLCHAR "context");
2307 xmlTextWriterStartElement(writer, ISC_XMLCHAR "id");
2308 xmlTextWriterWriteFormatString(writer, "%p", ctx);
2309 xmlTextWriterEndElement(writer); /* id */
2311 if (ctx->name[0] != 0) {
2312 xmlTextWriterStartElement(writer, ISC_XMLCHAR "name");
2313 xmlTextWriterWriteFormatString(writer, "%s", ctx->name);
2314 xmlTextWriterEndElement(writer); /* name */
2317 REQUIRE(VALID_CONTEXT(ctx));
2318 MCTXLOCK(ctx, &ctx->lock);
2320 summary->contextsize += sizeof(*ctx) +
2321 (ctx->max_size + 1) * sizeof(struct stats) +
2322 ctx->max_size * sizeof(element *) +
2323 ctx->basic_table_count * sizeof(char *);
2324 #if ISC_MEM_TRACKLINES
2325 if (ctx->debuglist != NULL) {
2326 summary->contextsize +=
2327 (ctx->max_size + 1) * sizeof(debuglist_t) +
2328 ctx->debuglistcnt * sizeof(debuglink_t);
2331 xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
2332 xmlTextWriterWriteFormatString(writer, "%d", ctx->references);
2333 xmlTextWriterEndElement(writer); /* references */
2335 summary->total += ctx->total;
2336 xmlTextWriterStartElement(writer, ISC_XMLCHAR "total");
2337 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2338 (isc_uint64_t)ctx->total);
2339 xmlTextWriterEndElement(writer); /* total */
2341 summary->inuse += ctx->inuse;
2342 xmlTextWriterStartElement(writer, ISC_XMLCHAR "inuse");
2343 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2344 (isc_uint64_t)ctx->inuse);
2345 xmlTextWriterEndElement(writer); /* inuse */
2347 xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxinuse");
2348 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2349 (isc_uint64_t)ctx->maxinuse);
2350 xmlTextWriterEndElement(writer); /* maxinuse */
2352 xmlTextWriterStartElement(writer, ISC_XMLCHAR "blocksize");
2353 if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2354 summary->blocksize += ctx->basic_table_count *
2355 NUM_BASIC_BLOCKS * ctx->mem_target;
2356 xmlTextWriterWriteFormatString(writer,
2357 "%" ISC_PRINT_QUADFORMAT "u",
2359 ctx->basic_table_count *
2363 xmlTextWriterWriteFormatString(writer, "%s", "-");
2364 xmlTextWriterEndElement(writer); /* blocksize */
2366 xmlTextWriterStartElement(writer, ISC_XMLCHAR "pools");
2367 xmlTextWriterWriteFormatString(writer, "%u", ctx->poolcnt);
2368 xmlTextWriterEndElement(writer); /* pools */
2369 summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
2371 xmlTextWriterStartElement(writer, ISC_XMLCHAR "hiwater");
2372 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2373 (isc_uint64_t)ctx->hi_water);
2374 xmlTextWriterEndElement(writer); /* hiwater */
2376 xmlTextWriterStartElement(writer, ISC_XMLCHAR "lowater");
2377 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2378 (isc_uint64_t)ctx->lo_water);
2379 xmlTextWriterEndElement(writer); /* lowater */
2381 MCTXUNLOCK(ctx, &ctx->lock);
2383 xmlTextWriterEndElement(writer); /* context */
2387 isc_mem_renderxml(xmlTextWriterPtr writer) {
2389 summarystat_t summary;
2392 memset(&summary, 0, sizeof(summary));
2394 xmlTextWriterStartElement(writer, ISC_XMLCHAR "contexts");
2396 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2400 for (ctx = ISC_LIST_HEAD(contexts);
2402 ctx = ISC_LIST_NEXT(ctx, link)) {
2403 renderctx(ctx, &summary, writer);
2407 xmlTextWriterEndElement(writer); /* contexts */
2409 xmlTextWriterStartElement(writer, ISC_XMLCHAR "summary");
2411 xmlTextWriterStartElement(writer, ISC_XMLCHAR "TotalUse");
2412 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2414 xmlTextWriterEndElement(writer); /* TotalUse */
2416 xmlTextWriterStartElement(writer, ISC_XMLCHAR "InUse");
2417 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2419 xmlTextWriterEndElement(writer); /* InUse */
2421 xmlTextWriterStartElement(writer, ISC_XMLCHAR "BlockSize");
2422 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2424 xmlTextWriterEndElement(writer); /* BlockSize */
2426 xmlTextWriterStartElement(writer, ISC_XMLCHAR "ContextSize");
2427 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2428 summary.contextsize);
2429 xmlTextWriterEndElement(writer); /* ContextSize */
2431 xmlTextWriterStartElement(writer, ISC_XMLCHAR "Lost");
2432 xmlTextWriterWriteFormatString(writer, "%" ISC_PRINT_QUADFORMAT "u",
2434 xmlTextWriterEndElement(writer); /* Lost */
2436 xmlTextWriterEndElement(writer); /* summary */
2439 #endif /* HAVE_LIBXML2 */