Merge branch 'vendor/TCSH'
[dragonfly.git] / contrib / bind-9.3 / lib / isc / include / isc / mem.h
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1997-2001  Internet Software Consortium.
4  *
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.
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: mem.h,v 1.54.12.4 2004/10/11 05:55:51 marka Exp $ */
19
20 #ifndef ISC_MEM_H
21 #define ISC_MEM_H 1
22
23 #include <stdio.h>
24
25 #include <isc/lang.h>
26 #include <isc/mutex.h>
27 #include <isc/platform.h>
28 #include <isc/types.h>
29
30 ISC_LANG_BEGINDECLS
31
32 #define ISC_MEM_LOWATER 0
33 #define ISC_MEM_HIWATER 1
34 typedef void (*isc_mem_water_t)(void *, int);
35
36 typedef void * (*isc_memalloc_t)(void *, size_t);
37 typedef void (*isc_memfree_t)(void *, void *);
38
39 /*
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.
43  */
44 #ifndef ISC_MEM_DEBUG
45 #define ISC_MEM_DEBUG 1
46 #endif
47
48 /*
49  * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
50  * allocation and freeing by file and line number.
51  */
52 #ifndef ISC_MEM_TRACKLINES
53 #define ISC_MEM_TRACKLINES 1
54 #endif
55
56 /*
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.
59  */
60 #ifndef ISC_MEM_CHECKOVERRUN
61 #define ISC_MEM_CHECKOVERRUN 1
62 #endif
63
64 /*
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
68  * the same reasons.
69  */
70 #ifndef ISC_MEM_FILL
71 #define ISC_MEM_FILL 1
72 #endif
73
74 /*
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.
78  */
79 #ifndef ISC_MEMPOOL_NAMES
80 #define ISC_MEMPOOL_NAMES 1
81 #endif
82
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
87 /*
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:
93  *
94  * ISC_MEM_DEBUGTRACE
95  *      Log each allocation and free to isc_lctx.
96  *
97  * ISC_MEM_DEBUGRECORD
98  *      Remember each allocation, and match them up on free.
99  *      Crash if a free doesn't match an allocation.
100  *
101  * ISC_MEM_DEBUGUSAGE
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.
104  */
105
106 #if ISC_MEM_TRACKLINES
107 #define _ISC_MEM_FILELINE       , __FILE__, __LINE__
108 #define _ISC_MEM_FLARG          , const char *, int
109 #else
110 #define _ISC_MEM_FILELINE
111 #define _ISC_MEM_FLARG
112 #endif
113
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)
118
119 /*
120  * isc_mem_putanddetach() is a convienence function for use where you
121  * have a structure with an attached memory context.
122  *
123  * Given:
124  *
125  * struct {
126  *      ...
127  *      isc_mem_t *mctx;
128  *      ...
129  * } *ptr;
130  *
131  * isc_mem_t *mctx;
132  *
133  * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr));
134  *
135  * is the equivalent of:
136  *
137  * mctx = NULL;
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);
142  */
143
144 #if ISC_MEM_DEBUG
145 #define isc_mem_put(c, p, s) \
146         do { \
147                 isc__mem_put((c), (p), (s) _ISC_MEM_FILELINE); \
148                 (p) = NULL; \
149         } while (0)
150 #define isc_mem_putanddetach(c, p, s) \
151         do { \
152                 isc__mem_putanddetach((c), (p), (s) _ISC_MEM_FILELINE); \
153                 (p) = NULL; \
154         } while (0)
155 #define isc_mem_free(c, p) \
156         do { \
157                 isc__mem_free((c), (p) _ISC_MEM_FILELINE); \
158                 (p) = NULL; \
159         } while (0)
160 #define isc_mempool_put(c, p) \
161         do { \
162                 isc__mempool_put((c), (p) _ISC_MEM_FILELINE); \
163                 (p) = NULL; \
164         } while (0)
165 #else
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)
171 #endif
172
173 isc_result_t 
174 isc_mem_create(size_t max_size, size_t target_size,
175                isc_mem_t **mctxp);
176
177 isc_result_t 
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);
181 /*
182  * Create a memory context.
183  *
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
191  * ignored.
192  *
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.
196  *
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()
201  * and free().
202  *
203  * Requires:
204  * mctxp != NULL && *mctxp == NULL */
205
206 void 
207 isc_mem_attach(isc_mem_t *, isc_mem_t **);
208 void 
209 isc_mem_detach(isc_mem_t **);
210 /*
211  * Attach to / detach from a memory context.
212  *
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.
216  * 
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.
221  */
222
223 void 
224 isc_mem_destroy(isc_mem_t **);
225 /*
226  * Destroy a memory context.
227  */
228
229 isc_result_t 
230 isc_mem_ondestroy(isc_mem_t *ctx,
231                   isc_task_t *task,
232                   isc_event_t **event);
233 /*
234  * Request to be notified with an event when a memory context has
235  * been successfully destroyed.
236  */
237
238 void 
239 isc_mem_stats(isc_mem_t *mctx, FILE *out);
240 /*
241  * Print memory usage statistics for 'mctx' on the stream 'out'.
242  */
243
244 void 
245 isc_mem_setdestroycheck(isc_mem_t *mctx,
246                         isc_boolean_t on);
247 /*
248  * Iff 'on' is ISC_TRUE, 'mctx' will check for memory leaks when
249  * destroyed and abort the program if any are present.
250  */
251
252 void 
253 isc_mem_setquota(isc_mem_t *, size_t);
254 size_t 
255 isc_mem_getquota(isc_mem_t *);
256 /*
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.
260  */
261
262 size_t 
263 isc_mem_inuse(isc_mem_t *mctx);
264 /*
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.
268  */
269
270 void
271 isc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg,
272                  size_t hiwater, size_t lowater);
273 /*
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.
278  *
279  * If 'water' is NULL then 'water_arg', 'hi_water' and 'lo_water' are
280  * ignored and the state is reset.
281  *
282  * Requires:
283  *
284  *      'water' is not NULL.
285  *      hi_water >= lo_water
286  */
287
288 /*
289  * Memory pools
290  */
291
292 isc_result_t
293 isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
294 /*
295  * Create a memory pool.
296  *
297  * Requires:
298  *      mctx is a valid memory context.
299  *      size > 0
300  *      mpctxp != NULL and *mpctxp == NULL
301  *
302  * Defaults:
303  *      maxalloc = UINT_MAX
304  *      freemax = 1
305  *      fillcount = 1
306  *
307  * Returns:
308  *      ISC_R_NOMEMORY          -- not enough memory to create pool
309  *      ISC_R_SUCCESS           -- all is well.
310  */
311
312 void
313 isc_mempool_destroy(isc_mempool_t **mpctxp);
314 /*
315  * Destroy a memory pool.
316  *
317  * Requires:
318  *      mpctxp != NULL && *mpctxp is a valid pool.
319  *      The pool has no un"put" allocations outstanding
320  */
321
322 void
323 isc_mempool_setname(isc_mempool_t *mpctx, const char *name);
324 /*
325  * Associate a name with a memory pool.  At most 15 characters may be used.
326  *
327  * Requires:
328  *      mpctx is a valid pool.
329  *      name != NULL;
330  */
331
332 void
333 isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
334 /*
335  * Associate a lock with this memory pool.
336  *
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.
340  *
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.
346  *
347  * Requires:
348  *
349  *      mpctpx is a valid pool.
350  *
351  *      lock != NULL.
352  *
353  *      No previous lock is assigned to this pool.
354  *
355  *      The lock is initialized before calling this function via the normal
356  *      means of doing that.
357  */
358
359 /*
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.
363  *
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.
368  *
369  * All functions require (in addition to other requirements):
370  *      mpctx is a valid memory pool
371  */
372
373 unsigned int
374 isc_mempool_getfreemax(isc_mempool_t *mpctx);
375 /*
376  * Returns the maximum allowed size of the free list.
377  */
378
379 void
380 isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
381 /*
382  * Sets the maximum allowed size of the free list.
383  */
384
385 unsigned int
386 isc_mempool_getfreecount(isc_mempool_t *mpctx);
387 /*
388  * Returns current size of the free list.
389  */
390
391 unsigned int
392 isc_mempool_getmaxalloc(isc_mempool_t *mpctx);
393 /*
394  * Returns the maximum allowed number of allocations.
395  */
396
397 void
398 isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
399 /*
400  * Sets the maximum allowed number of allocations.
401  *
402  * Additional requirements:
403  *      limit > 0
404  */
405
406 unsigned int
407 isc_mempool_getallocated(isc_mempool_t *mpctx);
408 /*
409  * Returns the number of items allocated from this pool.
410  */
411
412 unsigned int
413 isc_mempool_getfillcount(isc_mempool_t *mpctx);
414 /*
415  * Returns the number of items allocated as a block from the parent memory
416  * context when the free list is empty.
417  */
418
419 void
420 isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
421 /*
422  * Sets the fillcount.
423  *
424  * Additional requirements:
425  *      limit > 0
426  */
427
428
429 /*
430  * Pseudo-private functions for use via macros.  Do not call directly.
431  */
432 void *          
433 isc__mem_get(isc_mem_t *, size_t _ISC_MEM_FLARG);
434 void            
435 isc__mem_putanddetach(isc_mem_t **, void *,
436                                       size_t _ISC_MEM_FLARG);
437 void            
438 isc__mem_put(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
439 void *          
440 isc__mem_allocate(isc_mem_t *, size_t _ISC_MEM_FLARG);
441 void            
442 isc__mem_free(isc_mem_t *, void * _ISC_MEM_FLARG);
443 char *          
444 isc__mem_strdup(isc_mem_t *, const char *_ISC_MEM_FLARG);
445 void *          
446 isc__mempool_get(isc_mempool_t * _ISC_MEM_FLARG);
447 void            
448 isc__mempool_put(isc_mempool_t *, void * _ISC_MEM_FLARG);
449
450 ISC_LANG_ENDDECLS
451
452 #endif /* ISC_MEM_H */