mem.c revision 1.1 1 1.1 christos /* $NetBSD: mem.c,v 1.1 2018/08/12 12:08:23 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.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
7 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
8 1.1 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 1.1 christos *
10 1.1 christos * See the COPYRIGHT file distributed with this work for additional
11 1.1 christos * information regarding copyright ownership.
12 1.1 christos */
13 1.1 christos
14 1.1 christos /*! \file */
15 1.1 christos
16 1.1 christos #include <config.h>
17 1.1 christos
18 1.1 christos #include <stdio.h>
19 1.1 christos #include <stdlib.h>
20 1.1 christos #include <stddef.h>
21 1.1 christos
22 1.1 christos #include <limits.h>
23 1.1 christos
24 1.1 christos #include <isc/bind9.h>
25 1.1 christos #include <isc/json.h>
26 1.1 christos #include <isc/magic.h>
27 1.1 christos #include <isc/hash.h>
28 1.1 christos #include <isc/mem.h>
29 1.1 christos #include <isc/msgs.h>
30 1.1 christos #include <isc/once.h>
31 1.1 christos #include <isc/ondestroy.h>
32 1.1 christos #include <isc/string.h>
33 1.1 christos #include <isc/mutex.h>
34 1.1 christos #include <isc/print.h>
35 1.1 christos #include <isc/util.h>
36 1.1 christos #include <isc/xml.h>
37 1.1 christos
38 1.1 christos #define MCTXLOCK(m, l) if (((m)->flags & ISC_MEMFLAG_NOLOCK) == 0) LOCK(l)
39 1.1 christos #define MCTXUNLOCK(m, l) if (((m)->flags & ISC_MEMFLAG_NOLOCK) == 0) UNLOCK(l)
40 1.1 christos
41 1.1 christos #ifndef ISC_MEM_DEBUGGING
42 1.1 christos #define ISC_MEM_DEBUGGING 0
43 1.1 christos #endif
44 1.1 christos LIBISC_EXTERNAL_DATA unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING;
45 1.1 christos LIBISC_EXTERNAL_DATA unsigned int isc_mem_defaultflags = ISC_MEMFLAG_DEFAULT;
46 1.1 christos
47 1.1 christos /*
48 1.1 christos * Constants.
49 1.1 christos */
50 1.1 christos
51 1.1 christos #define DEF_MAX_SIZE 1100
52 1.1 christos #define DEF_MEM_TARGET 4096
53 1.1 christos #define ALIGNMENT_SIZE 8U /*%< must be a power of 2 */
54 1.1 christos #define NUM_BASIC_BLOCKS 64 /*%< must be > 1 */
55 1.1 christos #define TABLE_INCREMENT 1024
56 1.1 christos #define DEBUG_TABLE_COUNT 512U
57 1.1 christos
58 1.1 christos /*
59 1.1 christos * Types.
60 1.1 christos */
61 1.1 christos typedef struct isc__mem isc__mem_t;
62 1.1 christos typedef struct isc__mempool isc__mempool_t;
63 1.1 christos
64 1.1 christos #if ISC_MEM_TRACKLINES
65 1.1 christos typedef struct debuglink debuglink_t;
66 1.1 christos struct debuglink {
67 1.1 christos ISC_LINK(debuglink_t) link;
68 1.1 christos const void *ptr;
69 1.1 christos size_t size;
70 1.1 christos const char *file;
71 1.1 christos unsigned int line;
72 1.1 christos };
73 1.1 christos
74 1.1 christos typedef ISC_LIST(debuglink_t) debuglist_t;
75 1.1 christos
76 1.1 christos #define FLARG_PASS , file, line
77 1.1 christos #define FLARG , const char *file, unsigned int line
78 1.1 christos #else
79 1.1 christos #define FLARG_PASS
80 1.1 christos #define FLARG
81 1.1 christos #endif
82 1.1 christos
83 1.1 christos typedef struct element element;
84 1.1 christos struct element {
85 1.1 christos element * next;
86 1.1 christos };
87 1.1 christos
88 1.1 christos typedef struct {
89 1.1 christos /*!
90 1.1 christos * This structure must be ALIGNMENT_SIZE bytes.
91 1.1 christos */
92 1.1 christos union {
93 1.1 christos size_t size;
94 1.1 christos isc__mem_t *ctx;
95 1.1 christos char bytes[ALIGNMENT_SIZE];
96 1.1 christos } u;
97 1.1 christos } size_info;
98 1.1 christos
99 1.1 christos struct stats {
100 1.1 christos unsigned long gets;
101 1.1 christos unsigned long totalgets;
102 1.1 christos unsigned long blocks;
103 1.1 christos unsigned long freefrags;
104 1.1 christos };
105 1.1 christos
106 1.1 christos #define MEM_MAGIC ISC_MAGIC('M', 'e', 'm', 'C')
107 1.1 christos #define VALID_CONTEXT(c) ISC_MAGIC_VALID(c, MEM_MAGIC)
108 1.1 christos
109 1.1 christos /* List of all active memory contexts. */
110 1.1 christos
111 1.1 christos static ISC_LIST(isc__mem_t) contexts;
112 1.1 christos
113 1.1 christos static isc_once_t once = ISC_ONCE_INIT;
114 1.1 christos static isc_mutex_t contextslock;
115 1.1 christos static isc_mutex_t createlock;
116 1.1 christos
117 1.1 christos /*%
118 1.1 christos * Total size of lost memory due to a bug of external library.
119 1.1 christos * Locked by the global lock.
120 1.1 christos */
121 1.1 christos static isc_uint64_t totallost;
122 1.1 christos
123 1.1 christos struct isc__mem {
124 1.1 christos isc_mem_t common;
125 1.1 christos isc_ondestroy_t ondestroy;
126 1.1 christos unsigned int flags;
127 1.1 christos isc_mutex_t lock;
128 1.1 christos isc_memalloc_t memalloc;
129 1.1 christos isc_memfree_t memfree;
130 1.1 christos void * arg;
131 1.1 christos size_t max_size;
132 1.1 christos isc_boolean_t checkfree;
133 1.1 christos struct stats * stats;
134 1.1 christos unsigned int references;
135 1.1 christos char name[16];
136 1.1 christos void * tag;
137 1.1 christos size_t quota;
138 1.1 christos size_t total;
139 1.1 christos size_t inuse;
140 1.1 christos size_t maxinuse;
141 1.1 christos size_t malloced;
142 1.1 christos size_t maxmalloced;
143 1.1 christos size_t hi_water;
144 1.1 christos size_t lo_water;
145 1.1 christos isc_boolean_t hi_called;
146 1.1 christos isc_boolean_t is_overmem;
147 1.1 christos isc_mem_water_t water;
148 1.1 christos void * water_arg;
149 1.1 christos ISC_LIST(isc__mempool_t) pools;
150 1.1 christos unsigned int poolcnt;
151 1.1 christos
152 1.1 christos /* ISC_MEMFLAG_INTERNAL */
153 1.1 christos size_t mem_target;
154 1.1 christos element ** freelists;
155 1.1 christos element * basic_blocks;
156 1.1 christos unsigned char ** basic_table;
157 1.1 christos unsigned int basic_table_count;
158 1.1 christos unsigned int basic_table_size;
159 1.1 christos unsigned char * lowest;
160 1.1 christos unsigned char * highest;
161 1.1 christos
162 1.1 christos #if ISC_MEM_TRACKLINES
163 1.1 christos debuglist_t * debuglist;
164 1.1 christos size_t debuglistcnt;
165 1.1 christos #endif
166 1.1 christos
167 1.1 christos unsigned int memalloc_failures;
168 1.1 christos ISC_LINK(isc__mem_t) link;
169 1.1 christos };
170 1.1 christos
171 1.1 christos #define MEMPOOL_MAGIC ISC_MAGIC('M', 'E', 'M', 'p')
172 1.1 christos #define VALID_MEMPOOL(c) ISC_MAGIC_VALID(c, MEMPOOL_MAGIC)
173 1.1 christos
174 1.1 christos struct isc__mempool {
175 1.1 christos /* always unlocked */
176 1.1 christos isc_mempool_t common; /*%< common header of mempool's */
177 1.1 christos isc_mutex_t *lock; /*%< optional lock */
178 1.1 christos isc__mem_t *mctx; /*%< our memory context */
179 1.1 christos /*%< locked via the memory context's lock */
180 1.1 christos ISC_LINK(isc__mempool_t) link; /*%< next pool in this mem context */
181 1.1 christos /*%< optionally locked from here down */
182 1.1 christos element *items; /*%< low water item list */
183 1.1 christos size_t size; /*%< size of each item on this pool */
184 1.1 christos unsigned int maxalloc; /*%< max number of items allowed */
185 1.1 christos unsigned int allocated; /*%< # of items currently given out */
186 1.1 christos unsigned int freecount; /*%< # of items on reserved list */
187 1.1 christos unsigned int freemax; /*%< # of items allowed on free list */
188 1.1 christos unsigned int fillcount; /*%< # of items to fetch on each fill */
189 1.1 christos /*%< Stats only. */
190 1.1 christos unsigned int gets; /*%< # of requests to this pool */
191 1.1 christos /*%< Debugging only. */
192 1.1 christos #if ISC_MEMPOOL_NAMES
193 1.1 christos char name[16]; /*%< printed name in stats reports */
194 1.1 christos #endif
195 1.1 christos };
196 1.1 christos
197 1.1 christos /*
198 1.1 christos * Private Inline-able.
199 1.1 christos */
200 1.1 christos
201 1.1 christos #if ! ISC_MEM_TRACKLINES
202 1.1 christos #define ADD_TRACE(a, b, c, d, e)
203 1.1 christos #define DELETE_TRACE(a, b, c, d, e)
204 1.1 christos #define ISC_MEMFUNC_SCOPE
205 1.1 christos #else
206 1.1 christos #define TRACE_OR_RECORD (ISC_MEM_DEBUGTRACE|ISC_MEM_DEBUGRECORD)
207 1.1 christos #define ADD_TRACE(a, b, c, d, e) \
208 1.1 christos do { \
209 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0 && \
210 1.1 christos b != NULL)) \
211 1.1 christos add_trace_entry(a, b, c, d, e); \
212 1.1 christos } while (0)
213 1.1 christos #define DELETE_TRACE(a, b, c, d, e) \
214 1.1 christos do { \
215 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0 && \
216 1.1 christos b != NULL)) \
217 1.1 christos delete_trace_entry(a, b, c, d, e); \
218 1.1 christos } while(0)
219 1.1 christos
220 1.1 christos static void
221 1.1 christos print_active(isc__mem_t *ctx, FILE *out);
222 1.1 christos
223 1.1 christos #endif /* ISC_MEM_TRACKLINES */
224 1.1 christos
225 1.1 christos /*%
226 1.1 christos * The following are intended for internal use (indicated by "isc__"
227 1.1 christos * prefix) but are not declared as static, allowing direct access
228 1.1 christos * from unit tests, etc.
229 1.1 christos */
230 1.1 christos
231 1.1 christos isc_result_t
232 1.1 christos isc__mem_create2(size_t init_max_size, size_t target_size,
233 1.1 christos isc_mem_t **ctxp, unsigned int flags);
234 1.1 christos void
235 1.1 christos isc__mem_attach(isc_mem_t *source, isc_mem_t **targetp);
236 1.1 christos void
237 1.1 christos isc__mem_detach(isc_mem_t **ctxp);
238 1.1 christos void
239 1.1 christos isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG);
240 1.1 christos void
241 1.1 christos isc__mem_destroy(isc_mem_t **ctxp);
242 1.1 christos isc_result_t
243 1.1 christos isc__mem_ondestroy(isc_mem_t *ctx, isc_task_t *task, isc_event_t **event);
244 1.1 christos void *
245 1.1 christos isc___mem_get(isc_mem_t *ctx, size_t size FLARG);
246 1.1 christos void
247 1.1 christos isc___mem_put(isc_mem_t *ctx, void *ptr, size_t size FLARG);
248 1.1 christos void
249 1.1 christos isc__mem_stats(isc_mem_t *ctx, FILE *out);
250 1.1 christos void *
251 1.1 christos isc___mem_allocate(isc_mem_t *ctx, size_t size FLARG);
252 1.1 christos void *
253 1.1 christos isc___mem_reallocate(isc_mem_t *ctx, void *ptr, size_t size FLARG);
254 1.1 christos void
255 1.1 christos isc___mem_free(isc_mem_t *ctx, void *ptr FLARG);
256 1.1 christos char *
257 1.1 christos isc___mem_strdup(isc_mem_t *mctx, const char *s FLARG);
258 1.1 christos void
259 1.1 christos isc__mem_setdestroycheck(isc_mem_t *ctx, isc_boolean_t flag);
260 1.1 christos void
261 1.1 christos isc__mem_setquota(isc_mem_t *ctx, size_t quota);
262 1.1 christos size_t
263 1.1 christos isc__mem_getquota(isc_mem_t *ctx);
264 1.1 christos size_t
265 1.1 christos isc__mem_inuse(isc_mem_t *ctx);
266 1.1 christos size_t
267 1.1 christos isc__mem_maxinuse(isc_mem_t *ctx);
268 1.1 christos size_t
269 1.1 christos isc__mem_total(isc_mem_t *ctx);
270 1.1 christos isc_boolean_t
271 1.1 christos isc__mem_isovermem(isc_mem_t *ctx);
272 1.1 christos void
273 1.1 christos isc__mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
274 1.1 christos size_t hiwater, size_t lowater);
275 1.1 christos void
276 1.1 christos isc__mem_waterack(isc_mem_t *ctx0, int flag);
277 1.1 christos void
278 1.1 christos isc__mem_setname(isc_mem_t *ctx, const char *name, void *tag);
279 1.1 christos const char *
280 1.1 christos isc__mem_getname(isc_mem_t *ctx);
281 1.1 christos void *
282 1.1 christos isc__mem_gettag(isc_mem_t *ctx);
283 1.1 christos isc_result_t
284 1.1 christos isc__mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
285 1.1 christos void
286 1.1 christos isc__mempool_setname(isc_mempool_t *mpctx, const char *name);
287 1.1 christos void
288 1.1 christos isc__mempool_destroy(isc_mempool_t **mpctxp);
289 1.1 christos void
290 1.1 christos isc__mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
291 1.1 christos void *
292 1.1 christos isc___mempool_get(isc_mempool_t *mpctx FLARG);
293 1.1 christos void
294 1.1 christos isc___mempool_put(isc_mempool_t *mpctx, void *mem FLARG);
295 1.1 christos void
296 1.1 christos isc__mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
297 1.1 christos unsigned int
298 1.1 christos isc__mempool_getfreemax(isc_mempool_t *mpctx);
299 1.1 christos unsigned int
300 1.1 christos isc__mempool_getfreecount(isc_mempool_t *mpctx);
301 1.1 christos void
302 1.1 christos isc__mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
303 1.1 christos unsigned int
304 1.1 christos isc__mempool_getmaxalloc(isc_mempool_t *mpctx);
305 1.1 christos unsigned int
306 1.1 christos isc__mempool_getallocated(isc_mempool_t *mpctx);
307 1.1 christos void
308 1.1 christos isc__mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
309 1.1 christos unsigned int
310 1.1 christos isc__mempool_getfillcount(isc_mempool_t *mpctx);
311 1.1 christos void
312 1.1 christos isc__mem_printactive(isc_mem_t *ctx0, FILE *file);
313 1.1 christos unsigned int
314 1.1 christos isc__mem_references(isc_mem_t *ctx0);
315 1.1 christos
316 1.1 christos static struct isc__memmethods {
317 1.1 christos isc_memmethods_t methods;
318 1.1 christos
319 1.1 christos /*%
320 1.1 christos * The following are defined just for avoiding unused static functions.
321 1.1 christos */
322 1.1 christos void *createx, *create, *create2, *ondestroy, *stats,
323 1.1 christos *setquota, *getquota, *setname, *getname, *gettag;
324 1.1 christos } memmethods = {
325 1.1 christos {
326 1.1 christos isc__mem_attach,
327 1.1 christos isc__mem_detach,
328 1.1 christos isc__mem_destroy,
329 1.1 christos isc___mem_get,
330 1.1 christos isc___mem_put,
331 1.1 christos isc___mem_putanddetach,
332 1.1 christos isc___mem_allocate,
333 1.1 christos isc___mem_reallocate,
334 1.1 christos isc___mem_strdup,
335 1.1 christos isc___mem_free,
336 1.1 christos isc__mem_setdestroycheck,
337 1.1 christos isc__mem_setwater,
338 1.1 christos isc__mem_waterack,
339 1.1 christos isc__mem_inuse,
340 1.1 christos isc__mem_maxinuse,
341 1.1 christos isc__mem_total,
342 1.1 christos isc__mem_isovermem,
343 1.1 christos isc__mempool_create
344 1.1 christos },
345 1.1 christos (void *)isc_mem_createx,
346 1.1 christos (void *)isc_mem_create,
347 1.1 christos (void *)isc_mem_create2,
348 1.1 christos (void *)isc_mem_ondestroy,
349 1.1 christos (void *)isc_mem_stats,
350 1.1 christos (void *)isc_mem_setquota,
351 1.1 christos (void *)isc_mem_getquota,
352 1.1 christos (void *)isc_mem_setname,
353 1.1 christos (void *)isc_mem_getname,
354 1.1 christos (void *)isc_mem_gettag
355 1.1 christos };
356 1.1 christos
357 1.1 christos static struct isc__mempoolmethods {
358 1.1 christos isc_mempoolmethods_t methods;
359 1.1 christos
360 1.1 christos /*%
361 1.1 christos * The following are defined just for avoiding unused static functions.
362 1.1 christos */
363 1.1 christos void *getfreemax, *getfreecount, *getmaxalloc, *getfillcount;
364 1.1 christos } mempoolmethods = {
365 1.1 christos {
366 1.1 christos isc__mempool_destroy,
367 1.1 christos isc___mempool_get,
368 1.1 christos isc___mempool_put,
369 1.1 christos isc__mempool_getallocated,
370 1.1 christos isc__mempool_setmaxalloc,
371 1.1 christos isc__mempool_setfreemax,
372 1.1 christos isc__mempool_setname,
373 1.1 christos isc__mempool_associatelock,
374 1.1 christos isc__mempool_setfillcount
375 1.1 christos },
376 1.1 christos (void *)isc_mempool_getfreemax,
377 1.1 christos (void *)isc_mempool_getfreecount,
378 1.1 christos (void *)isc_mempool_getmaxalloc,
379 1.1 christos (void *)isc_mempool_getfillcount
380 1.1 christos };
381 1.1 christos
382 1.1 christos #if ISC_MEM_TRACKLINES
383 1.1 christos /*!
384 1.1 christos * mctx must be locked.
385 1.1 christos */
386 1.1 christos static void
387 1.1 christos add_trace_entry(isc__mem_t *mctx, const void *ptr, size_t size FLARG) {
388 1.1 christos debuglink_t *dl;
389 1.1 christos isc_uint32_t hash;
390 1.1 christos isc_uint32_t idx;
391 1.1 christos
392 1.1 christos if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
393 1.1 christos fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
394 1.1 christos ISC_MSG_ADDTRACE,
395 1.1 christos "add %p size %u "
396 1.1 christos "file %s line %u mctx %p\n"),
397 1.1 christos ptr, size, file, line, mctx);
398 1.1 christos
399 1.1 christos if (mctx->debuglist == NULL)
400 1.1 christos return;
401 1.1 christos
402 1.1 christos hash = isc_hash_function(&ptr, sizeof(ptr), ISC_TRUE, NULL);
403 1.1 christos idx = hash % DEBUG_TABLE_COUNT;
404 1.1 christos
405 1.1 christos dl = malloc(sizeof(debuglink_t));
406 1.1 christos INSIST(dl != NULL);
407 1.1 christos mctx->malloced += sizeof(debuglink_t);
408 1.1 christos if (mctx->malloced > mctx->maxmalloced)
409 1.1 christos mctx->maxmalloced = mctx->malloced;
410 1.1 christos
411 1.1 christos ISC_LINK_INIT(dl, link);
412 1.1 christos dl->ptr = ptr;
413 1.1 christos dl->size = size;
414 1.1 christos dl->file = file;
415 1.1 christos dl->line = line;
416 1.1 christos
417 1.1 christos ISC_LIST_PREPEND(mctx->debuglist[idx], dl, link);
418 1.1 christos mctx->debuglistcnt++;
419 1.1 christos }
420 1.1 christos
421 1.1 christos static void
422 1.1 christos delete_trace_entry(isc__mem_t *mctx, const void *ptr, size_t size,
423 1.1 christos const char *file, unsigned int line)
424 1.1 christos {
425 1.1 christos debuglink_t *dl;
426 1.1 christos isc_uint32_t hash;
427 1.1 christos isc_uint32_t idx;
428 1.1 christos
429 1.1 christos if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0)
430 1.1 christos fprintf(stderr, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
431 1.1 christos ISC_MSG_DELTRACE,
432 1.1 christos "del %p size %u "
433 1.1 christos "file %s line %u mctx %p\n"),
434 1.1 christos ptr, size, file, line, mctx);
435 1.1 christos
436 1.1 christos if (mctx->debuglist == NULL)
437 1.1 christos return;
438 1.1 christos
439 1.1 christos hash = isc_hash_function(&ptr, sizeof(ptr), ISC_TRUE, NULL);
440 1.1 christos idx = hash % DEBUG_TABLE_COUNT;
441 1.1 christos
442 1.1 christos dl = ISC_LIST_HEAD(mctx->debuglist[idx]);
443 1.1 christos while (ISC_LIKELY(dl != NULL)) {
444 1.1 christos if (ISC_UNLIKELY(dl->ptr == ptr)) {
445 1.1 christos ISC_LIST_UNLINK(mctx->debuglist[idx], dl, link);
446 1.1 christos mctx->malloced -= sizeof(*dl);
447 1.1 christos free(dl);
448 1.1 christos return;
449 1.1 christos }
450 1.1 christos dl = ISC_LIST_NEXT(dl, link);
451 1.1 christos }
452 1.1 christos
453 1.1 christos /*
454 1.1 christos * If we get here, we didn't find the item on the list. We're
455 1.1 christos * screwed.
456 1.1 christos */
457 1.1 christos INSIST(0);
458 1.1 christos }
459 1.1 christos #endif /* ISC_MEM_TRACKLINES */
460 1.1 christos
461 1.1 christos static inline size_t
462 1.1 christos rmsize(size_t size) {
463 1.1 christos /*
464 1.1 christos * round down to ALIGNMENT_SIZE
465 1.1 christos */
466 1.1 christos return (size & (~(ALIGNMENT_SIZE - 1)));
467 1.1 christos }
468 1.1 christos
469 1.1 christos static inline size_t
470 1.1 christos quantize(size_t size) {
471 1.1 christos /*!
472 1.1 christos * Round up the result in order to get a size big
473 1.1 christos * enough to satisfy the request and be aligned on ALIGNMENT_SIZE
474 1.1 christos * byte boundaries.
475 1.1 christos */
476 1.1 christos
477 1.1 christos if (size == 0U)
478 1.1 christos return (ALIGNMENT_SIZE);
479 1.1 christos return ((size + ALIGNMENT_SIZE - 1) & (~(ALIGNMENT_SIZE - 1)));
480 1.1 christos }
481 1.1 christos
482 1.1 christos static inline isc_boolean_t
483 1.1 christos more_basic_blocks(isc__mem_t *ctx) {
484 1.1 christos void *tmp;
485 1.1 christos unsigned char *curr, *next;
486 1.1 christos unsigned char *first, *last;
487 1.1 christos unsigned char **table;
488 1.1 christos unsigned int table_size;
489 1.1 christos size_t increment;
490 1.1 christos int i;
491 1.1 christos
492 1.1 christos /* Require: we hold the context lock. */
493 1.1 christos
494 1.1 christos /*
495 1.1 christos * Did we hit the quota for this context?
496 1.1 christos */
497 1.1 christos increment = NUM_BASIC_BLOCKS * ctx->mem_target;
498 1.1 christos if (ctx->quota != 0U && ctx->total + increment > ctx->quota)
499 1.1 christos return (ISC_FALSE);
500 1.1 christos
501 1.1 christos INSIST(ctx->basic_table_count <= ctx->basic_table_size);
502 1.1 christos if (ctx->basic_table_count == ctx->basic_table_size) {
503 1.1 christos table_size = ctx->basic_table_size + TABLE_INCREMENT;
504 1.1 christos table = (ctx->memalloc)(ctx->arg,
505 1.1 christos table_size * sizeof(unsigned char *));
506 1.1 christos if (table == NULL) {
507 1.1 christos ctx->memalloc_failures++;
508 1.1 christos return (ISC_FALSE);
509 1.1 christos }
510 1.1 christos ctx->malloced += table_size * sizeof(unsigned char *);
511 1.1 christos if (ctx->malloced > ctx->maxmalloced)
512 1.1 christos ctx->maxmalloced = ctx->malloced;
513 1.1 christos if (ctx->basic_table_size != 0) {
514 1.1 christos memmove(table, ctx->basic_table,
515 1.1 christos ctx->basic_table_size *
516 1.1 christos sizeof(unsigned char *));
517 1.1 christos (ctx->memfree)(ctx->arg, ctx->basic_table);
518 1.1 christos ctx->malloced -= ctx->basic_table_size *
519 1.1 christos sizeof(unsigned char *);
520 1.1 christos }
521 1.1 christos ctx->basic_table = table;
522 1.1 christos ctx->basic_table_size = table_size;
523 1.1 christos }
524 1.1 christos
525 1.1 christos tmp = (ctx->memalloc)(ctx->arg, NUM_BASIC_BLOCKS * ctx->mem_target);
526 1.1 christos if (tmp == NULL) {
527 1.1 christos ctx->memalloc_failures++;
528 1.1 christos return (ISC_FALSE);
529 1.1 christos }
530 1.1 christos ctx->total += increment;
531 1.1 christos ctx->basic_table[ctx->basic_table_count] = tmp;
532 1.1 christos ctx->basic_table_count++;
533 1.1 christos ctx->malloced += NUM_BASIC_BLOCKS * ctx->mem_target;
534 1.1 christos if (ctx->malloced > ctx->maxmalloced)
535 1.1 christos ctx->maxmalloced = ctx->malloced;
536 1.1 christos
537 1.1 christos curr = tmp;
538 1.1 christos next = curr + ctx->mem_target;
539 1.1 christos for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
540 1.1 christos ((element *)curr)->next = (element *)next;
541 1.1 christos curr = next;
542 1.1 christos next += ctx->mem_target;
543 1.1 christos }
544 1.1 christos /*
545 1.1 christos * curr is now pointing at the last block in the
546 1.1 christos * array.
547 1.1 christos */
548 1.1 christos ((element *)curr)->next = NULL;
549 1.1 christos first = tmp;
550 1.1 christos last = first + NUM_BASIC_BLOCKS * ctx->mem_target - 1;
551 1.1 christos if (first < ctx->lowest || ctx->lowest == NULL)
552 1.1 christos ctx->lowest = first;
553 1.1 christos if (last > ctx->highest)
554 1.1 christos ctx->highest = last;
555 1.1 christos ctx->basic_blocks = tmp;
556 1.1 christos
557 1.1 christos return (ISC_TRUE);
558 1.1 christos }
559 1.1 christos
560 1.1 christos static inline isc_boolean_t
561 1.1 christos more_frags(isc__mem_t *ctx, size_t new_size) {
562 1.1 christos int i, frags;
563 1.1 christos size_t total_size;
564 1.1 christos void *tmp;
565 1.1 christos unsigned char *curr, *next;
566 1.1 christos
567 1.1 christos /*!
568 1.1 christos * Try to get more fragments by chopping up a basic block.
569 1.1 christos */
570 1.1 christos
571 1.1 christos if (ctx->basic_blocks == NULL) {
572 1.1 christos if (!more_basic_blocks(ctx)) {
573 1.1 christos /*
574 1.1 christos * We can't get more memory from the OS, or we've
575 1.1 christos * hit the quota for this context.
576 1.1 christos */
577 1.1 christos /*
578 1.1 christos * XXXRTH "At quota" notification here.
579 1.1 christos */
580 1.1 christos return (ISC_FALSE);
581 1.1 christos }
582 1.1 christos }
583 1.1 christos
584 1.1 christos total_size = ctx->mem_target;
585 1.1 christos tmp = ctx->basic_blocks;
586 1.1 christos ctx->basic_blocks = ctx->basic_blocks->next;
587 1.1 christos frags = (int)(total_size / new_size);
588 1.1 christos ctx->stats[new_size].blocks++;
589 1.1 christos ctx->stats[new_size].freefrags += frags;
590 1.1 christos /*
591 1.1 christos * Set up a linked-list of blocks of size
592 1.1 christos * "new_size".
593 1.1 christos */
594 1.1 christos curr = tmp;
595 1.1 christos next = curr + new_size;
596 1.1 christos total_size -= new_size;
597 1.1 christos for (i = 0; i < (frags - 1); i++) {
598 1.1 christos ((element *)curr)->next = (element *)next;
599 1.1 christos curr = next;
600 1.1 christos next += new_size;
601 1.1 christos total_size -= new_size;
602 1.1 christos }
603 1.1 christos /*
604 1.1 christos * Add the remaining fragment of the basic block to a free list.
605 1.1 christos */
606 1.1 christos total_size = rmsize(total_size);
607 1.1 christos if (total_size > 0U) {
608 1.1 christos ((element *)next)->next = ctx->freelists[total_size];
609 1.1 christos ctx->freelists[total_size] = (element *)next;
610 1.1 christos ctx->stats[total_size].freefrags++;
611 1.1 christos }
612 1.1 christos /*
613 1.1 christos * curr is now pointing at the last block in the
614 1.1 christos * array.
615 1.1 christos */
616 1.1 christos ((element *)curr)->next = NULL;
617 1.1 christos ctx->freelists[new_size] = tmp;
618 1.1 christos
619 1.1 christos return (ISC_TRUE);
620 1.1 christos }
621 1.1 christos
622 1.1 christos static inline void *
623 1.1 christos mem_getunlocked(isc__mem_t *ctx, size_t size) {
624 1.1 christos size_t new_size = quantize(size);
625 1.1 christos void *ret;
626 1.1 christos
627 1.1 christos if (new_size >= ctx->max_size) {
628 1.1 christos /*
629 1.1 christos * memget() was called on something beyond our upper limit.
630 1.1 christos */
631 1.1 christos if (ctx->quota != 0U && ctx->total + size > ctx->quota) {
632 1.1 christos ret = NULL;
633 1.1 christos goto done;
634 1.1 christos }
635 1.1 christos ret = (ctx->memalloc)(ctx->arg, size);
636 1.1 christos if (ret == NULL) {
637 1.1 christos ctx->memalloc_failures++;
638 1.1 christos goto done;
639 1.1 christos }
640 1.1 christos ctx->total += size;
641 1.1 christos ctx->inuse += size;
642 1.1 christos ctx->stats[ctx->max_size].gets++;
643 1.1 christos ctx->stats[ctx->max_size].totalgets++;
644 1.1 christos ctx->malloced += size;
645 1.1 christos if (ctx->malloced > ctx->maxmalloced)
646 1.1 christos ctx->maxmalloced = ctx->malloced;
647 1.1 christos /*
648 1.1 christos * If we don't set new_size to size, then the
649 1.1 christos * ISC_MEMFLAG_FILL code might write over bytes we don't
650 1.1 christos * own.
651 1.1 christos */
652 1.1 christos new_size = size;
653 1.1 christos goto done;
654 1.1 christos }
655 1.1 christos /*
656 1.1 christos * If there are no blocks in the free list for this size, get a chunk
657 1.1 christos * of memory and then break it up into "new_size"-sized blocks, adding
658 1.1 christos * them to the free list.
659 1.1 christos */
660 1.1 christos if (ctx->freelists[new_size] == NULL && !more_frags(ctx, new_size))
661 1.1 christos return (NULL);
662 1.1 christos
663 1.1 christos /*
664 1.1 christos * The free list uses the "rounded-up" size "new_size".
665 1.1 christos */
666 1.1 christos
667 1.1 christos ret = ctx->freelists[new_size];
668 1.1 christos ctx->freelists[new_size] = ctx->freelists[new_size]->next;
669 1.1 christos
670 1.1 christos
671 1.1 christos /*
672 1.1 christos * The stats[] uses the _actual_ "size" requested by the
673 1.1 christos * caller, with the caveat (in the code above) that "size" >= the
674 1.1 christos * max. size (max_size) ends up getting recorded as a call to
675 1.1 christos * max_size.
676 1.1 christos */
677 1.1 christos ctx->stats[size].gets++;
678 1.1 christos ctx->stats[size].totalgets++;
679 1.1 christos ctx->stats[new_size].freefrags--;
680 1.1 christos ctx->inuse += new_size;
681 1.1 christos
682 1.1 christos done:
683 1.1 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0) &&
684 1.1 christos ISC_LIKELY(ret != NULL))
685 1.1 christos memset(ret, 0xbe, new_size); /* Mnemonic for "beef". */
686 1.1 christos
687 1.1 christos return (ret);
688 1.1 christos }
689 1.1 christos
690 1.1 christos #if ISC_MEM_CHECKOVERRUN
691 1.1 christos static inline void
692 1.1 christos check_overrun(void *mem, size_t size, size_t new_size) {
693 1.1 christos unsigned char *cp;
694 1.1 christos
695 1.1 christos cp = (unsigned char *)mem;
696 1.1 christos cp += size;
697 1.1 christos while (size < new_size) {
698 1.1 christos INSIST(*cp == 0xbe);
699 1.1 christos cp++;
700 1.1 christos size++;
701 1.1 christos }
702 1.1 christos }
703 1.1 christos #endif
704 1.1 christos
705 1.1 christos /* coverity[+free : arg-1] */
706 1.1 christos static inline void
707 1.1 christos mem_putunlocked(isc__mem_t *ctx, void *mem, size_t size) {
708 1.1 christos size_t new_size = quantize(size);
709 1.1 christos
710 1.1 christos if (new_size >= ctx->max_size) {
711 1.1 christos /*
712 1.1 christos * memput() called on something beyond our upper limit.
713 1.1 christos */
714 1.1 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0))
715 1.1 christos memset(mem, 0xde, size); /* Mnemonic for "dead". */
716 1.1 christos
717 1.1 christos (ctx->memfree)(ctx->arg, mem);
718 1.1 christos INSIST(ctx->stats[ctx->max_size].gets != 0U);
719 1.1 christos ctx->stats[ctx->max_size].gets--;
720 1.1 christos INSIST(size <= ctx->inuse);
721 1.1 christos ctx->inuse -= size;
722 1.1 christos ctx->malloced -= size;
723 1.1 christos return;
724 1.1 christos }
725 1.1 christos
726 1.1 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
727 1.1 christos #if ISC_MEM_CHECKOVERRUN
728 1.1 christos check_overrun(mem, size, new_size);
729 1.1 christos #endif
730 1.1 christos memset(mem, 0xde, new_size); /* Mnemonic for "dead". */
731 1.1 christos }
732 1.1 christos
733 1.1 christos /*
734 1.1 christos * The free list uses the "rounded-up" size "new_size".
735 1.1 christos */
736 1.1 christos ((element *)mem)->next = ctx->freelists[new_size];
737 1.1 christos ctx->freelists[new_size] = (element *)mem;
738 1.1 christos
739 1.1 christos /*
740 1.1 christos * The stats[] uses the _actual_ "size" requested by the
741 1.1 christos * caller, with the caveat (in the code above) that "size" >= the
742 1.1 christos * max. size (max_size) ends up getting recorded as a call to
743 1.1 christos * max_size.
744 1.1 christos */
745 1.1 christos INSIST(ctx->stats[size].gets != 0U);
746 1.1 christos ctx->stats[size].gets--;
747 1.1 christos ctx->stats[new_size].freefrags++;
748 1.1 christos ctx->inuse -= new_size;
749 1.1 christos }
750 1.1 christos
751 1.1 christos /*!
752 1.1 christos * Perform a malloc, doing memory filling and overrun detection as necessary.
753 1.1 christos */
754 1.1 christos static inline void *
755 1.1 christos mem_get(isc__mem_t *ctx, size_t size) {
756 1.1 christos char *ret;
757 1.1 christos
758 1.1 christos #if ISC_MEM_CHECKOVERRUN
759 1.1 christos size += 1;
760 1.1 christos #endif
761 1.1 christos ret = (ctx->memalloc)(ctx->arg, size);
762 1.1 christos if (ret == NULL)
763 1.1 christos ctx->memalloc_failures++;
764 1.1 christos
765 1.1 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0)) {
766 1.1 christos if (ISC_LIKELY(ret != NULL))
767 1.1 christos memset(ret, 0xbe, size); /* Mnemonic for "beef". */
768 1.1 christos }
769 1.1 christos #if ISC_MEM_CHECKOVERRUN
770 1.1 christos else {
771 1.1 christos if (ISC_LIKELY(ret != NULL))
772 1.1 christos ret[size-1] = 0xbe;
773 1.1 christos }
774 1.1 christos #endif
775 1.1 christos
776 1.1 christos return (ret);
777 1.1 christos }
778 1.1 christos
779 1.1 christos /*!
780 1.1 christos * Perform a free, doing memory filling and overrun detection as necessary.
781 1.1 christos */
782 1.1 christos /* coverity[+free : arg-1] */
783 1.1 christos static inline void
784 1.1 christos mem_put(isc__mem_t *ctx, void *mem, size_t size) {
785 1.1 christos #if ISC_MEM_CHECKOVERRUN
786 1.1 christos INSIST(((unsigned char *)mem)[size] == 0xbe);
787 1.1 christos size += 1;
788 1.1 christos #endif
789 1.1 christos if (ISC_UNLIKELY((ctx->flags & ISC_MEMFLAG_FILL) != 0))
790 1.1 christos memset(mem, 0xde, size); /* Mnemonic for "dead". */
791 1.1 christos (ctx->memfree)(ctx->arg, mem);
792 1.1 christos }
793 1.1 christos
794 1.1 christos /*!
795 1.1 christos * Update internal counters after a memory get.
796 1.1 christos */
797 1.1 christos static inline void
798 1.1 christos mem_getstats(isc__mem_t *ctx, size_t size) {
799 1.1 christos ctx->total += size;
800 1.1 christos ctx->inuse += size;
801 1.1 christos
802 1.1 christos if (size > ctx->max_size) {
803 1.1 christos ctx->stats[ctx->max_size].gets++;
804 1.1 christos ctx->stats[ctx->max_size].totalgets++;
805 1.1 christos } else {
806 1.1 christos ctx->stats[size].gets++;
807 1.1 christos ctx->stats[size].totalgets++;
808 1.1 christos }
809 1.1 christos
810 1.1 christos #if ISC_MEM_CHECKOVERRUN
811 1.1 christos size += 1;
812 1.1 christos #endif
813 1.1 christos ctx->malloced += size;
814 1.1 christos if (ctx->malloced > ctx->maxmalloced)
815 1.1 christos ctx->maxmalloced = ctx->malloced;
816 1.1 christos }
817 1.1 christos
818 1.1 christos /*!
819 1.1 christos * Update internal counters after a memory put.
820 1.1 christos */
821 1.1 christos static inline void
822 1.1 christos mem_putstats(isc__mem_t *ctx, void *ptr, size_t size) {
823 1.1 christos UNUSED(ptr);
824 1.1 christos
825 1.1 christos INSIST(ctx->inuse >= size);
826 1.1 christos ctx->inuse -= size;
827 1.1 christos
828 1.1 christos if (size > ctx->max_size) {
829 1.1 christos INSIST(ctx->stats[ctx->max_size].gets > 0U);
830 1.1 christos ctx->stats[ctx->max_size].gets--;
831 1.1 christos } else {
832 1.1 christos INSIST(ctx->stats[size].gets > 0U);
833 1.1 christos ctx->stats[size].gets--;
834 1.1 christos }
835 1.1 christos #if ISC_MEM_CHECKOVERRUN
836 1.1 christos size += 1;
837 1.1 christos #endif
838 1.1 christos ctx->malloced -= size;
839 1.1 christos }
840 1.1 christos
841 1.1 christos /*
842 1.1 christos * Private.
843 1.1 christos */
844 1.1 christos
845 1.1 christos static void *
846 1.1 christos default_memalloc(void *arg, size_t size) {
847 1.1 christos UNUSED(arg);
848 1.1 christos if (size == 0U)
849 1.1 christos size = 1;
850 1.1 christos return (malloc(size));
851 1.1 christos }
852 1.1 christos
853 1.1 christos static void
854 1.1 christos default_memfree(void *arg, void *ptr) {
855 1.1 christos UNUSED(arg);
856 1.1 christos free(ptr);
857 1.1 christos }
858 1.1 christos
859 1.1 christos static void
860 1.1 christos initialize_action(void) {
861 1.1 christos RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
862 1.1 christos RUNTIME_CHECK(isc_mutex_init(&contextslock) == ISC_R_SUCCESS);
863 1.1 christos ISC_LIST_INIT(contexts);
864 1.1 christos totallost = 0;
865 1.1 christos }
866 1.1 christos
867 1.1 christos /*
868 1.1 christos * Public.
869 1.1 christos */
870 1.1 christos
871 1.1 christos isc_result_t
872 1.1 christos isc_mem_createx(size_t init_max_size, size_t target_size,
873 1.1 christos isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
874 1.1 christos isc_mem_t **ctxp)
875 1.1 christos {
876 1.1 christos return (isc_mem_createx2(init_max_size, target_size, memalloc, memfree,
877 1.1 christos arg, ctxp, isc_mem_defaultflags));
878 1.1 christos
879 1.1 christos }
880 1.1 christos
881 1.1 christos isc_result_t
882 1.1 christos isc_mem_createx2(size_t init_max_size, size_t target_size,
883 1.1 christos isc_memalloc_t memalloc, isc_memfree_t memfree, void *arg,
884 1.1 christos isc_mem_t **ctxp, unsigned int flags)
885 1.1 christos {
886 1.1 christos isc__mem_t *ctx;
887 1.1 christos isc_result_t result;
888 1.1 christos
889 1.1 christos REQUIRE(ctxp != NULL && *ctxp == NULL);
890 1.1 christos REQUIRE(memalloc != NULL);
891 1.1 christos REQUIRE(memfree != NULL);
892 1.1 christos
893 1.1 christos INSIST((ALIGNMENT_SIZE & (ALIGNMENT_SIZE - 1)) == 0);
894 1.1 christos
895 1.1 christos RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
896 1.1 christos
897 1.1 christos ctx = (memalloc)(arg, sizeof(*ctx));
898 1.1 christos if (ctx == NULL)
899 1.1 christos return (ISC_R_NOMEMORY);
900 1.1 christos
901 1.1 christos if ((flags & ISC_MEMFLAG_NOLOCK) == 0) {
902 1.1 christos result = isc_mutex_init(&ctx->lock);
903 1.1 christos if (result != ISC_R_SUCCESS) {
904 1.1 christos (memfree)(arg, ctx);
905 1.1 christos return (result);
906 1.1 christos }
907 1.1 christos }
908 1.1 christos
909 1.1 christos if (init_max_size == 0U)
910 1.1 christos ctx->max_size = DEF_MAX_SIZE;
911 1.1 christos else
912 1.1 christos ctx->max_size = init_max_size;
913 1.1 christos ctx->flags = flags;
914 1.1 christos ctx->references = 1;
915 1.1 christos memset(ctx->name, 0, sizeof(ctx->name));
916 1.1 christos ctx->tag = NULL;
917 1.1 christos ctx->quota = 0;
918 1.1 christos ctx->total = 0;
919 1.1 christos ctx->inuse = 0;
920 1.1 christos ctx->maxinuse = 0;
921 1.1 christos ctx->malloced = sizeof(*ctx);
922 1.1 christos ctx->maxmalloced = sizeof(*ctx);
923 1.1 christos ctx->hi_water = 0;
924 1.1 christos ctx->lo_water = 0;
925 1.1 christos ctx->hi_called = ISC_FALSE;
926 1.1 christos ctx->is_overmem = ISC_FALSE;
927 1.1 christos ctx->water = NULL;
928 1.1 christos ctx->water_arg = NULL;
929 1.1 christos ctx->common.impmagic = MEM_MAGIC;
930 1.1 christos ctx->common.magic = ISCAPI_MCTX_MAGIC;
931 1.1 christos ctx->common.methods = (isc_memmethods_t *)&memmethods;
932 1.1 christos isc_ondestroy_init(&ctx->ondestroy);
933 1.1 christos ctx->memalloc = memalloc;
934 1.1 christos ctx->memfree = memfree;
935 1.1 christos ctx->arg = arg;
936 1.1 christos ctx->stats = NULL;
937 1.1 christos ctx->checkfree = ISC_TRUE;
938 1.1 christos #if ISC_MEM_TRACKLINES
939 1.1 christos ctx->debuglist = NULL;
940 1.1 christos ctx->debuglistcnt = 0;
941 1.1 christos #endif
942 1.1 christos ISC_LIST_INIT(ctx->pools);
943 1.1 christos ctx->poolcnt = 0;
944 1.1 christos ctx->freelists = NULL;
945 1.1 christos ctx->basic_blocks = NULL;
946 1.1 christos ctx->basic_table = NULL;
947 1.1 christos ctx->basic_table_count = 0;
948 1.1 christos ctx->basic_table_size = 0;
949 1.1 christos ctx->lowest = NULL;
950 1.1 christos ctx->highest = NULL;
951 1.1 christos
952 1.1 christos ctx->stats = (memalloc)(arg,
953 1.1 christos (ctx->max_size+1) * sizeof(struct stats));
954 1.1 christos if (ctx->stats == NULL) {
955 1.1 christos result = ISC_R_NOMEMORY;
956 1.1 christos goto error;
957 1.1 christos }
958 1.1 christos memset(ctx->stats, 0, (ctx->max_size + 1) * sizeof(struct stats));
959 1.1 christos ctx->malloced += (ctx->max_size+1) * sizeof(struct stats);
960 1.1 christos ctx->maxmalloced += (ctx->max_size+1) * sizeof(struct stats);
961 1.1 christos
962 1.1 christos if ((flags & ISC_MEMFLAG_INTERNAL) != 0) {
963 1.1 christos if (target_size == 0U)
964 1.1 christos ctx->mem_target = DEF_MEM_TARGET;
965 1.1 christos else
966 1.1 christos ctx->mem_target = target_size;
967 1.1 christos ctx->freelists = (memalloc)(arg, ctx->max_size *
968 1.1 christos sizeof(element *));
969 1.1 christos if (ctx->freelists == NULL) {
970 1.1 christos result = ISC_R_NOMEMORY;
971 1.1 christos goto error;
972 1.1 christos }
973 1.1 christos memset(ctx->freelists, 0,
974 1.1 christos ctx->max_size * sizeof(element *));
975 1.1 christos ctx->malloced += ctx->max_size * sizeof(element *);
976 1.1 christos ctx->maxmalloced += ctx->max_size * sizeof(element *);
977 1.1 christos }
978 1.1 christos
979 1.1 christos #if ISC_MEM_TRACKLINES
980 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGRECORD) != 0)) {
981 1.1 christos unsigned int i;
982 1.1 christos
983 1.1 christos ctx->debuglist = (memalloc)(arg, (DEBUG_TABLE_COUNT *
984 1.1 christos sizeof(debuglist_t)));
985 1.1 christos if (ctx->debuglist == NULL) {
986 1.1 christos result = ISC_R_NOMEMORY;
987 1.1 christos goto error;
988 1.1 christos }
989 1.1 christos for (i = 0; i < DEBUG_TABLE_COUNT; i++)
990 1.1 christos ISC_LIST_INIT(ctx->debuglist[i]);
991 1.1 christos ctx->malloced += DEBUG_TABLE_COUNT * sizeof(debuglist_t);
992 1.1 christos ctx->maxmalloced += DEBUG_TABLE_COUNT * sizeof(debuglist_t);
993 1.1 christos }
994 1.1 christos #endif
995 1.1 christos
996 1.1 christos ctx->memalloc_failures = 0;
997 1.1 christos
998 1.1 christos LOCK(&contextslock);
999 1.1 christos ISC_LIST_INITANDAPPEND(contexts, ctx, link);
1000 1.1 christos UNLOCK(&contextslock);
1001 1.1 christos
1002 1.1 christos *ctxp = (isc_mem_t *)ctx;
1003 1.1 christos return (ISC_R_SUCCESS);
1004 1.1 christos
1005 1.1 christos error:
1006 1.1 christos if (ctx != NULL) {
1007 1.1 christos if (ctx->stats != NULL)
1008 1.1 christos (memfree)(arg, ctx->stats);
1009 1.1 christos if (ctx->freelists != NULL)
1010 1.1 christos (memfree)(arg, ctx->freelists);
1011 1.1 christos #if ISC_MEM_TRACKLINES
1012 1.1 christos if (ctx->debuglist != NULL)
1013 1.1 christos (ctx->memfree)(ctx->arg, ctx->debuglist);
1014 1.1 christos #endif /* ISC_MEM_TRACKLINES */
1015 1.1 christos if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
1016 1.1 christos DESTROYLOCK(&ctx->lock);
1017 1.1 christos (memfree)(arg, ctx);
1018 1.1 christos }
1019 1.1 christos
1020 1.1 christos return (result);
1021 1.1 christos }
1022 1.1 christos
1023 1.1 christos static void
1024 1.1 christos destroy(isc__mem_t *ctx) {
1025 1.1 christos unsigned int i;
1026 1.1 christos isc_ondestroy_t ondest;
1027 1.1 christos
1028 1.1 christos LOCK(&contextslock);
1029 1.1 christos ISC_LIST_UNLINK(contexts, ctx, link);
1030 1.1 christos totallost += ctx->inuse;
1031 1.1 christos UNLOCK(&contextslock);
1032 1.1 christos
1033 1.1 christos ctx->common.impmagic = 0;
1034 1.1 christos ctx->common.magic = 0;
1035 1.1 christos
1036 1.1 christos INSIST(ISC_LIST_EMPTY(ctx->pools));
1037 1.1 christos
1038 1.1 christos #if ISC_MEM_TRACKLINES
1039 1.1 christos if (ISC_UNLIKELY(ctx->debuglist != NULL)) {
1040 1.1 christos debuglink_t *dl;
1041 1.1 christos for (i = 0; i < DEBUG_TABLE_COUNT; i++)
1042 1.1 christos for (dl = ISC_LIST_HEAD(ctx->debuglist[i]);
1043 1.1 christos dl != NULL;
1044 1.1 christos dl = ISC_LIST_HEAD(ctx->debuglist[i])) {
1045 1.1 christos if (ctx->checkfree && dl->ptr != NULL)
1046 1.1 christos print_active(ctx, stderr);
1047 1.1 christos INSIST (!ctx->checkfree || dl->ptr == NULL);
1048 1.1 christos
1049 1.1 christos ISC_LIST_UNLINK(ctx->debuglist[i],
1050 1.1 christos dl, link);
1051 1.1 christos free(dl);
1052 1.1 christos ctx->malloced -= sizeof(*dl);
1053 1.1 christos }
1054 1.1 christos
1055 1.1 christos (ctx->memfree)(ctx->arg, ctx->debuglist);
1056 1.1 christos ctx->malloced -= DEBUG_TABLE_COUNT * sizeof(debuglist_t);
1057 1.1 christos }
1058 1.1 christos #endif
1059 1.1 christos INSIST(ctx->references == 0);
1060 1.1 christos
1061 1.1 christos if (ctx->checkfree) {
1062 1.1 christos for (i = 0; i <= ctx->max_size; i++) {
1063 1.1 christos if (ctx->stats[i].gets != 0U) {
1064 1.1 christos fprintf(stderr,
1065 1.1 christos "Failing assertion due to probable "
1066 1.1 christos "leaked memory in context %p (\"%s\") "
1067 1.1 christos "(stats[%u].gets == %lu).\n",
1068 1.1 christos ctx, ctx->name, i, ctx->stats[i].gets);
1069 1.1 christos #if ISC_MEM_TRACKLINES
1070 1.1 christos print_active(ctx, stderr);
1071 1.1 christos #endif
1072 1.1 christos INSIST(ctx->stats[i].gets == 0U);
1073 1.1 christos }
1074 1.1 christos }
1075 1.1 christos }
1076 1.1 christos
1077 1.1 christos (ctx->memfree)(ctx->arg, ctx->stats);
1078 1.1 christos ctx->malloced -= (ctx->max_size+1) * sizeof(struct stats);
1079 1.1 christos
1080 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1081 1.1 christos for (i = 0; i < ctx->basic_table_count; i++) {
1082 1.1 christos (ctx->memfree)(ctx->arg, ctx->basic_table[i]);
1083 1.1 christos ctx->malloced -= NUM_BASIC_BLOCKS * ctx->mem_target;
1084 1.1 christos }
1085 1.1 christos (ctx->memfree)(ctx->arg, ctx->freelists);
1086 1.1 christos ctx->malloced -= ctx->max_size * sizeof(element *);
1087 1.1 christos if (ctx->basic_table != NULL) {
1088 1.1 christos (ctx->memfree)(ctx->arg, ctx->basic_table);
1089 1.1 christos ctx->malloced -= ctx->basic_table_size *
1090 1.1 christos sizeof(unsigned char *);
1091 1.1 christos }
1092 1.1 christos }
1093 1.1 christos
1094 1.1 christos ondest = ctx->ondestroy;
1095 1.1 christos
1096 1.1 christos if ((ctx->flags & ISC_MEMFLAG_NOLOCK) == 0)
1097 1.1 christos DESTROYLOCK(&ctx->lock);
1098 1.1 christos ctx->malloced -= sizeof(*ctx);
1099 1.1 christos if (ctx->checkfree)
1100 1.1 christos INSIST(ctx->malloced == 0);
1101 1.1 christos (ctx->memfree)(ctx->arg, ctx);
1102 1.1 christos
1103 1.1 christos isc_ondestroy_notify(&ondest, ctx);
1104 1.1 christos }
1105 1.1 christos
1106 1.1 christos void
1107 1.1 christos isc__mem_attach(isc_mem_t *source0, isc_mem_t **targetp) {
1108 1.1 christos isc__mem_t *source = (isc__mem_t *)source0;
1109 1.1 christos
1110 1.1 christos REQUIRE(VALID_CONTEXT(source));
1111 1.1 christos REQUIRE(targetp != NULL && *targetp == NULL);
1112 1.1 christos
1113 1.1 christos MCTXLOCK(source, &source->lock);
1114 1.1 christos source->references++;
1115 1.1 christos MCTXUNLOCK(source, &source->lock);
1116 1.1 christos
1117 1.1 christos *targetp = (isc_mem_t *)source;
1118 1.1 christos }
1119 1.1 christos
1120 1.1 christos void
1121 1.1 christos isc__mem_detach(isc_mem_t **ctxp) {
1122 1.1 christos isc__mem_t *ctx;
1123 1.1 christos isc_boolean_t want_destroy = ISC_FALSE;
1124 1.1 christos
1125 1.1 christos REQUIRE(ctxp != NULL);
1126 1.1 christos ctx = (isc__mem_t *)*ctxp;
1127 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1128 1.1 christos
1129 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1130 1.1 christos INSIST(ctx->references > 0);
1131 1.1 christos ctx->references--;
1132 1.1 christos if (ctx->references == 0)
1133 1.1 christos want_destroy = ISC_TRUE;
1134 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1135 1.1 christos
1136 1.1 christos if (want_destroy)
1137 1.1 christos destroy(ctx);
1138 1.1 christos
1139 1.1 christos *ctxp = NULL;
1140 1.1 christos }
1141 1.1 christos
1142 1.1 christos /*
1143 1.1 christos * isc_mem_putanddetach() is the equivalent of:
1144 1.1 christos *
1145 1.1 christos * mctx = NULL;
1146 1.1 christos * isc_mem_attach(ptr->mctx, &mctx);
1147 1.1 christos * isc_mem_detach(&ptr->mctx);
1148 1.1 christos * isc_mem_put(mctx, ptr, sizeof(*ptr);
1149 1.1 christos * isc_mem_detach(&mctx);
1150 1.1 christos */
1151 1.1 christos
1152 1.1 christos void
1153 1.1 christos isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) {
1154 1.1 christos isc__mem_t *ctx;
1155 1.1 christos isc_boolean_t want_destroy = ISC_FALSE;
1156 1.1 christos size_info *si;
1157 1.1 christos size_t oldsize;
1158 1.1 christos
1159 1.1 christos REQUIRE(ctxp != NULL);
1160 1.1 christos ctx = (isc__mem_t *)*ctxp;
1161 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1162 1.1 christos REQUIRE(ptr != NULL);
1163 1.1 christos
1164 1.1 christos /*
1165 1.1 christos * Must be before mem_putunlocked() as ctxp is usually within
1166 1.1 christos * [ptr..ptr+size).
1167 1.1 christos */
1168 1.1 christos *ctxp = NULL;
1169 1.1 christos
1170 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging &
1171 1.1 christos (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0))
1172 1.1 christos {
1173 1.1 christos if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
1174 1.1 christos si = &(((size_info *)ptr)[-1]);
1175 1.1 christos oldsize = si->u.size - ALIGNMENT_SIZE;
1176 1.1 christos if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
1177 1.1 christos oldsize -= ALIGNMENT_SIZE;
1178 1.1 christos INSIST(oldsize == size);
1179 1.1 christos }
1180 1.1 christos isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
1181 1.1 christos
1182 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1183 1.1 christos ctx->references--;
1184 1.1 christos if (ctx->references == 0)
1185 1.1 christos want_destroy = ISC_TRUE;
1186 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1187 1.1 christos if (want_destroy)
1188 1.1 christos destroy(ctx);
1189 1.1 christos
1190 1.1 christos return;
1191 1.1 christos }
1192 1.1 christos
1193 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1194 1.1 christos
1195 1.1 christos DELETE_TRACE(ctx, ptr, size, file, line);
1196 1.1 christos
1197 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1198 1.1 christos mem_putunlocked(ctx, ptr, size);
1199 1.1 christos } else {
1200 1.1 christos mem_putstats(ctx, ptr, size);
1201 1.1 christos mem_put(ctx, ptr, size);
1202 1.1 christos }
1203 1.1 christos
1204 1.1 christos INSIST(ctx->references > 0);
1205 1.1 christos ctx->references--;
1206 1.1 christos if (ctx->references == 0)
1207 1.1 christos want_destroy = ISC_TRUE;
1208 1.1 christos
1209 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1210 1.1 christos
1211 1.1 christos if (want_destroy)
1212 1.1 christos destroy(ctx);
1213 1.1 christos }
1214 1.1 christos
1215 1.1 christos void
1216 1.1 christos isc__mem_destroy(isc_mem_t **ctxp) {
1217 1.1 christos isc__mem_t *ctx;
1218 1.1 christos
1219 1.1 christos /*
1220 1.1 christos * This routine provides legacy support for callers who use mctxs
1221 1.1 christos * without attaching/detaching.
1222 1.1 christos */
1223 1.1 christos
1224 1.1 christos REQUIRE(ctxp != NULL);
1225 1.1 christos ctx = (isc__mem_t *)*ctxp;
1226 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1227 1.1 christos
1228 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1229 1.1 christos #if ISC_MEM_TRACKLINES
1230 1.1 christos if (ctx->references != 1)
1231 1.1 christos print_active(ctx, stderr);
1232 1.1 christos #endif
1233 1.1 christos REQUIRE(ctx->references == 1);
1234 1.1 christos ctx->references--;
1235 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1236 1.1 christos
1237 1.1 christos destroy(ctx);
1238 1.1 christos
1239 1.1 christos *ctxp = NULL;
1240 1.1 christos }
1241 1.1 christos
1242 1.1 christos isc_result_t
1243 1.1 christos isc_mem_ondestroy(isc_mem_t *ctx0, isc_task_t *task, isc_event_t **event) {
1244 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1245 1.1 christos isc_result_t res;
1246 1.1 christos
1247 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1248 1.1 christos res = isc_ondestroy_register(&ctx->ondestroy, task, event);
1249 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1250 1.1 christos
1251 1.1 christos return (res);
1252 1.1 christos }
1253 1.1 christos
1254 1.1 christos void *
1255 1.1 christos isc___mem_get(isc_mem_t *ctx0, size_t size FLARG) {
1256 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1257 1.1 christos void *ptr;
1258 1.1 christos isc_boolean_t call_water = ISC_FALSE;
1259 1.1 christos
1260 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1261 1.1 christos
1262 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging &
1263 1.1 christos (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0))
1264 1.1 christos return (isc__mem_allocate(ctx0, size FLARG_PASS));
1265 1.1 christos
1266 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1267 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1268 1.1 christos ptr = mem_getunlocked(ctx, size);
1269 1.1 christos } else {
1270 1.1 christos ptr = mem_get(ctx, size);
1271 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1272 1.1 christos if (ptr != NULL)
1273 1.1 christos mem_getstats(ctx, size);
1274 1.1 christos }
1275 1.1 christos
1276 1.1 christos ADD_TRACE(ctx, ptr, size, file, line);
1277 1.1 christos
1278 1.1 christos if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water) {
1279 1.1 christos ctx->is_overmem = ISC_TRUE;
1280 1.1 christos if (!ctx->hi_called)
1281 1.1 christos call_water = ISC_TRUE;
1282 1.1 christos }
1283 1.1 christos if (ctx->inuse > ctx->maxinuse) {
1284 1.1 christos ctx->maxinuse = ctx->inuse;
1285 1.1 christos if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1286 1.1 christos (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0)
1287 1.1 christos fprintf(stderr, "maxinuse = %lu\n",
1288 1.1 christos (unsigned long)ctx->inuse);
1289 1.1 christos }
1290 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1291 1.1 christos
1292 1.1 christos if (call_water && (ctx->water != NULL))
1293 1.1 christos (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
1294 1.1 christos
1295 1.1 christos return (ptr);
1296 1.1 christos }
1297 1.1 christos
1298 1.1 christos void
1299 1.1 christos isc___mem_put(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
1300 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1301 1.1 christos isc_boolean_t call_water = ISC_FALSE;
1302 1.1 christos size_info *si;
1303 1.1 christos size_t oldsize;
1304 1.1 christos
1305 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1306 1.1 christos REQUIRE(ptr != NULL);
1307 1.1 christos
1308 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging &
1309 1.1 christos (ISC_MEM_DEBUGSIZE|ISC_MEM_DEBUGCTX)) != 0))
1310 1.1 christos {
1311 1.1 christos if ((isc_mem_debugging & ISC_MEM_DEBUGSIZE) != 0) {
1312 1.1 christos si = &(((size_info *)ptr)[-1]);
1313 1.1 christos oldsize = si->u.size - ALIGNMENT_SIZE;
1314 1.1 christos if ((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)
1315 1.1 christos oldsize -= ALIGNMENT_SIZE;
1316 1.1 christos INSIST(oldsize == size);
1317 1.1 christos }
1318 1.1 christos isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS);
1319 1.1 christos return;
1320 1.1 christos }
1321 1.1 christos
1322 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1323 1.1 christos
1324 1.1 christos DELETE_TRACE(ctx, ptr, size, file, line);
1325 1.1 christos
1326 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1327 1.1 christos mem_putunlocked(ctx, ptr, size);
1328 1.1 christos } else {
1329 1.1 christos mem_putstats(ctx, ptr, size);
1330 1.1 christos mem_put(ctx, ptr, size);
1331 1.1 christos }
1332 1.1 christos
1333 1.1 christos /*
1334 1.1 christos * The check against ctx->lo_water == 0 is for the condition
1335 1.1 christos * when the context was pushed over hi_water but then had
1336 1.1 christos * isc_mem_setwater() called with 0 for hi_water and lo_water.
1337 1.1 christos */
1338 1.1 christos if ((ctx->inuse < ctx->lo_water) || (ctx->lo_water == 0U)) {
1339 1.1 christos ctx->is_overmem = ISC_FALSE;
1340 1.1 christos if (ctx->hi_called)
1341 1.1 christos call_water = ISC_TRUE;
1342 1.1 christos }
1343 1.1 christos
1344 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1345 1.1 christos
1346 1.1 christos if (call_water && (ctx->water != NULL))
1347 1.1 christos (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1348 1.1 christos }
1349 1.1 christos
1350 1.1 christos void
1351 1.1 christos isc__mem_waterack(isc_mem_t *ctx0, int flag) {
1352 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1353 1.1 christos
1354 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1355 1.1 christos
1356 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1357 1.1 christos if (flag == ISC_MEM_LOWATER)
1358 1.1 christos ctx->hi_called = ISC_FALSE;
1359 1.1 christos else if (flag == ISC_MEM_HIWATER)
1360 1.1 christos ctx->hi_called = ISC_TRUE;
1361 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1362 1.1 christos }
1363 1.1 christos
1364 1.1 christos #if ISC_MEM_TRACKLINES
1365 1.1 christos static void
1366 1.1 christos print_active(isc__mem_t *mctx, FILE *out) {
1367 1.1 christos if (mctx->debuglist != NULL) {
1368 1.1 christos debuglink_t *dl;
1369 1.1 christos unsigned int i;
1370 1.1 christos const char *format;
1371 1.1 christos isc_boolean_t found;
1372 1.1 christos
1373 1.1 christos fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1374 1.1 christos ISC_MSG_DUMPALLOC,
1375 1.1 christos "Dump of all outstanding "
1376 1.1 christos "memory allocations:\n"));
1377 1.1 christos found = ISC_FALSE;
1378 1.1 christos format = isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1379 1.1 christos ISC_MSG_PTRFILELINE,
1380 1.1 christos "\tptr %p size %u file %s line %u\n");
1381 1.1 christos for (i = 0; i < DEBUG_TABLE_COUNT; i++) {
1382 1.1 christos dl = ISC_LIST_HEAD(mctx->debuglist[i]);
1383 1.1 christos
1384 1.1 christos if (dl != NULL)
1385 1.1 christos found = ISC_TRUE;
1386 1.1 christos
1387 1.1 christos while (dl != NULL) {
1388 1.1 christos if (dl->ptr != NULL)
1389 1.1 christos fprintf(out, format,
1390 1.1 christos dl->ptr, dl->size,
1391 1.1 christos dl->file, dl->line);
1392 1.1 christos dl = ISC_LIST_NEXT(dl, link);
1393 1.1 christos }
1394 1.1 christos }
1395 1.1 christos
1396 1.1 christos if (!found)
1397 1.1 christos fputs(isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1398 1.1 christos ISC_MSG_NONE, "\tNone.\n"), out);
1399 1.1 christos }
1400 1.1 christos }
1401 1.1 christos #endif
1402 1.1 christos
1403 1.1 christos /*
1404 1.1 christos * Print the stats[] on the stream "out" with suitable formatting.
1405 1.1 christos */
1406 1.1 christos void
1407 1.1 christos isc_mem_stats(isc_mem_t *ctx0, FILE *out) {
1408 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1409 1.1 christos size_t i;
1410 1.1 christos const struct stats *s;
1411 1.1 christos const isc__mempool_t *pool;
1412 1.1 christos
1413 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1414 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1415 1.1 christos
1416 1.1 christos for (i = 0; i <= ctx->max_size; i++) {
1417 1.1 christos s = &ctx->stats[i];
1418 1.1 christos
1419 1.1 christos if (s->totalgets == 0U && s->gets == 0U)
1420 1.1 christos continue;
1421 1.1 christos fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
1422 1.1 christos (i == ctx->max_size) ? ">=" : " ",
1423 1.1 christos (unsigned long) i, s->totalgets, s->gets);
1424 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0 &&
1425 1.1 christos (s->blocks != 0U || s->freefrags != 0U))
1426 1.1 christos fprintf(out, " (%lu bl, %lu ff)",
1427 1.1 christos s->blocks, s->freefrags);
1428 1.1 christos fputc('\n', out);
1429 1.1 christos }
1430 1.1 christos
1431 1.1 christos /*
1432 1.1 christos * Note that since a pool can be locked now, these stats might be
1433 1.1 christos * somewhat off if the pool is in active use at the time the stats
1434 1.1 christos * are dumped. The link fields are protected by the isc_mem_t's
1435 1.1 christos * lock, however, so walking this list and extracting integers from
1436 1.1 christos * stats fields is always safe.
1437 1.1 christos */
1438 1.1 christos pool = ISC_LIST_HEAD(ctx->pools);
1439 1.1 christos if (pool != NULL) {
1440 1.1 christos fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1441 1.1 christos ISC_MSG_POOLSTATS,
1442 1.1 christos "[Pool statistics]\n"));
1443 1.1 christos fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n",
1444 1.1 christos isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1445 1.1 christos ISC_MSG_POOLNAME, "name"),
1446 1.1 christos isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1447 1.1 christos ISC_MSG_POOLSIZE, "size"),
1448 1.1 christos isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1449 1.1 christos ISC_MSG_POOLMAXALLOC, "maxalloc"),
1450 1.1 christos isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1451 1.1 christos ISC_MSG_POOLALLOCATED, "allocated"),
1452 1.1 christos isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1453 1.1 christos ISC_MSG_POOLFREECOUNT, "freecount"),
1454 1.1 christos isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1455 1.1 christos ISC_MSG_POOLFREEMAX, "freemax"),
1456 1.1 christos isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1457 1.1 christos ISC_MSG_POOLFILLCOUNT, "fillcount"),
1458 1.1 christos isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM,
1459 1.1 christos ISC_MSG_POOLGETS, "gets"),
1460 1.1 christos "L");
1461 1.1 christos }
1462 1.1 christos while (pool != NULL) {
1463 1.1 christos fprintf(out, "%15s %10lu %10u %10u %10u %10u %10u %10u %s\n",
1464 1.1 christos #if ISC_MEMPOOL_NAMES
1465 1.1 christos pool->name,
1466 1.1 christos #else
1467 1.1 christos "(not tracked)",
1468 1.1 christos #endif
1469 1.1 christos (unsigned long) pool->size, pool->maxalloc,
1470 1.1 christos pool->allocated, pool->freecount, pool->freemax,
1471 1.1 christos pool->fillcount, pool->gets,
1472 1.1 christos (pool->lock == NULL ? "N" : "Y"));
1473 1.1 christos pool = ISC_LIST_NEXT(pool, link);
1474 1.1 christos }
1475 1.1 christos
1476 1.1 christos #if ISC_MEM_TRACKLINES
1477 1.1 christos print_active(ctx, out);
1478 1.1 christos #endif
1479 1.1 christos
1480 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1481 1.1 christos }
1482 1.1 christos
1483 1.1 christos /*
1484 1.1 christos * Replacements for malloc() and free() -- they implicitly remember the
1485 1.1 christos * size of the object allocated (with some additional overhead).
1486 1.1 christos */
1487 1.1 christos
1488 1.1 christos static void *
1489 1.1 christos mem_allocateunlocked(isc_mem_t *ctx0, size_t size) {
1490 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1491 1.1 christos size_info *si;
1492 1.1 christos
1493 1.1 christos size += ALIGNMENT_SIZE;
1494 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0))
1495 1.1 christos size += ALIGNMENT_SIZE;
1496 1.1 christos
1497 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0)
1498 1.1 christos si = mem_getunlocked(ctx, size);
1499 1.1 christos else
1500 1.1 christos si = mem_get(ctx, size);
1501 1.1 christos
1502 1.1 christos if (si == NULL)
1503 1.1 christos return (NULL);
1504 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
1505 1.1 christos si->u.ctx = ctx;
1506 1.1 christos si++;
1507 1.1 christos }
1508 1.1 christos si->u.size = size;
1509 1.1 christos return (&si[1]);
1510 1.1 christos }
1511 1.1 christos
1512 1.1 christos void *
1513 1.1 christos isc___mem_allocate(isc_mem_t *ctx0, size_t size FLARG) {
1514 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1515 1.1 christos size_info *si;
1516 1.1 christos isc_boolean_t call_water = ISC_FALSE;
1517 1.1 christos
1518 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1519 1.1 christos
1520 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1521 1.1 christos si = mem_allocateunlocked((isc_mem_t *)ctx, size);
1522 1.1 christos if (((ctx->flags & ISC_MEMFLAG_INTERNAL) == 0) && (si != NULL))
1523 1.1 christos mem_getstats(ctx, si[-1].u.size);
1524 1.1 christos
1525 1.1 christos ADD_TRACE(ctx, si, si[-1].u.size, file, line);
1526 1.1 christos if (ctx->hi_water != 0U && ctx->inuse > ctx->hi_water &&
1527 1.1 christos !ctx->is_overmem) {
1528 1.1 christos ctx->is_overmem = ISC_TRUE;
1529 1.1 christos }
1530 1.1 christos
1531 1.1 christos if (ctx->hi_water != 0U && !ctx->hi_called &&
1532 1.1 christos ctx->inuse > ctx->hi_water) {
1533 1.1 christos ctx->hi_called = ISC_TRUE;
1534 1.1 christos call_water = ISC_TRUE;
1535 1.1 christos }
1536 1.1 christos if (ctx->inuse > ctx->maxinuse) {
1537 1.1 christos ctx->maxinuse = ctx->inuse;
1538 1.1 christos if (ISC_UNLIKELY(ctx->hi_water != 0U &&
1539 1.1 christos ctx->inuse > ctx->hi_water &&
1540 1.1 christos (isc_mem_debugging & ISC_MEM_DEBUGUSAGE) != 0))
1541 1.1 christos fprintf(stderr, "maxinuse = %lu\n",
1542 1.1 christos (unsigned long)ctx->inuse);
1543 1.1 christos }
1544 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1545 1.1 christos
1546 1.1 christos if (call_water)
1547 1.1 christos (ctx->water)(ctx->water_arg, ISC_MEM_HIWATER);
1548 1.1 christos
1549 1.1 christos return (si);
1550 1.1 christos }
1551 1.1 christos
1552 1.1 christos void *
1553 1.1 christos isc___mem_reallocate(isc_mem_t *ctx0, void *ptr, size_t size FLARG) {
1554 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1555 1.1 christos void *new_ptr = NULL;
1556 1.1 christos size_t oldsize, copysize;
1557 1.1 christos
1558 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1559 1.1 christos
1560 1.1 christos /*
1561 1.1 christos * This function emulates the realloc(3) standard library function:
1562 1.1 christos * - if size > 0, allocate new memory; and if ptr is non NULL, copy
1563 1.1 christos * as much of the old contents to the new buffer and free the old one.
1564 1.1 christos * Note that when allocation fails the original pointer is intact;
1565 1.1 christos * the caller must free it.
1566 1.1 christos * - if size is 0 and ptr is non NULL, simply free the given ptr.
1567 1.1 christos * - this function returns:
1568 1.1 christos * pointer to the newly allocated memory, or
1569 1.1 christos * NULL if allocation fails or doesn't happen.
1570 1.1 christos */
1571 1.1 christos if (size > 0U) {
1572 1.1 christos new_ptr = isc__mem_allocate(ctx0, size FLARG_PASS);
1573 1.1 christos if (new_ptr != NULL && ptr != NULL) {
1574 1.1 christos oldsize = (((size_info *)ptr)[-1]).u.size;
1575 1.1 christos INSIST(oldsize >= ALIGNMENT_SIZE);
1576 1.1 christos oldsize -= ALIGNMENT_SIZE;
1577 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging &
1578 1.1 christos ISC_MEM_DEBUGCTX) != 0))
1579 1.1 christos {
1580 1.1 christos INSIST(oldsize >= ALIGNMENT_SIZE);
1581 1.1 christos oldsize -= ALIGNMENT_SIZE;
1582 1.1 christos }
1583 1.1 christos copysize = (oldsize > size) ? size : oldsize;
1584 1.1 christos memmove(new_ptr, ptr, copysize);
1585 1.1 christos isc__mem_free(ctx0, ptr FLARG_PASS);
1586 1.1 christos }
1587 1.1 christos } else if (ptr != NULL)
1588 1.1 christos isc__mem_free(ctx0, ptr FLARG_PASS);
1589 1.1 christos
1590 1.1 christos return (new_ptr);
1591 1.1 christos }
1592 1.1 christos
1593 1.1 christos void
1594 1.1 christos isc___mem_free(isc_mem_t *ctx0, void *ptr FLARG) {
1595 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1596 1.1 christos size_info *si;
1597 1.1 christos size_t size;
1598 1.1 christos isc_boolean_t call_water= ISC_FALSE;
1599 1.1 christos
1600 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1601 1.1 christos REQUIRE(ptr != NULL);
1602 1.1 christos
1603 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & ISC_MEM_DEBUGCTX) != 0)) {
1604 1.1 christos si = &(((size_info *)ptr)[-2]);
1605 1.1 christos REQUIRE(si->u.ctx == ctx);
1606 1.1 christos size = si[1].u.size;
1607 1.1 christos } else {
1608 1.1 christos si = &(((size_info *)ptr)[-1]);
1609 1.1 christos size = si->u.size;
1610 1.1 christos }
1611 1.1 christos
1612 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1613 1.1 christos
1614 1.1 christos DELETE_TRACE(ctx, ptr, size, file, line);
1615 1.1 christos
1616 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1617 1.1 christos mem_putunlocked(ctx, si, size);
1618 1.1 christos } else {
1619 1.1 christos mem_putstats(ctx, si, size);
1620 1.1 christos mem_put(ctx, si, size);
1621 1.1 christos }
1622 1.1 christos
1623 1.1 christos /*
1624 1.1 christos * The check against ctx->lo_water == 0 is for the condition
1625 1.1 christos * when the context was pushed over hi_water but then had
1626 1.1 christos * isc_mem_setwater() called with 0 for hi_water and lo_water.
1627 1.1 christos */
1628 1.1 christos if (ctx->is_overmem &&
1629 1.1 christos (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1630 1.1 christos ctx->is_overmem = ISC_FALSE;
1631 1.1 christos }
1632 1.1 christos
1633 1.1 christos if (ctx->hi_called &&
1634 1.1 christos (ctx->inuse < ctx->lo_water || ctx->lo_water == 0U)) {
1635 1.1 christos ctx->hi_called = ISC_FALSE;
1636 1.1 christos
1637 1.1 christos if (ctx->water != NULL)
1638 1.1 christos call_water = ISC_TRUE;
1639 1.1 christos }
1640 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1641 1.1 christos
1642 1.1 christos if (call_water)
1643 1.1 christos (ctx->water)(ctx->water_arg, ISC_MEM_LOWATER);
1644 1.1 christos }
1645 1.1 christos
1646 1.1 christos
1647 1.1 christos /*
1648 1.1 christos * Other useful things.
1649 1.1 christos */
1650 1.1 christos
1651 1.1 christos char *
1652 1.1 christos isc___mem_strdup(isc_mem_t *mctx0, const char *s FLARG) {
1653 1.1 christos isc__mem_t *mctx = (isc__mem_t *)mctx0;
1654 1.1 christos size_t len;
1655 1.1 christos char *ns;
1656 1.1 christos
1657 1.1 christos REQUIRE(VALID_CONTEXT(mctx));
1658 1.1 christos REQUIRE(s != NULL);
1659 1.1 christos
1660 1.1 christos len = strlen(s) + 1;
1661 1.1 christos
1662 1.1 christos ns = isc__mem_allocate((isc_mem_t *)mctx, len FLARG_PASS);
1663 1.1 christos
1664 1.1 christos if (ns != NULL)
1665 1.1 christos strlcpy(ns, s, len);
1666 1.1 christos
1667 1.1 christos return (ns);
1668 1.1 christos }
1669 1.1 christos
1670 1.1 christos void
1671 1.1 christos isc__mem_setdestroycheck(isc_mem_t *ctx0, isc_boolean_t flag) {
1672 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1673 1.1 christos
1674 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1675 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1676 1.1 christos
1677 1.1 christos ctx->checkfree = flag;
1678 1.1 christos
1679 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1680 1.1 christos }
1681 1.1 christos
1682 1.1 christos /*
1683 1.1 christos * Quotas
1684 1.1 christos */
1685 1.1 christos
1686 1.1 christos void
1687 1.1 christos isc_mem_setquota(isc_mem_t *ctx0, size_t quota) {
1688 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1689 1.1 christos
1690 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1691 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1692 1.1 christos
1693 1.1 christos ctx->quota = quota;
1694 1.1 christos
1695 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1696 1.1 christos }
1697 1.1 christos
1698 1.1 christos size_t
1699 1.1 christos isc_mem_getquota(isc_mem_t *ctx0) {
1700 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1701 1.1 christos size_t quota;
1702 1.1 christos
1703 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1704 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1705 1.1 christos
1706 1.1 christos quota = ctx->quota;
1707 1.1 christos
1708 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1709 1.1 christos
1710 1.1 christos return (quota);
1711 1.1 christos }
1712 1.1 christos
1713 1.1 christos size_t
1714 1.1 christos isc__mem_inuse(isc_mem_t *ctx0) {
1715 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1716 1.1 christos size_t inuse;
1717 1.1 christos
1718 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1719 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1720 1.1 christos
1721 1.1 christos inuse = ctx->inuse;
1722 1.1 christos
1723 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1724 1.1 christos
1725 1.1 christos return (inuse);
1726 1.1 christos }
1727 1.1 christos
1728 1.1 christos size_t
1729 1.1 christos isc__mem_maxinuse(isc_mem_t *ctx0) {
1730 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1731 1.1 christos size_t maxinuse;
1732 1.1 christos
1733 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1734 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1735 1.1 christos
1736 1.1 christos maxinuse = ctx->maxinuse;
1737 1.1 christos
1738 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1739 1.1 christos
1740 1.1 christos return (maxinuse);
1741 1.1 christos }
1742 1.1 christos
1743 1.1 christos size_t
1744 1.1 christos isc__mem_total(isc_mem_t *ctx0) {
1745 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1746 1.1 christos size_t total;
1747 1.1 christos
1748 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1749 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1750 1.1 christos
1751 1.1 christos total = ctx->total;
1752 1.1 christos
1753 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1754 1.1 christos
1755 1.1 christos return (total);
1756 1.1 christos }
1757 1.1 christos
1758 1.1 christos void
1759 1.1 christos isc__mem_setwater(isc_mem_t *ctx0, isc_mem_water_t water, void *water_arg,
1760 1.1 christos size_t hiwater, size_t lowater)
1761 1.1 christos {
1762 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1763 1.1 christos isc_boolean_t callwater = ISC_FALSE;
1764 1.1 christos isc_mem_water_t oldwater;
1765 1.1 christos void *oldwater_arg;
1766 1.1 christos
1767 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1768 1.1 christos REQUIRE(hiwater >= lowater);
1769 1.1 christos
1770 1.1 christos MCTXLOCK(ctx, &ctx->lock);
1771 1.1 christos oldwater = ctx->water;
1772 1.1 christos oldwater_arg = ctx->water_arg;
1773 1.1 christos if (water == NULL) {
1774 1.1 christos callwater = ctx->hi_called;
1775 1.1 christos ctx->water = NULL;
1776 1.1 christos ctx->water_arg = NULL;
1777 1.1 christos ctx->hi_water = 0;
1778 1.1 christos ctx->lo_water = 0;
1779 1.1 christos } else {
1780 1.1 christos if (ctx->hi_called &&
1781 1.1 christos (ctx->water != water || ctx->water_arg != water_arg ||
1782 1.1 christos ctx->inuse < lowater || lowater == 0U))
1783 1.1 christos callwater = ISC_TRUE;
1784 1.1 christos ctx->water = water;
1785 1.1 christos ctx->water_arg = water_arg;
1786 1.1 christos ctx->hi_water = hiwater;
1787 1.1 christos ctx->lo_water = lowater;
1788 1.1 christos }
1789 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
1790 1.1 christos
1791 1.1 christos if (callwater && oldwater != NULL)
1792 1.1 christos (oldwater)(oldwater_arg, ISC_MEM_LOWATER);
1793 1.1 christos }
1794 1.1 christos
1795 1.1 christos isc_boolean_t
1796 1.1 christos isc__mem_isovermem(isc_mem_t *ctx0) {
1797 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1798 1.1 christos
1799 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1800 1.1 christos
1801 1.1 christos /*
1802 1.1 christos * We don't bother to lock the context because 100% accuracy isn't
1803 1.1 christos * necessary (and even if we locked the context the returned value
1804 1.1 christos * could be different from the actual state when it's used anyway)
1805 1.1 christos */
1806 1.1 christos return (ctx->is_overmem);
1807 1.1 christos }
1808 1.1 christos
1809 1.1 christos void
1810 1.1 christos isc_mem_setname(isc_mem_t *ctx0, const char *name, void *tag) {
1811 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1812 1.1 christos
1813 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1814 1.1 christos
1815 1.1 christos LOCK(&ctx->lock);
1816 1.1 christos strlcpy(ctx->name, name, sizeof(ctx->name));
1817 1.1 christos ctx->tag = tag;
1818 1.1 christos UNLOCK(&ctx->lock);
1819 1.1 christos }
1820 1.1 christos
1821 1.1 christos const char *
1822 1.1 christos isc_mem_getname(isc_mem_t *ctx0) {
1823 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1824 1.1 christos
1825 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1826 1.1 christos
1827 1.1 christos if (ctx->name[0] == 0)
1828 1.1 christos return ("");
1829 1.1 christos
1830 1.1 christos return (ctx->name);
1831 1.1 christos }
1832 1.1 christos
1833 1.1 christos void *
1834 1.1 christos isc_mem_gettag(isc_mem_t *ctx0) {
1835 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
1836 1.1 christos
1837 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
1838 1.1 christos
1839 1.1 christos return (ctx->tag);
1840 1.1 christos }
1841 1.1 christos
1842 1.1 christos /*
1843 1.1 christos * Memory pool stuff
1844 1.1 christos */
1845 1.1 christos
1846 1.1 christos isc_result_t
1847 1.1 christos isc__mempool_create(isc_mem_t *mctx0, size_t size, isc_mempool_t **mpctxp) {
1848 1.1 christos isc__mem_t *mctx = (isc__mem_t *)mctx0;
1849 1.1 christos isc__mempool_t *mpctx;
1850 1.1 christos
1851 1.1 christos REQUIRE(VALID_CONTEXT(mctx));
1852 1.1 christos REQUIRE(size > 0U);
1853 1.1 christos REQUIRE(mpctxp != NULL && *mpctxp == NULL);
1854 1.1 christos
1855 1.1 christos /*
1856 1.1 christos * Allocate space for this pool, initialize values, and if all works
1857 1.1 christos * well, attach to the memory context.
1858 1.1 christos */
1859 1.1 christos mpctx = isc_mem_get((isc_mem_t *)mctx, sizeof(isc__mempool_t));
1860 1.1 christos if (mpctx == NULL)
1861 1.1 christos return (ISC_R_NOMEMORY);
1862 1.1 christos
1863 1.1 christos mpctx->common.methods = (isc_mempoolmethods_t *)&mempoolmethods;
1864 1.1 christos mpctx->common.impmagic = MEMPOOL_MAGIC;
1865 1.1 christos mpctx->common.magic = ISCAPI_MPOOL_MAGIC;
1866 1.1 christos mpctx->lock = NULL;
1867 1.1 christos mpctx->mctx = mctx;
1868 1.1 christos mpctx->size = size;
1869 1.1 christos mpctx->maxalloc = UINT_MAX;
1870 1.1 christos mpctx->allocated = 0;
1871 1.1 christos mpctx->freecount = 0;
1872 1.1 christos mpctx->freemax = 1;
1873 1.1 christos mpctx->fillcount = 1;
1874 1.1 christos mpctx->gets = 0;
1875 1.1 christos #if ISC_MEMPOOL_NAMES
1876 1.1 christos mpctx->name[0] = 0;
1877 1.1 christos #endif
1878 1.1 christos mpctx->items = NULL;
1879 1.1 christos
1880 1.1 christos *mpctxp = (isc_mempool_t *)mpctx;
1881 1.1 christos
1882 1.1 christos MCTXLOCK(mctx, &mctx->lock);
1883 1.1 christos ISC_LIST_INITANDAPPEND(mctx->pools, mpctx, link);
1884 1.1 christos mctx->poolcnt++;
1885 1.1 christos MCTXUNLOCK(mctx, &mctx->lock);
1886 1.1 christos
1887 1.1 christos return (ISC_R_SUCCESS);
1888 1.1 christos }
1889 1.1 christos
1890 1.1 christos void
1891 1.1 christos isc__mempool_setname(isc_mempool_t *mpctx0, const char *name) {
1892 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1893 1.1 christos
1894 1.1 christos REQUIRE(name != NULL);
1895 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
1896 1.1 christos
1897 1.1 christos #if ISC_MEMPOOL_NAMES
1898 1.1 christos if (mpctx->lock != NULL)
1899 1.1 christos LOCK(mpctx->lock);
1900 1.1 christos
1901 1.1 christos strlcpy(mpctx->name, name, sizeof(mpctx->name));
1902 1.1 christos
1903 1.1 christos if (mpctx->lock != NULL)
1904 1.1 christos UNLOCK(mpctx->lock);
1905 1.1 christos #else
1906 1.1 christos UNUSED(mpctx);
1907 1.1 christos UNUSED(name);
1908 1.1 christos #endif
1909 1.1 christos }
1910 1.1 christos
1911 1.1 christos void
1912 1.1 christos isc__mempool_destroy(isc_mempool_t **mpctxp) {
1913 1.1 christos isc__mempool_t *mpctx;
1914 1.1 christos isc__mem_t *mctx;
1915 1.1 christos isc_mutex_t *lock;
1916 1.1 christos element *item;
1917 1.1 christos
1918 1.1 christos REQUIRE(mpctxp != NULL);
1919 1.1 christos mpctx = (isc__mempool_t *)*mpctxp;
1920 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
1921 1.1 christos #if ISC_MEMPOOL_NAMES
1922 1.1 christos if (mpctx->allocated > 0)
1923 1.1 christos UNEXPECTED_ERROR(__FILE__, __LINE__,
1924 1.1 christos "isc__mempool_destroy(): mempool %s "
1925 1.1 christos "leaked memory",
1926 1.1 christos mpctx->name);
1927 1.1 christos #endif
1928 1.1 christos REQUIRE(mpctx->allocated == 0);
1929 1.1 christos
1930 1.1 christos mctx = mpctx->mctx;
1931 1.1 christos
1932 1.1 christos lock = mpctx->lock;
1933 1.1 christos
1934 1.1 christos if (lock != NULL)
1935 1.1 christos LOCK(lock);
1936 1.1 christos
1937 1.1 christos /*
1938 1.1 christos * Return any items on the free list
1939 1.1 christos */
1940 1.1 christos MCTXLOCK(mctx, &mctx->lock);
1941 1.1 christos while (mpctx->items != NULL) {
1942 1.1 christos INSIST(mpctx->freecount > 0);
1943 1.1 christos mpctx->freecount--;
1944 1.1 christos item = mpctx->items;
1945 1.1 christos mpctx->items = item->next;
1946 1.1 christos
1947 1.1 christos if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
1948 1.1 christos mem_putunlocked(mctx, item, mpctx->size);
1949 1.1 christos } else {
1950 1.1 christos mem_putstats(mctx, item, mpctx->size);
1951 1.1 christos mem_put(mctx, item, mpctx->size);
1952 1.1 christos }
1953 1.1 christos }
1954 1.1 christos MCTXUNLOCK(mctx, &mctx->lock);
1955 1.1 christos
1956 1.1 christos /*
1957 1.1 christos * Remove our linked list entry from the memory context.
1958 1.1 christos */
1959 1.1 christos MCTXLOCK(mctx, &mctx->lock);
1960 1.1 christos ISC_LIST_UNLINK(mctx->pools, mpctx, link);
1961 1.1 christos mctx->poolcnt--;
1962 1.1 christos MCTXUNLOCK(mctx, &mctx->lock);
1963 1.1 christos
1964 1.1 christos mpctx->common.impmagic = 0;
1965 1.1 christos mpctx->common.magic = 0;
1966 1.1 christos
1967 1.1 christos isc_mem_put((isc_mem_t *)mpctx->mctx, mpctx, sizeof(isc__mempool_t));
1968 1.1 christos
1969 1.1 christos if (lock != NULL)
1970 1.1 christos UNLOCK(lock);
1971 1.1 christos
1972 1.1 christos *mpctxp = NULL;
1973 1.1 christos }
1974 1.1 christos
1975 1.1 christos void
1976 1.1 christos isc__mempool_associatelock(isc_mempool_t *mpctx0, isc_mutex_t *lock) {
1977 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1978 1.1 christos
1979 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
1980 1.1 christos REQUIRE(mpctx->lock == NULL);
1981 1.1 christos REQUIRE(lock != NULL);
1982 1.1 christos
1983 1.1 christos mpctx->lock = lock;
1984 1.1 christos }
1985 1.1 christos
1986 1.1 christos void *
1987 1.1 christos isc___mempool_get(isc_mempool_t *mpctx0 FLARG) {
1988 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
1989 1.1 christos element *item;
1990 1.1 christos isc__mem_t *mctx;
1991 1.1 christos unsigned int i;
1992 1.1 christos
1993 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
1994 1.1 christos
1995 1.1 christos mctx = mpctx->mctx;
1996 1.1 christos
1997 1.1 christos if (mpctx->lock != NULL)
1998 1.1 christos LOCK(mpctx->lock);
1999 1.1 christos
2000 1.1 christos /*
2001 1.1 christos * Don't let the caller go over quota
2002 1.1 christos */
2003 1.1 christos if (ISC_UNLIKELY(mpctx->allocated >= mpctx->maxalloc)) {
2004 1.1 christos item = NULL;
2005 1.1 christos goto out;
2006 1.1 christos }
2007 1.1 christos
2008 1.1 christos if (ISC_UNLIKELY(mpctx->items == NULL)) {
2009 1.1 christos /*
2010 1.1 christos * We need to dip into the well. Lock the memory context
2011 1.1 christos * here and fill up our free list.
2012 1.1 christos */
2013 1.1 christos MCTXLOCK(mctx, &mctx->lock);
2014 1.1 christos for (i = 0; i < mpctx->fillcount; i++) {
2015 1.1 christos if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2016 1.1 christos item = mem_getunlocked(mctx, mpctx->size);
2017 1.1 christos } else {
2018 1.1 christos item = mem_get(mctx, mpctx->size);
2019 1.1 christos if (item != NULL)
2020 1.1 christos mem_getstats(mctx, mpctx->size);
2021 1.1 christos }
2022 1.1 christos if (ISC_UNLIKELY(item == NULL))
2023 1.1 christos break;
2024 1.1 christos item->next = mpctx->items;
2025 1.1 christos mpctx->items = item;
2026 1.1 christos mpctx->freecount++;
2027 1.1 christos }
2028 1.1 christos MCTXUNLOCK(mctx, &mctx->lock);
2029 1.1 christos }
2030 1.1 christos
2031 1.1 christos /*
2032 1.1 christos * If we didn't get any items, return NULL.
2033 1.1 christos */
2034 1.1 christos item = mpctx->items;
2035 1.1 christos if (ISC_UNLIKELY(item == NULL))
2036 1.1 christos goto out;
2037 1.1 christos
2038 1.1 christos mpctx->items = item->next;
2039 1.1 christos INSIST(mpctx->freecount > 0);
2040 1.1 christos mpctx->freecount--;
2041 1.1 christos mpctx->gets++;
2042 1.1 christos mpctx->allocated++;
2043 1.1 christos
2044 1.1 christos out:
2045 1.1 christos if (mpctx->lock != NULL)
2046 1.1 christos UNLOCK(mpctx->lock);
2047 1.1 christos
2048 1.1 christos #if ISC_MEM_TRACKLINES
2049 1.1 christos if (ISC_UNLIKELY(((isc_mem_debugging & TRACE_OR_RECORD) != 0) &&
2050 1.1 christos item != NULL))
2051 1.1 christos {
2052 1.1 christos MCTXLOCK(mctx, &mctx->lock);
2053 1.1 christos ADD_TRACE(mctx, item, mpctx->size, file, line);
2054 1.1 christos MCTXUNLOCK(mctx, &mctx->lock);
2055 1.1 christos }
2056 1.1 christos #endif /* ISC_MEM_TRACKLINES */
2057 1.1 christos
2058 1.1 christos return (item);
2059 1.1 christos }
2060 1.1 christos
2061 1.1 christos /* coverity[+free : arg-1] */
2062 1.1 christos void
2063 1.1 christos isc___mempool_put(isc_mempool_t *mpctx0, void *mem FLARG) {
2064 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2065 1.1 christos isc__mem_t *mctx;
2066 1.1 christos element *item;
2067 1.1 christos
2068 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2069 1.1 christos REQUIRE(mem != NULL);
2070 1.1 christos
2071 1.1 christos mctx = mpctx->mctx;
2072 1.1 christos
2073 1.1 christos if (mpctx->lock != NULL)
2074 1.1 christos LOCK(mpctx->lock);
2075 1.1 christos
2076 1.1 christos INSIST(mpctx->allocated > 0);
2077 1.1 christos mpctx->allocated--;
2078 1.1 christos
2079 1.1 christos #if ISC_MEM_TRACKLINES
2080 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0)) {
2081 1.1 christos MCTXLOCK(mctx, &mctx->lock);
2082 1.1 christos DELETE_TRACE(mctx, mem, mpctx->size, file, line);
2083 1.1 christos MCTXUNLOCK(mctx, &mctx->lock);
2084 1.1 christos }
2085 1.1 christos #endif /* ISC_MEM_TRACKLINES */
2086 1.1 christos
2087 1.1 christos /*
2088 1.1 christos * If our free list is full, return this to the mctx directly.
2089 1.1 christos */
2090 1.1 christos if (mpctx->freecount >= mpctx->freemax) {
2091 1.1 christos MCTXLOCK(mctx, &mctx->lock);
2092 1.1 christos if ((mctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2093 1.1 christos mem_putunlocked(mctx, mem, mpctx->size);
2094 1.1 christos } else {
2095 1.1 christos mem_putstats(mctx, mem, mpctx->size);
2096 1.1 christos mem_put(mctx, mem, mpctx->size);
2097 1.1 christos }
2098 1.1 christos MCTXUNLOCK(mctx, &mctx->lock);
2099 1.1 christos if (mpctx->lock != NULL)
2100 1.1 christos UNLOCK(mpctx->lock);
2101 1.1 christos return;
2102 1.1 christos }
2103 1.1 christos
2104 1.1 christos /*
2105 1.1 christos * Otherwise, attach it to our free list and bump the counter.
2106 1.1 christos */
2107 1.1 christos mpctx->freecount++;
2108 1.1 christos item = (element *)mem;
2109 1.1 christos item->next = mpctx->items;
2110 1.1 christos mpctx->items = item;
2111 1.1 christos
2112 1.1 christos if (mpctx->lock != NULL)
2113 1.1 christos UNLOCK(mpctx->lock);
2114 1.1 christos }
2115 1.1 christos
2116 1.1 christos /*
2117 1.1 christos * Quotas
2118 1.1 christos */
2119 1.1 christos
2120 1.1 christos void
2121 1.1 christos isc__mempool_setfreemax(isc_mempool_t *mpctx0, unsigned int limit) {
2122 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2123 1.1 christos
2124 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2125 1.1 christos
2126 1.1 christos if (mpctx->lock != NULL)
2127 1.1 christos LOCK(mpctx->lock);
2128 1.1 christos
2129 1.1 christos mpctx->freemax = limit;
2130 1.1 christos
2131 1.1 christos if (mpctx->lock != NULL)
2132 1.1 christos UNLOCK(mpctx->lock);
2133 1.1 christos }
2134 1.1 christos
2135 1.1 christos unsigned int
2136 1.1 christos isc_mempool_getfreemax(isc_mempool_t *mpctx0) {
2137 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2138 1.1 christos unsigned int freemax;
2139 1.1 christos
2140 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2141 1.1 christos
2142 1.1 christos if (mpctx->lock != NULL)
2143 1.1 christos LOCK(mpctx->lock);
2144 1.1 christos
2145 1.1 christos freemax = mpctx->freemax;
2146 1.1 christos
2147 1.1 christos if (mpctx->lock != NULL)
2148 1.1 christos UNLOCK(mpctx->lock);
2149 1.1 christos
2150 1.1 christos return (freemax);
2151 1.1 christos }
2152 1.1 christos
2153 1.1 christos unsigned int
2154 1.1 christos isc_mempool_getfreecount(isc_mempool_t *mpctx0) {
2155 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2156 1.1 christos unsigned int freecount;
2157 1.1 christos
2158 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2159 1.1 christos
2160 1.1 christos if (mpctx->lock != NULL)
2161 1.1 christos LOCK(mpctx->lock);
2162 1.1 christos
2163 1.1 christos freecount = mpctx->freecount;
2164 1.1 christos
2165 1.1 christos if (mpctx->lock != NULL)
2166 1.1 christos UNLOCK(mpctx->lock);
2167 1.1 christos
2168 1.1 christos return (freecount);
2169 1.1 christos }
2170 1.1 christos
2171 1.1 christos void
2172 1.1 christos isc__mempool_setmaxalloc(isc_mempool_t *mpctx0, unsigned int limit) {
2173 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2174 1.1 christos
2175 1.1 christos REQUIRE(limit > 0);
2176 1.1 christos
2177 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2178 1.1 christos
2179 1.1 christos if (mpctx->lock != NULL)
2180 1.1 christos LOCK(mpctx->lock);
2181 1.1 christos
2182 1.1 christos mpctx->maxalloc = limit;
2183 1.1 christos
2184 1.1 christos if (mpctx->lock != NULL)
2185 1.1 christos UNLOCK(mpctx->lock);
2186 1.1 christos }
2187 1.1 christos
2188 1.1 christos unsigned int
2189 1.1 christos isc_mempool_getmaxalloc(isc_mempool_t *mpctx0) {
2190 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2191 1.1 christos unsigned int maxalloc;
2192 1.1 christos
2193 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2194 1.1 christos
2195 1.1 christos if (mpctx->lock != NULL)
2196 1.1 christos LOCK(mpctx->lock);
2197 1.1 christos
2198 1.1 christos maxalloc = mpctx->maxalloc;
2199 1.1 christos
2200 1.1 christos if (mpctx->lock != NULL)
2201 1.1 christos UNLOCK(mpctx->lock);
2202 1.1 christos
2203 1.1 christos return (maxalloc);
2204 1.1 christos }
2205 1.1 christos
2206 1.1 christos unsigned int
2207 1.1 christos isc__mempool_getallocated(isc_mempool_t *mpctx0) {
2208 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2209 1.1 christos unsigned int allocated;
2210 1.1 christos
2211 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2212 1.1 christos
2213 1.1 christos if (mpctx->lock != NULL)
2214 1.1 christos LOCK(mpctx->lock);
2215 1.1 christos
2216 1.1 christos allocated = mpctx->allocated;
2217 1.1 christos
2218 1.1 christos if (mpctx->lock != NULL)
2219 1.1 christos UNLOCK(mpctx->lock);
2220 1.1 christos
2221 1.1 christos return (allocated);
2222 1.1 christos }
2223 1.1 christos
2224 1.1 christos void
2225 1.1 christos isc__mempool_setfillcount(isc_mempool_t *mpctx0, unsigned int limit) {
2226 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2227 1.1 christos
2228 1.1 christos REQUIRE(limit > 0);
2229 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2230 1.1 christos
2231 1.1 christos if (mpctx->lock != NULL)
2232 1.1 christos LOCK(mpctx->lock);
2233 1.1 christos
2234 1.1 christos mpctx->fillcount = limit;
2235 1.1 christos
2236 1.1 christos if (mpctx->lock != NULL)
2237 1.1 christos UNLOCK(mpctx->lock);
2238 1.1 christos }
2239 1.1 christos
2240 1.1 christos unsigned int
2241 1.1 christos isc_mempool_getfillcount(isc_mempool_t *mpctx0) {
2242 1.1 christos isc__mempool_t *mpctx = (isc__mempool_t *)mpctx0;
2243 1.1 christos
2244 1.1 christos unsigned int fillcount;
2245 1.1 christos
2246 1.1 christos REQUIRE(VALID_MEMPOOL(mpctx));
2247 1.1 christos
2248 1.1 christos if (mpctx->lock != NULL)
2249 1.1 christos LOCK(mpctx->lock);
2250 1.1 christos
2251 1.1 christos fillcount = mpctx->fillcount;
2252 1.1 christos
2253 1.1 christos if (mpctx->lock != NULL)
2254 1.1 christos UNLOCK(mpctx->lock);
2255 1.1 christos
2256 1.1 christos return (fillcount);
2257 1.1 christos }
2258 1.1 christos
2259 1.1 christos isc_result_t
2260 1.1 christos isc__mem_register(void) {
2261 1.1 christos return (isc_mem_register(isc_mem_create2));
2262 1.1 christos }
2263 1.1 christos
2264 1.1 christos void
2265 1.1 christos isc__mem_printactive(isc_mem_t *ctx0, FILE *file) {
2266 1.1 christos #if ISC_MEM_TRACKLINES
2267 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
2268 1.1 christos
2269 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
2270 1.1 christos REQUIRE(file != NULL);
2271 1.1 christos
2272 1.1 christos print_active(ctx, file);
2273 1.1 christos #else
2274 1.1 christos UNUSED(ctx0);
2275 1.1 christos UNUSED(file);
2276 1.1 christos #endif
2277 1.1 christos }
2278 1.1 christos
2279 1.1 christos
2280 1.1 christos /*
2281 1.1 christos * Requires contextslock to be held by caller.
2282 1.1 christos */
2283 1.1 christos static void
2284 1.1 christos print_contexts(FILE *file) {
2285 1.1 christos isc__mem_t *ctx;
2286 1.1 christos
2287 1.1 christos for (ctx = ISC_LIST_HEAD(contexts);
2288 1.1 christos ctx != NULL;
2289 1.1 christos ctx = ISC_LIST_NEXT(ctx, link))
2290 1.1 christos {
2291 1.1 christos fprintf(file, "context: %p (%s): %u references\n",
2292 1.1 christos ctx,
2293 1.1 christos ctx->name[0] == 0 ? "<unknown>" : ctx->name,
2294 1.1 christos ctx->references);
2295 1.1 christos print_active(ctx, file);
2296 1.1 christos }
2297 1.1 christos fflush(file);
2298 1.1 christos }
2299 1.1 christos
2300 1.1 christos void
2301 1.1 christos isc_mem_printallactive(FILE *file) {
2302 1.1 christos #if !ISC_MEM_TRACKLINES
2303 1.1 christos UNUSED(file);
2304 1.1 christos #else
2305 1.1 christos RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2306 1.1 christos
2307 1.1 christos LOCK(&contextslock);
2308 1.1 christos print_contexts(file);
2309 1.1 christos UNLOCK(&contextslock);
2310 1.1 christos #endif
2311 1.1 christos }
2312 1.1 christos
2313 1.1 christos void
2314 1.1 christos isc_mem_checkdestroyed(FILE *file) {
2315 1.1 christos #if !ISC_MEM_TRACKLINES
2316 1.1 christos UNUSED(file);
2317 1.1 christos #endif
2318 1.1 christos
2319 1.1 christos RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2320 1.1 christos
2321 1.1 christos LOCK(&contextslock);
2322 1.1 christos if (!ISC_LIST_EMPTY(contexts)) {
2323 1.1 christos #if ISC_MEM_TRACKLINES
2324 1.1 christos if (ISC_UNLIKELY((isc_mem_debugging & TRACE_OR_RECORD) != 0)) {
2325 1.1 christos print_contexts(file);
2326 1.1 christos }
2327 1.1 christos #endif
2328 1.1 christos INSIST(0);
2329 1.1 christos }
2330 1.1 christos UNLOCK(&contextslock);
2331 1.1 christos }
2332 1.1 christos
2333 1.1 christos unsigned int
2334 1.1 christos isc_mem_references(isc_mem_t *ctx0) {
2335 1.1 christos isc__mem_t *ctx = (isc__mem_t *)ctx0;
2336 1.1 christos unsigned int references;
2337 1.1 christos
2338 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
2339 1.1 christos
2340 1.1 christos MCTXLOCK(ctx, &ctx->lock);
2341 1.1 christos references = ctx->references;
2342 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
2343 1.1 christos
2344 1.1 christos return (references);
2345 1.1 christos }
2346 1.1 christos
2347 1.1 christos typedef struct summarystat {
2348 1.1 christos isc_uint64_t total;
2349 1.1 christos isc_uint64_t inuse;
2350 1.1 christos isc_uint64_t malloced;
2351 1.1 christos isc_uint64_t blocksize;
2352 1.1 christos isc_uint64_t contextsize;
2353 1.1 christos } summarystat_t;
2354 1.1 christos
2355 1.1 christos #ifdef HAVE_LIBXML2
2356 1.1 christos #define TRY0(a) do { xmlrc = (a); if (xmlrc < 0) goto error; } while(0)
2357 1.1 christos static int
2358 1.1 christos xml_renderctx(isc__mem_t *ctx, summarystat_t *summary,
2359 1.1 christos xmlTextWriterPtr writer)
2360 1.1 christos {
2361 1.1 christos int xmlrc;
2362 1.1 christos
2363 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
2364 1.1 christos
2365 1.1 christos MCTXLOCK(ctx, &ctx->lock);
2366 1.1 christos
2367 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "context"));
2368 1.1 christos
2369 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "id"));
2370 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%p", ctx));
2371 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* id */
2372 1.1 christos
2373 1.1 christos if (ctx->name[0] != 0) {
2374 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
2375 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%s", ctx->name));
2376 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* name */
2377 1.1 christos }
2378 1.1 christos
2379 1.1 christos summary->contextsize += sizeof(*ctx) +
2380 1.1 christos (ctx->max_size + 1) * sizeof(struct stats) +
2381 1.1 christos ctx->max_size * sizeof(element *) +
2382 1.1 christos ctx->basic_table_count * sizeof(char *);
2383 1.1 christos #if ISC_MEM_TRACKLINES
2384 1.1 christos if (ctx->debuglist != NULL) {
2385 1.1 christos summary->contextsize +=
2386 1.1 christos DEBUG_TABLE_COUNT * sizeof(debuglist_t) +
2387 1.1 christos ctx->debuglistcnt * sizeof(debuglink_t);
2388 1.1 christos }
2389 1.1 christos #endif
2390 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "references"));
2391 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%d", ctx->references));
2392 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* references */
2393 1.1 christos
2394 1.1 christos summary->total += ctx->total;
2395 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "total"));
2396 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2397 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2398 1.1 christos (isc_uint64_t)ctx->total));
2399 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* total */
2400 1.1 christos
2401 1.1 christos summary->inuse += ctx->inuse;
2402 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "inuse"));
2403 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2404 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2405 1.1 christos (isc_uint64_t)ctx->inuse));
2406 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* inuse */
2407 1.1 christos
2408 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxinuse"));
2409 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2410 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2411 1.1 christos (isc_uint64_t)ctx->maxinuse));
2412 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* maxinuse */
2413 1.1 christos
2414 1.1 christos summary->malloced += ctx->malloced;
2415 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "malloced"));
2416 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2417 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2418 1.1 christos (isc_uint64_t)ctx->malloced));
2419 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* malloced */
2420 1.1 christos
2421 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "maxmalloced"));
2422 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2423 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2424 1.1 christos (isc_uint64_t)ctx->maxmalloced));
2425 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* maxmalloced */
2426 1.1 christos
2427 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "blocksize"));
2428 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2429 1.1 christos summary->blocksize += ctx->basic_table_count *
2430 1.1 christos NUM_BASIC_BLOCKS * ctx->mem_target;
2431 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2432 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2433 1.1 christos (isc_uint64_t)
2434 1.1 christos ctx->basic_table_count *
2435 1.1 christos NUM_BASIC_BLOCKS *
2436 1.1 christos ctx->mem_target));
2437 1.1 christos } else
2438 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%s", "-"));
2439 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* blocksize */
2440 1.1 christos
2441 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "pools"));
2442 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer, "%u", ctx->poolcnt));
2443 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* pools */
2444 1.1 christos summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
2445 1.1 christos
2446 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "hiwater"));
2447 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2448 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2449 1.1 christos (isc_uint64_t)ctx->hi_water));
2450 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* hiwater */
2451 1.1 christos
2452 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "lowater"));
2453 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2454 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2455 1.1 christos (isc_uint64_t)ctx->lo_water));
2456 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* lowater */
2457 1.1 christos
2458 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* context */
2459 1.1 christos
2460 1.1 christos error:
2461 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
2462 1.1 christos
2463 1.1 christos return (xmlrc);
2464 1.1 christos }
2465 1.1 christos
2466 1.1 christos int
2467 1.1 christos isc_mem_renderxml(xmlTextWriterPtr writer) {
2468 1.1 christos isc__mem_t *ctx;
2469 1.1 christos summarystat_t summary;
2470 1.1 christos isc_uint64_t lost;
2471 1.1 christos int xmlrc;
2472 1.1 christos
2473 1.1 christos memset(&summary, 0, sizeof(summary));
2474 1.1 christos
2475 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "contexts"));
2476 1.1 christos
2477 1.1 christos RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2478 1.1 christos
2479 1.1 christos LOCK(&contextslock);
2480 1.1 christos lost = totallost;
2481 1.1 christos for (ctx = ISC_LIST_HEAD(contexts);
2482 1.1 christos ctx != NULL;
2483 1.1 christos ctx = ISC_LIST_NEXT(ctx, link)) {
2484 1.1 christos xmlrc = xml_renderctx(ctx, &summary, writer);
2485 1.1 christos if (xmlrc < 0) {
2486 1.1 christos UNLOCK(&contextslock);
2487 1.1 christos goto error;
2488 1.1 christos }
2489 1.1 christos }
2490 1.1 christos UNLOCK(&contextslock);
2491 1.1 christos
2492 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* contexts */
2493 1.1 christos
2494 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "summary"));
2495 1.1 christos
2496 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "TotalUse"));
2497 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2498 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2499 1.1 christos summary.total));
2500 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* TotalUse */
2501 1.1 christos
2502 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "InUse"));
2503 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2504 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2505 1.1 christos summary.inuse));
2506 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* InUse */
2507 1.1 christos
2508 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "Malloced"));
2509 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2510 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2511 1.1 christos summary.malloced));
2512 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* InUse */
2513 1.1 christos
2514 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "BlockSize"));
2515 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2516 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2517 1.1 christos summary.blocksize));
2518 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* BlockSize */
2519 1.1 christos
2520 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "ContextSize"));
2521 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2522 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2523 1.1 christos summary.contextsize));
2524 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* ContextSize */
2525 1.1 christos
2526 1.1 christos TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "Lost"));
2527 1.1 christos TRY0(xmlTextWriterWriteFormatString(writer,
2528 1.1 christos "%" ISC_PRINT_QUADFORMAT "u",
2529 1.1 christos lost));
2530 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* Lost */
2531 1.1 christos
2532 1.1 christos TRY0(xmlTextWriterEndElement(writer)); /* summary */
2533 1.1 christos error:
2534 1.1 christos return (xmlrc);
2535 1.1 christos }
2536 1.1 christos
2537 1.1 christos #endif /* HAVE_LIBXML2 */
2538 1.1 christos
2539 1.1 christos #ifdef HAVE_JSON
2540 1.1 christos #define CHECKMEM(m) do { \
2541 1.1 christos if (m == NULL) { \
2542 1.1 christos result = ISC_R_NOMEMORY;\
2543 1.1 christos goto error;\
2544 1.1 christos } \
2545 1.1 christos } while(0)
2546 1.1 christos
2547 1.1 christos static isc_result_t
2548 1.1 christos json_renderctx(isc__mem_t *ctx, summarystat_t *summary, json_object *array) {
2549 1.1 christos isc_result_t result = ISC_R_FAILURE;
2550 1.1 christos json_object *ctxobj, *obj;
2551 1.1 christos char buf[1024];
2552 1.1 christos
2553 1.1 christos REQUIRE(VALID_CONTEXT(ctx));
2554 1.1 christos REQUIRE(summary != NULL);
2555 1.1 christos REQUIRE(array != NULL);
2556 1.1 christos
2557 1.1 christos MCTXLOCK(ctx, &ctx->lock);
2558 1.1 christos
2559 1.1 christos summary->contextsize += sizeof(*ctx) +
2560 1.1 christos (ctx->max_size + 1) * sizeof(struct stats) +
2561 1.1 christos ctx->max_size * sizeof(element *) +
2562 1.1 christos ctx->basic_table_count * sizeof(char *);
2563 1.1 christos summary->total += ctx->total;
2564 1.1 christos summary->inuse += ctx->inuse;
2565 1.1 christos summary->malloced += ctx->malloced;
2566 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0)
2567 1.1 christos summary->blocksize += ctx->basic_table_count *
2568 1.1 christos NUM_BASIC_BLOCKS * ctx->mem_target;
2569 1.1 christos #if ISC_MEM_TRACKLINES
2570 1.1 christos if (ctx->debuglist != NULL) {
2571 1.1 christos summary->contextsize +=
2572 1.1 christos DEBUG_TABLE_COUNT * sizeof(debuglist_t) +
2573 1.1 christos ctx->debuglistcnt * sizeof(debuglink_t);
2574 1.1 christos }
2575 1.1 christos #endif
2576 1.1 christos
2577 1.1 christos ctxobj = json_object_new_object();
2578 1.1 christos CHECKMEM(ctxobj);
2579 1.1 christos
2580 1.1 christos snprintf(buf, sizeof(buf), "%p", ctx);
2581 1.1 christos obj = json_object_new_string(buf);
2582 1.1 christos CHECKMEM(obj);
2583 1.1 christos json_object_object_add(ctxobj, "id", obj);
2584 1.1 christos
2585 1.1 christos if (ctx->name[0] != 0) {
2586 1.1 christos obj = json_object_new_string(ctx->name);
2587 1.1 christos CHECKMEM(obj);
2588 1.1 christos json_object_object_add(ctxobj, "name", obj);
2589 1.1 christos }
2590 1.1 christos
2591 1.1 christos obj = json_object_new_int64(ctx->references);
2592 1.1 christos CHECKMEM(obj);
2593 1.1 christos json_object_object_add(ctxobj, "references", obj);
2594 1.1 christos
2595 1.1 christos obj = json_object_new_int64(ctx->total);
2596 1.1 christos CHECKMEM(obj);
2597 1.1 christos json_object_object_add(ctxobj, "total", obj);
2598 1.1 christos
2599 1.1 christos obj = json_object_new_int64(ctx->inuse);
2600 1.1 christos CHECKMEM(obj);
2601 1.1 christos json_object_object_add(ctxobj, "inuse", obj);
2602 1.1 christos
2603 1.1 christos obj = json_object_new_int64(ctx->maxinuse);
2604 1.1 christos CHECKMEM(obj);
2605 1.1 christos json_object_object_add(ctxobj, "maxinuse", obj);
2606 1.1 christos
2607 1.1 christos obj = json_object_new_int64(ctx->malloced);
2608 1.1 christos CHECKMEM(obj);
2609 1.1 christos json_object_object_add(ctxobj, "malloced", obj);
2610 1.1 christos
2611 1.1 christos obj = json_object_new_int64(ctx->maxmalloced);
2612 1.1 christos CHECKMEM(obj);
2613 1.1 christos json_object_object_add(ctxobj, "maxmalloced", obj);
2614 1.1 christos
2615 1.1 christos if ((ctx->flags & ISC_MEMFLAG_INTERNAL) != 0) {
2616 1.1 christos isc_uint64_t blocksize;
2617 1.1 christos blocksize = ctx->basic_table_count * NUM_BASIC_BLOCKS *
2618 1.1 christos ctx->mem_target;
2619 1.1 christos obj = json_object_new_int64(blocksize);
2620 1.1 christos CHECKMEM(obj);
2621 1.1 christos json_object_object_add(ctxobj, "blocksize", obj);
2622 1.1 christos }
2623 1.1 christos
2624 1.1 christos obj = json_object_new_int64(ctx->poolcnt);
2625 1.1 christos CHECKMEM(obj);
2626 1.1 christos json_object_object_add(ctxobj, "pools", obj);
2627 1.1 christos
2628 1.1 christos summary->contextsize += ctx->poolcnt * sizeof(isc_mempool_t);
2629 1.1 christos
2630 1.1 christos obj = json_object_new_int64(ctx->hi_water);
2631 1.1 christos CHECKMEM(obj);
2632 1.1 christos json_object_object_add(ctxobj, "hiwater", obj);
2633 1.1 christos
2634 1.1 christos obj = json_object_new_int64(ctx->lo_water);
2635 1.1 christos CHECKMEM(obj);
2636 1.1 christos json_object_object_add(ctxobj, "lowater", obj);
2637 1.1 christos
2638 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
2639 1.1 christos json_object_array_add(array, ctxobj);
2640 1.1 christos return (ISC_R_SUCCESS);
2641 1.1 christos
2642 1.1 christos error:
2643 1.1 christos MCTXUNLOCK(ctx, &ctx->lock);
2644 1.1 christos if (ctxobj != NULL)
2645 1.1 christos json_object_put(ctxobj);
2646 1.1 christos return (result);
2647 1.1 christos }
2648 1.1 christos
2649 1.1 christos isc_result_t
2650 1.1 christos isc_mem_renderjson(json_object *memobj) {
2651 1.1 christos isc_result_t result = ISC_R_SUCCESS;
2652 1.1 christos isc__mem_t *ctx;
2653 1.1 christos summarystat_t summary;
2654 1.1 christos isc_uint64_t lost;
2655 1.1 christos json_object *ctxarray, *obj;
2656 1.1 christos
2657 1.1 christos memset(&summary, 0, sizeof(summary));
2658 1.1 christos RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2659 1.1 christos
2660 1.1 christos ctxarray = json_object_new_array();
2661 1.1 christos CHECKMEM(ctxarray);
2662 1.1 christos
2663 1.1 christos LOCK(&contextslock);
2664 1.1 christos lost = totallost;
2665 1.1 christos for (ctx = ISC_LIST_HEAD(contexts);
2666 1.1 christos ctx != NULL;
2667 1.1 christos ctx = ISC_LIST_NEXT(ctx, link)) {
2668 1.1 christos result = json_renderctx(ctx, &summary, ctxarray);
2669 1.1 christos if (result != ISC_R_SUCCESS) {
2670 1.1 christos UNLOCK(&contextslock);
2671 1.1 christos goto error;
2672 1.1 christos }
2673 1.1 christos }
2674 1.1 christos UNLOCK(&contextslock);
2675 1.1 christos
2676 1.1 christos obj = json_object_new_int64(summary.total);
2677 1.1 christos CHECKMEM(obj);
2678 1.1 christos json_object_object_add(memobj, "TotalUse", obj);
2679 1.1 christos
2680 1.1 christos obj = json_object_new_int64(summary.inuse);
2681 1.1 christos CHECKMEM(obj);
2682 1.1 christos json_object_object_add(memobj, "InUse", obj);
2683 1.1 christos
2684 1.1 christos obj = json_object_new_int64(summary.malloced);
2685 1.1 christos CHECKMEM(obj);
2686 1.1 christos json_object_object_add(memobj, "Malloced", obj);
2687 1.1 christos
2688 1.1 christos obj = json_object_new_int64(summary.blocksize);
2689 1.1 christos CHECKMEM(obj);
2690 1.1 christos json_object_object_add(memobj, "BlockSize", obj);
2691 1.1 christos
2692 1.1 christos obj = json_object_new_int64(summary.contextsize);
2693 1.1 christos CHECKMEM(obj);
2694 1.1 christos json_object_object_add(memobj, "ContextSize", obj);
2695 1.1 christos
2696 1.1 christos obj = json_object_new_int64(lost);
2697 1.1 christos CHECKMEM(obj);
2698 1.1 christos json_object_object_add(memobj, "Lost", obj);
2699 1.1 christos
2700 1.1 christos json_object_object_add(memobj, "contexts", ctxarray);
2701 1.1 christos return (ISC_R_SUCCESS);
2702 1.1 christos
2703 1.1 christos error:
2704 1.1 christos if (ctxarray != NULL)
2705 1.1 christos json_object_put(ctxarray);
2706 1.1 christos return (result);
2707 1.1 christos }
2708 1.1 christos #endif /* HAVE_JSON */
2709 1.1 christos
2710 1.1 christos static isc_memcreatefunc_t mem_createfunc = NULL;
2711 1.1 christos
2712 1.1 christos isc_result_t
2713 1.1 christos isc_mem_register(isc_memcreatefunc_t createfunc) {
2714 1.1 christos isc_result_t result = ISC_R_SUCCESS;
2715 1.1 christos
2716 1.1 christos RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
2717 1.1 christos
2718 1.1 christos LOCK(&createlock);
2719 1.1 christos if (mem_createfunc == NULL)
2720 1.1 christos mem_createfunc = createfunc;
2721 1.1 christos else
2722 1.1 christos result = ISC_R_EXISTS;
2723 1.1 christos UNLOCK(&createlock);
2724 1.1 christos
2725 1.1 christos return (result);
2726 1.1 christos }
2727 1.1 christos
2728 1.1 christos
2729 1.1 christos isc_result_t
2730 1.1 christos isc__mem_create2(size_t init_max_size, size_t target_size, isc_mem_t **mctxp,
2731 1.1 christos unsigned int flags)
2732 1.1 christos {
2733 1.1 christos isc_result_t result;
2734 1.1 christos
2735 1.1 christos LOCK(&createlock);
2736 1.1 christos
2737 1.1 christos REQUIRE(mem_createfunc != NULL);
2738 1.1 christos result = (*mem_createfunc)(init_max_size, target_size, mctxp, flags);
2739 1.1 christos
2740 1.1 christos UNLOCK(&createlock);
2741 1.1 christos
2742 1.1 christos return (result);
2743 1.1 christos }
2744 1.1 christos
2745 1.1 christos isc_result_t
2746 1.1 christos isc_mem_create(size_t init_max_size, size_t target_size, isc_mem_t **mctxp) {
2747 1.1 christos isc_result_t result;
2748 1.1 christos
2749 1.1 christos if (isc_bind9)
2750 1.1 christos return (isc_mem_createx2(init_max_size, target_size,
2751 1.1 christos default_memalloc, default_memfree,
2752 1.1 christos NULL, mctxp, isc_mem_defaultflags));
2753 1.1 christos LOCK(&createlock);
2754 1.1 christos
2755 1.1 christos REQUIRE(mem_createfunc != NULL);
2756 1.1 christos result = (*mem_createfunc)(init_max_size, target_size, mctxp,
2757 1.1 christos isc_mem_defaultflags);
2758 1.1 christos
2759 1.1 christos UNLOCK(&createlock);
2760 1.1 christos
2761 1.1 christos return (result);
2762 1.1 christos }
2763 1.1 christos
2764 1.1 christos isc_result_t
2765 1.1 christos isc_mem_create2(size_t init_max_size, size_t target_size, isc_mem_t **mctxp,
2766 1.1 christos unsigned int flags)
2767 1.1 christos {
2768 1.1 christos if (isc_bind9)
2769 1.1 christos return (isc_mem_createx2(init_max_size, target_size,
2770 1.1 christos default_memalloc, default_memfree,
2771 1.1 christos NULL, mctxp, flags));
2772 1.1 christos
2773 1.1 christos return (isc_mem_createx2(init_max_size, target_size,
2774 1.1 christos default_memalloc, default_memfree,
2775 1.1 christos NULL, mctxp, flags));
2776 1.1 christos }
2777 1.1 christos
2778 1.1 christos void
2779 1.1 christos isc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) {
2780 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(source));
2781 1.1 christos REQUIRE(targetp != NULL && *targetp == NULL);
2782 1.1 christos
2783 1.1 christos if (isc_bind9)
2784 1.1 christos isc__mem_attach(source, targetp);
2785 1.1 christos else
2786 1.1 christos source->methods->attach(source, targetp);
2787 1.1 christos
2788 1.1 christos ENSURE(*targetp == source);
2789 1.1 christos }
2790 1.1 christos
2791 1.1 christos void
2792 1.1 christos isc_mem_detach(isc_mem_t **mctxp) {
2793 1.1 christos REQUIRE(mctxp != NULL && ISCAPI_MCTX_VALID(*mctxp));
2794 1.1 christos
2795 1.1 christos if (isc_bind9)
2796 1.1 christos isc__mem_detach(mctxp);
2797 1.1 christos else
2798 1.1 christos (*mctxp)->methods->detach(mctxp);
2799 1.1 christos
2800 1.1 christos ENSURE(*mctxp == NULL);
2801 1.1 christos }
2802 1.1 christos
2803 1.1 christos void
2804 1.1 christos isc_mem_destroy(isc_mem_t **mctxp) {
2805 1.1 christos REQUIRE(mctxp != NULL && ISCAPI_MCTX_VALID(*mctxp));
2806 1.1 christos
2807 1.1 christos if (isc_bind9)
2808 1.1 christos isc__mem_destroy(mctxp);
2809 1.1 christos else
2810 1.1 christos (*mctxp)->methods->destroy(mctxp);
2811 1.1 christos
2812 1.1 christos ENSURE(*mctxp == NULL);
2813 1.1 christos }
2814 1.1 christos
2815 1.1 christos void
2816 1.1 christos isc_mem_setdestroycheck(isc_mem_t *mctx, isc_boolean_t flag) {
2817 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2818 1.1 christos
2819 1.1 christos mctx->methods->setdestroycheck(mctx, flag);
2820 1.1 christos }
2821 1.1 christos
2822 1.1 christos void
2823 1.1 christos isc_mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
2824 1.1 christos size_t hiwater, size_t lowater)
2825 1.1 christos {
2826 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(ctx));
2827 1.1 christos
2828 1.1 christos if (isc_bind9)
2829 1.1 christos isc__mem_setwater(ctx, water, water_arg, hiwater, lowater);
2830 1.1 christos else
2831 1.1 christos ctx->methods->setwater(ctx, water, water_arg, hiwater, lowater);
2832 1.1 christos }
2833 1.1 christos
2834 1.1 christos void
2835 1.1 christos isc_mem_waterack(isc_mem_t *ctx, int flag) {
2836 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(ctx));
2837 1.1 christos
2838 1.1 christos if (isc_bind9)
2839 1.1 christos isc__mem_waterack(ctx, flag);
2840 1.1 christos else
2841 1.1 christos ctx->methods->waterack(ctx, flag);
2842 1.1 christos }
2843 1.1 christos
2844 1.1 christos size_t
2845 1.1 christos isc_mem_inuse(isc_mem_t *mctx) {
2846 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2847 1.1 christos
2848 1.1 christos if (isc_bind9)
2849 1.1 christos return (isc__mem_inuse(mctx));
2850 1.1 christos
2851 1.1 christos return (mctx->methods->inuse(mctx));
2852 1.1 christos }
2853 1.1 christos
2854 1.1 christos size_t
2855 1.1 christos isc_mem_maxinuse(isc_mem_t *mctx) {
2856 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2857 1.1 christos
2858 1.1 christos if (isc_bind9)
2859 1.1 christos return (isc__mem_maxinuse(mctx));
2860 1.1 christos
2861 1.1 christos return (mctx->methods->maxinuse(mctx));
2862 1.1 christos }
2863 1.1 christos
2864 1.1 christos size_t
2865 1.1 christos isc_mem_total(isc_mem_t *mctx) {
2866 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2867 1.1 christos
2868 1.1 christos if (isc_bind9)
2869 1.1 christos return (isc__mem_total(mctx));
2870 1.1 christos
2871 1.1 christos return (mctx->methods->total(mctx));
2872 1.1 christos }
2873 1.1 christos
2874 1.1 christos isc_boolean_t
2875 1.1 christos isc_mem_isovermem(isc_mem_t *mctx) {
2876 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2877 1.1 christos
2878 1.1 christos if (isc_bind9)
2879 1.1 christos return (isc__mem_isovermem(mctx));
2880 1.1 christos
2881 1.1 christos return (mctx->methods->isovermem(mctx));
2882 1.1 christos }
2883 1.1 christos
2884 1.1 christos
2885 1.1 christos isc_result_t
2886 1.1 christos isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) {
2887 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2888 1.1 christos
2889 1.1 christos return (mctx->methods->mpcreate(mctx, size, mpctxp));
2890 1.1 christos }
2891 1.1 christos
2892 1.1 christos void
2893 1.1 christos isc_mempool_destroy(isc_mempool_t **mpctxp) {
2894 1.1 christos REQUIRE(mpctxp != NULL && ISCAPI_MPOOL_VALID(*mpctxp));
2895 1.1 christos
2896 1.1 christos if (isc_bind9)
2897 1.1 christos isc__mempool_destroy(mpctxp);
2898 1.1 christos else
2899 1.1 christos (*mpctxp)->methods->destroy(mpctxp);
2900 1.1 christos
2901 1.1 christos ENSURE(*mpctxp == NULL);
2902 1.1 christos }
2903 1.1 christos
2904 1.1 christos unsigned int
2905 1.1 christos isc_mempool_getallocated(isc_mempool_t *mpctx) {
2906 1.1 christos REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
2907 1.1 christos
2908 1.1 christos if (isc_bind9)
2909 1.1 christos return (isc__mempool_getallocated(mpctx));
2910 1.1 christos
2911 1.1 christos return (mpctx->methods->getallocated(mpctx));
2912 1.1 christos }
2913 1.1 christos
2914 1.1 christos void
2915 1.1 christos isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit) {
2916 1.1 christos REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
2917 1.1 christos
2918 1.1 christos if (isc_bind9)
2919 1.1 christos isc__mempool_setmaxalloc(mpctx, limit);
2920 1.1 christos else
2921 1.1 christos mpctx->methods->setmaxalloc(mpctx, limit);
2922 1.1 christos }
2923 1.1 christos
2924 1.1 christos void
2925 1.1 christos isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit) {
2926 1.1 christos REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
2927 1.1 christos
2928 1.1 christos if (isc_bind9)
2929 1.1 christos isc__mempool_setfreemax(mpctx, limit);
2930 1.1 christos else
2931 1.1 christos mpctx->methods->setfreemax(mpctx, limit);
2932 1.1 christos }
2933 1.1 christos
2934 1.1 christos void
2935 1.1 christos isc_mempool_setname(isc_mempool_t *mpctx, const char *name) {
2936 1.1 christos REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
2937 1.1 christos
2938 1.1 christos if (isc_bind9)
2939 1.1 christos isc__mempool_setname(mpctx, name);
2940 1.1 christos else
2941 1.1 christos mpctx->methods->setname(mpctx, name);
2942 1.1 christos }
2943 1.1 christos
2944 1.1 christos void
2945 1.1 christos isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock) {
2946 1.1 christos REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
2947 1.1 christos
2948 1.1 christos if (isc_bind9)
2949 1.1 christos isc__mempool_associatelock(mpctx, lock);
2950 1.1 christos else
2951 1.1 christos mpctx->methods->associatelock(mpctx, lock);
2952 1.1 christos }
2953 1.1 christos
2954 1.1 christos void
2955 1.1 christos isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit) {
2956 1.1 christos REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
2957 1.1 christos
2958 1.1 christos if (isc_bind9)
2959 1.1 christos isc__mempool_setfillcount(mpctx, limit);
2960 1.1 christos else
2961 1.1 christos mpctx->methods->setfillcount(mpctx, limit);
2962 1.1 christos }
2963 1.1 christos
2964 1.1 christos void *
2965 1.1 christos isc__mem_get(isc_mem_t *mctx, size_t size FLARG) {
2966 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2967 1.1 christos
2968 1.1 christos if (isc_bind9)
2969 1.1 christos return (isc___mem_get(mctx, size FLARG_PASS));
2970 1.1 christos
2971 1.1 christos return (mctx->methods->memget(mctx, size FLARG_PASS));
2972 1.1 christos
2973 1.1 christos }
2974 1.1 christos
2975 1.1 christos void
2976 1.1 christos isc__mem_put(isc_mem_t *mctx, void *ptr, size_t size FLARG) {
2977 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
2978 1.1 christos
2979 1.1 christos if (isc_bind9)
2980 1.1 christos isc___mem_put(mctx, ptr, size FLARG_PASS);
2981 1.1 christos else
2982 1.1 christos mctx->methods->memput(mctx, ptr, size FLARG_PASS);
2983 1.1 christos }
2984 1.1 christos
2985 1.1 christos void
2986 1.1 christos isc__mem_putanddetach(isc_mem_t **mctxp, void *ptr, size_t size FLARG) {
2987 1.1 christos REQUIRE(mctxp != NULL && ISCAPI_MCTX_VALID(*mctxp));
2988 1.1 christos
2989 1.1 christos if (isc_bind9)
2990 1.1 christos isc___mem_putanddetach(mctxp, ptr, size FLARG_PASS);
2991 1.1 christos else
2992 1.1 christos (*mctxp)->methods->memputanddetach(mctxp, ptr, size FLARG_PASS);
2993 1.1 christos
2994 1.1 christos /*
2995 1.1 christos * XXX: We cannot always ensure *mctxp == NULL here
2996 1.1 christos * (see lib/isc/mem.c).
2997 1.1 christos */
2998 1.1 christos }
2999 1.1 christos
3000 1.1 christos void *
3001 1.1 christos isc__mem_allocate(isc_mem_t *mctx, size_t size FLARG) {
3002 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
3003 1.1 christos
3004 1.1 christos if (isc_bind9)
3005 1.1 christos return (isc___mem_allocate(mctx, size FLARG_PASS));
3006 1.1 christos
3007 1.1 christos return (mctx->methods->memallocate(mctx, size FLARG_PASS));
3008 1.1 christos }
3009 1.1 christos
3010 1.1 christos void *
3011 1.1 christos isc__mem_reallocate(isc_mem_t *mctx, void *ptr, size_t size FLARG) {
3012 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
3013 1.1 christos
3014 1.1 christos if (isc_bind9)
3015 1.1 christos return (isc___mem_reallocate(mctx, ptr, size FLARG_PASS));
3016 1.1 christos
3017 1.1 christos return (mctx->methods->memreallocate(mctx, ptr, size FLARG_PASS));
3018 1.1 christos }
3019 1.1 christos
3020 1.1 christos char *
3021 1.1 christos isc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
3022 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
3023 1.1 christos
3024 1.1 christos if (isc_bind9)
3025 1.1 christos return (isc___mem_strdup(mctx, s FLARG_PASS));
3026 1.1 christos
3027 1.1 christos return (mctx->methods->memstrdup(mctx, s FLARG_PASS));
3028 1.1 christos }
3029 1.1 christos
3030 1.1 christos void
3031 1.1 christos isc__mem_free(isc_mem_t *mctx, void *ptr FLARG) {
3032 1.1 christos REQUIRE(ISCAPI_MCTX_VALID(mctx));
3033 1.1 christos
3034 1.1 christos if (isc_bind9)
3035 1.1 christos isc___mem_free(mctx, ptr FLARG_PASS);
3036 1.1 christos else
3037 1.1 christos mctx->methods->memfree(mctx, ptr FLARG_PASS);
3038 1.1 christos }
3039 1.1 christos
3040 1.1 christos void *
3041 1.1 christos isc__mempool_get(isc_mempool_t *mpctx FLARG) {
3042 1.1 christos REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
3043 1.1 christos
3044 1.1 christos if (isc_bind9)
3045 1.1 christos return (isc___mempool_get(mpctx FLARG_PASS));
3046 1.1 christos
3047 1.1 christos return (mpctx->methods->get(mpctx FLARG_PASS));
3048 1.1 christos }
3049 1.1 christos
3050 1.1 christos void
3051 1.1 christos isc__mempool_put(isc_mempool_t *mpctx, void *mem FLARG) {
3052 1.1 christos REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
3053 1.1 christos
3054 1.1 christos if (isc_bind9)
3055 1.1 christos isc___mempool_put(mpctx, mem FLARG_PASS);
3056 1.1 christos else
3057 1.1 christos mpctx->methods->put(mpctx, mem FLARG_PASS);
3058 1.1 christos }
3059