blob: b3278f9dc508bd9e326a3481046fe90b78ef0ad7 [file] [log] [blame]
Dave Barach6a5adc32018-07-04 10:56:23 -04001/*
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
6
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!
11
12* Quickstart
13
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.
19
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.
33
34* Vital statistics:
35
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.)
42
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.
47
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.
53
54 Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead)
55 8-byte ptrs: 32 bytes (including overhead)
56
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.
65
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.
78
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.)
89
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.
99
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.
105
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.
111
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.
128
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
136 like memset.
137
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
140 others as well.
141
142* Overview of algorithms
143
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.
149
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.)
162
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.
173
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.
178
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
182 ignored.
183
184 For a longer but out of date high-level description, see
185 http://gee.cs.oswego.edu/dl/html/malloc.html
186
187* MSPACES
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)
198
199 (Note: If you only need one instance of an mspace, you can instead
200 use "USE_DL_PREFIX" to relabel the global malloc.)
201
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);
208 }
209 void tlfree(void* mem) { mspace_free(tlms, mem); }
210
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.
217
218 ------------------------- Compile-time options ---------------------------
219
220Be careful in setting #define values for numerical constants of type
221size_t. On some systems, literal values are not automatically extended
222to size_t precision unless they are explicitly casted. You can also
223use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below.
224
225WIN32 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.
240
241DLMALLOC_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")))
247
248MALLOC_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.
254
255MSPACES default: 0 (false)
256 If true, compile in support for independent allocation spaces.
257 This is only supported if HAVE_MMAP is true.
258
259ONLY_MSPACES default: 0 (false)
260 If true, only compile in mspace versions, not regular versions.
261
262USE_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,
268 as described below.
269
270USE_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
274 used.
275
276USE_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.
280
281LOCK_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.
286
287FOOTERS default: 0
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.
291
292INSECURE default: 0
293 If true, omit checks for usage errors and heap space overwrites.
294
295USE_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.
299
300MALLOC_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.
305
306DLM_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.
316
317PROCEED_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.
327
328DEBUG 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.
338
339DLM_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.
345
346MALLOC_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.
349
350HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES
351 True if this system supports sbrk or an emulation of it.
352
353MORECORE 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.
363
364MORECORE_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.
371
372MORECORE_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
376 arguments.
377
378NO_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.
383
384HAVE_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.
393
394HAVE_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.
397
398MMAP_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.
401
402USE_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.)
409
410malloc_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
415 initialization.
416
417USE_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.
420
421NO_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
424 those in this file.
425
426MALLINFO_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
430
431NO_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.
434
435REALLOC_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
439 realloc(p, 0).
440
441LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H
442LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H
443LACKS_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.
446
447DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS,
448 system_info.dwAllocationGranularity in WIN32,
449 otherwise 64K.
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
460 "TOP_PAD")
461
462DEFAULT_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.
484
485DEFAULT_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.
508
509MAX_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.
522*/
523
524#include <vppinfra/clib.h>
525#include <vppinfra/cache.h>
526
Dave Barachdb0a7ec2018-07-26 16:16:55 -0400527/* --- begin vpp customizations --- */
528
529#if CLIB_DEBUG > 0
530#define FOOTERS 1 /* extra debugging */
531#endif
532#define USE_LOCKS 1
533#define DLM_ABORT {extern void os_panic(void); os_panic(); abort();}
534#define ONLY_MSPACES 1
535
536/* --- end vpp customizations --- */
537
Dave Barach6a5adc32018-07-04 10:56:23 -0400538/* Version identifier to allow people to support multiple versions */
539#ifndef DLMALLOC_VERSION
540#define DLMALLOC_VERSION 20806
541#endif /* DLMALLOC_VERSION */
542
543#ifndef DLMALLOC_EXPORT
544#define DLMALLOC_EXPORT extern
545#endif
546
547#ifndef WIN32
548#ifdef _WIN32
549#define WIN32 1
550#endif /* _WIN32 */
551#ifdef _WIN32_WCE
552#define LACKS_FCNTL_H
553#define WIN32 1
554#endif /* _WIN32_WCE */
555#endif /* WIN32 */
556#ifdef WIN32
557#define WIN32_LEAN_AND_MEAN
558#include <windows.h>
559#include <tchar.h>
560#define HAVE_MMAP 1
561#define HAVE_MORECORE 0
562#define LACKS_UNISTD_H
563#define LACKS_SYS_PARAM_H
564#define LACKS_SYS_MMAN_H
565#define LACKS_STRING_H
566#define LACKS_STRINGS_H
567#define LACKS_SYS_TYPES_H
568#define LACKS_ERRNO_H
569#define LACKS_SCHED_H
570#ifndef MALLOC_FAILURE_ACTION
571#define MALLOC_FAILURE_ACTION
572#endif /* MALLOC_FAILURE_ACTION */
573#ifndef MMAP_CLEARS
574#ifdef _WIN32_WCE /* WINCE reportedly does not clear */
575#define MMAP_CLEARS 0
576#else
577#define MMAP_CLEARS 1
578#endif /* _WIN32_WCE */
579#endif /*MMAP_CLEARS */
580#endif /* WIN32 */
581
582#if defined(DARWIN) || defined(_DARWIN)
583/* Mac OSX docs advise not to use sbrk; it seems better to use mmap */
584#ifndef HAVE_MORECORE
585#define HAVE_MORECORE 0
586#define HAVE_MMAP 1
587/* OSX allocators provide 16 byte alignment */
588#ifndef MALLOC_ALIGNMENT
589#define MALLOC_ALIGNMENT ((size_t)16U)
590#endif
591#endif /* HAVE_MORECORE */
592#endif /* DARWIN */
593
594#ifndef LACKS_SYS_TYPES_H
595#include <sys/types.h> /* For size_t */
596#endif /* LACKS_SYS_TYPES_H */
597
598/* The maximum possible size_t value has all bits set */
599#define MAX_SIZE_T (~(size_t)0)
600
Dave Barach6a5adc32018-07-04 10:56:23 -0400601#ifndef USE_LOCKS /* ensure true if spin or recursive locks set */
602#define USE_LOCKS ((defined(USE_SPIN_LOCKS) && USE_SPIN_LOCKS != 0) || \
603 (defined(USE_RECURSIVE_LOCKS) && USE_RECURSIVE_LOCKS != 0))
604#endif /* USE_LOCKS */
605
606#if USE_LOCKS /* Spin locks for gcc >= 4.1, older gcc on x86, MSC >= 1310 */
607#if ((defined(__GNUC__) && \
608 ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) || \
609 defined(__i386__) || defined(__x86_64__))) || \
610 (defined(_MSC_VER) && _MSC_VER>=1310))
611#ifndef USE_SPIN_LOCKS
612#define USE_SPIN_LOCKS 1
613#endif /* USE_SPIN_LOCKS */
614#elif USE_SPIN_LOCKS
615#error "USE_SPIN_LOCKS defined without implementation"
616#endif /* ... locks available... */
617#elif !defined(USE_SPIN_LOCKS)
618#define USE_SPIN_LOCKS 0
619#endif /* USE_LOCKS */
620
621#ifndef ONLY_MSPACES
622#define ONLY_MSPACES 1
623#endif /* ONLY_MSPACES */
624#ifndef MSPACES
625#if ONLY_MSPACES
626#define MSPACES 1
627#else /* ONLY_MSPACES */
628#define MSPACES 0
629#endif /* ONLY_MSPACES */
630#endif /* MSPACES */
631#ifndef MALLOC_ALIGNMENT
632#define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *)))
633#endif /* MALLOC_ALIGNMENT */
634#ifndef FOOTERS
635#define FOOTERS 0
636#endif /* FOOTERS */
637#ifndef DLM_ABORT
638#define DLM_ABORT abort()
639#endif /* DLM_ABORT */
640#ifndef DLM_ABORT_ON_ASSERT_FAILURE
641#define DLM_ABORT_ON_ASSERT_FAILURE 1
642#endif /* DLM_ABORT_ON_ASSERT_FAILURE */
643#ifndef PROCEED_ON_ERROR
644#define PROCEED_ON_ERROR 0
645#endif /* PROCEED_ON_ERROR */
646
647#ifndef INSECURE
648#define INSECURE 0
649#endif /* INSECURE */
650#ifndef MALLOC_INSPECT_ALL
651#define MALLOC_INSPECT_ALL 0
652#endif /* MALLOC_INSPECT_ALL */
653#ifndef HAVE_MMAP
654#define HAVE_MMAP 1
655#endif /* HAVE_MMAP */
656#ifndef MMAP_CLEARS
657#define MMAP_CLEARS 1
658#endif /* MMAP_CLEARS */
659#ifndef HAVE_MREMAP
660#ifdef linux
661#define HAVE_MREMAP 1
662#define _GNU_SOURCE /* Turns on mremap() definition */
663#else /* linux */
664#define HAVE_MREMAP 0
665#endif /* linux */
666#endif /* HAVE_MREMAP */
667#ifndef MALLOC_FAILURE_ACTION
668#define MALLOC_FAILURE_ACTION errno = ENOMEM;
669#endif /* MALLOC_FAILURE_ACTION */
670#ifndef HAVE_MORECORE
671#if ONLY_MSPACES
672#define HAVE_MORECORE 0
673#else /* ONLY_MSPACES */
674#define HAVE_MORECORE 1
675#endif /* ONLY_MSPACES */
676#endif /* HAVE_MORECORE */
677#if !HAVE_MORECORE
678#define MORECORE_CONTIGUOUS 0
679#else /* !HAVE_MORECORE */
680#define MORECORE_DEFAULT sbrk
681#ifndef MORECORE_CONTIGUOUS
682#define MORECORE_CONTIGUOUS 1
683#endif /* MORECORE_CONTIGUOUS */
684#endif /* HAVE_MORECORE */
685#ifndef DEFAULT_GRANULARITY
686#if (MORECORE_CONTIGUOUS || defined(WIN32))
687#define DEFAULT_GRANULARITY (0) /* 0 means to compute in init_mparams */
688#else /* MORECORE_CONTIGUOUS */
689#define DEFAULT_GRANULARITY ((size_t)64U * (size_t)1024U)
690#endif /* MORECORE_CONTIGUOUS */
691#endif /* DEFAULT_GRANULARITY */
692#ifndef DEFAULT_TRIM_THRESHOLD
693#ifndef MORECORE_CANNOT_TRIM
694#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U)
695#else /* MORECORE_CANNOT_TRIM */
696#define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T
697#endif /* MORECORE_CANNOT_TRIM */
698#endif /* DEFAULT_TRIM_THRESHOLD */
699#ifndef DEFAULT_MMAP_THRESHOLD
700#if HAVE_MMAP
701#define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U)
702#else /* HAVE_MMAP */
703#define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T
704#endif /* HAVE_MMAP */
705#endif /* DEFAULT_MMAP_THRESHOLD */
706#ifndef MAX_RELEASE_CHECK_RATE
707#if HAVE_MMAP
708#define MAX_RELEASE_CHECK_RATE 4095
709#else
710#define MAX_RELEASE_CHECK_RATE MAX_SIZE_T
711#endif /* HAVE_MMAP */
712#endif /* MAX_RELEASE_CHECK_RATE */
713#ifndef USE_BUILTIN_FFS
714#define USE_BUILTIN_FFS 0
715#endif /* USE_BUILTIN_FFS */
716#ifndef USE_DEV_RANDOM
717#define USE_DEV_RANDOM 0
718#endif /* USE_DEV_RANDOM */
719#ifndef NO_MALLINFO
720#define NO_MALLINFO 0
721#endif /* NO_MALLINFO */
722#ifndef MALLINFO_FIELD_TYPE
723#define MALLINFO_FIELD_TYPE size_t
724#endif /* MALLINFO_FIELD_TYPE */
725#ifndef NO_MALLOC_STATS
726#define NO_MALLOC_STATS 0
727#endif /* NO_MALLOC_STATS */
728#ifndef NO_SEGMENT_TRAVERSAL
729#define NO_SEGMENT_TRAVERSAL 0
730#endif /* NO_SEGMENT_TRAVERSAL */
731
732/*
733 mallopt tuning options. SVID/XPG defines four standard parameter
734 numbers for mallopt, normally defined in malloc.h. None of these
735 are used in this malloc, so setting them has no effect. But this
736 malloc does support the following options.
737*/
738
739#define M_TRIM_THRESHOLD (-1)
740#define M_GRANULARITY (-2)
741#define M_MMAP_THRESHOLD (-3)
742
743/* ------------------------ Mallinfo declarations ------------------------ */
744
745#if !NO_MALLINFO
746/*
747 This version of malloc supports the standard SVID/XPG mallinfo
748 routine that returns a struct containing usage properties and
749 statistics. It should work on any system that has a
750 /usr/include/malloc.h defining struct mallinfo. The main
751 declaration needed is the mallinfo struct that is returned (by-copy)
752 by mallinfo(). The malloinfo struct contains a bunch of fields that
753 are not even meaningful in this version of malloc. These fields are
754 are instead filled by mallinfo() with other numbers that might be of
755 interest.
756
757 HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
758 /usr/include/malloc.h file that includes a declaration of struct
759 mallinfo. If so, it is included; else a compliant version is
760 declared below. These must be precisely the same for mallinfo() to
761 work. The original SVID version of this struct, defined on most
762 systems with mallinfo, declares all fields as ints. But some others
763 define as unsigned long. If your system defines the fields using a
764 type of different width than listed here, you MUST #include your
765 system version and #define HAVE_USR_INCLUDE_MALLOC_H.
766*/
767
768/* #define HAVE_USR_INCLUDE_MALLOC_H */
769
770#ifdef HAVE_USR_INCLUDE_MALLOC_H
771#include "/usr/include/malloc.h"
772#else /* HAVE_USR_INCLUDE_MALLOC_H */
773#ifndef STRUCT_MALLINFO_DECLARED
774/* HP-UX (and others?) redefines mallinfo unless _STRUCT_MALLINFO is defined */
775#define _STRUCT_MALLINFO
776#define STRUCT_MALLINFO_DECLARED 1
777struct mallinfo {
778 MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */
779 MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */
780 MALLINFO_FIELD_TYPE smblks; /* always 0 */
781 MALLINFO_FIELD_TYPE hblks; /* always 0 */
782 MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */
783 MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */
784 MALLINFO_FIELD_TYPE fsmblks; /* always 0 */
785 MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
786 MALLINFO_FIELD_TYPE fordblks; /* total free space */
787 MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
788};
789#endif /* STRUCT_MALLINFO_DECLARED */
790#endif /* HAVE_USR_INCLUDE_MALLOC_H */
791#endif /* NO_MALLINFO */
792
793/*
794 Try to persuade compilers to inline. The most critical functions for
795 inlining are defined as macros, so these aren't used for them.
796*/
797
798#ifndef FORCEINLINE
799 #if defined(__GNUC__)
800#define FORCEINLINE __inline __attribute__ ((always_inline))
801 #elif defined(_MSC_VER)
802 #define FORCEINLINE __forceinline
803 #endif
804#endif
805#ifndef NOINLINE
806 #if defined(__GNUC__)
807 #define NOINLINE __attribute__ ((noinline))
808 #elif defined(_MSC_VER)
809 #define NOINLINE __declspec(noinline)
810 #else
811 #define NOINLINE
812 #endif
813#endif
814
815#ifdef __cplusplus
816extern "C" {
817#ifndef FORCEINLINE
818 #define FORCEINLINE inline
819#endif
820#endif /* __cplusplus */
821#ifndef FORCEINLINE
822 #define FORCEINLINE
823#endif
824
825#if !ONLY_MSPACES
826
827/* ------------------- Declarations of public routines ------------------- */
828
829#ifndef USE_DL_PREFIX
830#define dlcalloc calloc
831#define dlfree free
832#define dlmalloc malloc
833#define dlmemalign memalign
834#define dlposix_memalign posix_memalign
835#define dlrealloc realloc
836#define dlrealloc_in_place realloc_in_place
837#define dlvalloc valloc
838#define dlpvalloc pvalloc
839#define dlmallinfo mallinfo
840#define dlmallopt mallopt
841#define dlmalloc_trim malloc_trim
842#define dlmalloc_stats malloc_stats
843#define dlmalloc_usable_size malloc_usable_size
844#define dlmalloc_footprint malloc_footprint
845#define dlmalloc_max_footprint malloc_max_footprint
846#define dlmalloc_footprint_limit malloc_footprint_limit
847#define dlmalloc_set_footprint_limit malloc_set_footprint_limit
848#define dlmalloc_inspect_all malloc_inspect_all
849#define dlindependent_calloc independent_calloc
850#define dlindependent_comalloc independent_comalloc
851#define dlbulk_free bulk_free
852#endif /* USE_DL_PREFIX */
853
854/*
855 malloc(size_t n)
856 Returns a pointer to a newly allocated chunk of at least n bytes, or
857 null if no space is available, in which case errno is set to ENOMEM
858 on ANSI C systems.
859
860 If n is zero, malloc returns a minimum-sized chunk. (The minimum
861 size is 16 bytes on most 32bit systems, and 32 bytes on 64bit
862 systems.) Note that size_t is an unsigned type, so calls with
863 arguments that would be negative if signed are interpreted as
864 requests for huge amounts of space, which will often fail. The
865 maximum supported value of n differs across systems, but is in all
866 cases less than the maximum representable value of a size_t.
867*/
868DLMALLOC_EXPORT void* dlmalloc(size_t);
869
870/*
871 free(void* p)
872 Releases the chunk of memory pointed to by p, that had been previously
873 allocated using malloc or a related routine such as realloc.
874 It has no effect if p is null. If p was not malloced or already
875 freed, free(p) will by default cause the current program to abort.
876*/
877DLMALLOC_EXPORT void dlfree(void*);
878
879/*
880 calloc(size_t n_elements, size_t element_size);
881 Returns a pointer to n_elements * element_size bytes, with all locations
882 set to zero.
883*/
884DLMALLOC_EXPORT void* dlcalloc(size_t, size_t);
885
886/*
887 realloc(void* p, size_t n)
888 Returns a pointer to a chunk of size n that contains the same data
889 as does chunk p up to the minimum of (n, p's size) bytes, or null
890 if no space is available.
891
892 The returned pointer may or may not be the same as p. The algorithm
893 prefers extending p in most cases when possible, otherwise it
894 employs the equivalent of a malloc-copy-free sequence.
895
896 If p is null, realloc is equivalent to malloc.
897
898 If space is not available, realloc returns null, errno is set (if on
899 ANSI) and p is NOT freed.
900
901 if n is for fewer bytes than already held by p, the newly unused
902 space is lopped off and freed if possible. realloc with a size
903 argument of zero (re)allocates a minimum-sized chunk.
904
905 The old unix realloc convention of allowing the last-free'd chunk
906 to be used as an argument to realloc is not supported.
907*/
908DLMALLOC_EXPORT void* dlrealloc(void*, size_t);
909
910/*
911 realloc_in_place(void* p, size_t n)
912 Resizes the space allocated for p to size n, only if this can be
913 done without moving p (i.e., only if there is adjacent space
914 available if n is greater than p's current allocated size, or n is
915 less than or equal to p's size). This may be used instead of plain
916 realloc if an alternative allocation strategy is needed upon failure
917 to expand space; for example, reallocation of a buffer that must be
918 memory-aligned or cleared. You can use realloc_in_place to trigger
919 these alternatives only when needed.
920
921 Returns p if successful; otherwise null.
922*/
923DLMALLOC_EXPORT void* dlrealloc_in_place(void*, size_t);
924
925/*
926 memalign(size_t alignment, size_t n);
927 Returns a pointer to a newly allocated chunk of n bytes, aligned
928 in accord with the alignment argument.
929
930 The alignment argument should be a power of two. If the argument is
931 not a power of two, the nearest greater power is used.
932 8-byte alignment is guaranteed by normal malloc calls, so don't
933 bother calling memalign with an argument of 8 or less.
934
935 Overreliance on memalign is a sure way to fragment space.
936*/
937DLMALLOC_EXPORT void* dlmemalign(size_t, size_t);
938
939/*
940 int posix_memalign(void** pp, size_t alignment, size_t n);
941 Allocates a chunk of n bytes, aligned in accord with the alignment
942 argument. Differs from memalign only in that it (1) assigns the
943 allocated memory to *pp rather than returning it, (2) fails and
944 returns EINVAL if the alignment is not a power of two (3) fails and
945 returns ENOMEM if memory cannot be allocated.
946*/
947DLMALLOC_EXPORT int dlposix_memalign(void**, size_t, size_t);
948
949/*
950 valloc(size_t n);
951 Equivalent to memalign(pagesize, n), where pagesize is the page
952 size of the system. If the pagesize is unknown, 4096 is used.
953*/
954DLMALLOC_EXPORT void* dlvalloc(size_t);
955
956/*
957 mallopt(int parameter_number, int parameter_value)
958 Sets tunable parameters The format is to provide a
959 (parameter-number, parameter-value) pair. mallopt then sets the
960 corresponding parameter to the argument value if it can (i.e., so
961 long as the value is meaningful), and returns 1 if successful else
962 0. To workaround the fact that mallopt is specified to use int,
963 not size_t parameters, the value -1 is specially treated as the
964 maximum unsigned size_t value.
965
966 SVID/XPG/ANSI defines four standard param numbers for mallopt,
967 normally defined in malloc.h. None of these are use in this malloc,
968 so setting them has no effect. But this malloc also supports other
969 options in mallopt. See below for details. Briefly, supported
970 parameters are as follows (listed defaults are for "typical"
971 configurations).
972
973 Symbol param # default allowed param values
974 M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables)
975 M_GRANULARITY -2 page size any power of 2 >= page size
976 M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support)
977*/
978DLMALLOC_EXPORT int dlmallopt(int, int);
979
980/*
981 malloc_footprint();
982 Returns the number of bytes obtained from the system. The total
983 number of bytes allocated by malloc, realloc etc., is less than this
984 value. Unlike mallinfo, this function returns only a precomputed
985 result, so can be called frequently to monitor memory consumption.
986 Even if locks are otherwise defined, this function does not use them,
987 so results might not be up to date.
988*/
989DLMALLOC_EXPORT size_t dlmalloc_footprint(void);
990
991/*
992 malloc_max_footprint();
993 Returns the maximum number of bytes obtained from the system. This
994 value will be greater than current footprint if deallocated space
995 has been reclaimed by the system. The peak number of bytes allocated
996 by malloc, realloc etc., is less than this value. Unlike mallinfo,
997 this function returns only a precomputed result, so can be called
998 frequently to monitor memory consumption. Even if locks are
999 otherwise defined, this function does not use them, so results might
1000 not be up to date.
1001*/
1002DLMALLOC_EXPORT size_t dlmalloc_max_footprint(void);
1003
1004/*
1005 malloc_footprint_limit();
1006 Returns the number of bytes that the heap is allowed to obtain from
1007 the system, returning the last value returned by
1008 malloc_set_footprint_limit, or the maximum size_t value if
1009 never set. The returned value reflects a permission. There is no
1010 guarantee that this number of bytes can actually be obtained from
1011 the system.
1012*/
1013DLMALLOC_EXPORT size_t dlmalloc_footprint_limit();
1014
1015/*
1016 malloc_set_footprint_limit();
1017 Sets the maximum number of bytes to obtain from the system, causing
1018 failure returns from malloc and related functions upon attempts to
1019 exceed this value. The argument value may be subject to page
1020 rounding to an enforceable limit; this actual value is returned.
1021 Using an argument of the maximum possible size_t effectively
1022 disables checks. If the argument is less than or equal to the
1023 current malloc_footprint, then all future allocations that require
1024 additional system memory will fail. However, invocation cannot
1025 retroactively deallocate existing used memory.
1026*/
1027DLMALLOC_EXPORT size_t dlmalloc_set_footprint_limit(size_t bytes);
1028
1029#if MALLOC_INSPECT_ALL
1030/*
1031 malloc_inspect_all(void(*handler)(void *start,
1032 void *end,
1033 size_t used_bytes,
1034 void* callback_arg),
1035 void* arg);
1036 Traverses the heap and calls the given handler for each managed
1037 region, skipping all bytes that are (or may be) used for bookkeeping
1038 purposes. Traversal does not include include chunks that have been
1039 directly memory mapped. Each reported region begins at the start
1040 address, and continues up to but not including the end address. The
1041 first used_bytes of the region contain allocated data. If
1042 used_bytes is zero, the region is unallocated. The handler is
1043 invoked with the given callback argument. If locks are defined, they
1044 are held during the entire traversal. It is a bad idea to invoke
1045 other malloc functions from within the handler.
1046
1047 For example, to count the number of in-use chunks with size greater
1048 than 1000, you could write:
1049 static int count = 0;
1050 void count_chunks(void* start, void* end, size_t used, void* arg) {
1051 if (used >= 1000) ++count;
1052 }
1053 then:
1054 malloc_inspect_all(count_chunks, NULL);
1055
1056 malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined.
1057*/
1058DLMALLOC_EXPORT void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*),
1059 void* arg);
1060
1061#endif /* MALLOC_INSPECT_ALL */
1062
1063#if !NO_MALLINFO
1064/*
1065 mallinfo()
1066 Returns (by copy) a struct containing various summary statistics:
1067
1068 arena: current total non-mmapped bytes allocated from system
1069 ordblks: the number of free chunks
1070 smblks: always zero.
1071 hblks: current number of mmapped regions
1072 hblkhd: total bytes held in mmapped regions
1073 usmblks: the maximum total allocated space. This will be greater
1074 than current total if trimming has occurred.
1075 fsmblks: always zero
1076 uordblks: current total allocated space (normal or mmapped)
1077 fordblks: total free space
1078 keepcost: the maximum number of bytes that could ideally be released
1079 back to system via malloc_trim. ("ideally" means that
1080 it ignores page restrictions etc.)
1081
1082 Because these fields are ints, but internal bookkeeping may
1083 be kept as longs, the reported values may wrap around zero and
1084 thus be inaccurate.
1085*/
1086DLMALLOC_EXPORT struct mallinfo dlmallinfo(void);
1087#endif /* NO_MALLINFO */
1088
1089/*
1090 independent_calloc(size_t n_elements, size_t element_size, void* chunks[]);
1091
1092 independent_calloc is similar to calloc, but instead of returning a
1093 single cleared space, it returns an array of pointers to n_elements
1094 independent elements that can hold contents of size elem_size, each
1095 of which starts out cleared, and can be independently freed,
1096 realloc'ed etc. The elements are guaranteed to be adjacently
1097 allocated (this is not guaranteed to occur with multiple callocs or
1098 mallocs), which may also improve cache locality in some
1099 applications.
1100
1101 The "chunks" argument is optional (i.e., may be null, which is
1102 probably the most typical usage). If it is null, the returned array
1103 is itself dynamically allocated and should also be freed when it is
1104 no longer needed. Otherwise, the chunks array must be of at least
1105 n_elements in length. It is filled in with the pointers to the
1106 chunks.
1107
1108 In either case, independent_calloc returns this pointer array, or
1109 null if the allocation failed. If n_elements is zero and "chunks"
1110 is null, it returns a chunk representing an array with zero elements
1111 (which should be freed if not wanted).
1112
1113 Each element must be freed when it is no longer needed. This can be
1114 done all at once using bulk_free.
1115
1116 independent_calloc simplifies and speeds up implementations of many
1117 kinds of pools. It may also be useful when constructing large data
1118 structures that initially have a fixed number of fixed-sized nodes,
1119 but the number is not known at compile time, and some of the nodes
1120 may later need to be freed. For example:
1121
1122 struct Node { int item; struct Node* next; };
1123
1124 struct Node* build_list() {
1125 struct Node** pool;
1126 int n = read_number_of_nodes_needed();
1127 if (n <= 0) return 0;
1128 pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
1129 if (pool == 0) die();
1130 // organize into a linked list...
1131 struct Node* first = pool[0];
1132 for (i = 0; i < n-1; ++i)
1133 pool[i]->next = pool[i+1];
1134 free(pool); // Can now free the array (or not, if it is needed later)
1135 return first;
1136 }
1137*/
1138DLMALLOC_EXPORT void** dlindependent_calloc(size_t, size_t, void**);
1139
1140/*
1141 independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);
1142
1143 independent_comalloc allocates, all at once, a set of n_elements
1144 chunks with sizes indicated in the "sizes" array. It returns
1145 an array of pointers to these elements, each of which can be
1146 independently freed, realloc'ed etc. The elements are guaranteed to
1147 be adjacently allocated (this is not guaranteed to occur with
1148 multiple callocs or mallocs), which may also improve cache locality
1149 in some applications.
1150
1151 The "chunks" argument is optional (i.e., may be null). If it is null
1152 the returned array is itself dynamically allocated and should also
1153 be freed when it is no longer needed. Otherwise, the chunks array
1154 must be of at least n_elements in length. It is filled in with the
1155 pointers to the chunks.
1156
1157 In either case, independent_comalloc returns this pointer array, or
1158 null if the allocation failed. If n_elements is zero and chunks is
1159 null, it returns a chunk representing an array with zero elements
1160 (which should be freed if not wanted).
1161
1162 Each element must be freed when it is no longer needed. This can be
1163 done all at once using bulk_free.
1164
1165 independent_comallac differs from independent_calloc in that each
1166 element may have a different size, and also that it does not
1167 automatically clear elements.
1168
1169 independent_comalloc can be used to speed up allocation in cases
1170 where several structs or objects must always be allocated at the
1171 same time. For example:
1172
1173 struct Head { ... }
1174 struct Foot { ... }
1175
1176 void send_message(char* msg) {
1177 int msglen = strlen(msg);
1178 size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
1179 void* chunks[3];
1180 if (independent_comalloc(3, sizes, chunks) == 0)
1181 die();
1182 struct Head* head = (struct Head*)(chunks[0]);
1183 char* body = (char*)(chunks[1]);
1184 struct Foot* foot = (struct Foot*)(chunks[2]);
1185 // ...
1186 }
1187
1188 In general though, independent_comalloc is worth using only for
1189 larger values of n_elements. For small values, you probably won't
1190 detect enough difference from series of malloc calls to bother.
1191
1192 Overuse of independent_comalloc can increase overall memory usage,
1193 since it cannot reuse existing noncontiguous small chunks that
1194 might be available for some of the elements.
1195*/
1196DLMALLOC_EXPORT void** dlindependent_comalloc(size_t, size_t*, void**);
1197
1198/*
1199 bulk_free(void* array[], size_t n_elements)
1200 Frees and clears (sets to null) each non-null pointer in the given
1201 array. This is likely to be faster than freeing them one-by-one.
1202 If footers are used, pointers that have been allocated in different
1203 mspaces are not freed or cleared, and the count of all such pointers
1204 is returned. For large arrays of pointers with poor locality, it
1205 may be worthwhile to sort this array before calling bulk_free.
1206*/
1207DLMALLOC_EXPORT size_t dlbulk_free(void**, size_t n_elements);
1208
1209/*
1210 pvalloc(size_t n);
1211 Equivalent to valloc(minimum-page-that-holds(n)), that is,
1212 round up n to nearest pagesize.
1213 */
1214DLMALLOC_EXPORT void* dlpvalloc(size_t);
1215
1216/*
1217 malloc_trim(size_t pad);
1218
1219 If possible, gives memory back to the system (via negative arguments
1220 to sbrk) if there is unused memory at the `high' end of the malloc
1221 pool or in unused MMAP segments. You can call this after freeing
1222 large blocks of memory to potentially reduce the system-level memory
1223 requirements of a program. However, it cannot guarantee to reduce
1224 memory. Under some allocation patterns, some large free blocks of
1225 memory will be locked between two used chunks, so they cannot be
1226 given back to the system.
1227
1228 The `pad' argument to malloc_trim represents the amount of free
1229 trailing space to leave untrimmed. If this argument is zero, only
1230 the minimum amount of memory to maintain internal data structures
1231 will be left. Non-zero arguments can be supplied to maintain enough
1232 trailing space to service future expected allocations without having
1233 to re-obtain memory from the system.
1234
1235 Malloc_trim returns 1 if it actually released any memory, else 0.
1236*/
1237DLMALLOC_EXPORT int dlmalloc_trim(size_t);
1238
1239/*
1240 malloc_stats();
1241 Prints on stderr the amount of space obtained from the system (both
1242 via sbrk and mmap), the maximum amount (which may be more than
1243 current if malloc_trim and/or munmap got called), and the current
1244 number of bytes allocated via malloc (or realloc, etc) but not yet
1245 freed. Note that this is the number of bytes allocated, not the
1246 number requested. It will be larger than the number requested
1247 because of alignment and bookkeeping overhead. Because it includes
1248 alignment wastage as being in use, this figure may be greater than
1249 zero even when no user-level chunks are allocated.
1250
1251 The reported current and maximum system memory can be inaccurate if
1252 a program makes other calls to system memory allocation functions
1253 (normally sbrk) outside of malloc.
1254
1255 malloc_stats prints only the most commonly interesting statistics.
1256 More information can be obtained by calling mallinfo.
1257*/
1258DLMALLOC_EXPORT void dlmalloc_stats(void);
1259
1260/*
1261 malloc_usable_size(void* p);
1262
1263 Returns the number of bytes you can actually use in
1264 an allocated chunk, which may be more than you requested (although
1265 often not) due to alignment and minimum size constraints.
1266 You can use this many bytes without worrying about
1267 overwriting other allocated objects. This is not a particularly great
1268 programming practice. malloc_usable_size can be more useful in
1269 debugging and assertions, for example:
1270
1271 p = malloc(n);
1272 assert(malloc_usable_size(p) >= 256);
1273*/
1274size_t dlmalloc_usable_size(void*);
1275
1276#endif /* ONLY_MSPACES */
1277
1278#if MSPACES
1279
1280/*
1281 mspace is an opaque type representing an independent
1282 region of space that supports mspace_malloc, etc.
1283*/
1284typedef void* mspace;
1285
1286/*
1287 create_mspace creates and returns a new independent space with the
1288 given initial capacity, or, if 0, the default granularity size. It
1289 returns null if there is no system memory available to create the
1290 space. If argument locked is non-zero, the space uses a separate
1291 lock to control access. The capacity of the space will grow
1292 dynamically as needed to service mspace_malloc requests. You can
1293 control the sizes of incremental increases of this space by
1294 compiling with a different DEFAULT_GRANULARITY or dynamically
1295 setting with mallopt(M_GRANULARITY, value).
1296*/
1297DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked);
1298
1299/*
1300 destroy_mspace destroys the given space, and attempts to return all
1301 of its memory back to the system, returning the total number of
1302 bytes freed. After destruction, the results of access to all memory
1303 used by the space become undefined.
1304*/
1305DLMALLOC_EXPORT size_t destroy_mspace(mspace msp);
1306
1307/*
1308 create_mspace_with_base uses the memory supplied as the initial base
1309 of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this
1310 space is used for bookkeeping, so the capacity must be at least this
1311 large. (Otherwise 0 is returned.) When this initial space is
1312 exhausted, additional memory will be obtained from the system.
1313 Destroying this space will deallocate all additionally allocated
1314 space (if possible) but not the initial base.
1315*/
1316DLMALLOC_EXPORT mspace create_mspace_with_base(void* base, size_t capacity, int locked);
1317
1318/*
1319 mspace_track_large_chunks controls whether requests for large chunks
1320 are allocated in their own untracked mmapped regions, separate from
1321 others in this mspace. By default large chunks are not tracked,
1322 which reduces fragmentation. However, such chunks are not
1323 necessarily released to the system upon destroy_mspace. Enabling
1324 tracking by setting to true may increase fragmentation, but avoids
1325 leakage when relying on destroy_mspace to release all memory
1326 allocated using this space. The function returns the previous
1327 setting.
1328*/
1329DLMALLOC_EXPORT int mspace_track_large_chunks(mspace msp, int enable);
1330
1331
1332/*
1333 mspace_malloc behaves as malloc, but operates within
1334 the given space.
1335*/
1336DLMALLOC_EXPORT void* mspace_malloc(mspace msp, size_t bytes);
1337
1338/*
1339 mspace_free behaves as free, but operates within
1340 the given space.
1341
1342 If compiled with FOOTERS==1, mspace_free is not actually needed.
1343 free may be called instead of mspace_free because freed chunks from
1344 any space are handled by their originating spaces.
1345*/
1346DLMALLOC_EXPORT void mspace_free(mspace msp, void* mem);
1347
1348/*
1349 mspace_realloc behaves as realloc, but operates within
1350 the given space.
1351
1352 If compiled with FOOTERS==1, mspace_realloc is not actually
1353 needed. realloc may be called instead of mspace_realloc because
1354 realloced chunks from any space are handled by their originating
1355 spaces.
1356*/
1357DLMALLOC_EXPORT void* mspace_realloc(mspace msp, void* mem, size_t newsize);
1358
1359/*
1360 mspace_calloc behaves as calloc, but operates within
1361 the given space.
1362*/
1363DLMALLOC_EXPORT void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
1364
1365/*
1366 mspace_memalign behaves as memalign, but operates within
1367 the given space.
1368*/
1369DLMALLOC_EXPORT void* mspace_memalign(mspace msp, size_t alignment, size_t bytes);
1370
1371/*
1372 mspace_independent_calloc behaves as independent_calloc, but
1373 operates within the given space.
1374*/
1375DLMALLOC_EXPORT void** mspace_independent_calloc(mspace msp, size_t n_elements,
1376 size_t elem_size, void* chunks[]);
1377
1378/*
1379 mspace_independent_comalloc behaves as independent_comalloc, but
1380 operates within the given space.
1381*/
1382DLMALLOC_EXPORT void** mspace_independent_comalloc(mspace msp, size_t n_elements,
1383 size_t sizes[], void* chunks[]);
1384
1385/*
1386 mspace_footprint() returns the number of bytes obtained from the
1387 system for this space.
1388*/
1389DLMALLOC_EXPORT size_t mspace_footprint(mspace msp);
1390
1391/*
1392 mspace_max_footprint() returns the peak number of bytes obtained from the
1393 system for this space.
1394*/
1395DLMALLOC_EXPORT size_t mspace_max_footprint(mspace msp);
1396
1397
1398#if !NO_MALLINFO
1399/*
1400 mspace_mallinfo behaves as mallinfo, but reports properties of
1401 the given space.
1402*/
1403DLMALLOC_EXPORT struct mallinfo mspace_mallinfo(mspace msp);
1404#endif /* NO_MALLINFO */
1405
1406/*
1407 malloc_usable_size(void* p) behaves the same as malloc_usable_size;
1408*/
1409DLMALLOC_EXPORT size_t mspace_usable_size(const void* mem);
1410
1411/*
1412 mspace_malloc_stats behaves as malloc_stats, but reports
1413 properties of the given space.
1414*/
1415DLMALLOC_EXPORT void mspace_malloc_stats(mspace msp);
1416
1417/*
1418 mspace_trim behaves as malloc_trim, but
1419 operates within the given space.
1420*/
1421DLMALLOC_EXPORT int mspace_trim(mspace msp, size_t pad);
1422
1423/*
1424 An alias for mallopt.
1425*/
1426DLMALLOC_EXPORT int mspace_mallopt(int, int);
1427
1428DLMALLOC_EXPORT void* mspace_get_aligned (mspace msp,
1429 unsigned long long n_user_data_bytes,
1430 unsigned long long align,
1431 unsigned long long align_offset);
1432
1433DLMALLOC_EXPORT int mspace_is_heap_object (mspace msp, void *p);
1434
1435DLMALLOC_EXPORT void mspace_get_address_and_size (mspace msp,
1436 unsigned long long *addrp,
1437 unsigned long long *sizep);
1438DLMALLOC_EXPORT void mspace_put (mspace msp, void *p);
1439DLMALLOC_EXPORT void mspace_put_no_offset (mspace msp, void *p);
1440DLMALLOC_EXPORT size_t mspace_usable_size_with_delta (const void *p);
1441DLMALLOC_EXPORT void mspace_disable_expand (mspace msp);
1442DLMALLOC_EXPORT void *mspace_least_addr (mspace msp);
1443DLMALLOC_EXPORT void mheap_get_trace (u64 offset, u64 size);
1444DLMALLOC_EXPORT void mheap_put_trace (u64 offset, u64 size);
1445DLMALLOC_EXPORT int mspace_enable_disable_trace (mspace msp, int enable);
1446
1447#endif /* MSPACES */
1448
1449#ifdef __cplusplus
1450} /* end of extern "C" */
1451#endif /* __cplusplus */
1452
1453/*
1454 ========================================================================
1455 To make a fully customizable malloc.h header file, cut everything
1456 above this line, put into file malloc.h, edit to suit, and #include it
1457 on the next line, as well as in programs that use this malloc.
1458 ========================================================================
1459*/