mem.h revision 290001
1/*
2 * Copyright (C) 2004-2012  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1997-2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id$ */
19
20#ifndef ISC_MEM_H
21#define ISC_MEM_H 1
22
23/*! \file isc/mem.h */
24
25#include <stdio.h>
26
27#include <isc/lang.h>
28#include <isc/mutex.h>
29#include <isc/platform.h>
30#include <isc/types.h>
31#include <isc/xml.h>
32
33ISC_LANG_BEGINDECLS
34
35#define ISC_MEM_LOWATER 0
36#define ISC_MEM_HIWATER 1
37typedef void (*isc_mem_water_t)(void *, int);
38
39typedef void * (*isc_memalloc_t)(void *, size_t);
40typedef void (*isc_memfree_t)(void *, void *);
41
42/*%
43 * Define ISC_MEM_DEBUG=1 to make all functions that free memory
44 * set the pointer being freed to NULL after being freed.
45 * This is the default; set ISC_MEM_DEBUG=0 to disable it.
46 */
47#ifndef ISC_MEM_DEBUG
48#define ISC_MEM_DEBUG 1
49#endif
50
51/*%
52 * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
53 * allocation and freeing by file and line number.
54 */
55#ifndef ISC_MEM_TRACKLINES
56#define ISC_MEM_TRACKLINES 1
57#endif
58
59/*%
60 * Define ISC_MEM_CHECKOVERRUN=1 to turn on checks for using memory outside
61 * the requested space.  This will increase the size of each allocation.
62 */
63#ifndef ISC_MEM_CHECKOVERRUN
64#define ISC_MEM_CHECKOVERRUN 1
65#endif
66
67/*%
68 * Define ISC_MEM_FILL=1 to fill each block of memory returned to the system
69 * with the byte string '0xbe'.  This helps track down uninitialized pointers
70 * and the like.  On freeing memory, the space is filled with '0xde' for
71 * the same reasons.
72 */
73#ifndef ISC_MEM_FILL
74#define ISC_MEM_FILL 1
75#endif
76
77/*%
78 * Define ISC_MEMPOOL_NAMES=1 to make memory pools store a symbolic
79 * name so that the leaking pool can be more readily identified in
80 * case of a memory leak.
81 */
82#ifndef ISC_MEMPOOL_NAMES
83#define ISC_MEMPOOL_NAMES 1
84#endif
85
86LIBISC_EXTERNAL_DATA extern unsigned int isc_mem_debugging;
87/*@{*/
88#define ISC_MEM_DEBUGTRACE		0x00000001U
89#define ISC_MEM_DEBUGRECORD		0x00000002U
90#define ISC_MEM_DEBUGUSAGE		0x00000004U
91#define ISC_MEM_DEBUGSIZE		0x00000008U
92#define ISC_MEM_DEBUGCTX		0x00000010U
93#define ISC_MEM_DEBUGALL		0x0000001FU
94/*!<
95 * The variable isc_mem_debugging holds a set of flags for
96 * turning certain memory debugging options on or off at
97 * runtime.  It is initialized to the value ISC_MEM_DEGBUGGING,
98 * which is 0 by default but may be overridden at compile time.
99 * The following flags can be specified:
100 *
101 * \li #ISC_MEM_DEBUGTRACE
102 *	Log each allocation and free to isc_lctx.
103 *
104 * \li #ISC_MEM_DEBUGRECORD
105 *	Remember each allocation, and match them up on free.
106 *	Crash if a free doesn't match an allocation.
107 *
108 * \li #ISC_MEM_DEBUGUSAGE
109 *	If a hi_water mark is set, print the maximum inuse memory
110 *	every time it is raised once it exceeds the hi_water mark.
111 *
112 * \li #ISC_MEM_DEBUGSIZE
113 *	Check the size argument being passed to isc_mem_put() matches
114 *	that passed to isc_mem_get().
115 *
116 * \li #ISC_MEM_DEBUGCTX
117 *	Check the mctx argument being passed to isc_mem_put() matches
118 *	that passed to isc_mem_get().
119 */
120/*@}*/
121
122#if ISC_MEM_TRACKLINES
123#define _ISC_MEM_FILELINE	, __FILE__, __LINE__
124#define _ISC_MEM_FLARG		, const char *, unsigned int
125#else
126#define _ISC_MEM_FILELINE
127#define _ISC_MEM_FLARG
128#endif
129
130/*!
131 * Define ISC_MEM_USE_INTERNAL_MALLOC=1 to use the internal malloc()
132 * implementation in preference to the system one.  The internal malloc()
133 * is very space-efficient, and quite fast on uniprocessor systems.  It
134 * performs poorly on multiprocessor machines.
135 * JT: we can overcome the performance issue on multiprocessor machines
136 * by carefully separating memory contexts.
137 */
138
139#ifndef ISC_MEM_USE_INTERNAL_MALLOC
140#define ISC_MEM_USE_INTERNAL_MALLOC 1
141#endif
142
143/*
144 * Flags for isc_mem_create2()calls.
145 */
146#define ISC_MEMFLAG_NOLOCK	0x00000001	 /* no lock is necessary */
147#define ISC_MEMFLAG_INTERNAL	0x00000002	 /* use internal malloc */
148#if ISC_MEM_USE_INTERNAL_MALLOC
149#define ISC_MEMFLAG_DEFAULT 	ISC_MEMFLAG_INTERNAL
150#else
151#define ISC_MEMFLAG_DEFAULT 	0
152#endif
153
154
155/*%<
156 * We use either isc___mem (three underscores) or isc__mem (two) depending on
157 * whether it's for BIND9's internal purpose (with -DBIND9) or generic export
158 * library.  This condition is generally handled in isc/namespace.h, but for
159 * Windows it doesn't work if it involves multiple times of macro expansion
160 * (such as isc_mem to isc__mem then to isc___mem).  The following definitions
161 * are used to work around this portability issue.  Right now, we don't support
162 * the export library for Windows, so we always use the three-underscore
163 * version.
164 */
165#ifdef WIN32
166#define ISCMEMFUNC(sfx) isc___mem_ ## sfx
167#define ISCMEMPOOLFUNC(sfx) isc___mempool_ ## sfx
168#else
169#define ISCMEMFUNC(sfx) isc__mem_ ## sfx
170#define ISCMEMPOOLFUNC(sfx) isc__mempool_ ## sfx
171#endif
172
173#define isc_mem_get(c, s)	ISCMEMFUNC(get)((c), (s) _ISC_MEM_FILELINE)
174#define isc_mem_allocate(c, s)	ISCMEMFUNC(allocate)((c), (s) _ISC_MEM_FILELINE)
175#define isc_mem_reallocate(c, p, s) ISCMEMFUNC(reallocate)((c), (p), (s) _ISC_MEM_FILELINE)
176#define isc_mem_strdup(c, p)	ISCMEMFUNC(strdup)((c), (p) _ISC_MEM_FILELINE)
177#define isc_mempool_get(c)	ISCMEMPOOLFUNC(get)((c) _ISC_MEM_FILELINE)
178
179/*%
180 * isc_mem_putanddetach() is a convenience function for use where you
181 * have a structure with an attached memory context.
182 *
183 * Given:
184 *
185 * \code
186 * struct {
187 *	...
188 *	isc_mem_t *mctx;
189 *	...
190 * } *ptr;
191 *
192 * isc_mem_t *mctx;
193 *
194 * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr));
195 * \endcode
196 *
197 * is the equivalent of:
198 *
199 * \code
200 * mctx = NULL;
201 * isc_mem_attach(ptr->mctx, &mctx);
202 * isc_mem_detach(&ptr->mctx);
203 * isc_mem_put(mctx, ptr, sizeof(*ptr));
204 * isc_mem_detach(&mctx);
205 * \endcode
206 */
207
208/*% memory and memory pool methods */
209typedef struct isc_memmethods {
210	void (*attach)(isc_mem_t *source, isc_mem_t **targetp);
211	void (*detach)(isc_mem_t **mctxp);
212	void (*destroy)(isc_mem_t **mctxp);
213	void *(*memget)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG);
214	void (*memput)(isc_mem_t *mctx, void *ptr, size_t size _ISC_MEM_FLARG);
215	void (*memputanddetach)(isc_mem_t **mctxp, void *ptr,
216				size_t size _ISC_MEM_FLARG);
217	void *(*memallocate)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG);
218	void *(*memreallocate)(isc_mem_t *mctx, void *ptr,
219			       size_t size _ISC_MEM_FLARG);
220	char *(*memstrdup)(isc_mem_t *mctx, const char *s _ISC_MEM_FLARG);
221	void (*memfree)(isc_mem_t *mctx, void *ptr _ISC_MEM_FLARG);
222	void (*setdestroycheck)(isc_mem_t *mctx, isc_boolean_t flag);
223	void (*setwater)(isc_mem_t *ctx, isc_mem_water_t water,
224			 void *water_arg, size_t hiwater, size_t lowater);
225	void (*waterack)(isc_mem_t *ctx, int flag);
226	size_t (*inuse)(isc_mem_t *mctx);
227	isc_boolean_t (*isovermem)(isc_mem_t *mctx);
228	isc_result_t (*mpcreate)(isc_mem_t *mctx, size_t size,
229				 isc_mempool_t **mpctxp);
230} isc_memmethods_t;
231
232typedef struct isc_mempoolmethods {
233	void (*destroy)(isc_mempool_t **mpctxp);
234	void *(*get)(isc_mempool_t *mpctx _ISC_MEM_FLARG);
235	void (*put)(isc_mempool_t *mpctx, void *mem _ISC_MEM_FLARG);
236	unsigned int (*getallocated)(isc_mempool_t *mpctx);
237	void (*setmaxalloc)(isc_mempool_t *mpctx, unsigned int limit);
238	void (*setfreemax)(isc_mempool_t *mpctx, unsigned int limit);
239	void (*setname)(isc_mempool_t *mpctx, const char *name);
240	void (*associatelock)(isc_mempool_t *mpctx, isc_mutex_t *lock);
241	void (*setfillcount)(isc_mempool_t *mpctx, unsigned int limit);
242} isc_mempoolmethods_t;
243
244/*%
245 * This structure is actually just the common prefix of a memory context
246 * implementation's version of an isc_mem_t.
247 * \brief
248 * Direct use of this structure by clients is forbidden.  mctx implementations
249 * may change the structure.  'magic' must be ISCAPI_MCTX_MAGIC for any of the
250 * isc_mem_ routines to work.  mctx implementations must maintain all mctx
251 * invariants.
252 */
253struct isc_mem {
254	unsigned int		impmagic;
255	unsigned int		magic;
256	isc_memmethods_t	*methods;
257};
258
259#define ISCAPI_MCTX_MAGIC	ISC_MAGIC('A','m','c','x')
260#define ISCAPI_MCTX_VALID(m)	((m) != NULL && \
261				 (m)->magic == ISCAPI_MCTX_MAGIC)
262
263/*%
264 * This is the common prefix of a memory pool context.  The same note as
265 * that for the mem structure applies.
266 */
267struct isc_mempool {
268	unsigned int		impmagic;
269	unsigned int		magic;
270	isc_mempoolmethods_t	*methods;
271};
272
273#define ISCAPI_MPOOL_MAGIC	ISC_MAGIC('A','m','p','l')
274#define ISCAPI_MPOOL_VALID(mp)	((mp) != NULL && \
275				 (mp)->magic == ISCAPI_MPOOL_MAGIC)
276
277#if ISC_MEM_DEBUG
278#define isc_mem_put(c, p, s) \
279	do { \
280		ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE);	\
281		(p) = NULL; \
282	} while (0)
283#define isc_mem_putanddetach(c, p, s) \
284	do { \
285		ISCMEMFUNC(putanddetach)((c), (p), (s) _ISC_MEM_FILELINE); \
286		(p) = NULL; \
287	} while (0)
288#define isc_mem_free(c, p) \
289	do { \
290		ISCMEMFUNC(free)((c), (p) _ISC_MEM_FILELINE);	\
291		(p) = NULL; \
292	} while (0)
293#define isc_mempool_put(c, p) \
294	do { \
295		ISCMEMPOOLFUNC(put)((c), (p) _ISC_MEM_FILELINE);	\
296		(p) = NULL; \
297	} while (0)
298#else
299#define isc_mem_put(c, p, s)	ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE)
300#define isc_mem_putanddetach(c, p, s) \
301	ISCMEMFUNC(putanddetach)((c), (p), (s) _ISC_MEM_FILELINE)
302#define isc_mem_free(c, p)	ISCMEMFUNC(free)((c), (p) _ISC_MEM_FILELINE)
303#define isc_mempool_put(c, p)	ISCMEMPOOLFUNC(put)((c), (p) _ISC_MEM_FILELINE)
304#endif
305
306/*@{*/
307isc_result_t
308isc_mem_create(size_t max_size, size_t target_size,
309	       isc_mem_t **mctxp);
310
311isc_result_t
312isc_mem_create2(size_t max_size, size_t target_size,
313		isc_mem_t **mctxp, unsigned int flags);
314
315isc_result_t
316isc_mem_createx(size_t max_size, size_t target_size,
317		isc_memalloc_t memalloc, isc_memfree_t memfree,
318		void *arg, isc_mem_t **mctxp);
319
320isc_result_t
321isc_mem_createx2(size_t max_size, size_t target_size,
322		 isc_memalloc_t memalloc, isc_memfree_t memfree,
323		 void *arg, isc_mem_t **mctxp, unsigned int flags);
324
325/*!<
326 * \brief Create a memory context.
327 *
328 * 'max_size' and 'target_size' are tuning parameters.  When
329 * ISC_MEMFLAG_INTERNAL is set, allocations smaller than 'max_size'
330 * will be satisfied by getting blocks of size 'target_size' from the
331 * system allocator and breaking them up into pieces; larger allocations
332 * will use the system allocator directly. If 'max_size' and/or
333 * 'target_size' are zero, default values will be * used.  When
334 * ISC_MEMFLAG_INTERNAL is not set, 'target_size' is ignored.
335 *
336 * 'max_size' is also used to size the statistics arrays and the array
337 * used to record active memory when ISC_MEM_DEBUGRECORD is set.  Setting
338 * 'max_size' too low can have detrimental effects on performance.
339 *
340 * A memory context created using isc_mem_createx() will obtain
341 * memory from the system by calling 'memalloc' and 'memfree',
342 * passing them the argument 'arg'.  A memory context created
343 * using isc_mem_create() will use the standard library malloc()
344 * and free().
345 *
346 * If ISC_MEMFLAG_NOLOCK is set in 'flags', the corresponding memory context
347 * will be accessed without locking.  The user who creates the context must
348 * ensure there be no race.  Since this can be a source of bug, it is generally
349 * inadvisable to use this flag unless the user is very sure about the race
350 * condition and the access to the object is highly performance sensitive.
351 *
352 * Requires:
353 * mctxp != NULL && *mctxp == NULL */
354/*@}*/
355
356/*@{*/
357void
358isc_mem_attach(isc_mem_t *, isc_mem_t **);
359void
360isc_mem_detach(isc_mem_t **);
361/*!<
362 * \brief Attach to / detach from a memory context.
363 *
364 * This is intended for applications that use multiple memory contexts
365 * in such a way that it is not obvious when the last allocations from
366 * a given context has been freed and destroying the context is safe.
367 *
368 * Most applications do not need to call these functions as they can
369 * simply create a single memory context at the beginning of main()
370 * and destroy it at the end of main(), thereby guaranteeing that it
371 * is not destroyed while there are outstanding allocations.
372 */
373/*@}*/
374
375void
376isc_mem_destroy(isc_mem_t **);
377/*%<
378 * Destroy a memory context.
379 */
380
381isc_result_t
382isc_mem_ondestroy(isc_mem_t *ctx,
383		  isc_task_t *task,
384		  isc_event_t **event);
385/*%<
386 * Request to be notified with an event when a memory context has
387 * been successfully destroyed.
388 */
389
390void
391isc_mem_stats(isc_mem_t *mctx, FILE *out);
392/*%<
393 * Print memory usage statistics for 'mctx' on the stream 'out'.
394 */
395
396void
397isc_mem_setdestroycheck(isc_mem_t *mctx,
398			isc_boolean_t on);
399/*%<
400 * If 'on' is ISC_TRUE, 'mctx' will check for memory leaks when
401 * destroyed and abort the program if any are present.
402 */
403
404/*@{*/
405void
406isc_mem_setquota(isc_mem_t *, size_t);
407size_t
408isc_mem_getquota(isc_mem_t *);
409/*%<
410 * Set/get the memory quota of 'mctx'.  This is a hard limit
411 * on the amount of memory that may be allocated from mctx;
412 * if it is exceeded, allocations will fail.
413 */
414/*@}*/
415
416size_t
417isc_mem_inuse(isc_mem_t *mctx);
418/*%<
419 * Get an estimate of the number of memory in use in 'mctx', in bytes.
420 * This includes quantization overhead, but does not include memory
421 * allocated from the system but not yet used.
422 */
423
424isc_boolean_t
425isc_mem_isovermem(isc_mem_t *mctx);
426/*%<
427 * Return true iff the memory context is in "over memory" state, i.e.,
428 * a hiwater mark has been set and the used amount of memory has exceeds
429 * the mark.
430 */
431
432void
433isc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg,
434		 size_t hiwater, size_t lowater);
435/*%<
436 * Set high and low water marks for this memory context.
437 *
438 * When the memory usage of 'mctx' exceeds 'hiwater',
439 * '(water)(water_arg, #ISC_MEM_HIWATER)' will be called.  'water' needs to
440 * call isc_mem_waterack() with #ISC_MEM_HIWATER to acknowledge the state
441 * change.  'water' may be called multiple times.
442 *
443 * When the usage drops below 'lowater', 'water' will again be called, this
444 * time with #ISC_MEM_LOWATER.  'water' need to calls isc_mem_waterack() with
445 * #ISC_MEM_LOWATER to acknowledge the change.
446 *
447 *	static void
448 *	water(void *arg, int mark) {
449 *		struct foo *foo = arg;
450 *
451 *		LOCK(&foo->marklock);
452 *		if (foo->mark != mark) {
453 * 			foo->mark = mark;
454 *			....
455 *			isc_mem_waterack(foo->mctx, mark);
456 *		}
457 *		UNLOCK(&foo->marklock);
458 *	}
459 *
460 * If 'water' is NULL then 'water_arg', 'hi_water' and 'lo_water' are
461 * ignored and the state is reset.
462 *
463 * Requires:
464 *
465 *	'water' is not NULL.
466 *	hi_water >= lo_water
467 */
468
469void
470isc_mem_waterack(isc_mem_t *ctx, int mark);
471/*%<
472 * Called to acknowledge changes in signaled by calls to 'water'.
473 */
474
475void
476isc_mem_printactive(isc_mem_t *mctx, FILE *file);
477/*%<
478 * Print to 'file' all active memory in 'mctx'.
479 *
480 * Requires ISC_MEM_DEBUGRECORD to have been set.
481 */
482
483void
484isc_mem_printallactive(FILE *file);
485/*%<
486 * Print to 'file' all active memory in all contexts.
487 *
488 * Requires ISC_MEM_DEBUGRECORD to have been set.
489 */
490
491void
492isc_mem_checkdestroyed(FILE *file);
493/*%<
494 * Check that all memory contexts have been destroyed.
495 * Prints out those that have not been.
496 * Fatally fails if there are still active contexts.
497 */
498
499unsigned int
500isc_mem_references(isc_mem_t *ctx);
501/*%<
502 * Return the current reference count.
503 */
504
505void
506isc_mem_setname(isc_mem_t *ctx, const char *name, void *tag);
507/*%<
508 * Name 'ctx'.
509 *
510 * Notes:
511 *
512 *\li	Only the first 15 characters of 'name' will be copied.
513 *
514 *\li	'tag' is for debugging purposes only.
515 *
516 * Requires:
517 *
518 *\li	'ctx' is a valid ctx.
519 */
520
521const char *
522isc_mem_getname(isc_mem_t *ctx);
523/*%<
524 * Get the name of 'ctx', as previously set using isc_mem_setname().
525 *
526 * Requires:
527 *\li	'ctx' is a valid ctx.
528 *
529 * Returns:
530 *\li	A non-NULL pointer to a null-terminated string.
531 * 	If the ctx has not been named, the string is
532 * 	empty.
533 */
534
535void *
536isc_mem_gettag(isc_mem_t *ctx);
537/*%<
538 * Get the tag value for  'task', as previously set using isc_mem_setname().
539 *
540 * Requires:
541 *\li	'ctx' is a valid ctx.
542 *
543 * Notes:
544 *\li	This function is for debugging purposes only.
545 *
546 * Requires:
547 *\li	'ctx' is a valid task.
548 */
549
550#ifdef HAVE_LIBXML2
551void
552isc_mem_renderxml(xmlTextWriterPtr writer);
553/*%<
554 * Render all contexts' statistics and status in XML for writer.
555 */
556#endif /* HAVE_LIBXML2 */
557
558/*
559 * Memory pools
560 */
561
562isc_result_t
563isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
564/*%<
565 * Create a memory pool.
566 *
567 * Requires:
568 *\li	mctx is a valid memory context.
569 *\li	size > 0
570 *\li	mpctxp != NULL and *mpctxp == NULL
571 *
572 * Defaults:
573 *\li	maxalloc = UINT_MAX
574 *\li	freemax = 1
575 *\li	fillcount = 1
576 *
577 * Returns:
578 *\li	#ISC_R_NOMEMORY		-- not enough memory to create pool
579 *\li	#ISC_R_SUCCESS		-- all is well.
580 */
581
582void
583isc_mempool_destroy(isc_mempool_t **mpctxp);
584/*%<
585 * Destroy a memory pool.
586 *
587 * Requires:
588 *\li	mpctxp != NULL && *mpctxp is a valid pool.
589 *\li	The pool has no un"put" allocations outstanding
590 */
591
592void
593isc_mempool_setname(isc_mempool_t *mpctx, const char *name);
594/*%<
595 * Associate a name with a memory pool.  At most 15 characters may be used.
596 *
597 * Requires:
598 *\li	mpctx is a valid pool.
599 *\li	name != NULL;
600 */
601
602void
603isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
604/*%<
605 * Associate a lock with this memory pool.
606 *
607 * This lock is used when getting or putting items using this memory pool,
608 * and it is also used to set or get internal state via the isc_mempool_get*()
609 * and isc_mempool_set*() set of functions.
610 *
611 * Multiple pools can each share a single lock.  For instance, if "manager"
612 * type object contained pools for various sizes of events, and each of
613 * these pools used a common lock.  Note that this lock must NEVER be used
614 * by other than mempool routines once it is given to a pool, since that can
615 * easily cause double locking.
616 *
617 * Requires:
618 *
619 *\li	mpctpx is a valid pool.
620 *
621 *\li	lock != NULL.
622 *
623 *\li	No previous lock is assigned to this pool.
624 *
625 *\li	The lock is initialized before calling this function via the normal
626 *	means of doing that.
627 */
628
629/*
630 * The following functions get/set various parameters.  Note that due to
631 * the unlocked nature of pools these are potentially random values unless
632 * the imposed externally provided locking protocols are followed.
633 *
634 * Also note that the quota limits will not always take immediate effect.
635 * For instance, setting "maxalloc" to a number smaller than the currently
636 * allocated count is permitted.  New allocations will be refused until
637 * the count drops below this threshold.
638 *
639 * All functions require (in addition to other requirements):
640 *	mpctx is a valid memory pool
641 */
642
643unsigned int
644isc_mempool_getfreemax(isc_mempool_t *mpctx);
645/*%<
646 * Returns the maximum allowed size of the free list.
647 */
648
649void
650isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
651/*%<
652 * Sets the maximum allowed size of the free list.
653 */
654
655unsigned int
656isc_mempool_getfreecount(isc_mempool_t *mpctx);
657/*%<
658 * Returns current size of the free list.
659 */
660
661unsigned int
662isc_mempool_getmaxalloc(isc_mempool_t *mpctx);
663/*!<
664 * Returns the maximum allowed number of allocations.
665 */
666
667void
668isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
669/*%<
670 * Sets the maximum allowed number of allocations.
671 *
672 * Additional requirements:
673 *\li	limit > 0
674 */
675
676unsigned int
677isc_mempool_getallocated(isc_mempool_t *mpctx);
678/*%<
679 * Returns the number of items allocated from this pool.
680 */
681
682unsigned int
683isc_mempool_getfillcount(isc_mempool_t *mpctx);
684/*%<
685 * Returns the number of items allocated as a block from the parent memory
686 * context when the free list is empty.
687 */
688
689void
690isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
691/*%<
692 * Sets the fillcount.
693 *
694 * Additional requirements:
695 *\li	limit > 0
696 */
697
698
699/*
700 * Pseudo-private functions for use via macros.  Do not call directly.
701 */
702void *
703ISCMEMFUNC(get)(isc_mem_t *, size_t _ISC_MEM_FLARG);
704void
705ISCMEMFUNC(putanddetach)(isc_mem_t **, void *, size_t _ISC_MEM_FLARG);
706void
707ISCMEMFUNC(put)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
708void *
709ISCMEMFUNC(allocate)(isc_mem_t *, size_t _ISC_MEM_FLARG);
710void *
711ISCMEMFUNC(reallocate)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
712void
713ISCMEMFUNC(free)(isc_mem_t *, void * _ISC_MEM_FLARG);
714char *
715ISCMEMFUNC(strdup)(isc_mem_t *, const char *_ISC_MEM_FLARG);
716void *
717ISCMEMPOOLFUNC(get)(isc_mempool_t * _ISC_MEM_FLARG);
718void
719ISCMEMPOOLFUNC(put)(isc_mempool_t *, void * _ISC_MEM_FLARG);
720
721#ifdef USE_MEMIMPREGISTER
722
723/*%<
724 * See isc_mem_create2() above.
725 */
726typedef isc_result_t
727(*isc_memcreatefunc_t)(size_t init_max_size, size_t target_size,
728		       isc_mem_t **ctxp, unsigned int flags);
729
730isc_result_t
731isc_mem_register(isc_memcreatefunc_t createfunc);
732/*%<
733 * Register a new memory management implementation and add it to the list of
734 * supported implementations.  This function must be called when a different
735 * memory management library is used than the one contained in the ISC library.
736 */
737
738isc_result_t
739isc__mem_register(void);
740/*%<
741 * A short cut function that specifies the memory management module in the ISC
742 * library for isc_mem_register().  An application that uses the ISC library
743 * usually do not have to care about this function: it would call
744 * isc_lib_register(), which internally calls this function.
745 */
746#endif /* USE_MEMIMPREGISTER */
747
748ISC_LANG_ENDDECLS
749
750#endif /* ISC_MEM_H */
751