Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 220 | Be careful in setting #define values for numerical constants of type |
| 221 | size_t. On some systems, literal values are not automatically extended |
| 222 | to size_t precision unless they are explicitly casted. You can also |
| 223 | use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below. |
| 224 | |
| 225 | WIN32 default: defined if _WIN32 defined |
| 226 | Defining WIN32 sets up defaults for MS environment and compilers. |
| 227 | Otherwise defaults are for unix. Beware that there seem to be some |
| 228 | cases where this malloc might not be a pure drop-in replacement for |
| 229 | Win32 malloc: Random-looking failures from Win32 GDI API's (eg; |
| 230 | SetDIBits()) may be due to bugs in some video driver implementations |
| 231 | when pixel buffers are malloc()ed, and the region spans more than |
| 232 | one VirtualAlloc()ed region. Because dlmalloc uses a small (64Kb) |
| 233 | default granularity, pixel buffers may straddle virtual allocation |
| 234 | regions more often than when using the Microsoft allocator. You can |
| 235 | avoid this by using VirtualAlloc() and VirtualFree() for all pixel |
| 236 | buffers rather than using malloc(). If this is not possible, |
| 237 | recompile this malloc with a larger DEFAULT_GRANULARITY. Note: |
| 238 | in cases where MSC and gcc (cygwin) are known to differ on WIN32, |
| 239 | conditions use _MSC_VER to distinguish them. |
| 240 | |
| 241 | DLMALLOC_EXPORT default: extern |
| 242 | Defines how public APIs are declared. If you want to export via a |
| 243 | Windows DLL, you might define this as |
| 244 | #define DLMALLOC_EXPORT extern __declspec(dllexport) |
| 245 | If you want a POSIX ELF shared object, you might use |
| 246 | #define DLMALLOC_EXPORT extern __attribute__((visibility("default"))) |
| 247 | |
| 248 | MALLOC_ALIGNMENT default: (size_t)(2 * sizeof(void *)) |
| 249 | Controls the minimum alignment for malloc'ed chunks. It must be a |
| 250 | power of two and at least 8, even on machines for which smaller |
| 251 | alignments would suffice. It may be defined as larger than this |
| 252 | though. Note however that code and data structures are optimized for |
| 253 | the case of 8-byte alignment. |
| 254 | |
| 255 | MSPACES default: 0 (false) |
| 256 | If true, compile in support for independent allocation spaces. |
| 257 | This is only supported if HAVE_MMAP is true. |
| 258 | |
| 259 | ONLY_MSPACES default: 0 (false) |
| 260 | If true, only compile in mspace versions, not regular versions. |
| 261 | |
| 262 | USE_LOCKS default: 0 (false) |
| 263 | Causes each call to each public routine to be surrounded with |
| 264 | pthread or WIN32 mutex lock/unlock. (If set true, this can be |
| 265 | overridden on a per-mspace basis for mspace versions.) If set to a |
| 266 | non-zero value other than 1, locks are used, but their |
| 267 | implementation is left out, so lock functions must be supplied manually, |
| 268 | as described below. |
| 269 | |
| 270 | USE_SPIN_LOCKS default: 1 iff USE_LOCKS and spin locks available |
| 271 | If true, uses custom spin locks for locking. This is currently |
| 272 | supported only gcc >= 4.1, older gccs on x86 platforms, and recent |
| 273 | MS compilers. Otherwise, posix locks or win32 critical sections are |
| 274 | used. |
| 275 | |
| 276 | USE_RECURSIVE_LOCKS default: not defined |
| 277 | If defined nonzero, uses recursive (aka reentrant) locks, otherwise |
| 278 | uses plain mutexes. This is not required for malloc proper, but may |
| 279 | be needed for layered allocators such as nedmalloc. |
| 280 | |
| 281 | LOCK_AT_FORK default: not defined |
| 282 | If defined nonzero, performs pthread_atfork upon initialization |
| 283 | to initialize child lock while holding parent lock. The implementation |
| 284 | assumes that pthread locks (not custom locks) are being used. In other |
| 285 | cases, you may need to customize the implementation. |
| 286 | |
| 287 | FOOTERS 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 | |
| 292 | INSECURE default: 0 |
| 293 | If true, omit checks for usage errors and heap space overwrites. |
| 294 | |
| 295 | USE_DL_PREFIX default: NOT defined |
| 296 | Causes compiler to prefix all public routines with the string 'dl'. |
| 297 | This can be useful when you only want to use this malloc in one part |
| 298 | of a program, using your regular system malloc elsewhere. |
| 299 | |
| 300 | MALLOC_INSPECT_ALL default: NOT defined |
| 301 | If defined, compiles malloc_inspect_all and mspace_inspect_all, that |
| 302 | perform traversal of all heap space. Unless access to these |
| 303 | functions is otherwise restricted, you probably do not want to |
| 304 | include them in secure implementations. |
| 305 | |
| 306 | DLM_ABORT default: defined as abort() |
| 307 | Defines how to abort on failed checks. On most systems, a failed |
| 308 | check cannot die with an "assert" or even print an informative |
| 309 | message, because the underlying print routines in turn call malloc, |
| 310 | which will fail again. Generally, the best policy is to simply call |
| 311 | abort(). It's not very useful to do more than this because many |
| 312 | errors due to overwriting will show up as address faults (null, odd |
| 313 | addresses etc) rather than malloc-triggered checks, so will also |
| 314 | abort. Also, most compilers know that abort() does not return, so |
| 315 | can better optimize code conditionally calling it. |
| 316 | |
| 317 | PROCEED_ON_ERROR default: defined as 0 (false) |
| 318 | Controls whether detected bad addresses cause them to bypassed |
| 319 | rather than aborting. If set, detected bad arguments to free and |
| 320 | realloc are ignored. And all bookkeeping information is zeroed out |
| 321 | upon a detected overwrite of freed heap space, thus losing the |
| 322 | ability to ever return it from malloc again, but enabling the |
| 323 | application to proceed. If PROCEED_ON_ERROR is defined, the |
| 324 | static variable malloc_corruption_error_count is compiled in |
| 325 | and can be examined to see if errors have occurred. This option |
| 326 | generates slower code than the default abort policy. |
| 327 | |
| 328 | DEBUG default: NOT defined |
| 329 | The DEBUG setting is mainly intended for people trying to modify |
| 330 | this code or diagnose problems when porting to new platforms. |
| 331 | However, it may also be able to better isolate user errors than just |
| 332 | using runtime checks. The assertions in the check routines spell |
| 333 | out in more detail the assumptions and invariants underlying the |
| 334 | algorithms. The checking is fairly extensive, and will slow down |
| 335 | execution noticeably. Calling malloc_stats or mallinfo with DEBUG |
| 336 | set will attempt to check every non-mmapped allocated and free chunk |
| 337 | in the course of computing the summaries. |
| 338 | |
| 339 | DLM_ABORT_ON_ASSERT_FAILURE default: defined as 1 (true) |
| 340 | Debugging assertion failures can be nearly impossible if your |
| 341 | version of the assert macro causes malloc to be called, which will |
| 342 | lead to a cascade of further failures, blowing the runtime stack. |
| 343 | DLM_ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(), |
| 344 | which will usually make debugging easier. |
| 345 | |
| 346 | MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32 |
| 347 | The action to take before "return 0" when malloc fails to be able to |
| 348 | return memory because there is none available. |
| 349 | |
| 350 | HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES |
| 351 | True if this system supports sbrk or an emulation of it. |
| 352 | |
| 353 | MORECORE default: sbrk |
| 354 | The name of the sbrk-style system routine to call to obtain more |
| 355 | memory. See below for guidance on writing custom MORECORE |
| 356 | functions. The type of the argument to sbrk/MORECORE varies across |
| 357 | systems. It cannot be size_t, because it supports negative |
| 358 | arguments, so it is normally the signed type of the same width as |
| 359 | size_t (sometimes declared as "intptr_t"). It doesn't much matter |
| 360 | though. Internally, we only call it with arguments less than half |
| 361 | the max value of a size_t, which should work across all reasonable |
| 362 | possibilities, although sometimes generating compiler warnings. |
| 363 | |
| 364 | MORECORE_CONTIGUOUS default: 1 (true) if HAVE_MORECORE |
| 365 | If true, take advantage of fact that consecutive calls to MORECORE |
| 366 | with positive arguments always return contiguous increasing |
| 367 | addresses. This is true of unix sbrk. It does not hurt too much to |
| 368 | set it true anyway, since malloc copes with non-contiguities. |
| 369 | Setting it false when definitely non-contiguous saves time |
| 370 | and possibly wasted space it would take to discover this though. |
| 371 | |
| 372 | MORECORE_CANNOT_TRIM default: NOT defined |
| 373 | True if MORECORE cannot release space back to the system when given |
| 374 | negative arguments. This is generally necessary only if you are |
| 375 | using a hand-crafted MORECORE function that cannot handle negative |
| 376 | arguments. |
| 377 | |
| 378 | NO_SEGMENT_TRAVERSAL default: 0 |
| 379 | If non-zero, suppresses traversals of memory segments |
| 380 | returned by either MORECORE or CALL_MMAP. This disables |
| 381 | merging of segments that are contiguous, and selectively |
| 382 | releasing them to the OS if unused, but bounds execution times. |
| 383 | |
| 384 | HAVE_MMAP default: 1 (true) |
| 385 | True if this system supports mmap or an emulation of it. If so, and |
| 386 | HAVE_MORECORE is not true, MMAP is used for all system |
| 387 | allocation. If set and HAVE_MORECORE is true as well, MMAP is |
| 388 | primarily used to directly allocate very large blocks. It is also |
| 389 | used as a backup strategy in cases where MORECORE fails to provide |
| 390 | space from system. Note: A single call to MUNMAP is assumed to be |
| 391 | able to unmap memory that may have be allocated using multiple calls |
| 392 | to MMAP, so long as they are adjacent. |
| 393 | |
| 394 | HAVE_MREMAP default: 1 on linux, else 0 |
| 395 | If true realloc() uses mremap() to re-allocate large blocks and |
| 396 | extend or shrink allocation spaces. |
| 397 | |
| 398 | MMAP_CLEARS default: 1 except on WINCE. |
| 399 | True if mmap clears memory so calloc doesn't need to. This is true |
| 400 | for standard unix mmap using /dev/zero and on WIN32 except for WINCE. |
| 401 | |
| 402 | USE_BUILTIN_FFS default: 0 (i.e., not used) |
| 403 | Causes malloc to use the builtin ffs() function to compute indices. |
| 404 | Some compilers may recognize and intrinsify ffs to be faster than the |
| 405 | supplied C version. Also, the case of x86 using gcc is special-cased |
| 406 | to an asm instruction, so is already as fast as it can be, and so |
| 407 | this setting has no effect. Similarly for Win32 under recent MS compilers. |
| 408 | (On most x86s, the asm version is only slightly faster than the C version.) |
| 409 | |
| 410 | malloc_getpagesize default: derive from system includes, or 4096. |
| 411 | The system page size. To the extent possible, this malloc manages |
| 412 | memory from the system in page-size units. This may be (and |
| 413 | usually is) a function rather than a constant. This is ignored |
| 414 | if WIN32, where page size is determined using getSystemInfo during |
| 415 | initialization. |
| 416 | |
| 417 | USE_DEV_RANDOM default: 0 (i.e., not used) |
| 418 | Causes malloc to use /dev/random to initialize secure magic seed for |
| 419 | stamping footers. Otherwise, the current time is used. |
| 420 | |
| 421 | NO_MALLINFO default: 0 |
| 422 | If defined, don't compile "mallinfo". This can be a simple way |
| 423 | of dealing with mismatches between system declarations and |
| 424 | those in this file. |
| 425 | |
| 426 | MALLINFO_FIELD_TYPE default: size_t |
| 427 | The type of the fields in the mallinfo struct. This was originally |
| 428 | defined as "int" in SVID etc, but is more usefully defined as |
| 429 | size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set |
| 430 | |
| 431 | NO_MALLOC_STATS default: 0 |
| 432 | If defined, don't compile "malloc_stats". This avoids calls to |
| 433 | fprintf and bringing in stdio dependencies you might not want. |
| 434 | |
| 435 | REALLOC_ZERO_BYTES_FREES default: not defined |
| 436 | This should be set if a call to realloc with zero bytes should |
| 437 | be the same as a call to free. Some people think it should. Otherwise, |
| 438 | since this malloc returns a unique pointer for malloc(0), so does |
| 439 | realloc(p, 0). |
| 440 | |
| 441 | LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H |
| 442 | LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H |
| 443 | LACKS_STDLIB_H LACKS_SCHED_H LACKS_TIME_H default: NOT defined unless on WIN32 |
| 444 | Define these if your system does not have these header files. |
| 445 | You might need to manually insert some of the declarations they provide. |
| 446 | |
| 447 | DEFAULT_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 | |
| 462 | DEFAULT_TRIM_THRESHOLD default: 2MB |
| 463 | Also settable using mallopt(M_TRIM_THRESHOLD, x) |
| 464 | The maximum amount of unused top-most memory to keep before |
| 465 | releasing via malloc_trim in free(). Automatic trimming is mainly |
| 466 | useful in long-lived programs using contiguous MORECORE. Because |
| 467 | trimming via sbrk can be slow on some systems, and can sometimes be |
| 468 | wasteful (in cases where programs immediately afterward allocate |
| 469 | more large chunks) the value should be high enough so that your |
| 470 | overall system performance would improve by releasing this much |
| 471 | memory. As a rough guide, you might set to a value close to the |
| 472 | average size of a process (program) running on your system. |
| 473 | Releasing this much memory would allow such a process to run in |
| 474 | memory. Generally, it is worth tuning trim thresholds when a |
| 475 | program undergoes phases where several large chunks are allocated |
| 476 | and released in ways that can reuse each other's storage, perhaps |
| 477 | mixed with phases where there are no such chunks at all. The trim |
| 478 | value must be greater than page size to have any useful effect. To |
| 479 | disable trimming completely, you can set to MAX_SIZE_T. Note that the trick |
| 480 | some people use of mallocing a huge space and then freeing it at |
| 481 | program startup, in an attempt to reserve system memory, doesn't |
| 482 | have the intended effect under automatic trimming, since that memory |
| 483 | will immediately be returned to the system. |
| 484 | |
| 485 | DEFAULT_MMAP_THRESHOLD default: 256K |
| 486 | Also settable using mallopt(M_MMAP_THRESHOLD, x) |
| 487 | The request size threshold for using MMAP to directly service a |
| 488 | request. Requests of at least this size that cannot be allocated |
| 489 | using already-existing space will be serviced via mmap. (If enough |
| 490 | normal freed space already exists it is used instead.) Using mmap |
| 491 | segregates relatively large chunks of memory so that they can be |
| 492 | individually obtained and released from the host system. A request |
| 493 | serviced through mmap is never reused by any other request (at least |
| 494 | not directly; the system may just so happen to remap successive |
| 495 | requests to the same locations). Segregating space in this way has |
| 496 | the benefits that: Mmapped space can always be individually released |
| 497 | back to the system, which helps keep the system level memory demands |
| 498 | of a long-lived program low. Also, mapped memory doesn't become |
| 499 | `locked' between other chunks, as can happen with normally allocated |
| 500 | chunks, which means that even trimming via malloc_trim would not |
| 501 | release them. However, it has the disadvantage that the space |
| 502 | cannot be reclaimed, consolidated, and then used to service later |
| 503 | requests, as happens with normal chunks. The advantages of mmap |
| 504 | nearly always outweigh disadvantages for "large" chunks, but the |
| 505 | value of "large" may vary across systems. The default is an |
| 506 | empirically derived value that works well in most systems. You can |
| 507 | disable mmap by setting to MAX_SIZE_T. |
| 508 | |
| 509 | MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP |
| 510 | The number of consolidated frees between checks to release |
| 511 | unused segments when freeing. When using non-contiguous segments, |
| 512 | especially with multiple mspaces, checking only for topmost space |
| 513 | doesn't always suffice to trigger trimming. To compensate for this, |
| 514 | free() will, with a period of MAX_RELEASE_CHECK_RATE (or the |
| 515 | current number of segments, if greater) try to release unused |
| 516 | segments to the OS when freeing chunks that result in |
| 517 | consolidation. The best value for this parameter is a compromise |
| 518 | between slowing down frees with relatively costly checks that |
| 519 | rarely trigger versus holding on to unused memory. To effectively |
| 520 | disable, set to MAX_SIZE_T. This may lead to a very slight speed |
| 521 | improvement at the expense of carrying around more memory. |
| 522 | */ |
| 523 | |
| 524 | #include <vppinfra/clib.h> |
| 525 | #include <vppinfra/cache.h> |
| 526 | |
Dave Barach | db0a7ec | 2018-07-26 16:16:55 -0400 | [diff] [blame] | 527 | /* --- begin vpp customizations --- */ |
| 528 | |
| 529 | #if CLIB_DEBUG > 0 |
| 530 | #define FOOTERS 1 /* extra debugging */ |
Florin Coras | d055475 | 2018-07-27 11:30:46 -0700 | [diff] [blame] | 531 | #define DLM_MAGIC_CONSTANT 0xdeaddabe |
Dave Barach | db0a7ec | 2018-07-26 16:16:55 -0400 | [diff] [blame] | 532 | #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 Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 539 | /* 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 Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 602 | #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 |
Andrew Yourtchenko | 37f4456 | 2018-11-26 15:40:20 +0100 | [diff] [blame] | 702 | /* |
| 703 | * The default value in the dlmalloc was set as follows: |
| 704 | * |
| 705 | * #define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U) |
| 706 | * |
| 707 | * Above this threshold the sys_alloc() calls mmap_alloc() to directly mmap the memory. |
| 708 | * However, the interaction of this path with the rest of the vpp infra results |
| 709 | * in vpp infra considering directly mmap-allocated pieces to not |
| 710 | * be part of the heap, with predictable consequence. |
| 711 | * |
| 712 | * A simple unit-test to show that behavior is to make a small private |
| 713 | * heap and repeatedly perform vec_add1() within that heap. |
| 714 | * |
| 715 | * The issue is because there is no tracking which mmap-allocated chunk |
| 716 | * belongs to which heap. |
| 717 | * |
| 718 | * The temporary approach is to dial up the threshold so that the problematic |
| 719 | * code path never gets called. The full fix needs to introduce |
| 720 | * introduce the vector of mmap-allocated chunks to mspace, and in general |
| 721 | * do some more thorough testing. |
| 722 | */ |
| 723 | #define DEFAULT_MMAP_THRESHOLD ((size_t)~0ULL) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 724 | #else /* HAVE_MMAP */ |
| 725 | #define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T |
| 726 | #endif /* HAVE_MMAP */ |
| 727 | #endif /* DEFAULT_MMAP_THRESHOLD */ |
| 728 | #ifndef MAX_RELEASE_CHECK_RATE |
| 729 | #if HAVE_MMAP |
| 730 | #define MAX_RELEASE_CHECK_RATE 4095 |
| 731 | #else |
| 732 | #define MAX_RELEASE_CHECK_RATE MAX_SIZE_T |
| 733 | #endif /* HAVE_MMAP */ |
| 734 | #endif /* MAX_RELEASE_CHECK_RATE */ |
| 735 | #ifndef USE_BUILTIN_FFS |
| 736 | #define USE_BUILTIN_FFS 0 |
| 737 | #endif /* USE_BUILTIN_FFS */ |
| 738 | #ifndef USE_DEV_RANDOM |
| 739 | #define USE_DEV_RANDOM 0 |
| 740 | #endif /* USE_DEV_RANDOM */ |
| 741 | #ifndef NO_MALLINFO |
| 742 | #define NO_MALLINFO 0 |
| 743 | #endif /* NO_MALLINFO */ |
| 744 | #ifndef MALLINFO_FIELD_TYPE |
| 745 | #define MALLINFO_FIELD_TYPE size_t |
| 746 | #endif /* MALLINFO_FIELD_TYPE */ |
| 747 | #ifndef NO_MALLOC_STATS |
| 748 | #define NO_MALLOC_STATS 0 |
| 749 | #endif /* NO_MALLOC_STATS */ |
| 750 | #ifndef NO_SEGMENT_TRAVERSAL |
| 751 | #define NO_SEGMENT_TRAVERSAL 0 |
| 752 | #endif /* NO_SEGMENT_TRAVERSAL */ |
| 753 | |
| 754 | /* |
| 755 | mallopt tuning options. SVID/XPG defines four standard parameter |
| 756 | numbers for mallopt, normally defined in malloc.h. None of these |
| 757 | are used in this malloc, so setting them has no effect. But this |
| 758 | malloc does support the following options. |
| 759 | */ |
| 760 | |
| 761 | #define M_TRIM_THRESHOLD (-1) |
| 762 | #define M_GRANULARITY (-2) |
| 763 | #define M_MMAP_THRESHOLD (-3) |
| 764 | |
| 765 | /* ------------------------ Mallinfo declarations ------------------------ */ |
| 766 | |
| 767 | #if !NO_MALLINFO |
| 768 | /* |
| 769 | This version of malloc supports the standard SVID/XPG mallinfo |
| 770 | routine that returns a struct containing usage properties and |
| 771 | statistics. It should work on any system that has a |
| 772 | /usr/include/malloc.h defining struct mallinfo. The main |
| 773 | declaration needed is the mallinfo struct that is returned (by-copy) |
| 774 | by mallinfo(). The malloinfo struct contains a bunch of fields that |
| 775 | are not even meaningful in this version of malloc. These fields are |
| 776 | are instead filled by mallinfo() with other numbers that might be of |
| 777 | interest. |
| 778 | |
| 779 | HAVE_USR_INCLUDE_MALLOC_H should be set if you have a |
| 780 | /usr/include/malloc.h file that includes a declaration of struct |
| 781 | mallinfo. If so, it is included; else a compliant version is |
| 782 | declared below. These must be precisely the same for mallinfo() to |
| 783 | work. The original SVID version of this struct, defined on most |
| 784 | systems with mallinfo, declares all fields as ints. But some others |
| 785 | define as unsigned long. If your system defines the fields using a |
| 786 | type of different width than listed here, you MUST #include your |
| 787 | system version and #define HAVE_USR_INCLUDE_MALLOC_H. |
| 788 | */ |
| 789 | |
| 790 | /* #define HAVE_USR_INCLUDE_MALLOC_H */ |
| 791 | |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 792 | #if 0 // def HAVE_USR_INCLUDE_MALLOC_H |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 793 | #include "/usr/include/malloc.h" |
| 794 | #else /* HAVE_USR_INCLUDE_MALLOC_H */ |
| 795 | #ifndef STRUCT_MALLINFO_DECLARED |
| 796 | /* HP-UX (and others?) redefines mallinfo unless _STRUCT_MALLINFO is defined */ |
| 797 | #define _STRUCT_MALLINFO |
| 798 | #define STRUCT_MALLINFO_DECLARED 1 |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 799 | struct dlmallinfo { |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 800 | MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */ |
| 801 | MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */ |
| 802 | MALLINFO_FIELD_TYPE smblks; /* always 0 */ |
| 803 | MALLINFO_FIELD_TYPE hblks; /* always 0 */ |
| 804 | MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */ |
| 805 | MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */ |
| 806 | MALLINFO_FIELD_TYPE fsmblks; /* always 0 */ |
| 807 | MALLINFO_FIELD_TYPE uordblks; /* total allocated space */ |
| 808 | MALLINFO_FIELD_TYPE fordblks; /* total free space */ |
| 809 | MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ |
| 810 | }; |
| 811 | #endif /* STRUCT_MALLINFO_DECLARED */ |
| 812 | #endif /* HAVE_USR_INCLUDE_MALLOC_H */ |
| 813 | #endif /* NO_MALLINFO */ |
| 814 | |
| 815 | /* |
| 816 | Try to persuade compilers to inline. The most critical functions for |
| 817 | inlining are defined as macros, so these aren't used for them. |
| 818 | */ |
| 819 | |
| 820 | #ifndef FORCEINLINE |
| 821 | #if defined(__GNUC__) |
| 822 | #define FORCEINLINE __inline __attribute__ ((always_inline)) |
| 823 | #elif defined(_MSC_VER) |
| 824 | #define FORCEINLINE __forceinline |
| 825 | #endif |
| 826 | #endif |
| 827 | #ifndef NOINLINE |
| 828 | #if defined(__GNUC__) |
| 829 | #define NOINLINE __attribute__ ((noinline)) |
| 830 | #elif defined(_MSC_VER) |
| 831 | #define NOINLINE __declspec(noinline) |
| 832 | #else |
| 833 | #define NOINLINE |
| 834 | #endif |
| 835 | #endif |
| 836 | |
| 837 | #ifdef __cplusplus |
| 838 | extern "C" { |
| 839 | #ifndef FORCEINLINE |
| 840 | #define FORCEINLINE inline |
| 841 | #endif |
| 842 | #endif /* __cplusplus */ |
| 843 | #ifndef FORCEINLINE |
| 844 | #define FORCEINLINE |
| 845 | #endif |
| 846 | |
| 847 | #if !ONLY_MSPACES |
| 848 | |
| 849 | /* ------------------- Declarations of public routines ------------------- */ |
| 850 | |
| 851 | #ifndef USE_DL_PREFIX |
| 852 | #define dlcalloc calloc |
| 853 | #define dlfree free |
| 854 | #define dlmalloc malloc |
| 855 | #define dlmemalign memalign |
| 856 | #define dlposix_memalign posix_memalign |
| 857 | #define dlrealloc realloc |
| 858 | #define dlrealloc_in_place realloc_in_place |
| 859 | #define dlvalloc valloc |
| 860 | #define dlpvalloc pvalloc |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 861 | // #define dlmallinfo mallinfo |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 862 | #define dlmallopt mallopt |
| 863 | #define dlmalloc_trim malloc_trim |
| 864 | #define dlmalloc_stats malloc_stats |
| 865 | #define dlmalloc_usable_size malloc_usable_size |
| 866 | #define dlmalloc_footprint malloc_footprint |
| 867 | #define dlmalloc_max_footprint malloc_max_footprint |
| 868 | #define dlmalloc_footprint_limit malloc_footprint_limit |
| 869 | #define dlmalloc_set_footprint_limit malloc_set_footprint_limit |
| 870 | #define dlmalloc_inspect_all malloc_inspect_all |
| 871 | #define dlindependent_calloc independent_calloc |
| 872 | #define dlindependent_comalloc independent_comalloc |
| 873 | #define dlbulk_free bulk_free |
| 874 | #endif /* USE_DL_PREFIX */ |
| 875 | |
| 876 | /* |
| 877 | malloc(size_t n) |
| 878 | Returns a pointer to a newly allocated chunk of at least n bytes, or |
| 879 | null if no space is available, in which case errno is set to ENOMEM |
| 880 | on ANSI C systems. |
| 881 | |
| 882 | If n is zero, malloc returns a minimum-sized chunk. (The minimum |
| 883 | size is 16 bytes on most 32bit systems, and 32 bytes on 64bit |
| 884 | systems.) Note that size_t is an unsigned type, so calls with |
| 885 | arguments that would be negative if signed are interpreted as |
| 886 | requests for huge amounts of space, which will often fail. The |
| 887 | maximum supported value of n differs across systems, but is in all |
| 888 | cases less than the maximum representable value of a size_t. |
| 889 | */ |
| 890 | DLMALLOC_EXPORT void* dlmalloc(size_t); |
| 891 | |
| 892 | /* |
| 893 | free(void* p) |
| 894 | Releases the chunk of memory pointed to by p, that had been previously |
| 895 | allocated using malloc or a related routine such as realloc. |
| 896 | It has no effect if p is null. If p was not malloced or already |
| 897 | freed, free(p) will by default cause the current program to abort. |
| 898 | */ |
| 899 | DLMALLOC_EXPORT void dlfree(void*); |
| 900 | |
| 901 | /* |
| 902 | calloc(size_t n_elements, size_t element_size); |
| 903 | Returns a pointer to n_elements * element_size bytes, with all locations |
| 904 | set to zero. |
| 905 | */ |
| 906 | DLMALLOC_EXPORT void* dlcalloc(size_t, size_t); |
| 907 | |
| 908 | /* |
| 909 | realloc(void* p, size_t n) |
| 910 | Returns a pointer to a chunk of size n that contains the same data |
| 911 | as does chunk p up to the minimum of (n, p's size) bytes, or null |
| 912 | if no space is available. |
| 913 | |
| 914 | The returned pointer may or may not be the same as p. The algorithm |
| 915 | prefers extending p in most cases when possible, otherwise it |
| 916 | employs the equivalent of a malloc-copy-free sequence. |
| 917 | |
| 918 | If p is null, realloc is equivalent to malloc. |
| 919 | |
| 920 | If space is not available, realloc returns null, errno is set (if on |
| 921 | ANSI) and p is NOT freed. |
| 922 | |
| 923 | if n is for fewer bytes than already held by p, the newly unused |
| 924 | space is lopped off and freed if possible. realloc with a size |
| 925 | argument of zero (re)allocates a minimum-sized chunk. |
| 926 | |
| 927 | The old unix realloc convention of allowing the last-free'd chunk |
| 928 | to be used as an argument to realloc is not supported. |
| 929 | */ |
| 930 | DLMALLOC_EXPORT void* dlrealloc(void*, size_t); |
| 931 | |
| 932 | /* |
| 933 | realloc_in_place(void* p, size_t n) |
| 934 | Resizes the space allocated for p to size n, only if this can be |
| 935 | done without moving p (i.e., only if there is adjacent space |
| 936 | available if n is greater than p's current allocated size, or n is |
| 937 | less than or equal to p's size). This may be used instead of plain |
| 938 | realloc if an alternative allocation strategy is needed upon failure |
| 939 | to expand space; for example, reallocation of a buffer that must be |
| 940 | memory-aligned or cleared. You can use realloc_in_place to trigger |
| 941 | these alternatives only when needed. |
| 942 | |
| 943 | Returns p if successful; otherwise null. |
| 944 | */ |
| 945 | DLMALLOC_EXPORT void* dlrealloc_in_place(void*, size_t); |
| 946 | |
| 947 | /* |
| 948 | memalign(size_t alignment, size_t n); |
| 949 | Returns a pointer to a newly allocated chunk of n bytes, aligned |
| 950 | in accord with the alignment argument. |
| 951 | |
| 952 | The alignment argument should be a power of two. If the argument is |
| 953 | not a power of two, the nearest greater power is used. |
| 954 | 8-byte alignment is guaranteed by normal malloc calls, so don't |
| 955 | bother calling memalign with an argument of 8 or less. |
| 956 | |
| 957 | Overreliance on memalign is a sure way to fragment space. |
| 958 | */ |
| 959 | DLMALLOC_EXPORT void* dlmemalign(size_t, size_t); |
| 960 | |
| 961 | /* |
| 962 | int posix_memalign(void** pp, size_t alignment, size_t n); |
| 963 | Allocates a chunk of n bytes, aligned in accord with the alignment |
| 964 | argument. Differs from memalign only in that it (1) assigns the |
| 965 | allocated memory to *pp rather than returning it, (2) fails and |
| 966 | returns EINVAL if the alignment is not a power of two (3) fails and |
| 967 | returns ENOMEM if memory cannot be allocated. |
| 968 | */ |
| 969 | DLMALLOC_EXPORT int dlposix_memalign(void**, size_t, size_t); |
| 970 | |
| 971 | /* |
| 972 | valloc(size_t n); |
| 973 | Equivalent to memalign(pagesize, n), where pagesize is the page |
| 974 | size of the system. If the pagesize is unknown, 4096 is used. |
| 975 | */ |
| 976 | DLMALLOC_EXPORT void* dlvalloc(size_t); |
| 977 | |
| 978 | /* |
| 979 | mallopt(int parameter_number, int parameter_value) |
| 980 | Sets tunable parameters The format is to provide a |
| 981 | (parameter-number, parameter-value) pair. mallopt then sets the |
| 982 | corresponding parameter to the argument value if it can (i.e., so |
| 983 | long as the value is meaningful), and returns 1 if successful else |
| 984 | 0. To workaround the fact that mallopt is specified to use int, |
| 985 | not size_t parameters, the value -1 is specially treated as the |
| 986 | maximum unsigned size_t value. |
| 987 | |
| 988 | SVID/XPG/ANSI defines four standard param numbers for mallopt, |
| 989 | normally defined in malloc.h. None of these are use in this malloc, |
| 990 | so setting them has no effect. But this malloc also supports other |
| 991 | options in mallopt. See below for details. Briefly, supported |
| 992 | parameters are as follows (listed defaults are for "typical" |
| 993 | configurations). |
| 994 | |
| 995 | Symbol param # default allowed param values |
| 996 | M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables) |
| 997 | M_GRANULARITY -2 page size any power of 2 >= page size |
| 998 | M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) |
| 999 | */ |
| 1000 | DLMALLOC_EXPORT int dlmallopt(int, int); |
| 1001 | |
| 1002 | /* |
| 1003 | malloc_footprint(); |
| 1004 | Returns the number of bytes obtained from the system. The total |
| 1005 | number of bytes allocated by malloc, realloc etc., is less than this |
| 1006 | value. Unlike mallinfo, this function returns only a precomputed |
| 1007 | result, so can be called frequently to monitor memory consumption. |
| 1008 | Even if locks are otherwise defined, this function does not use them, |
| 1009 | so results might not be up to date. |
| 1010 | */ |
| 1011 | DLMALLOC_EXPORT size_t dlmalloc_footprint(void); |
| 1012 | |
| 1013 | /* |
| 1014 | malloc_max_footprint(); |
| 1015 | Returns the maximum number of bytes obtained from the system. This |
| 1016 | value will be greater than current footprint if deallocated space |
| 1017 | has been reclaimed by the system. The peak number of bytes allocated |
| 1018 | by malloc, realloc etc., is less than this value. Unlike mallinfo, |
| 1019 | this function returns only a precomputed result, so can be called |
| 1020 | frequently to monitor memory consumption. Even if locks are |
| 1021 | otherwise defined, this function does not use them, so results might |
| 1022 | not be up to date. |
| 1023 | */ |
| 1024 | DLMALLOC_EXPORT size_t dlmalloc_max_footprint(void); |
| 1025 | |
| 1026 | /* |
| 1027 | malloc_footprint_limit(); |
| 1028 | Returns the number of bytes that the heap is allowed to obtain from |
| 1029 | the system, returning the last value returned by |
| 1030 | malloc_set_footprint_limit, or the maximum size_t value if |
| 1031 | never set. The returned value reflects a permission. There is no |
| 1032 | guarantee that this number of bytes can actually be obtained from |
| 1033 | the system. |
| 1034 | */ |
| 1035 | DLMALLOC_EXPORT size_t dlmalloc_footprint_limit(); |
| 1036 | |
| 1037 | /* |
| 1038 | malloc_set_footprint_limit(); |
| 1039 | Sets the maximum number of bytes to obtain from the system, causing |
| 1040 | failure returns from malloc and related functions upon attempts to |
| 1041 | exceed this value. The argument value may be subject to page |
| 1042 | rounding to an enforceable limit; this actual value is returned. |
| 1043 | Using an argument of the maximum possible size_t effectively |
| 1044 | disables checks. If the argument is less than or equal to the |
| 1045 | current malloc_footprint, then all future allocations that require |
| 1046 | additional system memory will fail. However, invocation cannot |
| 1047 | retroactively deallocate existing used memory. |
| 1048 | */ |
| 1049 | DLMALLOC_EXPORT size_t dlmalloc_set_footprint_limit(size_t bytes); |
| 1050 | |
| 1051 | #if MALLOC_INSPECT_ALL |
| 1052 | /* |
| 1053 | malloc_inspect_all(void(*handler)(void *start, |
| 1054 | void *end, |
| 1055 | size_t used_bytes, |
| 1056 | void* callback_arg), |
| 1057 | void* arg); |
| 1058 | Traverses the heap and calls the given handler for each managed |
| 1059 | region, skipping all bytes that are (or may be) used for bookkeeping |
| 1060 | purposes. Traversal does not include include chunks that have been |
| 1061 | directly memory mapped. Each reported region begins at the start |
| 1062 | address, and continues up to but not including the end address. The |
| 1063 | first used_bytes of the region contain allocated data. If |
| 1064 | used_bytes is zero, the region is unallocated. The handler is |
| 1065 | invoked with the given callback argument. If locks are defined, they |
| 1066 | are held during the entire traversal. It is a bad idea to invoke |
| 1067 | other malloc functions from within the handler. |
| 1068 | |
| 1069 | For example, to count the number of in-use chunks with size greater |
| 1070 | than 1000, you could write: |
| 1071 | static int count = 0; |
| 1072 | void count_chunks(void* start, void* end, size_t used, void* arg) { |
| 1073 | if (used >= 1000) ++count; |
| 1074 | } |
| 1075 | then: |
| 1076 | malloc_inspect_all(count_chunks, NULL); |
| 1077 | |
| 1078 | malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined. |
| 1079 | */ |
| 1080 | DLMALLOC_EXPORT void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*), |
| 1081 | void* arg); |
| 1082 | |
| 1083 | #endif /* MALLOC_INSPECT_ALL */ |
| 1084 | |
| 1085 | #if !NO_MALLINFO |
| 1086 | /* |
| 1087 | mallinfo() |
| 1088 | Returns (by copy) a struct containing various summary statistics: |
| 1089 | |
| 1090 | arena: current total non-mmapped bytes allocated from system |
| 1091 | ordblks: the number of free chunks |
| 1092 | smblks: always zero. |
| 1093 | hblks: current number of mmapped regions |
| 1094 | hblkhd: total bytes held in mmapped regions |
| 1095 | usmblks: the maximum total allocated space. This will be greater |
| 1096 | than current total if trimming has occurred. |
| 1097 | fsmblks: always zero |
| 1098 | uordblks: current total allocated space (normal or mmapped) |
| 1099 | fordblks: total free space |
| 1100 | keepcost: the maximum number of bytes that could ideally be released |
| 1101 | back to system via malloc_trim. ("ideally" means that |
| 1102 | it ignores page restrictions etc.) |
| 1103 | |
| 1104 | Because these fields are ints, but internal bookkeeping may |
| 1105 | be kept as longs, the reported values may wrap around zero and |
| 1106 | thus be inaccurate. |
| 1107 | */ |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 1108 | DLMALLOC_EXPORT struct dlmallinfo dlmallinfo(void); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1109 | #endif /* NO_MALLINFO */ |
| 1110 | |
| 1111 | /* |
| 1112 | independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); |
| 1113 | |
| 1114 | independent_calloc is similar to calloc, but instead of returning a |
| 1115 | single cleared space, it returns an array of pointers to n_elements |
| 1116 | independent elements that can hold contents of size elem_size, each |
| 1117 | of which starts out cleared, and can be independently freed, |
| 1118 | realloc'ed etc. The elements are guaranteed to be adjacently |
| 1119 | allocated (this is not guaranteed to occur with multiple callocs or |
| 1120 | mallocs), which may also improve cache locality in some |
| 1121 | applications. |
| 1122 | |
| 1123 | The "chunks" argument is optional (i.e., may be null, which is |
| 1124 | probably the most typical usage). If it is null, the returned array |
| 1125 | is itself dynamically allocated and should also be freed when it is |
| 1126 | no longer needed. Otherwise, the chunks array must be of at least |
| 1127 | n_elements in length. It is filled in with the pointers to the |
| 1128 | chunks. |
| 1129 | |
| 1130 | In either case, independent_calloc returns this pointer array, or |
| 1131 | null if the allocation failed. If n_elements is zero and "chunks" |
| 1132 | is null, it returns a chunk representing an array with zero elements |
| 1133 | (which should be freed if not wanted). |
| 1134 | |
| 1135 | Each element must be freed when it is no longer needed. This can be |
| 1136 | done all at once using bulk_free. |
| 1137 | |
| 1138 | independent_calloc simplifies and speeds up implementations of many |
| 1139 | kinds of pools. It may also be useful when constructing large data |
| 1140 | structures that initially have a fixed number of fixed-sized nodes, |
| 1141 | but the number is not known at compile time, and some of the nodes |
| 1142 | may later need to be freed. For example: |
| 1143 | |
| 1144 | struct Node { int item; struct Node* next; }; |
| 1145 | |
| 1146 | struct Node* build_list() { |
| 1147 | struct Node** pool; |
| 1148 | int n = read_number_of_nodes_needed(); |
| 1149 | if (n <= 0) return 0; |
| 1150 | pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0); |
| 1151 | if (pool == 0) die(); |
| 1152 | // organize into a linked list... |
| 1153 | struct Node* first = pool[0]; |
| 1154 | for (i = 0; i < n-1; ++i) |
| 1155 | pool[i]->next = pool[i+1]; |
| 1156 | free(pool); // Can now free the array (or not, if it is needed later) |
| 1157 | return first; |
| 1158 | } |
| 1159 | */ |
| 1160 | DLMALLOC_EXPORT void** dlindependent_calloc(size_t, size_t, void**); |
| 1161 | |
| 1162 | /* |
| 1163 | independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); |
| 1164 | |
| 1165 | independent_comalloc allocates, all at once, a set of n_elements |
| 1166 | chunks with sizes indicated in the "sizes" array. It returns |
| 1167 | an array of pointers to these elements, each of which can be |
| 1168 | independently freed, realloc'ed etc. The elements are guaranteed to |
| 1169 | be adjacently allocated (this is not guaranteed to occur with |
| 1170 | multiple callocs or mallocs), which may also improve cache locality |
| 1171 | in some applications. |
| 1172 | |
| 1173 | The "chunks" argument is optional (i.e., may be null). If it is null |
| 1174 | the returned array is itself dynamically allocated and should also |
| 1175 | be freed when it is no longer needed. Otherwise, the chunks array |
| 1176 | must be of at least n_elements in length. It is filled in with the |
| 1177 | pointers to the chunks. |
| 1178 | |
| 1179 | In either case, independent_comalloc returns this pointer array, or |
| 1180 | null if the allocation failed. If n_elements is zero and chunks is |
| 1181 | null, it returns a chunk representing an array with zero elements |
| 1182 | (which should be freed if not wanted). |
| 1183 | |
| 1184 | Each element must be freed when it is no longer needed. This can be |
| 1185 | done all at once using bulk_free. |
| 1186 | |
| 1187 | independent_comallac differs from independent_calloc in that each |
| 1188 | element may have a different size, and also that it does not |
| 1189 | automatically clear elements. |
| 1190 | |
| 1191 | independent_comalloc can be used to speed up allocation in cases |
| 1192 | where several structs or objects must always be allocated at the |
| 1193 | same time. For example: |
| 1194 | |
| 1195 | struct Head { ... } |
| 1196 | struct Foot { ... } |
| 1197 | |
| 1198 | void send_message(char* msg) { |
| 1199 | int msglen = strlen(msg); |
| 1200 | size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; |
| 1201 | void* chunks[3]; |
| 1202 | if (independent_comalloc(3, sizes, chunks) == 0) |
| 1203 | die(); |
| 1204 | struct Head* head = (struct Head*)(chunks[0]); |
| 1205 | char* body = (char*)(chunks[1]); |
| 1206 | struct Foot* foot = (struct Foot*)(chunks[2]); |
| 1207 | // ... |
| 1208 | } |
| 1209 | |
| 1210 | In general though, independent_comalloc is worth using only for |
| 1211 | larger values of n_elements. For small values, you probably won't |
| 1212 | detect enough difference from series of malloc calls to bother. |
| 1213 | |
| 1214 | Overuse of independent_comalloc can increase overall memory usage, |
| 1215 | since it cannot reuse existing noncontiguous small chunks that |
| 1216 | might be available for some of the elements. |
| 1217 | */ |
| 1218 | DLMALLOC_EXPORT void** dlindependent_comalloc(size_t, size_t*, void**); |
| 1219 | |
| 1220 | /* |
| 1221 | bulk_free(void* array[], size_t n_elements) |
| 1222 | Frees and clears (sets to null) each non-null pointer in the given |
| 1223 | array. This is likely to be faster than freeing them one-by-one. |
| 1224 | If footers are used, pointers that have been allocated in different |
| 1225 | mspaces are not freed or cleared, and the count of all such pointers |
| 1226 | is returned. For large arrays of pointers with poor locality, it |
| 1227 | may be worthwhile to sort this array before calling bulk_free. |
| 1228 | */ |
| 1229 | DLMALLOC_EXPORT size_t dlbulk_free(void**, size_t n_elements); |
| 1230 | |
| 1231 | /* |
| 1232 | pvalloc(size_t n); |
| 1233 | Equivalent to valloc(minimum-page-that-holds(n)), that is, |
| 1234 | round up n to nearest pagesize. |
| 1235 | */ |
| 1236 | DLMALLOC_EXPORT void* dlpvalloc(size_t); |
| 1237 | |
| 1238 | /* |
| 1239 | malloc_trim(size_t pad); |
| 1240 | |
| 1241 | If possible, gives memory back to the system (via negative arguments |
| 1242 | to sbrk) if there is unused memory at the `high' end of the malloc |
| 1243 | pool or in unused MMAP segments. You can call this after freeing |
| 1244 | large blocks of memory to potentially reduce the system-level memory |
| 1245 | requirements of a program. However, it cannot guarantee to reduce |
| 1246 | memory. Under some allocation patterns, some large free blocks of |
| 1247 | memory will be locked between two used chunks, so they cannot be |
| 1248 | given back to the system. |
| 1249 | |
| 1250 | The `pad' argument to malloc_trim represents the amount of free |
| 1251 | trailing space to leave untrimmed. If this argument is zero, only |
| 1252 | the minimum amount of memory to maintain internal data structures |
| 1253 | will be left. Non-zero arguments can be supplied to maintain enough |
| 1254 | trailing space to service future expected allocations without having |
| 1255 | to re-obtain memory from the system. |
| 1256 | |
| 1257 | Malloc_trim returns 1 if it actually released any memory, else 0. |
| 1258 | */ |
| 1259 | DLMALLOC_EXPORT int dlmalloc_trim(size_t); |
| 1260 | |
| 1261 | /* |
| 1262 | malloc_stats(); |
| 1263 | Prints on stderr the amount of space obtained from the system (both |
| 1264 | via sbrk and mmap), the maximum amount (which may be more than |
| 1265 | current if malloc_trim and/or munmap got called), and the current |
| 1266 | number of bytes allocated via malloc (or realloc, etc) but not yet |
| 1267 | freed. Note that this is the number of bytes allocated, not the |
| 1268 | number requested. It will be larger than the number requested |
| 1269 | because of alignment and bookkeeping overhead. Because it includes |
| 1270 | alignment wastage as being in use, this figure may be greater than |
| 1271 | zero even when no user-level chunks are allocated. |
| 1272 | |
| 1273 | The reported current and maximum system memory can be inaccurate if |
| 1274 | a program makes other calls to system memory allocation functions |
| 1275 | (normally sbrk) outside of malloc. |
| 1276 | |
| 1277 | malloc_stats prints only the most commonly interesting statistics. |
| 1278 | More information can be obtained by calling mallinfo. |
| 1279 | */ |
| 1280 | DLMALLOC_EXPORT void dlmalloc_stats(void); |
| 1281 | |
| 1282 | /* |
| 1283 | malloc_usable_size(void* p); |
| 1284 | |
| 1285 | Returns the number of bytes you can actually use in |
| 1286 | an allocated chunk, which may be more than you requested (although |
| 1287 | often not) due to alignment and minimum size constraints. |
| 1288 | You can use this many bytes without worrying about |
| 1289 | overwriting other allocated objects. This is not a particularly great |
| 1290 | programming practice. malloc_usable_size can be more useful in |
| 1291 | debugging and assertions, for example: |
| 1292 | |
| 1293 | p = malloc(n); |
| 1294 | assert(malloc_usable_size(p) >= 256); |
| 1295 | */ |
| 1296 | size_t dlmalloc_usable_size(void*); |
| 1297 | |
| 1298 | #endif /* ONLY_MSPACES */ |
| 1299 | |
| 1300 | #if MSPACES |
| 1301 | |
| 1302 | /* |
| 1303 | mspace is an opaque type representing an independent |
| 1304 | region of space that supports mspace_malloc, etc. |
| 1305 | */ |
| 1306 | typedef void* mspace; |
| 1307 | |
| 1308 | /* |
| 1309 | create_mspace creates and returns a new independent space with the |
| 1310 | given initial capacity, or, if 0, the default granularity size. It |
| 1311 | returns null if there is no system memory available to create the |
| 1312 | space. If argument locked is non-zero, the space uses a separate |
| 1313 | lock to control access. The capacity of the space will grow |
| 1314 | dynamically as needed to service mspace_malloc requests. You can |
| 1315 | control the sizes of incremental increases of this space by |
| 1316 | compiling with a different DEFAULT_GRANULARITY or dynamically |
| 1317 | setting with mallopt(M_GRANULARITY, value). |
| 1318 | */ |
| 1319 | DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked); |
| 1320 | |
| 1321 | /* |
| 1322 | destroy_mspace destroys the given space, and attempts to return all |
| 1323 | of its memory back to the system, returning the total number of |
| 1324 | bytes freed. After destruction, the results of access to all memory |
| 1325 | used by the space become undefined. |
| 1326 | */ |
| 1327 | DLMALLOC_EXPORT size_t destroy_mspace(mspace msp); |
| 1328 | |
| 1329 | /* |
| 1330 | create_mspace_with_base uses the memory supplied as the initial base |
| 1331 | of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this |
| 1332 | space is used for bookkeeping, so the capacity must be at least this |
| 1333 | large. (Otherwise 0 is returned.) When this initial space is |
| 1334 | exhausted, additional memory will be obtained from the system. |
| 1335 | Destroying this space will deallocate all additionally allocated |
| 1336 | space (if possible) but not the initial base. |
| 1337 | */ |
| 1338 | DLMALLOC_EXPORT mspace create_mspace_with_base(void* base, size_t capacity, int locked); |
| 1339 | |
| 1340 | /* |
| 1341 | mspace_track_large_chunks controls whether requests for large chunks |
| 1342 | are allocated in their own untracked mmapped regions, separate from |
| 1343 | others in this mspace. By default large chunks are not tracked, |
| 1344 | which reduces fragmentation. However, such chunks are not |
| 1345 | necessarily released to the system upon destroy_mspace. Enabling |
| 1346 | tracking by setting to true may increase fragmentation, but avoids |
| 1347 | leakage when relying on destroy_mspace to release all memory |
| 1348 | allocated using this space. The function returns the previous |
| 1349 | setting. |
| 1350 | */ |
| 1351 | DLMALLOC_EXPORT int mspace_track_large_chunks(mspace msp, int enable); |
| 1352 | |
| 1353 | |
| 1354 | /* |
| 1355 | mspace_malloc behaves as malloc, but operates within |
| 1356 | the given space. |
| 1357 | */ |
| 1358 | DLMALLOC_EXPORT void* mspace_malloc(mspace msp, size_t bytes); |
| 1359 | |
| 1360 | /* |
| 1361 | mspace_free behaves as free, but operates within |
| 1362 | the given space. |
| 1363 | |
| 1364 | If compiled with FOOTERS==1, mspace_free is not actually needed. |
| 1365 | free may be called instead of mspace_free because freed chunks from |
| 1366 | any space are handled by their originating spaces. |
| 1367 | */ |
| 1368 | DLMALLOC_EXPORT void mspace_free(mspace msp, void* mem); |
| 1369 | |
| 1370 | /* |
| 1371 | mspace_realloc behaves as realloc, but operates within |
| 1372 | the given space. |
| 1373 | |
| 1374 | If compiled with FOOTERS==1, mspace_realloc is not actually |
| 1375 | needed. realloc may be called instead of mspace_realloc because |
| 1376 | realloced chunks from any space are handled by their originating |
| 1377 | spaces. |
| 1378 | */ |
| 1379 | DLMALLOC_EXPORT void* mspace_realloc(mspace msp, void* mem, size_t newsize); |
| 1380 | |
| 1381 | /* |
| 1382 | mspace_calloc behaves as calloc, but operates within |
| 1383 | the given space. |
| 1384 | */ |
| 1385 | DLMALLOC_EXPORT void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); |
| 1386 | |
| 1387 | /* |
| 1388 | mspace_memalign behaves as memalign, but operates within |
| 1389 | the given space. |
| 1390 | */ |
| 1391 | DLMALLOC_EXPORT void* mspace_memalign(mspace msp, size_t alignment, size_t bytes); |
| 1392 | |
| 1393 | /* |
| 1394 | mspace_independent_calloc behaves as independent_calloc, but |
| 1395 | operates within the given space. |
| 1396 | */ |
| 1397 | DLMALLOC_EXPORT void** mspace_independent_calloc(mspace msp, size_t n_elements, |
| 1398 | size_t elem_size, void* chunks[]); |
| 1399 | |
| 1400 | /* |
| 1401 | mspace_independent_comalloc behaves as independent_comalloc, but |
| 1402 | operates within the given space. |
| 1403 | */ |
| 1404 | DLMALLOC_EXPORT void** mspace_independent_comalloc(mspace msp, size_t n_elements, |
| 1405 | size_t sizes[], void* chunks[]); |
| 1406 | |
| 1407 | /* |
| 1408 | mspace_footprint() returns the number of bytes obtained from the |
| 1409 | system for this space. |
| 1410 | */ |
| 1411 | DLMALLOC_EXPORT size_t mspace_footprint(mspace msp); |
| 1412 | |
| 1413 | /* |
| 1414 | mspace_max_footprint() returns the peak number of bytes obtained from the |
| 1415 | system for this space. |
| 1416 | */ |
| 1417 | DLMALLOC_EXPORT size_t mspace_max_footprint(mspace msp); |
| 1418 | |
| 1419 | |
| 1420 | #if !NO_MALLINFO |
| 1421 | /* |
| 1422 | mspace_mallinfo behaves as mallinfo, but reports properties of |
| 1423 | the given space. |
| 1424 | */ |
Dave Barach | af7dd5b | 2018-08-23 17:08:44 -0400 | [diff] [blame] | 1425 | DLMALLOC_EXPORT struct dlmallinfo mspace_mallinfo(mspace msp); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1426 | #endif /* NO_MALLINFO */ |
| 1427 | |
| 1428 | /* |
| 1429 | malloc_usable_size(void* p) behaves the same as malloc_usable_size; |
| 1430 | */ |
| 1431 | DLMALLOC_EXPORT size_t mspace_usable_size(const void* mem); |
| 1432 | |
| 1433 | /* |
| 1434 | mspace_malloc_stats behaves as malloc_stats, but reports |
| 1435 | properties of the given space. |
| 1436 | */ |
| 1437 | DLMALLOC_EXPORT void mspace_malloc_stats(mspace msp); |
| 1438 | |
| 1439 | /* |
| 1440 | mspace_trim behaves as malloc_trim, but |
| 1441 | operates within the given space. |
| 1442 | */ |
| 1443 | DLMALLOC_EXPORT int mspace_trim(mspace msp, size_t pad); |
| 1444 | |
| 1445 | /* |
| 1446 | An alias for mallopt. |
| 1447 | */ |
| 1448 | DLMALLOC_EXPORT int mspace_mallopt(int, int); |
| 1449 | |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 1450 | DLMALLOC_EXPORT void* mspace_get_aligned (mspace msp, |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 1451 | unsigned long n_user_data_bytes, |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 1452 | unsigned long align, |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 1453 | unsigned long align_offset); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1454 | |
| 1455 | DLMALLOC_EXPORT int mspace_is_heap_object (mspace msp, void *p); |
| 1456 | |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 1457 | DLMALLOC_EXPORT void mspace_get_address_and_size (mspace msp, char **addrp, size_t *sizep); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1458 | DLMALLOC_EXPORT void mspace_put (mspace msp, void *p); |
| 1459 | DLMALLOC_EXPORT void mspace_put_no_offset (mspace msp, void *p); |
| 1460 | DLMALLOC_EXPORT size_t mspace_usable_size_with_delta (const void *p); |
| 1461 | DLMALLOC_EXPORT void mspace_disable_expand (mspace msp); |
| 1462 | DLMALLOC_EXPORT void *mspace_least_addr (mspace msp); |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 1463 | DLMALLOC_EXPORT void mheap_get_trace (uword offset, uword size); |
| 1464 | DLMALLOC_EXPORT void mheap_put_trace (uword offset, uword size); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1465 | DLMALLOC_EXPORT int mspace_enable_disable_trace (mspace msp, int enable); |
Dave Barach | d67a428 | 2019-06-15 12:46:13 -0400 | [diff] [blame] | 1466 | DLMALLOC_EXPORT int mspace_is_traced (mspace msp); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 1467 | |
| 1468 | #endif /* MSPACES */ |
| 1469 | |
| 1470 | #ifdef __cplusplus |
| 1471 | } /* end of extern "C" */ |
| 1472 | #endif /* __cplusplus */ |
| 1473 | |
| 1474 | /* |
| 1475 | ======================================================================== |
| 1476 | To make a fully customizable malloc.h header file, cut everything |
| 1477 | above this line, put into file malloc.h, edit to suit, and #include it |
| 1478 | on the next line, as well as in programs that use this malloc. |
| 1479 | ======================================================================== |
| 1480 | */ |