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