mem.c revision 1.14 1 1.14 christos /* $NetBSD: mem.c,v 1.14 2023/06/26 22:03:01 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.12 christos * SPDX-License-Identifier: MPL-2.0
7 1.12 christos *
8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
10 1.8 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 1.1 christos *
12 1.1 christos * See the COPYRIGHT file distributed with this work for additional
13 1.1 christos * information regarding copyright ownership.
14 1.1 christos */
15 1.1 christos
16 1.1 christos /*! \file */
17 1.1 christos
18 1.6 christos #include <errno.h>
19 1.3 christos #include <inttypes.h>
20 1.6 christos #include <limits.h>
21 1.3 christos #include <stdbool.h>
22 1.6 christos #include <stddef.h>
23 1.1 christos #include <stdio.h>
24 1.1 christos #include <stdlib.h>
25 1.1 christos
26 1.1 christos #include <isc/bind9.h>
27 1.3 christos #include <isc/hash.h>
28 1.10 christos #include <isc/lib.h>
29 1.1 christos #include <isc/magic.h>
30 1.1 christos #include <isc/mem.h>
31 1.3 christos #include <isc/mutex.h>
32 1.1 christos #include <isc/once.h>
33 1.3 christos #include <isc/print.h>
34 1.3 christos #include <isc/refcount.h>
35 1.3 christos #include <isc/strerr.h>
36 1.1 christos #include <isc/string.h>
37 1.1 christos #include <isc/util.h>
38 1.6 christos
39 1.6 christos #ifdef HAVE_LIBXML2
40 1.6 christos #include <libxml/xmlwriter.h>
41 1.6 christos #define ISC_XMLCHAR (const xmlChar *)
42 1.6 christos #endif /* HAVE_LIBXML2 */
43 1.6 christos
44 1.6 christos #ifdef HAVE_JSON_C
45 1.6 christos #include <json_object.h>
46 1.6 christos #endif /* HAVE_JSON_C */
47 1.1 christos
48 1.3 christos #include "mem_p.h"
49 1.3 christos
50 1.6 christos #define MCTXLOCK(m) LOCK(&m->lock)
51 1.6 christos #define MCTXUNLOCK(m) UNLOCK(&m->lock)
52 1.1 christos
53 1.1 christos #ifndef ISC_MEM_DEBUGGING
54 1.1 christos #define ISC_MEM_DEBUGGING 0
55 1.6 christos #endif /* ifndef ISC_MEM_DEBUGGING */
56 1.1 christos LIBISC_EXTERNAL_DATA unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING;
57 1.1 christos LIBISC_EXTERNAL_DATA unsigned int isc_mem_defaultflags = ISC_MEMFLAG_DEFAULT;
58 1.1 christos
59 1.1 christos /*
60 1.1 christos * Constants.
61 1.1 christos */
62 1.1 christos
63 1.14 christos #define DEF_MAX_SIZE 1100
64 1.14 christos #define DEF_MEM_TARGET 4096
65 1.14 christos #define ALIGNMENT_SIZE \
66 1.14 christos 8U /*%< must be a power of 2, also update lib/dns/rbt.c */
67 1.6 christos #define NUM_BASIC_BLOCKS 64 /*%< must be > 1 */
68 1.6 christos #define TABLE_INCREMENT 1024
69 1.6 christos #define DEBUG_TABLE_COUNT 512U
70 1.1 christos
71 1.1 christos /*
72 1.1 christos * Types.
73 1.1 christos */
74 1.1 christos typedef struct isc__mem isc__mem_t;
75 1.1 christos typedef struct isc__mempool isc__mempool_t;
76 1.1 christos
77 1.1 christos #if ISC_MEM_TRACKLINES
78 1.1 christos typedef struct debuglink debuglink_t;
79 1.1 christos struct debuglink {
80 1.6 christos ISC_LINK(debuglink_t) link;
81 1.6 christos const void *ptr;
82 1.6 christos size_t size;
83 1.6 christos const char *file;
84 1.6 christos unsigned int line;
85 1.1 christos };
86 1.1 christos
87 1.6 christos typedef ISC_LIST(debuglink_t) debuglist_t;
88 1.1 christos
89 1.6 christos #define FLARG_PASS , file, line
90 1.6 christos #define FLARG , const char *file, unsigned int line
91 1.6 christos #else /* if ISC_MEM_TRACKLINES */
92 1.1 christos #define FLARG_PASS
93 1.1 christos #define FLARG
94 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
95 1.1 christos
96 1.1 christos typedef struct element element;
97 1.1 christos struct element {
98 1.6 christos element *next;
99 1.1 christos };
100 1.1 christos
101 1.1 christos typedef struct {
102 1.1 christos /*!
103 1.1 christos * This structure must be ALIGNMENT_SIZE bytes.
104 1.1 christos */
105 1.1 christos union {
106 1.6 christos size_t size;
107 1.6 christos isc__mem_t *ctx;
108 1.6 christos char bytes[ALIGNMENT_SIZE];
109 1.1 christos } u;
110 1.1 christos } size_info;
111 1.1 christos
112 1.1 christos struct stats {
113 1.6 christos unsigned long gets;
114 1.6 christos unsigned long totalgets;
115 1.6 christos unsigned long blocks;
116 1.6 christos unsigned long freefrags;
117 1.1 christos };
118 1.1 christos
119 1.6 christos #define MEM_MAGIC ISC_MAGIC('M', 'e', 'm', 'C')
120 1.6 christos #define VALID_CONTEXT(c) ISC_MAGIC_VALID(c, MEM_MAGIC)
121 1.1 christos
122 1.1 christos /* List of all active memory contexts. */
123 1.1 christos
124 1.6 christos static ISC_LIST(isc__mem_t) contexts;
125 1.1 christos
126 1.10 christos static isc_once_t init_once = ISC_ONCE_INIT;
127 1.10 christos static isc_once_t shut_once = ISC_ONCE_INIT;
128 1.6 christos static isc_mutex_t contextslock;
129 1.1 christos
130 1.1 christos /*%
131 1.1 christos * Total size of lost memory due to a bug of external library.
132 1.1 christos * Locked by the global lock.
133 1.1 christos */
134 1.6 christos static uint64_t totallost;
135 1.6 christos
136 1.6 christos /*%
137 1.6 christos * Memory allocation and free function definitions.
138 1.6 christos * isc__memalloc_t must deal with memory allocation failure
139 1.6 christos * and must never return NULL.
140 1.6 christos */
141 1.6 christos typedef void *(*isc__memalloc_t)(size_t);
142 1.6 christos typedef void (*isc__memfree_t)(void *);
143 1.1 christos
144 1.1 christos struct isc__mem {
145 1.6 christos isc_mem_t common;
146 1.6 christos unsigned int flags;
147 1.6 christos isc_mutex_t lock;
148 1.6 christos isc__memalloc_t memalloc;
149 1.6 christos isc__memfree_t memfree;
150 1.6 christos size_t max_size;
151 1.6 christos bool checkfree;
152 1.6 christos struct stats *stats;
153 1.6 christos isc_refcount_t references;
154 1.6 christos char name[16];
155 1.6 christos void *tag;
156 1.6 christos size_t total;
157 1.6 christos size_t inuse;
158 1.6 christos size_t maxinuse;
159 1.6 christos size_t malloced;
160 1.6 christos size_t maxmalloced;
161 1.6 christos size_t hi_water;
162 1.6 christos size_t lo_water;
163 1.6 christos bool hi_called;
164 1.6 christos bool is_overmem;
165 1.6 christos isc_mem_water_t water;
166 1.6 christos void *water_arg;
167 1.1 christos ISC_LIST(isc__mempool_t) pools;
168 1.6 christos unsigned int poolcnt;
169 1.1 christos
170 1.1 christos /* ISC_MEMFLAG_INTERNAL */
171 1.6 christos size_t mem_target;
172 1.6 christos element **freelists;
173 1.6 christos element *basic_blocks;
174 1.6 christos unsigned char **basic_table;
175 1.6 christos unsigned int basic_table_count;
176 1.6 christos unsigned int basic_table_size;
177 1.6 christos unsigned char *lowest;
178 1.6 christos unsigned char *highest;
179 1.1 christos
180 1.1 christos #if ISC_MEM_TRACKLINES
181 1.6 christos debuglist_t *debuglist;
182 1.6 christos size_t debuglistcnt;
183 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
184 1.1 christos
185 1.6 christos ISC_LINK(isc__mem_t) link;
186 1.1 christos };
187 1.1 christos
188 1.6 christos #define MEMPOOL_MAGIC ISC_MAGIC('M', 'E', 'M', 'p')
189 1.6 christos #define VALID_MEMPOOL(c) ISC_MAGIC_VALID(c, MEMPOOL_MAGIC)
190 1.1 christos
191 1.1 christos struct isc__mempool {
192 1.1 christos /* always unlocked */
193 1.12 christos isc_mempool_t common; /*%< common header of mempool's */
194 1.12 christos isc__mem_t *mctx; /*%< our memory context */
195 1.6 christos ISC_LINK(isc__mempool_t) link; /*%< next pool in this mem context */
196 1.12 christos element *items; /*%< low water item list */
197 1.12 christos size_t size; /*%< size of each item on this pool */
198 1.12 christos unsigned int maxalloc; /*%< max number of items allowed */
199 1.12 christos unsigned int allocated; /*%< # of items currently given out */
200 1.12 christos unsigned int freecount; /*%< # of items on reserved list */
201 1.12 christos unsigned int freemax; /*%< # of items allowed on free list */
202 1.12 christos unsigned int fillcount; /*%< # of items to fetch on each fill */
203 1.1 christos /*%< Stats only. */
204 1.6 christos unsigned int gets; /*%< # of requests to this pool */
205 1.6 christos /*%< Debugging only. */
206 1.1 christos #if ISC_MEMPOOL_NAMES
207 1.6 christos char name[16]; /*%< printed name in stats reports */
208 1.6 christos #endif /* if ISC_MEMPOOL_NAMES */
209 1.1 christos };
210 1.1 christos
211 1.1 christos /*
212 1.1 christos * Private Inline-able.
213 1.1 christos */
214 1.1 christos
215 1.6 christos #if !ISC_MEM_TRACKLINES
216 1.1 christos #define ADD_TRACE(a, b, c, d, e)
217 1.1 christos #define DELETE_TRACE(a, b, c, d, e)
218 1.1 christos #define ISC_MEMFUNC_SCOPE
219 1.6 christos #else /* if !ISC_MEM_TRACKLINES */
220 1.6 christos #define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE | ISC_MEM_DEBUGRECORD)
221 1.6 christos #define ADD_TRACE(a, b, c, d, e) \
222 1.6 christos do { \
223 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0 && \
224 1.6 christos b != NULL)) \
225 1.6 christos add_trace_entry(a, b, c, d, e); \
226 1.9 rillig } while (0)
227 1.6 christos #define DELETE_TRACE(a, b, c, d, e) \
228 1.6 christos do { \
229 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0 && \
230 1.6 christos b != NULL)) \
231 1.6 christos delete_trace_entry(a, b, c, d, e); \
232 1.9 rillig } while (0)
233 1.1 christos
234 1.1 christos static void
235 1.1 christos print_active(isc__mem_t *ctx, FILE *out);
236 1.1 christos
237 1.1 christos #endif /* ISC_MEM_TRACKLINES */
238 1.1 christos
239 1.3 christos static void *
240 1.1 christos isc___mem_get(isc_mem_t *ctx, size_t size FLARG);
241 1.3 christos static void
242 1.1 christos isc___mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG);
243 1.3 christos static void
244 1.3 christos isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG);
245 1.3 christos static void *
246 1.1 christos isc___mem_allocate(isc_mem_t *ctx, size_t size FLARG);
247 1.3 christos static void *
248 1.1 christos isc___mem_reallocate(isc_mem_t *ctx, void *ptr, size_t size FLARG);
249 1.3 christos static char *
250 1.3 christos isc___mem_strdup(isc_mem_t *mctx, const char *s FLARG);
251 1.11 christos static char *
252 1.11 christos isc___mem_strndup(isc_mem_t *mctx0, const char *s, size_t size FLARG);
253 1.3 christos static void
254 1.1 christos isc___mem_free(isc_mem_t *ctx, void *ptr FLARG);
255 1.1 christos
256 1.3 christos static isc_memmethods_t memmethods = {
257 1.6 christos isc___mem_get, isc___mem_put, isc___mem_putanddetach,
258 1.6 christos isc___mem_allocate, isc___mem_reallocate, isc___mem_strdup,
259 1.11 christos isc___mem_strndup, isc___mem_free,
260 1.1 christos };
261 1.1 christos
262 1.1 christos #if ISC_MEM_TRACKLINES
263 1.1 christos /*!
264 1.1 christos * mctx must be locked.
265 1.1 christos */
266 1.1 christos static void
267 1.1 christos add_trace_entry(isc__mem_t *mctx, const void *ptr, size_t size FLARG) {
268 1.1 christos debuglink_t *dl;
269 1.3 christos uint32_t hash;
270 1.3 christos uint32_t idx;
271 1.1 christos
272 1.4 christos if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
273 1.4 christos fprintf(stderr, "add %p size %zu file %s line %u mctx %p\n",
274 1.1 christos ptr, size, file, line, mctx);
275 1.4 christos }
276 1.1 christos
277 1.6 christos if (mctx->debuglist == NULL) {
278 1.1 christos return;
279 1.6 christos }
280 1.1 christos
281 1.6 christos #ifdef __COVERITY__
282 1.6 christos /*
283 1.6 christos * Use simple conversion from pointer to hash to avoid
284 1.6 christos * tainting 'ptr' due to byte swap in isc_hash_function.
285 1.6 christos */
286 1.6 christos hash = (uintptr_t)ptr >> 3;
287 1.6 christos #else
288 1.5 christos hash = isc_hash_function(&ptr, sizeof(ptr), true);
289 1.6 christos #endif
290 1.1 christos idx = hash % DEBUG_TABLE_COUNT;
291 1.1 christos
292 1.1 christos dl = malloc(sizeof(debuglink_t));
293 1.1 christos INSIST(dl != NULL);
294 1.1 christos mctx->malloced += sizeof(debuglink_t);
295 1.6 christos if (mctx->malloced > mctx->maxmalloced) {
296 1.1 christos mctx->maxmalloced = mctx->malloced;
297 1.6 christos }
298 1.1 christos
299 1.1 christos ISC_LINK_INIT(dl, link);
300 1.1 christos dl->ptr = ptr;
301 1.1 christos dl->size = size;
302 1.1 christos dl->file = file;
303 1.1 christos dl->line = line;
304 1.1 christos
305 1.1 christos ISC_LIST_PREPEND(mctx->debuglist[idx], dl, link);
306 1.1 christos mctx->debuglistcnt++;
307 1.1 christos }
308 1.1 christos
309 1.1 christos static void
310 1.1 christos delete_trace_entry(isc__mem_t *mctx, const void *ptr, size_t size,
311 1.6 christos const char *file, unsigned int line) {
312 1.1 christos debuglink_t *dl;
313 1.3 christos uint32_t hash;
314 1.3 christos uint32_t idx;
315 1.1 christos
316 1.4 christos if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
317 1.4 christos fprintf(stderr, "del %p size %zu file %s line %u mctx %p\n",
318 1.1 christos ptr, size, file, line, mctx);
319 1.4 christos }
320 1.1 christos
321 1.6 christos if (mctx->debuglist == NULL) {
322 1.1 christos return;
323 1.6 christos }
324 1.1 christos
325 1.6 christos #ifdef __COVERITY__
326 1.6 christos /*
327 1.6 christos * Use simple conversion from pointer to hash to avoid
328 1.6 christos * tainting 'ptr' due to byte swap in isc_hash_function.
329 1.6 christos */
330 1.6 christos hash = (uintptr_t)ptr >> 3;
331 1.6 christos #else
332 1.5 christos hash = isc_hash_function(&ptr, sizeof(ptr), true);
333 1.6 christos #endif
334 1.1 christos idx = hash % DEBUG_TABLE_COUNT;
335 1.1 christos
336 1.1 christos dl = ISC_LIST_HEAD(mctx->debuglist[idx]);
337 1.1 christos while (ISC_LIKELY(dl != NULL)) {
338 1.1 christos if (ISC_UNLIKELY(dl->ptr == ptr)) {
339 1.1 christos ISC_LIST_UNLINK(mctx->debuglist[idx], dl, link);
340 1.1 christos mctx->malloced -= sizeof(*dl);
341 1.1 christos free(dl);
342 1.1 christos return;
343 1.1 christos }
344 1.1 christos dl = ISC_LIST_NEXT(dl, link);
345 1.1 christos }
346 1.1 christos
347 1.1 christos /*
348 1.1 christos * If we get here, we didn't find the item on the list. We're
349 1.1 christos * screwed.
350 1.1 christos */
351 1.12 christos UNREACHABLE();
352 1.1 christos }
353 1.1 christos #endif /* ISC_MEM_TRACKLINES */
354 1.1 christos
355 1.12 christos static size_t
356 1.1 christos rmsize(size_t size) {
357 1.1 christos /*
358 1.1 christos * round down to ALIGNMENT_SIZE
359 1.1 christos */
360 1.1 christos return (size & (~(ALIGNMENT_SIZE - 1)));
361 1.1 christos }
362 1.1 christos
363 1.12 christos static size_t
364 1.1 christos quantize(size_t size) {
365 1.1 christos /*!
366 1.1 christos * Round up the result in order to get a size big
367 1.1 christos * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
368 1.1 christos * byte boundaries.
369 1.1 christos */
370 1.1 christos
371 1.6 christos if (size == 0U) {
372 1.1 christos return (ALIGNMENT_SIZE);
373 1.6 christos }
374 1.1 christos return ((size + ALIGNMENT_SIZE - 1) & (~(ALIGNMENT_SIZE - 1)));
375 1.1 christos }
376 1.1 christos
377 1.12 christos static void
378 1.1 christos more_basic_blocks(isc__mem_t *ctx) {
379 1.1 christos void *tmp;
380 1.1 christos unsigned char *curr, *next;
381 1.1 christos unsigned char *first, *last;
382 1.1 christos unsigned char **table;
383 1.1 christos unsigned int table_size;
384 1.1 christos
385 1.1 christos /* Require: we hold the context lock. */
386 1.1 christos
387 1.1 christos INSIST(ctx->basic_table_count <= ctx->basic_table_size);
388 1.1 christos if (ctx->basic_table_count == ctx->basic_table_size) {
389 1.1 christos table_size = ctx->basic_table_size + TABLE_INCREMENT;
390 1.6 christos table = (ctx->memalloc)(table_size * sizeof(unsigned char *));
391 1.1 christos ctx->malloced += table_size * sizeof(unsigned char *);
392 1.6 christos if (ctx->malloced > ctx->maxmalloced) {
393 1.1 christos ctx->maxmalloced = ctx->malloced;
394 1.6 christos }
395 1.1 christos if (ctx->basic_table_size != 0) {
396 1.1 christos memmove(table, ctx->basic_table,
397 1.1 christos ctx->basic_table_size *
398 1.6 christos sizeof(unsigned char *));
399 1.6 christos (ctx->memfree)(ctx->basic_table);
400 1.1 christos ctx->malloced -= ctx->basic_table_size *
401 1.1 christos sizeof(unsigned char *);
402 1.1 christos }
403 1.1 christos ctx->basic_table = table;
404 1.1 christos ctx->basic_table_size = table_size;
405 1.1 christos }
406 1.1 christos
407 1.6 christos tmp = (ctx->memalloc)(NUM_BASIC_BLOCKS * ctx->mem_target);
408 1.6 christos ctx->total += NUM_BASIC_BLOCKS * ctx->mem_target;
409 1.1 christos ctx->basic_table[ctx->basic_table_count] = tmp;
410 1.1 christos ctx->basic_table_count++;
411 1.1 christos ctx->malloced += NUM_BASIC_BLOCKS * ctx->mem_target;
412 1.6 christos if (ctx->malloced > ctx->maxmalloced) {
413 1.6 christos ctx->maxmalloced = ctx->malloced;
414 1.6 christos }
415 1.1 christos
416 1.1 christos curr = tmp;
417 1.1 christos next = curr + ctx->mem_target;
418 1.6 christos for (int i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
419 1.1 christos ((element *)curr)->next = (element *)next;
420 1.1 christos curr = next;
421 1.1 christos next += ctx->mem_target;
422 1.1 christos }
423 1.1 christos /*
424 1.1 christos * curr is now pointing at the last block in the
425 1.1 christos * array.
426 1.1 christos */
427 1.1 christos ((element *)curr)->next = NULL;
428 1.1 christos first = tmp;
429 1.1 christos last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
430 1.6 christos if (first < ctx->lowest || ctx->lowest == NULL) {
431 1.1 christos ctx->lowest = first;
432 1.6 christos }
433 1.6 christos if (last > ctx->highest) {
434 1.1 christos ctx->highest = last;
435 1.6 christos }
436 1.1 christos ctx->basic_blocks = tmp;
437 1.1 christos }
438 1.1 christos
439 1.12 christos static void
440 1.1 christos more_frags(isc__mem_t *ctx, size_t new_size) {
441 1.6 christos int frags;
442 1.1 christos size_t total_size;
443 1.1 christos void *tmp;
444 1.1 christos unsigned char *curr, *next;
445 1.1 christos
446 1.1 christos /*!
447 1.1 christos * Try to get more fragments by chopping up a basic block.
448 1.1 christos */
449 1.1 christos
450 1.1 christos if (ctx->basic_blocks == NULL) {
451 1.6 christos more_basic_blocks(ctx);
452 1.1 christos }
453 1.7 christos INSIST(ctx->basic_blocks != NULL);
454 1.1 christos
455 1.1 christos total_size = ctx->mem_target;
456 1.1 christos tmp = ctx->basic_blocks;
457 1.1 christos ctx->basic_blocks = ctx->basic_blocks->next;
458 1.1 christos frags = (int)(total_size / new_size);
459 1.1 christos ctx->stats[new_size].blocks++;
460 1.1 christos ctx->stats[new_size].freefrags += frags;
461 1.1 christos /*
462 1.1 christos * Set up a linked-list of blocks of size
463 1.1 christos * "new_size".
464 1.1 christos */
465 1.1 christos curr = tmp;
466 1.1 christos next = curr + new_size;
467 1.1 christos total_size -= new_size;
468 1.6 christos for (int i = 0; i < (frags - 1); i++) {
469 1.1 christos ((element *)curr)->next = (element *)next;
470 1.1 christos curr = next;
471 1.1 christos next += new_size;
472 1.1 christos total_size -= new_size;
473 1.1 christos }
474 1.1 christos /*
475 1.1 christos * Add the remaining fragment of the basic block to a free list.
476 1.1 christos */
477 1.1 christos total_size = rmsize(total_size);
478 1.1 christos if (total_size > 0U) {
479 1.1 christos ((element *)next)->next = ctx->freelists[total_size];
480 1.1 christos ctx->freelists[total_size] = (element *)next;
481 1.1 christos ctx->stats[total_size].freefrags++;
482 1.1 christos }
483 1.1 christos /*
484 1.1 christos * curr is now pointing at the last block in the
485 1.1 christos * array.
486 1.1 christos */
487 1.1 christos ((element *)curr)->next = NULL;
488 1.1 christos ctx->freelists[new_size] = tmp;
489 1.1 christos }
490 1.1 christos
491 1.12 christos static void *
492 1.1 christos mem_getunlocked(isc__mem_t *ctx, size_t size) {
493 1.1 christos size_t new_size = quantize(size);
494 1.1 christos void *ret;
495 1.1 christos
496 1.1 christos if (new_size >= ctx->max_size) {
497 1.1 christos /*
498 1.1 christos * memget() was called on something beyond our upper limit.
499 1.1 christos */
500 1.6 christos ret = (ctx->memalloc)(size);
501 1.1 christos ctx->total += size;
502 1.1 christos ctx->inuse += size;
503 1.1 christos ctx->stats[ctx->max_size].gets++;
504 1.1 christos ctx->stats[ctx->max_size].totalgets++;
505 1.1 christos ctx->malloced += size;
506 1.6 christos if (ctx->malloced > ctx->maxmalloced) {
507 1.1 christos ctx->maxmalloced = ctx->malloced;
508 1.6 christos }
509 1.1 christos /*
510 1.1 christos * If we don't set new_size to size, then the
511 1.1 christos * ISC_MEMFLAG_FILL code might write over bytes we don't
512 1.1 christos * own.
513 1.1 christos */
514 1.1 christos new_size = size;
515 1.1 christos goto done;
516 1.1 christos }
517 1.1 christos /*
518 1.1 christos * If there are no blocks in the free list for this size, get a chunk
519 1.1 christos * of memory and then break it up into "new_size"-sized blocks, adding
520 1.1 christos * them to the free list.
521 1.1 christos */
522 1.6 christos if (ctx->freelists[new_size] == NULL) {
523 1.6 christos more_frags(ctx, new_size);
524 1.6 christos }
525 1.7 christos INSIST(ctx->freelists[new_size] != NULL);
526 1.1 christos
527 1.1 christos /*
528 1.1 christos * The free list uses the "rounded-up" size "new_size".
529 1.1 christos */
530 1.1 christos
531 1.1 christos ret = ctx->freelists[new_size];
532 1.1 christos ctx->freelists[new_size] = ctx->freelists[new_size]->next;
533 1.1 christos
534 1.1 christos /*
535 1.1 christos * The stats[] uses the _actual_ "size" requested by the
536 1.1 christos * caller, with the caveat (in the code above) that "size" >= the
537 1.1 christos * max. size (max_size) ends up getting recorded as a call to
538 1.1 christos * max_size.
539 1.1 christos */
540 1.1 christos ctx->stats[size].gets++;
541 1.1 christos ctx->stats[size].totalgets++;
542 1.1 christos ctx->stats[new_size].freefrags--;
543 1.1 christos ctx->inuse += new_size;
544 1.1 christos
545 1.6 christos done:
546 1.1 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0) &&
547 1.1 christos ISC_LIKELY(ret != NULL))
548 1.6 christos {
549 1.1 christos memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
550 1.6 christos }
551 1.1 christos
552 1.1 christos return (ret);
553 1.1 christos }
554 1.1 christos
555 1.1 christos #if ISC_MEM_CHECKOVERRUN
556 1.12 christos static void
557 1.1 christos check_overrun(void *mem, size_t size, size_t new_size) {
558 1.1 christos unsigned char *cp;
559 1.1 christos
560 1.1 christos cp = (unsigned char *)mem;
561 1.1 christos cp += size;
562 1.1 christos while (size < new_size) {
563 1.1 christos INSIST(*cp == 0xbe);
564 1.1 christos cp++;
565 1.1 christos size++;
566 1.1 christos }
567 1.1 christos }
568 1.6 christos #endif /* if ISC_MEM_CHECKOVERRUN */
569 1.1 christos
570 1.1 christos /* coverity[+free : arg-1] */
571 1.12 christos static void
572 1.1 christos mem_putunlocked(isc__mem_t *ctx, void *mem, size_t size) {
573 1.1 christos size_t new_size = quantize(size);
574 1.1 christos
575 1.1 christos if (new_size >= ctx->max_size) {
576 1.1 christos /*
577 1.1 christos * memput() called on something beyond our upper limit.
578 1.1 christos */
579 1.6 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
580 1.1 christos memset(mem, 0xde, size); /* Mnemonic for "dead". */
581 1.6 christos }
582 1.1 christos
583 1.6 christos (ctx->memfree)(mem);
584 1.1 christos INSIST(ctx->stats[ctx->max_size].gets != 0U);
585 1.1 christos ctx->stats[ctx->max_size].gets--;
586 1.1 christos INSIST(size <= ctx->inuse);
587 1.1 christos ctx->inuse -= size;
588 1.1 christos ctx->malloced -= size;
589 1.1 christos return;
590 1.1 christos }
591 1.1 christos
592 1.1 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
593 1.1 christos #if ISC_MEM_CHECKOVERRUN
594 1.1 christos check_overrun(mem, size, new_size);
595 1.6 christos #endif /* if ISC_MEM_CHECKOVERRUN */
596 1.1 christos memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
597 1.1 christos }
598 1.1 christos
599 1.1 christos /*
600 1.1 christos * The free list uses the "rounded-up" size "new_size".
601 1.1 christos */
602 1.1 christos ((element *)mem)->next = ctx->freelists[new_size];
603 1.1 christos ctx->freelists[new_size] = (element *)mem;
604 1.1 christos
605 1.1 christos /*
606 1.1 christos * The stats[] uses the _actual_ "size" requested by the
607 1.1 christos * caller, with the caveat (in the code above) that "size" >= the
608 1.1 christos * max. size (max_size) ends up getting recorded as a call to
609 1.1 christos * max_size.
610 1.1 christos */
611 1.1 christos INSIST(ctx->stats[size].gets != 0U);
612 1.1 christos ctx->stats[size].gets--;
613 1.1 christos ctx->stats[new_size].freefrags++;
614 1.1 christos ctx->inuse -= new_size;
615 1.1 christos }
616 1.1 christos
617 1.1 christos /*!
618 1.1 christos * Perform a malloc, doing memory filling and overrun detection as necessary.
619 1.1 christos */
620 1.12 christos static void *
621 1.1 christos mem_get(isc__mem_t *ctx, size_t size) {
622 1.1 christos char *ret;
623 1.1 christos
624 1.1 christos #if ISC_MEM_CHECKOVERRUN
625 1.1 christos size += 1;
626 1.6 christos #endif /* if ISC_MEM_CHECKOVERRUN */
627 1.6 christos ret = (ctx->memalloc)(size);
628 1.1 christos
629 1.1 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
630 1.6 christos if (ISC_LIKELY(ret != NULL)) {
631 1.1 christos memset(ret, 0xbe, size); /* Mnemonic for "beef". */
632 1.6 christos }
633 1.1 christos }
634 1.1 christos #if ISC_MEM_CHECKOVERRUN
635 1.6 christos else
636 1.6 christos {
637 1.6 christos if (ISC_LIKELY(ret != NULL)) {
638 1.6 christos ret[size - 1] = 0xbe;
639 1.6 christos }
640 1.1 christos }
641 1.6 christos #endif /* if ISC_MEM_CHECKOVERRUN */
642 1.1 christos
643 1.1 christos return (ret);
644 1.1 christos }
645 1.1 christos
646 1.1 christos /*!
647 1.1 christos * Perform a free, doing memory filling and overrun detection as necessary.
648 1.1 christos */
649 1.1 christos /* coverity[+free : arg-1] */
650 1.12 christos static void
651 1.1 christos mem_put(isc__mem_t *ctx, void *mem, size_t size) {
652 1.1 christos #if ISC_MEM_CHECKOVERRUN
653 1.1 christos INSIST(((unsigned char *)mem)[size] == 0xbe);
654 1.1 christos size += 1;
655 1.6 christos #endif /* if ISC_MEM_CHECKOVERRUN */
656 1.6 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
657 1.1 christos memset(mem, 0xde, size); /* Mnemonic for "dead". */
658 1.6 christos }
659 1.6 christos (ctx->memfree)(mem);
660 1.1 christos }
661 1.1 christos
662 1.1 christos /*!
663 1.1 christos * Update internal counters after a memory get.
664 1.1 christos */
665 1.12 christos static void
666 1.1 christos mem_getstats(isc__mem_t *ctx, size_t size) {
667 1.1 christos ctx->total += size;
668 1.1 christos ctx->inuse += size;
669 1.1 christos
670 1.1 christos if (size > ctx->max_size) {
671 1.1 christos ctx->stats[ctx->max_size].gets++;
672 1.1 christos ctx->stats[ctx->max_size].totalgets++;
673 1.1 christos } else {
674 1.1 christos ctx->stats[size].gets++;
675 1.1 christos ctx->stats[size].totalgets++;
676 1.1 christos }
677 1.1 christos
678 1.1 christos #if ISC_MEM_CHECKOVERRUN
679 1.1 christos size += 1;
680 1.6 christos #endif /* if ISC_MEM_CHECKOVERRUN */
681 1.1 christos ctx->malloced += size;
682 1.6 christos if (ctx->malloced > ctx->maxmalloced) {
683 1.1 christos ctx->maxmalloced = ctx->malloced;
684 1.6 christos }
685 1.1 christos }
686 1.1 christos
687 1.1 christos /*!
688 1.1 christos * Update internal counters after a memory put.
689 1.1 christos */
690 1.12 christos static void
691 1.1 christos mem_putstats(isc__mem_t *ctx, void *ptr, size_t size) {
692 1.1 christos UNUSED(ptr);
693 1.1 christos
694 1.1 christos INSIST(ctx->inuse >= size);
695 1.1 christos ctx->inuse -= size;
696 1.1 christos
697 1.1 christos if (size > ctx->max_size) {
698 1.1 christos INSIST(ctx->stats[ctx->max_size].gets > 0U);
699 1.1 christos ctx->stats[ctx->max_size].gets--;
700 1.1 christos } else {
701 1.1 christos INSIST(ctx->stats[size].gets > 0U);
702 1.1 christos ctx->stats[size].gets--;
703 1.1 christos }
704 1.1 christos #if ISC_MEM_CHECKOVERRUN
705 1.1 christos size += 1;
706 1.6 christos #endif /* if ISC_MEM_CHECKOVERRUN */
707 1.1 christos ctx->malloced -= size;
708 1.1 christos }
709 1.1 christos
710 1.1 christos /*
711 1.1 christos * Private.
712 1.1 christos */
713 1.1 christos
714 1.1 christos static void *
715 1.6 christos default_memalloc(size_t size) {
716 1.3 christos void *ptr;
717 1.3 christos
718 1.3 christos ptr = malloc(size);
719 1.3 christos
720 1.3 christos /*
721 1.3 christos * If the space cannot be allocated, a null pointer is returned. If the
722 1.3 christos * size of the space requested is zero, the behavior is
723 1.3 christos * implementation-defined: either a null pointer is returned, or the
724 1.3 christos * behavior is as if the size were some nonzero value, except that the
725 1.3 christos * returned pointer shall not be used to access an object.
726 1.3 christos * [ISO9899 7.22.3]
727 1.3 christos *
728 1.3 christos * [ISO9899]
729 1.3 christos * ISO/IEC WG 9899:2011: Programming languages - C.
730 1.6 christos * International Organization for Standardization, Geneva,
731 1.6 christos * Switzerland.
732 1.3 christos * http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf
733 1.3 christos */
734 1.3 christos
735 1.3 christos if (ptr == NULL && size != 0) {
736 1.3 christos char strbuf[ISC_STRERRORSIZE];
737 1.3 christos strerror_r(errno, strbuf, sizeof(strbuf));
738 1.3 christos isc_error_fatal(__FILE__, __LINE__, "malloc failed: %s",
739 1.3 christos strbuf);
740 1.3 christos }
741 1.3 christos
742 1.3 christos return (ptr);
743 1.1 christos }
744 1.1 christos
745 1.1 christos static void
746 1.6 christos default_memfree(void *ptr) {
747 1.1 christos free(ptr);
748 1.1 christos }
749 1.1 christos
750 1.1 christos static void
751 1.10 christos mem_initialize(void) {
752 1.3 christos isc_mutex_init(&contextslock);
753 1.1 christos ISC_LIST_INIT(contexts);
754 1.1 christos totallost = 0;
755 1.1 christos }
756 1.1 christos
757 1.10 christos void
758 1.10 christos isc__mem_initialize(void) {
759 1.10 christos RUNTIME_CHECK(isc_once_do(&init_once, mem_initialize) == ISC_R_SUCCESS);
760 1.10 christos }
761 1.10 christos
762 1.10 christos static void
763 1.10 christos mem_shutdown(void) {
764 1.10 christos isc__mem_checkdestroyed();
765 1.10 christos
766 1.10 christos isc_mutex_destroy(&contextslock);
767 1.10 christos }
768 1.10 christos
769 1.10 christos void
770 1.10 christos isc__mem_shutdown(void) {
771 1.10 christos RUNTIME_CHECK(isc_once_do(&shut_once, mem_shutdown) == ISC_R_SUCCESS);
772 1.10 christos }
773 1.10 christos
774 1.6 christos static void
775 1.6 christos mem_create(isc_mem_t **ctxp, unsigned int flags) {
776 1.6 christos REQUIRE(ctxp != NULL && *ctxp == NULL);
777 1.10 christos #if __SANITIZE_ADDRESS__
778 1.10 christos REQUIRE((flags & ISC_MEMFLAG_INTERNAL) == 0);
779 1.10 christos #endif
780 1.1 christos
781 1.1 christos isc__mem_t *ctx;
782 1.1 christos
783 1.10 christos isc_enable_constructors();
784 1.10 christos
785 1.3 christos STATIC_ASSERT((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0,
786 1.3 christos "wrong alignment size");
787 1.1 christos
788 1.6 christos ctx = (default_memalloc)(sizeof(*ctx));
789 1.1 christos
790 1.6 christos isc_mutex_init(&ctx->lock);
791 1.1 christos
792 1.6 christos ctx->max_size = DEF_MAX_SIZE;
793 1.1 christos ctx->flags = flags;
794 1.3 christos isc_refcount_init(&ctx->references, 1);
795 1.1 christos memset(ctx->name, 0, sizeof(ctx->name));
796 1.1 christos ctx->tag = NULL;
797 1.1 christos ctx->total = 0;
798 1.1 christos ctx->inuse = 0;
799 1.1 christos ctx->maxinuse = 0;
800 1.1 christos ctx->malloced = sizeof(*ctx);
801 1.1 christos ctx->maxmalloced = sizeof(*ctx);
802 1.1 christos ctx->hi_water = 0;
803 1.1 christos ctx->lo_water = 0;
804 1.3 christos ctx->hi_called = false;
805 1.3 christos ctx->is_overmem = false;
806 1.1 christos ctx->water = NULL;
807 1.1 christos ctx->water_arg = NULL;
808 1.1 christos ctx->common.impmagic = MEM_MAGIC;
809 1.1 christos ctx->common.magic = ISCAPI_MCTX_MAGIC;
810 1.1 christos ctx->common.methods = (isc_memmethods_t *)&memmethods;
811 1.6 christos ctx->memalloc = default_memalloc;
812 1.6 christos ctx->memfree = default_memfree;
813 1.1 christos ctx->stats = NULL;
814 1.3 christos ctx->checkfree = true;
815 1.1 christos #if ISC_MEM_TRACKLINES
816 1.1 christos ctx->debuglist = NULL;
817 1.1 christos ctx->debuglistcnt = 0;
818 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
819 1.1 christos ISC_LIST_INIT(ctx->pools);
820 1.1 christos ctx->poolcnt = 0;
821 1.1 christos ctx->freelists = NULL;
822 1.1 christos ctx->basic_blocks = NULL;
823 1.1 christos ctx->basic_table = NULL;
824 1.1 christos ctx->basic_table_count = 0;
825 1.1 christos ctx->basic_table_size = 0;
826 1.1 christos ctx->lowest = NULL;
827 1.1 christos ctx->highest = NULL;
828 1.1 christos
829 1.6 christos ctx->stats =
830 1.6 christos (ctx->memalloc)((ctx->max_size + 1) * sizeof(struct stats));
831 1.3 christos
832 1.1 christos memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
833 1.6 christos ctx->malloced += (ctx->max_size + 1) * sizeof(struct stats);
834 1.6 christos ctx->maxmalloced += (ctx->max_size + 1) * sizeof(struct stats);
835 1.1 christos
836 1.1 christos if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
837 1.6 christos ctx->mem_target = DEF_MEM_TARGET;
838 1.6 christos ctx->freelists =
839 1.6 christos (ctx->memalloc)(ctx->max_size * sizeof(element *));
840 1.6 christos memset(ctx->freelists, 0, ctx->max_size * sizeof(element *));
841 1.1 christos ctx->malloced += ctx->max_size * sizeof(element *);
842 1.1 christos ctx->maxmalloced += ctx->max_size * sizeof(element *);
843 1.1 christos }
844 1.1 christos
845 1.1 christos #if ISC_MEM_TRACKLINES
846 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0)) {
847 1.1 christos unsigned int i;
848 1.1 christos
849 1.6 christos ctx->debuglist = (ctx->memalloc)(
850 1.6 christos (DEBUG_TABLE_COUNT * sizeof(debuglist_t)));
851 1.6 christos for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
852 1.1 christos ISC_LIST_INIT(ctx->debuglist[i]);
853 1.6 christos }
854 1.1 christos ctx->malloced += DEBUG_TABLE_COUNT * sizeof(debuglist_t);
855 1.1 christos ctx->maxmalloced += DEBUG_TABLE_COUNT * sizeof(debuglist_t);
856 1.1 christos }
857 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
858 1.1 christos
859 1.1 christos LOCK(&contextslock);
860 1.1 christos ISC_LIST_INITANDAPPEND(contexts, ctx, link);
861 1.1 christos UNLOCK(&contextslock);
862 1.1 christos
863 1.1 christos *ctxp = (isc_mem_t *)ctx;
864 1.6 christos }
865 1.3 christos
866 1.6 christos /*
867 1.6 christos * Public.
868 1.6 christos */
869 1.1 christos
870 1.1 christos static void
871 1.1 christos destroy(isc__mem_t *ctx) {
872 1.1 christos unsigned int i;
873 1.1 christos
874 1.1 christos LOCK(&contextslock);
875 1.1 christos ISC_LIST_UNLINK(contexts, ctx, link);
876 1.1 christos totallost += ctx->inuse;
877 1.1 christos UNLOCK(&contextslock);
878 1.1 christos
879 1.1 christos ctx->common.impmagic = 0;
880 1.1 christos ctx->common.magic = 0;
881 1.1 christos
882 1.1 christos INSIST(ISC_LIST_EMPTY(ctx->pools));
883 1.1 christos
884 1.1 christos #if ISC_MEM_TRACKLINES
885 1.1 christos if (ISC_UNLIKELY(ctx->debuglist != NULL)) {
886 1.1 christos debuglink_t *dl;
887 1.6 christos for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
888 1.6 christos for (dl = ISC_LIST_HEAD(ctx->debuglist[i]); dl != NULL;
889 1.6 christos dl = ISC_LIST_HEAD(ctx->debuglist[i]))
890 1.6 christos {
891 1.6 christos if (ctx->checkfree && dl->ptr != NULL) {
892 1.1 christos print_active(ctx, stderr);
893 1.6 christos }
894 1.6 christos INSIST(!ctx->checkfree || dl->ptr == NULL);
895 1.1 christos
896 1.6 christos ISC_LIST_UNLINK(ctx->debuglist[i], dl, link);
897 1.1 christos free(dl);
898 1.1 christos ctx->malloced -= sizeof(*dl);
899 1.1 christos }
900 1.6 christos }
901 1.1 christos
902 1.6 christos (ctx->memfree)(ctx->debuglist);
903 1.1 christos ctx->malloced -= DEBUG_TABLE_COUNT * sizeof(debuglist_t);
904 1.1 christos }
905 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
906 1.1 christos
907 1.1 christos if (ctx->checkfree) {
908 1.1 christos for (i = 0; i <= ctx->max_size; i++) {
909 1.1 christos if (ctx->stats[i].gets != 0U) {
910 1.1 christos fprintf(stderr,
911 1.1 christos "Failing assertion due to probable "
912 1.1 christos "leaked memory in context %p (\"%s\") "
913 1.1 christos "(stats[%u].gets == %lu).\n",
914 1.1 christos ctx, ctx->name, i, ctx->stats[i].gets);
915 1.1 christos #if ISC_MEM_TRACKLINES
916 1.1 christos print_active(ctx, stderr);
917 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
918 1.1 christos INSIST(ctx->stats[i].gets == 0U);
919 1.1 christos }
920 1.1 christos }
921 1.1 christos }
922 1.1 christos
923 1.6 christos (ctx->memfree)(ctx->stats);
924 1.6 christos ctx->malloced -= (ctx->max_size + 1) * sizeof(struct stats);
925 1.1 christos
926 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
927 1.1 christos for (i = 0; i < ctx->basic_table_count; i++) {
928 1.6 christos (ctx->memfree)(ctx->basic_table[i]);
929 1.1 christos ctx->malloced -= NUM_BASIC_BLOCKS * ctx->mem_target;
930 1.1 christos }
931 1.6 christos (ctx->memfree)(ctx->freelists);
932 1.1 christos ctx->malloced -= ctx->max_size * sizeof(element *);
933 1.1 christos if (ctx->basic_table != NULL) {
934 1.6 christos (ctx->memfree)(ctx->basic_table);
935 1.1 christos ctx->malloced -= ctx->basic_table_size *
936 1.1 christos sizeof(unsigned char *);
937 1.1 christos }
938 1.1 christos }
939 1.1 christos
940 1.6 christos isc_mutex_destroy(&ctx->lock);
941 1.6 christos
942 1.1 christos ctx->malloced -= sizeof(*ctx);
943 1.6 christos if (ctx->checkfree) {
944 1.1 christos INSIST(ctx->malloced == 0);
945 1.6 christos }
946 1.6 christos (ctx->memfree)(ctx);
947 1.1 christos }
948 1.1 christos
949 1.1 christos void
950 1.3 christos isc_mem_attach(isc_mem_t *source0, isc_mem_t **targetp) {
951 1.6 christos REQUIRE(VALID_CONTEXT(source0));
952 1.6 christos REQUIRE(targetp != NULL && *targetp == NULL);
953 1.6 christos
954 1.1 christos isc__mem_t *source = (isc__mem_t *)source0;
955 1.1 christos
956 1.3 christos isc_refcount_increment(&source->references);
957 1.1 christos
958 1.1 christos *targetp = (isc_mem_t *)source;
959 1.1 christos }
960 1.1 christos
961 1.1 christos void
962 1.3 christos isc_mem_detach(isc_mem_t **ctxp) {
963 1.3 christos REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
964 1.6 christos
965 1.3 christos isc__mem_t *ctx = (isc__mem_t *)*ctxp;
966 1.3 christos *ctxp = NULL;
967 1.1 christos
968 1.3 christos if (isc_refcount_decrement(&ctx->references) == 1) {
969 1.3 christos isc_refcount_destroy(&ctx->references);
970 1.1 christos destroy(ctx);
971 1.3 christos }
972 1.1 christos }
973 1.1 christos
974 1.1 christos /*
975 1.1 christos * isc_mem_putanddetach() is the equivalent of:
976 1.1 christos *
977 1.1 christos * mctx = NULL;
978 1.1 christos * isc_mem_attach(ptr->mctx, &mctx);
979 1.1 christos * isc_mem_detach(&ptr->mctx);
980 1.1 christos * isc_mem_put(mctx, ptr, sizeof(*ptr);
981 1.1 christos * isc_mem_detach(&mctx);
982 1.1 christos */
983 1.1 christos
984 1.1 christos void
985 1.1 christos isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
986 1.3 christos REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
987 1.1 christos REQUIRE(ptr != NULL);
988 1.6 christos
989 1.3 christos isc__mem_t *ctx = (isc__mem_t *)*ctxp;
990 1.1 christos *ctxp = NULL;
991 1.1 christos
992 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging &
993 1.6 christos (ISC_MEM_DEBUGSIZE | ISC_MEM_DEBUGCTX)) != 0))
994 1.1 christos {
995 1.1 christos if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
996 1.3 christos size_info *si = &(((size_info *)ptr)[-1]);
997 1.3 christos size_t oldsize = si->u.size - ALIGNMENT_SIZE;
998 1.6 christos if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
999 1.1 christos oldsize -= ALIGNMENT_SIZE;
1000 1.6 christos }
1001 1.1 christos INSIST(oldsize == size);
1002 1.1 christos }
1003 1.1 christos isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
1004 1.1 christos
1005 1.3 christos goto destroy;
1006 1.1 christos }
1007 1.1 christos
1008 1.6 christos MCTXLOCK(ctx);
1009 1.1 christos
1010 1.1 christos DELETE_TRACE(ctx, ptr, size, file, line);
1011 1.1 christos
1012 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1013 1.1 christos mem_putunlocked(ctx, ptr, size);
1014 1.1 christos } else {
1015 1.1 christos mem_putstats(ctx, ptr, size);
1016 1.1 christos mem_put(ctx, ptr, size);
1017 1.1 christos }
1018 1.6 christos MCTXUNLOCK(ctx);
1019 1.1 christos
1020 1.3 christos destroy:
1021 1.3 christos if (isc_refcount_decrement(&ctx->references) == 1) {
1022 1.3 christos isc_refcount_destroy(&ctx->references);
1023 1.1 christos destroy(ctx);
1024 1.3 christos }
1025 1.1 christos }
1026 1.1 christos
1027 1.1 christos void
1028 1.3 christos isc_mem_destroy(isc_mem_t **ctxp) {
1029 1.1 christos /*
1030 1.1 christos * This routine provides legacy support for callers who use mctxs
1031 1.1 christos * without attaching/detaching.
1032 1.1 christos */
1033 1.1 christos
1034 1.6 christos REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
1035 1.6 christos
1036 1.6 christos isc__mem_t *ctx = (isc__mem_t *)*ctxp;
1037 1.1 christos
1038 1.1 christos #if ISC_MEM_TRACKLINES
1039 1.6 christos if (isc_refcount_decrement(&ctx->references) > 1) {
1040 1.1 christos print_active(ctx, stderr);
1041 1.3 christos }
1042 1.6 christos #else /* if ISC_MEM_TRACKLINES */
1043 1.8 christos isc_refcount_decrementz(&ctx->references);
1044 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
1045 1.3 christos isc_refcount_destroy(&ctx->references);
1046 1.1 christos destroy(ctx);
1047 1.1 christos
1048 1.1 christos *ctxp = NULL;
1049 1.1 christos }
1050 1.1 christos
1051 1.1 christos void *
1052 1.1 christos isc___mem_get(isc_mem_t *ctx0, size_t size FLARG) {
1053 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1054 1.6 christos
1055 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1056 1.1 christos void *ptr;
1057 1.3 christos bool call_water = false;
1058 1.1 christos
1059 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging &
1060 1.6 christos (ISC_MEM_DEBUGSIZE | ISC_MEM_DEBUGCTX)) != 0))
1061 1.6 christos {
1062 1.1 christos return (isc__mem_allocate(ctx0, size FLARG_PASS));
1063 1.6 christos }
1064 1.1 christos
1065 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1066 1.6 christos MCTXLOCK(ctx);
1067 1.1 christos ptr = mem_getunlocked(ctx, size);
1068 1.1 christos } else {
1069 1.1 christos ptr = mem_get(ctx, size);
1070 1.6 christos MCTXLOCK(ctx);
1071 1.6 christos if (ptr != NULL) {
1072 1.1 christos mem_getstats(ctx, size);
1073 1.6 christos }
1074 1.1 christos }
1075 1.1 christos
1076 1.1 christos ADD_TRACE(ctx, ptr, size, file, line);
1077 1.1 christos
1078 1.1 christos if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water) {
1079 1.3 christos ctx->is_overmem = true;
1080 1.6 christos if (!ctx->hi_called) {
1081 1.3 christos call_water = true;
1082 1.6 christos }
1083 1.1 christos }
1084 1.1 christos if (ctx->inuse > ctx->maxinuse) {
1085 1.1 christos ctx->maxinuse = ctx->inuse;
1086 1.1 christos if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1087 1.1 christos (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
1088 1.6 christos {
1089 1.1 christos fprintf(stderr, "maxinuse = %lu\n",
1090 1.1 christos (unsigned long)ctx->inuse);
1091 1.6 christos }
1092 1.1 christos }
1093 1.6 christos MCTXUNLOCK(ctx);
1094 1.1 christos
1095 1.6 christos if (call_water && (ctx->water != NULL)) {
1096 1.1 christos (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
1097 1.6 christos }
1098 1.1 christos
1099 1.1 christos return (ptr);
1100 1.1 christos }
1101 1.1 christos
1102 1.1 christos void
1103 1.1 christos isc___mem_put(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
1104 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1105 1.6 christos REQUIRE(ptr != NULL);
1106 1.6 christos
1107 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1108 1.3 christos bool call_water = false;
1109 1.1 christos size_info *si;
1110 1.1 christos size_t oldsize;
1111 1.1 christos
1112 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging &
1113 1.6 christos (ISC_MEM_DEBUGSIZE | ISC_MEM_DEBUGCTX)) != 0))
1114 1.1 christos {
1115 1.1 christos if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
1116 1.1 christos si = &(((size_info *)ptr)[-1]);
1117 1.1 christos oldsize = si->u.size - ALIGNMENT_SIZE;
1118 1.6 christos if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0) {
1119 1.1 christos oldsize -= ALIGNMENT_SIZE;
1120 1.6 christos }
1121 1.1 christos INSIST(oldsize == size);
1122 1.1 christos }
1123 1.1 christos isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
1124 1.1 christos return;
1125 1.1 christos }
1126 1.1 christos
1127 1.6 christos MCTXLOCK(ctx);
1128 1.1 christos
1129 1.1 christos DELETE_TRACE(ctx, ptr, size, file, line);
1130 1.1 christos
1131 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1132 1.1 christos mem_putunlocked(ctx, ptr, size);
1133 1.1 christos } else {
1134 1.1 christos mem_putstats(ctx, ptr, size);
1135 1.1 christos mem_put(ctx, ptr, size);
1136 1.1 christos }
1137 1.1 christos
1138 1.1 christos /*
1139 1.1 christos * The check against ctx->lo_water == 0 is for the condition
1140 1.1 christos * when the context was pushed over hi_water but then had
1141 1.1 christos * isc_mem_setwater() called with 0 for hi_water and lo_water.
1142 1.1 christos */
1143 1.1 christos if ((ctx->inuse < ctx->lo_water) || (ctx->lo_water == 0U)) {
1144 1.3 christos ctx->is_overmem = false;
1145 1.6 christos if (ctx->hi_called) {
1146 1.3 christos call_water = true;
1147 1.6 christos }
1148 1.1 christos }
1149 1.1 christos
1150 1.6 christos MCTXUNLOCK(ctx);
1151 1.1 christos
1152 1.6 christos if (call_water && (ctx->water != NULL)) {
1153 1.1 christos (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1154 1.6 christos }
1155 1.1 christos }
1156 1.1 christos
1157 1.1 christos void
1158 1.3 christos isc_mem_waterack(isc_mem_t *ctx0, int flag) {
1159 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1160 1.6 christos
1161 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1162 1.1 christos
1163 1.6 christos MCTXLOCK(ctx);
1164 1.6 christos if (flag == ISC_MEM_LOWATER) {
1165 1.3 christos ctx->hi_called = false;
1166 1.6 christos } else if (flag == ISC_MEM_HIWATER) {
1167 1.3 christos ctx->hi_called = true;
1168 1.6 christos }
1169 1.6 christos MCTXUNLOCK(ctx);
1170 1.1 christos }
1171 1.1 christos
1172 1.1 christos #if ISC_MEM_TRACKLINES
1173 1.1 christos static void
1174 1.1 christos print_active(isc__mem_t *mctx, FILE *out) {
1175 1.1 christos if (mctx->debuglist != NULL) {
1176 1.1 christos debuglink_t *dl;
1177 1.1 christos unsigned int i;
1178 1.3 christos bool found;
1179 1.1 christos
1180 1.4 christos fputs("Dump of all outstanding memory allocations:\n", out);
1181 1.3 christos found = false;
1182 1.1 christos for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
1183 1.1 christos dl = ISC_LIST_HEAD(mctx->debuglist[i]);
1184 1.1 christos
1185 1.4 christos if (dl != NULL) {
1186 1.3 christos found = true;
1187 1.4 christos }
1188 1.1 christos
1189 1.1 christos while (dl != NULL) {
1190 1.4 christos if (dl->ptr != NULL) {
1191 1.4 christos fprintf(out,
1192 1.6 christos "\tptr %p size %zu file %s "
1193 1.6 christos "line %u\n",
1194 1.6 christos dl->ptr, dl->size, dl->file,
1195 1.6 christos dl->line);
1196 1.4 christos }
1197 1.1 christos dl = ISC_LIST_NEXT(dl, link);
1198 1.1 christos }
1199 1.1 christos }
1200 1.1 christos
1201 1.4 christos if (!found) {
1202 1.4 christos fputs("\tNone.\n", out);
1203 1.4 christos }
1204 1.1 christos }
1205 1.1 christos }
1206 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
1207 1.1 christos
1208 1.1 christos /*
1209 1.1 christos * Print the stats[] on the stream "out" with suitable formatting.
1210 1.1 christos */
1211 1.1 christos void
1212 1.1 christos isc_mem_stats(isc_mem_t *ctx0, FILE *out) {
1213 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1214 1.6 christos
1215 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1216 1.1 christos size_t i;
1217 1.1 christos const struct stats *s;
1218 1.1 christos const isc__mempool_t *pool;
1219 1.1 christos
1220 1.6 christos MCTXLOCK(ctx);
1221 1.1 christos
1222 1.1 christos for (i = 0; i <= ctx->max_size; i++) {
1223 1.1 christos s = &ctx->stats[i];
1224 1.1 christos
1225 1.6 christos if (s->totalgets == 0U && s->gets == 0U) {
1226 1.1 christos continue;
1227 1.6 christos }
1228 1.1 christos fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
1229 1.6 christos (i == ctx->max_size) ? ">=" : " ", (unsigned long)i,
1230 1.6 christos s->totalgets, s->gets);
1231 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
1232 1.1 christos (s->blocks != 0U || s->freefrags != 0U))
1233 1.6 christos {
1234 1.6 christos fprintf(out, " (%lu bl, %lu ff)", s->blocks,
1235 1.6 christos s->freefrags);
1236 1.6 christos }
1237 1.1 christos fputc('\n', out);
1238 1.1 christos }
1239 1.1 christos
1240 1.1 christos /*
1241 1.1 christos * Note that since a pool can be locked now, these stats might be
1242 1.1 christos * somewhat off if the pool is in active use at the time the stats
1243 1.1 christos * are dumped. The link fields are protected by the isc_mem_t's
1244 1.1 christos * lock, however, so walking this list and extracting integers from
1245 1.1 christos * stats fields is always safe.
1246 1.1 christos */
1247 1.1 christos pool = ISC_LIST_HEAD(ctx->pools);
1248 1.1 christos if (pool != NULL) {
1249 1.4 christos fputs("[Pool statistics]\n", out);
1250 1.1 christos fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
1251 1.4 christos "name", "size", "maxalloc", "allocated", "freecount",
1252 1.4 christos "freemax", "fillcount", "gets", "L");
1253 1.1 christos }
1254 1.1 christos while (pool != NULL) {
1255 1.1 christos fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
1256 1.1 christos #if ISC_MEMPOOL_NAMES
1257 1.1 christos pool->name,
1258 1.6 christos #else /* if ISC_MEMPOOL_NAMES */
1259 1.1 christos "(not tracked)",
1260 1.6 christos #endif /* if ISC_MEMPOOL_NAMES */
1261 1.6 christos (unsigned long)pool->size, pool->maxalloc,
1262 1.1 christos pool->allocated, pool->freecount, pool->freemax,
1263 1.12 christos pool->fillcount, pool->gets, "N");
1264 1.1 christos pool = ISC_LIST_NEXT(pool, link);
1265 1.1 christos }
1266 1.1 christos
1267 1.1 christos #if ISC_MEM_TRACKLINES
1268 1.1 christos print_active(ctx, out);
1269 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
1270 1.1 christos
1271 1.6 christos MCTXUNLOCK(ctx);
1272 1.1 christos }
1273 1.1 christos
1274 1.1 christos /*
1275 1.1 christos * Replacements for malloc() and free() -- they implicitly remember the
1276 1.1 christos * size of the object allocated (with some additional overhead).
1277 1.1 christos */
1278 1.1 christos
1279 1.1 christos static void *
1280 1.1 christos mem_allocateunlocked(isc_mem_t *ctx0, size_t size) {
1281 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1282 1.1 christos size_info *si;
1283 1.1 christos
1284 1.1 christos size += ALIGNMENT_SIZE;
1285 1.6 christos if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
1286 1.1 christos size += ALIGNMENT_SIZE;
1287 1.6 christos }
1288 1.1 christos
1289 1.6 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1290 1.1 christos si = mem_getunlocked(ctx, size);
1291 1.6 christos } else {
1292 1.1 christos si = mem_get(ctx, size);
1293 1.6 christos }
1294 1.1 christos
1295 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
1296 1.1 christos si->u.ctx = ctx;
1297 1.1 christos si++;
1298 1.1 christos }
1299 1.1 christos si->u.size = size;
1300 1.1 christos return (&si[1]);
1301 1.1 christos }
1302 1.1 christos
1303 1.1 christos void *
1304 1.1 christos isc___mem_allocate(isc_mem_t *ctx0, size_t size FLARG) {
1305 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1306 1.6 christos
1307 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1308 1.1 christos size_info *si;
1309 1.3 christos bool call_water = false;
1310 1.1 christos
1311 1.6 christos MCTXLOCK(ctx);
1312 1.1 christos si = mem_allocateunlocked((isc_mem_t *)ctx, size);
1313 1.6 christos if (((ctx->flags & ISC_MEMFLAG_INTERNAL) == 0)) {
1314 1.1 christos mem_getstats(ctx, si[-1].u.size);
1315 1.6 christos }
1316 1.1 christos
1317 1.1 christos ADD_TRACE(ctx, si, si[-1].u.size, file, line);
1318 1.1 christos if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1319 1.13 christos !ctx->is_overmem)
1320 1.13 christos {
1321 1.3 christos ctx->is_overmem = true;
1322 1.1 christos }
1323 1.1 christos
1324 1.1 christos if (ctx->hi_water != 0U && !ctx->hi_called &&
1325 1.13 christos ctx->inuse > ctx->hi_water)
1326 1.13 christos {
1327 1.3 christos ctx->hi_called = true;
1328 1.3 christos call_water = true;
1329 1.1 christos }
1330 1.1 christos if (ctx->inuse > ctx->maxinuse) {
1331 1.1 christos ctx->maxinuse = ctx->inuse;
1332 1.1 christos if (ISC_UNLIKELY(ctx->hi_water != 0U &&
1333 1.1 christos ctx->inuse > ctx->hi_water &&
1334 1.1 christos (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0))
1335 1.6 christos {
1336 1.1 christos fprintf(stderr, "maxinuse = %lu\n",
1337 1.1 christos (unsigned long)ctx->inuse);
1338 1.6 christos }
1339 1.1 christos }
1340 1.6 christos MCTXUNLOCK(ctx);
1341 1.1 christos
1342 1.6 christos if (call_water) {
1343 1.1 christos (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
1344 1.6 christos }
1345 1.1 christos
1346 1.1 christos return (si);
1347 1.1 christos }
1348 1.1 christos
1349 1.1 christos void *
1350 1.1 christos isc___mem_reallocate(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
1351 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1352 1.6 christos
1353 1.1 christos void *new_ptr = NULL;
1354 1.1 christos size_t oldsize, copysize;
1355 1.1 christos
1356 1.1 christos /*
1357 1.1 christos * This function emulates the realloc(3) standard library function:
1358 1.1 christos * - if size > 0, allocate new memory; and if ptr is non NULL, copy
1359 1.1 christos * as much of the old contents to the new buffer and free the old one.
1360 1.1 christos * Note that when allocation fails the original pointer is intact;
1361 1.1 christos * the caller must free it.
1362 1.1 christos * - if size is 0 and ptr is non NULL, simply free the given ptr.
1363 1.1 christos * - this function returns:
1364 1.1 christos * pointer to the newly allocated memory, or
1365 1.1 christos * NULL if allocation fails or doesn't happen.
1366 1.1 christos */
1367 1.1 christos if (size > 0U) {
1368 1.1 christos new_ptr = isc__mem_allocate(ctx0, size FLARG_PASS);
1369 1.1 christos if (new_ptr != NULL && ptr != NULL) {
1370 1.1 christos oldsize = (((size_info *)ptr)[-1]).u.size;
1371 1.1 christos INSIST(oldsize >= ALIGNMENT_SIZE);
1372 1.1 christos oldsize -= ALIGNMENT_SIZE;
1373 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging &
1374 1.13 christos ISC_MEM_DEBUGCTX) != 0))
1375 1.13 christos {
1376 1.1 christos INSIST(oldsize >= ALIGNMENT_SIZE);
1377 1.1 christos oldsize -= ALIGNMENT_SIZE;
1378 1.1 christos }
1379 1.1 christos copysize = (oldsize > size) ? size : oldsize;
1380 1.1 christos memmove(new_ptr, ptr, copysize);
1381 1.1 christos isc__mem_free(ctx0, ptr FLARG_PASS);
1382 1.1 christos }
1383 1.6 christos } else if (ptr != NULL) {
1384 1.1 christos isc__mem_free(ctx0, ptr FLARG_PASS);
1385 1.6 christos }
1386 1.1 christos
1387 1.1 christos return (new_ptr);
1388 1.1 christos }
1389 1.1 christos
1390 1.1 christos void
1391 1.1 christos isc___mem_free(isc_mem_t *ctx0, void *ptr FLARG) {
1392 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1393 1.6 christos REQUIRE(ptr != NULL);
1394 1.6 christos
1395 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1396 1.1 christos size_info *si;
1397 1.1 christos size_t size;
1398 1.6 christos bool call_water = false;
1399 1.1 christos
1400 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
1401 1.1 christos si = &(((size_info *)ptr)[-2]);
1402 1.1 christos REQUIRE(si->u.ctx == ctx);
1403 1.1 christos size = si[1].u.size;
1404 1.1 christos } else {
1405 1.1 christos si = &(((size_info *)ptr)[-1]);
1406 1.1 christos size = si->u.size;
1407 1.1 christos }
1408 1.1 christos
1409 1.6 christos MCTXLOCK(ctx);
1410 1.1 christos
1411 1.1 christos DELETE_TRACE(ctx, ptr, size, file, line);
1412 1.1 christos
1413 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1414 1.1 christos mem_putunlocked(ctx, si, size);
1415 1.1 christos } else {
1416 1.1 christos mem_putstats(ctx, si, size);
1417 1.1 christos mem_put(ctx, si, size);
1418 1.1 christos }
1419 1.1 christos
1420 1.1 christos /*
1421 1.1 christos * The check against ctx->lo_water == 0 is for the condition
1422 1.1 christos * when the context was pushed over hi_water but then had
1423 1.1 christos * isc_mem_setwater() called with 0 for hi_water and lo_water.
1424 1.1 christos */
1425 1.1 christos if (ctx->is_overmem &&
1426 1.13 christos (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U))
1427 1.13 christos {
1428 1.3 christos ctx->is_overmem = false;
1429 1.1 christos }
1430 1.1 christos
1431 1.1 christos if (ctx->hi_called &&
1432 1.13 christos (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U))
1433 1.13 christos {
1434 1.3 christos ctx->hi_called = false;
1435 1.1 christos
1436 1.6 christos if (ctx->water != NULL) {
1437 1.3 christos call_water = true;
1438 1.6 christos }
1439 1.1 christos }
1440 1.6 christos MCTXUNLOCK(ctx);
1441 1.1 christos
1442 1.6 christos if (call_water) {
1443 1.1 christos (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1444 1.6 christos }
1445 1.1 christos }
1446 1.1 christos
1447 1.1 christos /*
1448 1.1 christos * Other useful things.
1449 1.1 christos */
1450 1.1 christos
1451 1.1 christos char *
1452 1.1 christos isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
1453 1.6 christos REQUIRE(VALID_CONTEXT(mctx0));
1454 1.6 christos REQUIRE(s != NULL);
1455 1.6 christos
1456 1.1 christos isc__mem_t *mctx = (isc__mem_t *)mctx0;
1457 1.1 christos size_t len;
1458 1.1 christos char *ns;
1459 1.1 christos
1460 1.1 christos len = strlen(s) + 1;
1461 1.1 christos
1462 1.1 christos ns = isc__mem_allocate((isc_mem_t *)mctx, len FLARG_PASS);
1463 1.1 christos
1464 1.6 christos if (ns != NULL) {
1465 1.1 christos strlcpy(ns, s, len);
1466 1.6 christos }
1467 1.1 christos
1468 1.1 christos return (ns);
1469 1.1 christos }
1470 1.1 christos
1471 1.11 christos char *
1472 1.11 christos isc___mem_strndup(isc_mem_t *mctx0, const char *s, size_t size FLARG) {
1473 1.11 christos REQUIRE(VALID_CONTEXT(mctx0));
1474 1.11 christos REQUIRE(s != NULL);
1475 1.11 christos
1476 1.11 christos isc__mem_t *mctx = (isc__mem_t *)mctx0;
1477 1.11 christos size_t len;
1478 1.11 christos char *ns;
1479 1.11 christos
1480 1.11 christos len = strlen(s) + 1;
1481 1.11 christos if (len > size) {
1482 1.11 christos len = size;
1483 1.11 christos }
1484 1.11 christos
1485 1.11 christos ns = isc__mem_allocate((isc_mem_t *)mctx, len FLARG_PASS);
1486 1.11 christos
1487 1.11 christos if (ns != NULL) {
1488 1.11 christos strlcpy(ns, s, len);
1489 1.11 christos }
1490 1.11 christos
1491 1.11 christos return (ns);
1492 1.11 christos }
1493 1.11 christos
1494 1.1 christos void
1495 1.3 christos isc_mem_setdestroycheck(isc_mem_t *ctx0, bool flag) {
1496 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1497 1.6 christos
1498 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1499 1.1 christos
1500 1.6 christos MCTXLOCK(ctx);
1501 1.1 christos
1502 1.1 christos ctx->checkfree = flag;
1503 1.1 christos
1504 1.6 christos MCTXUNLOCK(ctx);
1505 1.1 christos }
1506 1.1 christos
1507 1.1 christos size_t
1508 1.3 christos isc_mem_inuse(isc_mem_t *ctx0) {
1509 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1510 1.6 christos
1511 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1512 1.1 christos size_t inuse;
1513 1.1 christos
1514 1.6 christos MCTXLOCK(ctx);
1515 1.1 christos
1516 1.1 christos inuse = ctx->inuse;
1517 1.1 christos
1518 1.6 christos MCTXUNLOCK(ctx);
1519 1.1 christos
1520 1.1 christos return (inuse);
1521 1.1 christos }
1522 1.1 christos
1523 1.1 christos size_t
1524 1.3 christos isc_mem_maxinuse(isc_mem_t *ctx0) {
1525 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1526 1.6 christos
1527 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1528 1.1 christos size_t maxinuse;
1529 1.1 christos
1530 1.6 christos MCTXLOCK(ctx);
1531 1.1 christos
1532 1.1 christos maxinuse = ctx->maxinuse;
1533 1.1 christos
1534 1.6 christos MCTXUNLOCK(ctx);
1535 1.1 christos
1536 1.1 christos return (maxinuse);
1537 1.1 christos }
1538 1.1 christos
1539 1.1 christos size_t
1540 1.3 christos isc_mem_total(isc_mem_t *ctx0) {
1541 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1542 1.6 christos
1543 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1544 1.1 christos size_t total;
1545 1.1 christos
1546 1.6 christos MCTXLOCK(ctx);
1547 1.1 christos
1548 1.1 christos total = ctx->total;
1549 1.1 christos
1550 1.6 christos MCTXUNLOCK(ctx);
1551 1.1 christos
1552 1.1 christos return (total);
1553 1.1 christos }
1554 1.1 christos
1555 1.1 christos void
1556 1.3 christos isc_mem_setwater(isc_mem_t *ctx0, isc_mem_water_t water, void *water_arg,
1557 1.6 christos size_t hiwater, size_t lowater) {
1558 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1559 1.6 christos REQUIRE(hiwater >= lowater);
1560 1.6 christos
1561 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1562 1.3 christos bool callwater = false;
1563 1.1 christos isc_mem_water_t oldwater;
1564 1.1 christos void *oldwater_arg;
1565 1.1 christos
1566 1.6 christos MCTXLOCK(ctx);
1567 1.1 christos oldwater = ctx->water;
1568 1.1 christos oldwater_arg = ctx->water_arg;
1569 1.1 christos if (water == NULL) {
1570 1.1 christos callwater = ctx->hi_called;
1571 1.1 christos ctx->water = NULL;
1572 1.1 christos ctx->water_arg = NULL;
1573 1.1 christos ctx->hi_water = 0;
1574 1.1 christos ctx->lo_water = 0;
1575 1.1 christos } else {
1576 1.1 christos if (ctx->hi_called &&
1577 1.1 christos (ctx->water != water || ctx->water_arg != water_arg ||
1578 1.1 christos ctx->inuse < lowater || lowater == 0U))
1579 1.6 christos {
1580 1.3 christos callwater = true;
1581 1.6 christos }
1582 1.1 christos ctx->water = water;
1583 1.1 christos ctx->water_arg = water_arg;
1584 1.1 christos ctx->hi_water = hiwater;
1585 1.1 christos ctx->lo_water = lowater;
1586 1.1 christos }
1587 1.6 christos MCTXUNLOCK(ctx);
1588 1.1 christos
1589 1.6 christos if (callwater && oldwater != NULL) {
1590 1.1 christos (oldwater)(oldwater_arg, ISC_MEM_LOWATER);
1591 1.6 christos }
1592 1.1 christos }
1593 1.1 christos
1594 1.8 christos ISC_NO_SANITIZE_THREAD bool
1595 1.3 christos isc_mem_isovermem(isc_mem_t *ctx0) {
1596 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1597 1.6 christos
1598 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1599 1.1 christos
1600 1.1 christos /*
1601 1.1 christos * We don't bother to lock the context because 100% accuracy isn't
1602 1.1 christos * necessary (and even if we locked the context the returned value
1603 1.1 christos * could be different from the actual state when it's used anyway)
1604 1.1 christos */
1605 1.1 christos return (ctx->is_overmem);
1606 1.1 christos }
1607 1.1 christos
1608 1.1 christos void
1609 1.1 christos isc_mem_setname(isc_mem_t *ctx0, const char *name, void *tag) {
1610 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1611 1.6 christos
1612 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1613 1.1 christos
1614 1.1 christos LOCK(&ctx->lock);
1615 1.1 christos strlcpy(ctx->name, name, sizeof(ctx->name));
1616 1.1 christos ctx->tag = tag;
1617 1.1 christos UNLOCK(&ctx->lock);
1618 1.1 christos }
1619 1.1 christos
1620 1.1 christos const char *
1621 1.1 christos isc_mem_getname(isc_mem_t *ctx0) {
1622 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1623 1.6 christos
1624 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1625 1.1 christos
1626 1.6 christos if (ctx->name[0] == 0) {
1627 1.1 christos return ("");
1628 1.6 christos }
1629 1.1 christos
1630 1.1 christos return (ctx->name);
1631 1.1 christos }
1632 1.1 christos
1633 1.1 christos void *
1634 1.1 christos isc_mem_gettag(isc_mem_t *ctx0) {
1635 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
1636 1.6 christos
1637 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1638 1.1 christos
1639 1.1 christos return (ctx->tag);
1640 1.1 christos }
1641 1.1 christos
1642 1.1 christos /*
1643 1.1 christos * Memory pool stuff
1644 1.1 christos */
1645 1.1 christos
1646 1.6 christos void
1647 1.3 christos isc_mempool_create(isc_mem_t *mctx0, size_t size, isc_mempool_t **mpctxp) {
1648 1.6 christos REQUIRE(VALID_CONTEXT(mctx0));
1649 1.6 christos REQUIRE(size > 0U);
1650 1.6 christos REQUIRE(mpctxp != NULL && *mpctxp == NULL);
1651 1.6 christos
1652 1.1 christos isc__mem_t *mctx = (isc__mem_t *)mctx0;
1653 1.1 christos isc__mempool_t *mpctx;
1654 1.1 christos
1655 1.1 christos /*
1656 1.1 christos * Allocate space for this pool, initialize values, and if all works
1657 1.1 christos * well, attach to the memory context.
1658 1.1 christos */
1659 1.1 christos mpctx = isc_mem_get((isc_mem_t *)mctx, sizeof(isc__mempool_t));
1660 1.1 christos
1661 1.1 christos mpctx->common.impmagic = MEMPOOL_MAGIC;
1662 1.1 christos mpctx->common.magic = ISCAPI_MPOOL_MAGIC;
1663 1.12 christos mpctx->mctx = NULL;
1664 1.12 christos isc_mem_attach((isc_mem_t *)mctx, (isc_mem_t **)&mpctx->mctx);
1665 1.3 christos /*
1666 1.3 christos * Mempools are stored as a linked list of element.
1667 1.3 christos */
1668 1.3 christos if (size < sizeof(element)) {
1669 1.3 christos size = sizeof(element);
1670 1.3 christos }
1671 1.1 christos mpctx->size = size;
1672 1.1 christos mpctx->maxalloc = UINT_MAX;
1673 1.1 christos mpctx->allocated = 0;
1674 1.1 christos mpctx->freecount = 0;
1675 1.1 christos mpctx->freemax = 1;
1676 1.1 christos mpctx->fillcount = 1;
1677 1.1 christos mpctx->gets = 0;
1678 1.1 christos #if ISC_MEMPOOL_NAMES
1679 1.1 christos mpctx->name[0] = 0;
1680 1.6 christos #endif /* if ISC_MEMPOOL_NAMES */
1681 1.1 christos mpctx->items = NULL;
1682 1.1 christos
1683 1.1 christos *mpctxp = (isc_mempool_t *)mpctx;
1684 1.1 christos
1685 1.6 christos MCTXLOCK(mctx);
1686 1.1 christos ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
1687 1.1 christos mctx->poolcnt++;
1688 1.6 christos MCTXUNLOCK(mctx);
1689 1.1 christos }
1690 1.1 christos
1691 1.1 christos void
1692 1.3 christos isc_mempool_setname(isc_mempool_t *mpctx0, const char *name) {
1693 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1694 1.6 christos REQUIRE(name != NULL);
1695 1.6 christos
1696 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1697 1.1 christos
1698 1.1 christos #if ISC_MEMPOOL_NAMES
1699 1.1 christos strlcpy(mpctx->name, name, sizeof(mpctx->name));
1700 1.6 christos #else /* if ISC_MEMPOOL_NAMES */
1701 1.1 christos UNUSED(mpctx);
1702 1.1 christos UNUSED(name);
1703 1.6 christos #endif /* if ISC_MEMPOOL_NAMES */
1704 1.1 christos }
1705 1.1 christos
1706 1.1 christos void
1707 1.3 christos isc_mempool_destroy(isc_mempool_t **mpctxp) {
1708 1.6 christos REQUIRE(mpctxp != NULL);
1709 1.6 christos REQUIRE(VALID_MEMPOOL(*mpctxp));
1710 1.6 christos
1711 1.1 christos isc__mempool_t *mpctx;
1712 1.1 christos isc__mem_t *mctx;
1713 1.1 christos element *item;
1714 1.1 christos
1715 1.1 christos mpctx = (isc__mempool_t *)*mpctxp;
1716 1.1 christos #if ISC_MEMPOOL_NAMES
1717 1.6 christos if (mpctx->allocated > 0) {
1718 1.1 christos UNEXPECTED_ERROR(__FILE__, __LINE__,
1719 1.3 christos "isc_mempool_destroy(): mempool %s "
1720 1.1 christos "leaked memory",
1721 1.1 christos mpctx->name);
1722 1.6 christos }
1723 1.6 christos #endif /* if ISC_MEMPOOL_NAMES */
1724 1.1 christos REQUIRE(mpctx->allocated == 0);
1725 1.1 christos
1726 1.1 christos mctx = mpctx->mctx;
1727 1.1 christos
1728 1.1 christos /*
1729 1.1 christos * Return any items on the free list
1730 1.1 christos */
1731 1.6 christos MCTXLOCK(mctx);
1732 1.1 christos while (mpctx->items != NULL) {
1733 1.1 christos INSIST(mpctx->freecount > 0);
1734 1.1 christos mpctx->freecount--;
1735 1.1 christos item = mpctx->items;
1736 1.1 christos mpctx->items = item->next;
1737 1.12 christos mem_putstats(mctx, item, mpctx->size);
1738 1.12 christos mem_put(mctx, item, mpctx->size);
1739 1.1 christos }
1740 1.6 christos MCTXUNLOCK(mctx);
1741 1.1 christos
1742 1.1 christos /*
1743 1.1 christos * Remove our linked list entry from the memory context.
1744 1.1 christos */
1745 1.6 christos MCTXLOCK(mctx);
1746 1.1 christos ISC_LIST_UNLINK(mctx->pools, mpctx, link);
1747 1.1 christos mctx->poolcnt--;
1748 1.6 christos MCTXUNLOCK(mctx);
1749 1.1 christos
1750 1.1 christos mpctx->common.impmagic = 0;
1751 1.1 christos mpctx->common.magic = 0;
1752 1.1 christos
1753 1.12 christos isc_mem_putanddetach((isc_mem_t **)&mpctx->mctx, mpctx,
1754 1.12 christos sizeof(isc__mempool_t));
1755 1.1 christos
1756 1.1 christos *mpctxp = NULL;
1757 1.1 christos }
1758 1.1 christos
1759 1.10 christos #if __SANITIZE_ADDRESS__
1760 1.10 christos void *
1761 1.10 christos isc__mempool_get(isc_mempool_t *mpctx0 FLARG) {
1762 1.10 christos void *item = NULL;
1763 1.10 christos
1764 1.10 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1765 1.10 christos
1766 1.10 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1767 1.10 christos isc_mem_t *mctx = (isc_mem_t *)mpctx->mctx;
1768 1.10 christos
1769 1.10 christos /*
1770 1.10 christos * Don't let the caller go over quota
1771 1.10 christos */
1772 1.10 christos if (ISC_UNLIKELY(mpctx->allocated >= mpctx->maxalloc)) {
1773 1.10 christos goto out;
1774 1.10 christos }
1775 1.10 christos
1776 1.10 christos item = isc__mem_get(mctx, mpctx->size FLARG_PASS);
1777 1.10 christos mpctx->gets++;
1778 1.10 christos mpctx->allocated++;
1779 1.10 christos
1780 1.10 christos out:
1781 1.10 christos return (item);
1782 1.10 christos }
1783 1.10 christos
1784 1.10 christos void
1785 1.10 christos isc__mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
1786 1.10 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1787 1.10 christos
1788 1.10 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1789 1.10 christos isc_mem_t *mctx = (isc_mem_t *)mpctx->mctx;
1790 1.10 christos
1791 1.10 christos REQUIRE(mem != NULL);
1792 1.10 christos
1793 1.10 christos INSIST(mpctx->allocated > 0);
1794 1.10 christos mpctx->allocated--;
1795 1.10 christos
1796 1.10 christos isc__mem_put(mctx, mem, mpctx->size FLARG_PASS);
1797 1.10 christos }
1798 1.10 christos
1799 1.10 christos #else /* __SANITIZE_ADDRESS__ */
1800 1.1 christos void *
1801 1.3 christos isc__mempool_get(isc_mempool_t *mpctx0 FLARG) {
1802 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1803 1.6 christos
1804 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1805 1.1 christos element *item;
1806 1.1 christos isc__mem_t *mctx;
1807 1.1 christos unsigned int i;
1808 1.1 christos
1809 1.1 christos mctx = mpctx->mctx;
1810 1.1 christos
1811 1.1 christos /*
1812 1.1 christos * Don't let the caller go over quota
1813 1.1 christos */
1814 1.1 christos if (ISC_UNLIKELY(mpctx->allocated >= mpctx->maxalloc)) {
1815 1.1 christos item = NULL;
1816 1.1 christos goto out;
1817 1.1 christos }
1818 1.1 christos
1819 1.1 christos if (ISC_UNLIKELY(mpctx->items == NULL)) {
1820 1.1 christos /*
1821 1.1 christos * We need to dip into the well. Lock the memory context
1822 1.1 christos * here and fill up our free list.
1823 1.1 christos */
1824 1.6 christos MCTXLOCK(mctx);
1825 1.1 christos for (i = 0; i < mpctx->fillcount; i++) {
1826 1.12 christos item = mem_get(mctx, mpctx->size);
1827 1.12 christos mem_getstats(mctx, mpctx->size);
1828 1.1 christos item->next = mpctx->items;
1829 1.1 christos mpctx->items = item;
1830 1.1 christos mpctx->freecount++;
1831 1.1 christos }
1832 1.6 christos MCTXUNLOCK(mctx);
1833 1.1 christos }
1834 1.1 christos
1835 1.1 christos /*
1836 1.1 christos * If we didn't get any items, return NULL.
1837 1.1 christos */
1838 1.1 christos item = mpctx->items;
1839 1.6 christos if (ISC_UNLIKELY(item == NULL)) {
1840 1.1 christos goto out;
1841 1.6 christos }
1842 1.1 christos
1843 1.1 christos mpctx->items = item->next;
1844 1.1 christos INSIST(mpctx->freecount > 0);
1845 1.1 christos mpctx->freecount--;
1846 1.1 christos mpctx->gets++;
1847 1.1 christos mpctx->allocated++;
1848 1.1 christos
1849 1.6 christos out:
1850 1.1 christos #if ISC_MEM_TRACKLINES
1851 1.1 christos if (ISC_UNLIKELY(((isc_mem_debugging & TRACE_OR_RECORD) != 0) &&
1852 1.13 christos item != NULL))
1853 1.13 christos {
1854 1.6 christos MCTXLOCK(mctx);
1855 1.1 christos ADD_TRACE(mctx, item, mpctx->size, file, line);
1856 1.6 christos MCTXUNLOCK(mctx);
1857 1.1 christos }
1858 1.1 christos #endif /* ISC_MEM_TRACKLINES */
1859 1.1 christos
1860 1.1 christos return (item);
1861 1.1 christos }
1862 1.1 christos
1863 1.1 christos /* coverity[+free : arg-1] */
1864 1.1 christos void
1865 1.3 christos isc__mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
1866 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1867 1.6 christos REQUIRE(mem != NULL);
1868 1.6 christos
1869 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1870 1.6 christos isc__mem_t *mctx = mpctx->mctx;
1871 1.1 christos element *item;
1872 1.1 christos
1873 1.1 christos INSIST(mpctx->allocated > 0);
1874 1.1 christos mpctx->allocated--;
1875 1.1 christos
1876 1.1 christos #if ISC_MEM_TRACKLINES
1877 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0)) {
1878 1.6 christos MCTXLOCK(mctx);
1879 1.1 christos DELETE_TRACE(mctx, mem, mpctx->size, file, line);
1880 1.6 christos MCTXUNLOCK(mctx);
1881 1.1 christos }
1882 1.1 christos #endif /* ISC_MEM_TRACKLINES */
1883 1.1 christos
1884 1.1 christos /*
1885 1.1 christos * If our free list is full, return this to the mctx directly.
1886 1.1 christos */
1887 1.1 christos if (mpctx->freecount >= mpctx->freemax) {
1888 1.6 christos MCTXLOCK(mctx);
1889 1.12 christos mem_putstats(mctx, mem, mpctx->size);
1890 1.12 christos mem_put(mctx, mem, mpctx->size);
1891 1.6 christos MCTXUNLOCK(mctx);
1892 1.1 christos return;
1893 1.1 christos }
1894 1.1 christos
1895 1.1 christos /*
1896 1.1 christos * Otherwise, attach it to our free list and bump the counter.
1897 1.1 christos */
1898 1.1 christos mpctx->freecount++;
1899 1.1 christos item = (element *)mem;
1900 1.1 christos item->next = mpctx->items;
1901 1.1 christos mpctx->items = item;
1902 1.1 christos }
1903 1.1 christos
1904 1.10 christos #endif /* __SANITIZE_ADDRESS__ */
1905 1.10 christos
1906 1.1 christos /*
1907 1.1 christos * Quotas
1908 1.1 christos */
1909 1.1 christos
1910 1.1 christos void
1911 1.3 christos isc_mempool_setfreemax(isc_mempool_t *mpctx0, unsigned int limit) {
1912 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1913 1.6 christos
1914 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1915 1.1 christos
1916 1.1 christos mpctx->freemax = limit;
1917 1.1 christos }
1918 1.1 christos
1919 1.1 christos unsigned int
1920 1.1 christos isc_mempool_getfreemax(isc_mempool_t *mpctx0) {
1921 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1922 1.6 christos
1923 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1924 1.1 christos
1925 1.12 christos return (mpctx->freemax);
1926 1.1 christos }
1927 1.1 christos
1928 1.1 christos unsigned int
1929 1.1 christos isc_mempool_getfreecount(isc_mempool_t *mpctx0) {
1930 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1931 1.6 christos
1932 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1933 1.1 christos
1934 1.12 christos return (mpctx->freecount);
1935 1.1 christos }
1936 1.1 christos
1937 1.1 christos void
1938 1.3 christos isc_mempool_setmaxalloc(isc_mempool_t *mpctx0, unsigned int limit) {
1939 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1940 1.1 christos REQUIRE(limit > 0);
1941 1.1 christos
1942 1.6 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1943 1.1 christos
1944 1.1 christos mpctx->maxalloc = limit;
1945 1.1 christos }
1946 1.1 christos
1947 1.1 christos unsigned int
1948 1.1 christos isc_mempool_getmaxalloc(isc_mempool_t *mpctx0) {
1949 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1950 1.6 christos
1951 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1952 1.1 christos
1953 1.12 christos return (mpctx->maxalloc);
1954 1.1 christos }
1955 1.1 christos
1956 1.1 christos unsigned int
1957 1.3 christos isc_mempool_getallocated(isc_mempool_t *mpctx0) {
1958 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1959 1.6 christos
1960 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1961 1.1 christos
1962 1.12 christos return (mpctx->allocated);
1963 1.1 christos }
1964 1.1 christos
1965 1.1 christos void
1966 1.3 christos isc_mempool_setfillcount(isc_mempool_t *mpctx0, unsigned int limit) {
1967 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1968 1.6 christos REQUIRE(limit > 0);
1969 1.6 christos
1970 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1971 1.1 christos
1972 1.1 christos mpctx->fillcount = limit;
1973 1.1 christos }
1974 1.1 christos
1975 1.1 christos unsigned int
1976 1.1 christos isc_mempool_getfillcount(isc_mempool_t *mpctx0) {
1977 1.6 christos REQUIRE(VALID_MEMPOOL(mpctx0));
1978 1.6 christos
1979 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1980 1.1 christos
1981 1.12 christos return (mpctx->fillcount);
1982 1.1 christos }
1983 1.1 christos
1984 1.1 christos /*
1985 1.1 christos * Requires contextslock to be held by caller.
1986 1.1 christos */
1987 1.1 christos static void
1988 1.1 christos print_contexts(FILE *file) {
1989 1.1 christos isc__mem_t *ctx;
1990 1.1 christos
1991 1.6 christos for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
1992 1.13 christos ctx = ISC_LIST_NEXT(ctx, link))
1993 1.13 christos {
1994 1.3 christos fprintf(file, "context: %p (%s): %" PRIuFAST32 " references\n",
1995 1.6 christos ctx, ctx->name[0] == 0 ? "<unknown>" : ctx->name,
1996 1.3 christos isc_refcount_current(&ctx->references));
1997 1.1 christos print_active(ctx, file);
1998 1.1 christos }
1999 1.1 christos fflush(file);
2000 1.1 christos }
2001 1.1 christos
2002 1.12 christos static atomic_uintptr_t checkdestroyed = 0;
2003 1.10 christos
2004 1.1 christos void
2005 1.1 christos isc_mem_checkdestroyed(FILE *file) {
2006 1.10 christos atomic_store_release(&checkdestroyed, (uintptr_t)file);
2007 1.10 christos }
2008 1.10 christos
2009 1.10 christos void
2010 1.10 christos isc__mem_checkdestroyed(void) {
2011 1.10 christos FILE *file = (FILE *)atomic_load_acquire(&checkdestroyed);
2012 1.1 christos
2013 1.10 christos if (file == NULL) {
2014 1.10 christos return;
2015 1.10 christos }
2016 1.1 christos
2017 1.1 christos LOCK(&contextslock);
2018 1.1 christos if (!ISC_LIST_EMPTY(contexts)) {
2019 1.1 christos #if ISC_MEM_TRACKLINES
2020 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0)) {
2021 1.1 christos print_contexts(file);
2022 1.1 christos }
2023 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
2024 1.12 christos UNREACHABLE();
2025 1.1 christos }
2026 1.1 christos UNLOCK(&contextslock);
2027 1.1 christos }
2028 1.1 christos
2029 1.1 christos unsigned int
2030 1.1 christos isc_mem_references(isc_mem_t *ctx0) {
2031 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
2032 1.3 christos return (isc_refcount_current(&ctx->references));
2033 1.1 christos }
2034 1.1 christos
2035 1.1 christos typedef struct summarystat {
2036 1.6 christos uint64_t total;
2037 1.6 christos uint64_t inuse;
2038 1.6 christos uint64_t malloced;
2039 1.6 christos uint64_t blocksize;
2040 1.6 christos uint64_t contextsize;
2041 1.1 christos } summarystat_t;
2042 1.1 christos
2043 1.1 christos #ifdef HAVE_LIBXML2
2044 1.6 christos #define TRY0(a) \
2045 1.6 christos do { \
2046 1.6 christos xmlrc = (a); \
2047 1.6 christos if (xmlrc < 0) \
2048 1.6 christos goto error; \
2049 1.9 rillig } while (0)
2050 1.1 christos static int
2051 1.1 christos xml_renderctx(isc__mem_t *ctx, summarystat_t *summary,
2052 1.6 christos xmlTextWriterPtr writer) {
2053 1.6 christos REQUIRE(VALID_CONTEXT(ctx));
2054 1.6 christos
2055 1.1 christos int xmlrc;
2056 1.1 christos
2057 1.6 christos MCTXLOCK(ctx);
2058 1.1 christos
2059 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "context"));
2060 1.1 christos
2061 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "id"));
2062 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%p", ctx));
2063 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* id */
2064 1.1 christos
2065 1.1 christos if (ctx->name[0] != 0) {
2066 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
2067 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%s", ctx->name));
2068 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* name */
2069 1.1 christos }
2070 1.1 christos
2071 1.1 christos summary->contextsize += sizeof(*ctx) +
2072 1.6 christos (ctx->max_size + 1) * sizeof(struct stats) +
2073 1.6 christos ctx->max_size * sizeof(element *) +
2074 1.6 christos ctx->basic_table_count * sizeof(char *);
2075 1.1 christos #if ISC_MEM_TRACKLINES
2076 1.1 christos if (ctx->debuglist != NULL) {
2077 1.6 christos summary->contextsize += DEBUG_TABLE_COUNT *
2078 1.6 christos sizeof(debuglist_t) +
2079 1.6 christos ctx->debuglistcnt * sizeof(debuglink_t);
2080 1.1 christos }
2081 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
2082 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "references"));
2083 1.6 christos TRY0(xmlTextWriterWriteFormatString(
2084 1.6 christos writer, "%" PRIuFAST32,
2085 1.6 christos isc_refcount_current(&ctx->references)));
2086 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* references */
2087 1.1 christos
2088 1.1 christos summary->total += ctx->total;
2089 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "total"));
2090 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2091 1.3 christos (uint64_t)ctx->total));
2092 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* total */
2093 1.1 christos
2094 1.1 christos summary->inuse += ctx->inuse;
2095 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "inuse"));
2096 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2097 1.3 christos (uint64_t)ctx->inuse));
2098 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* inuse */
2099 1.1 christos
2100 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxinuse"));
2101 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2102 1.3 christos (uint64_t)ctx->maxinuse));
2103 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* maxinuse */
2104 1.1 christos
2105 1.1 christos summary->malloced += ctx->malloced;
2106 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "malloced"));
2107 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2108 1.3 christos (uint64_t)ctx->malloced));
2109 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* malloced */
2110 1.1 christos
2111 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxmalloced"));
2112 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2113 1.3 christos (uint64_t)ctx->maxmalloced));
2114 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* maxmalloced */
2115 1.1 christos
2116 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "blocksize"));
2117 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2118 1.1 christos summary->blocksize += ctx->basic_table_count *
2119 1.6 christos NUM_BASIC_BLOCKS * ctx->mem_target;
2120 1.6 christos TRY0(xmlTextWriterWriteFormatString(
2121 1.6 christos writer, "%" PRIu64 "",
2122 1.6 christos (uint64_t)ctx->basic_table_count * NUM_BASIC_BLOCKS *
2123 1.6 christos ctx->mem_target));
2124 1.6 christos } else {
2125 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%s", "-"));
2126 1.6 christos }
2127 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* blocksize */
2128 1.1 christos
2129 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "pools"));
2130 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%u", ctx->poolcnt));
2131 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* pools */
2132 1.1 christos summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
2133 1.1 christos
2134 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "hiwater"));
2135 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2136 1.3 christos (uint64_t)ctx->hi_water));
2137 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* hiwater */
2138 1.1 christos
2139 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "lowater"));
2140 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2141 1.3 christos (uint64_t)ctx->lo_water));
2142 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* lowater */
2143 1.1 christos
2144 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* context */
2145 1.1 christos
2146 1.6 christos error:
2147 1.6 christos MCTXUNLOCK(ctx);
2148 1.1 christos
2149 1.1 christos return (xmlrc);
2150 1.1 christos }
2151 1.1 christos
2152 1.1 christos int
2153 1.6 christos isc_mem_renderxml(void *writer0) {
2154 1.1 christos isc__mem_t *ctx;
2155 1.1 christos summarystat_t summary;
2156 1.3 christos uint64_t lost;
2157 1.1 christos int xmlrc;
2158 1.6 christos xmlTextWriterPtr writer = (xmlTextWriterPtr)writer0;
2159 1.1 christos
2160 1.1 christos memset(&summary, 0, sizeof(summary));
2161 1.1 christos
2162 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "contexts"));
2163 1.1 christos
2164 1.1 christos LOCK(&contextslock);
2165 1.1 christos lost = totallost;
2166 1.6 christos for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
2167 1.13 christos ctx = ISC_LIST_NEXT(ctx, link))
2168 1.13 christos {
2169 1.1 christos xmlrc = xml_renderctx(ctx, &summary, writer);
2170 1.1 christos if (xmlrc < 0) {
2171 1.1 christos UNLOCK(&contextslock);
2172 1.1 christos goto error;
2173 1.1 christos }
2174 1.1 christos }
2175 1.1 christos UNLOCK(&contextslock);
2176 1.1 christos
2177 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* contexts */
2178 1.1 christos
2179 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "summary"));
2180 1.1 christos
2181 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "TotalUse"));
2182 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2183 1.1 christos summary.total));
2184 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* TotalUse */
2185 1.1 christos
2186 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "InUse"));
2187 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2188 1.1 christos summary.inuse));
2189 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* InUse */
2190 1.1 christos
2191 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "Malloced"));
2192 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2193 1.1 christos summary.malloced));
2194 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* InUse */
2195 1.1 christos
2196 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "BlockSize"));
2197 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2198 1.1 christos summary.blocksize));
2199 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* BlockSize */
2200 1.1 christos
2201 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "ContextSize"));
2202 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "",
2203 1.1 christos summary.contextsize));
2204 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* ContextSize */
2205 1.1 christos
2206 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "Lost"));
2207 1.6 christos TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "", lost));
2208 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* Lost */
2209 1.1 christos
2210 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* summary */
2211 1.6 christos error:
2212 1.1 christos return (xmlrc);
2213 1.1 christos }
2214 1.1 christos
2215 1.1 christos #endif /* HAVE_LIBXML2 */
2216 1.1 christos
2217 1.6 christos #ifdef HAVE_JSON_C
2218 1.3 christos #define CHECKMEM(m) RUNTIME_CHECK(m != NULL)
2219 1.1 christos
2220 1.1 christos static isc_result_t
2221 1.1 christos json_renderctx(isc__mem_t *ctx, summarystat_t *summary, json_object *array) {
2222 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
2223 1.1 christos REQUIRE(summary != NULL);
2224 1.1 christos REQUIRE(array != NULL);
2225 1.1 christos
2226 1.6 christos json_object *ctxobj, *obj;
2227 1.6 christos char buf[1024];
2228 1.6 christos
2229 1.6 christos MCTXLOCK(ctx);
2230 1.1 christos
2231 1.1 christos summary->contextsize += sizeof(*ctx) +
2232 1.6 christos (ctx->max_size + 1) * sizeof(struct stats) +
2233 1.6 christos ctx->max_size * sizeof(element *) +
2234 1.6 christos ctx->basic_table_count * sizeof(char *);
2235 1.1 christos summary->total += ctx->total;
2236 1.1 christos summary->inuse += ctx->inuse;
2237 1.1 christos summary->malloced += ctx->malloced;
2238 1.6 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2239 1.1 christos summary->blocksize += ctx->basic_table_count *
2240 1.6 christos NUM_BASIC_BLOCKS * ctx->mem_target;
2241 1.6 christos }
2242 1.1 christos #if ISC_MEM_TRACKLINES
2243 1.1 christos if (ctx->debuglist != NULL) {
2244 1.6 christos summary->contextsize += DEBUG_TABLE_COUNT *
2245 1.6 christos sizeof(debuglist_t) +
2246 1.6 christos ctx->debuglistcnt * sizeof(debuglink_t);
2247 1.1 christos }
2248 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
2249 1.1 christos
2250 1.1 christos ctxobj = json_object_new_object();
2251 1.1 christos CHECKMEM(ctxobj);
2252 1.1 christos
2253 1.1 christos snprintf(buf, sizeof(buf), "%p", ctx);
2254 1.1 christos obj = json_object_new_string(buf);
2255 1.1 christos CHECKMEM(obj);
2256 1.1 christos json_object_object_add(ctxobj, "id", obj);
2257 1.1 christos
2258 1.1 christos if (ctx->name[0] != 0) {
2259 1.1 christos obj = json_object_new_string(ctx->name);
2260 1.1 christos CHECKMEM(obj);
2261 1.1 christos json_object_object_add(ctxobj, "name", obj);
2262 1.1 christos }
2263 1.1 christos
2264 1.3 christos obj = json_object_new_int64(isc_refcount_current(&ctx->references));
2265 1.1 christos CHECKMEM(obj);
2266 1.1 christos json_object_object_add(ctxobj, "references", obj);
2267 1.1 christos
2268 1.1 christos obj = json_object_new_int64(ctx->total);
2269 1.1 christos CHECKMEM(obj);
2270 1.1 christos json_object_object_add(ctxobj, "total", obj);
2271 1.1 christos
2272 1.1 christos obj = json_object_new_int64(ctx->inuse);
2273 1.1 christos CHECKMEM(obj);
2274 1.1 christos json_object_object_add(ctxobj, "inuse", obj);
2275 1.1 christos
2276 1.1 christos obj = json_object_new_int64(ctx->maxinuse);
2277 1.1 christos CHECKMEM(obj);
2278 1.1 christos json_object_object_add(ctxobj, "maxinuse", obj);
2279 1.1 christos
2280 1.1 christos obj = json_object_new_int64(ctx->malloced);
2281 1.1 christos CHECKMEM(obj);
2282 1.1 christos json_object_object_add(ctxobj, "malloced", obj);
2283 1.1 christos
2284 1.1 christos obj = json_object_new_int64(ctx->maxmalloced);
2285 1.1 christos CHECKMEM(obj);
2286 1.1 christos json_object_object_add(ctxobj, "maxmalloced", obj);
2287 1.1 christos
2288 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2289 1.3 christos uint64_t blocksize;
2290 1.1 christos blocksize = ctx->basic_table_count * NUM_BASIC_BLOCKS *
2291 1.6 christos ctx->mem_target;
2292 1.1 christos obj = json_object_new_int64(blocksize);
2293 1.1 christos CHECKMEM(obj);
2294 1.1 christos json_object_object_add(ctxobj, "blocksize", obj);
2295 1.1 christos }
2296 1.1 christos
2297 1.1 christos obj = json_object_new_int64(ctx->poolcnt);
2298 1.1 christos CHECKMEM(obj);
2299 1.1 christos json_object_object_add(ctxobj, "pools", obj);
2300 1.1 christos
2301 1.1 christos summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
2302 1.1 christos
2303 1.1 christos obj = json_object_new_int64(ctx->hi_water);
2304 1.1 christos CHECKMEM(obj);
2305 1.1 christos json_object_object_add(ctxobj, "hiwater", obj);
2306 1.1 christos
2307 1.1 christos obj = json_object_new_int64(ctx->lo_water);
2308 1.1 christos CHECKMEM(obj);
2309 1.1 christos json_object_object_add(ctxobj, "lowater", obj);
2310 1.1 christos
2311 1.6 christos MCTXUNLOCK(ctx);
2312 1.1 christos json_object_array_add(array, ctxobj);
2313 1.1 christos return (ISC_R_SUCCESS);
2314 1.1 christos }
2315 1.1 christos
2316 1.1 christos isc_result_t
2317 1.6 christos isc_mem_renderjson(void *memobj0) {
2318 1.1 christos isc_result_t result = ISC_R_SUCCESS;
2319 1.1 christos isc__mem_t *ctx;
2320 1.1 christos summarystat_t summary;
2321 1.3 christos uint64_t lost;
2322 1.1 christos json_object *ctxarray, *obj;
2323 1.6 christos json_object *memobj = (json_object *)memobj0;
2324 1.1 christos
2325 1.1 christos memset(&summary, 0, sizeof(summary));
2326 1.1 christos
2327 1.1 christos ctxarray = json_object_new_array();
2328 1.1 christos CHECKMEM(ctxarray);
2329 1.1 christos
2330 1.1 christos LOCK(&contextslock);
2331 1.1 christos lost = totallost;
2332 1.6 christos for (ctx = ISC_LIST_HEAD(contexts); ctx != NULL;
2333 1.13 christos ctx = ISC_LIST_NEXT(ctx, link))
2334 1.13 christos {
2335 1.1 christos result = json_renderctx(ctx, &summary, ctxarray);
2336 1.1 christos if (result != ISC_R_SUCCESS) {
2337 1.1 christos UNLOCK(&contextslock);
2338 1.1 christos goto error;
2339 1.1 christos }
2340 1.1 christos }
2341 1.1 christos UNLOCK(&contextslock);
2342 1.1 christos
2343 1.1 christos obj = json_object_new_int64(summary.total);
2344 1.1 christos CHECKMEM(obj);
2345 1.1 christos json_object_object_add(memobj, "TotalUse", obj);
2346 1.1 christos
2347 1.1 christos obj = json_object_new_int64(summary.inuse);
2348 1.1 christos CHECKMEM(obj);
2349 1.1 christos json_object_object_add(memobj, "InUse", obj);
2350 1.1 christos
2351 1.1 christos obj = json_object_new_int64(summary.malloced);
2352 1.1 christos CHECKMEM(obj);
2353 1.1 christos json_object_object_add(memobj, "Malloced", obj);
2354 1.1 christos
2355 1.1 christos obj = json_object_new_int64(summary.blocksize);
2356 1.1 christos CHECKMEM(obj);
2357 1.1 christos json_object_object_add(memobj, "BlockSize", obj);
2358 1.1 christos
2359 1.1 christos obj = json_object_new_int64(summary.contextsize);
2360 1.1 christos CHECKMEM(obj);
2361 1.1 christos json_object_object_add(memobj, "ContextSize", obj);
2362 1.1 christos
2363 1.1 christos obj = json_object_new_int64(lost);
2364 1.1 christos CHECKMEM(obj);
2365 1.1 christos json_object_object_add(memobj, "Lost", obj);
2366 1.1 christos
2367 1.1 christos json_object_object_add(memobj, "contexts", ctxarray);
2368 1.1 christos return (ISC_R_SUCCESS);
2369 1.1 christos
2370 1.6 christos error:
2371 1.6 christos if (ctxarray != NULL) {
2372 1.1 christos json_object_put(ctxarray);
2373 1.6 christos }
2374 1.1 christos return (result);
2375 1.1 christos }
2376 1.6 christos #endif /* HAVE_JSON_C */
2377 1.1 christos
2378 1.6 christos void
2379 1.6 christos isc_mem_create(isc_mem_t **mctxp) {
2380 1.6 christos mem_create(mctxp, isc_mem_defaultflags);
2381 1.1 christos }
2382 1.1 christos
2383 1.1 christos void *
2384 1.1 christos isc__mem_get(isc_mem_t *mctx, size_t size FLARG) {
2385 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2386 1.1 christos
2387 1.1 christos return (mctx->methods->memget(mctx, size FLARG_PASS));
2388 1.1 christos }
2389 1.1 christos
2390 1.1 christos void
2391 1.1 christos isc__mem_put(isc_mem_t *mctx, void *ptr, size_t size FLARG) {
2392 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2393 1.1 christos
2394 1.3 christos mctx->methods->memput(mctx, ptr, size FLARG_PASS);
2395 1.1 christos }
2396 1.1 christos
2397 1.1 christos void
2398 1.1 christos isc__mem_putanddetach(isc_mem_t **mctxp, void *ptr, size_t size FLARG) {
2399 1.1 christos REQUIRE(mctxp != NULL && ISCAPI_MCTX_VALID(*mctxp));
2400 1.1 christos
2401 1.3 christos (*mctxp)->methods->memputanddetach(mctxp, ptr, size FLARG_PASS);
2402 1.1 christos }
2403 1.1 christos
2404 1.1 christos void *
2405 1.1 christos isc__mem_allocate(isc_mem_t *mctx, size_t size FLARG) {
2406 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2407 1.1 christos
2408 1.1 christos return (mctx->methods->memallocate(mctx, size FLARG_PASS));
2409 1.1 christos }
2410 1.1 christos
2411 1.1 christos void *
2412 1.1 christos isc__mem_reallocate(isc_mem_t *mctx, void *ptr, size_t size FLARG) {
2413 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2414 1.1 christos
2415 1.1 christos return (mctx->methods->memreallocate(mctx, ptr, size FLARG_PASS));
2416 1.1 christos }
2417 1.1 christos
2418 1.1 christos char *
2419 1.1 christos isc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
2420 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2421 1.1 christos
2422 1.1 christos return (mctx->methods->memstrdup(mctx, s FLARG_PASS));
2423 1.1 christos }
2424 1.1 christos
2425 1.11 christos char *
2426 1.11 christos isc__mem_strndup(isc_mem_t *mctx, const char *s, size_t size FLARG) {
2427 1.11 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2428 1.11 christos
2429 1.11 christos return (mctx->methods->memstrndup(mctx, s, size FLARG_PASS));
2430 1.11 christos }
2431 1.11 christos
2432 1.1 christos void
2433 1.1 christos isc__mem_free(isc_mem_t *mctx, void *ptr FLARG) {
2434 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2435 1.1 christos
2436 1.3 christos mctx->methods->memfree(mctx, ptr FLARG_PASS);
2437 1.1 christos }
2438 1.1 christos
2439 1.3 christos void
2440 1.3 christos isc__mem_printactive(isc_mem_t *ctx0, FILE *file) {
2441 1.3 christos #if ISC_MEM_TRACKLINES
2442 1.6 christos REQUIRE(VALID_CONTEXT(ctx0));
2443 1.6 christos REQUIRE(file != NULL);
2444 1.6 christos
2445 1.3 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
2446 1.1 christos
2447 1.3 christos print_active(ctx, file);
2448 1.6 christos #else /* if ISC_MEM_TRACKLINES */
2449 1.3 christos UNUSED(ctx0);
2450 1.3 christos UNUSED(file);
2451 1.6 christos #endif /* if ISC_MEM_TRACKLINES */
2452 1.1 christos }
2453