xref: /aosp_15_r20/external/dlmalloc/dlmalloc.h (revision 2680e0c0bdff5fc86b0432efeeb5c26d7a2d8c83)
1*2680e0c0SChristopher Ferris /*
2*2680e0c0SChristopher Ferris   Default header file for malloc-2.8.x, written by Doug Lea
3*2680e0c0SChristopher Ferris   and released to the public domain, as explained at
4*2680e0c0SChristopher Ferris   http://creativecommons.org/publicdomain/zero/1.0/
5*2680e0c0SChristopher Ferris 
6*2680e0c0SChristopher Ferris   This header is for ANSI C/C++ only.  You can set any of
7*2680e0c0SChristopher Ferris   the following #defines before including:
8*2680e0c0SChristopher Ferris 
9*2680e0c0SChristopher Ferris   * If USE_DL_PREFIX is defined, it is assumed that malloc.c
10*2680e0c0SChristopher Ferris     was also compiled with this option, so all routines
11*2680e0c0SChristopher Ferris     have names starting with "dl".
12*2680e0c0SChristopher Ferris 
13*2680e0c0SChristopher Ferris   * If HAVE_USR_INCLUDE_MALLOC_H is defined, it is assumed that this
14*2680e0c0SChristopher Ferris     file will be #included AFTER <malloc.h>. This is needed only if
15*2680e0c0SChristopher Ferris     your system defines a struct mallinfo that is incompatible with the
16*2680e0c0SChristopher Ferris     standard one declared here.  Otherwise, you can include this file
17*2680e0c0SChristopher Ferris     INSTEAD of your system system <malloc.h>.  At least on ANSI, all
18*2680e0c0SChristopher Ferris     declarations should be compatible with system versions
19*2680e0c0SChristopher Ferris 
20*2680e0c0SChristopher Ferris   * If MSPACES is defined, declarations for mspace versions are included.
21*2680e0c0SChristopher Ferris */
22*2680e0c0SChristopher Ferris 
23*2680e0c0SChristopher Ferris #ifndef MALLOC_280_H
24*2680e0c0SChristopher Ferris #define MALLOC_280_H
25*2680e0c0SChristopher Ferris 
26*2680e0c0SChristopher Ferris #ifdef __cplusplus
27*2680e0c0SChristopher Ferris extern "C" {
28*2680e0c0SChristopher Ferris #endif
29*2680e0c0SChristopher Ferris 
30*2680e0c0SChristopher Ferris #include <stddef.h>   /* for size_t */
31*2680e0c0SChristopher Ferris 
32*2680e0c0SChristopher Ferris #ifndef ONLY_MSPACES
33*2680e0c0SChristopher Ferris #define ONLY_MSPACES 0     /* define to a value */
34*2680e0c0SChristopher Ferris #elif ONLY_MSPACES != 0
35*2680e0c0SChristopher Ferris #define ONLY_MSPACES 1
36*2680e0c0SChristopher Ferris #endif  /* ONLY_MSPACES */
37*2680e0c0SChristopher Ferris #ifndef NO_MALLINFO
38*2680e0c0SChristopher Ferris #define NO_MALLINFO 0
39*2680e0c0SChristopher Ferris #endif  /* NO_MALLINFO */
40*2680e0c0SChristopher Ferris 
41*2680e0c0SChristopher Ferris #ifndef MSPACES
42*2680e0c0SChristopher Ferris #if ONLY_MSPACES
43*2680e0c0SChristopher Ferris #define MSPACES 1
44*2680e0c0SChristopher Ferris #else   /* ONLY_MSPACES */
45*2680e0c0SChristopher Ferris #define MSPACES 0
46*2680e0c0SChristopher Ferris #endif  /* ONLY_MSPACES */
47*2680e0c0SChristopher Ferris #endif  /* MSPACES */
48*2680e0c0SChristopher Ferris 
49*2680e0c0SChristopher Ferris #if !ONLY_MSPACES
50*2680e0c0SChristopher Ferris 
51*2680e0c0SChristopher Ferris #ifndef USE_DL_PREFIX
52*2680e0c0SChristopher Ferris #define dlcalloc               calloc
53*2680e0c0SChristopher Ferris #define dlfree                 free
54*2680e0c0SChristopher Ferris #define dlmalloc               malloc
55*2680e0c0SChristopher Ferris #define dlmemalign             memalign
56*2680e0c0SChristopher Ferris #define dlposix_memalign       posix_memalign
57*2680e0c0SChristopher Ferris #define dlrealloc              realloc
58*2680e0c0SChristopher Ferris #define dlvalloc               valloc
59*2680e0c0SChristopher Ferris #define dlpvalloc              pvalloc
60*2680e0c0SChristopher Ferris #define dlmallinfo             mallinfo
61*2680e0c0SChristopher Ferris #define dlmallopt              mallopt
62*2680e0c0SChristopher Ferris #define dlmalloc_trim          malloc_trim
63*2680e0c0SChristopher Ferris #define dlmalloc_stats         malloc_stats
64*2680e0c0SChristopher Ferris #define dlmalloc_usable_size   malloc_usable_size
65*2680e0c0SChristopher Ferris #define dlmalloc_footprint     malloc_footprint
66*2680e0c0SChristopher Ferris #define dlmalloc_max_footprint malloc_max_footprint
67*2680e0c0SChristopher Ferris #define dlmalloc_footprint_limit malloc_footprint_limit
68*2680e0c0SChristopher Ferris #define dlmalloc_set_footprint_limit malloc_set_footprint_limit
69*2680e0c0SChristopher Ferris #define dlmalloc_inspect_all   malloc_inspect_all
70*2680e0c0SChristopher Ferris #define dlindependent_calloc   independent_calloc
71*2680e0c0SChristopher Ferris #define dlindependent_comalloc independent_comalloc
72*2680e0c0SChristopher Ferris #define dlbulk_free            bulk_free
73*2680e0c0SChristopher Ferris #endif /* USE_DL_PREFIX */
74*2680e0c0SChristopher Ferris 
75*2680e0c0SChristopher Ferris #if !NO_MALLINFO
76*2680e0c0SChristopher Ferris #ifndef HAVE_USR_INCLUDE_MALLOC_H
77*2680e0c0SChristopher Ferris #ifndef _MALLOC_H
78*2680e0c0SChristopher Ferris #ifndef MALLINFO_FIELD_TYPE
79*2680e0c0SChristopher Ferris #define MALLINFO_FIELD_TYPE size_t
80*2680e0c0SChristopher Ferris #endif /* MALLINFO_FIELD_TYPE */
81*2680e0c0SChristopher Ferris #ifndef STRUCT_MALLINFO_DECLARED
82*2680e0c0SChristopher Ferris #define STRUCT_MALLINFO_DECLARED 1
83*2680e0c0SChristopher Ferris struct mallinfo {
84*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE arena;    /* non-mmapped space allocated from system */
85*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE ordblks;  /* number of free chunks */
86*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE smblks;   /* always 0 */
87*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE hblks;    /* always 0 */
88*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE hblkhd;   /* space in mmapped regions */
89*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE usmblks;  /* maximum total allocated space */
90*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE fsmblks;  /* always 0 */
91*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
92*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE fordblks; /* total free space */
93*2680e0c0SChristopher Ferris   MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
94*2680e0c0SChristopher Ferris };
95*2680e0c0SChristopher Ferris #endif /* STRUCT_MALLINFO_DECLARED */
96*2680e0c0SChristopher Ferris #endif  /* _MALLOC_H */
97*2680e0c0SChristopher Ferris #endif  /* HAVE_USR_INCLUDE_MALLOC_H */
98*2680e0c0SChristopher Ferris #endif  /* !NO_MALLINFO */
99*2680e0c0SChristopher Ferris 
100*2680e0c0SChristopher Ferris /*
101*2680e0c0SChristopher Ferris   malloc(size_t n)
102*2680e0c0SChristopher Ferris   Returns a pointer to a newly allocated chunk of at least n bytes, or
103*2680e0c0SChristopher Ferris   null if no space is available, in which case errno is set to ENOMEM
104*2680e0c0SChristopher Ferris   on ANSI C systems.
105*2680e0c0SChristopher Ferris 
106*2680e0c0SChristopher Ferris   If n is zero, malloc returns a minimum-sized chunk. (The minimum
107*2680e0c0SChristopher Ferris   size is 16 bytes on most 32bit systems, and 32 bytes on 64bit
108*2680e0c0SChristopher Ferris   systems.)  Note that size_t is an unsigned type, so calls with
109*2680e0c0SChristopher Ferris   arguments that would be negative if signed are interpreted as
110*2680e0c0SChristopher Ferris   requests for huge amounts of space, which will often fail. The
111*2680e0c0SChristopher Ferris   maximum supported value of n differs across systems, but is in all
112*2680e0c0SChristopher Ferris   cases less than the maximum representable value of a size_t.
113*2680e0c0SChristopher Ferris */
114*2680e0c0SChristopher Ferris void* dlmalloc(size_t);
115*2680e0c0SChristopher Ferris 
116*2680e0c0SChristopher Ferris /*
117*2680e0c0SChristopher Ferris   free(void* p)
118*2680e0c0SChristopher Ferris   Releases the chunk of memory pointed to by p, that had been previously
119*2680e0c0SChristopher Ferris   allocated using malloc or a related routine such as realloc.
120*2680e0c0SChristopher Ferris   It has no effect if p is null. If p was not malloced or already
121*2680e0c0SChristopher Ferris   freed, free(p) will by default cuase the current program to abort.
122*2680e0c0SChristopher Ferris */
123*2680e0c0SChristopher Ferris void  dlfree(void*);
124*2680e0c0SChristopher Ferris 
125*2680e0c0SChristopher Ferris /*
126*2680e0c0SChristopher Ferris   calloc(size_t n_elements, size_t element_size);
127*2680e0c0SChristopher Ferris   Returns a pointer to n_elements * element_size bytes, with all locations
128*2680e0c0SChristopher Ferris   set to zero.
129*2680e0c0SChristopher Ferris */
130*2680e0c0SChristopher Ferris void* dlcalloc(size_t, size_t);
131*2680e0c0SChristopher Ferris 
132*2680e0c0SChristopher Ferris /*
133*2680e0c0SChristopher Ferris   realloc(void* p, size_t n)
134*2680e0c0SChristopher Ferris   Returns a pointer to a chunk of size n that contains the same data
135*2680e0c0SChristopher Ferris   as does chunk p up to the minimum of (n, p's size) bytes, or null
136*2680e0c0SChristopher Ferris   if no space is available.
137*2680e0c0SChristopher Ferris 
138*2680e0c0SChristopher Ferris   The returned pointer may or may not be the same as p. The algorithm
139*2680e0c0SChristopher Ferris   prefers extending p in most cases when possible, otherwise it
140*2680e0c0SChristopher Ferris   employs the equivalent of a malloc-copy-free sequence.
141*2680e0c0SChristopher Ferris 
142*2680e0c0SChristopher Ferris   If p is null, realloc is equivalent to malloc.
143*2680e0c0SChristopher Ferris 
144*2680e0c0SChristopher Ferris   If space is not available, realloc returns null, errno is set (if on
145*2680e0c0SChristopher Ferris   ANSI) and p is NOT freed.
146*2680e0c0SChristopher Ferris 
147*2680e0c0SChristopher Ferris   if n is for fewer bytes than already held by p, the newly unused
148*2680e0c0SChristopher Ferris   space is lopped off and freed if possible.  realloc with a size
149*2680e0c0SChristopher Ferris   argument of zero (re)allocates a minimum-sized chunk.
150*2680e0c0SChristopher Ferris 
151*2680e0c0SChristopher Ferris   The old unix realloc convention of allowing the last-free'd chunk
152*2680e0c0SChristopher Ferris   to be used as an argument to realloc is not supported.
153*2680e0c0SChristopher Ferris */
154*2680e0c0SChristopher Ferris void* dlrealloc(void*, size_t);
155*2680e0c0SChristopher Ferris 
156*2680e0c0SChristopher Ferris /*
157*2680e0c0SChristopher Ferris   realloc_in_place(void* p, size_t n)
158*2680e0c0SChristopher Ferris   Resizes the space allocated for p to size n, only if this can be
159*2680e0c0SChristopher Ferris   done without moving p (i.e., only if there is adjacent space
160*2680e0c0SChristopher Ferris   available if n is greater than p's current allocated size, or n is
161*2680e0c0SChristopher Ferris   less than or equal to p's size). This may be used instead of plain
162*2680e0c0SChristopher Ferris   realloc if an alternative allocation strategy is needed upon failure
163*2680e0c0SChristopher Ferris   to expand space; for example, reallocation of a buffer that must be
164*2680e0c0SChristopher Ferris   memory-aligned or cleared. You can use realloc_in_place to trigger
165*2680e0c0SChristopher Ferris   these alternatives only when needed.
166*2680e0c0SChristopher Ferris 
167*2680e0c0SChristopher Ferris   Returns p if successful; otherwise null.
168*2680e0c0SChristopher Ferris */
169*2680e0c0SChristopher Ferris void* dlrealloc_in_place(void*, size_t);
170*2680e0c0SChristopher Ferris 
171*2680e0c0SChristopher Ferris /*
172*2680e0c0SChristopher Ferris   memalign(size_t alignment, size_t n);
173*2680e0c0SChristopher Ferris   Returns a pointer to a newly allocated chunk of n bytes, aligned
174*2680e0c0SChristopher Ferris   in accord with the alignment argument.
175*2680e0c0SChristopher Ferris 
176*2680e0c0SChristopher Ferris   The alignment argument should be a power of two. If the argument is
177*2680e0c0SChristopher Ferris   not a power of two, the nearest greater power is used.
178*2680e0c0SChristopher Ferris   8-byte alignment is guaranteed by normal malloc calls, so don't
179*2680e0c0SChristopher Ferris   bother calling memalign with an argument of 8 or less.
180*2680e0c0SChristopher Ferris 
181*2680e0c0SChristopher Ferris   Overreliance on memalign is a sure way to fragment space.
182*2680e0c0SChristopher Ferris */
183*2680e0c0SChristopher Ferris void* dlmemalign(size_t, size_t);
184*2680e0c0SChristopher Ferris 
185*2680e0c0SChristopher Ferris /*
186*2680e0c0SChristopher Ferris   int posix_memalign(void** pp, size_t alignment, size_t n);
187*2680e0c0SChristopher Ferris   Allocates a chunk of n bytes, aligned in accord with the alignment
188*2680e0c0SChristopher Ferris   argument. Differs from memalign only in that it (1) assigns the
189*2680e0c0SChristopher Ferris   allocated memory to *pp rather than returning it, (2) fails and
190*2680e0c0SChristopher Ferris   returns EINVAL if the alignment is not a power of two (3) fails and
191*2680e0c0SChristopher Ferris   returns ENOMEM if memory cannot be allocated.
192*2680e0c0SChristopher Ferris */
193*2680e0c0SChristopher Ferris int dlposix_memalign(void**, size_t, size_t);
194*2680e0c0SChristopher Ferris 
195*2680e0c0SChristopher Ferris /*
196*2680e0c0SChristopher Ferris   valloc(size_t n);
197*2680e0c0SChristopher Ferris   Equivalent to memalign(pagesize, n), where pagesize is the page
198*2680e0c0SChristopher Ferris   size of the system. If the pagesize is unknown, 4096 is used.
199*2680e0c0SChristopher Ferris */
200*2680e0c0SChristopher Ferris void* dlvalloc(size_t);
201*2680e0c0SChristopher Ferris 
202*2680e0c0SChristopher Ferris /*
203*2680e0c0SChristopher Ferris   mallopt(int parameter_number, int parameter_value)
204*2680e0c0SChristopher Ferris   Sets tunable parameters The format is to provide a
205*2680e0c0SChristopher Ferris   (parameter-number, parameter-value) pair.  mallopt then sets the
206*2680e0c0SChristopher Ferris   corresponding parameter to the argument value if it can (i.e., so
207*2680e0c0SChristopher Ferris   long as the value is meaningful), and returns 1 if successful else
208*2680e0c0SChristopher Ferris   0.  SVID/XPG/ANSI defines four standard param numbers for mallopt,
209*2680e0c0SChristopher Ferris   normally defined in malloc.h.  None of these are use in this malloc,
210*2680e0c0SChristopher Ferris   so setting them has no effect. But this malloc also supports other
211*2680e0c0SChristopher Ferris   options in mallopt:
212*2680e0c0SChristopher Ferris 
213*2680e0c0SChristopher Ferris   Symbol            param #  default    allowed param values
214*2680e0c0SChristopher Ferris   M_TRIM_THRESHOLD     -1   2*1024*1024   any   (-1U disables trimming)
215*2680e0c0SChristopher Ferris   M_GRANULARITY        -2     page size   any power of 2 >= page size
216*2680e0c0SChristopher Ferris   M_MMAP_THRESHOLD     -3      256*1024   any   (or 0 if no MMAP support)
217*2680e0c0SChristopher Ferris */
218*2680e0c0SChristopher Ferris int dlmallopt(int, int);
219*2680e0c0SChristopher Ferris 
220*2680e0c0SChristopher Ferris #define M_TRIM_THRESHOLD     (-1)
221*2680e0c0SChristopher Ferris #define M_GRANULARITY        (-2)
222*2680e0c0SChristopher Ferris #define M_MMAP_THRESHOLD     (-3)
223*2680e0c0SChristopher Ferris 
224*2680e0c0SChristopher Ferris 
225*2680e0c0SChristopher Ferris /*
226*2680e0c0SChristopher Ferris   malloc_footprint();
227*2680e0c0SChristopher Ferris   Returns the number of bytes obtained from the system.  The total
228*2680e0c0SChristopher Ferris   number of bytes allocated by malloc, realloc etc., is less than this
229*2680e0c0SChristopher Ferris   value. Unlike mallinfo, this function returns only a precomputed
230*2680e0c0SChristopher Ferris   result, so can be called frequently to monitor memory consumption.
231*2680e0c0SChristopher Ferris   Even if locks are otherwise defined, this function does not use them,
232*2680e0c0SChristopher Ferris   so results might not be up to date.
233*2680e0c0SChristopher Ferris */
234*2680e0c0SChristopher Ferris size_t dlmalloc_footprint(void);
235*2680e0c0SChristopher Ferris 
236*2680e0c0SChristopher Ferris /*
237*2680e0c0SChristopher Ferris   malloc_max_footprint();
238*2680e0c0SChristopher Ferris   Returns the maximum number of bytes obtained from the system. This
239*2680e0c0SChristopher Ferris   value will be greater than current footprint if deallocated space
240*2680e0c0SChristopher Ferris   has been reclaimed by the system. The peak number of bytes allocated
241*2680e0c0SChristopher Ferris   by malloc, realloc etc., is less than this value. Unlike mallinfo,
242*2680e0c0SChristopher Ferris   this function returns only a precomputed result, so can be called
243*2680e0c0SChristopher Ferris   frequently to monitor memory consumption.  Even if locks are
244*2680e0c0SChristopher Ferris   otherwise defined, this function does not use them, so results might
245*2680e0c0SChristopher Ferris   not be up to date.
246*2680e0c0SChristopher Ferris */
247*2680e0c0SChristopher Ferris size_t dlmalloc_max_footprint(void);
248*2680e0c0SChristopher Ferris 
249*2680e0c0SChristopher Ferris /*
250*2680e0c0SChristopher Ferris   malloc_footprint_limit();
251*2680e0c0SChristopher Ferris   Returns the number of bytes that the heap is allowed to obtain from
252*2680e0c0SChristopher Ferris   the system, returning the last value returned by
253*2680e0c0SChristopher Ferris   malloc_set_footprint_limit, or the maximum size_t value if
254*2680e0c0SChristopher Ferris   never set. The returned value reflects a permission. There is no
255*2680e0c0SChristopher Ferris   guarantee that this number of bytes can actually be obtained from
256*2680e0c0SChristopher Ferris   the system.
257*2680e0c0SChristopher Ferris */
258*2680e0c0SChristopher Ferris size_t dlmalloc_footprint_limit(void);
259*2680e0c0SChristopher Ferris 
260*2680e0c0SChristopher Ferris /*
261*2680e0c0SChristopher Ferris   malloc_set_footprint_limit();
262*2680e0c0SChristopher Ferris   Sets the maximum number of bytes to obtain from the system, causing
263*2680e0c0SChristopher Ferris   failure returns from malloc and related functions upon attempts to
264*2680e0c0SChristopher Ferris   exceed this value. The argument value may be subject to page
265*2680e0c0SChristopher Ferris   rounding to an enforceable limit; this actual value is returned.
266*2680e0c0SChristopher Ferris   Using an argument of the maximum possible size_t effectively
267*2680e0c0SChristopher Ferris   disables checks. If the argument is less than or equal to the
268*2680e0c0SChristopher Ferris   current malloc_footprint, then all future allocations that require
269*2680e0c0SChristopher Ferris   additional system memory will fail. However, invocation cannot
270*2680e0c0SChristopher Ferris   retroactively deallocate existing used memory.
271*2680e0c0SChristopher Ferris */
272*2680e0c0SChristopher Ferris size_t dlmalloc_set_footprint_limit(size_t bytes);
273*2680e0c0SChristopher Ferris 
274*2680e0c0SChristopher Ferris /*
275*2680e0c0SChristopher Ferris   malloc_inspect_all(void(*handler)(void *start,
276*2680e0c0SChristopher Ferris                                     void *end,
277*2680e0c0SChristopher Ferris                                     size_t used_bytes,
278*2680e0c0SChristopher Ferris                                     void* callback_arg),
279*2680e0c0SChristopher Ferris                       void* arg);
280*2680e0c0SChristopher Ferris   Traverses the heap and calls the given handler for each managed
281*2680e0c0SChristopher Ferris   region, skipping all bytes that are (or may be) used for bookkeeping
282*2680e0c0SChristopher Ferris   purposes.  Traversal does not include include chunks that have been
283*2680e0c0SChristopher Ferris   directly memory mapped. Each reported region begins at the start
284*2680e0c0SChristopher Ferris   address, and continues up to but not including the end address.  The
285*2680e0c0SChristopher Ferris   first used_bytes of the region contain allocated data. If
286*2680e0c0SChristopher Ferris   used_bytes is zero, the region is unallocated. The handler is
287*2680e0c0SChristopher Ferris   invoked with the given callback argument. If locks are defined, they
288*2680e0c0SChristopher Ferris   are held during the entire traversal. It is a bad idea to invoke
289*2680e0c0SChristopher Ferris   other malloc functions from within the handler.
290*2680e0c0SChristopher Ferris 
291*2680e0c0SChristopher Ferris   For example, to count the number of in-use chunks with size greater
292*2680e0c0SChristopher Ferris   than 1000, you could write:
293*2680e0c0SChristopher Ferris   static int count = 0;
294*2680e0c0SChristopher Ferris   void count_chunks(void* start, void* end, size_t used, void* arg) {
295*2680e0c0SChristopher Ferris     if (used >= 1000) ++count;
296*2680e0c0SChristopher Ferris   }
297*2680e0c0SChristopher Ferris   then:
298*2680e0c0SChristopher Ferris     malloc_inspect_all(count_chunks, NULL);
299*2680e0c0SChristopher Ferris 
300*2680e0c0SChristopher Ferris   malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined.
301*2680e0c0SChristopher Ferris */
302*2680e0c0SChristopher Ferris void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*),
303*2680e0c0SChristopher Ferris                            void* arg);
304*2680e0c0SChristopher Ferris 
305*2680e0c0SChristopher Ferris #if !NO_MALLINFO
306*2680e0c0SChristopher Ferris /*
307*2680e0c0SChristopher Ferris   mallinfo()
308*2680e0c0SChristopher Ferris   Returns (by copy) a struct containing various summary statistics:
309*2680e0c0SChristopher Ferris 
310*2680e0c0SChristopher Ferris   arena:     current total non-mmapped bytes allocated from system
311*2680e0c0SChristopher Ferris   ordblks:   the number of free chunks
312*2680e0c0SChristopher Ferris   smblks:    always zero.
313*2680e0c0SChristopher Ferris   hblks:     current number of mmapped regions
314*2680e0c0SChristopher Ferris   hblkhd:    total bytes held in mmapped regions
315*2680e0c0SChristopher Ferris   usmblks:   the maximum total allocated space. This will be greater
316*2680e0c0SChristopher Ferris                 than current total if trimming has occurred.
317*2680e0c0SChristopher Ferris   fsmblks:   always zero
318*2680e0c0SChristopher Ferris   uordblks:  current total allocated space (normal or mmapped)
319*2680e0c0SChristopher Ferris   fordblks:  total free space
320*2680e0c0SChristopher Ferris   keepcost:  the maximum number of bytes that could ideally be released
321*2680e0c0SChristopher Ferris                back to system via malloc_trim. ("ideally" means that
322*2680e0c0SChristopher Ferris                it ignores page restrictions etc.)
323*2680e0c0SChristopher Ferris 
324*2680e0c0SChristopher Ferris   Because these fields are ints, but internal bookkeeping may
325*2680e0c0SChristopher Ferris   be kept as longs, the reported values may wrap around zero and
326*2680e0c0SChristopher Ferris   thus be inaccurate.
327*2680e0c0SChristopher Ferris */
328*2680e0c0SChristopher Ferris 
329*2680e0c0SChristopher Ferris struct mallinfo dlmallinfo(void);
330*2680e0c0SChristopher Ferris #endif  /* NO_MALLINFO */
331*2680e0c0SChristopher Ferris 
332*2680e0c0SChristopher Ferris /*
333*2680e0c0SChristopher Ferris   independent_calloc(size_t n_elements, size_t element_size, void* chunks[]);
334*2680e0c0SChristopher Ferris 
335*2680e0c0SChristopher Ferris   independent_calloc is similar to calloc, but instead of returning a
336*2680e0c0SChristopher Ferris   single cleared space, it returns an array of pointers to n_elements
337*2680e0c0SChristopher Ferris   independent elements that can hold contents of size elem_size, each
338*2680e0c0SChristopher Ferris   of which starts out cleared, and can be independently freed,
339*2680e0c0SChristopher Ferris   realloc'ed etc. The elements are guaranteed to be adjacently
340*2680e0c0SChristopher Ferris   allocated (this is not guaranteed to occur with multiple callocs or
341*2680e0c0SChristopher Ferris   mallocs), which may also improve cache locality in some
342*2680e0c0SChristopher Ferris   applications.
343*2680e0c0SChristopher Ferris 
344*2680e0c0SChristopher Ferris   The "chunks" argument is optional (i.e., may be null, which is
345*2680e0c0SChristopher Ferris   probably the most typical usage). If it is null, the returned array
346*2680e0c0SChristopher Ferris   is itself dynamically allocated and should also be freed when it is
347*2680e0c0SChristopher Ferris   no longer needed. Otherwise, the chunks array must be of at least
348*2680e0c0SChristopher Ferris   n_elements in length. It is filled in with the pointers to the
349*2680e0c0SChristopher Ferris   chunks.
350*2680e0c0SChristopher Ferris 
351*2680e0c0SChristopher Ferris   In either case, independent_calloc returns this pointer array, or
352*2680e0c0SChristopher Ferris   null if the allocation failed.  If n_elements is zero and "chunks"
353*2680e0c0SChristopher Ferris   is null, it returns a chunk representing an array with zero elements
354*2680e0c0SChristopher Ferris   (which should be freed if not wanted).
355*2680e0c0SChristopher Ferris 
356*2680e0c0SChristopher Ferris   Each element must be freed when it is no longer needed. This can be
357*2680e0c0SChristopher Ferris   done all at once using bulk_free.
358*2680e0c0SChristopher Ferris 
359*2680e0c0SChristopher Ferris   independent_calloc simplifies and speeds up implementations of many
360*2680e0c0SChristopher Ferris   kinds of pools.  It may also be useful when constructing large data
361*2680e0c0SChristopher Ferris   structures that initially have a fixed number of fixed-sized nodes,
362*2680e0c0SChristopher Ferris   but the number is not known at compile time, and some of the nodes
363*2680e0c0SChristopher Ferris   may later need to be freed. For example:
364*2680e0c0SChristopher Ferris 
365*2680e0c0SChristopher Ferris   struct Node { int item; struct Node* next; };
366*2680e0c0SChristopher Ferris 
367*2680e0c0SChristopher Ferris   struct Node* build_list() {
368*2680e0c0SChristopher Ferris     struct Node** pool;
369*2680e0c0SChristopher Ferris     int n = read_number_of_nodes_needed();
370*2680e0c0SChristopher Ferris     if (n <= 0) return 0;
371*2680e0c0SChristopher Ferris     pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
372*2680e0c0SChristopher Ferris     if (pool == 0) die();
373*2680e0c0SChristopher Ferris     // organize into a linked list...
374*2680e0c0SChristopher Ferris     struct Node* first = pool[0];
375*2680e0c0SChristopher Ferris     for (i = 0; i < n-1; ++i)
376*2680e0c0SChristopher Ferris       pool[i]->next = pool[i+1];
377*2680e0c0SChristopher Ferris     free(pool);     // Can now free the array (or not, if it is needed later)
378*2680e0c0SChristopher Ferris     return first;
379*2680e0c0SChristopher Ferris   }
380*2680e0c0SChristopher Ferris */
381*2680e0c0SChristopher Ferris void** dlindependent_calloc(size_t, size_t, void**);
382*2680e0c0SChristopher Ferris 
383*2680e0c0SChristopher Ferris /*
384*2680e0c0SChristopher Ferris   independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);
385*2680e0c0SChristopher Ferris 
386*2680e0c0SChristopher Ferris   independent_comalloc allocates, all at once, a set of n_elements
387*2680e0c0SChristopher Ferris   chunks with sizes indicated in the "sizes" array.    It returns
388*2680e0c0SChristopher Ferris   an array of pointers to these elements, each of which can be
389*2680e0c0SChristopher Ferris   independently freed, realloc'ed etc. The elements are guaranteed to
390*2680e0c0SChristopher Ferris   be adjacently allocated (this is not guaranteed to occur with
391*2680e0c0SChristopher Ferris   multiple callocs or mallocs), which may also improve cache locality
392*2680e0c0SChristopher Ferris   in some applications.
393*2680e0c0SChristopher Ferris 
394*2680e0c0SChristopher Ferris   The "chunks" argument is optional (i.e., may be null). If it is null
395*2680e0c0SChristopher Ferris   the returned array is itself dynamically allocated and should also
396*2680e0c0SChristopher Ferris   be freed when it is no longer needed. Otherwise, the chunks array
397*2680e0c0SChristopher Ferris   must be of at least n_elements in length. It is filled in with the
398*2680e0c0SChristopher Ferris   pointers to the chunks.
399*2680e0c0SChristopher Ferris 
400*2680e0c0SChristopher Ferris   In either case, independent_comalloc returns this pointer array, or
401*2680e0c0SChristopher Ferris   null if the allocation failed.  If n_elements is zero and chunks is
402*2680e0c0SChristopher Ferris   null, it returns a chunk representing an array with zero elements
403*2680e0c0SChristopher Ferris   (which should be freed if not wanted).
404*2680e0c0SChristopher Ferris 
405*2680e0c0SChristopher Ferris   Each element must be freed when it is no longer needed. This can be
406*2680e0c0SChristopher Ferris   done all at once using bulk_free.
407*2680e0c0SChristopher Ferris 
408*2680e0c0SChristopher Ferris   independent_comallac differs from independent_calloc in that each
409*2680e0c0SChristopher Ferris   element may have a different size, and also that it does not
410*2680e0c0SChristopher Ferris   automatically clear elements.
411*2680e0c0SChristopher Ferris 
412*2680e0c0SChristopher Ferris   independent_comalloc can be used to speed up allocation in cases
413*2680e0c0SChristopher Ferris   where several structs or objects must always be allocated at the
414*2680e0c0SChristopher Ferris   same time.  For example:
415*2680e0c0SChristopher Ferris 
416*2680e0c0SChristopher Ferris   struct Head { ... }
417*2680e0c0SChristopher Ferris   struct Foot { ... }
418*2680e0c0SChristopher Ferris 
419*2680e0c0SChristopher Ferris   void send_message(char* msg) {
420*2680e0c0SChristopher Ferris     int msglen = strlen(msg);
421*2680e0c0SChristopher Ferris     size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
422*2680e0c0SChristopher Ferris     void* chunks[3];
423*2680e0c0SChristopher Ferris     if (independent_comalloc(3, sizes, chunks) == 0)
424*2680e0c0SChristopher Ferris       die();
425*2680e0c0SChristopher Ferris     struct Head* head = (struct Head*)(chunks[0]);
426*2680e0c0SChristopher Ferris     char*        body = (char*)(chunks[1]);
427*2680e0c0SChristopher Ferris     struct Foot* foot = (struct Foot*)(chunks[2]);
428*2680e0c0SChristopher Ferris     // ...
429*2680e0c0SChristopher Ferris   }
430*2680e0c0SChristopher Ferris 
431*2680e0c0SChristopher Ferris   In general though, independent_comalloc is worth using only for
432*2680e0c0SChristopher Ferris   larger values of n_elements. For small values, you probably won't
433*2680e0c0SChristopher Ferris   detect enough difference from series of malloc calls to bother.
434*2680e0c0SChristopher Ferris 
435*2680e0c0SChristopher Ferris   Overuse of independent_comalloc can increase overall memory usage,
436*2680e0c0SChristopher Ferris   since it cannot reuse existing noncontiguous small chunks that
437*2680e0c0SChristopher Ferris   might be available for some of the elements.
438*2680e0c0SChristopher Ferris */
439*2680e0c0SChristopher Ferris void** dlindependent_comalloc(size_t, size_t*, void**);
440*2680e0c0SChristopher Ferris 
441*2680e0c0SChristopher Ferris /*
442*2680e0c0SChristopher Ferris   bulk_free(void* array[], size_t n_elements)
443*2680e0c0SChristopher Ferris   Frees and clears (sets to null) each non-null pointer in the given
444*2680e0c0SChristopher Ferris   array.  This is likely to be faster than freeing them one-by-one.
445*2680e0c0SChristopher Ferris   If footers are used, pointers that have been allocated in different
446*2680e0c0SChristopher Ferris   mspaces are not freed or cleared, and the count of all such pointers
447*2680e0c0SChristopher Ferris   is returned.  For large arrays of pointers with poor locality, it
448*2680e0c0SChristopher Ferris   may be worthwhile to sort this array before calling bulk_free.
449*2680e0c0SChristopher Ferris */
450*2680e0c0SChristopher Ferris size_t  dlbulk_free(void**, size_t n_elements);
451*2680e0c0SChristopher Ferris 
452*2680e0c0SChristopher Ferris /*
453*2680e0c0SChristopher Ferris   pvalloc(size_t n);
454*2680e0c0SChristopher Ferris   Equivalent to valloc(minimum-page-that-holds(n)), that is,
455*2680e0c0SChristopher Ferris   round up n to nearest pagesize.
456*2680e0c0SChristopher Ferris  */
457*2680e0c0SChristopher Ferris void*  dlpvalloc(size_t);
458*2680e0c0SChristopher Ferris 
459*2680e0c0SChristopher Ferris /*
460*2680e0c0SChristopher Ferris   malloc_trim(size_t pad);
461*2680e0c0SChristopher Ferris 
462*2680e0c0SChristopher Ferris   If possible, gives memory back to the system (via negative arguments
463*2680e0c0SChristopher Ferris   to sbrk) if there is unused memory at the `high' end of the malloc
464*2680e0c0SChristopher Ferris   pool or in unused MMAP segments. You can call this after freeing
465*2680e0c0SChristopher Ferris   large blocks of memory to potentially reduce the system-level memory
466*2680e0c0SChristopher Ferris   requirements of a program. However, it cannot guarantee to reduce
467*2680e0c0SChristopher Ferris   memory. Under some allocation patterns, some large free blocks of
468*2680e0c0SChristopher Ferris   memory will be locked between two used chunks, so they cannot be
469*2680e0c0SChristopher Ferris   given back to the system.
470*2680e0c0SChristopher Ferris 
471*2680e0c0SChristopher Ferris   The `pad' argument to malloc_trim represents the amount of free
472*2680e0c0SChristopher Ferris   trailing space to leave untrimmed. If this argument is zero, only
473*2680e0c0SChristopher Ferris   the minimum amount of memory to maintain internal data structures
474*2680e0c0SChristopher Ferris   will be left. Non-zero arguments can be supplied to maintain enough
475*2680e0c0SChristopher Ferris   trailing space to service future expected allocations without having
476*2680e0c0SChristopher Ferris   to re-obtain memory from the system.
477*2680e0c0SChristopher Ferris 
478*2680e0c0SChristopher Ferris   Malloc_trim returns 1 if it actually released any memory, else 0.
479*2680e0c0SChristopher Ferris */
480*2680e0c0SChristopher Ferris int  dlmalloc_trim(size_t);
481*2680e0c0SChristopher Ferris 
482*2680e0c0SChristopher Ferris /*
483*2680e0c0SChristopher Ferris   malloc_stats();
484*2680e0c0SChristopher Ferris   Prints on stderr the amount of space obtained from the system (both
485*2680e0c0SChristopher Ferris   via sbrk and mmap), the maximum amount (which may be more than
486*2680e0c0SChristopher Ferris   current if malloc_trim and/or munmap got called), and the current
487*2680e0c0SChristopher Ferris   number of bytes allocated via malloc (or realloc, etc) but not yet
488*2680e0c0SChristopher Ferris   freed. Note that this is the number of bytes allocated, not the
489*2680e0c0SChristopher Ferris   number requested. It will be larger than the number requested
490*2680e0c0SChristopher Ferris   because of alignment and bookkeeping overhead. Because it includes
491*2680e0c0SChristopher Ferris   alignment wastage as being in use, this figure may be greater than
492*2680e0c0SChristopher Ferris   zero even when no user-level chunks are allocated.
493*2680e0c0SChristopher Ferris 
494*2680e0c0SChristopher Ferris   The reported current and maximum system memory can be inaccurate if
495*2680e0c0SChristopher Ferris   a program makes other calls to system memory allocation functions
496*2680e0c0SChristopher Ferris   (normally sbrk) outside of malloc.
497*2680e0c0SChristopher Ferris 
498*2680e0c0SChristopher Ferris   malloc_stats prints only the most commonly interesting statistics.
499*2680e0c0SChristopher Ferris   More information can be obtained by calling mallinfo.
500*2680e0c0SChristopher Ferris 
501*2680e0c0SChristopher Ferris   malloc_stats is not compiled if NO_MALLOC_STATS is defined.
502*2680e0c0SChristopher Ferris */
503*2680e0c0SChristopher Ferris void  dlmalloc_stats(void);
504*2680e0c0SChristopher Ferris 
505*2680e0c0SChristopher Ferris #endif /* !ONLY_MSPACES */
506*2680e0c0SChristopher Ferris 
507*2680e0c0SChristopher Ferris /*
508*2680e0c0SChristopher Ferris   malloc_usable_size(void* p);
509*2680e0c0SChristopher Ferris 
510*2680e0c0SChristopher Ferris   Returns the number of bytes you can actually use in
511*2680e0c0SChristopher Ferris   an allocated chunk, which may be more than you requested (although
512*2680e0c0SChristopher Ferris   often not) due to alignment and minimum size constraints.
513*2680e0c0SChristopher Ferris   You can use this many bytes without worrying about
514*2680e0c0SChristopher Ferris   overwriting other allocated objects. This is not a particularly great
515*2680e0c0SChristopher Ferris   programming practice. malloc_usable_size can be more useful in
516*2680e0c0SChristopher Ferris   debugging and assertions, for example:
517*2680e0c0SChristopher Ferris 
518*2680e0c0SChristopher Ferris   p = malloc(n);
519*2680e0c0SChristopher Ferris   assert(malloc_usable_size(p) >= 256);
520*2680e0c0SChristopher Ferris */
521*2680e0c0SChristopher Ferris size_t dlmalloc_usable_size(const void*);
522*2680e0c0SChristopher Ferris 
523*2680e0c0SChristopher Ferris #if MSPACES
524*2680e0c0SChristopher Ferris 
525*2680e0c0SChristopher Ferris /*
526*2680e0c0SChristopher Ferris   mspace is an opaque type representing an independent
527*2680e0c0SChristopher Ferris   region of space that supports mspace_malloc, etc.
528*2680e0c0SChristopher Ferris */
529*2680e0c0SChristopher Ferris typedef void* mspace;
530*2680e0c0SChristopher Ferris 
531*2680e0c0SChristopher Ferris /*
532*2680e0c0SChristopher Ferris   create_mspace creates and returns a new independent space with the
533*2680e0c0SChristopher Ferris   given initial capacity, or, if 0, the default granularity size.  It
534*2680e0c0SChristopher Ferris   returns null if there is no system memory available to create the
535*2680e0c0SChristopher Ferris   space.  If argument locked is non-zero, the space uses a separate
536*2680e0c0SChristopher Ferris   lock to control access. The capacity of the space will grow
537*2680e0c0SChristopher Ferris   dynamically as needed to service mspace_malloc requests.  You can
538*2680e0c0SChristopher Ferris   control the sizes of incremental increases of this space by
539*2680e0c0SChristopher Ferris   compiling with a different DEFAULT_GRANULARITY or dynamically
540*2680e0c0SChristopher Ferris   setting with mallopt(M_GRANULARITY, value).
541*2680e0c0SChristopher Ferris */
542*2680e0c0SChristopher Ferris mspace create_mspace(size_t capacity, int locked);
543*2680e0c0SChristopher Ferris 
544*2680e0c0SChristopher Ferris /*
545*2680e0c0SChristopher Ferris   destroy_mspace destroys the given space, and attempts to return all
546*2680e0c0SChristopher Ferris   of its memory back to the system, returning the total number of
547*2680e0c0SChristopher Ferris   bytes freed. After destruction, the results of access to all memory
548*2680e0c0SChristopher Ferris   used by the space become undefined.
549*2680e0c0SChristopher Ferris */
550*2680e0c0SChristopher Ferris size_t destroy_mspace(mspace msp);
551*2680e0c0SChristopher Ferris 
552*2680e0c0SChristopher Ferris /*
553*2680e0c0SChristopher Ferris   create_mspace_with_base uses the memory supplied as the initial base
554*2680e0c0SChristopher Ferris   of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this
555*2680e0c0SChristopher Ferris   space is used for bookkeeping, so the capacity must be at least this
556*2680e0c0SChristopher Ferris   large. (Otherwise 0 is returned.) When this initial space is
557*2680e0c0SChristopher Ferris   exhausted, additional memory will be obtained from the system.
558*2680e0c0SChristopher Ferris   Destroying this space will deallocate all additionally allocated
559*2680e0c0SChristopher Ferris   space (if possible) but not the initial base.
560*2680e0c0SChristopher Ferris */
561*2680e0c0SChristopher Ferris mspace create_mspace_with_base(void* base, size_t capacity, int locked);
562*2680e0c0SChristopher Ferris 
563*2680e0c0SChristopher Ferris /*
564*2680e0c0SChristopher Ferris   mspace_track_large_chunks controls whether requests for large chunks
565*2680e0c0SChristopher Ferris   are allocated in their own untracked mmapped regions, separate from
566*2680e0c0SChristopher Ferris   others in this mspace. By default large chunks are not tracked,
567*2680e0c0SChristopher Ferris   which reduces fragmentation. However, such chunks are not
568*2680e0c0SChristopher Ferris   necessarily released to the system upon destroy_mspace.  Enabling
569*2680e0c0SChristopher Ferris   tracking by setting to true may increase fragmentation, but avoids
570*2680e0c0SChristopher Ferris   leakage when relying on destroy_mspace to release all memory
571*2680e0c0SChristopher Ferris   allocated using this space.  The function returns the previous
572*2680e0c0SChristopher Ferris   setting.
573*2680e0c0SChristopher Ferris */
574*2680e0c0SChristopher Ferris int mspace_track_large_chunks(mspace msp, int enable);
575*2680e0c0SChristopher Ferris 
576*2680e0c0SChristopher Ferris #if !NO_MALLINFO
577*2680e0c0SChristopher Ferris /*
578*2680e0c0SChristopher Ferris   mspace_mallinfo behaves as mallinfo, but reports properties of
579*2680e0c0SChristopher Ferris   the given space.
580*2680e0c0SChristopher Ferris */
581*2680e0c0SChristopher Ferris struct mallinfo mspace_mallinfo(mspace msp);
582*2680e0c0SChristopher Ferris #endif /* NO_MALLINFO */
583*2680e0c0SChristopher Ferris 
584*2680e0c0SChristopher Ferris /*
585*2680e0c0SChristopher Ferris   An alias for mallopt.
586*2680e0c0SChristopher Ferris */
587*2680e0c0SChristopher Ferris int mspace_mallopt(int, int);
588*2680e0c0SChristopher Ferris 
589*2680e0c0SChristopher Ferris /*
590*2680e0c0SChristopher Ferris   The following operate identically to their malloc counterparts
591*2680e0c0SChristopher Ferris   but operate only for the given mspace argument
592*2680e0c0SChristopher Ferris */
593*2680e0c0SChristopher Ferris void* mspace_malloc(mspace msp, size_t bytes);
594*2680e0c0SChristopher Ferris void mspace_free(mspace msp, void* mem);
595*2680e0c0SChristopher Ferris void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
596*2680e0c0SChristopher Ferris void* mspace_realloc(mspace msp, void* mem, size_t newsize);
597*2680e0c0SChristopher Ferris void* mspace_realloc_in_place(mspace msp, void* mem, size_t newsize);
598*2680e0c0SChristopher Ferris void* mspace_memalign(mspace msp, size_t alignment, size_t bytes);
599*2680e0c0SChristopher Ferris void** mspace_independent_calloc(mspace msp, size_t n_elements,
600*2680e0c0SChristopher Ferris                                  size_t elem_size, void* chunks[]);
601*2680e0c0SChristopher Ferris void** mspace_independent_comalloc(mspace msp, size_t n_elements,
602*2680e0c0SChristopher Ferris                                    size_t sizes[], void* chunks[]);
603*2680e0c0SChristopher Ferris size_t mspace_bulk_free(mspace msp, void**, size_t n_elements);
604*2680e0c0SChristopher Ferris size_t mspace_usable_size(const void* mem);
605*2680e0c0SChristopher Ferris void mspace_malloc_stats(mspace msp);
606*2680e0c0SChristopher Ferris int mspace_trim(mspace msp, size_t pad);
607*2680e0c0SChristopher Ferris size_t mspace_footprint(mspace msp);
608*2680e0c0SChristopher Ferris size_t mspace_max_footprint(mspace msp);
609*2680e0c0SChristopher Ferris size_t mspace_footprint_limit(mspace msp);
610*2680e0c0SChristopher Ferris size_t mspace_set_footprint_limit(mspace msp, size_t bytes);
611*2680e0c0SChristopher Ferris void mspace_inspect_all(mspace msp,
612*2680e0c0SChristopher Ferris                         void(*handler)(void *, void *, size_t, void*),
613*2680e0c0SChristopher Ferris                         void* arg);
614*2680e0c0SChristopher Ferris #endif  /* MSPACES */
615*2680e0c0SChristopher Ferris 
616*2680e0c0SChristopher Ferris #ifdef __cplusplus
617*2680e0c0SChristopher Ferris };  /* end of extern "C" */
618*2680e0c0SChristopher Ferris #endif
619*2680e0c0SChristopher Ferris 
620*2680e0c0SChristopher Ferris #endif /* MALLOC_280_H */
621