2 This is a version (aka dlmalloc) of malloc/free/realloc written by
3 Doug Lea and released to the public domain, as explained at
4 http://creativecommons.org/publicdomain/zero/1.0/ Send questions,
5 comments, complaints, performance data, etc to dl@cs.oswego.edu
7 * Version 2.8.6 Wed Aug 29 06:57:58 2012 Doug Lea
8 Note: There may be an updated version of this malloc obtainable at
9 ftp://gee.cs.oswego.edu/pub/misc/malloc.c
10 Check before installing!
14 This library is all in one file to simplify the most common usage:
15 ftp it, compile it (-O3), and link it into another program. All of
16 the compile-time options default to reasonable values for use on
17 most platforms. You might later want to step through various
18 compile-time and dynamic tuning options.
20 For convenience, an include file for code using this malloc is at:
21 ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.6.h
22 You don't really need this .h file unless you call functions not
23 defined in your system include files. The .h file contains only the
24 excerpts from this file needed for using this malloc on ANSI C/C++
25 systems, so long as you haven't changed compile-time options about
26 naming and tuning parameters. If you do, then you can create your
27 own malloc.h that does include all settings by cutting at the point
28 indicated below. Note that you may already by default be using a C
29 library containing a malloc that is based on some version of this
30 malloc (for example in linux). You might still want to use the one
31 in this file to customize settings or to avoid overheads associated
32 with library versions.
36 Supported pointer/size_t representation: 4 or 8 bytes
37 size_t MUST be an unsigned type of the same width as
38 pointers. (If you are using an ancient system that declares
39 size_t as a signed type, or need it to be a different width
40 than pointers, you can use a previous release of this malloc
41 (e.g. 2.7.2) supporting these.)
43 Alignment: 8 bytes (minimum)
44 This suffices for nearly all current machines and C compilers.
45 However, you can define MALLOC_ALIGNMENT to be wider than this
46 if necessary (up to 128bytes), at the expense of using more space.
48 Minimum overhead per allocated chunk: 4 or 8 bytes (if 4byte sizes)
49 8 or 16 bytes (if 8byte sizes)
50 Each malloced chunk has a hidden word of overhead holding size
51 and status information, and additional cross-check word
52 if FOOTERS is defined.
54 Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead)
55 8-byte ptrs: 32 bytes (including overhead)
57 Even a request for zero bytes (i.e., malloc(0)) returns a
58 pointer to something of the minimum allocatable size.
59 The maximum overhead wastage (i.e., number of extra bytes
60 allocated than were requested in malloc) is less than or equal
61 to the minimum size, except for requests >= mmap_threshold that
62 are serviced via mmap(), where the worst case wastage is about
63 32 bytes plus the remainder from a system page (the minimal
64 mmap unit); typically 4096 or 8192 bytes.
66 Security: static-safe; optionally more or less
67 The "security" of malloc refers to the ability of malicious
68 code to accentuate the effects of errors (for example, freeing
69 space that is not currently malloc'ed or overwriting past the
70 ends of chunks) in code that calls malloc. This malloc
71 guarantees not to modify any memory locations below the base of
72 heap, i.e., static variables, even in the presence of usage
73 errors. The routines additionally detect most improper frees
74 and reallocs. All this holds as long as the static bookkeeping
75 for malloc itself is not corrupted by some other means. This
76 is only one aspect of security -- these checks do not, and
77 cannot, detect all possible programming errors.
79 If FOOTERS is defined nonzero, then each allocated chunk
80 carries an additional check word to verify that it was malloced
81 from its space. These check words are the same within each
82 execution of a program using malloc, but differ across
83 executions, so externally crafted fake chunks cannot be
84 freed. This improves security by rejecting frees/reallocs that
85 could corrupt heap memory, in addition to the checks preventing
86 writes to statics that are always on. This may further improve
87 security at the expense of time and space overhead. (Note that
88 FOOTERS may also be worth using with MSPACES.)
90 By default detected errors cause the program to abort (calling
91 "abort()"). You can override this to instead proceed past
92 errors by defining PROCEED_ON_ERROR. In this case, a bad free
93 has no effect, and a malloc that encounters a bad address
94 caused by user overwrites will ignore the bad address by
95 dropping pointers and indices to all known memory. This may
96 be appropriate for programs that should continue if at all
97 possible in the face of programming errors, although they may
98 run out of memory because dropped memory is never reclaimed.
100 If you don't like either of these options, you can define
101 CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything
102 else. And if if you are sure that your program using malloc has
103 no errors or vulnerabilities, you can define INSECURE to 1,
104 which might (or might not) provide a small performance improvement.
106 It is also possible to limit the maximum total allocatable
107 space, using malloc_set_footprint_limit. This is not
108 designed as a security feature in itself (calls to set limits
109 are not screened or privileged), but may be useful as one
110 aspect of a secure implementation.
112 Thread-safety: NOT thread-safe unless USE_LOCKS defined non-zero
113 When USE_LOCKS is defined, each public call to malloc, free,
114 etc is surrounded with a lock. By default, this uses a plain
115 pthread mutex, win32 critical section, or a spin-lock if if
116 available for the platform and not disabled by setting
117 USE_SPIN_LOCKS=0. However, if USE_RECURSIVE_LOCKS is defined,
118 recursive versions are used instead (which are not required for
119 base functionality but may be needed in layered extensions).
120 Using a global lock is not especially fast, and can be a major
121 bottleneck. It is designed only to provide minimal protection
122 in concurrent environments, and to provide a basis for
123 extensions. If you are using malloc in a concurrent program,
124 consider instead using nedmalloc
125 (http://www.nedprod.com/programs/portable/nedmalloc/) or
126 ptmalloc (See http://www.malloc.de), which are derived from
127 versions of this malloc.
129 System requirements: Any combination of MORECORE and/or MMAP/MUNMAP
130 This malloc can use unix sbrk or any emulation (invoked using
131 the CALL_MORECORE macro) and/or mmap/munmap or any emulation
132 (invoked using CALL_MMAP/CALL_MUNMAP) to get and release system
133 memory. On most unix systems, it tends to work best if both
134 MORECORE and MMAP are enabled. On Win32, it uses emulations
135 based on VirtualAlloc. It also uses common C library functions
138 Compliance: I believe it is compliant with the Single Unix Specification
139 (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably
142 * Overview of algorithms
144 This is not the fastest, most space-conserving, most portable, or
145 most tunable malloc ever written. However it is among the fastest
146 while also being among the most space-conserving, portable and
147 tunable. Consistent balance across these factors results in a good
148 general-purpose allocator for malloc-intensive programs.
150 In most ways, this malloc is a best-fit allocator. Generally, it
151 chooses the best-fitting existing chunk for a request, with ties
152 broken in approximately least-recently-used order. (This strategy
153 normally maintains low fragmentation.) However, for requests less
154 than 256bytes, it deviates from best-fit when there is not an
155 exactly fitting available chunk by preferring to use space adjacent
156 to that used for the previous small request, as well as by breaking
157 ties in approximately most-recently-used order. (These enhance
158 locality of series of small allocations.) And for very large requests
159 (>= 256Kb by default), it relies on system memory mapping
160 facilities, if supported. (This helps avoid carrying around and
161 possibly fragmenting memory used only for large chunks.)
163 All operations (except malloc_stats and mallinfo) have execution
164 times that are bounded by a constant factor of the number of bits in
165 a size_t, not counting any clearing in calloc or copying in realloc,
166 or actions surrounding MORECORE and MMAP that have times
167 proportional to the number of non-contiguous regions returned by
168 system allocation routines, which is often just 1. In real-time
169 applications, you can optionally suppress segment traversals using
170 NO_SEGMENT_TRAVERSAL, which assures bounded execution even when
171 system allocators return non-contiguous spaces, at the typical
172 expense of carrying around more memory and increased fragmentation.
174 The implementation is not very modular and seriously overuses
175 macros. Perhaps someday all C compilers will do as good a job
176 inlining modular code as can now be done by brute-force expansion,
177 but now, enough of them seem not to.
179 Some compilers issue a lot of warnings about code that is
180 dead/unreachable only on some platforms, and also about intentional
181 uses of negation on unsigned types. All known cases of each can be
184 For a longer but out of date high-level description, see
185 http://gee.cs.oswego.edu/dl/html/malloc.html
188 If MSPACES is defined, then in addition to malloc, free, etc.,
189 this file also defines mspace_malloc, mspace_free, etc. These
190 are versions of malloc routines that take an "mspace" argument
191 obtained using create_mspace, to control all internal bookkeeping.
192 If ONLY_MSPACES is defined, only these versions are compiled.
193 So if you would like to use this allocator for only some allocations,
194 and your system malloc for others, you can compile with
195 ONLY_MSPACES and then do something like...
196 static mspace mymspace = create_mspace(0,0); // for example
197 #define mymalloc(bytes) mspace_malloc(mymspace, bytes)
199 (Note: If you only need one instance of an mspace, you can instead
200 use "USE_DL_PREFIX" to relabel the global malloc.)
202 You can similarly create thread-local allocators by storing
203 mspaces as thread-locals. For example:
204 static __thread mspace tlms = 0;
205 void* tlmalloc(size_t bytes) {
206 if (tlms == 0) tlms = create_mspace(0, 0);
207 return mspace_malloc(tlms, bytes);
209 void tlfree(void* mem) { mspace_free(tlms, mem); }
211 Unless FOOTERS is defined, each mspace is completely independent.
212 You cannot allocate from one and free to another (although
213 conformance is only weakly checked, so usage errors are not always
214 caught). If FOOTERS is defined, then each chunk carries around a tag
215 indicating its originating mspace, and frees are directed to their
216 originating spaces. Normally, this requires use of locks.
218 ------------------------- Compile-time options ---------------------------
220 Be careful in setting #define values for numerical constants of type
221 size_t. On some systems, literal values are not automatically extended
222 to size_t precision unless they are explicitly casted. You can also
223 use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below.
225 WIN32 default: defined if _WIN32 defined
226 Defining WIN32 sets up defaults for MS environment and compilers.
227 Otherwise defaults are for unix. Beware that there seem to be some
228 cases where this malloc might not be a pure drop-in replacement for
229 Win32 malloc: Random-looking failures from Win32 GDI API's (eg;
230 SetDIBits()) may be due to bugs in some video driver implementations
231 when pixel buffers are malloc()ed, and the region spans more than
232 one VirtualAlloc()ed region. Because dlmalloc uses a small (64Kb)
233 default granularity, pixel buffers may straddle virtual allocation
234 regions more often than when using the Microsoft allocator. You can
235 avoid this by using VirtualAlloc() and VirtualFree() for all pixel
236 buffers rather than using malloc(). If this is not possible,
237 recompile this malloc with a larger DEFAULT_GRANULARITY. Note:
238 in cases where MSC and gcc (cygwin) are known to differ on WIN32,
239 conditions use _MSC_VER to distinguish them.
241 DLMALLOC_EXPORT default: extern
242 Defines how public APIs are declared. If you want to export via a
243 Windows DLL, you might define this as
244 #define DLMALLOC_EXPORT extern __declspec(dllexport)
245 If you want a POSIX ELF shared object, you might use
246 #define DLMALLOC_EXPORT extern __attribute__((visibility("default")))
248 MALLOC_ALIGNMENT default: (size_t)(2 * sizeof(void *))
249 Controls the minimum alignment for malloc'ed chunks. It must be a
250 power of two and at least 8, even on machines for which smaller
251 alignments would suffice. It may be defined as larger than this
252 though. Note however that code and data structures are optimized for
253 the case of 8-byte alignment.
255 MSPACES default: 0 (false)
256 If true, compile in support for independent allocation spaces.
257 This is only supported if HAVE_MMAP is true.
259 ONLY_MSPACES default: 0 (false)
260 If true, only compile in mspace versions, not regular versions.
262 USE_LOCKS default: 0 (false)
263 Causes each call to each public routine to be surrounded with
264 pthread or WIN32 mutex lock/unlock. (If set true, this can be
265 overridden on a per-mspace basis for mspace versions.) If set to a
266 non-zero value other than 1, locks are used, but their
267 implementation is left out, so lock functions must be supplied manually,
270 USE_SPIN_LOCKS default: 1 iff USE_LOCKS and spin locks available
271 If true, uses custom spin locks for locking. This is currently
272 supported only gcc >= 4.1, older gccs on x86 platforms, and recent
273 MS compilers. Otherwise, posix locks or win32 critical sections are
276 USE_RECURSIVE_LOCKS default: not defined
277 If defined nonzero, uses recursive (aka reentrant) locks, otherwise
278 uses plain mutexes. This is not required for malloc proper, but may
279 be needed for layered allocators such as nedmalloc.
281 LOCK_AT_FORK default: not defined
282 If defined nonzero, performs pthread_atfork upon initialization
283 to initialize child lock while holding parent lock. The implementation
284 assumes that pthread locks (not custom locks) are being used. In other
285 cases, you may need to customize the implementation.
288 If true, provide extra checking and dispatching by placing
289 information in the footers of allocated chunks. This adds
290 space and time overhead.
293 If true, omit checks for usage errors and heap space overwrites.
295 USE_DL_PREFIX default: NOT defined
296 Causes compiler to prefix all public routines with the string 'dl'.
297 This can be useful when you only want to use this malloc in one part
298 of a program, using your regular system malloc elsewhere.
300 MALLOC_INSPECT_ALL default: NOT defined
301 If defined, compiles malloc_inspect_all and mspace_inspect_all, that
302 perform traversal of all heap space. Unless access to these
303 functions is otherwise restricted, you probably do not want to
304 include them in secure implementations.
306 DLM_ABORT default: defined as abort()
307 Defines how to abort on failed checks. On most systems, a failed
308 check cannot die with an "assert" or even print an informative
309 message, because the underlying print routines in turn call malloc,
310 which will fail again. Generally, the best policy is to simply call
311 abort(). It's not very useful to do more than this because many
312 errors due to overwriting will show up as address faults (null, odd
313 addresses etc) rather than malloc-triggered checks, so will also
314 abort. Also, most compilers know that abort() does not return, so
315 can better optimize code conditionally calling it.
317 PROCEED_ON_ERROR default: defined as 0 (false)
318 Controls whether detected bad addresses cause them to bypassed
319 rather than aborting. If set, detected bad arguments to free and
320 realloc are ignored. And all bookkeeping information is zeroed out
321 upon a detected overwrite of freed heap space, thus losing the
322 ability to ever return it from malloc again, but enabling the
323 application to proceed. If PROCEED_ON_ERROR is defined, the
324 static variable malloc_corruption_error_count is compiled in
325 and can be examined to see if errors have occurred. This option
326 generates slower code than the default abort policy.
328 DEBUG default: NOT defined
329 The DEBUG setting is mainly intended for people trying to modify
330 this code or diagnose problems when porting to new platforms.
331 However, it may also be able to better isolate user errors than just
332 using runtime checks. The assertions in the check routines spell
333 out in more detail the assumptions and invariants underlying the
334 algorithms. The checking is fairly extensive, and will slow down
335 execution noticeably. Calling malloc_stats or mallinfo with DEBUG
336 set will attempt to check every non-mmapped allocated and free chunk
337 in the course of computing the summaries.
339 DLM_ABORT_ON_ASSERT_FAILURE default: defined as 1 (true)
340 Debugging assertion failures can be nearly impossible if your
341 version of the assert macro causes malloc to be called, which will
342 lead to a cascade of further failures, blowing the runtime stack.
343 DLM_ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(),
344 which will usually make debugging easier.
346 MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32
347 The action to take before "return 0" when malloc fails to be able to
348 return memory because there is none available.
350 HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES
351 True if this system supports sbrk or an emulation of it.
353 MORECORE default: sbrk
354 The name of the sbrk-style system routine to call to obtain more
355 memory. See below for guidance on writing custom MORECORE
356 functions. The type of the argument to sbrk/MORECORE varies across
357 systems. It cannot be size_t, because it supports negative
358 arguments, so it is normally the signed type of the same width as
359 size_t (sometimes declared as "intptr_t"). It doesn't much matter
360 though. Internally, we only call it with arguments less than half
361 the max value of a size_t, which should work across all reasonable
362 possibilities, although sometimes generating compiler warnings.
364 MORECORE_CONTIGUOUS default: 1 (true) if HAVE_MORECORE
365 If true, take advantage of fact that consecutive calls to MORECORE
366 with positive arguments always return contiguous increasing
367 addresses. This is true of unix sbrk. It does not hurt too much to
368 set it true anyway, since malloc copes with non-contiguities.
369 Setting it false when definitely non-contiguous saves time
370 and possibly wasted space it would take to discover this though.
372 MORECORE_CANNOT_TRIM default: NOT defined
373 True if MORECORE cannot release space back to the system when given
374 negative arguments. This is generally necessary only if you are
375 using a hand-crafted MORECORE function that cannot handle negative
378 NO_SEGMENT_TRAVERSAL default: 0
379 If non-zero, suppresses traversals of memory segments
380 returned by either MORECORE or CALL_MMAP. This disables
381 merging of segments that are contiguous, and selectively
382 releasing them to the OS if unused, but bounds execution times.
384 HAVE_MMAP default: 1 (true)
385 True if this system supports mmap or an emulation of it. If so, and
386 HAVE_MORECORE is not true, MMAP is used for all system
387 allocation. If set and HAVE_MORECORE is true as well, MMAP is
388 primarily used to directly allocate very large blocks. It is also
389 used as a backup strategy in cases where MORECORE fails to provide
390 space from system. Note: A single call to MUNMAP is assumed to be
391 able to unmap memory that may have be allocated using multiple calls
392 to MMAP, so long as they are adjacent.
394 HAVE_MREMAP default: 1 on linux, else 0
395 If true realloc() uses mremap() to re-allocate large blocks and
396 extend or shrink allocation spaces.
398 MMAP_CLEARS default: 1 except on WINCE.
399 True if mmap clears memory so calloc doesn't need to. This is true
400 for standard unix mmap using /dev/zero and on WIN32 except for WINCE.
402 USE_BUILTIN_FFS default: 0 (i.e., not used)
403 Causes malloc to use the builtin ffs() function to compute indices.
404 Some compilers may recognize and intrinsify ffs to be faster than the
405 supplied C version. Also, the case of x86 using gcc is special-cased
406 to an asm instruction, so is already as fast as it can be, and so
407 this setting has no effect. Similarly for Win32 under recent MS compilers.
408 (On most x86s, the asm version is only slightly faster than the C version.)
410 malloc_getpagesize default: derive from system includes, or 4096.
411 The system page size. To the extent possible, this malloc manages
412 memory from the system in page-size units. This may be (and
413 usually is) a function rather than a constant. This is ignored
414 if WIN32, where page size is determined using getSystemInfo during
417 USE_DEV_RANDOM default: 0 (i.e., not used)
418 Causes malloc to use /dev/random to initialize secure magic seed for
419 stamping footers. Otherwise, the current time is used.
421 NO_MALLINFO default: 0
422 If defined, don't compile "mallinfo". This can be a simple way
423 of dealing with mismatches between system declarations and
426 MALLINFO_FIELD_TYPE default: size_t
427 The type of the fields in the mallinfo struct. This was originally
428 defined as "int" in SVID etc, but is more usefully defined as
429 size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set
431 NO_MALLOC_STATS default: 0
432 If defined, don't compile "malloc_stats". This avoids calls to
433 fprintf and bringing in stdio dependencies you might not want.
435 REALLOC_ZERO_BYTES_FREES default: not defined
436 This should be set if a call to realloc with zero bytes should
437 be the same as a call to free. Some people think it should. Otherwise,
438 since this malloc returns a unique pointer for malloc(0), so does
441 LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H
442 LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H
443 LACKS_STDLIB_H LACKS_SCHED_H LACKS_TIME_H default: NOT defined unless on WIN32
444 Define these if your system does not have these header files.
445 You might need to manually insert some of the declarations they provide.
447 DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS,
448 system_info.dwAllocationGranularity in WIN32,
450 Also settable using mallopt(M_GRANULARITY, x)
451 The unit for allocating and deallocating memory from the system. On
452 most systems with contiguous MORECORE, there is no reason to
453 make this more than a page. However, systems with MMAP tend to
454 either require or encourage larger granularities. You can increase
455 this value to prevent system allocation functions to be called so
456 often, especially if they are slow. The value must be at least one
457 page and must be a power of two. Setting to 0 causes initialization
458 to either page size or win32 region size. (Note: In previous
459 versions of malloc, the equivalent of this option was called
462 DEFAULT_TRIM_THRESHOLD default: 2MB
463 Also settable using mallopt(M_TRIM_THRESHOLD, x)
464 The maximum amount of unused top-most memory to keep before
465 releasing via malloc_trim in free(). Automatic trimming is mainly
466 useful in long-lived programs using contiguous MORECORE. Because
467 trimming via sbrk can be slow on some systems, and can sometimes be
468 wasteful (in cases where programs immediately afterward allocate
469 more large chunks) the value should be high enough so that your
470 overall system performance would improve by releasing this much
471 memory. As a rough guide, you might set to a value close to the
472 average size of a process (program) running on your system.
473 Releasing this much memory would allow such a process to run in
474 memory. Generally, it is worth tuning trim thresholds when a
475 program undergoes phases where several large chunks are allocated
476 and released in ways that can reuse each other's storage, perhaps
477 mixed with phases where there are no such chunks at all. The trim
478 value must be greater than page size to have any useful effect. To
479 disable trimming completely, you can set to MAX_SIZE_T. Note that the trick
480 some people use of mallocing a huge space and then freeing it at
481 program startup, in an attempt to reserve system memory, doesn't
482 have the intended effect under automatic trimming, since that memory
483 will immediately be returned to the system.
485 DEFAULT_MMAP_THRESHOLD default: 256K
486 Also settable using mallopt(M_MMAP_THRESHOLD, x)
487 The request size threshold for using MMAP to directly service a
488 request. Requests of at least this size that cannot be allocated
489 using already-existing space will be serviced via mmap. (If enough
490 normal freed space already exists it is used instead.) Using mmap
491 segregates relatively large chunks of memory so that they can be
492 individually obtained and released from the host system. A request
493 serviced through mmap is never reused by any other request (at least
494 not directly; the system may just so happen to remap successive
495 requests to the same locations). Segregating space in this way has
496 the benefits that: Mmapped space can always be individually released
497 back to the system, which helps keep the system level memory demands
498 of a long-lived program low. Also, mapped memory doesn't become
499 `locked' between other chunks, as can happen with normally allocated
500 chunks, which means that even trimming via malloc_trim would not
501 release them. However, it has the disadvantage that the space
502 cannot be reclaimed, consolidated, and then used to service later
503 requests, as happens with normal chunks. The advantages of mmap
504 nearly always outweigh disadvantages for "large" chunks, but the
505 value of "large" may vary across systems. The default is an
506 empirically derived value that works well in most systems. You can
507 disable mmap by setting to MAX_SIZE_T.
509 MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP
510 The number of consolidated frees between checks to release
511 unused segments when freeing. When using non-contiguous segments,
512 especially with multiple mspaces, checking only for topmost space
513 doesn't always suffice to trigger trimming. To compensate for this,
514 free() will, with a period of MAX_RELEASE_CHECK_RATE (or the
515 current number of segments, if greater) try to release unused
516 segments to the OS when freeing chunks that result in
517 consolidation. The best value for this parameter is a compromise
518 between slowing down frees with relatively costly checks that
519 rarely trigger versus holding on to unused memory. To effectively
520 disable, set to MAX_SIZE_T. This may lead to a very slight speed
521 improvement at the expense of carrying around more memory.
524 #include <vppinfra/clib.h>
525 #include <vppinfra/cache.h>
527 /* Version identifier to allow people to support multiple versions */
528 #ifndef DLMALLOC_VERSION
529 #define DLMALLOC_VERSION 20806
530 #endif /* DLMALLOC_VERSION */
532 #ifndef DLMALLOC_EXPORT
533 #define DLMALLOC_EXPORT extern
541 #define LACKS_FCNTL_H
543 #endif /* _WIN32_WCE */
546 #define WIN32_LEAN_AND_MEAN
550 #define HAVE_MORECORE 0
551 #define LACKS_UNISTD_H
552 #define LACKS_SYS_PARAM_H
553 #define LACKS_SYS_MMAN_H
554 #define LACKS_STRING_H
555 #define LACKS_STRINGS_H
556 #define LACKS_SYS_TYPES_H
557 #define LACKS_ERRNO_H
558 #define LACKS_SCHED_H
559 #ifndef MALLOC_FAILURE_ACTION
560 #define MALLOC_FAILURE_ACTION
561 #endif /* MALLOC_FAILURE_ACTION */
563 #ifdef _WIN32_WCE /* WINCE reportedly does not clear */
564 #define MMAP_CLEARS 0
566 #define MMAP_CLEARS 1
567 #endif /* _WIN32_WCE */
568 #endif /*MMAP_CLEARS */
571 #if defined(DARWIN) || defined(_DARWIN)
572 /* Mac OSX docs advise not to use sbrk; it seems better to use mmap */
573 #ifndef HAVE_MORECORE
574 #define HAVE_MORECORE 0
576 /* OSX allocators provide 16 byte alignment */
577 #ifndef MALLOC_ALIGNMENT
578 #define MALLOC_ALIGNMENT ((size_t)16U)
580 #endif /* HAVE_MORECORE */
583 #ifndef LACKS_SYS_TYPES_H
584 #include <sys/types.h> /* For size_t */
585 #endif /* LACKS_SYS_TYPES_H */
587 /* The maximum possible size_t value has all bits set */
588 #define MAX_SIZE_T (~(size_t)0)
592 #ifndef USE_LOCKS /* ensure true if spin or recursive locks set */
593 #define USE_LOCKS ((defined(USE_SPIN_LOCKS) && USE_SPIN_LOCKS != 0) || \
594 (defined(USE_RECURSIVE_LOCKS) && USE_RECURSIVE_LOCKS != 0))
595 #endif /* USE_LOCKS */
597 #if USE_LOCKS /* Spin locks for gcc >= 4.1, older gcc on x86, MSC >= 1310 */
598 #if ((defined(__GNUC__) && \
599 ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) || \
600 defined(__i386__) || defined(__x86_64__))) || \
601 (defined(_MSC_VER) && _MSC_VER>=1310))
602 #ifndef USE_SPIN_LOCKS
603 #define USE_SPIN_LOCKS 1
604 #endif /* USE_SPIN_LOCKS */
606 #error "USE_SPIN_LOCKS defined without implementation"
607 #endif /* ... locks available... */
608 #elif !defined(USE_SPIN_LOCKS)
609 #define USE_SPIN_LOCKS 0
610 #endif /* USE_LOCKS */
613 #define ONLY_MSPACES 1
614 #endif /* ONLY_MSPACES */
618 #else /* ONLY_MSPACES */
620 #endif /* ONLY_MSPACES */
622 #ifndef MALLOC_ALIGNMENT
623 #define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *)))
624 #endif /* MALLOC_ALIGNMENT */
629 #define DLM_ABORT abort()
630 #endif /* DLM_ABORT */
631 #ifndef DLM_ABORT_ON_ASSERT_FAILURE
632 #define DLM_ABORT_ON_ASSERT_FAILURE 1
633 #endif /* DLM_ABORT_ON_ASSERT_FAILURE */
634 #ifndef PROCEED_ON_ERROR
635 #define PROCEED_ON_ERROR 0
636 #endif /* PROCEED_ON_ERROR */
640 #endif /* INSECURE */
641 #ifndef MALLOC_INSPECT_ALL
642 #define MALLOC_INSPECT_ALL 0
643 #endif /* MALLOC_INSPECT_ALL */
646 #endif /* HAVE_MMAP */
648 #define MMAP_CLEARS 1
649 #endif /* MMAP_CLEARS */
652 #define HAVE_MREMAP 1
653 #define _GNU_SOURCE /* Turns on mremap() definition */
655 #define HAVE_MREMAP 0
657 #endif /* HAVE_MREMAP */
658 #ifndef MALLOC_FAILURE_ACTION
659 #define MALLOC_FAILURE_ACTION errno = ENOMEM;
660 #endif /* MALLOC_FAILURE_ACTION */
661 #ifndef HAVE_MORECORE
663 #define HAVE_MORECORE 0
664 #else /* ONLY_MSPACES */
665 #define HAVE_MORECORE 1
666 #endif /* ONLY_MSPACES */
667 #endif /* HAVE_MORECORE */
669 #define MORECORE_CONTIGUOUS 0
670 #else /* !HAVE_MORECORE */
671 #define MORECORE_DEFAULT sbrk
672 #ifndef MORECORE_CONTIGUOUS
673 #define MORECORE_CONTIGUOUS 1
674 #endif /* MORECORE_CONTIGUOUS */
675 #endif /* HAVE_MORECORE */
676 #ifndef DEFAULT_GRANULARITY
677 #if (MORECORE_CONTIGUOUS || defined(WIN32))
678 #define DEFAULT_GRANULARITY (0) /* 0 means to compute in init_mparams */
679 #else /* MORECORE_CONTIGUOUS */
680 #define DEFAULT_GRANULARITY ((size_t)64U * (size_t)1024U)
681 #endif /* MORECORE_CONTIGUOUS */
682 #endif /* DEFAULT_GRANULARITY */
683 #ifndef DEFAULT_TRIM_THRESHOLD
684 #ifndef MORECORE_CANNOT_TRIM
685 #define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U)
686 #else /* MORECORE_CANNOT_TRIM */
687 #define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T
688 #endif /* MORECORE_CANNOT_TRIM */
689 #endif /* DEFAULT_TRIM_THRESHOLD */
690 #ifndef DEFAULT_MMAP_THRESHOLD
692 #define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U)
693 #else /* HAVE_MMAP */
694 #define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T
695 #endif /* HAVE_MMAP */
696 #endif /* DEFAULT_MMAP_THRESHOLD */
697 #ifndef MAX_RELEASE_CHECK_RATE
699 #define MAX_RELEASE_CHECK_RATE 4095
701 #define MAX_RELEASE_CHECK_RATE MAX_SIZE_T
702 #endif /* HAVE_MMAP */
703 #endif /* MAX_RELEASE_CHECK_RATE */
704 #ifndef USE_BUILTIN_FFS
705 #define USE_BUILTIN_FFS 0
706 #endif /* USE_BUILTIN_FFS */
707 #ifndef USE_DEV_RANDOM
708 #define USE_DEV_RANDOM 0
709 #endif /* USE_DEV_RANDOM */
711 #define NO_MALLINFO 0
712 #endif /* NO_MALLINFO */
713 #ifndef MALLINFO_FIELD_TYPE
714 #define MALLINFO_FIELD_TYPE size_t
715 #endif /* MALLINFO_FIELD_TYPE */
716 #ifndef NO_MALLOC_STATS
717 #define NO_MALLOC_STATS 0
718 #endif /* NO_MALLOC_STATS */
719 #ifndef NO_SEGMENT_TRAVERSAL
720 #define NO_SEGMENT_TRAVERSAL 0
721 #endif /* NO_SEGMENT_TRAVERSAL */
724 mallopt tuning options. SVID/XPG defines four standard parameter
725 numbers for mallopt, normally defined in malloc.h. None of these
726 are used in this malloc, so setting them has no effect. But this
727 malloc does support the following options.
730 #define M_TRIM_THRESHOLD (-1)
731 #define M_GRANULARITY (-2)
732 #define M_MMAP_THRESHOLD (-3)
734 /* ------------------------ Mallinfo declarations ------------------------ */
738 This version of malloc supports the standard SVID/XPG mallinfo
739 routine that returns a struct containing usage properties and
740 statistics. It should work on any system that has a
741 /usr/include/malloc.h defining struct mallinfo. The main
742 declaration needed is the mallinfo struct that is returned (by-copy)
743 by mallinfo(). The malloinfo struct contains a bunch of fields that
744 are not even meaningful in this version of malloc. These fields are
745 are instead filled by mallinfo() with other numbers that might be of
748 HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
749 /usr/include/malloc.h file that includes a declaration of struct
750 mallinfo. If so, it is included; else a compliant version is
751 declared below. These must be precisely the same for mallinfo() to
752 work. The original SVID version of this struct, defined on most
753 systems with mallinfo, declares all fields as ints. But some others
754 define as unsigned long. If your system defines the fields using a
755 type of different width than listed here, you MUST #include your
756 system version and #define HAVE_USR_INCLUDE_MALLOC_H.
759 /* #define HAVE_USR_INCLUDE_MALLOC_H */
761 #ifdef HAVE_USR_INCLUDE_MALLOC_H
762 #include "/usr/include/malloc.h"
763 #else /* HAVE_USR_INCLUDE_MALLOC_H */
764 #ifndef STRUCT_MALLINFO_DECLARED
765 /* HP-UX (and others?) redefines mallinfo unless _STRUCT_MALLINFO is defined */
766 #define _STRUCT_MALLINFO
767 #define STRUCT_MALLINFO_DECLARED 1
769 MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */
770 MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */
771 MALLINFO_FIELD_TYPE smblks; /* always 0 */
772 MALLINFO_FIELD_TYPE hblks; /* always 0 */
773 MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */
774 MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */
775 MALLINFO_FIELD_TYPE fsmblks; /* always 0 */
776 MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
777 MALLINFO_FIELD_TYPE fordblks; /* total free space */
778 MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
780 #endif /* STRUCT_MALLINFO_DECLARED */
781 #endif /* HAVE_USR_INCLUDE_MALLOC_H */
782 #endif /* NO_MALLINFO */
785 Try to persuade compilers to inline. The most critical functions for
786 inlining are defined as macros, so these aren't used for them.
790 #if defined(__GNUC__)
791 #define FORCEINLINE __inline __attribute__ ((always_inline))
792 #elif defined(_MSC_VER)
793 #define FORCEINLINE __forceinline
797 #if defined(__GNUC__)
798 #define NOINLINE __attribute__ ((noinline))
799 #elif defined(_MSC_VER)
800 #define NOINLINE __declspec(noinline)
809 #define FORCEINLINE inline
811 #endif /* __cplusplus */
818 /* ------------------- Declarations of public routines ------------------- */
820 #ifndef USE_DL_PREFIX
821 #define dlcalloc calloc
823 #define dlmalloc malloc
824 #define dlmemalign memalign
825 #define dlposix_memalign posix_memalign
826 #define dlrealloc realloc
827 #define dlrealloc_in_place realloc_in_place
828 #define dlvalloc valloc
829 #define dlpvalloc pvalloc
830 #define dlmallinfo mallinfo
831 #define dlmallopt mallopt
832 #define dlmalloc_trim malloc_trim
833 #define dlmalloc_stats malloc_stats
834 #define dlmalloc_usable_size malloc_usable_size
835 #define dlmalloc_footprint malloc_footprint
836 #define dlmalloc_max_footprint malloc_max_footprint
837 #define dlmalloc_footprint_limit malloc_footprint_limit
838 #define dlmalloc_set_footprint_limit malloc_set_footprint_limit
839 #define dlmalloc_inspect_all malloc_inspect_all
840 #define dlindependent_calloc independent_calloc
841 #define dlindependent_comalloc independent_comalloc
842 #define dlbulk_free bulk_free
843 #endif /* USE_DL_PREFIX */
847 Returns a pointer to a newly allocated chunk of at least n bytes, or
848 null if no space is available, in which case errno is set to ENOMEM
851 If n is zero, malloc returns a minimum-sized chunk. (The minimum
852 size is 16 bytes on most 32bit systems, and 32 bytes on 64bit
853 systems.) Note that size_t is an unsigned type, so calls with
854 arguments that would be negative if signed are interpreted as
855 requests for huge amounts of space, which will often fail. The
856 maximum supported value of n differs across systems, but is in all
857 cases less than the maximum representable value of a size_t.
859 DLMALLOC_EXPORT void* dlmalloc(size_t);
863 Releases the chunk of memory pointed to by p, that had been previously
864 allocated using malloc or a related routine such as realloc.
865 It has no effect if p is null. If p was not malloced or already
866 freed, free(p) will by default cause the current program to abort.
868 DLMALLOC_EXPORT void dlfree(void*);
871 calloc(size_t n_elements, size_t element_size);
872 Returns a pointer to n_elements * element_size bytes, with all locations
875 DLMALLOC_EXPORT void* dlcalloc(size_t, size_t);
878 realloc(void* p, size_t n)
879 Returns a pointer to a chunk of size n that contains the same data
880 as does chunk p up to the minimum of (n, p's size) bytes, or null
881 if no space is available.
883 The returned pointer may or may not be the same as p. The algorithm
884 prefers extending p in most cases when possible, otherwise it
885 employs the equivalent of a malloc-copy-free sequence.
887 If p is null, realloc is equivalent to malloc.
889 If space is not available, realloc returns null, errno is set (if on
890 ANSI) and p is NOT freed.
892 if n is for fewer bytes than already held by p, the newly unused
893 space is lopped off and freed if possible. realloc with a size
894 argument of zero (re)allocates a minimum-sized chunk.
896 The old unix realloc convention of allowing the last-free'd chunk
897 to be used as an argument to realloc is not supported.
899 DLMALLOC_EXPORT void* dlrealloc(void*, size_t);
902 realloc_in_place(void* p, size_t n)
903 Resizes the space allocated for p to size n, only if this can be
904 done without moving p (i.e., only if there is adjacent space
905 available if n is greater than p's current allocated size, or n is
906 less than or equal to p's size). This may be used instead of plain
907 realloc if an alternative allocation strategy is needed upon failure
908 to expand space; for example, reallocation of a buffer that must be
909 memory-aligned or cleared. You can use realloc_in_place to trigger
910 these alternatives only when needed.
912 Returns p if successful; otherwise null.
914 DLMALLOC_EXPORT void* dlrealloc_in_place(void*, size_t);
917 memalign(size_t alignment, size_t n);
918 Returns a pointer to a newly allocated chunk of n bytes, aligned
919 in accord with the alignment argument.
921 The alignment argument should be a power of two. If the argument is
922 not a power of two, the nearest greater power is used.
923 8-byte alignment is guaranteed by normal malloc calls, so don't
924 bother calling memalign with an argument of 8 or less.
926 Overreliance on memalign is a sure way to fragment space.
928 DLMALLOC_EXPORT void* dlmemalign(size_t, size_t);
931 int posix_memalign(void** pp, size_t alignment, size_t n);
932 Allocates a chunk of n bytes, aligned in accord with the alignment
933 argument. Differs from memalign only in that it (1) assigns the
934 allocated memory to *pp rather than returning it, (2) fails and
935 returns EINVAL if the alignment is not a power of two (3) fails and
936 returns ENOMEM if memory cannot be allocated.
938 DLMALLOC_EXPORT int dlposix_memalign(void**, size_t, size_t);
942 Equivalent to memalign(pagesize, n), where pagesize is the page
943 size of the system. If the pagesize is unknown, 4096 is used.
945 DLMALLOC_EXPORT void* dlvalloc(size_t);
948 mallopt(int parameter_number, int parameter_value)
949 Sets tunable parameters The format is to provide a
950 (parameter-number, parameter-value) pair. mallopt then sets the
951 corresponding parameter to the argument value if it can (i.e., so
952 long as the value is meaningful), and returns 1 if successful else
953 0. To workaround the fact that mallopt is specified to use int,
954 not size_t parameters, the value -1 is specially treated as the
955 maximum unsigned size_t value.
957 SVID/XPG/ANSI defines four standard param numbers for mallopt,
958 normally defined in malloc.h. None of these are use in this malloc,
959 so setting them has no effect. But this malloc also supports other
960 options in mallopt. See below for details. Briefly, supported
961 parameters are as follows (listed defaults are for "typical"
964 Symbol param # default allowed param values
965 M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables)
966 M_GRANULARITY -2 page size any power of 2 >= page size
967 M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support)
969 DLMALLOC_EXPORT int dlmallopt(int, int);
973 Returns the number of bytes obtained from the system. The total
974 number of bytes allocated by malloc, realloc etc., is less than this
975 value. Unlike mallinfo, this function returns only a precomputed
976 result, so can be called frequently to monitor memory consumption.
977 Even if locks are otherwise defined, this function does not use them,
978 so results might not be up to date.
980 DLMALLOC_EXPORT size_t dlmalloc_footprint(void);
983 malloc_max_footprint();
984 Returns the maximum number of bytes obtained from the system. This
985 value will be greater than current footprint if deallocated space
986 has been reclaimed by the system. The peak number of bytes allocated
987 by malloc, realloc etc., is less than this value. Unlike mallinfo,
988 this function returns only a precomputed result, so can be called
989 frequently to monitor memory consumption. Even if locks are
990 otherwise defined, this function does not use them, so results might
993 DLMALLOC_EXPORT size_t dlmalloc_max_footprint(void);
996 malloc_footprint_limit();
997 Returns the number of bytes that the heap is allowed to obtain from
998 the system, returning the last value returned by
999 malloc_set_footprint_limit, or the maximum size_t value if
1000 never set. The returned value reflects a permission. There is no
1001 guarantee that this number of bytes can actually be obtained from
1004 DLMALLOC_EXPORT size_t dlmalloc_footprint_limit();
1007 malloc_set_footprint_limit();
1008 Sets the maximum number of bytes to obtain from the system, causing
1009 failure returns from malloc and related functions upon attempts to
1010 exceed this value. The argument value may be subject to page
1011 rounding to an enforceable limit; this actual value is returned.
1012 Using an argument of the maximum possible size_t effectively
1013 disables checks. If the argument is less than or equal to the
1014 current malloc_footprint, then all future allocations that require
1015 additional system memory will fail. However, invocation cannot
1016 retroactively deallocate existing used memory.
1018 DLMALLOC_EXPORT size_t dlmalloc_set_footprint_limit(size_t bytes);
1020 #if MALLOC_INSPECT_ALL
1022 malloc_inspect_all(void(*handler)(void *start,
1025 void* callback_arg),
1027 Traverses the heap and calls the given handler for each managed
1028 region, skipping all bytes that are (or may be) used for bookkeeping
1029 purposes. Traversal does not include include chunks that have been
1030 directly memory mapped. Each reported region begins at the start
1031 address, and continues up to but not including the end address. The
1032 first used_bytes of the region contain allocated data. If
1033 used_bytes is zero, the region is unallocated. The handler is
1034 invoked with the given callback argument. If locks are defined, they
1035 are held during the entire traversal. It is a bad idea to invoke
1036 other malloc functions from within the handler.
1038 For example, to count the number of in-use chunks with size greater
1039 than 1000, you could write:
1040 static int count = 0;
1041 void count_chunks(void* start, void* end, size_t used, void* arg) {
1042 if (used >= 1000) ++count;
1045 malloc_inspect_all(count_chunks, NULL);
1047 malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined.
1049 DLMALLOC_EXPORT void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*),
1052 #endif /* MALLOC_INSPECT_ALL */
1057 Returns (by copy) a struct containing various summary statistics:
1059 arena: current total non-mmapped bytes allocated from system
1060 ordblks: the number of free chunks
1061 smblks: always zero.
1062 hblks: current number of mmapped regions
1063 hblkhd: total bytes held in mmapped regions
1064 usmblks: the maximum total allocated space. This will be greater
1065 than current total if trimming has occurred.
1066 fsmblks: always zero
1067 uordblks: current total allocated space (normal or mmapped)
1068 fordblks: total free space
1069 keepcost: the maximum number of bytes that could ideally be released
1070 back to system via malloc_trim. ("ideally" means that
1071 it ignores page restrictions etc.)
1073 Because these fields are ints, but internal bookkeeping may
1074 be kept as longs, the reported values may wrap around zero and
1077 DLMALLOC_EXPORT struct mallinfo dlmallinfo(void);
1078 #endif /* NO_MALLINFO */
1081 independent_calloc(size_t n_elements, size_t element_size, void* chunks[]);
1083 independent_calloc is similar to calloc, but instead of returning a
1084 single cleared space, it returns an array of pointers to n_elements
1085 independent elements that can hold contents of size elem_size, each
1086 of which starts out cleared, and can be independently freed,
1087 realloc'ed etc. The elements are guaranteed to be adjacently
1088 allocated (this is not guaranteed to occur with multiple callocs or
1089 mallocs), which may also improve cache locality in some
1092 The "chunks" argument is optional (i.e., may be null, which is
1093 probably the most typical usage). If it is null, the returned array
1094 is itself dynamically allocated and should also be freed when it is
1095 no longer needed. Otherwise, the chunks array must be of at least
1096 n_elements in length. It is filled in with the pointers to the
1099 In either case, independent_calloc returns this pointer array, or
1100 null if the allocation failed. If n_elements is zero and "chunks"
1101 is null, it returns a chunk representing an array with zero elements
1102 (which should be freed if not wanted).
1104 Each element must be freed when it is no longer needed. This can be
1105 done all at once using bulk_free.
1107 independent_calloc simplifies and speeds up implementations of many
1108 kinds of pools. It may also be useful when constructing large data
1109 structures that initially have a fixed number of fixed-sized nodes,
1110 but the number is not known at compile time, and some of the nodes
1111 may later need to be freed. For example:
1113 struct Node { int item; struct Node* next; };
1115 struct Node* build_list() {
1117 int n = read_number_of_nodes_needed();
1118 if (n <= 0) return 0;
1119 pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
1120 if (pool == 0) die();
1121 // organize into a linked list...
1122 struct Node* first = pool[0];
1123 for (i = 0; i < n-1; ++i)
1124 pool[i]->next = pool[i+1];
1125 free(pool); // Can now free the array (or not, if it is needed later)
1129 DLMALLOC_EXPORT void** dlindependent_calloc(size_t, size_t, void**);
1132 independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);
1134 independent_comalloc allocates, all at once, a set of n_elements
1135 chunks with sizes indicated in the "sizes" array. It returns
1136 an array of pointers to these elements, each of which can be
1137 independently freed, realloc'ed etc. The elements are guaranteed to
1138 be adjacently allocated (this is not guaranteed to occur with
1139 multiple callocs or mallocs), which may also improve cache locality
1140 in some applications.
1142 The "chunks" argument is optional (i.e., may be null). If it is null
1143 the returned array is itself dynamically allocated and should also
1144 be freed when it is no longer needed. Otherwise, the chunks array
1145 must be of at least n_elements in length. It is filled in with the
1146 pointers to the chunks.
1148 In either case, independent_comalloc returns this pointer array, or
1149 null if the allocation failed. If n_elements is zero and chunks is
1150 null, it returns a chunk representing an array with zero elements
1151 (which should be freed if not wanted).
1153 Each element must be freed when it is no longer needed. This can be
1154 done all at once using bulk_free.
1156 independent_comallac differs from independent_calloc in that each
1157 element may have a different size, and also that it does not
1158 automatically clear elements.
1160 independent_comalloc can be used to speed up allocation in cases
1161 where several structs or objects must always be allocated at the
1162 same time. For example:
1167 void send_message(char* msg) {
1168 int msglen = strlen(msg);
1169 size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
1171 if (independent_comalloc(3, sizes, chunks) == 0)
1173 struct Head* head = (struct Head*)(chunks[0]);
1174 char* body = (char*)(chunks[1]);
1175 struct Foot* foot = (struct Foot*)(chunks[2]);
1179 In general though, independent_comalloc is worth using only for
1180 larger values of n_elements. For small values, you probably won't
1181 detect enough difference from series of malloc calls to bother.
1183 Overuse of independent_comalloc can increase overall memory usage,
1184 since it cannot reuse existing noncontiguous small chunks that
1185 might be available for some of the elements.
1187 DLMALLOC_EXPORT void** dlindependent_comalloc(size_t, size_t*, void**);
1190 bulk_free(void* array[], size_t n_elements)
1191 Frees and clears (sets to null) each non-null pointer in the given
1192 array. This is likely to be faster than freeing them one-by-one.
1193 If footers are used, pointers that have been allocated in different
1194 mspaces are not freed or cleared, and the count of all such pointers
1195 is returned. For large arrays of pointers with poor locality, it
1196 may be worthwhile to sort this array before calling bulk_free.
1198 DLMALLOC_EXPORT size_t dlbulk_free(void**, size_t n_elements);
1202 Equivalent to valloc(minimum-page-that-holds(n)), that is,
1203 round up n to nearest pagesize.
1205 DLMALLOC_EXPORT void* dlpvalloc(size_t);
1208 malloc_trim(size_t pad);
1210 If possible, gives memory back to the system (via negative arguments
1211 to sbrk) if there is unused memory at the `high' end of the malloc
1212 pool or in unused MMAP segments. You can call this after freeing
1213 large blocks of memory to potentially reduce the system-level memory
1214 requirements of a program. However, it cannot guarantee to reduce
1215 memory. Under some allocation patterns, some large free blocks of
1216 memory will be locked between two used chunks, so they cannot be
1217 given back to the system.
1219 The `pad' argument to malloc_trim represents the amount of free
1220 trailing space to leave untrimmed. If this argument is zero, only
1221 the minimum amount of memory to maintain internal data structures
1222 will be left. Non-zero arguments can be supplied to maintain enough
1223 trailing space to service future expected allocations without having
1224 to re-obtain memory from the system.
1226 Malloc_trim returns 1 if it actually released any memory, else 0.
1228 DLMALLOC_EXPORT int dlmalloc_trim(size_t);
1232 Prints on stderr the amount of space obtained from the system (both
1233 via sbrk and mmap), the maximum amount (which may be more than
1234 current if malloc_trim and/or munmap got called), and the current
1235 number of bytes allocated via malloc (or realloc, etc) but not yet
1236 freed. Note that this is the number of bytes allocated, not the
1237 number requested. It will be larger than the number requested
1238 because of alignment and bookkeeping overhead. Because it includes
1239 alignment wastage as being in use, this figure may be greater than
1240 zero even when no user-level chunks are allocated.
1242 The reported current and maximum system memory can be inaccurate if
1243 a program makes other calls to system memory allocation functions
1244 (normally sbrk) outside of malloc.
1246 malloc_stats prints only the most commonly interesting statistics.
1247 More information can be obtained by calling mallinfo.
1249 DLMALLOC_EXPORT void dlmalloc_stats(void);
1252 malloc_usable_size(void* p);
1254 Returns the number of bytes you can actually use in
1255 an allocated chunk, which may be more than you requested (although
1256 often not) due to alignment and minimum size constraints.
1257 You can use this many bytes without worrying about
1258 overwriting other allocated objects. This is not a particularly great
1259 programming practice. malloc_usable_size can be more useful in
1260 debugging and assertions, for example:
1263 assert(malloc_usable_size(p) >= 256);
1265 size_t dlmalloc_usable_size(void*);
1267 #endif /* ONLY_MSPACES */
1272 mspace is an opaque type representing an independent
1273 region of space that supports mspace_malloc, etc.
1275 typedef void* mspace;
1278 create_mspace creates and returns a new independent space with the
1279 given initial capacity, or, if 0, the default granularity size. It
1280 returns null if there is no system memory available to create the
1281 space. If argument locked is non-zero, the space uses a separate
1282 lock to control access. The capacity of the space will grow
1283 dynamically as needed to service mspace_malloc requests. You can
1284 control the sizes of incremental increases of this space by
1285 compiling with a different DEFAULT_GRANULARITY or dynamically
1286 setting with mallopt(M_GRANULARITY, value).
1288 DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked);
1291 destroy_mspace destroys the given space, and attempts to return all
1292 of its memory back to the system, returning the total number of
1293 bytes freed. After destruction, the results of access to all memory
1294 used by the space become undefined.
1296 DLMALLOC_EXPORT size_t destroy_mspace(mspace msp);
1299 create_mspace_with_base uses the memory supplied as the initial base
1300 of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this
1301 space is used for bookkeeping, so the capacity must be at least this
1302 large. (Otherwise 0 is returned.) When this initial space is
1303 exhausted, additional memory will be obtained from the system.
1304 Destroying this space will deallocate all additionally allocated
1305 space (if possible) but not the initial base.
1307 DLMALLOC_EXPORT mspace create_mspace_with_base(void* base, size_t capacity, int locked);
1310 mspace_track_large_chunks controls whether requests for large chunks
1311 are allocated in their own untracked mmapped regions, separate from
1312 others in this mspace. By default large chunks are not tracked,
1313 which reduces fragmentation. However, such chunks are not
1314 necessarily released to the system upon destroy_mspace. Enabling
1315 tracking by setting to true may increase fragmentation, but avoids
1316 leakage when relying on destroy_mspace to release all memory
1317 allocated using this space. The function returns the previous
1320 DLMALLOC_EXPORT int mspace_track_large_chunks(mspace msp, int enable);
1324 mspace_malloc behaves as malloc, but operates within
1327 DLMALLOC_EXPORT void* mspace_malloc(mspace msp, size_t bytes);
1330 mspace_free behaves as free, but operates within
1333 If compiled with FOOTERS==1, mspace_free is not actually needed.
1334 free may be called instead of mspace_free because freed chunks from
1335 any space are handled by their originating spaces.
1337 DLMALLOC_EXPORT void mspace_free(mspace msp, void* mem);
1340 mspace_realloc behaves as realloc, but operates within
1343 If compiled with FOOTERS==1, mspace_realloc is not actually
1344 needed. realloc may be called instead of mspace_realloc because
1345 realloced chunks from any space are handled by their originating
1348 DLMALLOC_EXPORT void* mspace_realloc(mspace msp, void* mem, size_t newsize);
1351 mspace_calloc behaves as calloc, but operates within
1354 DLMALLOC_EXPORT void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
1357 mspace_memalign behaves as memalign, but operates within
1360 DLMALLOC_EXPORT void* mspace_memalign(mspace msp, size_t alignment, size_t bytes);
1363 mspace_independent_calloc behaves as independent_calloc, but
1364 operates within the given space.
1366 DLMALLOC_EXPORT void** mspace_independent_calloc(mspace msp, size_t n_elements,
1367 size_t elem_size, void* chunks[]);
1370 mspace_independent_comalloc behaves as independent_comalloc, but
1371 operates within the given space.
1373 DLMALLOC_EXPORT void** mspace_independent_comalloc(mspace msp, size_t n_elements,
1374 size_t sizes[], void* chunks[]);
1377 mspace_footprint() returns the number of bytes obtained from the
1378 system for this space.
1380 DLMALLOC_EXPORT size_t mspace_footprint(mspace msp);
1383 mspace_max_footprint() returns the peak number of bytes obtained from the
1384 system for this space.
1386 DLMALLOC_EXPORT size_t mspace_max_footprint(mspace msp);
1391 mspace_mallinfo behaves as mallinfo, but reports properties of
1394 DLMALLOC_EXPORT struct mallinfo mspace_mallinfo(mspace msp);
1395 #endif /* NO_MALLINFO */
1398 malloc_usable_size(void* p) behaves the same as malloc_usable_size;
1400 DLMALLOC_EXPORT size_t mspace_usable_size(const void* mem);
1403 mspace_malloc_stats behaves as malloc_stats, but reports
1404 properties of the given space.
1406 DLMALLOC_EXPORT void mspace_malloc_stats(mspace msp);
1409 mspace_trim behaves as malloc_trim, but
1410 operates within the given space.
1412 DLMALLOC_EXPORT int mspace_trim(mspace msp, size_t pad);
1415 An alias for mallopt.
1417 DLMALLOC_EXPORT int mspace_mallopt(int, int);
1419 DLMALLOC_EXPORT void* mspace_get_aligned (mspace msp,
1420 unsigned long long n_user_data_bytes,
1421 unsigned long long align,
1422 unsigned long long align_offset);
1424 DLMALLOC_EXPORT int mspace_is_heap_object (mspace msp, void *p);
1426 DLMALLOC_EXPORT void mspace_get_address_and_size (mspace msp,
1427 unsigned long long *addrp,
1428 unsigned long long *sizep);
1429 DLMALLOC_EXPORT void mspace_put (mspace msp, void *p);
1430 DLMALLOC_EXPORT void mspace_put_no_offset (mspace msp, void *p);
1431 DLMALLOC_EXPORT size_t mspace_usable_size_with_delta (const void *p);
1432 DLMALLOC_EXPORT void mspace_disable_expand (mspace msp);
1433 DLMALLOC_EXPORT void *mspace_least_addr (mspace msp);
1434 DLMALLOC_EXPORT void mheap_get_trace (u64 offset, u64 size);
1435 DLMALLOC_EXPORT void mheap_put_trace (u64 offset, u64 size);
1436 DLMALLOC_EXPORT int mspace_enable_disable_trace (mspace msp, int enable);
1438 #endif /* MSPACES */
1441 } /* end of extern "C" */
1442 #endif /* __cplusplus */
1445 ========================================================================
1446 To make a fully customizable malloc.h header file, cut everything
1447 above this line, put into file malloc.h, edit to suit, and #include it
1448 on the next line, as well as in programs that use this malloc.
1449 ========================================================================