1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun A version of malloc/free/realloc written by Doug Lea and released to the
3*4882a593Smuzhiyun public domain. Send questions/comments/complaints/performance data
4*4882a593Smuzhiyun to dl@cs.oswego.edu
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun * VERSION 2.6.6 Sun Mar 5 19:10:03 2000 Doug Lea (dl at gee)
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun Note: There may be an updated version of this malloc obtainable at
9*4882a593Smuzhiyun ftp://g.oswego.edu/pub/misc/malloc.c
10*4882a593Smuzhiyun Check before installing!
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun * Why use this malloc?
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun This is not the fastest, most space-conserving, most portable, or
15*4882a593Smuzhiyun most tunable malloc ever written. However it is among the fastest
16*4882a593Smuzhiyun while also being among the most space-conserving, portable and tunable.
17*4882a593Smuzhiyun Consistent balance across these factors results in a good general-purpose
18*4882a593Smuzhiyun allocator. For a high-level description, see
19*4882a593Smuzhiyun http://g.oswego.edu/dl/html/malloc.html
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun * Synopsis of public routines
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun (Much fuller descriptions are contained in the program documentation below.)
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun malloc(size_t n);
26*4882a593Smuzhiyun Return a pointer to a newly allocated chunk of at least n bytes, or null
27*4882a593Smuzhiyun if no space is available.
28*4882a593Smuzhiyun free(Void_t* p);
29*4882a593Smuzhiyun Release the chunk of memory pointed to by p, or no effect if p is null.
30*4882a593Smuzhiyun realloc(Void_t* p, size_t n);
31*4882a593Smuzhiyun Return a pointer to a chunk of size n that contains the same data
32*4882a593Smuzhiyun as does chunk p up to the minimum of (n, p's size) bytes, or null
33*4882a593Smuzhiyun if no space is available. The returned pointer may or may not be
34*4882a593Smuzhiyun the same as p. If p is null, equivalent to malloc. Unless the
35*4882a593Smuzhiyun #define REALLOC_ZERO_BYTES_FREES below is set, realloc with a
36*4882a593Smuzhiyun size argument of zero (re)allocates a minimum-sized chunk.
37*4882a593Smuzhiyun memalign(size_t alignment, size_t n);
38*4882a593Smuzhiyun Return a pointer to a newly allocated chunk of n bytes, aligned
39*4882a593Smuzhiyun in accord with the alignment argument, which must be a power of
40*4882a593Smuzhiyun two.
41*4882a593Smuzhiyun valloc(size_t n);
42*4882a593Smuzhiyun Equivalent to memalign(pagesize, n), where pagesize is the page
43*4882a593Smuzhiyun size of the system (or as near to this as can be figured out from
44*4882a593Smuzhiyun all the includes/defines below.)
45*4882a593Smuzhiyun pvalloc(size_t n);
46*4882a593Smuzhiyun Equivalent to valloc(minimum-page-that-holds(n)), that is,
47*4882a593Smuzhiyun round up n to nearest pagesize.
48*4882a593Smuzhiyun calloc(size_t unit, size_t quantity);
49*4882a593Smuzhiyun Returns a pointer to quantity * unit bytes, with all locations
50*4882a593Smuzhiyun set to zero.
51*4882a593Smuzhiyun cfree(Void_t* p);
52*4882a593Smuzhiyun Equivalent to free(p).
53*4882a593Smuzhiyun malloc_trim(size_t pad);
54*4882a593Smuzhiyun Release all but pad bytes of freed top-most memory back
55*4882a593Smuzhiyun to the system. Return 1 if successful, else 0.
56*4882a593Smuzhiyun malloc_usable_size(Void_t* p);
57*4882a593Smuzhiyun Report the number usable allocated bytes associated with allocated
58*4882a593Smuzhiyun chunk p. This may or may not report more bytes than were requested,
59*4882a593Smuzhiyun due to alignment and minimum size constraints.
60*4882a593Smuzhiyun malloc_stats();
61*4882a593Smuzhiyun Prints brief summary statistics on stderr.
62*4882a593Smuzhiyun mallinfo()
63*4882a593Smuzhiyun Returns (by copy) a struct containing various summary statistics.
64*4882a593Smuzhiyun mallopt(int parameter_number, int parameter_value)
65*4882a593Smuzhiyun Changes one of the tunable parameters described below. Returns
66*4882a593Smuzhiyun 1 if successful in changing the parameter, else 0.
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun * Vital statistics:
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun Alignment: 8-byte
71*4882a593Smuzhiyun 8 byte alignment is currently hardwired into the design. This
72*4882a593Smuzhiyun seems to suffice for all current machines and C compilers.
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun Assumed pointer representation: 4 or 8 bytes
75*4882a593Smuzhiyun Code for 8-byte pointers is untested by me but has worked
76*4882a593Smuzhiyun reliably by Wolfram Gloger, who contributed most of the
77*4882a593Smuzhiyun changes supporting this.
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun Assumed size_t representation: 4 or 8 bytes
80*4882a593Smuzhiyun Note that size_t is allowed to be 4 bytes even if pointers are 8.
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun Minimum overhead per allocated chunk: 4 or 8 bytes
83*4882a593Smuzhiyun Each malloced chunk has a hidden overhead of 4 bytes holding size
84*4882a593Smuzhiyun and status information.
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead)
87*4882a593Smuzhiyun 8-byte ptrs: 24/32 bytes (including, 4/8 overhead)
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
90*4882a593Smuzhiyun ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
91*4882a593Smuzhiyun needed; 4 (8) for a trailing size field
92*4882a593Smuzhiyun and 8 (16) bytes for free list pointers. Thus, the minimum
93*4882a593Smuzhiyun allocatable size is 16/24/32 bytes.
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun Even a request for zero bytes (i.e., malloc(0)) returns a
96*4882a593Smuzhiyun pointer to something of the minimum allocatable size.
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun Maximum allocated size: 4-byte size_t: 2^31 - 8 bytes
99*4882a593Smuzhiyun 8-byte size_t: 2^63 - 16 bytes
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun It is assumed that (possibly signed) size_t bit values suffice to
102*4882a593Smuzhiyun represent chunk sizes. `Possibly signed' is due to the fact
103*4882a593Smuzhiyun that `size_t' may be defined on a system as either a signed or
104*4882a593Smuzhiyun an unsigned type. To be conservative, values that would appear
105*4882a593Smuzhiyun as negative numbers are avoided.
106*4882a593Smuzhiyun Requests for sizes with a negative sign bit when the request
107*4882a593Smuzhiyun size is treaded as a long will return null.
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun Maximum overhead wastage per allocated chunk: normally 15 bytes
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun Alignnment demands, plus the minimum allocatable size restriction
112*4882a593Smuzhiyun make the normal worst-case wastage 15 bytes (i.e., up to 15
113*4882a593Smuzhiyun more bytes will be allocated than were requested in malloc), with
114*4882a593Smuzhiyun two exceptions:
115*4882a593Smuzhiyun 1. Because requests for zero bytes allocate non-zero space,
116*4882a593Smuzhiyun the worst case wastage for a request of zero bytes is 24 bytes.
117*4882a593Smuzhiyun 2. For requests >= mmap_threshold that are serviced via
118*4882a593Smuzhiyun mmap(), the worst case wastage is 8 bytes plus the remainder
119*4882a593Smuzhiyun from a system page (the minimal mmap unit); typically 4096 bytes.
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun * Limitations
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun Here are some features that are NOT currently supported
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun * No user-definable hooks for callbacks and the like.
126*4882a593Smuzhiyun * No automated mechanism for fully checking that all accesses
127*4882a593Smuzhiyun to malloced memory stay within their bounds.
128*4882a593Smuzhiyun * No support for compaction.
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun * Synopsis of compile-time options:
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun People have reported using previous versions of this malloc on all
133*4882a593Smuzhiyun versions of Unix, sometimes by tweaking some of the defines
134*4882a593Smuzhiyun below. It has been tested most extensively on Solaris and
135*4882a593Smuzhiyun Linux. It is also reported to work on WIN32 platforms.
136*4882a593Smuzhiyun People have also reported adapting this malloc for use in
137*4882a593Smuzhiyun stand-alone embedded systems.
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun The implementation is in straight, hand-tuned ANSI C. Among other
140*4882a593Smuzhiyun consequences, it uses a lot of macros. Because of this, to be at
141*4882a593Smuzhiyun all usable, this code should be compiled using an optimizing compiler
142*4882a593Smuzhiyun (for example gcc -O2) that can simplify expressions and control
143*4882a593Smuzhiyun paths.
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun __STD_C (default: derived from C compiler defines)
146*4882a593Smuzhiyun Nonzero if using ANSI-standard C compiler, a C++ compiler, or
147*4882a593Smuzhiyun a C compiler sufficiently close to ANSI to get away with it.
148*4882a593Smuzhiyun DEBUG (default: NOT defined)
149*4882a593Smuzhiyun Define to enable debugging. Adds fairly extensive assertion-based
150*4882a593Smuzhiyun checking to help track down memory errors, but noticeably slows down
151*4882a593Smuzhiyun execution.
152*4882a593Smuzhiyun REALLOC_ZERO_BYTES_FREES (default: NOT defined)
153*4882a593Smuzhiyun Define this if you think that realloc(p, 0) should be equivalent
154*4882a593Smuzhiyun to free(p). Otherwise, since malloc returns a unique pointer for
155*4882a593Smuzhiyun malloc(0), so does realloc(p, 0).
156*4882a593Smuzhiyun HAVE_MEMCPY (default: defined)
157*4882a593Smuzhiyun Define if you are not otherwise using ANSI STD C, but still
158*4882a593Smuzhiyun have memcpy and memset in your C library and want to use them.
159*4882a593Smuzhiyun Otherwise, simple internal versions are supplied.
160*4882a593Smuzhiyun USE_MEMCPY (default: 1 if HAVE_MEMCPY is defined, 0 otherwise)
161*4882a593Smuzhiyun Define as 1 if you want the C library versions of memset and
162*4882a593Smuzhiyun memcpy called in realloc and calloc (otherwise macro versions are used).
163*4882a593Smuzhiyun At least on some platforms, the simple macro versions usually
164*4882a593Smuzhiyun outperform libc versions.
165*4882a593Smuzhiyun HAVE_MMAP (default: defined as 1)
166*4882a593Smuzhiyun Define to non-zero to optionally make malloc() use mmap() to
167*4882a593Smuzhiyun allocate very large blocks.
168*4882a593Smuzhiyun HAVE_MREMAP (default: defined as 0 unless Linux libc set)
169*4882a593Smuzhiyun Define to non-zero to optionally make realloc() use mremap() to
170*4882a593Smuzhiyun reallocate very large blocks.
171*4882a593Smuzhiyun malloc_getpagesize (default: derived from system #includes)
172*4882a593Smuzhiyun Either a constant or routine call returning the system page size.
173*4882a593Smuzhiyun HAVE_USR_INCLUDE_MALLOC_H (default: NOT defined)
174*4882a593Smuzhiyun Optionally define if you are on a system with a /usr/include/malloc.h
175*4882a593Smuzhiyun that declares struct mallinfo. It is not at all necessary to
176*4882a593Smuzhiyun define this even if you do, but will ensure consistency.
177*4882a593Smuzhiyun INTERNAL_SIZE_T (default: size_t)
178*4882a593Smuzhiyun Define to a 32-bit type (probably `unsigned int') if you are on a
179*4882a593Smuzhiyun 64-bit machine, yet do not want or need to allow malloc requests of
180*4882a593Smuzhiyun greater than 2^31 to be handled. This saves space, especially for
181*4882a593Smuzhiyun very small chunks.
182*4882a593Smuzhiyun INTERNAL_LINUX_C_LIB (default: NOT defined)
183*4882a593Smuzhiyun Defined only when compiled as part of Linux libc.
184*4882a593Smuzhiyun Also note that there is some odd internal name-mangling via defines
185*4882a593Smuzhiyun (for example, internally, `malloc' is named `mALLOc') needed
186*4882a593Smuzhiyun when compiling in this case. These look funny but don't otherwise
187*4882a593Smuzhiyun affect anything.
188*4882a593Smuzhiyun WIN32 (default: undefined)
189*4882a593Smuzhiyun Define this on MS win (95, nt) platforms to compile in sbrk emulation.
190*4882a593Smuzhiyun LACKS_UNISTD_H (default: undefined if not WIN32)
191*4882a593Smuzhiyun Define this if your system does not have a <unistd.h>.
192*4882a593Smuzhiyun LACKS_SYS_PARAM_H (default: undefined if not WIN32)
193*4882a593Smuzhiyun Define this if your system does not have a <sys/param.h>.
194*4882a593Smuzhiyun MORECORE (default: sbrk)
195*4882a593Smuzhiyun The name of the routine to call to obtain more memory from the system.
196*4882a593Smuzhiyun MORECORE_FAILURE (default: -1)
197*4882a593Smuzhiyun The value returned upon failure of MORECORE.
198*4882a593Smuzhiyun MORECORE_CLEARS (default 1)
199*4882a593Smuzhiyun true (1) if the routine mapped to MORECORE zeroes out memory (which
200*4882a593Smuzhiyun holds for sbrk).
201*4882a593Smuzhiyun DEFAULT_TRIM_THRESHOLD
202*4882a593Smuzhiyun DEFAULT_TOP_PAD
203*4882a593Smuzhiyun DEFAULT_MMAP_THRESHOLD
204*4882a593Smuzhiyun DEFAULT_MMAP_MAX
205*4882a593Smuzhiyun Default values of tunable parameters (described in detail below)
206*4882a593Smuzhiyun controlling interaction with host system routines (sbrk, mmap, etc).
207*4882a593Smuzhiyun These values may also be changed dynamically via mallopt(). The
208*4882a593Smuzhiyun preset defaults are those that give best performance for typical
209*4882a593Smuzhiyun programs/systems.
210*4882a593Smuzhiyun USE_DL_PREFIX (default: undefined)
211*4882a593Smuzhiyun Prefix all public routines with the string 'dl'. Useful to
212*4882a593Smuzhiyun quickly avoid procedure declaration conflicts and linker symbol
213*4882a593Smuzhiyun conflicts with existing memory allocation routines.
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun */
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun #ifndef __MALLOC_H__
220*4882a593Smuzhiyun #define __MALLOC_H__
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* Preliminaries */
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun #ifndef __STD_C
225*4882a593Smuzhiyun #ifdef __STDC__
226*4882a593Smuzhiyun #define __STD_C 1
227*4882a593Smuzhiyun #else
228*4882a593Smuzhiyun #if __cplusplus
229*4882a593Smuzhiyun #define __STD_C 1
230*4882a593Smuzhiyun #else
231*4882a593Smuzhiyun #define __STD_C 0
232*4882a593Smuzhiyun #endif /*__cplusplus*/
233*4882a593Smuzhiyun #endif /*__STDC__*/
234*4882a593Smuzhiyun #endif /*__STD_C*/
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun #ifndef Void_t
237*4882a593Smuzhiyun #if (__STD_C || defined(WIN32))
238*4882a593Smuzhiyun #define Void_t void
239*4882a593Smuzhiyun #else
240*4882a593Smuzhiyun #define Void_t char
241*4882a593Smuzhiyun #endif
242*4882a593Smuzhiyun #endif /*Void_t*/
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun #if __STD_C
245*4882a593Smuzhiyun #include <linux/stddef.h> /* for size_t */
246*4882a593Smuzhiyun #else
247*4882a593Smuzhiyun #include <sys/types.h>
248*4882a593Smuzhiyun #endif /* __STD_C */
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun #ifdef __cplusplus
251*4882a593Smuzhiyun extern "C" {
252*4882a593Smuzhiyun #endif
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun #if 0 /* not for U-Boot */
255*4882a593Smuzhiyun #include <stdio.h> /* needed for malloc_stats */
256*4882a593Smuzhiyun #endif
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /*
260*4882a593Smuzhiyun Compile-time options
261*4882a593Smuzhiyun */
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /*
265*4882a593Smuzhiyun Debugging:
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun Because freed chunks may be overwritten with link fields, this
268*4882a593Smuzhiyun malloc will often die when freed memory is overwritten by user
269*4882a593Smuzhiyun programs. This can be very effective (albeit in an annoying way)
270*4882a593Smuzhiyun in helping track down dangling pointers.
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun If you compile with -DDEBUG, a number of assertion checks are
273*4882a593Smuzhiyun enabled that will catch more memory errors. You probably won't be
274*4882a593Smuzhiyun able to make much sense of the actual assertion errors, but they
275*4882a593Smuzhiyun should help you locate incorrectly overwritten memory. The
276*4882a593Smuzhiyun checking is fairly extensive, and will slow down execution
277*4882a593Smuzhiyun noticeably. Calling malloc_stats or mallinfo with DEBUG set will
278*4882a593Smuzhiyun attempt to check every non-mmapped allocated and free chunk in the
279*4882a593Smuzhiyun course of computing the summmaries. (By nature, mmapped regions
280*4882a593Smuzhiyun cannot be checked very much automatically.)
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun Setting DEBUG may also be helpful if you are trying to modify
283*4882a593Smuzhiyun this code. The assertions in the check routines spell out in more
284*4882a593Smuzhiyun detail the assumptions and invariants underlying the algorithms.
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun */
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /*
289*4882a593Smuzhiyun INTERNAL_SIZE_T is the word-size used for internal bookkeeping
290*4882a593Smuzhiyun of chunk sizes. On a 64-bit machine, you can reduce malloc
291*4882a593Smuzhiyun overhead by defining INTERNAL_SIZE_T to be a 32 bit `unsigned int'
292*4882a593Smuzhiyun at the expense of not being able to handle requests greater than
293*4882a593Smuzhiyun 2^31. This limitation is hardly ever a concern; you are encouraged
294*4882a593Smuzhiyun to set this. However, the default version is the same as size_t.
295*4882a593Smuzhiyun */
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun #ifndef INTERNAL_SIZE_T
298*4882a593Smuzhiyun #define INTERNAL_SIZE_T size_t
299*4882a593Smuzhiyun #endif
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /*
302*4882a593Smuzhiyun REALLOC_ZERO_BYTES_FREES should be set if a call to
303*4882a593Smuzhiyun realloc with zero bytes should be the same as a call to free.
304*4882a593Smuzhiyun Some people think it should. Otherwise, since this malloc
305*4882a593Smuzhiyun returns a unique pointer for malloc(0), so does realloc(p, 0).
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /* #define REALLOC_ZERO_BYTES_FREES */
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /*
313*4882a593Smuzhiyun WIN32 causes an emulation of sbrk to be compiled in
314*4882a593Smuzhiyun mmap-based options are not currently supported in WIN32.
315*4882a593Smuzhiyun */
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /* #define WIN32 */
318*4882a593Smuzhiyun #ifdef WIN32
319*4882a593Smuzhiyun #define MORECORE wsbrk
320*4882a593Smuzhiyun #define HAVE_MMAP 0
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun #define LACKS_UNISTD_H
323*4882a593Smuzhiyun #define LACKS_SYS_PARAM_H
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /*
326*4882a593Smuzhiyun Include 'windows.h' to get the necessary declarations for the
327*4882a593Smuzhiyun Microsoft Visual C++ data structures and routines used in the 'sbrk'
328*4882a593Smuzhiyun emulation.
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun Define WIN32_LEAN_AND_MEAN so that only the essential Microsoft
331*4882a593Smuzhiyun Visual C++ header files are included.
332*4882a593Smuzhiyun */
333*4882a593Smuzhiyun #define WIN32_LEAN_AND_MEAN
334*4882a593Smuzhiyun #include <windows.h>
335*4882a593Smuzhiyun #endif
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun /*
339*4882a593Smuzhiyun HAVE_MEMCPY should be defined if you are not otherwise using
340*4882a593Smuzhiyun ANSI STD C, but still have memcpy and memset in your C library
341*4882a593Smuzhiyun and want to use them in calloc and realloc. Otherwise simple
342*4882a593Smuzhiyun macro versions are defined here.
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun USE_MEMCPY should be defined as 1 if you actually want to
345*4882a593Smuzhiyun have memset and memcpy called. People report that the macro
346*4882a593Smuzhiyun versions are often enough faster than libc versions on many
347*4882a593Smuzhiyun systems that it is better to use them.
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun */
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun #define HAVE_MEMCPY
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun #ifndef USE_MEMCPY
354*4882a593Smuzhiyun #ifdef HAVE_MEMCPY
355*4882a593Smuzhiyun #define USE_MEMCPY 1
356*4882a593Smuzhiyun #else
357*4882a593Smuzhiyun #define USE_MEMCPY 0
358*4882a593Smuzhiyun #endif
359*4882a593Smuzhiyun #endif
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun #if (__STD_C || defined(HAVE_MEMCPY))
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun #if __STD_C
364*4882a593Smuzhiyun void* memset(void*, int, size_t);
365*4882a593Smuzhiyun void* memcpy(void*, const void*, size_t);
366*4882a593Smuzhiyun #else
367*4882a593Smuzhiyun #ifdef WIN32
368*4882a593Smuzhiyun /* On Win32 platforms, 'memset()' and 'memcpy()' are already declared in */
369*4882a593Smuzhiyun /* 'windows.h' */
370*4882a593Smuzhiyun #else
371*4882a593Smuzhiyun Void_t* memset();
372*4882a593Smuzhiyun Void_t* memcpy();
373*4882a593Smuzhiyun #endif
374*4882a593Smuzhiyun #endif
375*4882a593Smuzhiyun #endif
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun #if USE_MEMCPY
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* The following macros are only invoked with (2n+1)-multiples of
380*4882a593Smuzhiyun INTERNAL_SIZE_T units, with a positive integer n. This is exploited
381*4882a593Smuzhiyun for fast inline execution when n is small. */
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun #define MALLOC_ZERO(charp, nbytes) \
384*4882a593Smuzhiyun do { \
385*4882a593Smuzhiyun INTERNAL_SIZE_T mzsz = (nbytes); \
386*4882a593Smuzhiyun if(mzsz <= 9*sizeof(mzsz)) { \
387*4882a593Smuzhiyun INTERNAL_SIZE_T* mz = (INTERNAL_SIZE_T*) (charp); \
388*4882a593Smuzhiyun if(mzsz >= 5*sizeof(mzsz)) { *mz++ = 0; \
389*4882a593Smuzhiyun *mz++ = 0; \
390*4882a593Smuzhiyun if(mzsz >= 7*sizeof(mzsz)) { *mz++ = 0; \
391*4882a593Smuzhiyun *mz++ = 0; \
392*4882a593Smuzhiyun if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0; \
393*4882a593Smuzhiyun *mz++ = 0; }}} \
394*4882a593Smuzhiyun *mz++ = 0; \
395*4882a593Smuzhiyun *mz++ = 0; \
396*4882a593Smuzhiyun *mz = 0; \
397*4882a593Smuzhiyun } else memset((charp), 0, mzsz); \
398*4882a593Smuzhiyun } while(0)
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun #define MALLOC_COPY(dest,src,nbytes) \
401*4882a593Smuzhiyun do { \
402*4882a593Smuzhiyun INTERNAL_SIZE_T mcsz = (nbytes); \
403*4882a593Smuzhiyun if(mcsz <= 9*sizeof(mcsz)) { \
404*4882a593Smuzhiyun INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) (src); \
405*4882a593Smuzhiyun INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) (dest); \
406*4882a593Smuzhiyun if(mcsz >= 5*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
407*4882a593Smuzhiyun *mcdst++ = *mcsrc++; \
408*4882a593Smuzhiyun if(mcsz >= 7*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
409*4882a593Smuzhiyun *mcdst++ = *mcsrc++; \
410*4882a593Smuzhiyun if(mcsz >= 9*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
411*4882a593Smuzhiyun *mcdst++ = *mcsrc++; }}} \
412*4882a593Smuzhiyun *mcdst++ = *mcsrc++; \
413*4882a593Smuzhiyun *mcdst++ = *mcsrc++; \
414*4882a593Smuzhiyun *mcdst = *mcsrc ; \
415*4882a593Smuzhiyun } else memcpy(dest, src, mcsz); \
416*4882a593Smuzhiyun } while(0)
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun #else /* !USE_MEMCPY */
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun /* Use Duff's device for good zeroing/copying performance. */
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun #define MALLOC_ZERO(charp, nbytes) \
423*4882a593Smuzhiyun do { \
424*4882a593Smuzhiyun INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp); \
425*4882a593Smuzhiyun long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
426*4882a593Smuzhiyun if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
427*4882a593Smuzhiyun switch (mctmp) { \
428*4882a593Smuzhiyun case 0: for(;;) { *mzp++ = 0; \
429*4882a593Smuzhiyun case 7: *mzp++ = 0; \
430*4882a593Smuzhiyun case 6: *mzp++ = 0; \
431*4882a593Smuzhiyun case 5: *mzp++ = 0; \
432*4882a593Smuzhiyun case 4: *mzp++ = 0; \
433*4882a593Smuzhiyun case 3: *mzp++ = 0; \
434*4882a593Smuzhiyun case 2: *mzp++ = 0; \
435*4882a593Smuzhiyun case 1: *mzp++ = 0; if(mcn <= 0) break; mcn--; } \
436*4882a593Smuzhiyun } \
437*4882a593Smuzhiyun } while(0)
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun #define MALLOC_COPY(dest,src,nbytes) \
440*4882a593Smuzhiyun do { \
441*4882a593Smuzhiyun INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src; \
442*4882a593Smuzhiyun INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest; \
443*4882a593Smuzhiyun long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
444*4882a593Smuzhiyun if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
445*4882a593Smuzhiyun switch (mctmp) { \
446*4882a593Smuzhiyun case 0: for(;;) { *mcdst++ = *mcsrc++; \
447*4882a593Smuzhiyun case 7: *mcdst++ = *mcsrc++; \
448*4882a593Smuzhiyun case 6: *mcdst++ = *mcsrc++; \
449*4882a593Smuzhiyun case 5: *mcdst++ = *mcsrc++; \
450*4882a593Smuzhiyun case 4: *mcdst++ = *mcsrc++; \
451*4882a593Smuzhiyun case 3: *mcdst++ = *mcsrc++; \
452*4882a593Smuzhiyun case 2: *mcdst++ = *mcsrc++; \
453*4882a593Smuzhiyun case 1: *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; } \
454*4882a593Smuzhiyun } \
455*4882a593Smuzhiyun } while(0)
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun #endif
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun /*
461*4882a593Smuzhiyun Define HAVE_MMAP to optionally make malloc() use mmap() to
462*4882a593Smuzhiyun allocate very large blocks. These will be returned to the
463*4882a593Smuzhiyun operating system immediately after a free().
464*4882a593Smuzhiyun */
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun /***
467*4882a593Smuzhiyun #ifndef HAVE_MMAP
468*4882a593Smuzhiyun #define HAVE_MMAP 1
469*4882a593Smuzhiyun #endif
470*4882a593Smuzhiyun ***/
471*4882a593Smuzhiyun #undef HAVE_MMAP /* Not available for U-Boot */
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /*
474*4882a593Smuzhiyun Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
475*4882a593Smuzhiyun large blocks. This is currently only possible on Linux with
476*4882a593Smuzhiyun kernel versions newer than 1.3.77.
477*4882a593Smuzhiyun */
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun /***
480*4882a593Smuzhiyun #ifndef HAVE_MREMAP
481*4882a593Smuzhiyun #ifdef INTERNAL_LINUX_C_LIB
482*4882a593Smuzhiyun #define HAVE_MREMAP 1
483*4882a593Smuzhiyun #else
484*4882a593Smuzhiyun #define HAVE_MREMAP 0
485*4882a593Smuzhiyun #endif
486*4882a593Smuzhiyun #endif
487*4882a593Smuzhiyun ***/
488*4882a593Smuzhiyun #undef HAVE_MREMAP /* Not available for U-Boot */
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun #ifdef HAVE_MMAP
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun #include <unistd.h>
493*4882a593Smuzhiyun #include <fcntl.h>
494*4882a593Smuzhiyun #include <sys/mman.h>
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
497*4882a593Smuzhiyun #define MAP_ANONYMOUS MAP_ANON
498*4882a593Smuzhiyun #endif
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun #endif /* HAVE_MMAP */
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun /*
503*4882a593Smuzhiyun Access to system page size. To the extent possible, this malloc
504*4882a593Smuzhiyun manages memory from the system in page-size units.
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun The following mechanics for getpagesize were adapted from
507*4882a593Smuzhiyun bsd/gnu getpagesize.h
508*4882a593Smuzhiyun */
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun #define LACKS_UNISTD_H /* Shortcut for U-Boot */
511*4882a593Smuzhiyun #define malloc_getpagesize 4096
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun #ifndef LACKS_UNISTD_H
514*4882a593Smuzhiyun # include <unistd.h>
515*4882a593Smuzhiyun #endif
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun #ifndef malloc_getpagesize
518*4882a593Smuzhiyun # ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */
519*4882a593Smuzhiyun # ifndef _SC_PAGE_SIZE
520*4882a593Smuzhiyun # define _SC_PAGE_SIZE _SC_PAGESIZE
521*4882a593Smuzhiyun # endif
522*4882a593Smuzhiyun # endif
523*4882a593Smuzhiyun # ifdef _SC_PAGE_SIZE
524*4882a593Smuzhiyun # define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
525*4882a593Smuzhiyun # else
526*4882a593Smuzhiyun # if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
527*4882a593Smuzhiyun extern size_t getpagesize();
528*4882a593Smuzhiyun # define malloc_getpagesize getpagesize()
529*4882a593Smuzhiyun # else
530*4882a593Smuzhiyun # ifdef WIN32
531*4882a593Smuzhiyun # define malloc_getpagesize (4096) /* TBD: Use 'GetSystemInfo' instead */
532*4882a593Smuzhiyun # else
533*4882a593Smuzhiyun # ifndef LACKS_SYS_PARAM_H
534*4882a593Smuzhiyun # include <sys/param.h>
535*4882a593Smuzhiyun # endif
536*4882a593Smuzhiyun # ifdef EXEC_PAGESIZE
537*4882a593Smuzhiyun # define malloc_getpagesize EXEC_PAGESIZE
538*4882a593Smuzhiyun # else
539*4882a593Smuzhiyun # ifdef NBPG
540*4882a593Smuzhiyun # ifndef CLSIZE
541*4882a593Smuzhiyun # define malloc_getpagesize NBPG
542*4882a593Smuzhiyun # else
543*4882a593Smuzhiyun # define malloc_getpagesize (NBPG * CLSIZE)
544*4882a593Smuzhiyun # endif
545*4882a593Smuzhiyun # else
546*4882a593Smuzhiyun # ifdef NBPC
547*4882a593Smuzhiyun # define malloc_getpagesize NBPC
548*4882a593Smuzhiyun # else
549*4882a593Smuzhiyun # ifdef PAGESIZE
550*4882a593Smuzhiyun # define malloc_getpagesize PAGESIZE
551*4882a593Smuzhiyun # else
552*4882a593Smuzhiyun # define malloc_getpagesize (4096) /* just guess */
553*4882a593Smuzhiyun # endif
554*4882a593Smuzhiyun # endif
555*4882a593Smuzhiyun # endif
556*4882a593Smuzhiyun # endif
557*4882a593Smuzhiyun # endif
558*4882a593Smuzhiyun # endif
559*4882a593Smuzhiyun # endif
560*4882a593Smuzhiyun #endif
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun /*
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun This version of malloc supports the standard SVID/XPG mallinfo
566*4882a593Smuzhiyun routine that returns a struct containing the same kind of
567*4882a593Smuzhiyun information you can get from malloc_stats. It should work on
568*4882a593Smuzhiyun any SVID/XPG compliant system that has a /usr/include/malloc.h
569*4882a593Smuzhiyun defining struct mallinfo. (If you'd like to install such a thing
570*4882a593Smuzhiyun yourself, cut out the preliminary declarations as described above
571*4882a593Smuzhiyun and below and save them in a malloc.h file. But there's no
572*4882a593Smuzhiyun compelling reason to bother to do this.)
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun The main declaration needed is the mallinfo struct that is returned
575*4882a593Smuzhiyun (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a
576*4882a593Smuzhiyun bunch of fields, most of which are not even meaningful in this
577*4882a593Smuzhiyun version of malloc. Some of these fields are are instead filled by
578*4882a593Smuzhiyun mallinfo() with other numbers that might possibly be of interest.
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
581*4882a593Smuzhiyun /usr/include/malloc.h file that includes a declaration of struct
582*4882a593Smuzhiyun mallinfo. If so, it is included; else an SVID2/XPG2 compliant
583*4882a593Smuzhiyun version is declared below. These must be precisely the same for
584*4882a593Smuzhiyun mallinfo() to work.
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun */
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /* #define HAVE_USR_INCLUDE_MALLOC_H */
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun #ifdef HAVE_USR_INCLUDE_MALLOC_H
591*4882a593Smuzhiyun #include "/usr/include/malloc.h"
592*4882a593Smuzhiyun #else
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun /* SVID2/XPG mallinfo structure */
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun struct mallinfo {
597*4882a593Smuzhiyun int arena; /* total space allocated from system */
598*4882a593Smuzhiyun int ordblks; /* number of non-inuse chunks */
599*4882a593Smuzhiyun int smblks; /* unused -- always zero */
600*4882a593Smuzhiyun int hblks; /* number of mmapped regions */
601*4882a593Smuzhiyun int hblkhd; /* total space in mmapped regions */
602*4882a593Smuzhiyun int usmblks; /* unused -- always zero */
603*4882a593Smuzhiyun int fsmblks; /* unused -- always zero */
604*4882a593Smuzhiyun int uordblks; /* total allocated space */
605*4882a593Smuzhiyun int fordblks; /* total non-inuse space */
606*4882a593Smuzhiyun int keepcost; /* top-most, releasable (via malloc_trim) space */
607*4882a593Smuzhiyun };
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun /* SVID2/XPG mallopt options */
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun #define M_MXFAST 1 /* UNUSED in this malloc */
612*4882a593Smuzhiyun #define M_NLBLKS 2 /* UNUSED in this malloc */
613*4882a593Smuzhiyun #define M_GRAIN 3 /* UNUSED in this malloc */
614*4882a593Smuzhiyun #define M_KEEP 4 /* UNUSED in this malloc */
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun #endif
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun /* mallopt options that actually do something */
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun #define M_TRIM_THRESHOLD -1
621*4882a593Smuzhiyun #define M_TOP_PAD -2
622*4882a593Smuzhiyun #define M_MMAP_THRESHOLD -3
623*4882a593Smuzhiyun #define M_MMAP_MAX -4
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun #ifndef DEFAULT_TRIM_THRESHOLD
627*4882a593Smuzhiyun #define DEFAULT_TRIM_THRESHOLD (128 * 1024)
628*4882a593Smuzhiyun #endif
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun /*
631*4882a593Smuzhiyun M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
632*4882a593Smuzhiyun to keep before releasing via malloc_trim in free().
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun Automatic trimming is mainly useful in long-lived programs.
635*4882a593Smuzhiyun Because trimming via sbrk can be slow on some systems, and can
636*4882a593Smuzhiyun sometimes be wasteful (in cases where programs immediately
637*4882a593Smuzhiyun afterward allocate more large chunks) the value should be high
638*4882a593Smuzhiyun enough so that your overall system performance would improve by
639*4882a593Smuzhiyun releasing.
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun The trim threshold and the mmap control parameters (see below)
642*4882a593Smuzhiyun can be traded off with one another. Trimming and mmapping are
643*4882a593Smuzhiyun two different ways of releasing unused memory back to the
644*4882a593Smuzhiyun system. Between these two, it is often possible to keep
645*4882a593Smuzhiyun system-level demands of a long-lived program down to a bare
646*4882a593Smuzhiyun minimum. For example, in one test suite of sessions measuring
647*4882a593Smuzhiyun the XF86 X server on Linux, using a trim threshold of 128K and a
648*4882a593Smuzhiyun mmap threshold of 192K led to near-minimal long term resource
649*4882a593Smuzhiyun consumption.
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun If you are using this malloc in a long-lived program, it should
652*4882a593Smuzhiyun pay to experiment with these values. As a rough guide, you
653*4882a593Smuzhiyun might set to a value close to the average size of a process
654*4882a593Smuzhiyun (program) running on your system. Releasing this much memory
655*4882a593Smuzhiyun would allow such a process to run in memory. Generally, it's
656*4882a593Smuzhiyun worth it to tune for trimming rather tham memory mapping when a
657*4882a593Smuzhiyun program undergoes phases where several large chunks are
658*4882a593Smuzhiyun allocated and released in ways that can reuse each other's
659*4882a593Smuzhiyun storage, perhaps mixed with phases where there are no such
660*4882a593Smuzhiyun chunks at all. And in well-behaved long-lived programs,
661*4882a593Smuzhiyun controlling release of large blocks via trimming versus mapping
662*4882a593Smuzhiyun is usually faster.
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun However, in most programs, these parameters serve mainly as
665*4882a593Smuzhiyun protection against the system-level effects of carrying around
666*4882a593Smuzhiyun massive amounts of unneeded memory. Since frequent calls to
667*4882a593Smuzhiyun sbrk, mmap, and munmap otherwise degrade performance, the default
668*4882a593Smuzhiyun parameters are set to relatively high values that serve only as
669*4882a593Smuzhiyun safeguards.
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun The default trim value is high enough to cause trimming only in
672*4882a593Smuzhiyun fairly extreme (by current memory consumption standards) cases.
673*4882a593Smuzhiyun It must be greater than page size to have any useful effect. To
674*4882a593Smuzhiyun disable trimming completely, you can set to (unsigned long)(-1);
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun */
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun #ifndef DEFAULT_TOP_PAD
681*4882a593Smuzhiyun #define DEFAULT_TOP_PAD (0)
682*4882a593Smuzhiyun #endif
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun /*
685*4882a593Smuzhiyun M_TOP_PAD is the amount of extra `padding' space to allocate or
686*4882a593Smuzhiyun retain whenever sbrk is called. It is used in two ways internally:
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun * When sbrk is called to extend the top of the arena to satisfy
689*4882a593Smuzhiyun a new malloc request, this much padding is added to the sbrk
690*4882a593Smuzhiyun request.
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun * When malloc_trim is called automatically from free(),
693*4882a593Smuzhiyun it is used as the `pad' argument.
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun In both cases, the actual amount of padding is rounded
696*4882a593Smuzhiyun so that the end of the arena is always a system page boundary.
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun The main reason for using padding is to avoid calling sbrk so
699*4882a593Smuzhiyun often. Having even a small pad greatly reduces the likelihood
700*4882a593Smuzhiyun that nearly every malloc request during program start-up (or
701*4882a593Smuzhiyun after trimming) will invoke sbrk, which needlessly wastes
702*4882a593Smuzhiyun time.
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun Automatic rounding-up to page-size units is normally sufficient
705*4882a593Smuzhiyun to avoid measurable overhead, so the default is 0. However, in
706*4882a593Smuzhiyun systems where sbrk is relatively slow, it can pay to increase
707*4882a593Smuzhiyun this value, at the expense of carrying around more memory than
708*4882a593Smuzhiyun the program needs.
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun */
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun #ifndef DEFAULT_MMAP_THRESHOLD
714*4882a593Smuzhiyun #define DEFAULT_MMAP_THRESHOLD (128 * 1024)
715*4882a593Smuzhiyun #endif
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun /*
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun M_MMAP_THRESHOLD is the request size threshold for using mmap()
720*4882a593Smuzhiyun to service a request. Requests of at least this size that cannot
721*4882a593Smuzhiyun be allocated using already-existing space will be serviced via mmap.
722*4882a593Smuzhiyun (If enough normal freed space already exists it is used instead.)
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun Using mmap segregates relatively large chunks of memory so that
725*4882a593Smuzhiyun they can be individually obtained and released from the host
726*4882a593Smuzhiyun system. A request serviced through mmap is never reused by any
727*4882a593Smuzhiyun other request (at least not directly; the system may just so
728*4882a593Smuzhiyun happen to remap successive requests to the same locations).
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun Segregating space in this way has the benefit that mmapped space
731*4882a593Smuzhiyun can ALWAYS be individually released back to the system, which
732*4882a593Smuzhiyun helps keep the system level memory demands of a long-lived
733*4882a593Smuzhiyun program low. Mapped memory can never become `locked' between
734*4882a593Smuzhiyun other chunks, as can happen with normally allocated chunks, which
735*4882a593Smuzhiyun menas that even trimming via malloc_trim would not release them.
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun However, it has the disadvantages that:
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun 1. The space cannot be reclaimed, consolidated, and then
740*4882a593Smuzhiyun used to service later requests, as happens with normal chunks.
741*4882a593Smuzhiyun 2. It can lead to more wastage because of mmap page alignment
742*4882a593Smuzhiyun requirements
743*4882a593Smuzhiyun 3. It causes malloc performance to be more dependent on host
744*4882a593Smuzhiyun system memory management support routines which may vary in
745*4882a593Smuzhiyun implementation quality and may impose arbitrary
746*4882a593Smuzhiyun limitations. Generally, servicing a request via normal
747*4882a593Smuzhiyun malloc steps is faster than going through a system's mmap.
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun All together, these considerations should lead you to use mmap
750*4882a593Smuzhiyun only for relatively large requests.
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun */
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun #ifndef DEFAULT_MMAP_MAX
757*4882a593Smuzhiyun #ifdef HAVE_MMAP
758*4882a593Smuzhiyun #define DEFAULT_MMAP_MAX (64)
759*4882a593Smuzhiyun #else
760*4882a593Smuzhiyun #define DEFAULT_MMAP_MAX (0)
761*4882a593Smuzhiyun #endif
762*4882a593Smuzhiyun #endif
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun /*
765*4882a593Smuzhiyun M_MMAP_MAX is the maximum number of requests to simultaneously
766*4882a593Smuzhiyun service using mmap. This parameter exists because:
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun 1. Some systems have a limited number of internal tables for
769*4882a593Smuzhiyun use by mmap.
770*4882a593Smuzhiyun 2. In most systems, overreliance on mmap can degrade overall
771*4882a593Smuzhiyun performance.
772*4882a593Smuzhiyun 3. If a program allocates many large regions, it is probably
773*4882a593Smuzhiyun better off using normal sbrk-based allocation routines that
774*4882a593Smuzhiyun can reclaim and reallocate normal heap memory. Using a
775*4882a593Smuzhiyun small value allows transition into this mode after the
776*4882a593Smuzhiyun first few allocations.
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun Setting to 0 disables all use of mmap. If HAVE_MMAP is not set,
779*4882a593Smuzhiyun the default value is 0, and attempts to set it to non-zero values
780*4882a593Smuzhiyun in mallopt will fail.
781*4882a593Smuzhiyun */
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun /*
785*4882a593Smuzhiyun USE_DL_PREFIX will prefix all public routines with the string 'dl'.
786*4882a593Smuzhiyun Useful to quickly avoid procedure declaration conflicts and linker
787*4882a593Smuzhiyun symbol conflicts with existing memory allocation routines.
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun */
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun /* #define USE_DL_PREFIX */
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun /*
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun Special defines for linux libc
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun Except when compiled using these special defines for Linux libc
799*4882a593Smuzhiyun using weak aliases, this malloc is NOT designed to work in
800*4882a593Smuzhiyun multithreaded applications. No semaphores or other concurrency
801*4882a593Smuzhiyun control are provided to ensure that multiple malloc or free calls
802*4882a593Smuzhiyun don't run at the same time, which could be disasterous. A single
803*4882a593Smuzhiyun semaphore could be used across malloc, realloc, and free (which is
804*4882a593Smuzhiyun essentially the effect of the linux weak alias approach). It would
805*4882a593Smuzhiyun be hard to obtain finer granularity.
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun */
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun #ifdef INTERNAL_LINUX_C_LIB
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun #if __STD_C
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun Void_t * __default_morecore_init (ptrdiff_t);
815*4882a593Smuzhiyun Void_t *(*__morecore)(ptrdiff_t) = __default_morecore_init;
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun #else
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun Void_t * __default_morecore_init ();
820*4882a593Smuzhiyun Void_t *(*__morecore)() = __default_morecore_init;
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun #endif
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun #define MORECORE (*__morecore)
825*4882a593Smuzhiyun #define MORECORE_FAILURE 0
826*4882a593Smuzhiyun #define MORECORE_CLEARS 1
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun #else /* INTERNAL_LINUX_C_LIB */
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun #if __STD_C
831*4882a593Smuzhiyun extern Void_t* sbrk(ptrdiff_t);
832*4882a593Smuzhiyun #else
833*4882a593Smuzhiyun extern Void_t* sbrk();
834*4882a593Smuzhiyun #endif
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun #ifndef MORECORE
837*4882a593Smuzhiyun #define MORECORE sbrk
838*4882a593Smuzhiyun #endif
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun #ifndef MORECORE_FAILURE
841*4882a593Smuzhiyun #define MORECORE_FAILURE -1
842*4882a593Smuzhiyun #endif
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun #ifndef MORECORE_CLEARS
845*4882a593Smuzhiyun #define MORECORE_CLEARS 1
846*4882a593Smuzhiyun #endif
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun #endif /* INTERNAL_LINUX_C_LIB */
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun #if defined(INTERNAL_LINUX_C_LIB) && defined(__ELF__)
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun #define cALLOc __libc_calloc
853*4882a593Smuzhiyun #define fREe __libc_free
854*4882a593Smuzhiyun #define mALLOc __libc_malloc
855*4882a593Smuzhiyun #define mEMALIGn __libc_memalign
856*4882a593Smuzhiyun #define rEALLOc __libc_realloc
857*4882a593Smuzhiyun #define vALLOc __libc_valloc
858*4882a593Smuzhiyun #define pvALLOc __libc_pvalloc
859*4882a593Smuzhiyun #define mALLINFo __libc_mallinfo
860*4882a593Smuzhiyun #define mALLOPt __libc_mallopt
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun #pragma weak calloc = __libc_calloc
863*4882a593Smuzhiyun #pragma weak free = __libc_free
864*4882a593Smuzhiyun #pragma weak cfree = __libc_free
865*4882a593Smuzhiyun #pragma weak malloc = __libc_malloc
866*4882a593Smuzhiyun #pragma weak memalign = __libc_memalign
867*4882a593Smuzhiyun #pragma weak realloc = __libc_realloc
868*4882a593Smuzhiyun #pragma weak valloc = __libc_valloc
869*4882a593Smuzhiyun #pragma weak pvalloc = __libc_pvalloc
870*4882a593Smuzhiyun #pragma weak mallinfo = __libc_mallinfo
871*4882a593Smuzhiyun #pragma weak mallopt = __libc_mallopt
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun #else
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
876*4882a593Smuzhiyun #define malloc malloc_simple
877*4882a593Smuzhiyun #define realloc realloc_simple
878*4882a593Smuzhiyun #define memalign memalign_simple
free(void * ptr)879*4882a593Smuzhiyun static inline void free(void *ptr) {}
880*4882a593Smuzhiyun void *calloc(size_t nmemb, size_t size);
881*4882a593Smuzhiyun void *memalign_simple(size_t alignment, size_t bytes);
882*4882a593Smuzhiyun void *realloc_simple(void *ptr, size_t size);
883*4882a593Smuzhiyun #else
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun # ifdef USE_DL_PREFIX
886*4882a593Smuzhiyun # define cALLOc dlcalloc
887*4882a593Smuzhiyun # define fREe dlfree
888*4882a593Smuzhiyun # define mALLOc dlmalloc
889*4882a593Smuzhiyun # define mEMALIGn dlmemalign
890*4882a593Smuzhiyun # define rEALLOc dlrealloc
891*4882a593Smuzhiyun # define vALLOc dlvalloc
892*4882a593Smuzhiyun # define pvALLOc dlpvalloc
893*4882a593Smuzhiyun # define mALLINFo dlmallinfo
894*4882a593Smuzhiyun # define mALLOPt dlmallopt
895*4882a593Smuzhiyun # else /* USE_DL_PREFIX */
896*4882a593Smuzhiyun # define cALLOc calloc
897*4882a593Smuzhiyun # define fREe free
898*4882a593Smuzhiyun # define mALLOc malloc
899*4882a593Smuzhiyun # define mEMALIGn memalign
900*4882a593Smuzhiyun # define rEALLOc realloc
901*4882a593Smuzhiyun # define vALLOc valloc
902*4882a593Smuzhiyun # define pvALLOc pvalloc
903*4882a593Smuzhiyun # define mALLINFo mallinfo
904*4882a593Smuzhiyun # define mALLOPt mallopt
905*4882a593Smuzhiyun # endif /* USE_DL_PREFIX */
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun #endif
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun /* Set up pre-relocation malloc() ready for use */
910*4882a593Smuzhiyun int initf_malloc(void);
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun /* Public routines */
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun /* Simple versions which can be used when space is tight */
915*4882a593Smuzhiyun void *malloc_simple(size_t size);
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun #pragma GCC visibility push(hidden)
918*4882a593Smuzhiyun # if __STD_C
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun Void_t* mALLOc(size_t);
921*4882a593Smuzhiyun void fREe(Void_t*);
922*4882a593Smuzhiyun Void_t* rEALLOc(Void_t*, size_t);
923*4882a593Smuzhiyun Void_t* mEMALIGn(size_t, size_t);
924*4882a593Smuzhiyun Void_t* vALLOc(size_t);
925*4882a593Smuzhiyun Void_t* pvALLOc(size_t);
926*4882a593Smuzhiyun Void_t* cALLOc(size_t, size_t);
927*4882a593Smuzhiyun void cfree(Void_t*);
928*4882a593Smuzhiyun int malloc_trim(size_t);
929*4882a593Smuzhiyun size_t malloc_usable_size(Void_t*);
930*4882a593Smuzhiyun void malloc_stats(void);
931*4882a593Smuzhiyun int mALLOPt(int, int);
932*4882a593Smuzhiyun struct mallinfo mALLINFo(void);
933*4882a593Smuzhiyun # else
934*4882a593Smuzhiyun Void_t* mALLOc();
935*4882a593Smuzhiyun void fREe();
936*4882a593Smuzhiyun Void_t* rEALLOc();
937*4882a593Smuzhiyun Void_t* mEMALIGn();
938*4882a593Smuzhiyun Void_t* vALLOc();
939*4882a593Smuzhiyun Void_t* pvALLOc();
940*4882a593Smuzhiyun Void_t* cALLOc();
941*4882a593Smuzhiyun void cfree();
942*4882a593Smuzhiyun int malloc_trim();
943*4882a593Smuzhiyun size_t malloc_usable_size();
944*4882a593Smuzhiyun void malloc_stats();
945*4882a593Smuzhiyun int mALLOPt();
946*4882a593Smuzhiyun struct mallinfo mALLINFo();
947*4882a593Smuzhiyun # endif
948*4882a593Smuzhiyun #endif
949*4882a593Smuzhiyun #pragma GCC visibility pop
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun /*
952*4882a593Smuzhiyun * Begin and End of memory area for malloc(), and current "brk"
953*4882a593Smuzhiyun */
954*4882a593Smuzhiyun extern ulong mem_malloc_start;
955*4882a593Smuzhiyun extern ulong mem_malloc_end;
956*4882a593Smuzhiyun extern ulong mem_malloc_brk;
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun void mem_malloc_init(ulong start, ulong size);
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun #ifdef __cplusplus
961*4882a593Smuzhiyun }; /* end of extern "C" */
962*4882a593Smuzhiyun #endif
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun #endif /* __MALLOC_H__ */
965