2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1997-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: mem.h,v 1.54.12.4 2004/10/11 05:55:51 marka Exp $ */
26 #include <isc/mutex.h>
27 #include <isc/platform.h>
28 #include <isc/types.h>
32 #define ISC_MEM_LOWATER 0
33 #define ISC_MEM_HIWATER 1
34 typedef void (*isc_mem_water_t)(void *, int);
36 typedef void * (*isc_memalloc_t)(void *, size_t);
37 typedef void (*isc_memfree_t)(void *, void *);
40 * Define ISC_MEM_DEBUG=1 to make all functions that free memory
41 * set the pointer being freed to NULL after being freed.
42 * This is the default; set ISC_MEM_DEBUG=0 to disable it.
45 #define ISC_MEM_DEBUG 1
49 * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
50 * allocation and freeing by file and line number.
52 #ifndef ISC_MEM_TRACKLINES
53 #define ISC_MEM_TRACKLINES 1
57 * Define ISC_MEM_CHECKOVERRUN=1 to turn on checks for using memory outside
58 * the requested space. This will increase the size of each allocation.
60 #ifndef ISC_MEM_CHECKOVERRUN
61 #define ISC_MEM_CHECKOVERRUN 1
65 * Define ISC_MEM_FILL=1 to fill each block of memory returned to the system
66 * with the byte string '0xbe'. This helps track down uninitialized pointers
67 * and the like. On freeing memory, the space is filled with '0xde' for
71 #define ISC_MEM_FILL 1
75 * Define ISC_MEMPOOL_NAMES=1 to make memory pools store a symbolic
76 * name so that the leaking pool can be more readily identified in
77 * case of a memory leak.
79 #ifndef ISC_MEMPOOL_NAMES
80 #define ISC_MEMPOOL_NAMES 1
83 LIBISC_EXTERNAL_DATA extern unsigned int isc_mem_debugging;
84 #define ISC_MEM_DEBUGTRACE 0x00000001U
85 #define ISC_MEM_DEBUGRECORD 0x00000002U
86 #define ISC_MEM_DEBUGUSAGE 0x00000004U
88 * The variable isc_mem_debugging holds a set of flags for
89 * turning certain memory debugging options on or off at
90 * runtime. Its is intialized to the value ISC_MEM_DEGBUGGING,
91 * which is 0 by default but may be overridden at compile time.
92 * The following flags can be specified:
95 * Log each allocation and free to isc_lctx.
98 * Remember each allocation, and match them up on free.
99 * Crash if a free doesn't match an allocation.
102 * If a hi_water mark is set, print the maximium inuse memory
103 * every time it is raised once it exceeds the hi_water mark.
106 #if ISC_MEM_TRACKLINES
107 #define _ISC_MEM_FILELINE , __FILE__, __LINE__
108 #define _ISC_MEM_FLARG , const char *, int
110 #define _ISC_MEM_FILELINE
111 #define _ISC_MEM_FLARG
114 #define isc_mem_get(c, s) isc__mem_get((c), (s) _ISC_MEM_FILELINE)
115 #define isc_mem_allocate(c, s) isc__mem_allocate((c), (s) _ISC_MEM_FILELINE)
116 #define isc_mem_strdup(c, p) isc__mem_strdup((c), (p) _ISC_MEM_FILELINE)
117 #define isc_mempool_get(c) isc__mempool_get((c) _ISC_MEM_FILELINE)
120 * isc_mem_putanddetach() is a convienence function for use where you
121 * have a structure with an attached memory context.
133 * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr));
135 * is the equivalent of:
138 * isc_mem_attach(ptr->mctx, &mctx);
139 * isc_mem_detach(&ptr->mctx);
140 * isc_mem_put(mctx, ptr, sizeof(*ptr));
141 * isc_mem_detach(&mctx);
145 #define isc_mem_put(c, p, s) \
147 isc__mem_put((c), (p), (s) _ISC_MEM_FILELINE); \
150 #define isc_mem_putanddetach(c, p, s) \
152 isc__mem_putanddetach((c), (p), (s) _ISC_MEM_FILELINE); \
155 #define isc_mem_free(c, p) \
157 isc__mem_free((c), (p) _ISC_MEM_FILELINE); \
160 #define isc_mempool_put(c, p) \
162 isc__mempool_put((c), (p) _ISC_MEM_FILELINE); \
166 #define isc_mem_put(c, p, s) isc__mem_put((c), (p), (s) _ISC_MEM_FILELINE)
167 #define isc_mem_putanddetach(c, p, s) \
168 isc__mem_putanddetach((c), (p), (s) _ISC_MEM_FILELINE)
169 #define isc_mem_free(c, p) isc__mem_free((c), (p) _ISC_MEM_FILELINE)
170 #define isc_mempool_put(c, p) isc__mempool_put((c), (p) _ISC_MEM_FILELINE)
174 isc_mem_create(size_t max_size, size_t target_size,
178 isc_mem_createx(size_t max_size, size_t target_size,
179 isc_memalloc_t memalloc, isc_memfree_t memfree,
180 void *arg, isc_mem_t **mctxp);
182 * Create a memory context.
184 * 'max_size' and 'target_size' are tuning parameters. When
185 * ISC_MEM_USE_INTERNAL_MALLOC is true, allocations smaller than
186 * 'max_size' will be satisfied by getting blocks of size
187 * 'target_size' from the system allocator and breaking them up into
188 * pieces; larger allocations will use the system allocator directly.
189 * If 'max_size' and/or 'target_size' are zero, default values will be
190 * used. When ISC_MEM_USE_INTERNAL_MALLOC is false, 'target_size' is
193 * 'max_size' is also used to size the statistics arrays and the array
194 * used to record active memory when ISC_MEM_DEBUGRECORD is set. Settin
195 * 'max_size' too low can have detrimental effects on performance.
197 * A memory context created using isc_mem_createx() will obtain
198 * memory from the system by calling 'memalloc' and 'memfree',
199 * passing them the argument 'arg'. A memory context created
200 * using isc_mem_create() will use the standard library malloc()
204 * mctxp != NULL && *mctxp == NULL */
207 isc_mem_attach(isc_mem_t *, isc_mem_t **);
209 isc_mem_detach(isc_mem_t **);
211 * Attach to / detach from a memory context.
213 * This is intended for applications that use multiple memory contexts
214 * in such a way that it is not obvious when the last allocations from
215 * a given context has been freed and destroying the context is safe.
217 * Most applications do not need to call these functions as they can
218 * simply create a single memory context at the beginning of main()
219 * and destroy it at the end of main(), thereby guaranteeing that it
220 * is not destroyed while there are outstanding allocations.
224 isc_mem_destroy(isc_mem_t **);
226 * Destroy a memory context.
230 isc_mem_ondestroy(isc_mem_t *ctx,
232 isc_event_t **event);
234 * Request to be notified with an event when a memory context has
235 * been successfully destroyed.
239 isc_mem_stats(isc_mem_t *mctx, FILE *out);
241 * Print memory usage statistics for 'mctx' on the stream 'out'.
245 isc_mem_setdestroycheck(isc_mem_t *mctx,
248 * Iff 'on' is ISC_TRUE, 'mctx' will check for memory leaks when
249 * destroyed and abort the program if any are present.
253 isc_mem_setquota(isc_mem_t *, size_t);
255 isc_mem_getquota(isc_mem_t *);
257 * Set/get the memory quota of 'mctx'. This is a hard limit
258 * on the amount of memory that may be allocated from mctx;
259 * if it is exceeded, allocations will fail.
263 isc_mem_inuse(isc_mem_t *mctx);
265 * Get an estimate of the number of memory in use in 'mctx', in bytes.
266 * This includes quantization overhead, but does not include memory
267 * allocated from the system but not yet used.
271 isc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg,
272 size_t hiwater, size_t lowater);
274 * Set high and low water marks for this memory context. When the memory
275 * usage of 'mctx' exceeds 'hiwater', '(water)(water_arg, ISC_MEM_HIWATER)'
276 * will be called. When the usage drops below 'lowater', 'water' will
277 * again be called, this time with ISC_MEM_LOWATER.
279 * If 'water' is NULL then 'water_arg', 'hi_water' and 'lo_water' are
280 * ignored and the state is reset.
284 * 'water' is not NULL.
285 * hi_water >= lo_water
293 isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
295 * Create a memory pool.
298 * mctx is a valid memory context.
300 * mpctxp != NULL and *mpctxp == NULL
303 * maxalloc = UINT_MAX
308 * ISC_R_NOMEMORY -- not enough memory to create pool
309 * ISC_R_SUCCESS -- all is well.
313 isc_mempool_destroy(isc_mempool_t **mpctxp);
315 * Destroy a memory pool.
318 * mpctxp != NULL && *mpctxp is a valid pool.
319 * The pool has no un"put" allocations outstanding
323 isc_mempool_setname(isc_mempool_t *mpctx, const char *name);
325 * Associate a name with a memory pool. At most 15 characters may be used.
328 * mpctx is a valid pool.
333 isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
335 * Associate a lock with this memory pool.
337 * This lock is used when getting or putting items using this memory pool,
338 * and it is also used to set or get internal state via the isc_mempool_get*()
339 * and isc_mempool_set*() set of functions.
341 * Mutiple pools can each share a single lock. For instance, if "manager"
342 * type object contained pools for various sizes of events, and each of
343 * these pools used a common lock. Note that this lock must NEVER be used
344 * by other than mempool routines once it is given to a pool, since that can
345 * easily cause double locking.
349 * mpctpx is a valid pool.
353 * No previous lock is assigned to this pool.
355 * The lock is initialized before calling this function via the normal
356 * means of doing that.
360 * The following functions get/set various parameters. Note that due to
361 * the unlocked nature of pools these are potentially random values unless
362 * the imposed externally provided locking protocols are followed.
364 * Also note that the quota limits will not always take immediate effect.
365 * For instance, setting "maxalloc" to a number smaller than the currently
366 * allocated count is permitted. New allocations will be refused until
367 * the count drops below this threshold.
369 * All functions require (in addition to other requirements):
370 * mpctx is a valid memory pool
374 isc_mempool_getfreemax(isc_mempool_t *mpctx);
376 * Returns the maximum allowed size of the free list.
380 isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
382 * Sets the maximum allowed size of the free list.
386 isc_mempool_getfreecount(isc_mempool_t *mpctx);
388 * Returns current size of the free list.
392 isc_mempool_getmaxalloc(isc_mempool_t *mpctx);
394 * Returns the maximum allowed number of allocations.
398 isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
400 * Sets the maximum allowed number of allocations.
402 * Additional requirements:
407 isc_mempool_getallocated(isc_mempool_t *mpctx);
409 * Returns the number of items allocated from this pool.
413 isc_mempool_getfillcount(isc_mempool_t *mpctx);
415 * Returns the number of items allocated as a block from the parent memory
416 * context when the free list is empty.
420 isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
422 * Sets the fillcount.
424 * Additional requirements:
430 * Pseudo-private functions for use via macros. Do not call directly.
433 isc__mem_get(isc_mem_t *, size_t _ISC_MEM_FLARG);
435 isc__mem_putanddetach(isc_mem_t **, void *,
436 size_t _ISC_MEM_FLARG);
438 isc__mem_put(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
440 isc__mem_allocate(isc_mem_t *, size_t _ISC_MEM_FLARG);
442 isc__mem_free(isc_mem_t *, void * _ISC_MEM_FLARG);
444 isc__mem_strdup(isc_mem_t *, const char *_ISC_MEM_FLARG);
446 isc__mempool_get(isc_mempool_t * _ISC_MEM_FLARG);
448 isc__mempool_put(isc_mempool_t *, void * _ISC_MEM_FLARG);
452 #endif /* ISC_MEM_H */