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