From 2bdec18d0e8d95e7cbc0eeaa931b6948f61f4c96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Cant=C3=B3n=20Cort=C3=A9s?= Date: Thu, 13 Oct 2022 19:16:09 +0200 Subject: [PATCH 01/10] Fix dlmalloc for allocations bigger than 2GB. --- system/lib/dlmalloc.c | 689 +++++++++++++++++++++--------------------- 1 file changed, 347 insertions(+), 342 deletions(-) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index b4bded1872a03..6297b0534e7c7 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -49,20 +49,20 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); Doug Lea and released to the public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/ Send questions, comments, complaints, performance data, etc to dl@cs.oswego.edu - + * Version 2.8.6 Wed Aug 29 06:57:58 2012 Doug Lea Note: There may be an updated version of this malloc obtainable at ftp://gee.cs.oswego.edu/pub/misc/malloc.c Check before installing! - + * Quickstart - + This library is all in one file to simplify the most common usage: ftp it, compile it (-O3), and link it into another program. All of the compile-time options default to reasonable values for use on most platforms. You might later want to step through various compile-time and dynamic tuning options. - + For convenience, an include file for code using this malloc is at: ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.6.h You don't really need this .h file unless you call functions not @@ -76,30 +76,30 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); malloc (for example in linux). You might still want to use the one in this file to customize settings or to avoid overheads associated with library versions. - + * Vital statistics: - + Supported pointer/size_t representation: 4 or 8 bytes size_t MUST be an unsigned type of the same width as pointers. (If you are using an ancient system that declares size_t as a signed type, or need it to be a different width than pointers, you can use a previous release of this malloc (e.g. 2.7.2) supporting these.) - + Alignment: 8 bytes (minimum) This suffices for nearly all current machines and C compilers. However, you can define MALLOC_ALIGNMENT to be wider than this if necessary (up to 128bytes), at the expense of using more space. - + Minimum overhead per allocated chunk: 4 or 8 bytes (if 4byte sizes) 8 or 16 bytes (if 8byte sizes) Each malloced chunk has a hidden word of overhead holding size and status information, and additional cross-check word if FOOTERS is defined. - + Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead) 8-byte ptrs: 32 bytes (including overhead) - + Even a request for zero bytes (i.e., malloc(0)) returns a pointer to something of the minimum allocatable size. The maximum overhead wastage (i.e., number of extra bytes @@ -108,7 +108,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); are serviced via mmap(), where the worst case wastage is about 32 bytes plus the remainder from a system page (the minimal mmap unit); typically 4096 or 8192 bytes. - + Security: static-safe; optionally more or less The "security" of malloc refers to the ability of malicious code to accentuate the effects of errors (for example, freeing @@ -121,7 +121,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); for malloc itself is not corrupted by some other means. This is only one aspect of security -- these checks do not, and cannot, detect all possible programming errors. - + If FOOTERS is defined nonzero, then each allocated chunk carries an additional check word to verify that it was malloced from its space. These check words are the same within each @@ -132,7 +132,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); writes to statics that are always on. This may further improve security at the expense of time and space overhead. (Note that FOOTERS may also be worth using with MSPACES.) - + By default detected errors cause the program to abort (calling "abort()"). You can override this to instead proceed past errors by defining PROCEED_ON_ERROR. In this case, a bad free @@ -142,19 +142,19 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); be appropriate for programs that should continue if at all possible in the face of programming errors, although they may run out of memory because dropped memory is never reclaimed. - + If you don't like either of these options, you can define CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything else. And if if you are sure that your program using malloc has no errors or vulnerabilities, you can define INSECURE to 1, which might (or might not) provide a small performance improvement. - + It is also possible to limit the maximum total allocatable space, using malloc_set_footprint_limit. This is not designed as a security feature in itself (calls to set limits are not screened or privileged), but may be useful as one aspect of a secure implementation. - + Thread-safety: NOT thread-safe unless USE_LOCKS defined non-zero When USE_LOCKS is defined, each public call to malloc, free, etc is surrounded with a lock. By default, this uses a plain @@ -171,7 +171,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); (http://www.nedprod.com/programs/portable/nedmalloc/) or ptmalloc (See http://www.malloc.de), which are derived from versions of this malloc. - + System requirements: Any combination of MORECORE and/or MMAP/MUNMAP This malloc can use unix sbrk or any emulation (invoked using the CALL_MORECORE macro) and/or mmap/munmap or any emulation @@ -180,19 +180,19 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); MORECORE and MMAP are enabled. On Win32, it uses emulations based on VirtualAlloc. It also uses common C library functions like memset. - + Compliance: I believe it is compliant with the Single Unix Specification (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably others as well. - + * Overview of algorithms - + This is not the fastest, most space-conserving, most portable, or most tunable malloc ever written. However it is among the fastest while also being among the most space-conserving, portable and tunable. Consistent balance across these factors results in a good general-purpose allocator for malloc-intensive programs. - + In most ways, this malloc is a best-fit allocator. Generally, it chooses the best-fitting existing chunk for a request, with ties broken in approximately least-recently-used order. (This strategy @@ -205,7 +205,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); (>= 256Kb by default), it relies on system memory mapping facilities, if supported. (This helps avoid carrying around and possibly fragmenting memory used only for large chunks.) - + All operations (except malloc_stats and mallinfo) have execution times that are bounded by a constant factor of the number of bits in a size_t, not counting any clearing in calloc or copying in realloc, @@ -216,20 +216,20 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); NO_SEGMENT_TRAVERSAL, which assures bounded execution even when system allocators return non-contiguous spaces, at the typical expense of carrying around more memory and increased fragmentation. - + The implementation is not very modular and seriously overuses macros. Perhaps someday all C compilers will do as good a job inlining modular code as can now be done by brute-force expansion, but now, enough of them seem not to. - + Some compilers issue a lot of warnings about code that is dead/unreachable only on some platforms, and also about intentional uses of negation on unsigned types. All known cases of each can be ignored. - + For a longer but out of date high-level description, see http://gee.cs.oswego.edu/dl/html/malloc.html - + * MSPACES If MSPACES is defined, then in addition to malloc, free, etc., this file also defines mspace_malloc, mspace_free, etc. These @@ -241,10 +241,10 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); ONLY_MSPACES and then do something like... static mspace mymspace = create_mspace(0,0); // for example #define mymalloc(bytes) mspace_malloc(mymspace, bytes) - + (Note: If you only need one instance of an mspace, you can instead use "USE_DL_PREFIX" to relabel the global malloc.) - + You can similarly create thread-local allocators by storing mspaces as thread-locals. For example: static __thread mspace tlms = 0; @@ -253,21 +253,21 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); return mspace_malloc(tlms, bytes); } void tlfree(void* mem) { mspace_free(tlms, mem); } - + Unless FOOTERS is defined, each mspace is completely independent. You cannot allocate from one and free to another (although conformance is only weakly checked, so usage errors are not always caught). If FOOTERS is defined, then each chunk carries around a tag indicating its originating mspace, and frees are directed to their originating spaces. Normally, this requires use of locks. - + ------------------------- Compile-time options --------------------------- - + Be careful in setting #define values for numerical constants of type size_t. On some systems, literal values are not automatically extended to size_t precision unless they are explicitly casted. You can also use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below. - + WIN32 default: defined if _WIN32 defined Defining WIN32 sets up defaults for MS environment and compilers. Otherwise defaults are for unix. Beware that there seem to be some @@ -283,28 +283,28 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); recompile this malloc with a larger DEFAULT_GRANULARITY. Note: in cases where MSC and gcc (cygwin) are known to differ on WIN32, conditions use _MSC_VER to distinguish them. - + DLMALLOC_EXPORT default: extern Defines how public APIs are declared. If you want to export via a Windows DLL, you might define this as #define DLMALLOC_EXPORT extern __declspec(dllexport) If you want a POSIX ELF shared object, you might use #define DLMALLOC_EXPORT extern __attribute__((visibility("default"))) - + MALLOC_ALIGNMENT default: (size_t)(2 * sizeof(void *)) Controls the minimum alignment for malloc'ed chunks. It must be a power of two and at least 8, even on machines for which smaller alignments would suffice. It may be defined as larger than this though. Note however that code and data structures are optimized for the case of 8-byte alignment. - + MSPACES default: 0 (false) If true, compile in support for independent allocation spaces. This is only supported if HAVE_MMAP is true. - + ONLY_MSPACES default: 0 (false) If true, only compile in mspace versions, not regular versions. - + USE_LOCKS default: 0 (false) Causes each call to each public routine to be surrounded with pthread or WIN32 mutex lock/unlock. (If set true, this can be @@ -312,43 +312,43 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); non-zero value other than 1, locks are used, but their implementation is left out, so lock functions must be supplied manually, as described below. - + USE_SPIN_LOCKS default: 1 iff USE_LOCKS and spin locks available If true, uses custom spin locks for locking. This is currently supported only gcc >= 4.1, older gccs on x86 platforms, and recent MS compilers. Otherwise, posix locks or win32 critical sections are used. - + USE_RECURSIVE_LOCKS default: not defined If defined nonzero, uses recursive (aka reentrant) locks, otherwise uses plain mutexes. This is not required for malloc proper, but may be needed for layered allocators such as nedmalloc. - + LOCK_AT_FORK default: not defined If defined nonzero, performs pthread_atfork upon initialization to initialize child lock while holding parent lock. The implementation assumes that pthread locks (not custom locks) are being used. In other cases, you may need to customize the implementation. - + FOOTERS default: 0 If true, provide extra checking and dispatching by placing information in the footers of allocated chunks. This adds space and time overhead. - + INSECURE default: 0 If true, omit checks for usage errors and heap space overwrites. - + USE_DL_PREFIX default: NOT defined Causes compiler to prefix all public routines with the string 'dl'. This can be useful when you only want to use this malloc in one part of a program, using your regular system malloc elsewhere. - + MALLOC_INSPECT_ALL default: NOT defined If defined, compiles malloc_inspect_all and mspace_inspect_all, that perform traversal of all heap space. Unless access to these functions is otherwise restricted, you probably do not want to include them in secure implementations. - + ABORT default: defined as abort() Defines how to abort on failed checks. On most systems, a failed check cannot die with an "assert" or even print an informative @@ -359,7 +359,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); addresses etc) rather than malloc-triggered checks, so will also abort. Also, most compilers know that abort() does not return, so can better optimize code conditionally calling it. - + PROCEED_ON_ERROR default: defined as 0 (false) Controls whether detected bad addresses cause them to bypassed rather than aborting. If set, detected bad arguments to free and @@ -370,7 +370,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); static variable malloc_corruption_error_count is compiled in and can be examined to see if errors have occurred. This option generates slower code than the default abort policy. - + DEBUG default: NOT defined The DEBUG setting is mainly intended for people trying to modify this code or diagnose problems when porting to new platforms. @@ -381,21 +381,21 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); execution noticeably. Calling malloc_stats or mallinfo with DEBUG set will attempt to check every non-mmapped allocated and free chunk in the course of computing the summaries. - + ABORT_ON_ASSERT_FAILURE default: defined as 1 (true) Debugging assertion failures can be nearly impossible if your version of the assert macro causes malloc to be called, which will lead to a cascade of further failures, blowing the runtime stack. ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(), which will usually make debugging easier. - + MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32 The action to take before "return 0" when malloc fails to be able to return memory because there is none available. - + HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES True if this system supports sbrk or an emulation of it. - + MORECORE default: sbrk The name of the sbrk-style system routine to call to obtain more memory. See below for guidance on writing custom MORECORE @@ -406,7 +406,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); though. Internally, we only call it with arguments less than half the max value of a size_t, which should work across all reasonable possibilities, although sometimes generating compiler warnings. - + MORECORE_CONTIGUOUS default: 1 (true) if HAVE_MORECORE If true, take advantage of fact that consecutive calls to MORECORE with positive arguments always return contiguous increasing @@ -414,19 +414,19 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); set it true anyway, since malloc copes with non-contiguities. Setting it false when definitely non-contiguous saves time and possibly wasted space it would take to discover this though. - + MORECORE_CANNOT_TRIM default: NOT defined True if MORECORE cannot release space back to the system when given negative arguments. This is generally necessary only if you are using a hand-crafted MORECORE function that cannot handle negative arguments. - + NO_SEGMENT_TRAVERSAL default: 0 If non-zero, suppresses traversals of memory segments returned by either MORECORE or CALL_MMAP. This disables merging of segments that are contiguous, and selectively releasing them to the OS if unused, but bounds execution times. - + HAVE_MMAP default: 1 (true) True if this system supports mmap or an emulation of it. If so, and HAVE_MORECORE is not true, MMAP is used for all system @@ -436,15 +436,15 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); space from system. Note: A single call to MUNMAP is assumed to be able to unmap memory that may have be allocated using multiple calls to MMAP, so long as they are adjacent. - + HAVE_MREMAP default: 1 on linux, else 0 If true realloc() uses mremap() to re-allocate large blocks and extend or shrink allocation spaces. - + MMAP_CLEARS default: 1 except on WINCE. True if mmap clears memory so calloc doesn't need to. This is true for standard unix mmap using /dev/zero and on WIN32 except for WINCE. - + USE_BUILTIN_FFS default: 0 (i.e., not used) Causes malloc to use the builtin ffs() function to compute indices. Some compilers may recognize and intrinsify ffs to be faster than the @@ -452,44 +452,44 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); to an asm instruction, so is already as fast as it can be, and so this setting has no effect. Similarly for Win32 under recent MS compilers. (On most x86s, the asm version is only slightly faster than the C version.) - + malloc_getpagesize default: derive from system includes, or 4096. The system page size. To the extent possible, this malloc manages memory from the system in page-size units. This may be (and usually is) a function rather than a constant. This is ignored if WIN32, where page size is determined using getSystemInfo during initialization. - + USE_DEV_RANDOM default: 0 (i.e., not used) Causes malloc to use /dev/random to initialize secure magic seed for stamping footers. Otherwise, the current time is used. - + NO_MALLINFO default: 0 If defined, don't compile "mallinfo". This can be a simple way of dealing with mismatches between system declarations and those in this file. - + MALLINFO_FIELD_TYPE default: size_t The type of the fields in the mallinfo struct. This was originally defined as "int" in SVID etc, but is more usefully defined as size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set - + NO_MALLOC_STATS default: 0 If defined, don't compile "malloc_stats". This avoids calls to fprintf and bringing in stdio dependencies you might not want. - + REALLOC_ZERO_BYTES_FREES default: not defined This should be set if a call to realloc with zero bytes should be the same as a call to free. Some people think it should. Otherwise, since this malloc returns a unique pointer for malloc(0), so does realloc(p, 0). - + LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H LACKS_STDLIB_H LACKS_SCHED_H LACKS_TIME_H default: NOT defined unless on WIN32 Define these if your system does not have these header files. You might need to manually insert some of the declarations they provide. - + DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS, system_info.dwAllocationGranularity in WIN32, otherwise 64K. @@ -504,7 +504,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); to either page size or win32 region size. (Note: In previous versions of malloc, the equivalent of this option was called "TOP_PAD") - + DEFAULT_TRIM_THRESHOLD default: 2MB Also settable using mallopt(M_TRIM_THRESHOLD, x) The maximum amount of unused top-most memory to keep before @@ -527,7 +527,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); program startup, in an attempt to reserve system memory, doesn't have the intended effect under automatic trimming, since that memory will immediately be returned to the system. - + DEFAULT_MMAP_THRESHOLD default: 256K Also settable using mallopt(M_MMAP_THRESHOLD, x) The request size threshold for using MMAP to directly service a @@ -551,7 +551,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); value of "large" may vary across systems. The default is an empirically derived value that works well in most systems. You can disable mmap by setting to MAX_SIZE_T. - + MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP The number of consolidated frees between checks to release unused segments when freeing. When using non-contiguous segments, @@ -792,7 +792,7 @@ defined(__i386__) || defined(__x86_64__))) || \ are not even meaningful in this version of malloc. These fields are are instead filled by mallinfo() with other numbers that might be of interest. - + HAVE_USR_INCLUDE_MALLOC_H should be set if you have a /usr/include/malloc.h file that includes a declaration of struct mallinfo. If so, it is included; else a compliant version is @@ -860,11 +860,11 @@ extern "C" { #ifndef FORCEINLINE #define FORCEINLINE #endif - + #if !ONLY_MSPACES - + /* ------------------- Declarations of public routines ------------------- */ - + #ifndef USE_DL_PREFIX // XXX Emscripten XXX #if defined(__EMSCRIPTEN__) @@ -902,13 +902,13 @@ void** independent_comalloc(size_t, size_t*, void**) __attribute__((weak, alias( size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_free"))); #endif /*__EMSCRIPTEN__*/ #endif /* USE_DL_PREFIX */ - + /* malloc(size_t n) Returns a pointer to a newly allocated chunk of at least n bytes, or null if no space is available, in which case errno is set to ENOMEM on ANSI C systems. - + If n is zero, malloc returns a minimum-sized chunk. (The minimum size is 16 bytes on most 32bit systems, and 32 bytes on 64bit systems.) Note that size_t is an unsigned type, so calls with @@ -918,7 +918,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f cases less than the maximum representable value of a size_t. */ DLMALLOC_EXPORT void* dlmalloc(size_t); - + /* free(void* p) Releases the chunk of memory pointed to by p, that had been previously @@ -927,38 +927,38 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f freed, free(p) will by default cause the current program to abort. */ DLMALLOC_EXPORT void dlfree(void*); - + /* calloc(size_t n_elements, size_t element_size); Returns a pointer to n_elements * element_size bytes, with all locations set to zero. */ DLMALLOC_EXPORT void* dlcalloc(size_t, size_t); - + /* realloc(void* p, size_t n) Returns a pointer to a chunk of size n that contains the same data as does chunk p up to the minimum of (n, p's size) bytes, or null if no space is available. - + The returned pointer may or may not be the same as p. The algorithm prefers extending p in most cases when possible, otherwise it employs the equivalent of a malloc-copy-free sequence. - + If p is null, realloc is equivalent to malloc. - + If space is not available, realloc returns null, errno is set (if on ANSI) and p is NOT freed. - + if n is for fewer bytes than already held by p, the newly unused space is lopped off and freed if possible. realloc with a size argument of zero (re)allocates a minimum-sized chunk. - + The old unix realloc convention of allowing the last-free'd chunk to be used as an argument to realloc is not supported. */ DLMALLOC_EXPORT void* dlrealloc(void*, size_t); - + /* realloc_in_place(void* p, size_t n) Resizes the space allocated for p to size n, only if this can be @@ -969,25 +969,25 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f to expand space; for example, reallocation of a buffer that must be memory-aligned or cleared. You can use realloc_in_place to trigger these alternatives only when needed. - + Returns p if successful; otherwise null. */ DLMALLOC_EXPORT void* dlrealloc_in_place(void*, size_t); - + /* memalign(size_t alignment, size_t n); Returns a pointer to a newly allocated chunk of n bytes, aligned in accord with the alignment argument. - + The alignment argument should be a power of two. If the argument is not a power of two, the nearest greater power is used. 8-byte alignment is guaranteed by normal malloc calls, so don't bother calling memalign with an argument of 8 or less. - + Overreliance on memalign is a sure way to fragment space. */ DLMALLOC_EXPORT void* dlmemalign(size_t, size_t); - + /* int posix_memalign(void** pp, size_t alignment, size_t n); Allocates a chunk of n bytes, aligned in accord with the alignment @@ -997,14 +997,14 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f returns ENOMEM if memory cannot be allocated. */ DLMALLOC_EXPORT int dlposix_memalign(void**, size_t, size_t); - + /* valloc(size_t n); Equivalent to memalign(pagesize, n), where pagesize is the page size of the system. If the pagesize is unknown, 4096 is used. */ DLMALLOC_EXPORT void* dlvalloc(size_t); - + /* mallopt(int parameter_number, int parameter_value) Sets tunable parameters The format is to provide a @@ -1014,21 +1014,21 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f 0. To workaround the fact that mallopt is specified to use int, not size_t parameters, the value -1 is specially treated as the maximum unsigned size_t value. - + SVID/XPG/ANSI defines four standard param numbers for mallopt, normally defined in malloc.h. None of these are use in this malloc, so setting them has no effect. But this malloc also supports other options in mallopt. See below for details. Briefly, supported parameters are as follows (listed defaults are for "typical" configurations). - + Symbol param # default allowed param values M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables) M_GRANULARITY -2 page size any power of 2 >= page size M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) */ DLMALLOC_EXPORT int dlmallopt(int, int); - + /* malloc_footprint(); Returns the number of bytes obtained from the system. The total @@ -1039,7 +1039,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f so results might not be up to date. */ DLMALLOC_EXPORT size_t dlmalloc_footprint(void); - + /* malloc_max_footprint(); Returns the maximum number of bytes obtained from the system. This @@ -1052,7 +1052,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f not be up to date. */ DLMALLOC_EXPORT size_t dlmalloc_max_footprint(void); - + /* malloc_footprint_limit(); Returns the number of bytes that the heap is allowed to obtain from @@ -1063,7 +1063,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f the system. */ DLMALLOC_EXPORT size_t dlmalloc_footprint_limit(); - + /* malloc_set_footprint_limit(); Sets the maximum number of bytes to obtain from the system, causing @@ -1077,7 +1077,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f retroactively deallocate existing used memory. */ DLMALLOC_EXPORT size_t dlmalloc_set_footprint_limit(size_t bytes); - + #if MALLOC_INSPECT_ALL /* malloc_inspect_all(void(*handler)(void *start, @@ -1095,7 +1095,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f invoked with the given callback argument. If locks are defined, they are held during the entire traversal. It is a bad idea to invoke other malloc functions from within the handler. - + For example, to count the number of in-use chunks with size greater than 1000, you could write: static int count = 0; @@ -1104,19 +1104,19 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f } then: malloc_inspect_all(count_chunks, NULL); - + malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined. */ DLMALLOC_EXPORT void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*), void* arg); - + #endif /* MALLOC_INSPECT_ALL */ - + #if !NO_MALLINFO /* mallinfo() Returns (by copy) a struct containing various summary statistics: - + arena: current total non-mmapped bytes allocated from system ordblks: the number of free chunks smblks: always zero. @@ -1130,17 +1130,17 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f keepcost: the maximum number of bytes that could ideally be released back to system via malloc_trim. ("ideally" means that it ignores page restrictions etc.) - + Because these fields are ints, but internal bookkeeping may be kept as longs, the reported values may wrap around zero and thus be inaccurate. */ DLMALLOC_EXPORT struct mallinfo dlmallinfo(void); #endif /* NO_MALLINFO */ - + /* independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); - + independent_calloc is similar to calloc, but instead of returning a single cleared space, it returns an array of pointers to n_elements independent elements that can hold contents of size elem_size, each @@ -1149,30 +1149,30 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f allocated (this is not guaranteed to occur with multiple callocs or mallocs), which may also improve cache locality in some applications. - + The "chunks" argument is optional (i.e., may be null, which is probably the most typical usage). If it is null, the returned array is itself dynamically allocated and should also be freed when it is no longer needed. Otherwise, the chunks array must be of at least n_elements in length. It is filled in with the pointers to the chunks. - + In either case, independent_calloc returns this pointer array, or null if the allocation failed. If n_elements is zero and "chunks" is null, it returns a chunk representing an array with zero elements (which should be freed if not wanted). - + Each element must be freed when it is no longer needed. This can be done all at once using bulk_free. - + independent_calloc simplifies and speeds up implementations of many kinds of pools. It may also be useful when constructing large data structures that initially have a fixed number of fixed-sized nodes, but the number is not known at compile time, and some of the nodes may later need to be freed. For example: - + struct Node { int item; struct Node* next; }; - + struct Node* build_list() { struct Node** pool; int n = read_number_of_nodes_needed(); @@ -1188,10 +1188,10 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f } */ DLMALLOC_EXPORT void** dlindependent_calloc(size_t, size_t, void**); - + /* independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); - + independent_comalloc allocates, all at once, a set of n_elements chunks with sizes indicated in the "sizes" array. It returns an array of pointers to these elements, each of which can be @@ -1199,32 +1199,32 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f be adjacently allocated (this is not guaranteed to occur with multiple callocs or mallocs), which may also improve cache locality in some applications. - + The "chunks" argument is optional (i.e., may be null). If it is null the returned array is itself dynamically allocated and should also be freed when it is no longer needed. Otherwise, the chunks array must be of at least n_elements in length. It is filled in with the pointers to the chunks. - + In either case, independent_comalloc returns this pointer array, or null if the allocation failed. If n_elements is zero and chunks is null, it returns a chunk representing an array with zero elements (which should be freed if not wanted). - + Each element must be freed when it is no longer needed. This can be done all at once using bulk_free. - + independent_comallac differs from independent_calloc in that each element may have a different size, and also that it does not automatically clear elements. - + independent_comalloc can be used to speed up allocation in cases where several structs or objects must always be allocated at the same time. For example: - + struct Head { ... } struct Foot { ... } - + void send_message(char* msg) { int msglen = strlen(msg); size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; @@ -1236,17 +1236,17 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f struct Foot* foot = (struct Foot*)(chunks[2]); // ... } - + In general though, independent_comalloc is worth using only for larger values of n_elements. For small values, you probably won't detect enough difference from series of malloc calls to bother. - + Overuse of independent_comalloc can increase overall memory usage, since it cannot reuse existing noncontiguous small chunks that might be available for some of the elements. */ DLMALLOC_EXPORT void** dlindependent_comalloc(size_t, size_t*, void**); - + /* bulk_free(void* array[], size_t n_elements) Frees and clears (sets to null) each non-null pointer in the given @@ -1257,17 +1257,17 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f may be worthwhile to sort this array before calling bulk_free. */ DLMALLOC_EXPORT size_t dlbulk_free(void**, size_t n_elements); - + /* pvalloc(size_t n); Equivalent to valloc(minimum-page-that-holds(n)), that is, round up n to nearest pagesize. */ DLMALLOC_EXPORT void* dlpvalloc(size_t); - + /* malloc_trim(size_t pad); - + If possible, gives memory back to the system (via negative arguments to sbrk) if there is unused memory at the `high' end of the malloc pool or in unused MMAP segments. You can call this after freeing @@ -1276,18 +1276,18 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f memory. Under some allocation patterns, some large free blocks of memory will be locked between two used chunks, so they cannot be given back to the system. - + The `pad' argument to malloc_trim represents the amount of free trailing space to leave untrimmed. If this argument is zero, only the minimum amount of memory to maintain internal data structures will be left. Non-zero arguments can be supplied to maintain enough trailing space to service future expected allocations without having to re-obtain memory from the system. - + Malloc_trim returns 1 if it actually released any memory, else 0. */ DLMALLOC_EXPORT int dlmalloc_trim(size_t); - + /* malloc_stats(); Prints on stderr the amount of space obtained from the system (both @@ -1299,19 +1299,19 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f because of alignment and bookkeeping overhead. Because it includes alignment wastage as being in use, this figure may be greater than zero even when no user-level chunks are allocated. - + The reported current and maximum system memory can be inaccurate if a program makes other calls to system memory allocation functions (normally sbrk) outside of malloc. - + malloc_stats prints only the most commonly interesting statistics. More information can be obtained by calling mallinfo. */ DLMALLOC_EXPORT void dlmalloc_stats(void); - + /* malloc_usable_size(void* p); - + Returns the number of bytes you can actually use in an allocated chunk, which may be more than you requested (although often not) due to alignment and minimum size constraints. @@ -1319,23 +1319,23 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f overwriting other allocated objects. This is not a particularly great programming practice. malloc_usable_size can be more useful in debugging and assertions, for example: - + p = malloc(n); assert(malloc_usable_size(p) >= 256); */ /* XXX EMSCRIPTEN: mark for export (and therefore weak) */ DLMALLOC_EXPORT size_t dlmalloc_usable_size(void*); - + #endif /* ONLY_MSPACES */ - + #if MSPACES - + /* mspace is an opaque type representing an independent region of space that supports mspace_malloc, etc. */ typedef void* mspace; - + /* create_mspace creates and returns a new independent space with the given initial capacity, or, if 0, the default granularity size. It @@ -1348,7 +1348,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f setting with mallopt(M_GRANULARITY, value). */ DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked); - + /* destroy_mspace destroys the given space, and attempts to return all of its memory back to the system, returning the total number of @@ -1356,7 +1356,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f used by the space become undefined. */ DLMALLOC_EXPORT size_t destroy_mspace(mspace msp); - + /* create_mspace_with_base uses the memory supplied as the initial base of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this @@ -1367,7 +1367,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f space (if possible) but not the initial base. */ DLMALLOC_EXPORT mspace create_mspace_with_base(void* base, size_t capacity, int locked); - + /* mspace_track_large_chunks controls whether requests for large chunks are allocated in their own untracked mmapped regions, separate from @@ -1380,74 +1380,74 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f setting. */ DLMALLOC_EXPORT int mspace_track_large_chunks(mspace msp, int enable); - - + + /* mspace_malloc behaves as malloc, but operates within the given space. */ DLMALLOC_EXPORT void* mspace_malloc(mspace msp, size_t bytes); - + /* mspace_free behaves as free, but operates within the given space. - + If compiled with FOOTERS==1, mspace_free is not actually needed. free may be called instead of mspace_free because freed chunks from any space are handled by their originating spaces. */ DLMALLOC_EXPORT void mspace_free(mspace msp, void* mem); - + /* mspace_realloc behaves as realloc, but operates within the given space. - + If compiled with FOOTERS==1, mspace_realloc is not actually needed. realloc may be called instead of mspace_realloc because realloced chunks from any space are handled by their originating spaces. */ DLMALLOC_EXPORT void* mspace_realloc(mspace msp, void* mem, size_t newsize); - + /* mspace_calloc behaves as calloc, but operates within the given space. */ DLMALLOC_EXPORT void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); - + /* mspace_memalign behaves as memalign, but operates within the given space. */ DLMALLOC_EXPORT void* mspace_memalign(mspace msp, size_t alignment, size_t bytes); - + /* mspace_independent_calloc behaves as independent_calloc, but operates within the given space. */ DLMALLOC_EXPORT void** mspace_independent_calloc(mspace msp, size_t n_elements, size_t elem_size, void* chunks[]); - + /* mspace_independent_comalloc behaves as independent_comalloc, but operates within the given space. */ DLMALLOC_EXPORT void** mspace_independent_comalloc(mspace msp, size_t n_elements, size_t sizes[], void* chunks[]); - + /* mspace_footprint() returns the number of bytes obtained from the system for this space. */ DLMALLOC_EXPORT size_t mspace_footprint(mspace msp); - + /* mspace_max_footprint() returns the peak number of bytes obtained from the system for this space. */ DLMALLOC_EXPORT size_t mspace_max_footprint(mspace msp); - - + + #if !NO_MALLINFO /* mspace_mallinfo behaves as mallinfo, but reports properties of @@ -1455,31 +1455,31 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f */ DLMALLOC_EXPORT struct mallinfo mspace_mallinfo(mspace msp); #endif /* NO_MALLINFO */ - + /* malloc_usable_size(void* p) behaves the same as malloc_usable_size; */ DLMALLOC_EXPORT size_t mspace_usable_size(const void* mem); - + /* mspace_malloc_stats behaves as malloc_stats, but reports properties of the given space. */ DLMALLOC_EXPORT void mspace_malloc_stats(mspace msp); - + /* mspace_trim behaves as malloc_trim, but operates within the given space. */ DLMALLOC_EXPORT int mspace_trim(mspace msp, size_t pad); - + /* An alias for mallopt. */ DLMALLOC_EXPORT int mspace_mallopt(int, int); - + #endif /* MSPACES */ - + #ifdef __cplusplus } /* end of extern "C" */ #endif /* __cplusplus */ @@ -1678,7 +1678,12 @@ extern size_t getpagesize(); #define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1) #define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2) #define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES) +#if __EMSCRIPTEN__ +/* Emscripten's sbrk can interpret unsigned values greater than (MAX_SIZE_T / 2U) (2GB) correctly */ +#define HALF_MAX_SIZE_T (MAX_SIZE_T) +#else #define HALF_MAX_SIZE_T (MAX_SIZE_T / 2U) +#endif /* The bit mask value corresponding to MALLOC_ALIGNMENT */ #define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE) @@ -1845,7 +1850,7 @@ static FORCEINLINE int win32munmap(void* ptr, size_t size) { /* When locks are defined, there is one global lock, plus one per-mspace lock. - + The global lock_ensures that mparams.magic and other unique mparams values are initialized only once. It also protects sequences of calls to MORECORE. In many cases sys_alloc requires @@ -1853,21 +1858,21 @@ static FORCEINLINE int win32munmap(void* ptr, size_t size) { threads. This does not protect against direct calls to MORECORE by other threads not using this lock, so there is still code to cope the best we can on interference. - + Per-mspace locks surround calls to malloc, free, etc. By default, locks are simple non-reentrant mutexes. - + Because lock-protected regions generally have bounded times, it is OK to use the supplied simple spinlocks. Spinlocks are likely to improve performance for lightly contended applications, but worsen performance under heavy contention. - + If USE_LOCKS is > 1, the definitions of lock routines here are bypassed, in which case you will need to define the type MLOCK_T, and at least INITIAL_LOCK, DESTROY_LOCK, ACQUIRE_LOCK, RELEASE_LOCK and TRY_LOCK. You must also declare a static MLOCK_T malloc_global_mutex = { initialization values };. - + */ #if !USE_LOCKS @@ -2118,11 +2123,11 @@ static int pthread_init_lock (MLOCK_T *lk) { /* (The following includes lightly edited explanations by Colin Plumb.) - + The malloc_chunk declaration below is misleading (but accurate and necessary). It declares a "view" into memory allowing access to necessary fields at known offsets from a given base. - + Chunks of memory are maintained using a `boundary tag' method as originally described by Knuth. (See the paper by Paul Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a survey of such @@ -2130,14 +2135,14 @@ static int pthread_init_lock (MLOCK_T *lk) { each chunk and at the end. This makes consolidating fragmented chunks into bigger chunks fast. The head fields also hold bits representing whether chunks are free or in use. - + Here are some pictures to make it clearer. They are "exploded" to show that the state of a chunk can be thought of as extending from the high 31 bits of the head field of its header through the prev_foot and PINUSE_BIT bit of the following chunk header. - + A chunk that's in use looks like: - + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Size of previous chunk (if P = 0) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -2157,9 +2162,9 @@ static int pthread_init_lock (MLOCK_T *lk) { +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1| | Size of next chunk (may or may not be in use) | +-+ mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - + And if it's free, it looks like this: - + chunk-> +- -+ | User payload (must be in use, or we would have merged!) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -2187,16 +2192,16 @@ static int pthread_init_lock (MLOCK_T *lk) { +-+ Note that since we always merge adjacent free chunks, the chunks adjacent to a free chunk must be in use. - + Given a pointer to a chunk (which can be derived trivially from the payload pointer) we can, in O(1) time, find out whether the adjacent chunks are free, and if so, unlink them from the lists that they are on and merge them with the current chunk. - + Chunks always begin on even word boundaries, so the mem portion (which is returned to the user) is also on an even word boundary, and thus at least double-word aligned. - + The P (PINUSE_BIT) bit, stored in the unused low-order bit of the chunk size (which is always a multiple of two words), is an in-use bit for the *previous* chunk. If that bit is *clear*, then the @@ -2207,13 +2212,13 @@ static int pthread_init_lock (MLOCK_T *lk) { any given chunk, then you CANNOT determine the size of the previous chunk, and might even get a memory addressing fault when trying to do so. - + The C (CINUSE_BIT) bit, stored in the unused second-lowest bit of the chunk size redundantly records whether the current chunk is inuse (unless the chunk is mmapped). This redundancy enables usage checks within free and realloc, and reduces indirection when freeing and consolidating chunks. - + Each freshly allocated chunk must have both cinuse and pinuse set. That is, each allocated chunk borders either a previously allocated and still in-use chunk, or the base of its memory arena. This is @@ -2221,14 +2226,14 @@ static int pthread_init_lock (MLOCK_T *lk) { found chunk. Further, no free chunk physically borders another one, so each free chunk is known to be preceded and followed by either inuse chunks or the ends of memory. - + Note that the `foot' of the current chunk is actually represented as the prev_foot of the NEXT chunk. This makes it easier to deal with alignments etc but can be very confusing when trying to extend or adapt this code. - + The exceptions to all this are - + 1. The special chunk `top' is the top-most available chunk (i.e., the one bordering the end of available memory). It is treated specially. Top is never included in any bin, is used only if @@ -2240,7 +2245,7 @@ static int pthread_init_lock (MLOCK_T *lk) { contiguous chunk that would have to index off it. However, space is still allocated for it (TOP_FOOT_SIZE) to enable separation or merging when space is extended. - + 3. Chunks allocated via mmap, have both cinuse and pinuse bits cleared in their head fields. Because they are allocated one-by-one, each must carry its own prev_foot field, which is @@ -2248,7 +2253,7 @@ static int pthread_init_lock (MLOCK_T *lk) { region, which is needed to preserve alignment. Each mmapped chunk is trailed by the first two fields of a fake next-chunk for sake of usage checks. - + */ struct malloc_chunk { @@ -2309,7 +2314,7 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ The head field of a chunk is or'ed with PINUSE_BIT when previous adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in use, unless mmapped, in which case both bits are cleared. - + FLAG4_BIT is not used by this malloc, but might be useful in extensions. */ @@ -2374,10 +2379,10 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ /* When chunks are not in use, they are treated as nodes of either lists or trees. - + "Small" chunks are stored in circular doubly-linked lists, and look like this: - + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Size of previous chunk | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -2393,12 +2398,12 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ `foot:' | Size of chunk, in bytes | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - + Larger chunks are kept in a form of bitwise digital trees (aka tries) keyed on chunksizes. Because malloc_tree_chunks are only for free chunks greater than 256 bytes, their size doesn't impose any constraints on user chunk sizes. Each node looks like: - + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Size of previous chunk | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -2421,7 +2426,7 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ `foot:' | Size of chunk, in bytes | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - + Each tree holding treenodes is a tree of unique chunk sizes. Chunks of the same size are arranged in a circularly-linked list, with only the oldest chunk (the next to be used, in our FIFO ordering) @@ -2429,14 +2434,14 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ parent pointer.) If a chunk with the same size an an existing node is inserted, it is linked off the existing node using pointers that work in the same way as fd/bk pointers of small chunks. - + Each tree contains a power of 2 sized range of chunk sizes (the smallest is 0x100 <= x < 0x180), which is is divided in half at each tree level, with the chunks in the smaller half of the range (0x100 <= x < 0x140 for the top nose) in the left subtree and the larger half (0x140 <= x < 0x180) in the right subtree. This is, of course, done by inspecting individual bits. - + Using these rules, each node's left subtree contains all smaller sizes than its right subtree. However, the node at the root of each subtree has no particular ordering relationship to either. (The @@ -2444,7 +2449,7 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ If we remove the last chunk of a given size from the interior of the tree, we need to replace it with a leaf node. The tree ordering rules permit a node to be replaced by any leaf below it. - + The smallest chunk in a tree (a common operation in a best-fit allocator) can be found by walking a path to the leftmost leaf in the tree. Unlike a usual binary tree, where we follow left child @@ -2452,7 +2457,7 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ pointer any time the left one is null, until we reach a leaf with both child pointers null. The smallest chunk in the tree will be somewhere along that path. - + The worst case number of steps to add, find, or remove a node is bounded by the number of bits differentiating chunks within bins. Under current bin calculations, this ranges from 6 up to 21 @@ -2466,7 +2471,7 @@ struct malloc_tree_chunk { size_t head; struct malloc_tree_chunk* fd; struct malloc_tree_chunk* bk; - + struct malloc_tree_chunk* child[2]; struct malloc_tree_chunk* parent; bindex_t index; @@ -2488,7 +2493,7 @@ typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */ the space. Large chunks that are directly allocated by mmap are not included in this list. They are instead independently created and destroyed without otherwise keeping track of them. - + Segment management mainly comes into play for spaces allocated by MMAP. Any call to MMAP might or might not return memory that is adjacent to an existing segment. MORECORE normally contiguously @@ -2502,7 +2507,7 @@ typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */ contiguity when we get it. It is probably possible to do better than this on some systems, but no general scheme seems to be significantly better. - + Management entails a simpler variant of the consolidation scheme used for chunks to reduce fragmentation -- new adjacent memory is normally prepended or appended to an existing segment. However, @@ -2510,19 +2515,19 @@ typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */ reflect the fact that segment processing is relatively infrequent (occurring only when getting memory from system) and that we don't expect to have huge numbers of segments: - + * Segments are not indexed, so traversal requires linear scans. (It would be possible to index these, but is not worth the extra overhead and complexity for most programs on most platforms.) * New segments are only appended to old ones when holding top-most memory; if they cannot be prepended to others, they are held in different segments. - + Except for the top-most segment of an mstate, each segment record is kept at the tail of its segment. Segments are added by pushing segment records onto the list headed by &mstate.seg for the containing mstate. - + Segment flags control allocation/merge/deallocation policies: * If EXTERN_BIT set, then we did not allocate this segment, and so should not try to deallocate or merge with others. @@ -2554,7 +2559,7 @@ typedef struct malloc_segment* msegmentptr; /* A malloc_state holds all of the bookkeeping for a space. The main fields are: - + Top The topmost chunk of the currently active segment. Its size is cached in topsize. The actual size of topmost space is @@ -2563,14 +2568,14 @@ typedef struct malloc_segment* msegmentptr; space from the system. The size at which to autotrim top is cached from mparams in trim_check, except that it is disabled if an autotrim fails. - + Designated victim (dv) This is the preferred chunk for servicing small requests that don't have exact fits. It is normally the chunk split off most recently to service another small request. Its size is cached in dvsize. The link fields of this chunk are not maintained since it is not kept in a bin. - + SmallBins An array of bin headers for free chunks. These bins hold chunks with sizes less than MIN_LARGE_SIZE bytes. Each bin contains @@ -2580,13 +2585,13 @@ typedef struct malloc_segment* msegmentptr; itself). This avoids special-casing for headers. But to avoid waste, we allocate only the fd/bk pointers of bins, and then use repositioning tricks to treat these as the fields of a chunk. - + TreeBins Treebins are pointers to the roots of trees holding a range of sizes. There are 2 equally spaced treebins for each power of two from TREE_SHIFT to TREE_SHIFT+16. The last bin holds anything larger. - + Bin maps There is one bit map for small bins ("smallmap") and one for treebins ("treemap). Each bin sets its bit when non-empty, and @@ -2599,38 +2604,38 @@ typedef struct malloc_segment* msegmentptr; supplement at http://hackersdelight.org/). Many of these are intended to reduce the branchiness of paths through malloc etc, as well as to reduce the number of memory locations read or written. - + Segments A list of segments headed by an embedded malloc_segment record representing the initial space. - + Address check support The least_addr field is the least address ever obtained from MORECORE or MMAP. Attempted frees and reallocs of any address less than this are trapped (unless INSECURE is defined). - + Magic tag A cross-check field that should always hold same value as mparams.magic. - + Max allowed footprint The maximum allowed bytes to allocate from system (zero means no limit) - + Flags Bits recording whether to use MMAP, locks, or contiguous MORECORE - + Statistics Each space keeps track of current and maximum system memory obtained via MORECORE or MMAP. - + Trim support Fields holding the amount of unused topmost memory that should trigger trimming, and a counter to force periodic scanning to release unused non-topmost segments. - + Locking If USE_LOCKS is defined, the "mutex" lock is acquired and released around every public call using this mspace. - + Extension support A void* pointer and a size_t field that can be used to help implement extensions to this malloc. @@ -3057,7 +3062,7 @@ I = (bindex_t)(N + Y);\ check all of those linked or offsetted from other embedded data structures. These checks are interspersed with main code in a way that tends to minimize their run-time cost. - + When FOOTERS is defined, in addition to range checking, we also verify footer fields of inuse chunks, which can be used guarantee that the mstate controlling malloc/free is intact. This is a @@ -3171,13 +3176,13 @@ static int init_mparams(void) { if (malloc_global_mutex_status <= 0) init_malloc_global_mutex(); #endif - + ACQUIRE_MALLOC_GLOBAL_LOCK(); if (mparams.magic == 0) { size_t magic; size_t psize; size_t gsize; - + #ifndef WIN32 psize = malloc_getpagesize; gsize = ((DEFAULT_GRANULARITY != 0)? DEFAULT_GRANULARITY : psize); @@ -3190,7 +3195,7 @@ static int init_mparams(void) { DEFAULT_GRANULARITY : system_info.dwAllocationGranularity); } #endif /* WIN32 */ - + /* Sanity-check configuration: size_t must be unsigned and as wide as pointer type. ints must be at least 4 bytes. @@ -3215,7 +3220,7 @@ static int init_mparams(void) { #else /* MORECORE_CONTIGUOUS */ mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT; #endif /* MORECORE_CONTIGUOUS */ - + #if !ONLY_MSPACES /* Set up lock for main malloc area */ gm->mflags = mparams.default_mflags; @@ -3224,7 +3229,7 @@ static int init_mparams(void) { #if LOCK_AT_FORK pthread_atfork(&pre_fork, &post_fork_parent, &post_fork_child); #endif - + { #if USE_DEV_RANDOM int fd; @@ -3250,7 +3255,7 @@ static int init_mparams(void) { (*(volatile size_t *)(&(mparams.magic))) = magic; } } - + RELEASE_MALLOC_GLOBAL_LOCK(); return 1; } @@ -3376,7 +3381,7 @@ static void do_check_tree(mstate m, tchunkptr t) { assert(tsize >= MIN_LARGE_SIZE); assert(tsize >= minsize_for_tree_index(idx)); assert((idx == NTREEBINS-1) || (tsize < minsize_for_tree_index((idx+1)))); - + do { /* traverse through chain of same-sized nodes */ do_check_any_chunk(m, ((mchunkptr)u)); assert(u->index == tindex); @@ -3527,21 +3532,21 @@ static void do_check_malloc_state(mstate m) { do_check_smallbin(m, i); for (i = 0; i < NTREEBINS; ++i) do_check_treebin(m, i); - + if (m->dvsize != 0) { /* check dv chunk */ do_check_any_chunk(m, m->dv); assert(m->dvsize == chunksize(m->dv)); assert(m->dvsize >= MIN_CHUNK_SIZE); assert(bin_find(m, m->dv) == 0); } - + if (m->top != 0) { /* check top chunk */ do_check_top_chunk(m, m->top); /*assert(m->topsize == chunksize(m->top)); redundant */ assert(m->topsize > 0); assert(bin_find(m, m->top) == 0); } - + total = traverse_and_check(m); assert(total <= m->footprint); assert(m->footprint <= m->max_footprint); @@ -3575,7 +3580,7 @@ static struct mallinfo internal_mallinfo(mstate m) { } s = s->next; } - + nm.arena = sum; nm.ordblks = nfree; nm.hblkhd = m->footprint - sum; @@ -3584,7 +3589,7 @@ static struct mallinfo internal_mallinfo(mstate m) { nm.fordblks = mfree; nm.keepcost = m->topsize; } - + POSTACTION(m); } return nm; @@ -3604,7 +3609,7 @@ static void internal_malloc_stats(mstate m) { maxfp = m->max_footprint; fp = m->footprint; used = fp - (m->topsize + TOP_FOOT_SIZE); - + while (s != 0) { mchunkptr q = align_as_chunk(s->base); while (segment_holds(s, q) && @@ -3765,7 +3770,7 @@ break;\ /* Unlink steps: - + 1. If x is a chained node, unlink it from its same-sized fd/bk links and choose its bk node as its replacement. 2. If x was the last node of its size, but not a leaf node, it must @@ -3908,7 +3913,7 @@ static void* mmap_alloc(mstate m, size_t nb) { mark_inuse_foot(m, p, psize); chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0; - + if (m->least_addr == 0 || mm < m->least_addr) m->least_addr = mm; if ((m->footprint += mmsize) > m->max_footprint) @@ -3944,7 +3949,7 @@ static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb, int flags) { mark_inuse_foot(m, newp, psize); chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0; - + if (cp < m->least_addr) m->least_addr = cp; if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint) @@ -3965,7 +3970,7 @@ static void init_top(mstate m, mchunkptr p, size_t psize) { size_t offset = align_offset(chunk2mem(p)); p = (mchunkptr)((char*)p + offset); psize -= offset; - + m->top = p; m->topsize = psize; p->head = psize | PINUSE_BIT; @@ -4012,11 +4017,11 @@ static void* prepend_alloc(mstate m, char* newbase, char* oldbase, mchunkptr q = chunk_plus_offset(p, nb); size_t qsize = psize - nb; set_size_and_pinuse_of_inuse_chunk(m, p, nb); - + assert((char*)oldfirst > (char*)q); assert(pinuse(oldfirst)); assert(qsize >= MIN_CHUNK_SIZE); - + /* consolidate remainder with first chunk of old base */ if (oldfirst == m->top) { size_t tsize = m->topsize += qsize; @@ -4040,7 +4045,7 @@ static void* prepend_alloc(mstate m, char* newbase, char* oldbase, insert_chunk(m, q, qsize); check_free_chunk(m, q); } - + check_malloced_chunk(m, chunk2mem(p), nb); return chunk2mem(p); } @@ -4061,10 +4066,10 @@ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { mchunkptr tnext = chunk_plus_offset(sp, ssize); mchunkptr p = tnext; int nfences = 0; - + /* reset top to new space */ init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); - + /* Set up segment record */ assert(is_aligned(ss)); set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); @@ -4073,7 +4078,7 @@ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { m->seg.size = tsize; m->seg.sflags = mmapped; m->seg.next = ss; - + /* Insert trailing fenceposts */ for (;;) { mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); @@ -4085,7 +4090,7 @@ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { break; } assert(nfences >= 2); - + /* Insert the rest of old top into a bin as an ordinary free chunk */ if (csp != old_top) { mchunkptr q = (mchunkptr)old_top; @@ -4094,7 +4099,7 @@ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { set_free_with_pinuse(q, psize, tn); insert_chunk(m, q, psize); } - + check_top_chunk(m, m->top); } @@ -4106,16 +4111,16 @@ static void* sys_alloc(mstate m, size_t nb) { size_t tsize = 0; flag_t mmap_flag = 0; size_t asize; /* allocation size */ - + ensure_initialization(); - + /* Directly map large chunks, but only if already initialized */ if (use_mmap(m) && nb >= mparams.mmap_threshold && m->topsize != 0) { void* mem = mmap_alloc(m, nb); if (mem != 0) return mem; } - + asize = granularity_align(nb + SYS_ALLOC_PADDING); if (asize <= nb) return 0; /* wraparound */ @@ -4124,7 +4129,7 @@ static void* sys_alloc(mstate m, size_t nb) { if (fp <= m->footprint || fp > m->footprint_limit) return 0; } - + /* Try getting memory in any of three ways (in most-preferred to least-preferred order): @@ -4140,19 +4145,19 @@ static void* sys_alloc(mstate m, size_t nb) { find space. 3. A call to MORECORE that cannot usually contiguously extend memory. (disabled if not HAVE_MORECORE) - + In all cases, we need to request enough bytes from system to ensure we can malloc nb bytes upon success, so pad with enough space for top_foot, plus alignment-pad to make sure we don't lose bytes if not on boundary, and round this up to a granularity unit. */ - + if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) { char* br = CMFAIL; size_t ssize = asize; /* sbrk call size */ msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top); ACQUIRE_MALLOC_GLOBAL_LOCK(); - + if (ss == 0) { /* First time through or recovery */ char* base = (char*)CALL_MORECORE(0); if (base != CMFAIL) { @@ -4180,7 +4185,7 @@ static void* sys_alloc(mstate m, size_t nb) { tsize = ssize; } } - + if (tbase == CMFAIL) { /* Cope with partial failure */ if (br != CMFAIL) { /* Try to use/extend the space we did get */ if (ssize < HALF_MAX_SIZE_T && @@ -4204,10 +4209,10 @@ static void* sys_alloc(mstate m, size_t nb) { else disable_contiguous(m); /* Don't try contiguous path in the future */ } - + RELEASE_MALLOC_GLOBAL_LOCK(); } - + if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */ char* mp = (char*)(CALL_MMAP(asize)); if (mp != CMFAIL) { @@ -4216,7 +4221,7 @@ static void* sys_alloc(mstate m, size_t nb) { mmap_flag = USE_MMAP_BIT; } } - + if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ if (asize < HALF_MAX_SIZE_T) { char* br = CMFAIL; @@ -4234,12 +4239,12 @@ static void* sys_alloc(mstate m, size_t nb) { } } } - + if (tbase != CMFAIL) { - + if ((m->footprint += tsize) > m->max_footprint) m->max_footprint = m->footprint; - + if (!is_initialized(m)) { /* first-time initialization */ if (m->least_addr == 0 || tbase < m->least_addr) m->least_addr = tbase; @@ -4260,7 +4265,7 @@ static void* sys_alloc(mstate m, size_t nb) { init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) -TOP_FOOT_SIZE); } } - + else { /* Try to merge with an existing segment */ msegmentptr sp = &m->seg; @@ -4292,7 +4297,7 @@ static void* sys_alloc(mstate m, size_t nb) { add_segment(m, tbase, tsize, mmap_flag); } } - + if (nb < m->topsize) { /* Allocate from new or extended top space */ size_t rsize = m->topsize -= nb; mchunkptr p = m->top; @@ -4304,7 +4309,7 @@ static void* sys_alloc(mstate m, size_t nb) { return chunk2mem(p); } } - + MALLOC_FAILURE_ACTION; return 0; } @@ -4364,14 +4369,14 @@ static int sys_trim(mstate m, size_t pad) { ensure_initialization(); if (pad < MAX_REQUEST && is_initialized(m)) { pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ - + if (m->topsize > pad) { /* Shrink top space in granularity-size units, keeping at least one */ size_t unit = mparams.granularity; size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - SIZE_T_ONE) * unit; msegmentptr sp = segment_holding(m, (char*)m->top); - + if (!is_extern_segment(sp)) { if (is_mmapped_segment(sp)) { if (HAVE_MMAP && @@ -4403,7 +4408,7 @@ static int sys_trim(mstate m, size_t pad) { RELEASE_MALLOC_GLOBAL_LOCK(); } } - + if (released != 0) { sp->size -= released; m->footprint -= released; @@ -4411,16 +4416,16 @@ static int sys_trim(mstate m, size_t pad) { check_top_chunk(m, m->top); } } - + /* Unmap any unused mmapped segments */ if (HAVE_MMAP) released += release_unused_segments(m); - + /* On failure, disable autotrim to avoid repeated failed future calls */ if (released == 0 && m->topsize > m->trim_check) m->trim_check = MAX_SIZE_T; } - + return (released != 0)? 1 : 0; } @@ -4536,7 +4541,7 @@ static void* tmalloc_large(mstate m, size_t nb) { t = *treebin_at(m, i); } } - + while (t != 0) { /* find smallest of tree or subtree */ size_t trem = chunksize(t) - nb; if (trem < rsize) { @@ -4545,7 +4550,7 @@ static void* tmalloc_large(mstate m, size_t nb) { } t = leftmost_child(t); } - + /* If dv is a better fit, return 0 so malloc will use it */ if (v != 0 && rsize < (size_t)(m->dvsize - nb)) { if (RTCHECK(ok_address(m, v))) { /* split */ @@ -4577,7 +4582,7 @@ static void* tmalloc_small(mstate m, size_t nb) { compute_bit2idx(leastbit, i); v = t = *treebin_at(m, i); rsize = chunksize(t) - nb; - + while ((t = leftmost_child(t)) != 0) { size_t trem = chunksize(t) - nb; if (trem < rsize) { @@ -4585,7 +4590,7 @@ static void* tmalloc_small(mstate m, size_t nb) { v = t; } } - + if (RTCHECK(ok_address(m, v))) { mchunkptr r = chunk_plus_offset(v, nb); assert(chunksize(v) == rsize + nb); @@ -4601,7 +4606,7 @@ static void* tmalloc_small(mstate m, size_t nb) { return chunk2mem(v); } } - + CORRUPTION_ERROR_ACTION(m); return 0; } @@ -4628,14 +4633,14 @@ void* dlmalloc(size_t bytes) { 3. If it is big enough, use the top chunk. 4. If request size >= mmap threshold, try to directly mmap this chunk. 5. If available, get memory from system and use it - + The ugly goto's here ensure that postaction occurs along all paths. */ - + #if USE_LOCKS ensure_initialization(); /* initialize in sys_alloc if not using locks */ #endif - + if (!PREACTION(gm)) { void* mem; size_t nb; @@ -4645,7 +4650,7 @@ void* dlmalloc(size_t bytes) { nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); idx = small_index(nb); smallbits = gm->smallmap >> idx; - + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ mchunkptr b, p; idx += ~smallbits & 1; /* Uses next bin if idx empty */ @@ -4658,7 +4663,7 @@ void* dlmalloc(size_t bytes) { check_malloced_chunk(gm, mem, nb); goto postaction; } - + else if (nb > gm->dvsize) { if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ mchunkptr b, p, r; @@ -4685,7 +4690,7 @@ void* dlmalloc(size_t bytes) { check_malloced_chunk(gm, mem, nb); goto postaction; } - + else if (gm->treemap != 0 && (mem = tmalloc_small(gm, nb)) != 0) { check_malloced_chunk(gm, mem, nb); goto postaction; @@ -4701,7 +4706,7 @@ void* dlmalloc(size_t bytes) { goto postaction; } } - + if (nb <= gm->dvsize) { size_t rsize = gm->dvsize - nb; mchunkptr p = gm->dv; @@ -4721,7 +4726,7 @@ void* dlmalloc(size_t bytes) { check_malloced_chunk(gm, mem, nb); goto postaction; } - + else if (nb < gm->topsize) { /* Split top */ size_t rsize = gm->topsize -= nb; mchunkptr p = gm->top; @@ -4733,9 +4738,9 @@ void* dlmalloc(size_t bytes) { check_malloced_chunk(gm, mem, nb); goto postaction; } - + mem = sys_alloc(gm, nb); - + postaction: POSTACTION(gm); #if __EMSCRIPTEN__ @@ -4744,7 +4749,7 @@ void* dlmalloc(size_t bytes) { #endif return mem; } - + return 0; } @@ -4756,7 +4761,7 @@ void dlfree(void* mem) { free chunks, if they exist, and then place in a bin. Intermixed with special cases for top, dv, mmapped chunks, and usage errors. */ - + if (mem != 0) { #if __EMSCRIPTEN__ /* XXX Emscripten Tracing API. */ @@ -4803,7 +4808,7 @@ void dlfree(void* mem) { goto erroraction; } } - + if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { if (!cinuse(next)) { /* consolidate forward */ if (next == fm->top) { @@ -4837,7 +4842,7 @@ void dlfree(void* mem) { } else set_free_with_pinuse(p, psize, next); - + if (is_small(psize)) { insert_small_chunk(fm, p, psize); check_free_chunk(fm, p); @@ -5001,7 +5006,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { mchunkptr newp = (mchunkptr)pos; size_t leadsize = pos - (char*)(p); size_t newsize = chunksize(p) - leadsize; - + if (is_mmapped(p)) { /* For mmapped chunks, just adjust offset */ newp->prev_foot = p->prev_foot + leadsize; newp->head = newsize; @@ -5013,7 +5018,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { } p = newp; } - + /* Give back spare room at the end */ if (!is_mmapped(p)) { size_t size = chunksize(p); @@ -5025,7 +5030,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { dispose_chunk(m, remainder, remainder_size); } } - + mem = chunk2mem(p); assert (chunksize(p) >= nb); assert(((size_t)mem & (alignment - 1)) == 0); @@ -5048,7 +5053,7 @@ static void** ialloc(mstate m, size_t* sizes, int opts, void* chunks[]) { - + size_t element_size; /* chunksize of each element, if all same */ size_t contents_size; /* total size of elements */ size_t array_size; /* request size of pointer array */ @@ -5060,7 +5065,7 @@ static void** ialloc(mstate m, flag_t was_enabled; /* to disable mmap */ size_t size; size_t i; - + ensure_initialization(); /* compute array length, if needed */ if (chunks != 0) { @@ -5076,7 +5081,7 @@ static void** ialloc(mstate m, marray = 0; array_size = request2size(n_elements * (sizeof(void*))); } - + /* compute total element size */ if (opts & 0x1) { /* all-same-size */ element_size = request2size(*sizes); @@ -5088,9 +5093,9 @@ static void** ialloc(mstate m, for (i = 0; i != n_elements; ++i) contents_size += request2size(sizes[i]); } - + size = contents_size + array_size; - + /* Allocate the aggregate chunk. First disable direct-mmapping so malloc won't use it, since we would not be able to later @@ -5103,17 +5108,17 @@ static void** ialloc(mstate m, enable_mmap(m); if (mem == 0) return 0; - + if (PREACTION(m)) return 0; p = mem2chunk(mem); remainder_size = chunksize(p); - + assert(!is_mmapped(p)); - + if (opts & 0x2) { /* optionally clear the elements */ memset((size_t*)mem, 0, remainder_size - SIZE_T_SIZE - array_size); } - + /* If not provided, allocate the pointer array as final part of chunk */ if (marray == 0) { size_t array_chunk_size; @@ -5123,7 +5128,7 @@ static void** ialloc(mstate m, set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size); remainder_size = contents_size; } - + /* split out elements */ for (i = 0; ; ++i) { marray[i] = chunk2mem(p); @@ -5141,7 +5146,7 @@ static void** ialloc(mstate m, break; } } - + #if DEBUG if (marray != chunks) { /* final element must have exactly exhausted chunk */ @@ -5155,9 +5160,9 @@ static void** ialloc(mstate m, } for (i = 0; i != n_elements; ++i) check_inuse_chunk(m, mem2chunk(marray[i])); - + #endif /* DEBUG */ - + POSTACTION(m); return marray; } @@ -5595,7 +5600,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); idx = small_index(nb); smallbits = ms->smallmap >> idx; - + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ mchunkptr b, p; idx += ~smallbits & 1; /* Uses next bin if idx empty */ @@ -5608,7 +5613,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { check_malloced_chunk(ms, mem, nb); goto postaction; } - + else if (nb > ms->dvsize) { if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ mchunkptr b, p, r; @@ -5635,7 +5640,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { check_malloced_chunk(ms, mem, nb); goto postaction; } - + else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { check_malloced_chunk(ms, mem, nb); goto postaction; @@ -5651,7 +5656,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { goto postaction; } } - + if (nb <= ms->dvsize) { size_t rsize = ms->dvsize - nb; mchunkptr p = ms->dv; @@ -5671,7 +5676,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { check_malloced_chunk(ms, mem, nb); goto postaction; } - + else if (nb < ms->topsize) { /* Split top */ size_t rsize = ms->topsize -= nb; mchunkptr p = ms->top; @@ -5683,14 +5688,14 @@ void* mspace_malloc(mspace msp, size_t bytes) { check_malloced_chunk(ms, mem, nb); goto postaction; } - + mem = sys_alloc(ms, nb); - + postaction: POSTACTION(ms); return mem; } - + return 0; } @@ -5738,7 +5743,7 @@ void mspace_free(mspace msp, void* mem) { goto erroraction; } } - + if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { if (!cinuse(next)) { /* consolidate forward */ if (next == fm->top) { @@ -5772,7 +5777,7 @@ void mspace_free(mspace msp, void* mem) { } else set_free_with_pinuse(p, psize, next); - + if (is_small(psize)) { insert_small_chunk(fm, p, psize); check_free_chunk(fm, p); @@ -6070,7 +6075,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme /* Guidelines for creating a custom version of MORECORE: - + * For best performance, MORECORE should allocate in multiples of pagesize. * MORECORE may allocate more memory than requested. (Or even less, but this will usually result in a malloc failure.) @@ -6089,29 +6094,29 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme must not misinterpret negative args as large positive unsigned args. You can suppress all such calls from even occurring by defining MORECORE_CANNOT_TRIM, - + As an example alternative MORECORE, here is a custom allocator kindly contributed for pre-OSX macOS. It uses virtually but not necessarily physically contiguous non-paged memory (locked in, present and won't get swapped out). You can use it by uncommenting this section, adding some #includes, and setting up the appropriate defines above: - + #define MORECORE osMoreCore - + There is also a shutdown routine that should somehow be called for cleanup upon program exit. - + #define MAX_POOL_ENTRIES 100 #define MINIMUM_MORECORE_SIZE (64 * 1024U) static int next_os_pool; void *our_os_pools[MAX_POOL_ENTRIES]; - + void *osMoreCore(int size) { void *ptr = 0; static void *sbrk_top = 0; - + if (size > 0) { if (size < MINIMUM_MORECORE_SIZE) @@ -6139,14 +6144,14 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme return sbrk_top; } } - + // cleanup any allocated memory pools // called as last thing before shutting down driver - + void osCleanupMem(void) { void **ptr; - + for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++) if (*ptr) { @@ -6154,7 +6159,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme *ptr = 0; } } - + */ @@ -6165,7 +6170,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * don't reuse adjusted asize in sys_alloc * add LOCK_AT_FORK -- thanks to Kirill Artamonov for the suggestion * reduce compiler warnings -- thanks to all who reported/suggested these - + v2.8.5 Sun May 22 10:26:02 2011 Doug Lea (dl at gee) * Always perform unlink checks unless INSECURE * Add posix_memalign. @@ -6179,10 +6184,10 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Small fixes to mspace_destroy, reset_on_error. * Various configuration extensions/changes. Thanks to all who contributed these. - + V2.8.4a Thu Apr 28 14:39:43 2011 (dl at gee.cs.oswego.edu) * Update Creative Commons URL - + V2.8.4 Wed May 27 09:56:23 2009 Doug Lea (dl at gee) * Use zeros instead of prev foot for is_mmapped * Add mspace_track_large_chunks; thanks to Jean Brouwers @@ -6197,7 +6202,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Various small adjustments to reduce warnings on some compilers * Various configuration extensions/changes for more platforms. Thanks to all who contributed these. - + V2.8.3 Thu Sep 22 11:16:32 2005 Doug Lea (dl at gee) * Add max_footprint functions * Ensure all appropriate literals are size_t @@ -6210,14 +6215,14 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Simplify and fix segment insertion, trimming and mspace_destroy * Reinstate REALLOC_ZERO_BYTES_FREES option from 2.7.x * Thanks especially to Dennis Flanagan for help on these. - + V2.8.2 Sun Jun 12 16:01:10 2005 Doug Lea (dl at gee) * Fix memalign brace error. - + V2.8.1 Wed Jun 8 16:11:46 2005 Doug Lea (dl at gee) * Fix improper #endif nesting in C++ * Add explicit casts needed for C++ - + V2.8.0 Mon May 30 14:09:02 2005 Doug Lea (dl at gee) * Use trees for large bins * Support mspaces @@ -6233,10 +6238,10 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Remove useless cfree() to avoid conflicts with other apps. * Remove internal memcpy, memset. Compilers handle builtins better. * Remove some options that no one ever used and rename others. - + V2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee) * Fix malloc_state bitmap array misdeclaration - + V2.7.1 Thu Jul 25 10:58:03 2002 Doug Lea (dl at gee) * Allow tuning of FIRST_SORTED_BIN_SIZE * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte. @@ -6249,7 +6254,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield. * Branch-free bin calculation * Default trim and mmap thresholds now 256K. - + V2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee) * Introduce independent_comalloc and independent_calloc. Thanks to Michael Pachos for motivation and help. @@ -6273,7 +6278,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS Thanks to Tony E. Bennett and others. * Include errno.h to support default failure action. - + V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee) * return null for negative arguments * Added Several WIN32 cleanups from Martin C. Fong @@ -6289,10 +6294,10 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to avoid infinite loop * Always call 'fREe()' rather than 'free()' - + V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee) * Fixed ordering problem with boundary-stamping - + V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee) * Added pvalloc, as recommended by H.J. Liu * Added 64bit pointer support mainly from Wolfram Gloger @@ -6301,7 +6306,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * malloc_extend_top: fix mask error that caused wastage after foreign sbrks * Add linux mremap support code from HJ Liu - + V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee) * Integrated most documentation with the code. * Add support for mmap, with help from @@ -6322,7 +6327,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Added macros etc., allowing use in linux libc from H.J. Lu (hjl@gnu.ai.mit.edu) * Inverted this history list - + V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee) * Re-tuned and fixed to behave more nicely with V2.6.0 changes. * Removed all preallocation code since under current scheme @@ -6333,17 +6338,17 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme given above changes. * Use best fit for very large chunks to prevent some worst-cases. * Added some support for debugging - + V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee) * Removed footers when chunks are in use. Thanks to Paul Wilson (wilson@cs.texas.edu) for the suggestion. - + V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee) * Added malloc_trim, with help from Wolfram Gloger (wmglo@Dent.MED.Uni-Muenchen.DE). - + V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g) - + V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g) * realloc: try to expand in both directions * malloc: swap order of clean-bin strategy; @@ -6352,7 +6357,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Use bin counts as a guide to preallocation * Occasionally bin return list chunks in first scan * Add a few optimizations from colin@nyx10.cs.du.edu - + V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g) * faster bin computation & slightly different binning * merged all consolidations to one part of malloc proper @@ -6361,7 +6366,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Propagate failure in realloc if malloc returns 0 * Add stuff to allow compilation on non-ANSI compilers from kpv@research.att.com - + V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu) * removed potential for odd address access in prev_chunk * removed dependency on getpagesize.h @@ -6370,9 +6375,9 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * tested on sparc, hp-700, dec-mips, rs6000 with gcc & native cc (hp, dec only) allowing Detlefs & Zorn comparison study (in SIGPLAN Notices.) - + Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu) * Based loosely on libg++-1.2X malloc. (It retains some of the overall structure of old version, but most details differ.) - + */ From 2cc696ff963962b556fccbe5b49587ae562397ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Cant=C3=B3n=20Cort=C3=A9s?= Date: Thu, 13 Oct 2022 19:26:53 +0200 Subject: [PATCH 02/10] Remove unwanted changes --- system/lib/dlmalloc.c | 684 +++++++++++++++++++++--------------------- 1 file changed, 342 insertions(+), 342 deletions(-) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index 6297b0534e7c7..079c1d1918b3f 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -49,20 +49,20 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); Doug Lea and released to the public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/ Send questions, comments, complaints, performance data, etc to dl@cs.oswego.edu - + * Version 2.8.6 Wed Aug 29 06:57:58 2012 Doug Lea Note: There may be an updated version of this malloc obtainable at ftp://gee.cs.oswego.edu/pub/misc/malloc.c Check before installing! - + * Quickstart - + This library is all in one file to simplify the most common usage: ftp it, compile it (-O3), and link it into another program. All of the compile-time options default to reasonable values for use on most platforms. You might later want to step through various compile-time and dynamic tuning options. - + For convenience, an include file for code using this malloc is at: ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.6.h You don't really need this .h file unless you call functions not @@ -76,30 +76,30 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); malloc (for example in linux). You might still want to use the one in this file to customize settings or to avoid overheads associated with library versions. - + * Vital statistics: - + Supported pointer/size_t representation: 4 or 8 bytes size_t MUST be an unsigned type of the same width as pointers. (If you are using an ancient system that declares size_t as a signed type, or need it to be a different width than pointers, you can use a previous release of this malloc (e.g. 2.7.2) supporting these.) - + Alignment: 8 bytes (minimum) This suffices for nearly all current machines and C compilers. However, you can define MALLOC_ALIGNMENT to be wider than this if necessary (up to 128bytes), at the expense of using more space. - + Minimum overhead per allocated chunk: 4 or 8 bytes (if 4byte sizes) 8 or 16 bytes (if 8byte sizes) Each malloced chunk has a hidden word of overhead holding size and status information, and additional cross-check word if FOOTERS is defined. - + Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead) 8-byte ptrs: 32 bytes (including overhead) - + Even a request for zero bytes (i.e., malloc(0)) returns a pointer to something of the minimum allocatable size. The maximum overhead wastage (i.e., number of extra bytes @@ -108,7 +108,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); are serviced via mmap(), where the worst case wastage is about 32 bytes plus the remainder from a system page (the minimal mmap unit); typically 4096 or 8192 bytes. - + Security: static-safe; optionally more or less The "security" of malloc refers to the ability of malicious code to accentuate the effects of errors (for example, freeing @@ -121,7 +121,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); for malloc itself is not corrupted by some other means. This is only one aspect of security -- these checks do not, and cannot, detect all possible programming errors. - + If FOOTERS is defined nonzero, then each allocated chunk carries an additional check word to verify that it was malloced from its space. These check words are the same within each @@ -132,7 +132,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); writes to statics that are always on. This may further improve security at the expense of time and space overhead. (Note that FOOTERS may also be worth using with MSPACES.) - + By default detected errors cause the program to abort (calling "abort()"). You can override this to instead proceed past errors by defining PROCEED_ON_ERROR. In this case, a bad free @@ -142,19 +142,19 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); be appropriate for programs that should continue if at all possible in the face of programming errors, although they may run out of memory because dropped memory is never reclaimed. - + If you don't like either of these options, you can define CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything else. And if if you are sure that your program using malloc has no errors or vulnerabilities, you can define INSECURE to 1, which might (or might not) provide a small performance improvement. - + It is also possible to limit the maximum total allocatable space, using malloc_set_footprint_limit. This is not designed as a security feature in itself (calls to set limits are not screened or privileged), but may be useful as one aspect of a secure implementation. - + Thread-safety: NOT thread-safe unless USE_LOCKS defined non-zero When USE_LOCKS is defined, each public call to malloc, free, etc is surrounded with a lock. By default, this uses a plain @@ -171,7 +171,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); (http://www.nedprod.com/programs/portable/nedmalloc/) or ptmalloc (See http://www.malloc.de), which are derived from versions of this malloc. - + System requirements: Any combination of MORECORE and/or MMAP/MUNMAP This malloc can use unix sbrk or any emulation (invoked using the CALL_MORECORE macro) and/or mmap/munmap or any emulation @@ -180,19 +180,19 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); MORECORE and MMAP are enabled. On Win32, it uses emulations based on VirtualAlloc. It also uses common C library functions like memset. - + Compliance: I believe it is compliant with the Single Unix Specification (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably others as well. - + * Overview of algorithms - + This is not the fastest, most space-conserving, most portable, or most tunable malloc ever written. However it is among the fastest while also being among the most space-conserving, portable and tunable. Consistent balance across these factors results in a good general-purpose allocator for malloc-intensive programs. - + In most ways, this malloc is a best-fit allocator. Generally, it chooses the best-fitting existing chunk for a request, with ties broken in approximately least-recently-used order. (This strategy @@ -205,7 +205,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); (>= 256Kb by default), it relies on system memory mapping facilities, if supported. (This helps avoid carrying around and possibly fragmenting memory used only for large chunks.) - + All operations (except malloc_stats and mallinfo) have execution times that are bounded by a constant factor of the number of bits in a size_t, not counting any clearing in calloc or copying in realloc, @@ -216,20 +216,20 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); NO_SEGMENT_TRAVERSAL, which assures bounded execution even when system allocators return non-contiguous spaces, at the typical expense of carrying around more memory and increased fragmentation. - + The implementation is not very modular and seriously overuses macros. Perhaps someday all C compilers will do as good a job inlining modular code as can now be done by brute-force expansion, but now, enough of them seem not to. - + Some compilers issue a lot of warnings about code that is dead/unreachable only on some platforms, and also about intentional uses of negation on unsigned types. All known cases of each can be ignored. - + For a longer but out of date high-level description, see http://gee.cs.oswego.edu/dl/html/malloc.html - + * MSPACES If MSPACES is defined, then in addition to malloc, free, etc., this file also defines mspace_malloc, mspace_free, etc. These @@ -241,10 +241,10 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); ONLY_MSPACES and then do something like... static mspace mymspace = create_mspace(0,0); // for example #define mymalloc(bytes) mspace_malloc(mymspace, bytes) - + (Note: If you only need one instance of an mspace, you can instead use "USE_DL_PREFIX" to relabel the global malloc.) - + You can similarly create thread-local allocators by storing mspaces as thread-locals. For example: static __thread mspace tlms = 0; @@ -253,21 +253,21 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); return mspace_malloc(tlms, bytes); } void tlfree(void* mem) { mspace_free(tlms, mem); } - + Unless FOOTERS is defined, each mspace is completely independent. You cannot allocate from one and free to another (although conformance is only weakly checked, so usage errors are not always caught). If FOOTERS is defined, then each chunk carries around a tag indicating its originating mspace, and frees are directed to their originating spaces. Normally, this requires use of locks. - + ------------------------- Compile-time options --------------------------- - + Be careful in setting #define values for numerical constants of type size_t. On some systems, literal values are not automatically extended to size_t precision unless they are explicitly casted. You can also use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below. - + WIN32 default: defined if _WIN32 defined Defining WIN32 sets up defaults for MS environment and compilers. Otherwise defaults are for unix. Beware that there seem to be some @@ -283,28 +283,28 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); recompile this malloc with a larger DEFAULT_GRANULARITY. Note: in cases where MSC and gcc (cygwin) are known to differ on WIN32, conditions use _MSC_VER to distinguish them. - + DLMALLOC_EXPORT default: extern Defines how public APIs are declared. If you want to export via a Windows DLL, you might define this as #define DLMALLOC_EXPORT extern __declspec(dllexport) If you want a POSIX ELF shared object, you might use #define DLMALLOC_EXPORT extern __attribute__((visibility("default"))) - + MALLOC_ALIGNMENT default: (size_t)(2 * sizeof(void *)) Controls the minimum alignment for malloc'ed chunks. It must be a power of two and at least 8, even on machines for which smaller alignments would suffice. It may be defined as larger than this though. Note however that code and data structures are optimized for the case of 8-byte alignment. - + MSPACES default: 0 (false) If true, compile in support for independent allocation spaces. This is only supported if HAVE_MMAP is true. - + ONLY_MSPACES default: 0 (false) If true, only compile in mspace versions, not regular versions. - + USE_LOCKS default: 0 (false) Causes each call to each public routine to be surrounded with pthread or WIN32 mutex lock/unlock. (If set true, this can be @@ -312,43 +312,43 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); non-zero value other than 1, locks are used, but their implementation is left out, so lock functions must be supplied manually, as described below. - + USE_SPIN_LOCKS default: 1 iff USE_LOCKS and spin locks available If true, uses custom spin locks for locking. This is currently supported only gcc >= 4.1, older gccs on x86 platforms, and recent MS compilers. Otherwise, posix locks or win32 critical sections are used. - + USE_RECURSIVE_LOCKS default: not defined If defined nonzero, uses recursive (aka reentrant) locks, otherwise uses plain mutexes. This is not required for malloc proper, but may be needed for layered allocators such as nedmalloc. - + LOCK_AT_FORK default: not defined If defined nonzero, performs pthread_atfork upon initialization to initialize child lock while holding parent lock. The implementation assumes that pthread locks (not custom locks) are being used. In other cases, you may need to customize the implementation. - + FOOTERS default: 0 If true, provide extra checking and dispatching by placing information in the footers of allocated chunks. This adds space and time overhead. - + INSECURE default: 0 If true, omit checks for usage errors and heap space overwrites. - + USE_DL_PREFIX default: NOT defined Causes compiler to prefix all public routines with the string 'dl'. This can be useful when you only want to use this malloc in one part of a program, using your regular system malloc elsewhere. - + MALLOC_INSPECT_ALL default: NOT defined If defined, compiles malloc_inspect_all and mspace_inspect_all, that perform traversal of all heap space. Unless access to these functions is otherwise restricted, you probably do not want to include them in secure implementations. - + ABORT default: defined as abort() Defines how to abort on failed checks. On most systems, a failed check cannot die with an "assert" or even print an informative @@ -359,7 +359,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); addresses etc) rather than malloc-triggered checks, so will also abort. Also, most compilers know that abort() does not return, so can better optimize code conditionally calling it. - + PROCEED_ON_ERROR default: defined as 0 (false) Controls whether detected bad addresses cause them to bypassed rather than aborting. If set, detected bad arguments to free and @@ -370,7 +370,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); static variable malloc_corruption_error_count is compiled in and can be examined to see if errors have occurred. This option generates slower code than the default abort policy. - + DEBUG default: NOT defined The DEBUG setting is mainly intended for people trying to modify this code or diagnose problems when porting to new platforms. @@ -381,21 +381,21 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); execution noticeably. Calling malloc_stats or mallinfo with DEBUG set will attempt to check every non-mmapped allocated and free chunk in the course of computing the summaries. - + ABORT_ON_ASSERT_FAILURE default: defined as 1 (true) Debugging assertion failures can be nearly impossible if your version of the assert macro causes malloc to be called, which will lead to a cascade of further failures, blowing the runtime stack. ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(), which will usually make debugging easier. - + MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32 The action to take before "return 0" when malloc fails to be able to return memory because there is none available. - + HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES True if this system supports sbrk or an emulation of it. - + MORECORE default: sbrk The name of the sbrk-style system routine to call to obtain more memory. See below for guidance on writing custom MORECORE @@ -406,7 +406,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); though. Internally, we only call it with arguments less than half the max value of a size_t, which should work across all reasonable possibilities, although sometimes generating compiler warnings. - + MORECORE_CONTIGUOUS default: 1 (true) if HAVE_MORECORE If true, take advantage of fact that consecutive calls to MORECORE with positive arguments always return contiguous increasing @@ -414,19 +414,19 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); set it true anyway, since malloc copes with non-contiguities. Setting it false when definitely non-contiguous saves time and possibly wasted space it would take to discover this though. - + MORECORE_CANNOT_TRIM default: NOT defined True if MORECORE cannot release space back to the system when given negative arguments. This is generally necessary only if you are using a hand-crafted MORECORE function that cannot handle negative arguments. - + NO_SEGMENT_TRAVERSAL default: 0 If non-zero, suppresses traversals of memory segments returned by either MORECORE or CALL_MMAP. This disables merging of segments that are contiguous, and selectively releasing them to the OS if unused, but bounds execution times. - + HAVE_MMAP default: 1 (true) True if this system supports mmap or an emulation of it. If so, and HAVE_MORECORE is not true, MMAP is used for all system @@ -436,15 +436,15 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); space from system. Note: A single call to MUNMAP is assumed to be able to unmap memory that may have be allocated using multiple calls to MMAP, so long as they are adjacent. - + HAVE_MREMAP default: 1 on linux, else 0 If true realloc() uses mremap() to re-allocate large blocks and extend or shrink allocation spaces. - + MMAP_CLEARS default: 1 except on WINCE. True if mmap clears memory so calloc doesn't need to. This is true for standard unix mmap using /dev/zero and on WIN32 except for WINCE. - + USE_BUILTIN_FFS default: 0 (i.e., not used) Causes malloc to use the builtin ffs() function to compute indices. Some compilers may recognize and intrinsify ffs to be faster than the @@ -452,44 +452,44 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); to an asm instruction, so is already as fast as it can be, and so this setting has no effect. Similarly for Win32 under recent MS compilers. (On most x86s, the asm version is only slightly faster than the C version.) - + malloc_getpagesize default: derive from system includes, or 4096. The system page size. To the extent possible, this malloc manages memory from the system in page-size units. This may be (and usually is) a function rather than a constant. This is ignored if WIN32, where page size is determined using getSystemInfo during initialization. - + USE_DEV_RANDOM default: 0 (i.e., not used) Causes malloc to use /dev/random to initialize secure magic seed for stamping footers. Otherwise, the current time is used. - + NO_MALLINFO default: 0 If defined, don't compile "mallinfo". This can be a simple way of dealing with mismatches between system declarations and those in this file. - + MALLINFO_FIELD_TYPE default: size_t The type of the fields in the mallinfo struct. This was originally defined as "int" in SVID etc, but is more usefully defined as size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set - + NO_MALLOC_STATS default: 0 If defined, don't compile "malloc_stats". This avoids calls to fprintf and bringing in stdio dependencies you might not want. - + REALLOC_ZERO_BYTES_FREES default: not defined This should be set if a call to realloc with zero bytes should be the same as a call to free. Some people think it should. Otherwise, since this malloc returns a unique pointer for malloc(0), so does realloc(p, 0). - + LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H LACKS_STDLIB_H LACKS_SCHED_H LACKS_TIME_H default: NOT defined unless on WIN32 Define these if your system does not have these header files. You might need to manually insert some of the declarations they provide. - + DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS, system_info.dwAllocationGranularity in WIN32, otherwise 64K. @@ -504,7 +504,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); to either page size or win32 region size. (Note: In previous versions of malloc, the equivalent of this option was called "TOP_PAD") - + DEFAULT_TRIM_THRESHOLD default: 2MB Also settable using mallopt(M_TRIM_THRESHOLD, x) The maximum amount of unused top-most memory to keep before @@ -527,7 +527,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); program startup, in an attempt to reserve system memory, doesn't have the intended effect under automatic trimming, since that memory will immediately be returned to the system. - + DEFAULT_MMAP_THRESHOLD default: 256K Also settable using mallopt(M_MMAP_THRESHOLD, x) The request size threshold for using MMAP to directly service a @@ -551,7 +551,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); value of "large" may vary across systems. The default is an empirically derived value that works well in most systems. You can disable mmap by setting to MAX_SIZE_T. - + MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP The number of consolidated frees between checks to release unused segments when freeing. When using non-contiguous segments, @@ -792,7 +792,7 @@ defined(__i386__) || defined(__x86_64__))) || \ are not even meaningful in this version of malloc. These fields are are instead filled by mallinfo() with other numbers that might be of interest. - + HAVE_USR_INCLUDE_MALLOC_H should be set if you have a /usr/include/malloc.h file that includes a declaration of struct mallinfo. If so, it is included; else a compliant version is @@ -860,11 +860,11 @@ extern "C" { #ifndef FORCEINLINE #define FORCEINLINE #endif - + #if !ONLY_MSPACES - + /* ------------------- Declarations of public routines ------------------- */ - + #ifndef USE_DL_PREFIX // XXX Emscripten XXX #if defined(__EMSCRIPTEN__) @@ -902,13 +902,13 @@ void** independent_comalloc(size_t, size_t*, void**) __attribute__((weak, alias( size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_free"))); #endif /*__EMSCRIPTEN__*/ #endif /* USE_DL_PREFIX */ - + /* malloc(size_t n) Returns a pointer to a newly allocated chunk of at least n bytes, or null if no space is available, in which case errno is set to ENOMEM on ANSI C systems. - + If n is zero, malloc returns a minimum-sized chunk. (The minimum size is 16 bytes on most 32bit systems, and 32 bytes on 64bit systems.) Note that size_t is an unsigned type, so calls with @@ -918,7 +918,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f cases less than the maximum representable value of a size_t. */ DLMALLOC_EXPORT void* dlmalloc(size_t); - + /* free(void* p) Releases the chunk of memory pointed to by p, that had been previously @@ -927,38 +927,38 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f freed, free(p) will by default cause the current program to abort. */ DLMALLOC_EXPORT void dlfree(void*); - + /* calloc(size_t n_elements, size_t element_size); Returns a pointer to n_elements * element_size bytes, with all locations set to zero. */ DLMALLOC_EXPORT void* dlcalloc(size_t, size_t); - + /* realloc(void* p, size_t n) Returns a pointer to a chunk of size n that contains the same data as does chunk p up to the minimum of (n, p's size) bytes, or null if no space is available. - + The returned pointer may or may not be the same as p. The algorithm prefers extending p in most cases when possible, otherwise it employs the equivalent of a malloc-copy-free sequence. - + If p is null, realloc is equivalent to malloc. - + If space is not available, realloc returns null, errno is set (if on ANSI) and p is NOT freed. - + if n is for fewer bytes than already held by p, the newly unused space is lopped off and freed if possible. realloc with a size argument of zero (re)allocates a minimum-sized chunk. - + The old unix realloc convention of allowing the last-free'd chunk to be used as an argument to realloc is not supported. */ DLMALLOC_EXPORT void* dlrealloc(void*, size_t); - + /* realloc_in_place(void* p, size_t n) Resizes the space allocated for p to size n, only if this can be @@ -969,25 +969,25 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f to expand space; for example, reallocation of a buffer that must be memory-aligned or cleared. You can use realloc_in_place to trigger these alternatives only when needed. - + Returns p if successful; otherwise null. */ DLMALLOC_EXPORT void* dlrealloc_in_place(void*, size_t); - + /* memalign(size_t alignment, size_t n); Returns a pointer to a newly allocated chunk of n bytes, aligned in accord with the alignment argument. - + The alignment argument should be a power of two. If the argument is not a power of two, the nearest greater power is used. 8-byte alignment is guaranteed by normal malloc calls, so don't bother calling memalign with an argument of 8 or less. - + Overreliance on memalign is a sure way to fragment space. */ DLMALLOC_EXPORT void* dlmemalign(size_t, size_t); - + /* int posix_memalign(void** pp, size_t alignment, size_t n); Allocates a chunk of n bytes, aligned in accord with the alignment @@ -997,14 +997,14 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f returns ENOMEM if memory cannot be allocated. */ DLMALLOC_EXPORT int dlposix_memalign(void**, size_t, size_t); - + /* valloc(size_t n); Equivalent to memalign(pagesize, n), where pagesize is the page size of the system. If the pagesize is unknown, 4096 is used. */ DLMALLOC_EXPORT void* dlvalloc(size_t); - + /* mallopt(int parameter_number, int parameter_value) Sets tunable parameters The format is to provide a @@ -1014,21 +1014,21 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f 0. To workaround the fact that mallopt is specified to use int, not size_t parameters, the value -1 is specially treated as the maximum unsigned size_t value. - + SVID/XPG/ANSI defines four standard param numbers for mallopt, normally defined in malloc.h. None of these are use in this malloc, so setting them has no effect. But this malloc also supports other options in mallopt. See below for details. Briefly, supported parameters are as follows (listed defaults are for "typical" configurations). - + Symbol param # default allowed param values M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables) M_GRANULARITY -2 page size any power of 2 >= page size M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) */ DLMALLOC_EXPORT int dlmallopt(int, int); - + /* malloc_footprint(); Returns the number of bytes obtained from the system. The total @@ -1039,7 +1039,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f so results might not be up to date. */ DLMALLOC_EXPORT size_t dlmalloc_footprint(void); - + /* malloc_max_footprint(); Returns the maximum number of bytes obtained from the system. This @@ -1052,7 +1052,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f not be up to date. */ DLMALLOC_EXPORT size_t dlmalloc_max_footprint(void); - + /* malloc_footprint_limit(); Returns the number of bytes that the heap is allowed to obtain from @@ -1063,7 +1063,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f the system. */ DLMALLOC_EXPORT size_t dlmalloc_footprint_limit(); - + /* malloc_set_footprint_limit(); Sets the maximum number of bytes to obtain from the system, causing @@ -1077,7 +1077,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f retroactively deallocate existing used memory. */ DLMALLOC_EXPORT size_t dlmalloc_set_footprint_limit(size_t bytes); - + #if MALLOC_INSPECT_ALL /* malloc_inspect_all(void(*handler)(void *start, @@ -1095,7 +1095,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f invoked with the given callback argument. If locks are defined, they are held during the entire traversal. It is a bad idea to invoke other malloc functions from within the handler. - + For example, to count the number of in-use chunks with size greater than 1000, you could write: static int count = 0; @@ -1104,19 +1104,19 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f } then: malloc_inspect_all(count_chunks, NULL); - + malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined. */ DLMALLOC_EXPORT void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*), void* arg); - + #endif /* MALLOC_INSPECT_ALL */ - + #if !NO_MALLINFO /* mallinfo() Returns (by copy) a struct containing various summary statistics: - + arena: current total non-mmapped bytes allocated from system ordblks: the number of free chunks smblks: always zero. @@ -1130,17 +1130,17 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f keepcost: the maximum number of bytes that could ideally be released back to system via malloc_trim. ("ideally" means that it ignores page restrictions etc.) - + Because these fields are ints, but internal bookkeeping may be kept as longs, the reported values may wrap around zero and thus be inaccurate. */ DLMALLOC_EXPORT struct mallinfo dlmallinfo(void); #endif /* NO_MALLINFO */ - + /* independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); - + independent_calloc is similar to calloc, but instead of returning a single cleared space, it returns an array of pointers to n_elements independent elements that can hold contents of size elem_size, each @@ -1149,30 +1149,30 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f allocated (this is not guaranteed to occur with multiple callocs or mallocs), which may also improve cache locality in some applications. - + The "chunks" argument is optional (i.e., may be null, which is probably the most typical usage). If it is null, the returned array is itself dynamically allocated and should also be freed when it is no longer needed. Otherwise, the chunks array must be of at least n_elements in length. It is filled in with the pointers to the chunks. - + In either case, independent_calloc returns this pointer array, or null if the allocation failed. If n_elements is zero and "chunks" is null, it returns a chunk representing an array with zero elements (which should be freed if not wanted). - + Each element must be freed when it is no longer needed. This can be done all at once using bulk_free. - + independent_calloc simplifies and speeds up implementations of many kinds of pools. It may also be useful when constructing large data structures that initially have a fixed number of fixed-sized nodes, but the number is not known at compile time, and some of the nodes may later need to be freed. For example: - + struct Node { int item; struct Node* next; }; - + struct Node* build_list() { struct Node** pool; int n = read_number_of_nodes_needed(); @@ -1188,10 +1188,10 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f } */ DLMALLOC_EXPORT void** dlindependent_calloc(size_t, size_t, void**); - + /* independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); - + independent_comalloc allocates, all at once, a set of n_elements chunks with sizes indicated in the "sizes" array. It returns an array of pointers to these elements, each of which can be @@ -1199,32 +1199,32 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f be adjacently allocated (this is not guaranteed to occur with multiple callocs or mallocs), which may also improve cache locality in some applications. - + The "chunks" argument is optional (i.e., may be null). If it is null the returned array is itself dynamically allocated and should also be freed when it is no longer needed. Otherwise, the chunks array must be of at least n_elements in length. It is filled in with the pointers to the chunks. - + In either case, independent_comalloc returns this pointer array, or null if the allocation failed. If n_elements is zero and chunks is null, it returns a chunk representing an array with zero elements (which should be freed if not wanted). - + Each element must be freed when it is no longer needed. This can be done all at once using bulk_free. - + independent_comallac differs from independent_calloc in that each element may have a different size, and also that it does not automatically clear elements. - + independent_comalloc can be used to speed up allocation in cases where several structs or objects must always be allocated at the same time. For example: - + struct Head { ... } struct Foot { ... } - + void send_message(char* msg) { int msglen = strlen(msg); size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; @@ -1236,17 +1236,17 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f struct Foot* foot = (struct Foot*)(chunks[2]); // ... } - + In general though, independent_comalloc is worth using only for larger values of n_elements. For small values, you probably won't detect enough difference from series of malloc calls to bother. - + Overuse of independent_comalloc can increase overall memory usage, since it cannot reuse existing noncontiguous small chunks that might be available for some of the elements. */ DLMALLOC_EXPORT void** dlindependent_comalloc(size_t, size_t*, void**); - + /* bulk_free(void* array[], size_t n_elements) Frees and clears (sets to null) each non-null pointer in the given @@ -1257,17 +1257,17 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f may be worthwhile to sort this array before calling bulk_free. */ DLMALLOC_EXPORT size_t dlbulk_free(void**, size_t n_elements); - + /* pvalloc(size_t n); Equivalent to valloc(minimum-page-that-holds(n)), that is, round up n to nearest pagesize. */ DLMALLOC_EXPORT void* dlpvalloc(size_t); - + /* malloc_trim(size_t pad); - + If possible, gives memory back to the system (via negative arguments to sbrk) if there is unused memory at the `high' end of the malloc pool or in unused MMAP segments. You can call this after freeing @@ -1276,18 +1276,18 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f memory. Under some allocation patterns, some large free blocks of memory will be locked between two used chunks, so they cannot be given back to the system. - + The `pad' argument to malloc_trim represents the amount of free trailing space to leave untrimmed. If this argument is zero, only the minimum amount of memory to maintain internal data structures will be left. Non-zero arguments can be supplied to maintain enough trailing space to service future expected allocations without having to re-obtain memory from the system. - + Malloc_trim returns 1 if it actually released any memory, else 0. */ DLMALLOC_EXPORT int dlmalloc_trim(size_t); - + /* malloc_stats(); Prints on stderr the amount of space obtained from the system (both @@ -1299,19 +1299,19 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f because of alignment and bookkeeping overhead. Because it includes alignment wastage as being in use, this figure may be greater than zero even when no user-level chunks are allocated. - + The reported current and maximum system memory can be inaccurate if a program makes other calls to system memory allocation functions (normally sbrk) outside of malloc. - + malloc_stats prints only the most commonly interesting statistics. More information can be obtained by calling mallinfo. */ DLMALLOC_EXPORT void dlmalloc_stats(void); - + /* malloc_usable_size(void* p); - + Returns the number of bytes you can actually use in an allocated chunk, which may be more than you requested (although often not) due to alignment and minimum size constraints. @@ -1319,23 +1319,23 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f overwriting other allocated objects. This is not a particularly great programming practice. malloc_usable_size can be more useful in debugging and assertions, for example: - + p = malloc(n); assert(malloc_usable_size(p) >= 256); */ /* XXX EMSCRIPTEN: mark for export (and therefore weak) */ DLMALLOC_EXPORT size_t dlmalloc_usable_size(void*); - + #endif /* ONLY_MSPACES */ - + #if MSPACES - + /* mspace is an opaque type representing an independent region of space that supports mspace_malloc, etc. */ typedef void* mspace; - + /* create_mspace creates and returns a new independent space with the given initial capacity, or, if 0, the default granularity size. It @@ -1348,7 +1348,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f setting with mallopt(M_GRANULARITY, value). */ DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked); - + /* destroy_mspace destroys the given space, and attempts to return all of its memory back to the system, returning the total number of @@ -1356,7 +1356,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f used by the space become undefined. */ DLMALLOC_EXPORT size_t destroy_mspace(mspace msp); - + /* create_mspace_with_base uses the memory supplied as the initial base of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this @@ -1367,7 +1367,7 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f space (if possible) but not the initial base. */ DLMALLOC_EXPORT mspace create_mspace_with_base(void* base, size_t capacity, int locked); - + /* mspace_track_large_chunks controls whether requests for large chunks are allocated in their own untracked mmapped regions, separate from @@ -1380,74 +1380,74 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f setting. */ DLMALLOC_EXPORT int mspace_track_large_chunks(mspace msp, int enable); - - + + /* mspace_malloc behaves as malloc, but operates within the given space. */ DLMALLOC_EXPORT void* mspace_malloc(mspace msp, size_t bytes); - + /* mspace_free behaves as free, but operates within the given space. - + If compiled with FOOTERS==1, mspace_free is not actually needed. free may be called instead of mspace_free because freed chunks from any space are handled by their originating spaces. */ DLMALLOC_EXPORT void mspace_free(mspace msp, void* mem); - + /* mspace_realloc behaves as realloc, but operates within the given space. - + If compiled with FOOTERS==1, mspace_realloc is not actually needed. realloc may be called instead of mspace_realloc because realloced chunks from any space are handled by their originating spaces. */ DLMALLOC_EXPORT void* mspace_realloc(mspace msp, void* mem, size_t newsize); - + /* mspace_calloc behaves as calloc, but operates within the given space. */ DLMALLOC_EXPORT void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); - + /* mspace_memalign behaves as memalign, but operates within the given space. */ DLMALLOC_EXPORT void* mspace_memalign(mspace msp, size_t alignment, size_t bytes); - + /* mspace_independent_calloc behaves as independent_calloc, but operates within the given space. */ DLMALLOC_EXPORT void** mspace_independent_calloc(mspace msp, size_t n_elements, size_t elem_size, void* chunks[]); - + /* mspace_independent_comalloc behaves as independent_comalloc, but operates within the given space. */ DLMALLOC_EXPORT void** mspace_independent_comalloc(mspace msp, size_t n_elements, size_t sizes[], void* chunks[]); - + /* mspace_footprint() returns the number of bytes obtained from the system for this space. */ DLMALLOC_EXPORT size_t mspace_footprint(mspace msp); - + /* mspace_max_footprint() returns the peak number of bytes obtained from the system for this space. */ DLMALLOC_EXPORT size_t mspace_max_footprint(mspace msp); - - + + #if !NO_MALLINFO /* mspace_mallinfo behaves as mallinfo, but reports properties of @@ -1455,31 +1455,31 @@ size_t bulk_free(void**, size_t n_elements) __attribute__((weak, alias("dlbulk_f */ DLMALLOC_EXPORT struct mallinfo mspace_mallinfo(mspace msp); #endif /* NO_MALLINFO */ - + /* malloc_usable_size(void* p) behaves the same as malloc_usable_size; */ DLMALLOC_EXPORT size_t mspace_usable_size(const void* mem); - + /* mspace_malloc_stats behaves as malloc_stats, but reports properties of the given space. */ DLMALLOC_EXPORT void mspace_malloc_stats(mspace msp); - + /* mspace_trim behaves as malloc_trim, but operates within the given space. */ DLMALLOC_EXPORT int mspace_trim(mspace msp, size_t pad); - + /* An alias for mallopt. */ DLMALLOC_EXPORT int mspace_mallopt(int, int); - + #endif /* MSPACES */ - + #ifdef __cplusplus } /* end of extern "C" */ #endif /* __cplusplus */ @@ -1850,7 +1850,7 @@ static FORCEINLINE int win32munmap(void* ptr, size_t size) { /* When locks are defined, there is one global lock, plus one per-mspace lock. - + The global lock_ensures that mparams.magic and other unique mparams values are initialized only once. It also protects sequences of calls to MORECORE. In many cases sys_alloc requires @@ -1858,21 +1858,21 @@ static FORCEINLINE int win32munmap(void* ptr, size_t size) { threads. This does not protect against direct calls to MORECORE by other threads not using this lock, so there is still code to cope the best we can on interference. - + Per-mspace locks surround calls to malloc, free, etc. By default, locks are simple non-reentrant mutexes. - + Because lock-protected regions generally have bounded times, it is OK to use the supplied simple spinlocks. Spinlocks are likely to improve performance for lightly contended applications, but worsen performance under heavy contention. - + If USE_LOCKS is > 1, the definitions of lock routines here are bypassed, in which case you will need to define the type MLOCK_T, and at least INITIAL_LOCK, DESTROY_LOCK, ACQUIRE_LOCK, RELEASE_LOCK and TRY_LOCK. You must also declare a static MLOCK_T malloc_global_mutex = { initialization values };. - + */ #if !USE_LOCKS @@ -2123,11 +2123,11 @@ static int pthread_init_lock (MLOCK_T *lk) { /* (The following includes lightly edited explanations by Colin Plumb.) - + The malloc_chunk declaration below is misleading (but accurate and necessary). It declares a "view" into memory allowing access to necessary fields at known offsets from a given base. - + Chunks of memory are maintained using a `boundary tag' method as originally described by Knuth. (See the paper by Paul Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a survey of such @@ -2135,14 +2135,14 @@ static int pthread_init_lock (MLOCK_T *lk) { each chunk and at the end. This makes consolidating fragmented chunks into bigger chunks fast. The head fields also hold bits representing whether chunks are free or in use. - + Here are some pictures to make it clearer. They are "exploded" to show that the state of a chunk can be thought of as extending from the high 31 bits of the head field of its header through the prev_foot and PINUSE_BIT bit of the following chunk header. - + A chunk that's in use looks like: - + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Size of previous chunk (if P = 0) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -2162,9 +2162,9 @@ static int pthread_init_lock (MLOCK_T *lk) { +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1| | Size of next chunk (may or may not be in use) | +-+ mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - + And if it's free, it looks like this: - + chunk-> +- -+ | User payload (must be in use, or we would have merged!) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -2192,16 +2192,16 @@ static int pthread_init_lock (MLOCK_T *lk) { +-+ Note that since we always merge adjacent free chunks, the chunks adjacent to a free chunk must be in use. - + Given a pointer to a chunk (which can be derived trivially from the payload pointer) we can, in O(1) time, find out whether the adjacent chunks are free, and if so, unlink them from the lists that they are on and merge them with the current chunk. - + Chunks always begin on even word boundaries, so the mem portion (which is returned to the user) is also on an even word boundary, and thus at least double-word aligned. - + The P (PINUSE_BIT) bit, stored in the unused low-order bit of the chunk size (which is always a multiple of two words), is an in-use bit for the *previous* chunk. If that bit is *clear*, then the @@ -2212,13 +2212,13 @@ static int pthread_init_lock (MLOCK_T *lk) { any given chunk, then you CANNOT determine the size of the previous chunk, and might even get a memory addressing fault when trying to do so. - + The C (CINUSE_BIT) bit, stored in the unused second-lowest bit of the chunk size redundantly records whether the current chunk is inuse (unless the chunk is mmapped). This redundancy enables usage checks within free and realloc, and reduces indirection when freeing and consolidating chunks. - + Each freshly allocated chunk must have both cinuse and pinuse set. That is, each allocated chunk borders either a previously allocated and still in-use chunk, or the base of its memory arena. This is @@ -2226,14 +2226,14 @@ static int pthread_init_lock (MLOCK_T *lk) { found chunk. Further, no free chunk physically borders another one, so each free chunk is known to be preceded and followed by either inuse chunks or the ends of memory. - + Note that the `foot' of the current chunk is actually represented as the prev_foot of the NEXT chunk. This makes it easier to deal with alignments etc but can be very confusing when trying to extend or adapt this code. - + The exceptions to all this are - + 1. The special chunk `top' is the top-most available chunk (i.e., the one bordering the end of available memory). It is treated specially. Top is never included in any bin, is used only if @@ -2245,7 +2245,7 @@ static int pthread_init_lock (MLOCK_T *lk) { contiguous chunk that would have to index off it. However, space is still allocated for it (TOP_FOOT_SIZE) to enable separation or merging when space is extended. - + 3. Chunks allocated via mmap, have both cinuse and pinuse bits cleared in their head fields. Because they are allocated one-by-one, each must carry its own prev_foot field, which is @@ -2253,7 +2253,7 @@ static int pthread_init_lock (MLOCK_T *lk) { region, which is needed to preserve alignment. Each mmapped chunk is trailed by the first two fields of a fake next-chunk for sake of usage checks. - + */ struct malloc_chunk { @@ -2314,7 +2314,7 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ The head field of a chunk is or'ed with PINUSE_BIT when previous adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in use, unless mmapped, in which case both bits are cleared. - + FLAG4_BIT is not used by this malloc, but might be useful in extensions. */ @@ -2379,10 +2379,10 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ /* When chunks are not in use, they are treated as nodes of either lists or trees. - + "Small" chunks are stored in circular doubly-linked lists, and look like this: - + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Size of previous chunk | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -2398,12 +2398,12 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ `foot:' | Size of chunk, in bytes | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - + Larger chunks are kept in a form of bitwise digital trees (aka tries) keyed on chunksizes. Because malloc_tree_chunks are only for free chunks greater than 256 bytes, their size doesn't impose any constraints on user chunk sizes. Each node looks like: - + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Size of previous chunk | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -2426,7 +2426,7 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ `foot:' | Size of chunk, in bytes | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - + Each tree holding treenodes is a tree of unique chunk sizes. Chunks of the same size are arranged in a circularly-linked list, with only the oldest chunk (the next to be used, in our FIFO ordering) @@ -2434,14 +2434,14 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ parent pointer.) If a chunk with the same size an an existing node is inserted, it is linked off the existing node using pointers that work in the same way as fd/bk pointers of small chunks. - + Each tree contains a power of 2 sized range of chunk sizes (the smallest is 0x100 <= x < 0x180), which is is divided in half at each tree level, with the chunks in the smaller half of the range (0x100 <= x < 0x140 for the top nose) in the left subtree and the larger half (0x140 <= x < 0x180) in the right subtree. This is, of course, done by inspecting individual bits. - + Using these rules, each node's left subtree contains all smaller sizes than its right subtree. However, the node at the root of each subtree has no particular ordering relationship to either. (The @@ -2449,7 +2449,7 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ If we remove the last chunk of a given size from the interior of the tree, we need to replace it with a leaf node. The tree ordering rules permit a node to be replaced by any leaf below it. - + The smallest chunk in a tree (a common operation in a best-fit allocator) can be found by walking a path to the leftmost leaf in the tree. Unlike a usual binary tree, where we follow left child @@ -2457,7 +2457,7 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ pointer any time the left one is null, until we reach a leaf with both child pointers null. The smallest chunk in the tree will be somewhere along that path. - + The worst case number of steps to add, find, or remove a node is bounded by the number of bits differentiating chunks within bins. Under current bin calculations, this ranges from 6 up to 21 @@ -2471,7 +2471,7 @@ struct malloc_tree_chunk { size_t head; struct malloc_tree_chunk* fd; struct malloc_tree_chunk* bk; - + struct malloc_tree_chunk* child[2]; struct malloc_tree_chunk* parent; bindex_t index; @@ -2493,7 +2493,7 @@ typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */ the space. Large chunks that are directly allocated by mmap are not included in this list. They are instead independently created and destroyed without otherwise keeping track of them. - + Segment management mainly comes into play for spaces allocated by MMAP. Any call to MMAP might or might not return memory that is adjacent to an existing segment. MORECORE normally contiguously @@ -2507,7 +2507,7 @@ typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */ contiguity when we get it. It is probably possible to do better than this on some systems, but no general scheme seems to be significantly better. - + Management entails a simpler variant of the consolidation scheme used for chunks to reduce fragmentation -- new adjacent memory is normally prepended or appended to an existing segment. However, @@ -2515,19 +2515,19 @@ typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */ reflect the fact that segment processing is relatively infrequent (occurring only when getting memory from system) and that we don't expect to have huge numbers of segments: - + * Segments are not indexed, so traversal requires linear scans. (It would be possible to index these, but is not worth the extra overhead and complexity for most programs on most platforms.) * New segments are only appended to old ones when holding top-most memory; if they cannot be prepended to others, they are held in different segments. - + Except for the top-most segment of an mstate, each segment record is kept at the tail of its segment. Segments are added by pushing segment records onto the list headed by &mstate.seg for the containing mstate. - + Segment flags control allocation/merge/deallocation policies: * If EXTERN_BIT set, then we did not allocate this segment, and so should not try to deallocate or merge with others. @@ -2559,7 +2559,7 @@ typedef struct malloc_segment* msegmentptr; /* A malloc_state holds all of the bookkeeping for a space. The main fields are: - + Top The topmost chunk of the currently active segment. Its size is cached in topsize. The actual size of topmost space is @@ -2568,14 +2568,14 @@ typedef struct malloc_segment* msegmentptr; space from the system. The size at which to autotrim top is cached from mparams in trim_check, except that it is disabled if an autotrim fails. - + Designated victim (dv) This is the preferred chunk for servicing small requests that don't have exact fits. It is normally the chunk split off most recently to service another small request. Its size is cached in dvsize. The link fields of this chunk are not maintained since it is not kept in a bin. - + SmallBins An array of bin headers for free chunks. These bins hold chunks with sizes less than MIN_LARGE_SIZE bytes. Each bin contains @@ -2585,13 +2585,13 @@ typedef struct malloc_segment* msegmentptr; itself). This avoids special-casing for headers. But to avoid waste, we allocate only the fd/bk pointers of bins, and then use repositioning tricks to treat these as the fields of a chunk. - + TreeBins Treebins are pointers to the roots of trees holding a range of sizes. There are 2 equally spaced treebins for each power of two from TREE_SHIFT to TREE_SHIFT+16. The last bin holds anything larger. - + Bin maps There is one bit map for small bins ("smallmap") and one for treebins ("treemap). Each bin sets its bit when non-empty, and @@ -2604,38 +2604,38 @@ typedef struct malloc_segment* msegmentptr; supplement at http://hackersdelight.org/). Many of these are intended to reduce the branchiness of paths through malloc etc, as well as to reduce the number of memory locations read or written. - + Segments A list of segments headed by an embedded malloc_segment record representing the initial space. - + Address check support The least_addr field is the least address ever obtained from MORECORE or MMAP. Attempted frees and reallocs of any address less than this are trapped (unless INSECURE is defined). - + Magic tag A cross-check field that should always hold same value as mparams.magic. - + Max allowed footprint The maximum allowed bytes to allocate from system (zero means no limit) - + Flags Bits recording whether to use MMAP, locks, or contiguous MORECORE - + Statistics Each space keeps track of current and maximum system memory obtained via MORECORE or MMAP. - + Trim support Fields holding the amount of unused topmost memory that should trigger trimming, and a counter to force periodic scanning to release unused non-topmost segments. - + Locking If USE_LOCKS is defined, the "mutex" lock is acquired and released around every public call using this mspace. - + Extension support A void* pointer and a size_t field that can be used to help implement extensions to this malloc. @@ -3062,7 +3062,7 @@ I = (bindex_t)(N + Y);\ check all of those linked or offsetted from other embedded data structures. These checks are interspersed with main code in a way that tends to minimize their run-time cost. - + When FOOTERS is defined, in addition to range checking, we also verify footer fields of inuse chunks, which can be used guarantee that the mstate controlling malloc/free is intact. This is a @@ -3176,13 +3176,13 @@ static int init_mparams(void) { if (malloc_global_mutex_status <= 0) init_malloc_global_mutex(); #endif - + ACQUIRE_MALLOC_GLOBAL_LOCK(); if (mparams.magic == 0) { size_t magic; size_t psize; size_t gsize; - + #ifndef WIN32 psize = malloc_getpagesize; gsize = ((DEFAULT_GRANULARITY != 0)? DEFAULT_GRANULARITY : psize); @@ -3195,7 +3195,7 @@ static int init_mparams(void) { DEFAULT_GRANULARITY : system_info.dwAllocationGranularity); } #endif /* WIN32 */ - + /* Sanity-check configuration: size_t must be unsigned and as wide as pointer type. ints must be at least 4 bytes. @@ -3220,7 +3220,7 @@ static int init_mparams(void) { #else /* MORECORE_CONTIGUOUS */ mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT; #endif /* MORECORE_CONTIGUOUS */ - + #if !ONLY_MSPACES /* Set up lock for main malloc area */ gm->mflags = mparams.default_mflags; @@ -3229,7 +3229,7 @@ static int init_mparams(void) { #if LOCK_AT_FORK pthread_atfork(&pre_fork, &post_fork_parent, &post_fork_child); #endif - + { #if USE_DEV_RANDOM int fd; @@ -3255,7 +3255,7 @@ static int init_mparams(void) { (*(volatile size_t *)(&(mparams.magic))) = magic; } } - + RELEASE_MALLOC_GLOBAL_LOCK(); return 1; } @@ -3381,7 +3381,7 @@ static void do_check_tree(mstate m, tchunkptr t) { assert(tsize >= MIN_LARGE_SIZE); assert(tsize >= minsize_for_tree_index(idx)); assert((idx == NTREEBINS-1) || (tsize < minsize_for_tree_index((idx+1)))); - + do { /* traverse through chain of same-sized nodes */ do_check_any_chunk(m, ((mchunkptr)u)); assert(u->index == tindex); @@ -3532,21 +3532,21 @@ static void do_check_malloc_state(mstate m) { do_check_smallbin(m, i); for (i = 0; i < NTREEBINS; ++i) do_check_treebin(m, i); - + if (m->dvsize != 0) { /* check dv chunk */ do_check_any_chunk(m, m->dv); assert(m->dvsize == chunksize(m->dv)); assert(m->dvsize >= MIN_CHUNK_SIZE); assert(bin_find(m, m->dv) == 0); } - + if (m->top != 0) { /* check top chunk */ do_check_top_chunk(m, m->top); /*assert(m->topsize == chunksize(m->top)); redundant */ assert(m->topsize > 0); assert(bin_find(m, m->top) == 0); } - + total = traverse_and_check(m); assert(total <= m->footprint); assert(m->footprint <= m->max_footprint); @@ -3580,7 +3580,7 @@ static struct mallinfo internal_mallinfo(mstate m) { } s = s->next; } - + nm.arena = sum; nm.ordblks = nfree; nm.hblkhd = m->footprint - sum; @@ -3589,7 +3589,7 @@ static struct mallinfo internal_mallinfo(mstate m) { nm.fordblks = mfree; nm.keepcost = m->topsize; } - + POSTACTION(m); } return nm; @@ -3609,7 +3609,7 @@ static void internal_malloc_stats(mstate m) { maxfp = m->max_footprint; fp = m->footprint; used = fp - (m->topsize + TOP_FOOT_SIZE); - + while (s != 0) { mchunkptr q = align_as_chunk(s->base); while (segment_holds(s, q) && @@ -3770,7 +3770,7 @@ break;\ /* Unlink steps: - + 1. If x is a chained node, unlink it from its same-sized fd/bk links and choose its bk node as its replacement. 2. If x was the last node of its size, but not a leaf node, it must @@ -3913,7 +3913,7 @@ static void* mmap_alloc(mstate m, size_t nb) { mark_inuse_foot(m, p, psize); chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0; - + if (m->least_addr == 0 || mm < m->least_addr) m->least_addr = mm; if ((m->footprint += mmsize) > m->max_footprint) @@ -3949,7 +3949,7 @@ static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb, int flags) { mark_inuse_foot(m, newp, psize); chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0; - + if (cp < m->least_addr) m->least_addr = cp; if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint) @@ -3970,7 +3970,7 @@ static void init_top(mstate m, mchunkptr p, size_t psize) { size_t offset = align_offset(chunk2mem(p)); p = (mchunkptr)((char*)p + offset); psize -= offset; - + m->top = p; m->topsize = psize; p->head = psize | PINUSE_BIT; @@ -4017,11 +4017,11 @@ static void* prepend_alloc(mstate m, char* newbase, char* oldbase, mchunkptr q = chunk_plus_offset(p, nb); size_t qsize = psize - nb; set_size_and_pinuse_of_inuse_chunk(m, p, nb); - + assert((char*)oldfirst > (char*)q); assert(pinuse(oldfirst)); assert(qsize >= MIN_CHUNK_SIZE); - + /* consolidate remainder with first chunk of old base */ if (oldfirst == m->top) { size_t tsize = m->topsize += qsize; @@ -4045,7 +4045,7 @@ static void* prepend_alloc(mstate m, char* newbase, char* oldbase, insert_chunk(m, q, qsize); check_free_chunk(m, q); } - + check_malloced_chunk(m, chunk2mem(p), nb); return chunk2mem(p); } @@ -4066,10 +4066,10 @@ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { mchunkptr tnext = chunk_plus_offset(sp, ssize); mchunkptr p = tnext; int nfences = 0; - + /* reset top to new space */ init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); - + /* Set up segment record */ assert(is_aligned(ss)); set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); @@ -4078,7 +4078,7 @@ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { m->seg.size = tsize; m->seg.sflags = mmapped; m->seg.next = ss; - + /* Insert trailing fenceposts */ for (;;) { mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); @@ -4090,7 +4090,7 @@ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { break; } assert(nfences >= 2); - + /* Insert the rest of old top into a bin as an ordinary free chunk */ if (csp != old_top) { mchunkptr q = (mchunkptr)old_top; @@ -4099,7 +4099,7 @@ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { set_free_with_pinuse(q, psize, tn); insert_chunk(m, q, psize); } - + check_top_chunk(m, m->top); } @@ -4111,16 +4111,16 @@ static void* sys_alloc(mstate m, size_t nb) { size_t tsize = 0; flag_t mmap_flag = 0; size_t asize; /* allocation size */ - + ensure_initialization(); - + /* Directly map large chunks, but only if already initialized */ if (use_mmap(m) && nb >= mparams.mmap_threshold && m->topsize != 0) { void* mem = mmap_alloc(m, nb); if (mem != 0) return mem; } - + asize = granularity_align(nb + SYS_ALLOC_PADDING); if (asize <= nb) return 0; /* wraparound */ @@ -4129,7 +4129,7 @@ static void* sys_alloc(mstate m, size_t nb) { if (fp <= m->footprint || fp > m->footprint_limit) return 0; } - + /* Try getting memory in any of three ways (in most-preferred to least-preferred order): @@ -4145,19 +4145,19 @@ static void* sys_alloc(mstate m, size_t nb) { find space. 3. A call to MORECORE that cannot usually contiguously extend memory. (disabled if not HAVE_MORECORE) - + In all cases, we need to request enough bytes from system to ensure we can malloc nb bytes upon success, so pad with enough space for top_foot, plus alignment-pad to make sure we don't lose bytes if not on boundary, and round this up to a granularity unit. */ - + if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) { char* br = CMFAIL; size_t ssize = asize; /* sbrk call size */ msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top); ACQUIRE_MALLOC_GLOBAL_LOCK(); - + if (ss == 0) { /* First time through or recovery */ char* base = (char*)CALL_MORECORE(0); if (base != CMFAIL) { @@ -4185,7 +4185,7 @@ static void* sys_alloc(mstate m, size_t nb) { tsize = ssize; } } - + if (tbase == CMFAIL) { /* Cope with partial failure */ if (br != CMFAIL) { /* Try to use/extend the space we did get */ if (ssize < HALF_MAX_SIZE_T && @@ -4209,10 +4209,10 @@ static void* sys_alloc(mstate m, size_t nb) { else disable_contiguous(m); /* Don't try contiguous path in the future */ } - + RELEASE_MALLOC_GLOBAL_LOCK(); } - + if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */ char* mp = (char*)(CALL_MMAP(asize)); if (mp != CMFAIL) { @@ -4221,7 +4221,7 @@ static void* sys_alloc(mstate m, size_t nb) { mmap_flag = USE_MMAP_BIT; } } - + if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ if (asize < HALF_MAX_SIZE_T) { char* br = CMFAIL; @@ -4239,12 +4239,12 @@ static void* sys_alloc(mstate m, size_t nb) { } } } - + if (tbase != CMFAIL) { - + if ((m->footprint += tsize) > m->max_footprint) m->max_footprint = m->footprint; - + if (!is_initialized(m)) { /* first-time initialization */ if (m->least_addr == 0 || tbase < m->least_addr) m->least_addr = tbase; @@ -4265,7 +4265,7 @@ static void* sys_alloc(mstate m, size_t nb) { init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) -TOP_FOOT_SIZE); } } - + else { /* Try to merge with an existing segment */ msegmentptr sp = &m->seg; @@ -4297,7 +4297,7 @@ static void* sys_alloc(mstate m, size_t nb) { add_segment(m, tbase, tsize, mmap_flag); } } - + if (nb < m->topsize) { /* Allocate from new or extended top space */ size_t rsize = m->topsize -= nb; mchunkptr p = m->top; @@ -4309,7 +4309,7 @@ static void* sys_alloc(mstate m, size_t nb) { return chunk2mem(p); } } - + MALLOC_FAILURE_ACTION; return 0; } @@ -4369,14 +4369,14 @@ static int sys_trim(mstate m, size_t pad) { ensure_initialization(); if (pad < MAX_REQUEST && is_initialized(m)) { pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ - + if (m->topsize > pad) { /* Shrink top space in granularity-size units, keeping at least one */ size_t unit = mparams.granularity; size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - SIZE_T_ONE) * unit; msegmentptr sp = segment_holding(m, (char*)m->top); - + if (!is_extern_segment(sp)) { if (is_mmapped_segment(sp)) { if (HAVE_MMAP && @@ -4408,7 +4408,7 @@ static int sys_trim(mstate m, size_t pad) { RELEASE_MALLOC_GLOBAL_LOCK(); } } - + if (released != 0) { sp->size -= released; m->footprint -= released; @@ -4416,16 +4416,16 @@ static int sys_trim(mstate m, size_t pad) { check_top_chunk(m, m->top); } } - + /* Unmap any unused mmapped segments */ if (HAVE_MMAP) released += release_unused_segments(m); - + /* On failure, disable autotrim to avoid repeated failed future calls */ if (released == 0 && m->topsize > m->trim_check) m->trim_check = MAX_SIZE_T; } - + return (released != 0)? 1 : 0; } @@ -4541,7 +4541,7 @@ static void* tmalloc_large(mstate m, size_t nb) { t = *treebin_at(m, i); } } - + while (t != 0) { /* find smallest of tree or subtree */ size_t trem = chunksize(t) - nb; if (trem < rsize) { @@ -4550,7 +4550,7 @@ static void* tmalloc_large(mstate m, size_t nb) { } t = leftmost_child(t); } - + /* If dv is a better fit, return 0 so malloc will use it */ if (v != 0 && rsize < (size_t)(m->dvsize - nb)) { if (RTCHECK(ok_address(m, v))) { /* split */ @@ -4582,7 +4582,7 @@ static void* tmalloc_small(mstate m, size_t nb) { compute_bit2idx(leastbit, i); v = t = *treebin_at(m, i); rsize = chunksize(t) - nb; - + while ((t = leftmost_child(t)) != 0) { size_t trem = chunksize(t) - nb; if (trem < rsize) { @@ -4590,7 +4590,7 @@ static void* tmalloc_small(mstate m, size_t nb) { v = t; } } - + if (RTCHECK(ok_address(m, v))) { mchunkptr r = chunk_plus_offset(v, nb); assert(chunksize(v) == rsize + nb); @@ -4606,7 +4606,7 @@ static void* tmalloc_small(mstate m, size_t nb) { return chunk2mem(v); } } - + CORRUPTION_ERROR_ACTION(m); return 0; } @@ -4633,14 +4633,14 @@ void* dlmalloc(size_t bytes) { 3. If it is big enough, use the top chunk. 4. If request size >= mmap threshold, try to directly mmap this chunk. 5. If available, get memory from system and use it - + The ugly goto's here ensure that postaction occurs along all paths. */ - + #if USE_LOCKS ensure_initialization(); /* initialize in sys_alloc if not using locks */ #endif - + if (!PREACTION(gm)) { void* mem; size_t nb; @@ -4650,7 +4650,7 @@ void* dlmalloc(size_t bytes) { nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); idx = small_index(nb); smallbits = gm->smallmap >> idx; - + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ mchunkptr b, p; idx += ~smallbits & 1; /* Uses next bin if idx empty */ @@ -4663,7 +4663,7 @@ void* dlmalloc(size_t bytes) { check_malloced_chunk(gm, mem, nb); goto postaction; } - + else if (nb > gm->dvsize) { if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ mchunkptr b, p, r; @@ -4690,7 +4690,7 @@ void* dlmalloc(size_t bytes) { check_malloced_chunk(gm, mem, nb); goto postaction; } - + else if (gm->treemap != 0 && (mem = tmalloc_small(gm, nb)) != 0) { check_malloced_chunk(gm, mem, nb); goto postaction; @@ -4706,7 +4706,7 @@ void* dlmalloc(size_t bytes) { goto postaction; } } - + if (nb <= gm->dvsize) { size_t rsize = gm->dvsize - nb; mchunkptr p = gm->dv; @@ -4726,7 +4726,7 @@ void* dlmalloc(size_t bytes) { check_malloced_chunk(gm, mem, nb); goto postaction; } - + else if (nb < gm->topsize) { /* Split top */ size_t rsize = gm->topsize -= nb; mchunkptr p = gm->top; @@ -4738,9 +4738,9 @@ void* dlmalloc(size_t bytes) { check_malloced_chunk(gm, mem, nb); goto postaction; } - + mem = sys_alloc(gm, nb); - + postaction: POSTACTION(gm); #if __EMSCRIPTEN__ @@ -4749,7 +4749,7 @@ void* dlmalloc(size_t bytes) { #endif return mem; } - + return 0; } @@ -4761,7 +4761,7 @@ void dlfree(void* mem) { free chunks, if they exist, and then place in a bin. Intermixed with special cases for top, dv, mmapped chunks, and usage errors. */ - + if (mem != 0) { #if __EMSCRIPTEN__ /* XXX Emscripten Tracing API. */ @@ -4808,7 +4808,7 @@ void dlfree(void* mem) { goto erroraction; } } - + if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { if (!cinuse(next)) { /* consolidate forward */ if (next == fm->top) { @@ -4842,7 +4842,7 @@ void dlfree(void* mem) { } else set_free_with_pinuse(p, psize, next); - + if (is_small(psize)) { insert_small_chunk(fm, p, psize); check_free_chunk(fm, p); @@ -5006,7 +5006,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { mchunkptr newp = (mchunkptr)pos; size_t leadsize = pos - (char*)(p); size_t newsize = chunksize(p) - leadsize; - + if (is_mmapped(p)) { /* For mmapped chunks, just adjust offset */ newp->prev_foot = p->prev_foot + leadsize; newp->head = newsize; @@ -5018,7 +5018,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { } p = newp; } - + /* Give back spare room at the end */ if (!is_mmapped(p)) { size_t size = chunksize(p); @@ -5030,7 +5030,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { dispose_chunk(m, remainder, remainder_size); } } - + mem = chunk2mem(p); assert (chunksize(p) >= nb); assert(((size_t)mem & (alignment - 1)) == 0); @@ -5053,7 +5053,7 @@ static void** ialloc(mstate m, size_t* sizes, int opts, void* chunks[]) { - + size_t element_size; /* chunksize of each element, if all same */ size_t contents_size; /* total size of elements */ size_t array_size; /* request size of pointer array */ @@ -5065,7 +5065,7 @@ static void** ialloc(mstate m, flag_t was_enabled; /* to disable mmap */ size_t size; size_t i; - + ensure_initialization(); /* compute array length, if needed */ if (chunks != 0) { @@ -5081,7 +5081,7 @@ static void** ialloc(mstate m, marray = 0; array_size = request2size(n_elements * (sizeof(void*))); } - + /* compute total element size */ if (opts & 0x1) { /* all-same-size */ element_size = request2size(*sizes); @@ -5093,9 +5093,9 @@ static void** ialloc(mstate m, for (i = 0; i != n_elements; ++i) contents_size += request2size(sizes[i]); } - + size = contents_size + array_size; - + /* Allocate the aggregate chunk. First disable direct-mmapping so malloc won't use it, since we would not be able to later @@ -5108,17 +5108,17 @@ static void** ialloc(mstate m, enable_mmap(m); if (mem == 0) return 0; - + if (PREACTION(m)) return 0; p = mem2chunk(mem); remainder_size = chunksize(p); - + assert(!is_mmapped(p)); - + if (opts & 0x2) { /* optionally clear the elements */ memset((size_t*)mem, 0, remainder_size - SIZE_T_SIZE - array_size); } - + /* If not provided, allocate the pointer array as final part of chunk */ if (marray == 0) { size_t array_chunk_size; @@ -5128,7 +5128,7 @@ static void** ialloc(mstate m, set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size); remainder_size = contents_size; } - + /* split out elements */ for (i = 0; ; ++i) { marray[i] = chunk2mem(p); @@ -5146,7 +5146,7 @@ static void** ialloc(mstate m, break; } } - + #if DEBUG if (marray != chunks) { /* final element must have exactly exhausted chunk */ @@ -5160,9 +5160,9 @@ static void** ialloc(mstate m, } for (i = 0; i != n_elements; ++i) check_inuse_chunk(m, mem2chunk(marray[i])); - + #endif /* DEBUG */ - + POSTACTION(m); return marray; } @@ -5600,7 +5600,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); idx = small_index(nb); smallbits = ms->smallmap >> idx; - + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ mchunkptr b, p; idx += ~smallbits & 1; /* Uses next bin if idx empty */ @@ -5613,7 +5613,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { check_malloced_chunk(ms, mem, nb); goto postaction; } - + else if (nb > ms->dvsize) { if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ mchunkptr b, p, r; @@ -5640,7 +5640,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { check_malloced_chunk(ms, mem, nb); goto postaction; } - + else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { check_malloced_chunk(ms, mem, nb); goto postaction; @@ -5656,7 +5656,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { goto postaction; } } - + if (nb <= ms->dvsize) { size_t rsize = ms->dvsize - nb; mchunkptr p = ms->dv; @@ -5676,7 +5676,7 @@ void* mspace_malloc(mspace msp, size_t bytes) { check_malloced_chunk(ms, mem, nb); goto postaction; } - + else if (nb < ms->topsize) { /* Split top */ size_t rsize = ms->topsize -= nb; mchunkptr p = ms->top; @@ -5688,14 +5688,14 @@ void* mspace_malloc(mspace msp, size_t bytes) { check_malloced_chunk(ms, mem, nb); goto postaction; } - + mem = sys_alloc(ms, nb); - + postaction: POSTACTION(ms); return mem; } - + return 0; } @@ -5743,7 +5743,7 @@ void mspace_free(mspace msp, void* mem) { goto erroraction; } } - + if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { if (!cinuse(next)) { /* consolidate forward */ if (next == fm->top) { @@ -5777,7 +5777,7 @@ void mspace_free(mspace msp, void* mem) { } else set_free_with_pinuse(p, psize, next); - + if (is_small(psize)) { insert_small_chunk(fm, p, psize); check_free_chunk(fm, p); @@ -6075,7 +6075,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme /* Guidelines for creating a custom version of MORECORE: - + * For best performance, MORECORE should allocate in multiples of pagesize. * MORECORE may allocate more memory than requested. (Or even less, but this will usually result in a malloc failure.) @@ -6094,29 +6094,29 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme must not misinterpret negative args as large positive unsigned args. You can suppress all such calls from even occurring by defining MORECORE_CANNOT_TRIM, - + As an example alternative MORECORE, here is a custom allocator kindly contributed for pre-OSX macOS. It uses virtually but not necessarily physically contiguous non-paged memory (locked in, present and won't get swapped out). You can use it by uncommenting this section, adding some #includes, and setting up the appropriate defines above: - + #define MORECORE osMoreCore - + There is also a shutdown routine that should somehow be called for cleanup upon program exit. - + #define MAX_POOL_ENTRIES 100 #define MINIMUM_MORECORE_SIZE (64 * 1024U) static int next_os_pool; void *our_os_pools[MAX_POOL_ENTRIES]; - + void *osMoreCore(int size) { void *ptr = 0; static void *sbrk_top = 0; - + if (size > 0) { if (size < MINIMUM_MORECORE_SIZE) @@ -6144,14 +6144,14 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme return sbrk_top; } } - + // cleanup any allocated memory pools // called as last thing before shutting down driver - + void osCleanupMem(void) { void **ptr; - + for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++) if (*ptr) { @@ -6159,7 +6159,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme *ptr = 0; } } - + */ @@ -6170,7 +6170,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * don't reuse adjusted asize in sys_alloc * add LOCK_AT_FORK -- thanks to Kirill Artamonov for the suggestion * reduce compiler warnings -- thanks to all who reported/suggested these - + v2.8.5 Sun May 22 10:26:02 2011 Doug Lea (dl at gee) * Always perform unlink checks unless INSECURE * Add posix_memalign. @@ -6184,10 +6184,10 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Small fixes to mspace_destroy, reset_on_error. * Various configuration extensions/changes. Thanks to all who contributed these. - + V2.8.4a Thu Apr 28 14:39:43 2011 (dl at gee.cs.oswego.edu) * Update Creative Commons URL - + V2.8.4 Wed May 27 09:56:23 2009 Doug Lea (dl at gee) * Use zeros instead of prev foot for is_mmapped * Add mspace_track_large_chunks; thanks to Jean Brouwers @@ -6202,7 +6202,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Various small adjustments to reduce warnings on some compilers * Various configuration extensions/changes for more platforms. Thanks to all who contributed these. - + V2.8.3 Thu Sep 22 11:16:32 2005 Doug Lea (dl at gee) * Add max_footprint functions * Ensure all appropriate literals are size_t @@ -6215,14 +6215,14 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Simplify and fix segment insertion, trimming and mspace_destroy * Reinstate REALLOC_ZERO_BYTES_FREES option from 2.7.x * Thanks especially to Dennis Flanagan for help on these. - + V2.8.2 Sun Jun 12 16:01:10 2005 Doug Lea (dl at gee) * Fix memalign brace error. - + V2.8.1 Wed Jun 8 16:11:46 2005 Doug Lea (dl at gee) * Fix improper #endif nesting in C++ * Add explicit casts needed for C++ - + V2.8.0 Mon May 30 14:09:02 2005 Doug Lea (dl at gee) * Use trees for large bins * Support mspaces @@ -6238,10 +6238,10 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Remove useless cfree() to avoid conflicts with other apps. * Remove internal memcpy, memset. Compilers handle builtins better. * Remove some options that no one ever used and rename others. - + V2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee) * Fix malloc_state bitmap array misdeclaration - + V2.7.1 Thu Jul 25 10:58:03 2002 Doug Lea (dl at gee) * Allow tuning of FIRST_SORTED_BIN_SIZE * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte. @@ -6254,7 +6254,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield. * Branch-free bin calculation * Default trim and mmap thresholds now 256K. - + V2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee) * Introduce independent_comalloc and independent_calloc. Thanks to Michael Pachos for motivation and help. @@ -6278,7 +6278,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS Thanks to Tony E. Bennett and others. * Include errno.h to support default failure action. - + V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee) * return null for negative arguments * Added Several WIN32 cleanups from Martin C. Fong @@ -6294,10 +6294,10 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to avoid infinite loop * Always call 'fREe()' rather than 'free()' - + V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee) * Fixed ordering problem with boundary-stamping - + V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee) * Added pvalloc, as recommended by H.J. Liu * Added 64bit pointer support mainly from Wolfram Gloger @@ -6306,7 +6306,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * malloc_extend_top: fix mask error that caused wastage after foreign sbrks * Add linux mremap support code from HJ Liu - + V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee) * Integrated most documentation with the code. * Add support for mmap, with help from @@ -6327,7 +6327,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Added macros etc., allowing use in linux libc from H.J. Lu (hjl@gnu.ai.mit.edu) * Inverted this history list - + V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee) * Re-tuned and fixed to behave more nicely with V2.6.0 changes. * Removed all preallocation code since under current scheme @@ -6338,17 +6338,17 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme given above changes. * Use best fit for very large chunks to prevent some worst-cases. * Added some support for debugging - + V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee) * Removed footers when chunks are in use. Thanks to Paul Wilson (wilson@cs.texas.edu) for the suggestion. - + V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee) * Added malloc_trim, with help from Wolfram Gloger (wmglo@Dent.MED.Uni-Muenchen.DE). - + V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g) - + V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g) * realloc: try to expand in both directions * malloc: swap order of clean-bin strategy; @@ -6357,7 +6357,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Use bin counts as a guide to preallocation * Occasionally bin return list chunks in first scan * Add a few optimizations from colin@nyx10.cs.du.edu - + V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g) * faster bin computation & slightly different binning * merged all consolidations to one part of malloc proper @@ -6366,7 +6366,7 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * Propagate failure in realloc if malloc returns 0 * Add stuff to allow compilation on non-ANSI compilers from kpv@research.att.com - + V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu) * removed potential for odd address access in prev_chunk * removed dependency on getpagesize.h @@ -6375,9 +6375,9 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme * tested on sparc, hp-700, dec-mips, rs6000 with gcc & native cc (hp, dec only) allowing Detlefs & Zorn comparison study (in SIGPLAN Notices.) - + Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu) * Based loosely on libg++-1.2X malloc. (It retains some of the overall structure of old version, but most details differ.) - + */ From 8db6d3d2d9f431ac55ad11fde0c88de8a88cda43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Cant=C3=B3n=20Cort=C3=A9s?= Date: Thu, 13 Oct 2022 20:53:23 +0200 Subject: [PATCH 03/10] Added UNSIGNED_MORECORE option. Take it into account when trimming. --- system/lib/dlmalloc.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index 079c1d1918b3f..050281fb84171 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -6,6 +6,8 @@ #define DLMALLOC_EXPORT static /* mmap uses malloc, so malloc can't use mmap */ #define HAVE_MMAP 0 +/* Emscripten's sbrk can interpret unsigned values greater than (MAX_SIZE_T / 2U) (2GB) correctly */ +#define UNSIGNED_MORECORE 1 /* we can only grow the heap up anyhow, so don't try to trim */ #define MORECORE_CANNOT_TRIM 1 #ifndef DLMALLOC_DEBUG @@ -415,7 +417,11 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); Setting it false when definitely non-contiguous saves time and possibly wasted space it would take to discover this though. - MORECORE_CANNOT_TRIM default: NOT defined + UNSIGNED_MORECORE default: 0 (false) + True if MORECORE can only handle unsigned arguments. This sets + MORECORE_CANNOT_TRIM to 1 (true). + + MORECORE_CANNOT_TRIM default: 0 (false) True if MORECORE cannot release space back to the system when given negative arguments. This is generally necessary only if you are using a hand-crafted MORECORE function that cannot handle negative @@ -713,6 +719,14 @@ defined(__i386__) || defined(__x86_64__))) || \ #define HAVE_MORECORE 1 #endif /* ONLY_MSPACES */ #endif /* HAVE_MORECORE */ +#ifndef UNSIGNED_MORECORE +#define UNSIGNED_MORECORE 0 +#endif /* UNSIGNED_MORECORE */ +#if UNSIGNED_MORECORE +#ifndef MORECORE_CANNOT_TRIM +#define MORECORE_CANNOT_TRIM 1 +#endif /* MORECORE_CANNOT_TRIM */ +#endif /* UNSIGNED_MORECORE */ #if !HAVE_MORECORE #define MORECORE_CONTIGUOUS 0 #else /* !HAVE_MORECORE */ @@ -729,7 +743,7 @@ defined(__i386__) || defined(__x86_64__))) || \ #endif /* MORECORE_CONTIGUOUS */ #endif /* DEFAULT_GRANULARITY */ #ifndef DEFAULT_TRIM_THRESHOLD -#ifndef MORECORE_CANNOT_TRIM +#if !MORECORE_CANNOT_TRIM #define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U) #else /* MORECORE_CANNOT_TRIM */ #define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T @@ -1678,12 +1692,7 @@ extern size_t getpagesize(); #define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1) #define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2) #define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES) -#if __EMSCRIPTEN__ -/* Emscripten's sbrk can interpret unsigned values greater than (MAX_SIZE_T / 2U) (2GB) correctly */ -#define HALF_MAX_SIZE_T (MAX_SIZE_T) -#else #define HALF_MAX_SIZE_T (MAX_SIZE_T / 2U) -#endif /* The bit mask value corresponding to MALLOC_ALIGNMENT */ #define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE) @@ -2791,7 +2800,7 @@ static int has_segment_link(mstate m, msegmentptr ss) { } } -#ifndef MORECORE_CANNOT_TRIM +#if !MORECORE_CANNOT_TRIM #define should_trim(M,s) ((s) > (M)->trim_check) #else /* MORECORE_CANNOT_TRIM */ #define should_trim(M,s) (0) @@ -4166,7 +4175,8 @@ static void* sys_alloc(mstate m, size_t nb) { if (!is_page_aligned(base)) ssize += (page_align((size_t)base) - (size_t)base); fp = m->footprint + ssize; /* recheck limits */ - if (ssize > nb && ssize < HALF_MAX_SIZE_T && + if (ssize > nb && + (UNSIGNED_MORECORE || ssize < HALF_MAX_SIZE_T) && (m->footprint_limit == 0 || (fp > m->footprint && fp <= m->footprint_limit)) && (br = (char*)(CALL_MORECORE(ssize))) == base) { @@ -4179,7 +4189,7 @@ static void* sys_alloc(mstate m, size_t nb) { /* Subtract out existing available top space from MORECORE request. */ ssize = granularity_align(nb - m->topsize + SYS_ALLOC_PADDING); /* Use mem here only if it did continuously extend old space */ - if (ssize < HALF_MAX_SIZE_T && + if ((UNSIGNED_MORECORE || ssize < HALF_MAX_SIZE_T) && (br = (char*)(CALL_MORECORE(ssize))) == ss->base+ss->size) { tbase = br; tsize = ssize; @@ -4188,10 +4198,10 @@ static void* sys_alloc(mstate m, size_t nb) { if (tbase == CMFAIL) { /* Cope with partial failure */ if (br != CMFAIL) { /* Try to use/extend the space we did get */ - if (ssize < HALF_MAX_SIZE_T && + if ((UNSIGNED_MORECORE || ssize < HALF_MAX_SIZE_T) && ssize < nb + SYS_ALLOC_PADDING) { size_t esize = granularity_align(nb + SYS_ALLOC_PADDING - ssize); - if (esize < HALF_MAX_SIZE_T) { + if (UNSIGNED_MORECORE || esize < HALF_MAX_SIZE_T) { char* end = (char*)CALL_MORECORE(esize); if (end != CMFAIL) ssize += esize; @@ -4223,7 +4233,7 @@ static void* sys_alloc(mstate m, size_t nb) { } if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ - if (asize < HALF_MAX_SIZE_T) { + if (UNSIGNED_MORECORE || asize < HALF_MAX_SIZE_T) { char* br = CMFAIL; char* end = CMFAIL; ACQUIRE_MALLOC_GLOBAL_LOCK(); @@ -4391,7 +4401,7 @@ static int sys_trim(mstate m, size_t pad) { } } } - else if (HAVE_MORECORE) { + else if (HAVE_MORECORE && !UNSIGNED_MORECORE) { if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */ extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit; ACQUIRE_MALLOC_GLOBAL_LOCK(); @@ -6092,8 +6102,8 @@ extern __typeof(memalign) emscripten_builtin_memalign __attribute__((alias("dlme just return MFAIL when given negative arguments. Negative arguments are always multiples of pagesize. MORECORE must not misinterpret negative args as large positive unsigned - args. You can suppress all such calls from even occurring by defining - MORECORE_CANNOT_TRIM, + args unless UNSIGNED_MORECORE is defined. You can suppress all such calls + from even occurring by defining MORECORE_CANNOT_TRIM, As an example alternative MORECORE, here is a custom allocator kindly contributed for pre-OSX macOS. It uses virtually but not From 27260ba26ee3ab833248f2d983c1ee46e5cef922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Cant=C3=B3n=20Cort=C3=A9s?= Date: Thu, 13 Oct 2022 20:58:16 +0200 Subject: [PATCH 04/10] MORECORE_CANNOT_TRIM should always be defined. --- system/lib/dlmalloc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index 050281fb84171..39dee1a7d4be3 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -726,6 +726,10 @@ defined(__i386__) || defined(__x86_64__))) || \ #ifndef MORECORE_CANNOT_TRIM #define MORECORE_CANNOT_TRIM 1 #endif /* MORECORE_CANNOT_TRIM */ +#else /* !UNSIGNED_MORECORE */ +#ifndef MORECORE_CANNOT_TRIM +#define MORECORE_CANNOT_TRIM 0 +#endif /* MORECORE_CANNOT_TRIM */ #endif /* UNSIGNED_MORECORE */ #if !HAVE_MORECORE #define MORECORE_CONTIGUOUS 0 From b23e891076848ef613530e6b7f52d6dd1cbda94b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Cant=C3=B3n=20Cort=C3=A9s?= Date: Thu, 13 Oct 2022 20:59:55 +0200 Subject: [PATCH 05/10] Unconditionally set MORECORE_CANNOT_TRIM to 1 when UNSIGNED_MORECORE == 0 --- system/lib/dlmalloc.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index 39dee1a7d4be3..8b16f2058362d 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -723,9 +723,7 @@ defined(__i386__) || defined(__x86_64__))) || \ #define UNSIGNED_MORECORE 0 #endif /* UNSIGNED_MORECORE */ #if UNSIGNED_MORECORE -#ifndef MORECORE_CANNOT_TRIM #define MORECORE_CANNOT_TRIM 1 -#endif /* MORECORE_CANNOT_TRIM */ #else /* !UNSIGNED_MORECORE */ #ifndef MORECORE_CANNOT_TRIM #define MORECORE_CANNOT_TRIM 0 From 9125882ab680763b8ac86ae4ce5a753158c2c8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Cant=C3=B3n=20Cort=C3=A9s?= Date: Fri, 14 Oct 2022 13:52:57 +0200 Subject: [PATCH 06/10] Remove unneeded changes --- system/lib/dlmalloc.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index 8b16f2058362d..9bddb33956df7 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -421,7 +421,7 @@ _Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); True if MORECORE can only handle unsigned arguments. This sets MORECORE_CANNOT_TRIM to 1 (true). - MORECORE_CANNOT_TRIM default: 0 (false) + MORECORE_CANNOT_TRIM default: NOT defined True if MORECORE cannot release space back to the system when given negative arguments. This is generally necessary only if you are using a hand-crafted MORECORE function that cannot handle negative @@ -724,10 +724,6 @@ defined(__i386__) || defined(__x86_64__))) || \ #endif /* UNSIGNED_MORECORE */ #if UNSIGNED_MORECORE #define MORECORE_CANNOT_TRIM 1 -#else /* !UNSIGNED_MORECORE */ -#ifndef MORECORE_CANNOT_TRIM -#define MORECORE_CANNOT_TRIM 0 -#endif /* MORECORE_CANNOT_TRIM */ #endif /* UNSIGNED_MORECORE */ #if !HAVE_MORECORE #define MORECORE_CONTIGUOUS 0 @@ -745,7 +741,7 @@ defined(__i386__) || defined(__x86_64__))) || \ #endif /* MORECORE_CONTIGUOUS */ #endif /* DEFAULT_GRANULARITY */ #ifndef DEFAULT_TRIM_THRESHOLD -#if !MORECORE_CANNOT_TRIM +#ifndef MORECORE_CANNOT_TRIM #define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U) #else /* MORECORE_CANNOT_TRIM */ #define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T @@ -2802,7 +2798,7 @@ static int has_segment_link(mstate m, msegmentptr ss) { } } -#if !MORECORE_CANNOT_TRIM +#ifndef MORECORE_CANNOT_TRIM #define should_trim(M,s) ((s) > (M)->trim_check) #else /* MORECORE_CANNOT_TRIM */ #define should_trim(M,s) (0) From c7d033e279fefceab6b788d5f97f745022d3fa2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Cant=C3=B3n=20Cort=C3=A9s?= Date: Fri, 14 Oct 2022 13:57:06 +0200 Subject: [PATCH 07/10] Avoid trying to trim when UNSIGNED_MORECORE so that we don't allocate even more memory by error --- system/lib/dlmalloc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index 9bddb33956df7..63fc3478a1931 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -4204,7 +4204,9 @@ static void* sys_alloc(mstate m, size_t nb) { if (end != CMFAIL) ssize += esize; else { /* Can't use; try to release */ - (void) CALL_MORECORE(-ssize); + if (!UNSIGNED_MORECORE) { + (void) CALL_MORECORE(-ssize); + } br = CMFAIL; } } From dee20f8fa6fa3c1fc75e39fb57074c4a1a8b031e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Canto=CC=81n=20Corte=CC=81s?= Date: Thu, 20 Oct 2022 13:36:47 +0200 Subject: [PATCH 08/10] Add test --- system/lib/dlmalloc.c | 4 +++- test/test_browser.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index 63fc3478a1931..c3128e2fe4228 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -4401,7 +4401,8 @@ static int sys_trim(mstate m, size_t pad) { } } } - else if (HAVE_MORECORE && !UNSIGNED_MORECORE) { + else if (HAVE_MORECORE) { +#ifndef MORECORE_CANNOT_TRIM if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */ extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit; ACQUIRE_MALLOC_GLOBAL_LOCK(); @@ -4417,6 +4418,7 @@ static int sys_trim(mstate m, size_t pad) { } RELEASE_MALLOC_GLOBAL_LOCK(); } +#endif } if (released != 0) { diff --git a/test/test_browser.py b/test/test_browser.py index 42d49f084dbf5..ffecd48d0957b 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -5313,6 +5313,12 @@ def test(args): test(['-sMALLOC=emmalloc-memvalidate']) test(['-sMALLOC=emmalloc-memvalidate-verbose']) + # Test that it is possible to malloc() a huge 3GB memory block in 4GB mode using dlmalloc. + @no_firefox('no 4GB support yet') + def test_dlmalloc_3GB(self): + self.btest_exit(test_file('alloc_3gb.cpp'), + args=['-sMALLOC=dlmalloc', '-sMAXIMUM_MEMORY=4GB', '-sALLOW_MEMORY_GROWTH=1']) + @parameterized({ # the fetch backend works even on the main thread: we proxy to a background # thread and busy-wait From 38b4e4984254f4f8581d7fe4068ac86324408eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Canto=CC=81n=20Corte=CC=81s?= Date: Thu, 20 Oct 2022 13:40:34 +0200 Subject: [PATCH 09/10] Fixed indentation --- test/test_browser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_browser.py b/test/test_browser.py index ffecd48d0957b..d678b6725d0bb 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -5317,7 +5317,7 @@ def test(args): @no_firefox('no 4GB support yet') def test_dlmalloc_3GB(self): self.btest_exit(test_file('alloc_3gb.cpp'), - args=['-sMALLOC=dlmalloc', '-sMAXIMUM_MEMORY=4GB', '-sALLOW_MEMORY_GROWTH=1']) + args=['-sMALLOC=dlmalloc', '-sMAXIMUM_MEMORY=4GB', '-sALLOW_MEMORY_GROWTH=1']) @parameterized({ # the fetch backend works even on the main thread: we proxy to a background From 2cc29a630182b7288f49d5a993d45227f2f43e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Canto=CC=81n=20Corte=CC=81s?= Date: Thu, 20 Oct 2022 13:52:58 +0200 Subject: [PATCH 10/10] Fix MORECORE_CANNOT_TRIM use --- system/lib/dlmalloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index c3128e2fe4228..291707fe12285 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -4417,8 +4417,8 @@ static int sys_trim(mstate m, size_t pad) { } } RELEASE_MALLOC_GLOBAL_LOCK(); - } #endif + } } if (released != 0) {