slouken@1330: /* slouken@5535: Simple DirectMedia Layer slouken@8149: Copyright (C) 1997-2014 Sam Lantinga slouken@5535: slouken@5535: This software is provided 'as-is', without any express or implied slouken@5535: warranty. In no event will the authors be held liable for any damages slouken@5535: arising from the use of this software. slouken@5535: slouken@5535: Permission is granted to anyone to use this software for any purpose, slouken@5535: including commercial applications, and to alter it and redistribute it slouken@5535: freely, subject to the following restrictions: slouken@5535: slouken@5535: 1. The origin of this software must not be misrepresented; you must not slouken@5535: claim that you wrote the original software. If you use this software slouken@5535: in a product, an acknowledgment in the product documentation would be slouken@5535: appreciated but is not required. slouken@5535: 2. Altered source versions must be plainly marked as such, and must not be slouken@5535: misrepresented as being the original software. slouken@5535: 3. This notice may not be removed or altered from any source distribution. slouken@1330: */ icculus@8093: #include "../SDL_internal.h" slouken@1330: slouken@1330: /* This file contains portable memory management functions for SDL */ slouken@1330: slouken@1354: #include "SDL_stdinc.h" slouken@1330: slouken@7351: #if defined(HAVE_MALLOC) slouken@7351: slouken@7351: void *SDL_malloc(size_t size) slouken@7351: { slouken@7351: return malloc(size); slouken@7351: } slouken@7351: slouken@7351: void *SDL_calloc(size_t nmemb, size_t size) slouken@7351: { slouken@7351: return calloc(nmemb, size); slouken@7351: } slouken@7351: slouken@7351: void *SDL_realloc(void *ptr, size_t size) slouken@7351: { slouken@7351: return realloc(ptr, size); slouken@7351: } slouken@7351: slouken@7351: void SDL_free(void *ptr) slouken@7351: { slouken@7351: free(ptr); slouken@7351: } icculus@7003: icculus@7003: #else /* the rest of this is a LOT of tapdancing to implement malloc. :) */ slouken@1330: slouken@1465: #define LACKS_SYS_TYPES_H slouken@1330: #define LACKS_STDIO_H slouken@1330: #define LACKS_STRINGS_H slouken@1330: #define LACKS_STRING_H slouken@1330: #define LACKS_STDLIB_H slouken@1330: #define ABORT slouken@8758: #define USE_LOCKS 1 slouken@1330: slouken@1330: /* slouken@1330: This is a version (aka dlmalloc) of malloc/free/realloc written by slouken@1330: Doug Lea and released to the public domain, as explained at slouken@1330: http://creativecommons.org/licenses/publicdomain. Send questions, slouken@1330: comments, complaints, performance data, etc to dl@cs.oswego.edu slouken@1330: slouken@1330: * Version 2.8.3 Thu Sep 22 11:16:15 2005 Doug Lea (dl at gee) slouken@1330: slouken@1330: Note: There may be an updated version of this malloc obtainable at slouken@1330: ftp://gee.cs.oswego.edu/pub/misc/malloc.c slouken@1330: Check before installing! slouken@1330: slouken@1330: * Quickstart slouken@1330: slouken@1330: This library is all in one file to simplify the most common usage: slouken@1330: ftp it, compile it (-O3), and link it into another program. All of slouken@1330: the compile-time options default to reasonable values for use on slouken@1330: most platforms. You might later want to step through various slouken@1330: compile-time and dynamic tuning options. slouken@1330: slouken@1330: For convenience, an include file for code using this malloc is at: slouken@1330: ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.3.h slouken@1330: You don't really need this .h file unless you call functions not slouken@1330: defined in your system include files. The .h file contains only the slouken@1330: excerpts from this file needed for using this malloc on ANSI C/C++ slouken@1330: systems, so long as you haven't changed compile-time options about slouken@1330: naming and tuning parameters. If you do, then you can create your slouken@1330: own malloc.h that does include all settings by cutting at the point slouken@1330: indicated below. Note that you may already by default be using a C slouken@1330: library containing a malloc that is based on some version of this slouken@1330: malloc (for example in linux). You might still want to use the one slouken@1330: in this file to customize settings or to avoid overheads associated slouken@1330: with library versions. slouken@1330: slouken@1330: * Vital statistics: slouken@1330: slouken@1330: Supported pointer/size_t representation: 4 or 8 bytes slouken@1330: size_t MUST be an unsigned type of the same width as slouken@1330: pointers. (If you are using an ancient system that declares slouken@1330: size_t as a signed type, or need it to be a different width slouken@1330: than pointers, you can use a previous release of this malloc slouken@1330: (e.g. 2.7.2) supporting these.) slouken@1330: slouken@1330: Alignment: 8 bytes (default) slouken@1330: This suffices for nearly all current machines and C compilers. slouken@1330: However, you can define MALLOC_ALIGNMENT to be wider than this slouken@1330: if necessary (up to 128bytes), at the expense of using more space. slouken@1330: slouken@1330: Minimum overhead per allocated chunk: 4 or 8 bytes (if 4byte sizes) slouken@1330: 8 or 16 bytes (if 8byte sizes) slouken@1330: Each malloced chunk has a hidden word of overhead holding size slouken@1330: and status information, and additional cross-check word slouken@1330: if FOOTERS is defined. slouken@1330: slouken@1330: Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead) slouken@1330: 8-byte ptrs: 32 bytes (including overhead) slouken@1330: slouken@1330: Even a request for zero bytes (i.e., malloc(0)) returns a slouken@1330: pointer to something of the minimum allocatable size. slouken@1330: The maximum overhead wastage (i.e., number of extra bytes slouken@1330: allocated than were requested in malloc) is less than or equal slouken@1330: to the minimum size, except for requests >= mmap_threshold that slouken@1330: are serviced via mmap(), where the worst case wastage is about slouken@1330: 32 bytes plus the remainder from a system page (the minimal slouken@1330: mmap unit); typically 4096 or 8192 bytes. slouken@1330: slouken@1330: Security: static-safe; optionally more or less slouken@1330: The "security" of malloc refers to the ability of malicious slouken@1330: code to accentuate the effects of errors (for example, freeing slouken@1330: space that is not currently malloc'ed or overwriting past the slouken@1330: ends of chunks) in code that calls malloc. This malloc slouken@1330: guarantees not to modify any memory locations below the base of slouken@1330: heap, i.e., static variables, even in the presence of usage slouken@1330: errors. The routines additionally detect most improper frees slouken@1330: and reallocs. All this holds as long as the static bookkeeping slouken@1330: for malloc itself is not corrupted by some other means. This slouken@1330: is only one aspect of security -- these checks do not, and slouken@1330: cannot, detect all possible programming errors. slouken@1330: slouken@1330: If FOOTERS is defined nonzero, then each allocated chunk slouken@1330: carries an additional check word to verify that it was malloced slouken@1330: from its space. These check words are the same within each slouken@1330: execution of a program using malloc, but differ across slouken@1330: executions, so externally crafted fake chunks cannot be slouken@1330: freed. This improves security by rejecting frees/reallocs that slouken@1330: could corrupt heap memory, in addition to the checks preventing slouken@1330: writes to statics that are always on. This may further improve slouken@1330: security at the expense of time and space overhead. (Note that slouken@1330: FOOTERS may also be worth using with MSPACES.) slouken@1330: slouken@1330: By default detected errors cause the program to abort (calling slouken@1330: "abort()"). You can override this to instead proceed past slouken@1330: errors by defining PROCEED_ON_ERROR. In this case, a bad free slouken@1330: has no effect, and a malloc that encounters a bad address slouken@1330: caused by user overwrites will ignore the bad address by slouken@1330: dropping pointers and indices to all known memory. This may slouken@1330: be appropriate for programs that should continue if at all slouken@1330: possible in the face of programming errors, although they may slouken@1330: run out of memory because dropped memory is never reclaimed. slouken@1330: slouken@1330: If you don't like either of these options, you can define slouken@1330: CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything slouken@1330: else. And if if you are sure that your program using malloc has slouken@1330: no errors or vulnerabilities, you can define INSECURE to 1, slouken@1330: which might (or might not) provide a small performance improvement. slouken@1330: slouken@1330: Thread-safety: NOT thread-safe unless USE_LOCKS defined slouken@1330: When USE_LOCKS is defined, each public call to malloc, free, slouken@1330: etc is surrounded with either a pthread mutex or a win32 slouken@1330: spinlock (depending on WIN32). This is not especially fast, and slouken@1330: can be a major bottleneck. It is designed only to provide slouken@1330: minimal protection in concurrent environments, and to provide a slouken@1330: basis for extensions. If you are using malloc in a concurrent slouken@1330: program, consider instead using ptmalloc, which is derived from slouken@1330: a version of this malloc. (See http://www.malloc.de). slouken@1330: slouken@1330: System requirements: Any combination of MORECORE and/or MMAP/MUNMAP slouken@1330: This malloc can use unix sbrk or any emulation (invoked using slouken@1330: the CALL_MORECORE macro) and/or mmap/munmap or any emulation slouken@1330: (invoked using CALL_MMAP/CALL_MUNMAP) to get and release system slouken@1330: memory. On most unix systems, it tends to work best if both slouken@1330: MORECORE and MMAP are enabled. On Win32, it uses emulations slouken@1330: based on VirtualAlloc. It also uses common C library functions slouken@1330: like memset. slouken@1330: slouken@1330: Compliance: I believe it is compliant with the Single Unix Specification slouken@1330: (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably slouken@1330: others as well. slouken@1330: slouken@1330: * Overview of algorithms slouken@1330: slouken@1330: This is not the fastest, most space-conserving, most portable, or slouken@1330: most tunable malloc ever written. However it is among the fastest slouken@1330: while also being among the most space-conserving, portable and slouken@1330: tunable. Consistent balance across these factors results in a good slouken@1330: general-purpose allocator for malloc-intensive programs. slouken@1330: slouken@1330: In most ways, this malloc is a best-fit allocator. Generally, it slouken@1330: chooses the best-fitting existing chunk for a request, with ties slouken@1330: broken in approximately least-recently-used order. (This strategy slouken@1330: normally maintains low fragmentation.) However, for requests less slouken@1330: than 256bytes, it deviates from best-fit when there is not an slouken@1330: exactly fitting available chunk by preferring to use space adjacent slouken@1330: to that used for the previous small request, as well as by breaking slouken@1330: ties in approximately most-recently-used order. (These enhance slouken@1330: locality of series of small allocations.) And for very large requests slouken@1330: (>= 256Kb by default), it relies on system memory mapping slouken@1330: facilities, if supported. (This helps avoid carrying around and slouken@1330: possibly fragmenting memory used only for large chunks.) slouken@1330: slouken@1330: All operations (except malloc_stats and mallinfo) have execution slouken@1330: times that are bounded by a constant factor of the number of bits in slouken@1330: a size_t, not counting any clearing in calloc or copying in realloc, slouken@1330: or actions surrounding MORECORE and MMAP that have times slouken@1330: proportional to the number of non-contiguous regions returned by slouken@1330: system allocation routines, which is often just 1. slouken@1330: slouken@1330: The implementation is not very modular and seriously overuses slouken@1330: macros. Perhaps someday all C compilers will do as good a job slouken@1330: inlining modular code as can now be done by brute-force expansion, slouken@1330: but now, enough of them seem not to. slouken@1330: slouken@1330: Some compilers issue a lot of warnings about code that is slouken@1330: dead/unreachable only on some platforms, and also about intentional slouken@1330: uses of negation on unsigned types. All known cases of each can be slouken@1330: ignored. slouken@1330: slouken@1330: For a longer but out of date high-level description, see slouken@1330: http://gee.cs.oswego.edu/dl/html/malloc.html slouken@1330: slouken@1330: * MSPACES slouken@1330: If MSPACES is defined, then in addition to malloc, free, etc., slouken@1330: this file also defines mspace_malloc, mspace_free, etc. These slouken@1330: are versions of malloc routines that take an "mspace" argument slouken@1330: obtained using create_mspace, to control all internal bookkeeping. slouken@1330: If ONLY_MSPACES is defined, only these versions are compiled. slouken@1330: So if you would like to use this allocator for only some allocations, slouken@1330: and your system malloc for others, you can compile with slouken@1330: ONLY_MSPACES and then do something like... slouken@1330: static mspace mymspace = create_mspace(0,0); // for example slouken@1330: #define mymalloc(bytes) mspace_malloc(mymspace, bytes) slouken@1330: slouken@1330: (Note: If you only need one instance of an mspace, you can instead slouken@1330: use "USE_DL_PREFIX" to relabel the global malloc.) slouken@1330: slouken@1330: You can similarly create thread-local allocators by storing slouken@1330: mspaces as thread-locals. For example: slouken@1330: static __thread mspace tlms = 0; slouken@1330: void* tlmalloc(size_t bytes) { slouken@1330: if (tlms == 0) tlms = create_mspace(0, 0); slouken@1330: return mspace_malloc(tlms, bytes); slouken@1330: } slouken@1330: void tlfree(void* mem) { mspace_free(tlms, mem); } slouken@1330: slouken@1330: Unless FOOTERS is defined, each mspace is completely independent. slouken@1330: You cannot allocate from one and free to another (although slouken@1330: conformance is only weakly checked, so usage errors are not always slouken@1330: caught). If FOOTERS is defined, then each chunk carries around a tag slouken@1330: indicating its originating mspace, and frees are directed to their slouken@1330: originating spaces. slouken@1330: slouken@1330: ------------------------- Compile-time options --------------------------- slouken@1330: slouken@1330: Be careful in setting #define values for numerical constants of type slouken@1330: size_t. On some systems, literal values are not automatically extended slouken@1330: to size_t precision unless they are explicitly casted. slouken@1330: slouken@1330: WIN32 default: defined if _WIN32 defined slouken@1330: Defining WIN32 sets up defaults for MS environment and compilers. slouken@1330: Otherwise defaults are for unix. slouken@1330: slouken@1330: MALLOC_ALIGNMENT default: (size_t)8 slouken@1330: Controls the minimum alignment for malloc'ed chunks. It must be a slouken@1330: power of two and at least 8, even on machines for which smaller slouken@1330: alignments would suffice. It may be defined as larger than this slouken@1330: though. Note however that code and data structures are optimized for slouken@1330: the case of 8-byte alignment. slouken@1330: slouken@1330: MSPACES default: 0 (false) slouken@1330: If true, compile in support for independent allocation spaces. slouken@1330: This is only supported if HAVE_MMAP is true. slouken@1330: slouken@1330: ONLY_MSPACES default: 0 (false) slouken@1330: If true, only compile in mspace versions, not regular versions. slouken@1330: slouken@1330: USE_LOCKS default: 0 (false) slouken@1330: Causes each call to each public routine to be surrounded with slouken@1330: pthread or WIN32 mutex lock/unlock. (If set true, this can be slouken@1330: overridden on a per-mspace basis for mspace versions.) slouken@1330: slouken@1330: FOOTERS default: 0 slouken@1330: If true, provide extra checking and dispatching by placing slouken@1330: information in the footers of allocated chunks. This adds slouken@1330: space and time overhead. slouken@1330: slouken@1330: INSECURE default: 0 slouken@1330: If true, omit checks for usage errors and heap space overwrites. slouken@1330: slouken@1330: USE_DL_PREFIX default: NOT defined slouken@1330: Causes compiler to prefix all public routines with the string 'dl'. slouken@1330: This can be useful when you only want to use this malloc in one part slouken@1330: of a program, using your regular system malloc elsewhere. slouken@1330: slouken@1330: ABORT default: defined as abort() slouken@1330: Defines how to abort on failed checks. On most systems, a failed slouken@1330: check cannot die with an "assert" or even print an informative slouken@1330: message, because the underlying print routines in turn call malloc, slouken@1330: which will fail again. Generally, the best policy is to simply call slouken@1330: abort(). It's not very useful to do more than this because many slouken@1330: errors due to overwriting will show up as address faults (null, odd slouken@1330: addresses etc) rather than malloc-triggered checks, so will also slouken@1330: abort. Also, most compilers know that abort() does not return, so slouken@1330: can better optimize code conditionally calling it. slouken@1330: slouken@1330: PROCEED_ON_ERROR default: defined as 0 (false) slouken@1330: Controls whether detected bad addresses cause them to bypassed slouken@1330: rather than aborting. If set, detected bad arguments to free and slouken@1330: realloc are ignored. And all bookkeeping information is zeroed out slouken@1330: upon a detected overwrite of freed heap space, thus losing the slouken@1330: ability to ever return it from malloc again, but enabling the slouken@1330: application to proceed. If PROCEED_ON_ERROR is defined, the slouken@1330: static variable malloc_corruption_error_count is compiled in slouken@1330: and can be examined to see if errors have occurred. This option slouken@1330: generates slower code than the default abort policy. slouken@1330: slouken@1330: DEBUG default: NOT defined slouken@1330: The DEBUG setting is mainly intended for people trying to modify slouken@1330: this code or diagnose problems when porting to new platforms. slouken@1330: However, it may also be able to better isolate user errors than just slouken@1330: using runtime checks. The assertions in the check routines spell slouken@1330: out in more detail the assumptions and invariants underlying the slouken@1330: algorithms. The checking is fairly extensive, and will slow down slouken@1330: execution noticeably. Calling malloc_stats or mallinfo with DEBUG slouken@1330: set will attempt to check every non-mmapped allocated and free chunk slouken@1330: in the course of computing the summaries. slouken@1330: slouken@1330: ABORT_ON_ASSERT_FAILURE default: defined as 1 (true) slouken@1330: Debugging assertion failures can be nearly impossible if your slouken@1330: version of the assert macro causes malloc to be called, which will slouken@1330: lead to a cascade of further failures, blowing the runtime stack. slouken@1330: ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(), slouken@1330: which will usually make debugging easier. slouken@1330: slouken@1330: MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32 slouken@1330: The action to take before "return 0" when malloc fails to be able to slouken@1330: return memory because there is none available. slouken@1330: slouken@1330: HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES slouken@1330: True if this system supports sbrk or an emulation of it. slouken@1330: slouken@1330: MORECORE default: sbrk slouken@1330: The name of the sbrk-style system routine to call to obtain more slouken@1330: memory. See below for guidance on writing custom MORECORE slouken@1330: functions. The type of the argument to sbrk/MORECORE varies across slouken@1330: systems. It cannot be size_t, because it supports negative slouken@1330: arguments, so it is normally the signed type of the same width as slouken@1330: size_t (sometimes declared as "intptr_t"). It doesn't much matter slouken@1330: though. Internally, we only call it with arguments less than half slouken@1330: the max value of a size_t, which should work across all reasonable slouken@1330: possibilities, although sometimes generating compiler warnings. See slouken@1330: near the end of this file for guidelines for creating a custom slouken@1330: version of MORECORE. slouken@1330: slouken@1330: MORECORE_CONTIGUOUS default: 1 (true) slouken@1330: If true, take advantage of fact that consecutive calls to MORECORE slouken@1330: with positive arguments always return contiguous increasing slouken@1330: addresses. This is true of unix sbrk. It does not hurt too much to slouken@1330: set it true anyway, since malloc copes with non-contiguities. slouken@1330: Setting it false when definitely non-contiguous saves time slouken@1330: and possibly wasted space it would take to discover this though. slouken@1330: slouken@1330: MORECORE_CANNOT_TRIM default: NOT defined slouken@1330: True if MORECORE cannot release space back to the system when given slouken@1330: negative arguments. This is generally necessary only if you are slouken@1330: using a hand-crafted MORECORE function that cannot handle negative slouken@1330: arguments. slouken@1330: slouken@1330: HAVE_MMAP default: 1 (true) slouken@1330: True if this system supports mmap or an emulation of it. If so, and slouken@1330: HAVE_MORECORE is not true, MMAP is used for all system slouken@1330: allocation. If set and HAVE_MORECORE is true as well, MMAP is slouken@1330: primarily used to directly allocate very large blocks. It is also slouken@1330: used as a backup strategy in cases where MORECORE fails to provide slouken@1330: space from system. Note: A single call to MUNMAP is assumed to be slouken@1330: able to unmap memory that may have be allocated using multiple calls slouken@1330: to MMAP, so long as they are adjacent. slouken@1330: slouken@1330: HAVE_MREMAP default: 1 on linux, else 0 slouken@1330: If true realloc() uses mremap() to re-allocate large blocks and slouken@1330: extend or shrink allocation spaces. slouken@1330: slouken@1330: MMAP_CLEARS default: 1 on unix slouken@1330: True if mmap clears memory so calloc doesn't need to. This is true slouken@1330: for standard unix mmap using /dev/zero. slouken@1330: slouken@1330: USE_BUILTIN_FFS default: 0 (i.e., not used) slouken@1330: Causes malloc to use the builtin ffs() function to compute indices. slouken@1330: Some compilers may recognize and intrinsify ffs to be faster than the slouken@1330: supplied C version. Also, the case of x86 using gcc is special-cased slouken@1330: to an asm instruction, so is already as fast as it can be, and so slouken@1330: this setting has no effect. (On most x86s, the asm version is only slouken@1330: slightly faster than the C version.) slouken@1330: slouken@1330: malloc_getpagesize default: derive from system includes, or 4096. slouken@1330: The system page size. To the extent possible, this malloc manages slouken@1330: memory from the system in page-size units. This may be (and slouken@1330: usually is) a function rather than a constant. This is ignored slouken@1330: if WIN32, where page size is determined using getSystemInfo during slouken@1330: initialization. slouken@1330: slouken@1330: USE_DEV_RANDOM default: 0 (i.e., not used) slouken@1330: Causes malloc to use /dev/random to initialize secure magic seed for slouken@1330: stamping footers. Otherwise, the current time is used. slouken@1330: slouken@1330: NO_MALLINFO default: 0 slouken@1330: If defined, don't compile "mallinfo". This can be a simple way slouken@1330: of dealing with mismatches between system declarations and slouken@1330: those in this file. slouken@1330: slouken@1330: MALLINFO_FIELD_TYPE default: size_t slouken@1330: The type of the fields in the mallinfo struct. This was originally slouken@1330: defined as "int" in SVID etc, but is more usefully defined as slouken@1330: size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set slouken@1330: slouken@1330: REALLOC_ZERO_BYTES_FREES default: not defined slouken@7191: This should be set if a call to realloc with zero bytes should slouken@7191: be the same as a call to free. Some people think it should. Otherwise, slouken@7191: since this malloc returns a unique pointer for malloc(0), so does slouken@1330: realloc(p, 0). slouken@1330: slouken@1330: LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H slouken@1330: LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H slouken@1330: LACKS_STDLIB_H default: NOT defined unless on WIN32 slouken@1330: Define these if your system does not have these header files. slouken@1330: You might need to manually insert some of the declarations they provide. slouken@1330: slouken@1330: DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS, slouken@1330: system_info.dwAllocationGranularity in WIN32, slouken@1330: otherwise 64K. slouken@1330: Also settable using mallopt(M_GRANULARITY, x) slouken@1330: The unit for allocating and deallocating memory from the system. On slouken@1330: most systems with contiguous MORECORE, there is no reason to slouken@1330: make this more than a page. However, systems with MMAP tend to slouken@1330: either require or encourage larger granularities. You can increase slouken@1330: this value to prevent system allocation functions to be called so slouken@1330: often, especially if they are slow. The value must be at least one slouken@1330: page and must be a power of two. Setting to 0 causes initialization slouken@1330: to either page size or win32 region size. (Note: In previous slouken@1330: versions of malloc, the equivalent of this option was called slouken@1330: "TOP_PAD") slouken@1330: slouken@1330: DEFAULT_TRIM_THRESHOLD default: 2MB slouken@1330: Also settable using mallopt(M_TRIM_THRESHOLD, x) slouken@1330: The maximum amount of unused top-most memory to keep before slouken@1330: releasing via malloc_trim in free(). Automatic trimming is mainly slouken@1330: useful in long-lived programs using contiguous MORECORE. Because slouken@1330: trimming via sbrk can be slow on some systems, and can sometimes be slouken@1330: wasteful (in cases where programs immediately afterward allocate slouken@1330: more large chunks) the value should be high enough so that your slouken@1330: overall system performance would improve by releasing this much slouken@1330: memory. As a rough guide, you might set to a value close to the slouken@1330: average size of a process (program) running on your system. slouken@1330: Releasing this much memory would allow such a process to run in slouken@1330: memory. Generally, it is worth tuning trim thresholds when a slouken@1330: program undergoes phases where several large chunks are allocated slouken@1330: and released in ways that can reuse each other's storage, perhaps slouken@1330: mixed with phases where there are no such chunks at all. The trim slouken@1330: value must be greater than page size to have any useful effect. To slouken@1330: disable trimming completely, you can set to MAX_SIZE_T. Note that the trick slouken@1330: some people use of mallocing a huge space and then freeing it at slouken@1330: program startup, in an attempt to reserve system memory, doesn't slouken@1330: have the intended effect under automatic trimming, since that memory slouken@1330: will immediately be returned to the system. slouken@1330: slouken@1330: DEFAULT_MMAP_THRESHOLD default: 256K slouken@1330: Also settable using mallopt(M_MMAP_THRESHOLD, x) slouken@1330: The request size threshold for using MMAP to directly service a slouken@1330: request. Requests of at least this size that cannot be allocated slouken@1330: using already-existing space will be serviced via mmap. (If enough slouken@1330: normal freed space already exists it is used instead.) Using mmap slouken@1330: segregates relatively large chunks of memory so that they can be slouken@1330: individually obtained and released from the host system. A request slouken@1330: serviced through mmap is never reused by any other request (at least slouken@1330: not directly; the system may just so happen to remap successive slouken@1330: requests to the same locations). Segregating space in this way has slouken@1330: the benefits that: Mmapped space can always be individually released slouken@1330: back to the system, which helps keep the system level memory demands slouken@1330: of a long-lived program low. Also, mapped memory doesn't become slouken@1330: `locked' between other chunks, as can happen with normally allocated slouken@1330: chunks, which means that even trimming via malloc_trim would not slouken@1330: release them. However, it has the disadvantage that the space slouken@1330: cannot be reclaimed, consolidated, and then used to service later slouken@1330: requests, as happens with normal chunks. The advantages of mmap slouken@1330: nearly always outweigh disadvantages for "large" chunks, but the slouken@1330: value of "large" may vary across systems. The default is an slouken@1330: empirically derived value that works well in most systems. You can slouken@1330: disable mmap by setting to MAX_SIZE_T. slouken@1330: slouken@1330: */ slouken@1330: slouken@1330: #ifndef WIN32 slouken@1330: #ifdef _WIN32 slouken@1330: #define WIN32 1 slouken@1895: #endif /* _WIN32 */ slouken@1895: #endif /* WIN32 */ slouken@1330: #ifdef WIN32 slouken@1433: #define WIN32_LEAN_AND_MEAN slouken@1433: #include slouken@1330: #define HAVE_MMAP 1 slouken@1330: #define HAVE_MORECORE 0 slouken@1330: #define LACKS_UNISTD_H slouken@1330: #define LACKS_SYS_PARAM_H slouken@1330: #define LACKS_SYS_MMAN_H slouken@1330: #define LACKS_STRING_H slouken@1330: #define LACKS_STRINGS_H slouken@1330: #define LACKS_SYS_TYPES_H slouken@1330: #define LACKS_ERRNO_H slouken@1895: #define LACKS_FCNTL_H slouken@1330: #define MALLOC_FAILURE_ACTION slouken@1895: #define MMAP_CLEARS 0 /* WINCE and some others apparently don't clear */ slouken@1895: #endif /* WIN32 */ slouken@1330: slouken@1330: #if defined(DARWIN) || defined(_DARWIN) slouken@1330: /* Mac OSX docs advise not to use sbrk; it seems better to use mmap */ slouken@1330: #ifndef HAVE_MORECORE slouken@1330: #define HAVE_MORECORE 0 slouken@1330: #define HAVE_MMAP 1 slouken@1895: #endif /* HAVE_MORECORE */ slouken@1895: #endif /* DARWIN */ slouken@1330: slouken@1330: #ifndef LACKS_SYS_TYPES_H slouken@1895: #include /* For size_t */ slouken@1895: #endif /* LACKS_SYS_TYPES_H */ slouken@1330: slouken@1330: /* The maximum possible size_t value has all bits set */ slouken@1330: #define MAX_SIZE_T (~(size_t)0) slouken@1330: slouken@1330: #ifndef ONLY_MSPACES slouken@1330: #define ONLY_MSPACES 0 slouken@1895: #endif /* ONLY_MSPACES */ slouken@1330: #ifndef MSPACES slouken@1330: #if ONLY_MSPACES slouken@1330: #define MSPACES 1 slouken@1895: #else /* ONLY_MSPACES */ slouken@1330: #define MSPACES 0 slouken@1895: #endif /* ONLY_MSPACES */ slouken@1895: #endif /* MSPACES */ slouken@1330: #ifndef MALLOC_ALIGNMENT slouken@1330: #define MALLOC_ALIGNMENT ((size_t)8U) slouken@1895: #endif /* MALLOC_ALIGNMENT */ slouken@1330: #ifndef FOOTERS slouken@1330: #define FOOTERS 0 slouken@1895: #endif /* FOOTERS */ slouken@1330: #ifndef ABORT slouken@1330: #define ABORT abort() slouken@1895: #endif /* ABORT */ slouken@1330: #ifndef ABORT_ON_ASSERT_FAILURE slouken@1330: #define ABORT_ON_ASSERT_FAILURE 1 slouken@1895: #endif /* ABORT_ON_ASSERT_FAILURE */ slouken@1330: #ifndef PROCEED_ON_ERROR slouken@1330: #define PROCEED_ON_ERROR 0 slouken@1895: #endif /* PROCEED_ON_ERROR */ slouken@1330: #ifndef USE_LOCKS slouken@1330: #define USE_LOCKS 0 slouken@1895: #endif /* USE_LOCKS */ slouken@1330: #ifndef INSECURE slouken@1330: #define INSECURE 0 slouken@1895: #endif /* INSECURE */ slouken@1330: #ifndef HAVE_MMAP slouken@1330: #define HAVE_MMAP 1 slouken@1895: #endif /* HAVE_MMAP */ slouken@1330: #ifndef MMAP_CLEARS slouken@1330: #define MMAP_CLEARS 1 slouken@1895: #endif /* MMAP_CLEARS */ slouken@1330: #ifndef HAVE_MREMAP slouken@1330: #ifdef linux slouken@1330: #define HAVE_MREMAP 1 slouken@1895: #else /* linux */ slouken@1330: #define HAVE_MREMAP 0 slouken@1895: #endif /* linux */ slouken@1895: #endif /* HAVE_MREMAP */ slouken@1330: #ifndef MALLOC_FAILURE_ACTION slouken@1330: #define MALLOC_FAILURE_ACTION errno = ENOMEM; slouken@1895: #endif /* MALLOC_FAILURE_ACTION */ slouken@1330: #ifndef HAVE_MORECORE slouken@1330: #if ONLY_MSPACES slouken@1330: #define HAVE_MORECORE 0 slouken@1895: #else /* ONLY_MSPACES */ slouken@1330: #define HAVE_MORECORE 1 slouken@1895: #endif /* ONLY_MSPACES */ slouken@1895: #endif /* HAVE_MORECORE */ slouken@1330: #if !HAVE_MORECORE slouken@1330: #define MORECORE_CONTIGUOUS 0 slouken@1895: #else /* !HAVE_MORECORE */ slouken@1330: #ifndef MORECORE slouken@1330: #define MORECORE sbrk slouken@1895: #endif /* MORECORE */ slouken@1330: #ifndef MORECORE_CONTIGUOUS slouken@1330: #define MORECORE_CONTIGUOUS 1 slouken@1895: #endif /* MORECORE_CONTIGUOUS */ slouken@1895: #endif /* HAVE_MORECORE */ slouken@1330: #ifndef DEFAULT_GRANULARITY slouken@1330: #if MORECORE_CONTIGUOUS slouken@1895: #define DEFAULT_GRANULARITY (0) /* 0 means to compute in init_mparams */ slouken@1895: #else /* MORECORE_CONTIGUOUS */ slouken@1330: #define DEFAULT_GRANULARITY ((size_t)64U * (size_t)1024U) slouken@1895: #endif /* MORECORE_CONTIGUOUS */ slouken@1895: #endif /* DEFAULT_GRANULARITY */ slouken@1330: #ifndef DEFAULT_TRIM_THRESHOLD slouken@1330: #ifndef MORECORE_CANNOT_TRIM slouken@1330: #define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U) slouken@1895: #else /* MORECORE_CANNOT_TRIM */ slouken@1330: #define DEFAULT_TRIM_THRESHOLD MAX_SIZE_T slouken@1895: #endif /* MORECORE_CANNOT_TRIM */ slouken@1895: #endif /* DEFAULT_TRIM_THRESHOLD */ slouken@1330: #ifndef DEFAULT_MMAP_THRESHOLD slouken@1330: #if HAVE_MMAP slouken@1330: #define DEFAULT_MMAP_THRESHOLD ((size_t)256U * (size_t)1024U) slouken@1895: #else /* HAVE_MMAP */ slouken@1330: #define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T slouken@1895: #endif /* HAVE_MMAP */ slouken@1895: #endif /* DEFAULT_MMAP_THRESHOLD */ slouken@1330: #ifndef USE_BUILTIN_FFS slouken@1330: #define USE_BUILTIN_FFS 0 slouken@1895: #endif /* USE_BUILTIN_FFS */ slouken@1330: #ifndef USE_DEV_RANDOM slouken@1330: #define USE_DEV_RANDOM 0 slouken@1895: #endif /* USE_DEV_RANDOM */ slouken@1330: #ifndef NO_MALLINFO slouken@1330: #define NO_MALLINFO 0 slouken@1895: #endif /* NO_MALLINFO */ slouken@1330: #ifndef MALLINFO_FIELD_TYPE slouken@1330: #define MALLINFO_FIELD_TYPE size_t slouken@1895: #endif /* MALLINFO_FIELD_TYPE */ slouken@1330: slouken@7191: #define memset SDL_memset slouken@7191: #define memcpy SDL_memcpy slouken@7191: #define malloc SDL_malloc slouken@7191: #define calloc SDL_calloc slouken@7191: #define realloc SDL_realloc slouken@7191: #define free SDL_free slouken@1465: slouken@1330: /* slouken@1330: mallopt tuning options. SVID/XPG defines four standard parameter slouken@1330: numbers for mallopt, normally defined in malloc.h. None of these slouken@1330: are used in this malloc, so setting them has no effect. But this slouken@1330: malloc does support the following options. slouken@1330: */ slouken@1330: slouken@1330: #define M_TRIM_THRESHOLD (-1) slouken@1330: #define M_GRANULARITY (-2) slouken@1330: #define M_MMAP_THRESHOLD (-3) slouken@1330: slouken@1330: /* ------------------------ Mallinfo declarations ------------------------ */ slouken@1330: slouken@1330: #if !NO_MALLINFO slouken@1330: /* slouken@1330: This version of malloc supports the standard SVID/XPG mallinfo slouken@1330: routine that returns a struct containing usage properties and slouken@1330: statistics. It should work on any system that has a slouken@1330: /usr/include/malloc.h defining struct mallinfo. The main slouken@1330: declaration needed is the mallinfo struct that is returned (by-copy) slouken@1330: by mallinfo(). The malloinfo struct contains a bunch of fields that slouken@1330: are not even meaningful in this version of malloc. These fields are slouken@1330: are instead filled by mallinfo() with other numbers that might be of slouken@1330: interest. slouken@1330: slouken@1330: HAVE_USR_INCLUDE_MALLOC_H should be set if you have a slouken@1330: /usr/include/malloc.h file that includes a declaration of struct slouken@1330: mallinfo. If so, it is included; else a compliant version is slouken@1330: declared below. These must be precisely the same for mallinfo() to slouken@1330: work. The original SVID version of this struct, defined on most slouken@1330: systems with mallinfo, declares all fields as ints. But some others slouken@1330: define as unsigned long. If your system defines the fields using a slouken@1330: type of different width than listed here, you MUST #include your slouken@1330: system version and #define HAVE_USR_INCLUDE_MALLOC_H. slouken@1330: */ slouken@1330: slouken@1330: /* #define HAVE_USR_INCLUDE_MALLOC_H */ slouken@1330: slouken@1330: #ifdef HAVE_USR_INCLUDE_MALLOC_H slouken@1330: #include "/usr/include/malloc.h" slouken@1330: #else /* HAVE_USR_INCLUDE_MALLOC_H */ slouken@1330: slouken@1895: struct mallinfo slouken@1895: { slouken@1895: MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */ slouken@1895: MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */ slouken@1895: MALLINFO_FIELD_TYPE smblks; /* always 0 */ slouken@1895: MALLINFO_FIELD_TYPE hblks; /* always 0 */ slouken@1895: MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */ slouken@1895: MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */ slouken@1895: MALLINFO_FIELD_TYPE fsmblks; /* always 0 */ slouken@1895: MALLINFO_FIELD_TYPE uordblks; /* total allocated space */ slouken@1895: MALLINFO_FIELD_TYPE fordblks; /* total free space */ slouken@1895: MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ slouken@1330: }; slouken@1330: slouken@1330: #endif /* HAVE_USR_INCLUDE_MALLOC_H */ slouken@1330: #endif /* NO_MALLINFO */ slouken@1330: slouken@1330: #ifdef __cplusplus slouken@1895: extern "C" slouken@1895: { slouken@1895: #endif /* __cplusplus */ slouken@1330: slouken@1330: #if !ONLY_MSPACES slouken@1330: slouken@1330: /* ------------------- Declarations of public routines ------------------- */ slouken@1330: slouken@1330: #ifndef USE_DL_PREFIX slouken@1330: #define dlcalloc calloc slouken@1330: #define dlfree free slouken@1330: #define dlmalloc malloc slouken@1330: #define dlmemalign memalign slouken@1330: #define dlrealloc realloc slouken@1330: #define dlvalloc valloc slouken@1330: #define dlpvalloc pvalloc slouken@1330: #define dlmallinfo mallinfo slouken@1330: #define dlmallopt mallopt slouken@1330: #define dlmalloc_trim malloc_trim slouken@1330: #define dlmalloc_stats malloc_stats slouken@1330: #define dlmalloc_usable_size malloc_usable_size slouken@1330: #define dlmalloc_footprint malloc_footprint slouken@1330: #define dlmalloc_max_footprint malloc_max_footprint slouken@1330: #define dlindependent_calloc independent_calloc slouken@1330: #define dlindependent_comalloc independent_comalloc slouken@1895: #endif /* USE_DL_PREFIX */ slouken@1330: slouken@1330: slouken@1330: /* slouken@1330: malloc(size_t n) slouken@1330: Returns a pointer to a newly allocated chunk of at least n bytes, or slouken@1330: null if no space is available, in which case errno is set to ENOMEM slouken@1330: on ANSI C systems. slouken@1330: slouken@1330: If n is zero, malloc returns a minimum-sized chunk. (The minimum slouken@1330: size is 16 bytes on most 32bit systems, and 32 bytes on 64bit slouken@1330: systems.) Note that size_t is an unsigned type, so calls with slouken@1330: arguments that would be negative if signed are interpreted as slouken@1330: requests for huge amounts of space, which will often fail. The slouken@1330: maximum supported value of n differs across systems, but is in all slouken@1330: cases less than the maximum representable value of a size_t. slouken@1330: */ slouken@1895: void *dlmalloc(size_t); slouken@1330: slouken@1330: /* slouken@1330: free(void* p) slouken@1330: Releases the chunk of memory pointed to by p, that had been previously slouken@1330: allocated using malloc or a related routine such as realloc. slouken@1330: It has no effect if p is null. If p was not malloced or already slouken@1330: freed, free(p) will by default cause the current program to abort. slouken@1330: */ slouken@1895: void dlfree(void *); slouken@1330: slouken@1330: /* slouken@1330: calloc(size_t n_elements, size_t element_size); slouken@1330: Returns a pointer to n_elements * element_size bytes, with all locations slouken@1330: set to zero. slouken@1330: */ slouken@1895: void *dlcalloc(size_t, size_t); slouken@1330: slouken@1330: /* slouken@1330: realloc(void* p, size_t n) slouken@1330: Returns a pointer to a chunk of size n that contains the same data slouken@1330: as does chunk p up to the minimum of (n, p's size) bytes, or null slouken@1330: if no space is available. slouken@1330: slouken@1330: The returned pointer may or may not be the same as p. The algorithm slouken@1330: prefers extending p in most cases when possible, otherwise it slouken@1330: employs the equivalent of a malloc-copy-free sequence. slouken@1330: slouken@1330: If p is null, realloc is equivalent to malloc. slouken@1330: slouken@1330: If space is not available, realloc returns null, errno is set (if on slouken@1330: ANSI) and p is NOT freed. slouken@1330: slouken@1330: if n is for fewer bytes than already held by p, the newly unused slouken@1330: space is lopped off and freed if possible. realloc with a size slouken@1330: argument of zero (re)allocates a minimum-sized chunk. slouken@1330: slouken@1330: The old unix realloc convention of allowing the last-free'd chunk slouken@1330: to be used as an argument to realloc is not supported. slouken@1330: */ slouken@1330: slouken@1895: void *dlrealloc(void *, size_t); slouken@1330: slouken@1330: /* slouken@1330: memalign(size_t alignment, size_t n); slouken@1330: Returns a pointer to a newly allocated chunk of n bytes, aligned slouken@1330: in accord with the alignment argument. slouken@1330: slouken@1330: The alignment argument should be a power of two. If the argument is slouken@1330: not a power of two, the nearest greater power is used. slouken@1330: 8-byte alignment is guaranteed by normal malloc calls, so don't slouken@1330: bother calling memalign with an argument of 8 or less. slouken@1330: slouken@1330: Overreliance on memalign is a sure way to fragment space. slouken@1330: */ slouken@1895: void *dlmemalign(size_t, size_t); slouken@1330: slouken@1330: /* slouken@1330: valloc(size_t n); slouken@1330: Equivalent to memalign(pagesize, n), where pagesize is the page slouken@1330: size of the system. If the pagesize is unknown, 4096 is used. slouken@1330: */ slouken@1895: void *dlvalloc(size_t); slouken@1330: slouken@1330: /* slouken@1330: mallopt(int parameter_number, int parameter_value) slouken@1330: Sets tunable parameters The format is to provide a slouken@1330: (parameter-number, parameter-value) pair. mallopt then sets the slouken@1330: corresponding parameter to the argument value if it can (i.e., so slouken@1330: long as the value is meaningful), and returns 1 if successful else slouken@1330: 0. SVID/XPG/ANSI defines four standard param numbers for mallopt, slouken@1330: normally defined in malloc.h. None of these are use in this malloc, slouken@1330: so setting them has no effect. But this malloc also supports other slouken@1330: options in mallopt. See below for details. Briefly, supported slouken@1330: parameters are as follows (listed defaults are for "typical" slouken@1330: configurations). slouken@1330: slouken@1330: Symbol param # default allowed param values slouken@1330: M_TRIM_THRESHOLD -1 2*1024*1024 any (MAX_SIZE_T disables) slouken@1330: M_GRANULARITY -2 page size any power of 2 >= page size slouken@1330: M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) slouken@1330: */ slouken@1895: int dlmallopt(int, int); slouken@1330: slouken@1330: /* slouken@1330: malloc_footprint(); slouken@1330: Returns the number of bytes obtained from the system. The total slouken@1330: number of bytes allocated by malloc, realloc etc., is less than this slouken@1330: value. Unlike mallinfo, this function returns only a precomputed slouken@1330: result, so can be called frequently to monitor memory consumption. slouken@1330: Even if locks are otherwise defined, this function does not use them, slouken@1330: so results might not be up to date. slouken@1330: */ slouken@1895: size_t dlmalloc_footprint(void); slouken@1330: slouken@1330: /* slouken@1330: malloc_max_footprint(); slouken@1330: Returns the maximum number of bytes obtained from the system. This slouken@1330: value will be greater than current footprint if deallocated space slouken@1330: has been reclaimed by the system. The peak number of bytes allocated slouken@1330: by malloc, realloc etc., is less than this value. Unlike mallinfo, slouken@1330: this function returns only a precomputed result, so can be called slouken@1330: frequently to monitor memory consumption. Even if locks are slouken@1330: otherwise defined, this function does not use them, so results might slouken@1330: not be up to date. slouken@1330: */ slouken@1895: size_t dlmalloc_max_footprint(void); slouken@1330: slouken@1330: #if !NO_MALLINFO slouken@1330: /* slouken@1330: mallinfo() slouken@1330: Returns (by copy) a struct containing various summary statistics: slouken@1330: slouken@1330: arena: current total non-mmapped bytes allocated from system slouken@1330: ordblks: the number of free chunks slouken@1330: smblks: always zero. slouken@1330: hblks: current number of mmapped regions slouken@1330: hblkhd: total bytes held in mmapped regions slouken@1330: usmblks: the maximum total allocated space. This will be greater slouken@1330: than current total if trimming has occurred. slouken@1330: fsmblks: always zero slouken@1330: uordblks: current total allocated space (normal or mmapped) slouken@1330: fordblks: total free space slouken@1330: keepcost: the maximum number of bytes that could ideally be released slouken@1330: back to system via malloc_trim. ("ideally" means that slouken@1330: it ignores page restrictions etc.) slouken@1330: slouken@1330: Because these fields are ints, but internal bookkeeping may slouken@1330: be kept as longs, the reported values may wrap around zero and slouken@1330: thus be inaccurate. slouken@1330: */ slouken@1895: struct mallinfo dlmallinfo(void); slouken@1895: #endif /* NO_MALLINFO */ slouken@1330: slouken@1330: /* slouken@1330: independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); slouken@1330: slouken@1330: independent_calloc is similar to calloc, but instead of returning a slouken@1330: single cleared space, it returns an array of pointers to n_elements slouken@1330: independent elements that can hold contents of size elem_size, each slouken@1330: of which starts out cleared, and can be independently freed, slouken@1330: realloc'ed etc. The elements are guaranteed to be adjacently slouken@1330: allocated (this is not guaranteed to occur with multiple callocs or slouken@1330: mallocs), which may also improve cache locality in some slouken@1330: applications. slouken@1330: slouken@1330: The "chunks" argument is optional (i.e., may be null, which is slouken@1330: probably the most typical usage). If it is null, the returned array slouken@1330: is itself dynamically allocated and should also be freed when it is slouken@1330: no longer needed. Otherwise, the chunks array must be of at least slouken@1330: n_elements in length. It is filled in with the pointers to the slouken@1330: chunks. slouken@1330: slouken@1330: In either case, independent_calloc returns this pointer array, or slouken@1330: null if the allocation failed. If n_elements is zero and "chunks" slouken@1330: is null, it returns a chunk representing an array with zero elements slouken@1330: (which should be freed if not wanted). slouken@1330: slouken@1330: Each element must be individually freed when it is no longer slouken@1330: needed. If you'd like to instead be able to free all at once, you slouken@1330: should instead use regular calloc and assign pointers into this slouken@1330: space to represent elements. (In this case though, you cannot slouken@1330: independently free elements.) slouken@1330: slouken@1330: independent_calloc simplifies and speeds up implementations of many slouken@1330: kinds of pools. It may also be useful when constructing large data slouken@1330: structures that initially have a fixed number of fixed-sized nodes, slouken@1330: but the number is not known at compile time, and some of the nodes slouken@1330: may later need to be freed. For example: slouken@1330: slouken@1330: struct Node { int item; struct Node* next; }; slouken@1330: slouken@1330: struct Node* build_list() { slouken@1330: struct Node** pool; slouken@1330: int n = read_number_of_nodes_needed(); slouken@1330: if (n <= 0) return 0; slouken@1330: pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0); slouken@1330: if (pool == 0) die(); slouken@1330: // organize into a linked list... slouken@1330: struct Node* first = pool[0]; slouken@1330: for (i = 0; i < n-1; ++i) slouken@1330: pool[i]->next = pool[i+1]; slouken@1330: free(pool); // Can now free the array (or not, if it is needed later) slouken@1330: return first; slouken@1330: } slouken@1330: */ slouken@1895: void **dlindependent_calloc(size_t, size_t, void **); slouken@1330: slouken@1330: /* slouken@1330: independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); slouken@1330: slouken@1330: independent_comalloc allocates, all at once, a set of n_elements slouken@1330: chunks with sizes indicated in the "sizes" array. It returns slouken@1330: an array of pointers to these elements, each of which can be slouken@1330: independently freed, realloc'ed etc. The elements are guaranteed to slouken@1330: be adjacently allocated (this is not guaranteed to occur with slouken@1330: multiple callocs or mallocs), which may also improve cache locality slouken@1330: in some applications. slouken@1330: slouken@1330: The "chunks" argument is optional (i.e., may be null). If it is null slouken@1330: the returned array is itself dynamically allocated and should also slouken@1330: be freed when it is no longer needed. Otherwise, the chunks array slouken@1330: must be of at least n_elements in length. It is filled in with the slouken@1330: pointers to the chunks. slouken@1330: slouken@1330: In either case, independent_comalloc returns this pointer array, or slouken@1330: null if the allocation failed. If n_elements is zero and chunks is slouken@1330: null, it returns a chunk representing an array with zero elements slouken@1330: (which should be freed if not wanted). slouken@1330: slouken@1330: Each element must be individually freed when it is no longer slouken@1330: needed. If you'd like to instead be able to free all at once, you slouken@1330: should instead use a single regular malloc, and assign pointers at slouken@1330: particular offsets in the aggregate space. (In this case though, you slouken@1330: cannot independently free elements.) slouken@1330: slouken@1330: independent_comallac differs from independent_calloc in that each slouken@1330: element may have a different size, and also that it does not slouken@1330: automatically clear elements. slouken@1330: slouken@1330: independent_comalloc can be used to speed up allocation in cases slouken@1330: where several structs or objects must always be allocated at the slouken@1330: same time. For example: slouken@1330: slouken@1330: struct Head { ... } slouken@1330: struct Foot { ... } slouken@1330: slouken@1330: void send_message(char* msg) { slouken@1330: int msglen = strlen(msg); slouken@1330: size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; slouken@1330: void* chunks[3]; slouken@1330: if (independent_comalloc(3, sizes, chunks) == 0) slouken@1330: die(); slouken@1330: struct Head* head = (struct Head*)(chunks[0]); slouken@1330: char* body = (char*)(chunks[1]); slouken@1330: struct Foot* foot = (struct Foot*)(chunks[2]); slouken@1330: // ... slouken@1330: } slouken@1330: slouken@1330: In general though, independent_comalloc is worth using only for slouken@1330: larger values of n_elements. For small values, you probably won't slouken@1330: detect enough difference from series of malloc calls to bother. slouken@1330: slouken@1330: Overuse of independent_comalloc can increase overall memory usage, slouken@1330: since it cannot reuse existing noncontiguous small chunks that slouken@1330: might be available for some of the elements. slouken@1330: */ slouken@1895: void **dlindependent_comalloc(size_t, size_t *, void **); slouken@1330: slouken@1330: slouken@1330: /* slouken@1330: pvalloc(size_t n); slouken@1330: Equivalent to valloc(minimum-page-that-holds(n)), that is, slouken@1330: round up n to nearest pagesize. slouken@1330: */ slouken@1895: void *dlpvalloc(size_t); slouken@1330: slouken@1330: /* slouken@1330: malloc_trim(size_t pad); slouken@1330: slouken@1330: If possible, gives memory back to the system (via negative arguments slouken@1330: to sbrk) if there is unused memory at the `high' end of the malloc slouken@1330: pool or in unused MMAP segments. You can call this after freeing slouken@1330: large blocks of memory to potentially reduce the system-level memory slouken@1330: requirements of a program. However, it cannot guarantee to reduce slouken@1330: memory. Under some allocation patterns, some large free blocks of slouken@1330: memory will be locked between two used chunks, so they cannot be slouken@1330: given back to the system. slouken@1330: slouken@1330: The `pad' argument to malloc_trim represents the amount of free slouken@1330: trailing space to leave untrimmed. If this argument is zero, only slouken@1330: the minimum amount of memory to maintain internal data structures slouken@1330: will be left. Non-zero arguments can be supplied to maintain enough slouken@1330: trailing space to service future expected allocations without having slouken@1330: to re-obtain memory from the system. slouken@1330: slouken@1330: Malloc_trim returns 1 if it actually released any memory, else 0. slouken@1330: */ slouken@1895: int dlmalloc_trim(size_t); slouken@1330: slouken@1330: /* slouken@1330: malloc_usable_size(void* p); slouken@1330: slouken@1330: Returns the number of bytes you can actually use in slouken@1330: an allocated chunk, which may be more than you requested (although slouken@1330: often not) due to alignment and minimum size constraints. slouken@1330: You can use this many bytes without worrying about slouken@1330: overwriting other allocated objects. This is not a particularly great slouken@1330: programming practice. malloc_usable_size can be more useful in slouken@1330: debugging and assertions, for example: slouken@1330: slouken@1330: p = malloc(n); slouken@1330: assert(malloc_usable_size(p) >= 256); slouken@1330: */ slouken@1895: size_t dlmalloc_usable_size(void *); slouken@1330: slouken@1330: /* slouken@1330: malloc_stats(); slouken@1330: Prints on stderr the amount of space obtained from the system (both slouken@1330: via sbrk and mmap), the maximum amount (which may be more than slouken@1330: current if malloc_trim and/or munmap got called), and the current slouken@1330: number of bytes allocated via malloc (or realloc, etc) but not yet slouken@1330: freed. Note that this is the number of bytes allocated, not the slouken@1330: number requested. It will be larger than the number requested slouken@1330: because of alignment and bookkeeping overhead. Because it includes slouken@1330: alignment wastage as being in use, this figure may be greater than slouken@1330: zero even when no user-level chunks are allocated. slouken@1330: slouken@1330: The reported current and maximum system memory can be inaccurate if slouken@1330: a program makes other calls to system memory allocation functions slouken@1330: (normally sbrk) outside of malloc. slouken@1330: slouken@1330: malloc_stats prints only the most commonly interesting statistics. slouken@1330: More information can be obtained by calling mallinfo. slouken@1330: */ slouken@1895: void dlmalloc_stats(void); slouken@1895: slouken@1895: #endif /* ONLY_MSPACES */ slouken@1330: slouken@1330: #if MSPACES slouken@1330: slouken@1330: /* slouken@1330: mspace is an opaque type representing an independent slouken@1330: region of space that supports mspace_malloc, etc. slouken@1330: */ slouken@1895: typedef void *mspace; slouken@1330: slouken@1330: /* slouken@1330: create_mspace creates and returns a new independent space with the slouken@1330: given initial capacity, or, if 0, the default granularity size. It slouken@1330: returns null if there is no system memory available to create the slouken@1330: space. If argument locked is non-zero, the space uses a separate slouken@1330: lock to control access. The capacity of the space will grow slouken@1330: dynamically as needed to service mspace_malloc requests. You can slouken@1330: control the sizes of incremental increases of this space by slouken@1330: compiling with a different DEFAULT_GRANULARITY or dynamically slouken@1330: setting with mallopt(M_GRANULARITY, value). slouken@1330: */ slouken@1895: mspace create_mspace(size_t capacity, int locked); slouken@1330: slouken@1330: /* slouken@1330: destroy_mspace destroys the given space, and attempts to return all slouken@1330: of its memory back to the system, returning the total number of slouken@1330: bytes freed. After destruction, the results of access to all memory slouken@1330: used by the space become undefined. slouken@1330: */ slouken@1895: size_t destroy_mspace(mspace msp); slouken@1330: slouken@1330: /* slouken@1330: create_mspace_with_base uses the memory supplied as the initial base slouken@1330: of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this slouken@1330: space is used for bookkeeping, so the capacity must be at least this slouken@1330: large. (Otherwise 0 is returned.) When this initial space is slouken@1330: exhausted, additional memory will be obtained from the system. slouken@1330: Destroying this space will deallocate all additionally allocated slouken@1330: space (if possible) but not the initial base. slouken@1330: */ slouken@1895: mspace create_mspace_with_base(void *base, size_t capacity, int locked); slouken@1330: slouken@1330: /* slouken@1330: mspace_malloc behaves as malloc, but operates within slouken@1330: the given space. slouken@1330: */ slouken@1895: void *mspace_malloc(mspace msp, size_t bytes); slouken@1330: slouken@1330: /* slouken@1330: mspace_free behaves as free, but operates within slouken@1330: the given space. slouken@1330: slouken@1330: If compiled with FOOTERS==1, mspace_free is not actually needed. slouken@1330: free may be called instead of mspace_free because freed chunks from slouken@1330: any space are handled by their originating spaces. slouken@1330: */ slouken@1895: void mspace_free(mspace msp, void *mem); slouken@1330: slouken@1330: /* slouken@1330: mspace_realloc behaves as realloc, but operates within slouken@1330: the given space. slouken@1330: slouken@1330: If compiled with FOOTERS==1, mspace_realloc is not actually slouken@1330: needed. realloc may be called instead of mspace_realloc because slouken@1330: realloced chunks from any space are handled by their originating slouken@1330: spaces. slouken@1330: */ slouken@1895: void *mspace_realloc(mspace msp, void *mem, size_t newsize); slouken@1330: slouken@1330: /* slouken@1330: mspace_calloc behaves as calloc, but operates within slouken@1330: the given space. slouken@1330: */ slouken@1895: void *mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); slouken@1330: slouken@1330: /* slouken@1330: mspace_memalign behaves as memalign, but operates within slouken@1330: the given space. slouken@1330: */ slouken@1895: void *mspace_memalign(mspace msp, size_t alignment, size_t bytes); slouken@1330: slouken@1330: /* slouken@1330: mspace_independent_calloc behaves as independent_calloc, but slouken@1330: operates within the given space. slouken@1330: */ slouken@1895: void **mspace_independent_calloc(mspace msp, size_t n_elements, slouken@1895: size_t elem_size, void *chunks[]); slouken@1330: slouken@1330: /* slouken@1330: mspace_independent_comalloc behaves as independent_comalloc, but slouken@1330: operates within the given space. slouken@1330: */ slouken@1895: void **mspace_independent_comalloc(mspace msp, size_t n_elements, slouken@1895: size_t sizes[], void *chunks[]); slouken@1330: slouken@1330: /* slouken@1330: mspace_footprint() returns the number of bytes obtained from the slouken@1330: system for this space. slouken@1330: */ slouken@1895: size_t mspace_footprint(mspace msp); slouken@1330: slouken@1330: /* slouken@1330: mspace_max_footprint() returns the peak number of bytes obtained from the slouken@1330: system for this space. slouken@1330: */ slouken@1895: size_t mspace_max_footprint(mspace msp); slouken@1330: slouken@1330: slouken@1330: #if !NO_MALLINFO slouken@1330: /* slouken@1330: mspace_mallinfo behaves as mallinfo, but reports properties of slouken@1330: the given space. slouken@1330: */ slouken@1895: struct mallinfo mspace_mallinfo(mspace msp); slouken@1895: #endif /* NO_MALLINFO */ slouken@1330: slouken@1330: /* slouken@1330: mspace_malloc_stats behaves as malloc_stats, but reports slouken@1330: properties of the given space. slouken@1330: */ slouken@1895: void mspace_malloc_stats(mspace msp); slouken@1330: slouken@1330: /* slouken@1330: mspace_trim behaves as malloc_trim, but slouken@1330: operates within the given space. slouken@1330: */ slouken@1895: int mspace_trim(mspace msp, size_t pad); slouken@1330: slouken@1330: /* slouken@1330: An alias for mallopt. slouken@1330: */ slouken@1895: int mspace_mallopt(int, int); slouken@1895: slouken@1895: #endif /* MSPACES */ slouken@1330: slouken@1330: #ifdef __cplusplus slouken@1895: }; /* end of extern "C" */ slouken@1330: #endif /* __cplusplus */ slouken@1330: slouken@1330: /* slouken@1330: ======================================================================== slouken@1330: To make a fully customizable malloc.h header file, cut everything slouken@1330: above this line, put into file malloc.h, edit to suit, and #include it slouken@1330: on the next line, as well as in programs that use this malloc. slouken@1330: ======================================================================== slouken@1330: */ slouken@1330: slouken@1330: /* #include "malloc.h" */ slouken@1330: slouken@1330: /*------------------------------ internal #includes ---------------------- */ slouken@1330: slouken@1330: #ifdef _MSC_VER slouken@1895: #pragma warning( disable : 4146 ) /* no "unsigned" warnings */ slouken@1330: #endif /* _MSC_VER */ slouken@1330: slouken@1330: #ifndef LACKS_STDIO_H slouken@1895: #include /* for printing in malloc_stats */ slouken@1330: #endif slouken@1330: slouken@1330: #ifndef LACKS_ERRNO_H slouken@1895: #include /* for MALLOC_FAILURE_ACTION */ slouken@1330: #endif /* LACKS_ERRNO_H */ slouken@1330: #if FOOTERS slouken@1895: #include /* for magic initialization */ slouken@1330: #endif /* FOOTERS */ slouken@1330: #ifndef LACKS_STDLIB_H slouken@1895: #include /* for abort() */ slouken@1330: #endif /* LACKS_STDLIB_H */ slouken@1330: #ifdef DEBUG slouken@1330: #if ABORT_ON_ASSERT_FAILURE slouken@1330: #define assert(x) if(!(x)) ABORT slouken@1330: #else /* ABORT_ON_ASSERT_FAILURE */ slouken@1330: #include slouken@1330: #endif /* ABORT_ON_ASSERT_FAILURE */ slouken@1895: #else /* DEBUG */ slouken@1330: #define assert(x) slouken@1330: #endif /* DEBUG */ slouken@1330: #ifndef LACKS_STRING_H slouken@1895: #include /* for memset etc */ slouken@1895: #endif /* LACKS_STRING_H */ slouken@1330: #if USE_BUILTIN_FFS slouken@1330: #ifndef LACKS_STRINGS_H slouken@1895: #include /* for ffs */ slouken@1330: #endif /* LACKS_STRINGS_H */ slouken@1330: #endif /* USE_BUILTIN_FFS */ slouken@1330: #if HAVE_MMAP slouken@1330: #ifndef LACKS_SYS_MMAN_H slouken@1895: #include /* for mmap */ slouken@1330: #endif /* LACKS_SYS_MMAN_H */ slouken@1330: #ifndef LACKS_FCNTL_H slouken@1330: #include slouken@1330: #endif /* LACKS_FCNTL_H */ slouken@1330: #endif /* HAVE_MMAP */ slouken@1330: #if HAVE_MORECORE slouken@1330: #ifndef LACKS_UNISTD_H slouken@1895: #include /* for sbrk */ slouken@1330: #else /* LACKS_UNISTD_H */ slouken@1330: #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) slouken@1895: extern void *sbrk(ptrdiff_t); slouken@1330: #endif /* FreeBSD etc */ slouken@1330: #endif /* LACKS_UNISTD_H */ slouken@1330: #endif /* HAVE_MMAP */ slouken@1330: slouken@1330: #ifndef WIN32 slouken@1330: #ifndef malloc_getpagesize slouken@1895: # ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */ slouken@1330: # ifndef _SC_PAGE_SIZE slouken@1330: # define _SC_PAGE_SIZE _SC_PAGESIZE slouken@1330: # endif slouken@1330: # endif slouken@1330: # ifdef _SC_PAGE_SIZE slouken@1330: # define malloc_getpagesize sysconf(_SC_PAGE_SIZE) slouken@1330: # else slouken@1330: # if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE) slouken@1895: extern size_t getpagesize(); slouken@1330: # define malloc_getpagesize getpagesize() slouken@1330: # else slouken@1895: # ifdef WIN32 /* use supplied emulation of getpagesize */ slouken@1330: # define malloc_getpagesize getpagesize() slouken@1330: # else slouken@1330: # ifndef LACKS_SYS_PARAM_H slouken@1330: # include slouken@1330: # endif slouken@1330: # ifdef EXEC_PAGESIZE slouken@1330: # define malloc_getpagesize EXEC_PAGESIZE slouken@1330: # else slouken@1330: # ifdef NBPG slouken@1330: # ifndef CLSIZE slouken@1330: # define malloc_getpagesize NBPG slouken@1330: # else slouken@1330: # define malloc_getpagesize (NBPG * CLSIZE) slouken@1330: # endif slouken@1330: # else slouken@1330: # ifdef NBPC slouken@1330: # define malloc_getpagesize NBPC slouken@1330: # else slouken@1330: # ifdef PAGESIZE slouken@1330: # define malloc_getpagesize PAGESIZE slouken@1330: # else /* just guess */ slouken@1330: # define malloc_getpagesize ((size_t)4096U) slouken@1330: # endif slouken@1330: # endif slouken@1330: # endif slouken@1330: # endif slouken@1330: # endif slouken@1330: # endif slouken@1330: # endif slouken@1330: #endif slouken@1330: #endif slouken@1330: slouken@1330: /* ------------------- size_t and alignment properties -------------------- */ slouken@1330: slouken@1330: /* The byte and bit size of a size_t */ slouken@1330: #define SIZE_T_SIZE (sizeof(size_t)) slouken@1330: #define SIZE_T_BITSIZE (sizeof(size_t) << 3) slouken@1330: slouken@1330: /* Some constants coerced to size_t */ slouken@1330: /* Annoying but necessary to avoid errors on some plaftorms */ slouken@1330: #define SIZE_T_ZERO ((size_t)0) slouken@1330: #define SIZE_T_ONE ((size_t)1) slouken@1330: #define SIZE_T_TWO ((size_t)2) slouken@1330: #define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1) slouken@1330: #define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2) slouken@1330: #define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES) slouken@1330: #define HALF_MAX_SIZE_T (MAX_SIZE_T / 2U) slouken@1330: slouken@1330: /* The bit mask value corresponding to MALLOC_ALIGNMENT */ slouken@1330: #define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE) slouken@1330: slouken@1330: /* True if address a has acceptable alignment */ slouken@1330: #define is_aligned(A) (((size_t)((A)) & (CHUNK_ALIGN_MASK)) == 0) slouken@1330: slouken@1330: /* the number of bytes to offset an address to align it */ slouken@1330: #define align_offset(A)\ slouken@1330: ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\ slouken@1330: ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK)) slouken@1330: slouken@1330: /* -------------------------- MMAP preliminaries ------------------------- */ slouken@1330: slouken@1330: /* slouken@1330: If HAVE_MORECORE or HAVE_MMAP are false, we just define calls and slouken@1330: checks to fail so compiler optimizer can delete code rather than slouken@1330: using so many "#if"s. slouken@1330: */ slouken@1330: slouken@1330: slouken@1330: /* MORECORE and MMAP must return MFAIL on failure */ slouken@1330: #define MFAIL ((void*)(MAX_SIZE_T)) slouken@1895: #define CMFAIL ((char*)(MFAIL)) /* defined for convenience */ slouken@1330: slouken@1330: #if !HAVE_MMAP slouken@1330: #define IS_MMAPPED_BIT (SIZE_T_ZERO) slouken@1330: #define USE_MMAP_BIT (SIZE_T_ZERO) slouken@1330: #define CALL_MMAP(s) MFAIL slouken@1330: #define CALL_MUNMAP(a, s) (-1) slouken@1330: #define DIRECT_MMAP(s) MFAIL slouken@1330: slouken@1330: #else /* HAVE_MMAP */ slouken@1330: #define IS_MMAPPED_BIT (SIZE_T_ONE) slouken@1330: #define USE_MMAP_BIT (SIZE_T_ONE) slouken@1330: slouken@1330: #ifndef WIN32 slouken@1330: #define CALL_MUNMAP(a, s) munmap((a), (s)) slouken@1330: #define MMAP_PROT (PROT_READ|PROT_WRITE) slouken@1330: #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) slouken@1330: #define MAP_ANONYMOUS MAP_ANON slouken@1330: #endif /* MAP_ANON */ slouken@1330: #ifdef MAP_ANONYMOUS slouken@1330: #define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS) slouken@1330: #define CALL_MMAP(s) mmap(0, (s), MMAP_PROT, MMAP_FLAGS, -1, 0) slouken@1330: #else /* MAP_ANONYMOUS */ slouken@1330: /* slouken@1330: Nearly all versions of mmap support MAP_ANONYMOUS, so the following slouken@1330: is unlikely to be needed, but is supplied just in case. slouken@1330: */ slouken@1330: #define MMAP_FLAGS (MAP_PRIVATE) slouken@1895: static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */ slouken@1330: #define CALL_MMAP(s) ((dev_zero_fd < 0) ? \ slouken@1330: (dev_zero_fd = open("/dev/zero", O_RDWR), \ slouken@1330: mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) : \ slouken@1330: mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) slouken@1330: #endif /* MAP_ANONYMOUS */ slouken@1330: slouken@1330: #define DIRECT_MMAP(s) CALL_MMAP(s) slouken@1330: #else /* WIN32 */ slouken@1330: slouken@1330: /* Win32 MMAP via VirtualAlloc */ slouken@1895: static void * slouken@1895: win32mmap(size_t size) slouken@1895: { slouken@1895: void *ptr = slouken@1895: VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); slouken@1895: return (ptr != 0) ? ptr : MFAIL; slouken@1330: } slouken@1330: slouken@1330: /* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ slouken@1895: static void * slouken@1895: win32direct_mmap(size_t size) slouken@1895: { slouken@1895: void *ptr = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, slouken@1895: PAGE_READWRITE); slouken@1895: return (ptr != 0) ? ptr : MFAIL; slouken@1330: } slouken@1330: slouken@1330: /* This function supports releasing coalesed segments */ slouken@1895: static int slouken@1895: win32munmap(void *ptr, size_t size) slouken@1895: { slouken@1895: MEMORY_BASIC_INFORMATION minfo; slouken@1895: char *cptr = ptr; slouken@1895: while (size) { slouken@1895: if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0) slouken@1895: return -1; slouken@1895: if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr || slouken@1895: minfo.State != MEM_COMMIT || minfo.RegionSize > size) slouken@1895: return -1; slouken@1895: if (VirtualFree(cptr, 0, MEM_RELEASE) == 0) slouken@1895: return -1; slouken@1895: cptr += minfo.RegionSize; slouken@1895: size -= minfo.RegionSize; slouken@1895: } slouken@1895: return 0; slouken@1330: } slouken@1330: slouken@1330: #define CALL_MMAP(s) win32mmap(s) slouken@1330: #define CALL_MUNMAP(a, s) win32munmap((a), (s)) slouken@1330: #define DIRECT_MMAP(s) win32direct_mmap(s) slouken@1330: #endif /* WIN32 */ slouken@1330: #endif /* HAVE_MMAP */ slouken@1330: slouken@1330: #if HAVE_MMAP && HAVE_MREMAP slouken@1330: #define CALL_MREMAP(addr, osz, nsz, mv) mremap((addr), (osz), (nsz), (mv)) slouken@1895: #else /* HAVE_MMAP && HAVE_MREMAP */ slouken@1330: #define CALL_MREMAP(addr, osz, nsz, mv) MFAIL slouken@1330: #endif /* HAVE_MMAP && HAVE_MREMAP */ slouken@1330: slouken@1330: #if HAVE_MORECORE slouken@1330: #define CALL_MORECORE(S) MORECORE(S) slouken@1895: #else /* HAVE_MORECORE */ slouken@1330: #define CALL_MORECORE(S) MFAIL slouken@1330: #endif /* HAVE_MORECORE */ slouken@1330: slouken@1330: /* mstate bit set if continguous morecore disabled or failed */ slouken@1330: #define USE_NONCONTIGUOUS_BIT (4U) slouken@1330: slouken@1330: /* segment bit set in create_mspace_with_base */ slouken@1330: #define EXTERN_BIT (8U) slouken@1330: slouken@1330: slouken@1330: /* --------------------------- Lock preliminaries ------------------------ */ slouken@1330: slouken@1330: #if USE_LOCKS slouken@1330: slouken@1330: /* slouken@1330: When locks are defined, there are up to two global locks: slouken@1330: slouken@1330: * If HAVE_MORECORE, morecore_mutex protects sequences of calls to slouken@1330: MORECORE. In many cases sys_alloc requires two calls, that should slouken@1330: not be interleaved with calls by other threads. This does not slouken@1330: protect against direct calls to MORECORE by other threads not slouken@1330: using this lock, so there is still code to cope the best we can on slouken@1330: interference. slouken@1330: slouken@1330: * magic_init_mutex ensures that mparams.magic and other slouken@1330: unique mparams values are initialized only once. slouken@1330: */ slouken@1330: slouken@1330: #ifndef WIN32 slouken@1330: /* By default use posix locks */ slouken@1330: #include slouken@1330: #define MLOCK_T pthread_mutex_t slouken@1330: #define INITIAL_LOCK(l) pthread_mutex_init(l, NULL) slouken@1330: #define ACQUIRE_LOCK(l) pthread_mutex_lock(l) slouken@1330: #define RELEASE_LOCK(l) pthread_mutex_unlock(l) slouken@1330: slouken@1330: #if HAVE_MORECORE slouken@1330: static MLOCK_T morecore_mutex = PTHREAD_MUTEX_INITIALIZER; slouken@1330: #endif /* HAVE_MORECORE */ slouken@1330: slouken@1330: static MLOCK_T magic_init_mutex = PTHREAD_MUTEX_INITIALIZER; slouken@1330: slouken@1330: #else /* WIN32 */ slouken@1330: /* slouken@1330: Because lock-protected regions have bounded times, and there slouken@1330: are no recursive lock calls, we can use simple spinlocks. slouken@1330: */ slouken@1330: slouken@1330: #define MLOCK_T long slouken@1895: static int slouken@1895: win32_acquire_lock(MLOCK_T * sl) slouken@1895: { slouken@1895: for (;;) { slouken@1330: #ifdef InterlockedCompareExchangePointer slouken@1895: if (!InterlockedCompareExchange(sl, 1, 0)) slouken@1895: return 0; slouken@1895: #else /* Use older void* version */ slouken@1895: if (!InterlockedCompareExchange((void **) sl, (void *) 1, (void *) 0)) slouken@1895: return 0; slouken@1330: #endif /* InterlockedCompareExchangePointer */ slouken@1895: Sleep(0); slouken@1895: } slouken@1330: } slouken@1330: slouken@1895: static void slouken@1895: win32_release_lock(MLOCK_T * sl) slouken@1895: { slouken@1895: InterlockedExchange(sl, 0); slouken@1330: } slouken@1330: slouken@1330: #define INITIAL_LOCK(l) *(l)=0 slouken@1330: #define ACQUIRE_LOCK(l) win32_acquire_lock(l) slouken@1330: #define RELEASE_LOCK(l) win32_release_lock(l) slouken@1330: #if HAVE_MORECORE slouken@1330: static MLOCK_T morecore_mutex; slouken@1330: #endif /* HAVE_MORECORE */ slouken@1330: static MLOCK_T magic_init_mutex; slouken@1330: #endif /* WIN32 */ slouken@1330: slouken@1330: #define USE_LOCK_BIT (2U) slouken@1895: #else /* USE_LOCKS */ slouken@1330: #define USE_LOCK_BIT (0U) slouken@1330: #define INITIAL_LOCK(l) slouken@1330: #endif /* USE_LOCKS */ slouken@1330: slouken@1330: #if USE_LOCKS && HAVE_MORECORE slouken@1330: #define ACQUIRE_MORECORE_LOCK() ACQUIRE_LOCK(&morecore_mutex); slouken@1330: #define RELEASE_MORECORE_LOCK() RELEASE_LOCK(&morecore_mutex); slouken@1330: #else /* USE_LOCKS && HAVE_MORECORE */ slouken@1330: #define ACQUIRE_MORECORE_LOCK() slouken@1330: #define RELEASE_MORECORE_LOCK() slouken@1330: #endif /* USE_LOCKS && HAVE_MORECORE */ slouken@1330: slouken@1330: #if USE_LOCKS slouken@1330: #define ACQUIRE_MAGIC_INIT_LOCK() ACQUIRE_LOCK(&magic_init_mutex); slouken@1330: #define RELEASE_MAGIC_INIT_LOCK() RELEASE_LOCK(&magic_init_mutex); slouken@1895: #else /* USE_LOCKS */ slouken@1330: #define ACQUIRE_MAGIC_INIT_LOCK() slouken@1330: #define RELEASE_MAGIC_INIT_LOCK() slouken@1330: #endif /* USE_LOCKS */ slouken@1330: slouken@1330: slouken@1330: /* ----------------------- Chunk representations ------------------------ */ slouken@1330: slouken@1330: /* slouken@1330: (The following includes lightly edited explanations by Colin Plumb.) slouken@1330: slouken@1330: The malloc_chunk declaration below is misleading (but accurate and slouken@1330: necessary). It declares a "view" into memory allowing access to slouken@1330: necessary fields at known offsets from a given base. slouken@1330: slouken@1330: Chunks of memory are maintained using a `boundary tag' method as slouken@1330: originally described by Knuth. (See the paper by Paul Wilson slouken@1330: ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a survey of such slouken@1330: techniques.) Sizes of free chunks are stored both in the front of slouken@1330: each chunk and at the end. This makes consolidating fragmented slouken@1330: chunks into bigger chunks fast. The head fields also hold bits slouken@1330: representing whether chunks are free or in use. slouken@1330: slouken@1330: Here are some pictures to make it clearer. They are "exploded" to slouken@1330: show that the state of a chunk can be thought of as extending from slouken@1330: the high 31 bits of the head field of its header through the slouken@1330: prev_foot and PINUSE_BIT bit of the following chunk header. slouken@1330: slouken@1330: A chunk that's in use looks like: slouken@1330: slouken@1330: chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Size of previous chunk (if P = 1) | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P| slouken@1330: | Size of this chunk 1| +-+ slouken@1330: mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | | slouken@1330: +- -+ slouken@1330: | | slouken@1330: +- -+ slouken@1330: | : slouken@1330: +- size - sizeof(size_t) available payload bytes -+ slouken@1330: : | slouken@1330: chunk-> +- -+ slouken@1330: | | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1| slouken@1330: | Size of next chunk (may or may not be in use) | +-+ slouken@1330: mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: slouken@1330: And if it's free, it looks like this: slouken@1330: slouken@1330: chunk-> +- -+ slouken@1330: | User payload (must be in use, or we would have merged!) | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P| slouken@1330: | Size of this chunk 0| +-+ slouken@1330: mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Next pointer | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Prev pointer | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | : slouken@1330: +- size - sizeof(struct chunk) unused bytes -+ slouken@1330: : | slouken@1330: chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Size of this chunk | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |0| slouken@1330: | Size of next chunk (must be in use, or we would have merged)| +-+ slouken@1330: mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | : slouken@1330: +- User payload -+ slouken@1330: : | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: |0| slouken@1330: +-+ slouken@1330: Note that since we always merge adjacent free chunks, the chunks slouken@1330: adjacent to a free chunk must be in use. slouken@1330: slouken@1330: Given a pointer to a chunk (which can be derived trivially from the slouken@1330: payload pointer) we can, in O(1) time, find out whether the adjacent slouken@1330: chunks are free, and if so, unlink them from the lists that they slouken@1330: are on and merge them with the current chunk. slouken@1330: slouken@1330: Chunks always begin on even word boundaries, so the mem portion slouken@1330: (which is returned to the user) is also on an even word boundary, and slouken@1330: thus at least double-word aligned. slouken@1330: slouken@1330: The P (PINUSE_BIT) bit, stored in the unused low-order bit of the slouken@1330: chunk size (which is always a multiple of two words), is an in-use slouken@1330: bit for the *previous* chunk. If that bit is *clear*, then the slouken@1330: word before the current chunk size contains the previous chunk slouken@1330: size, and can be used to find the front of the previous chunk. slouken@1330: The very first chunk allocated always has this bit set, preventing slouken@1330: access to non-existent (or non-owned) memory. If pinuse is set for slouken@1330: any given chunk, then you CANNOT determine the size of the slouken@1330: previous chunk, and might even get a memory addressing fault when slouken@1330: trying to do so. slouken@1330: slouken@1330: The C (CINUSE_BIT) bit, stored in the unused second-lowest bit of slouken@1330: the chunk size redundantly records whether the current chunk is slouken@1330: inuse. This redundancy enables usage checks within free and realloc, slouken@1330: and reduces indirection when freeing and consolidating chunks. slouken@1330: slouken@1330: Each freshly allocated chunk must have both cinuse and pinuse set. slouken@1330: That is, each allocated chunk borders either a previously allocated slouken@1330: and still in-use chunk, or the base of its memory arena. This is slouken@1330: ensured by making all allocations from the the `lowest' part of any slouken@1330: found chunk. Further, no free chunk physically borders another one, slouken@1330: so each free chunk is known to be preceded and followed by either slouken@1330: inuse chunks or the ends of memory. slouken@1330: slouken@1330: Note that the `foot' of the current chunk is actually represented slouken@1330: as the prev_foot of the NEXT chunk. This makes it easier to slouken@1330: deal with alignments etc but can be very confusing when trying slouken@1330: to extend or adapt this code. slouken@1330: slouken@1330: The exceptions to all this are slouken@1330: slouken@1330: 1. The special chunk `top' is the top-most available chunk (i.e., slouken@1330: the one bordering the end of available memory). It is treated slouken@1330: specially. Top is never included in any bin, is used only if slouken@1330: no other chunk is available, and is released back to the slouken@1330: system if it is very large (see M_TRIM_THRESHOLD). In effect, slouken@1330: the top chunk is treated as larger (and thus less well slouken@1330: fitting) than any other available chunk. The top chunk slouken@1330: doesn't update its trailing size field since there is no next slouken@1330: contiguous chunk that would have to index off it. However, slouken@1330: space is still allocated for it (TOP_FOOT_SIZE) to enable slouken@1330: separation or merging when space is extended. slouken@1330: slouken@1330: 3. Chunks allocated via mmap, which have the lowest-order bit slouken@1330: (IS_MMAPPED_BIT) set in their prev_foot fields, and do not set slouken@1330: PINUSE_BIT in their head fields. Because they are allocated slouken@1330: one-by-one, each must carry its own prev_foot field, which is slouken@1330: also used to hold the offset this chunk has within its mmapped slouken@1330: region, which is needed to preserve alignment. Each mmapped slouken@1330: chunk is trailed by the first two fields of a fake next-chunk slouken@1330: for sake of usage checks. slouken@1330: slouken@1330: */ slouken@1330: slouken@1895: struct malloc_chunk slouken@1895: { slouken@1895: size_t prev_foot; /* Size of previous chunk (if free). */ slouken@1895: size_t head; /* Size and inuse bits. */ slouken@1895: struct malloc_chunk *fd; /* double links -- used only if free. */ slouken@1895: struct malloc_chunk *bk; slouken@1330: }; slouken@1330: slouken@1895: typedef struct malloc_chunk mchunk; slouken@1895: typedef struct malloc_chunk *mchunkptr; slouken@1895: typedef struct malloc_chunk *sbinptr; /* The type of bins of chunks */ slouken@1895: typedef size_t bindex_t; /* Described below */ slouken@1895: typedef unsigned int binmap_t; /* Described below */ slouken@1895: typedef unsigned int flag_t; /* The type of various bit flag sets */ slouken@1330: slouken@1330: /* ------------------- Chunks sizes and alignments ----------------------- */ slouken@1330: slouken@1330: #define MCHUNK_SIZE (sizeof(mchunk)) slouken@1330: slouken@1330: #if FOOTERS slouken@1330: #define CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) slouken@1330: #else /* FOOTERS */ slouken@1330: #define CHUNK_OVERHEAD (SIZE_T_SIZE) slouken@1330: #endif /* FOOTERS */ slouken@1330: slouken@1330: /* MMapped chunks need a second word of overhead ... */ slouken@1330: #define MMAP_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) slouken@1330: /* ... and additional padding for fake next-chunk at foot */ slouken@1330: #define MMAP_FOOT_PAD (FOUR_SIZE_T_SIZES) slouken@1330: slouken@1330: /* The smallest size we can malloc is an aligned minimal chunk */ slouken@1330: #define MIN_CHUNK_SIZE\ slouken@1330: ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) slouken@1330: slouken@1330: /* conversion from malloc headers to user pointers, and back */ slouken@1330: #define chunk2mem(p) ((void*)((char*)(p) + TWO_SIZE_T_SIZES)) slouken@1330: #define mem2chunk(mem) ((mchunkptr)((char*)(mem) - TWO_SIZE_T_SIZES)) slouken@1330: /* chunk associated with aligned address A */ slouken@1330: #define align_as_chunk(A) (mchunkptr)((A) + align_offset(chunk2mem(A))) slouken@1330: slouken@1330: /* Bounds on request (not chunk) sizes. */ slouken@1330: #define MAX_REQUEST ((-MIN_CHUNK_SIZE) << 2) slouken@1330: #define MIN_REQUEST (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE) slouken@1330: slouken@1330: /* pad request bytes into a usable size */ slouken@1330: #define pad_request(req) \ slouken@1330: (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) slouken@1330: slouken@1330: /* pad request, checking for minimum (but not maximum) */ slouken@1330: #define request2size(req) \ slouken@1330: (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req)) slouken@1330: slouken@1330: slouken@1330: /* ------------------ Operations on head and foot fields ----------------- */ slouken@1330: slouken@1330: /* slouken@1330: The head field of a chunk is or'ed with PINUSE_BIT when previous slouken@1330: adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in slouken@1330: use. If the chunk was obtained with mmap, the prev_foot field has slouken@1330: IS_MMAPPED_BIT set, otherwise holding the offset of the base of the slouken@1330: mmapped region to the base of the chunk. slouken@1330: */ slouken@1330: slouken@1330: #define PINUSE_BIT (SIZE_T_ONE) slouken@1330: #define CINUSE_BIT (SIZE_T_TWO) slouken@1330: #define INUSE_BITS (PINUSE_BIT|CINUSE_BIT) slouken@1330: slouken@1330: /* Head value for fenceposts */ slouken@1330: #define FENCEPOST_HEAD (INUSE_BITS|SIZE_T_SIZE) slouken@1330: slouken@1330: /* extraction of fields from head words */ slouken@1330: #define cinuse(p) ((p)->head & CINUSE_BIT) slouken@1330: #define pinuse(p) ((p)->head & PINUSE_BIT) slouken@1330: #define chunksize(p) ((p)->head & ~(INUSE_BITS)) slouken@1330: slouken@1330: #define clear_pinuse(p) ((p)->head &= ~PINUSE_BIT) slouken@1330: #define clear_cinuse(p) ((p)->head &= ~CINUSE_BIT) slouken@1330: slouken@1330: /* Treat space at ptr +/- offset as a chunk */ slouken@1330: #define chunk_plus_offset(p, s) ((mchunkptr)(((char*)(p)) + (s))) slouken@1330: #define chunk_minus_offset(p, s) ((mchunkptr)(((char*)(p)) - (s))) slouken@1330: slouken@1330: /* Ptr to next or previous physical malloc_chunk. */ slouken@1330: #define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->head & ~INUSE_BITS))) slouken@1330: #define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_foot) )) slouken@1330: slouken@1330: /* extract next chunk's pinuse bit */ slouken@1330: #define next_pinuse(p) ((next_chunk(p)->head) & PINUSE_BIT) slouken@1330: slouken@1330: /* Get/set size at footer */ slouken@1330: #define get_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_foot) slouken@1330: #define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_foot = (s)) slouken@1330: slouken@1330: /* Set size, pinuse bit, and foot */ slouken@1330: #define set_size_and_pinuse_of_free_chunk(p, s)\ slouken@1330: ((p)->head = (s|PINUSE_BIT), set_foot(p, s)) slouken@1330: slouken@1330: /* Set size, pinuse bit, foot, and clear next pinuse */ slouken@1330: #define set_free_with_pinuse(p, s, n)\ slouken@1330: (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s)) slouken@1330: slouken@1330: #define is_mmapped(p)\ slouken@1330: (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_MMAPPED_BIT)) slouken@1330: slouken@1330: /* Get the internal overhead associated with chunk p */ slouken@1330: #define overhead_for(p)\ slouken@1330: (is_mmapped(p)? MMAP_CHUNK_OVERHEAD : CHUNK_OVERHEAD) slouken@1330: slouken@1330: /* Return true if malloced space is not necessarily cleared */ slouken@1330: #if MMAP_CLEARS slouken@1330: #define calloc_must_clear(p) (!is_mmapped(p)) slouken@1330: #else /* MMAP_CLEARS */ slouken@1330: #define calloc_must_clear(p) (1) slouken@1330: #endif /* MMAP_CLEARS */ slouken@1330: slouken@1330: /* ---------------------- Overlaid data structures ----------------------- */ slouken@1330: slouken@1330: /* slouken@1330: When chunks are not in use, they are treated as nodes of either slouken@1330: lists or trees. slouken@1330: slouken@1330: "Small" chunks are stored in circular doubly-linked lists, and look slouken@1330: like this: slouken@1330: slouken@1330: chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Size of previous chunk | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: `head:' | Size of chunk, in bytes |P| slouken@1330: mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Forward pointer to next chunk in list | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Back pointer to previous chunk in list | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Unused space (may be 0 bytes long) . slouken@1330: . . slouken@1330: . | slouken@1330: nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: `foot:' | Size of chunk, in bytes | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: slouken@1330: Larger chunks are kept in a form of bitwise digital trees (aka slouken@1330: tries) keyed on chunksizes. Because malloc_tree_chunks are only for slouken@1330: free chunks greater than 256 bytes, their size doesn't impose any slouken@1330: constraints on user chunk sizes. Each node looks like: slouken@1330: slouken@1330: chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Size of previous chunk | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: `head:' | Size of chunk, in bytes |P| slouken@1330: mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Forward pointer to next chunk of same size | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Back pointer to previous chunk of same size | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Pointer to left child (child[0]) | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Pointer to right child (child[1]) | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Pointer to parent | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | bin index of this chunk | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: | Unused space . slouken@1330: . | slouken@1330: nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: `foot:' | Size of chunk, in bytes | slouken@1330: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ slouken@1330: slouken@1330: Each tree holding treenodes is a tree of unique chunk sizes. Chunks slouken@1330: of the same size are arranged in a circularly-linked list, with only slouken@1330: the oldest chunk (the next to be used, in our FIFO ordering) slouken@1330: actually in the tree. (Tree members are distinguished by a non-null slouken@1330: parent pointer.) If a chunk with the same size an an existing node slouken@1330: is inserted, it is linked off the existing node using pointers that slouken@1330: work in the same way as fd/bk pointers of small chunks. slouken@1330: slouken@1330: Each tree contains a power of 2 sized range of chunk sizes (the slouken@1330: smallest is 0x100 <= x < 0x180), which is is divided in half at each slouken@1330: tree level, with the chunks in the smaller half of the range (0x100 slouken@1330: <= x < 0x140 for the top nose) in the left subtree and the larger slouken@1330: half (0x140 <= x < 0x180) in the right subtree. This is, of course, slouken@1330: done by inspecting individual bits. slouken@1330: slouken@1330: Using these rules, each node's left subtree contains all smaller slouken@1330: sizes than its right subtree. However, the node at the root of each slouken@1330: subtree has no particular ordering relationship to either. (The slouken@1330: dividing line between the subtree sizes is based on trie relation.) slouken@1330: If we remove the last chunk of a given size from the interior of the slouken@1330: tree, we need to replace it with a leaf node. The tree ordering slouken@1330: rules permit a node to be replaced by any leaf below it. slouken@1330: slouken@1330: The smallest chunk in a tree (a common operation in a best-fit slouken@1330: allocator) can be found by walking a path to the leftmost leaf in slouken@1330: the tree. Unlike a usual binary tree, where we follow left child slouken@1330: pointers until we reach a null, here we follow the right child slouken@1330: pointer any time the left one is null, until we reach a leaf with slouken@1330: both child pointers null. The smallest chunk in the tree will be slouken@1330: somewhere along that path. slouken@1330: slouken@1330: The worst case number of steps to add, find, or remove a node is slouken@1330: bounded by the number of bits differentiating chunks within slouken@1330: bins. Under current bin calculations, this ranges from 6 up to 21 slouken@1330: (for 32 bit sizes) or up to 53 (for 64 bit sizes). The typical case slouken@1330: is of course much better. slouken@1330: */ slouken@1330: slouken@1895: struct malloc_tree_chunk slouken@1895: { slouken@1895: /* The first four fields must be compatible with malloc_chunk */ slouken@1895: size_t prev_foot; slouken@1895: size_t head; slouken@1895: struct malloc_tree_chunk *fd; slouken@1895: struct malloc_tree_chunk *bk; slouken@1895: slouken@1895: struct malloc_tree_chunk *child[2]; slouken@1895: struct malloc_tree_chunk *parent; slouken@1895: bindex_t index; slouken@1330: }; slouken@1330: slouken@1895: typedef struct malloc_tree_chunk tchunk; slouken@1895: typedef struct malloc_tree_chunk *tchunkptr; slouken@1895: typedef struct malloc_tree_chunk *tbinptr; /* The type of bins of trees */ slouken@1330: slouken@1330: /* A little helper macro for trees */ slouken@1330: #define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1]) slouken@1330: slouken@1330: /* ----------------------------- Segments -------------------------------- */ slouken@1330: slouken@1330: /* slouken@1330: Each malloc space may include non-contiguous segments, held in a slouken@1330: list headed by an embedded malloc_segment record representing the slouken@1330: top-most space. Segments also include flags holding properties of slouken@1330: the space. Large chunks that are directly allocated by mmap are not slouken@1330: included in this list. They are instead independently created and slouken@1330: destroyed without otherwise keeping track of them. slouken@1330: slouken@1330: Segment management mainly comes into play for spaces allocated by slouken@1330: MMAP. Any call to MMAP might or might not return memory that is slouken@1330: adjacent to an existing segment. MORECORE normally contiguously slouken@1330: extends the current space, so this space is almost always adjacent, slouken@1330: which is simpler and faster to deal with. (This is why MORECORE is slouken@1330: used preferentially to MMAP when both are available -- see slouken@1330: sys_alloc.) When allocating using MMAP, we don't use any of the slouken@1330: hinting mechanisms (inconsistently) supported in various slouken@1330: implementations of unix mmap, or distinguish reserving from slouken@1330: committing memory. Instead, we just ask for space, and exploit slouken@1330: contiguity when we get it. It is probably possible to do slouken@1330: better than this on some systems, but no general scheme seems slouken@1330: to be significantly better. slouken@1330: slouken@1330: Management entails a simpler variant of the consolidation scheme slouken@1330: used for chunks to reduce fragmentation -- new adjacent memory is slouken@1330: normally prepended or appended to an existing segment. However, slouken@1330: there are limitations compared to chunk consolidation that mostly slouken@1330: reflect the fact that segment processing is relatively infrequent slouken@1330: (occurring only when getting memory from system) and that we slouken@1330: don't expect to have huge numbers of segments: slouken@1330: slouken@1330: * Segments are not indexed, so traversal requires linear scans. (It slouken@1330: would be possible to index these, but is not worth the extra slouken@1330: overhead and complexity for most programs on most platforms.) slouken@1330: * New segments are only appended to old ones when holding top-most slouken@1330: memory; if they cannot be prepended to others, they are held in slouken@1330: different segments. slouken@1330: slouken@1330: Except for the top-most segment of an mstate, each segment record slouken@1330: is kept at the tail of its segment. Segments are added by pushing slouken@1330: segment records onto the list headed by &mstate.seg for the slouken@1330: containing mstate. slouken@1330: slouken@1330: Segment flags control allocation/merge/deallocation policies: slouken@1330: * If EXTERN_BIT set, then we did not allocate this segment, slouken@1330: and so should not try to deallocate or merge with others. slouken@1330: (This currently holds only for the initial segment passed slouken@1330: into create_mspace_with_base.) slouken@1330: * If IS_MMAPPED_BIT set, the segment may be merged with slouken@1330: other surrounding mmapped segments and trimmed/de-allocated slouken@1330: using munmap. slouken@1330: * If neither bit is set, then the segment was obtained using slouken@1330: MORECORE so can be merged with surrounding MORECORE'd segments slouken@1330: and deallocated/trimmed using MORECORE with negative arguments. slouken@1330: */ slouken@1330: slouken@1895: struct malloc_segment slouken@1895: { slouken@1895: char *base; /* base address */ slouken@1895: size_t size; /* allocated size */ slouken@1895: struct malloc_segment *next; /* ptr to next segment */ slouken@1895: flag_t sflags; /* mmap and extern flag */ slouken@1330: }; slouken@1330: slouken@1330: #define is_mmapped_segment(S) ((S)->sflags & IS_MMAPPED_BIT) slouken@1330: #define is_extern_segment(S) ((S)->sflags & EXTERN_BIT) slouken@1330: slouken@1895: typedef struct malloc_segment msegment; slouken@1895: typedef struct malloc_segment *msegmentptr; slouken@1330: slouken@1330: /* ---------------------------- malloc_state ----------------------------- */ slouken@1330: slouken@1330: /* slouken@1330: A malloc_state holds all of the bookkeeping for a space. slouken@1330: The main fields are: slouken@1330: slouken@1330: Top slouken@1330: The topmost chunk of the currently active segment. Its size is slouken@1330: cached in topsize. The actual size of topmost space is slouken@1330: topsize+TOP_FOOT_SIZE, which includes space reserved for adding slouken@1330: fenceposts and segment records if necessary when getting more slouken@1330: space from the system. The size at which to autotrim top is slouken@1330: cached from mparams in trim_check, except that it is disabled if slouken@1330: an autotrim fails. slouken@1330: slouken@1330: Designated victim (dv) slouken@1330: This is the preferred chunk for servicing small requests that slouken@1330: don't have exact fits. It is normally the chunk split off most slouken@1330: recently to service another small request. Its size is cached in slouken@1330: dvsize. The link fields of this chunk are not maintained since it slouken@1330: is not kept in a bin. slouken@1330: slouken@1330: SmallBins slouken@1330: An array of bin headers for free chunks. These bins hold chunks slouken@1330: with sizes less than MIN_LARGE_SIZE bytes. Each bin contains slouken@1330: chunks of all the same size, spaced 8 bytes apart. To simplify slouken@1330: use in double-linked lists, each bin header acts as a malloc_chunk slouken@1330: pointing to the real first node, if it exists (else pointing to slouken@1330: itself). This avoids special-casing for headers. But to avoid slouken@1330: waste, we allocate only the fd/bk pointers of bins, and then use slouken@1330: repositioning tricks to treat these as the fields of a chunk. slouken@1330: slouken@1330: TreeBins slouken@1330: Treebins are pointers to the roots of trees holding a range of slouken@1330: sizes. There are 2 equally spaced treebins for each power of two slouken@1330: from TREE_SHIFT to TREE_SHIFT+16. The last bin holds anything slouken@1330: larger. slouken@1330: slouken@1330: Bin maps slouken@1330: There is one bit map for small bins ("smallmap") and one for slouken@1330: treebins ("treemap). Each bin sets its bit when non-empty, and slouken@1330: clears the bit when empty. Bit operations are then used to avoid slouken@1330: bin-by-bin searching -- nearly all "search" is done without ever slouken@1330: looking at bins that won't be selected. The bit maps slouken@1330: conservatively use 32 bits per map word, even if on 64bit system. slouken@1330: For a good description of some of the bit-based techniques used slouken@1330: here, see Henry S. Warren Jr's book "Hacker's Delight" (and slouken@1330: supplement at http://hackersdelight.org/). Many of these are slouken@1330: intended to reduce the branchiness of paths through malloc etc, as slouken@1330: well as to reduce the number of memory locations read or written. slouken@1330: slouken@1330: Segments slouken@1330: A list of segments headed by an embedded malloc_segment record slouken@1330: representing the initial space. slouken@1330: slouken@1330: Address check support slouken@1330: The least_addr field is the least address ever obtained from slouken@1330: MORECORE or MMAP. Attempted frees and reallocs of any address less slouken@1330: than this are trapped (unless INSECURE is defined). slouken@1330: slouken@1330: Magic tag slouken@1330: A cross-check field that should always hold same value as mparams.magic. slouken@1330: slouken@1330: Flags slouken@1330: Bits recording whether to use MMAP, locks, or contiguous MORECORE slouken@1330: slouken@1330: Statistics slouken@1330: Each space keeps track of current and maximum system memory slouken@1330: obtained via MORECORE or MMAP. slouken@1330: slouken@1330: Locking slouken@1330: If USE_LOCKS is defined, the "mutex" lock is acquired and released slouken@1330: around every public call using this mspace. slouken@1330: */ slouken@1330: slouken@1330: /* Bin types, widths and sizes */ slouken@1330: #define NSMALLBINS (32U) slouken@1330: #define NTREEBINS (32U) slouken@1330: #define SMALLBIN_SHIFT (3U) slouken@1330: #define SMALLBIN_WIDTH (SIZE_T_ONE << SMALLBIN_SHIFT) slouken@1330: #define TREEBIN_SHIFT (8U) slouken@1330: #define MIN_LARGE_SIZE (SIZE_T_ONE << TREEBIN_SHIFT) slouken@1330: #define MAX_SMALL_SIZE (MIN_LARGE_SIZE - SIZE_T_ONE) slouken@1330: #define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD) slouken@1330: slouken@1895: struct malloc_state slouken@1895: { slouken@1895: binmap_t smallmap; slouken@1895: binmap_t treemap; slouken@1895: size_t dvsize; slouken@1895: size_t topsize; slouken@1895: char *least_addr; slouken@1895: mchunkptr dv; slouken@1895: mchunkptr top; slouken@1895: size_t trim_check; slouken@1895: size_t magic; slouken@1895: mchunkptr smallbins[(NSMALLBINS + 1) * 2]; slouken@1895: tbinptr treebins[NTREEBINS]; slouken@1895: size_t footprint; slouken@1895: size_t max_footprint; slouken@1895: flag_t mflags; slouken@1330: #if USE_LOCKS slouken@1895: MLOCK_T mutex; /* locate lock among fields that rarely change */ slouken@1895: #endif /* USE_LOCKS */ slouken@1895: msegment seg; slouken@1330: }; slouken@1330: slouken@1895: typedef struct malloc_state *mstate; slouken@1330: slouken@1330: /* ------------- Global malloc_state and malloc_params ------------------- */ slouken@1330: slouken@1330: /* slouken@1330: malloc_params holds global properties, including those that can be slouken@1330: dynamically set using mallopt. There is a single instance, mparams, slouken@1330: initialized in init_mparams. slouken@1330: */ slouken@1330: slouken@1895: struct malloc_params slouken@1895: { slouken@1895: size_t magic; slouken@1895: size_t page_size; slouken@1895: size_t granularity; slouken@1895: size_t mmap_threshold; slouken@1895: size_t trim_threshold; slouken@1895: flag_t default_mflags; slouken@1330: }; slouken@1330: slouken@1330: static struct malloc_params mparams; slouken@1330: slouken@1330: /* The global malloc_state used for all non-"mspace" calls */ slouken@1330: static struct malloc_state _gm_; slouken@1330: #define gm (&_gm_) slouken@1330: #define is_global(M) ((M) == &_gm_) slouken@1330: #define is_initialized(M) ((M)->top != 0) slouken@1330: slouken@1330: /* -------------------------- system alloc setup ------------------------- */ slouken@1330: slouken@1330: /* Operations on mflags */ slouken@1330: slouken@1330: #define use_lock(M) ((M)->mflags & USE_LOCK_BIT) slouken@1330: #define enable_lock(M) ((M)->mflags |= USE_LOCK_BIT) slouken@1330: #define disable_lock(M) ((M)->mflags &= ~USE_LOCK_BIT) slouken@1330: slouken@1330: #define use_mmap(M) ((M)->mflags & USE_MMAP_BIT) slouken@1330: #define enable_mmap(M) ((M)->mflags |= USE_MMAP_BIT) slouken@1330: #define disable_mmap(M) ((M)->mflags &= ~USE_MMAP_BIT) slouken@1330: slouken@1330: #define use_noncontiguous(M) ((M)->mflags & USE_NONCONTIGUOUS_BIT) slouken@1330: #define disable_contiguous(M) ((M)->mflags |= USE_NONCONTIGUOUS_BIT) slouken@1330: slouken@1330: #define set_lock(M,L)\ slouken@1330: ((M)->mflags = (L)?\ slouken@1330: ((M)->mflags | USE_LOCK_BIT) :\ slouken@1330: ((M)->mflags & ~USE_LOCK_BIT)) slouken@1330: slouken@1330: /* page-align a size */ slouken@1330: #define page_align(S)\ slouken@1330: (((S) + (mparams.page_size)) & ~(mparams.page_size - SIZE_T_ONE)) slouken@1330: slouken@1330: /* granularity-align a size */ slouken@1330: #define granularity_align(S)\ slouken@1330: (((S) + (mparams.granularity)) & ~(mparams.granularity - SIZE_T_ONE)) slouken@1330: slouken@1330: #define is_page_aligned(S)\ slouken@1330: (((size_t)(S) & (mparams.page_size - SIZE_T_ONE)) == 0) slouken@1330: #define is_granularity_aligned(S)\ slouken@1330: (((size_t)(S) & (mparams.granularity - SIZE_T_ONE)) == 0) slouken@1330: slouken@1330: /* True if segment S holds address A */ slouken@1330: #define segment_holds(S, A)\ slouken@1330: ((char*)(A) >= S->base && (char*)(A) < S->base + S->size) slouken@1330: slouken@1330: /* Return segment holding given address */ slouken@1895: static msegmentptr slouken@1895: segment_holding(mstate m, char *addr) slouken@1895: { slouken@1895: msegmentptr sp = &m->seg; slouken@1895: for (;;) { slouken@1895: if (addr >= sp->base && addr < sp->base + sp->size) slouken@1895: return sp; slouken@1895: if ((sp = sp->next) == 0) slouken@1895: return 0; slouken@1895: } slouken@1330: } slouken@1330: slouken@1330: /* Return true if segment contains a segment link */ slouken@1895: static int slouken@1895: has_segment_link(mstate m, msegmentptr ss) slouken@1895: { slouken@1895: msegmentptr sp = &m->seg; slouken@1895: for (;;) { slouken@1895: if ((char *) sp >= ss->base && (char *) sp < ss->base + ss->size) slouken@1895: return 1; slouken@1895: if ((sp = sp->next) == 0) slouken@1895: return 0; slouken@1895: } slouken@1330: } slouken@1330: slouken@1330: #ifndef MORECORE_CANNOT_TRIM slouken@1330: #define should_trim(M,s) ((s) > (M)->trim_check) slouken@1895: #else /* MORECORE_CANNOT_TRIM */ slouken@1330: #define should_trim(M,s) (0) slouken@1330: #endif /* MORECORE_CANNOT_TRIM */ slouken@1330: slouken@1330: /* slouken@1330: TOP_FOOT_SIZE is padding at the end of a segment, including space slouken@1330: that may be needed to place segment records and fenceposts when new slouken@1330: noncontiguous segments are added. slouken@1330: */ slouken@1330: #define TOP_FOOT_SIZE\ slouken@1330: (align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE) slouken@1330: slouken@1330: slouken@1330: /* ------------------------------- Hooks -------------------------------- */ slouken@1330: slouken@1330: /* slouken@1330: PREACTION should be defined to return 0 on success, and nonzero on slouken@1330: failure. If you are not using locking, you can redefine these to do slouken@1330: anything you like. slouken@1330: */ slouken@1330: slouken@1330: #if USE_LOCKS slouken@1330: slouken@1330: /* Ensure locks are initialized */ slouken@1330: #define GLOBALLY_INITIALIZE() (mparams.page_size == 0 && init_mparams()) slouken@1330: slouken@1330: #define PREACTION(M) ((GLOBALLY_INITIALIZE() || use_lock(M))? ACQUIRE_LOCK(&(M)->mutex) : 0) slouken@1330: #define POSTACTION(M) { if (use_lock(M)) RELEASE_LOCK(&(M)->mutex); } slouken@1330: #else /* USE_LOCKS */ slouken@1330: slouken@1330: #ifndef PREACTION slouken@1330: #define PREACTION(M) (0) slouken@1895: #endif /* PREACTION */ slouken@1330: slouken@1330: #ifndef POSTACTION slouken@1330: #define POSTACTION(M) slouken@1895: #endif /* POSTACTION */ slouken@1330: slouken@1330: #endif /* USE_LOCKS */ slouken@1330: slouken@1330: /* slouken@1330: CORRUPTION_ERROR_ACTION is triggered upon detected bad addresses. slouken@1330: USAGE_ERROR_ACTION is triggered on detected bad frees and slouken@1330: reallocs. The argument p is an address that might have triggered the slouken@1330: fault. It is ignored by the two predefined actions, but might be slouken@1330: useful in custom actions that try to help diagnose errors. slouken@1330: */ slouken@1330: slouken@1330: #if PROCEED_ON_ERROR slouken@1330: slouken@1330: /* A count of the number of corruption errors causing resets */ slouken@1330: int malloc_corruption_error_count; slouken@1330: slouken@1330: /* default corruption action */ slouken@1330: static void reset_on_error(mstate m); slouken@1330: slouken@1330: #define CORRUPTION_ERROR_ACTION(m) reset_on_error(m) slouken@1330: #define USAGE_ERROR_ACTION(m, p) slouken@1330: slouken@1330: #else /* PROCEED_ON_ERROR */ slouken@1330: slouken@1330: #ifndef CORRUPTION_ERROR_ACTION slouken@1330: #define CORRUPTION_ERROR_ACTION(m) ABORT slouken@1330: #endif /* CORRUPTION_ERROR_ACTION */ slouken@1330: slouken@1330: #ifndef USAGE_ERROR_ACTION slouken@1330: #define USAGE_ERROR_ACTION(m,p) ABORT slouken@1330: #endif /* USAGE_ERROR_ACTION */ slouken@1330: slouken@1330: #endif /* PROCEED_ON_ERROR */ slouken@1330: slouken@1330: /* -------------------------- Debugging setup ---------------------------- */ slouken@1330: slouken@1330: #if ! DEBUG slouken@1330: slouken@1330: #define check_free_chunk(M,P) slouken@1330: #define check_inuse_chunk(M,P) slouken@1330: #define check_malloced_chunk(M,P,N) slouken@1330: #define check_mmapped_chunk(M,P) slouken@1330: #define check_malloc_state(M) slouken@1330: #define check_top_chunk(M,P) slouken@1330: slouken@1330: #else /* DEBUG */ slouken@1330: #define check_free_chunk(M,P) do_check_free_chunk(M,P) slouken@1330: #define check_inuse_chunk(M,P) do_check_inuse_chunk(M,P) slouken@1330: #define check_top_chunk(M,P) do_check_top_chunk(M,P) slouken@1330: #define check_malloced_chunk(M,P,N) do_check_malloced_chunk(M,P,N) slouken@1330: #define check_mmapped_chunk(M,P) do_check_mmapped_chunk(M,P) slouken@1330: #define check_malloc_state(M) do_check_malloc_state(M) slouken@1330: slouken@1895: static void do_check_any_chunk(mstate m, mchunkptr p); slouken@1895: static void do_check_top_chunk(mstate m, mchunkptr p); slouken@1895: static void do_check_mmapped_chunk(mstate m, mchunkptr p); slouken@1895: static void do_check_inuse_chunk(mstate m, mchunkptr p); slouken@1895: static void do_check_free_chunk(mstate m, mchunkptr p); slouken@1895: static void do_check_malloced_chunk(mstate m, void *mem, size_t s); slouken@1895: static void do_check_tree(mstate m, tchunkptr t); slouken@1895: static void do_check_treebin(mstate m, bindex_t i); slouken@1895: static void do_check_smallbin(mstate m, bindex_t i); slouken@1895: static void do_check_malloc_state(mstate m); slouken@1895: static int bin_find(mstate m, mchunkptr x); slouken@1330: static size_t traverse_and_check(mstate m); slouken@1330: #endif /* DEBUG */ slouken@1330: slouken@1330: /* ---------------------------- Indexing Bins ---------------------------- */ slouken@1330: slouken@1330: #define is_small(s) (((s) >> SMALLBIN_SHIFT) < NSMALLBINS) slouken@1330: #define small_index(s) ((s) >> SMALLBIN_SHIFT) slouken@1330: #define small_index2size(i) ((i) << SMALLBIN_SHIFT) slouken@1330: #define MIN_SMALL_INDEX (small_index(MIN_CHUNK_SIZE)) slouken@1330: slouken@1330: /* addressing by index. See above about smallbin repositioning */ slouken@1330: #define smallbin_at(M, i) ((sbinptr)((char*)&((M)->smallbins[(i)<<1]))) slouken@1330: #define treebin_at(M,i) (&((M)->treebins[i])) slouken@1330: slouken@1330: /* assign tree index for size S to variable I */ slouken@1330: #if defined(__GNUC__) && defined(i386) slouken@1330: #define compute_tree_index(S, I)\ slouken@1330: {\ slouken@1330: size_t X = S >> TREEBIN_SHIFT;\ slouken@1330: if (X == 0)\ slouken@1330: I = 0;\ slouken@1330: else if (X > 0xFFFF)\ slouken@1330: I = NTREEBINS-1;\ slouken@1330: else {\ slouken@1330: unsigned int K;\ slouken@1330: __asm__("bsrl %1,%0\n\t" : "=r" (K) : "rm" (X));\ slouken@1330: I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ slouken@1330: }\ slouken@1330: } slouken@1330: #else /* GNUC */ slouken@1330: #define compute_tree_index(S, I)\ slouken@1330: {\ slouken@1330: size_t X = S >> TREEBIN_SHIFT;\ slouken@1330: if (X == 0)\ slouken@1330: I = 0;\ slouken@1330: else if (X > 0xFFFF)\ slouken@1330: I = NTREEBINS-1;\ slouken@1330: else {\ slouken@1330: unsigned int Y = (unsigned int)X;\ slouken@1330: unsigned int N = ((Y - 0x100) >> 16) & 8;\ slouken@1330: unsigned int K = (((Y <<= N) - 0x1000) >> 16) & 4;\ slouken@1330: N += K;\ slouken@1330: N += K = (((Y <<= K) - 0x4000) >> 16) & 2;\ slouken@1330: K = 14 - N + ((Y <<= K) >> 15);\ slouken@1330: I = (K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1));\ slouken@1330: }\ slouken@1330: } slouken@1330: #endif /* GNUC */ slouken@1330: slouken@1330: /* Bit representing maximum resolved size in a treebin at i */ slouken@1330: #define bit_for_tree_index(i) \ slouken@1330: (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2) slouken@1330: slouken@1330: /* Shift placing maximum resolved bit in a treebin at i as sign bit */ slouken@1330: #define leftshift_for_tree_index(i) \ slouken@1330: ((i == NTREEBINS-1)? 0 : \ slouken@1330: ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2))) slouken@1330: slouken@1330: /* The size of the smallest chunk held in bin with index i */ slouken@1330: #define minsize_for_tree_index(i) \ slouken@1330: ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) | \ slouken@1330: (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1))) slouken@1330: slouken@1330: slouken@1330: /* ------------------------ Operations on bin maps ----------------------- */ slouken@1330: slouken@1330: /* bit corresponding to given index */ slouken@1330: #define idx2bit(i) ((binmap_t)(1) << (i)) slouken@1330: slouken@1330: /* Mark/Clear bits with given index */ slouken@1330: #define mark_smallmap(M,i) ((M)->smallmap |= idx2bit(i)) slouken@1330: #define clear_smallmap(M,i) ((M)->smallmap &= ~idx2bit(i)) slouken@1330: #define smallmap_is_marked(M,i) ((M)->smallmap & idx2bit(i)) slouken@1330: slouken@1330: #define mark_treemap(M,i) ((M)->treemap |= idx2bit(i)) slouken@1330: #define clear_treemap(M,i) ((M)->treemap &= ~idx2bit(i)) slouken@1330: #define treemap_is_marked(M,i) ((M)->treemap & idx2bit(i)) slouken@1330: slouken@1330: /* index corresponding to given bit */ slouken@1330: slouken@1330: #if defined(__GNUC__) && defined(i386) slouken@1330: #define compute_bit2idx(X, I)\ slouken@1330: {\ slouken@1330: unsigned int J;\ slouken@1330: __asm__("bsfl %1,%0\n\t" : "=r" (J) : "rm" (X));\ slouken@1330: I = (bindex_t)J;\ slouken@1330: } slouken@1330: slouken@1330: #else /* GNUC */ slouken@1330: #if USE_BUILTIN_FFS slouken@1330: #define compute_bit2idx(X, I) I = ffs(X)-1 slouken@1330: slouken@1330: #else /* USE_BUILTIN_FFS */ slouken@1330: #define compute_bit2idx(X, I)\ slouken@1330: {\ slouken@1330: unsigned int Y = X - 1;\ slouken@1330: unsigned int K = Y >> (16-4) & 16;\ slouken@1330: unsigned int N = K; Y >>= K;\ slouken@1330: N += K = Y >> (8-3) & 8; Y >>= K;\ slouken@1330: N += K = Y >> (4-2) & 4; Y >>= K;\ slouken@1330: N += K = Y >> (2-1) & 2; Y >>= K;\ slouken@1330: N += K = Y >> (1-0) & 1; Y >>= K;\ slouken@1330: I = (bindex_t)(N + Y);\ slouken@1330: } slouken@1330: #endif /* USE_BUILTIN_FFS */ slouken@1330: #endif /* GNUC */ slouken@1330: slouken@1330: /* isolate the least set bit of a bitmap */ slouken@1330: #define least_bit(x) ((x) & -(x)) slouken@1330: slouken@1330: /* mask with all bits to left of least bit of x on */ slouken@1330: #define left_bits(x) ((x<<1) | -(x<<1)) slouken@1330: slouken@1330: /* mask with all bits to left of or equal to least bit of x on */ slouken@1330: #define same_or_left_bits(x) ((x) | -(x)) slouken@1330: slouken@1330: slouken@1330: /* ----------------------- Runtime Check Support ------------------------- */ slouken@1330: slouken@1330: /* slouken@1330: For security, the main invariant is that malloc/free/etc never slouken@1330: writes to a static address other than malloc_state, unless static slouken@1330: malloc_state itself has been corrupted, which cannot occur via slouken@1330: malloc (because of these checks). In essence this means that we slouken@1330: believe all pointers, sizes, maps etc held in malloc_state, but slouken@1330: check all of those linked or offsetted from other embedded data slouken@1330: structures. These checks are interspersed with main code in a way slouken@1330: that tends to minimize their run-time cost. slouken@1330: slouken@1330: When FOOTERS is defined, in addition to range checking, we also slouken@1330: verify footer fields of inuse chunks, which can be used guarantee slouken@1330: that the mstate controlling malloc/free is intact. This is a slouken@1330: streamlined version of the approach described by William Robertson slouken@1330: et al in "Run-time Detection of Heap-based Overflows" LISA'03 slouken@1330: http://www.usenix.org/events/lisa03/tech/robertson.html The footer slouken@1330: of an inuse chunk holds the xor of its mstate and a random seed, slouken@1330: that is checked upon calls to free() and realloc(). This is slouken@1330: (probablistically) unguessable from outside the program, but can be slouken@1330: computed by any code successfully malloc'ing any chunk, so does not slouken@1330: itself provide protection against code that has already broken slouken@1330: security through some other means. Unlike Robertson et al, we slouken@1330: always dynamically check addresses of all offset chunks (previous, slouken@1330: next, etc). This turns out to be cheaper than relying on hashes. slouken@1330: */ slouken@1330: slouken@1330: #if !INSECURE slouken@1330: /* Check if address a is at least as high as any from MORECORE or MMAP */ slouken@1330: #define ok_address(M, a) ((char*)(a) >= (M)->least_addr) slouken@1330: /* Check if address of next chunk n is higher than base chunk p */ slouken@1330: #define ok_next(p, n) ((char*)(p) < (char*)(n)) slouken@1330: /* Check if p has its cinuse bit on */ slouken@1330: #define ok_cinuse(p) cinuse(p) slouken@1330: /* Check if p has its pinuse bit on */ slouken@1330: #define ok_pinuse(p) pinuse(p) slouken@1330: slouken@1330: #else /* !INSECURE */ slouken@1330: #define ok_address(M, a) (1) slouken@1330: #define ok_next(b, n) (1) slouken@1330: #define ok_cinuse(p) (1) slouken@1330: #define ok_pinuse(p) (1) slouken@1330: #endif /* !INSECURE */ slouken@1330: slouken@1330: #if (FOOTERS && !INSECURE) slouken@1330: /* Check if (alleged) mstate m has expected magic field */ slouken@1330: #define ok_magic(M) ((M)->magic == mparams.magic) slouken@1895: #else /* (FOOTERS && !INSECURE) */ slouken@1330: #define ok_magic(M) (1) slouken@1330: #endif /* (FOOTERS && !INSECURE) */ slouken@1330: slouken@1330: slouken@1330: /* In gcc, use __builtin_expect to minimize impact of checks */ slouken@1330: #if !INSECURE slouken@1330: #if defined(__GNUC__) && __GNUC__ >= 3 slouken@1330: #define RTCHECK(e) __builtin_expect(e, 1) slouken@1330: #else /* GNUC */ slouken@1330: #define RTCHECK(e) (e) slouken@1330: #endif /* GNUC */ slouken@1330: #else /* !INSECURE */ slouken@1330: #define RTCHECK(e) (1) slouken@1330: #endif /* !INSECURE */ slouken@1330: slouken@1330: /* macros to set up inuse chunks with or without footers */ slouken@1330: slouken@1330: #if !FOOTERS slouken@1330: slouken@1330: #define mark_inuse_foot(M,p,s) slouken@1330: slouken@1330: /* Set cinuse bit and pinuse bit of next chunk */ slouken@1330: #define set_inuse(M,p,s)\ slouken@1330: ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ slouken@1330: ((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT) slouken@1330: slouken@1330: /* Set cinuse and pinuse of this chunk and pinuse of next chunk */ slouken@1330: #define set_inuse_and_pinuse(M,p,s)\ slouken@1330: ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ slouken@1330: ((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT) slouken@1330: slouken@1330: /* Set size, cinuse and pinuse bit of this chunk */ slouken@1330: #define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ slouken@1330: ((p)->head = (s|PINUSE_BIT|CINUSE_BIT)) slouken@1330: slouken@1330: #else /* FOOTERS */ slouken@1330: slouken@1330: /* Set foot of inuse chunk to be xor of mstate and seed */ slouken@1330: #define mark_inuse_foot(M,p,s)\ slouken@1330: (((mchunkptr)((char*)(p) + (s)))->prev_foot = ((size_t)(M) ^ mparams.magic)) slouken@1330: slouken@1330: #define get_mstate_for(p)\ slouken@1330: ((mstate)(((mchunkptr)((char*)(p) +\ slouken@1330: (chunksize(p))))->prev_foot ^ mparams.magic)) slouken@1330: slouken@1330: #define set_inuse(M,p,s)\ slouken@1330: ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ slouken@1330: (((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT), \ slouken@1330: mark_inuse_foot(M,p,s)) slouken@1330: slouken@1330: #define set_inuse_and_pinuse(M,p,s)\ slouken@1330: ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ slouken@1330: (((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT),\ slouken@1330: mark_inuse_foot(M,p,s)) slouken@1330: slouken@1330: #define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ slouken@1330: ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ slouken@1330: mark_inuse_foot(M, p, s)) slouken@1330: slouken@1330: #endif /* !FOOTERS */ slouken@1330: slouken@1330: /* ---------------------------- setting mparams -------------------------- */ slouken@1330: slouken@1330: /* Initialize mparams */ slouken@1895: static int slouken@1895: init_mparams(void) slouken@1895: { slouken@1895: if (mparams.page_size == 0) { slouken@1895: size_t s; slouken@1895: slouken@1895: mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD; slouken@1895: mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD; slouken@1330: #if MORECORE_CONTIGUOUS slouken@1895: mparams.default_mflags = USE_LOCK_BIT | USE_MMAP_BIT; slouken@1895: #else /* MORECORE_CONTIGUOUS */ slouken@1895: mparams.default_mflags = slouken@1895: USE_LOCK_BIT | USE_MMAP_BIT | USE_NONCONTIGUOUS_BIT; slouken@1330: #endif /* MORECORE_CONTIGUOUS */ slouken@1330: slouken@1330: #if (FOOTERS && !INSECURE) slouken@1895: { slouken@1330: #if USE_DEV_RANDOM slouken@1895: int fd; slouken@1895: unsigned char buf[sizeof(size_t)]; slouken@1895: /* Try to use /dev/urandom, else fall back on using time */ slouken@1895: if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 && slouken@1895: read(fd, buf, sizeof(buf)) == sizeof(buf)) { slouken@1895: s = *((size_t *) buf); slouken@1895: close(fd); slouken@1895: } else slouken@1330: #endif /* USE_DEV_RANDOM */ slouken@1895: s = (size_t) (time(0) ^ (size_t) 0x55555555U); slouken@1895: slouken@1895: s |= (size_t) 8U; /* ensure nonzero */ slouken@1895: s &= ~(size_t) 7U; /* improve chances of fault for bad values */ slouken@1895: slouken@1895: } slouken@1895: #else /* (FOOTERS && !INSECURE) */ slouken@1895: s = (size_t) 0x58585858U; slouken@1895: #endif /* (FOOTERS && !INSECURE) */ slouken@1895: ACQUIRE_MAGIC_INIT_LOCK(); slouken@1895: if (mparams.magic == 0) { slouken@1895: mparams.magic = s; slouken@1895: /* Set up lock for main malloc area */ slouken@1895: INITIAL_LOCK(&gm->mutex); slouken@1895: gm->mflags = mparams.default_mflags; slouken@1895: } slouken@1895: RELEASE_MAGIC_INIT_LOCK(); slouken@1895: slouken@1895: #ifndef WIN32 slouken@1895: mparams.page_size = malloc_getpagesize; slouken@1895: mparams.granularity = ((DEFAULT_GRANULARITY != 0) ? slouken@1895: DEFAULT_GRANULARITY : mparams.page_size); slouken@1895: #else /* WIN32 */ slouken@1895: { slouken@1895: SYSTEM_INFO system_info; slouken@1895: GetSystemInfo(&system_info); slouken@1895: mparams.page_size = system_info.dwPageSize; slouken@1895: mparams.granularity = system_info.dwAllocationGranularity; slouken@1895: } slouken@1895: #endif /* WIN32 */ slouken@1895: slouken@1895: /* Sanity-check configuration: slouken@1895: size_t must be unsigned and as wide as pointer type. slouken@1895: ints must be at least 4 bytes. slouken@1895: alignment must be at least 8. slouken@1895: Alignment, min chunk size, and page size must all be powers of 2. slouken@1895: */ slouken@1895: if ((sizeof(size_t) != sizeof(char *)) || slouken@1895: (MAX_SIZE_T < MIN_CHUNK_SIZE) || slouken@1895: (sizeof(int) < 4) || slouken@1895: (MALLOC_ALIGNMENT < (size_t) 8U) || slouken@1895: ((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT - SIZE_T_ONE)) != 0) || slouken@1895: ((MCHUNK_SIZE & (MCHUNK_SIZE - SIZE_T_ONE)) != 0) || slouken@1895: ((mparams.granularity & (mparams.granularity - SIZE_T_ONE)) != 0) slouken@1895: || ((mparams.page_size & (mparams.page_size - SIZE_T_ONE)) != 0)) slouken@1895: ABORT; slouken@1330: } slouken@1895: return 0; slouken@1895: } slouken@1895: slouken@1895: /* support for mallopt */ slouken@1895: static int slouken@1895: change_mparam(int param_number, int value) slouken@1895: { slouken@1895: size_t val = (size_t) value; slouken@1895: init_mparams(); slouken@1895: switch (param_number) { slouken@1895: case M_TRIM_THRESHOLD: slouken@1895: mparams.trim_threshold = val; slouken@1895: return 1; slouken@1895: case M_GRANULARITY: slouken@1895: if (val >= mparams.page_size && ((val & (val - 1)) == 0)) { slouken@1895: mparams.granularity = val; slouken@1895: return 1; slouken@1895: } else slouken@1895: return 0; slouken@1895: case M_MMAP_THRESHOLD: slouken@1895: mparams.mmap_threshold = val; slouken@1895: return 1; slouken@1895: default: slouken@1895: return 0; slouken@1330: } slouken@1330: } slouken@1330: slouken@1330: #if DEBUG slouken@1330: /* ------------------------- Debugging Support --------------------------- */ slouken@1330: slouken@1330: /* Check properties of any chunk, whether free, inuse, mmapped etc */ slouken@1895: static void slouken@1895: do_check_any_chunk(mstate m, mchunkptr p) slouken@1895: { slouken@1895: assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); slouken@1895: assert(ok_address(m, p)); slouken@1330: } slouken@1330: slouken@1330: /* Check properties of top chunk */ slouken@1895: static void slouken@1895: do_check_top_chunk(mstate m, mchunkptr p) slouken@1895: { slouken@1895: msegmentptr sp = segment_holding(m, (char *) p); slouken@1895: size_t sz = chunksize(p); slouken@1895: assert(sp != 0); slouken@1895: assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); slouken@1895: assert(ok_address(m, p)); slouken@1895: assert(sz == m->topsize); slouken@1895: assert(sz > 0); slouken@1895: assert(sz == ((sp->base + sp->size) - (char *) p) - TOP_FOOT_SIZE); slouken@1895: assert(pinuse(p)); slouken@1895: assert(!next_pinuse(p)); slouken@1330: } slouken@1330: slouken@1330: /* Check properties of (inuse) mmapped chunks */ slouken@1895: static void slouken@1895: do_check_mmapped_chunk(mstate m, mchunkptr p) slouken@1895: { slouken@1895: size_t sz = chunksize(p); slouken@1895: size_t len = (sz + (p->prev_foot & ~IS_MMAPPED_BIT) + MMAP_FOOT_PAD); slouken@1895: assert(is_mmapped(p)); slouken@1895: assert(use_mmap(m)); slouken@1895: assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); slouken@1895: assert(ok_address(m, p)); slouken@1895: assert(!is_small(sz)); slouken@1895: assert((len & (mparams.page_size - SIZE_T_ONE)) == 0); slouken@1895: assert(chunk_plus_offset(p, sz)->head == FENCEPOST_HEAD); slouken@1895: assert(chunk_plus_offset(p, sz + SIZE_T_SIZE)->head == 0); slouken@1330: } slouken@1330: slouken@1330: /* Check properties of inuse chunks */ slouken@1895: static void slouken@1895: do_check_inuse_chunk(mstate m, mchunkptr p) slouken@1895: { slouken@1895: do_check_any_chunk(m, p); slouken@1895: assert(cinuse(p)); slouken@1895: assert(next_pinuse(p)); slouken@1895: /* If not pinuse and not mmapped, previous chunk has OK offset */ slouken@1895: assert(is_mmapped(p) || pinuse(p) || next_chunk(prev_chunk(p)) == p); slouken@1895: if (is_mmapped(p)) slouken@1895: do_check_mmapped_chunk(m, p); slouken@1330: } slouken@1330: slouken@1330: /* Check properties of free chunks */ slouken@1895: static void slouken@1895: do_check_free_chunk(mstate m, mchunkptr p) slouken@1895: { slouken@1895: size_t sz = p->head & ~(PINUSE_BIT | CINUSE_BIT); slouken@1895: mchunkptr next = chunk_plus_offset(p, sz); slouken@1895: do_check_any_chunk(m, p); slouken@1895: assert(!cinuse(p)); slouken@1895: assert(!next_pinuse(p)); slouken@1895: assert(!is_mmapped(p)); slouken@1895: if (p != m->dv && p != m->top) { slouken@1895: if (sz >= MIN_CHUNK_SIZE) { slouken@1895: assert((sz & CHUNK_ALIGN_MASK) == 0); slouken@1895: assert(is_aligned(chunk2mem(p))); slouken@1895: assert(next->prev_foot == sz); slouken@1895: assert(pinuse(p)); slouken@1895: assert(next == m->top || cinuse(next)); slouken@1895: assert(p->fd->bk == p); slouken@1895: assert(p->bk->fd == p); slouken@1895: } else /* markers are always of size SIZE_T_SIZE */ slouken@1895: assert(sz == SIZE_T_SIZE); slouken@1330: } slouken@1330: } slouken@1330: slouken@1330: /* Check properties of malloced chunks at the point they are malloced */ slouken@1895: static void slouken@1895: do_check_malloced_chunk(mstate m, void *mem, size_t s) slouken@1895: { slouken@1895: if (mem != 0) { slouken@1895: mchunkptr p = mem2chunk(mem); slouken@1895: size_t sz = p->head & ~(PINUSE_BIT | CINUSE_BIT); slouken@1895: do_check_inuse_chunk(m, p); slouken@1895: assert((sz & CHUNK_ALIGN_MASK) == 0); slouken@1895: assert(sz >= MIN_CHUNK_SIZE); slouken@1895: assert(sz >= s); slouken@1895: /* unless mmapped, size is less than MIN_CHUNK_SIZE more than request */ slouken@1895: assert(is_mmapped(p) || sz < (s + MIN_CHUNK_SIZE)); slouken@1895: } slouken@1330: } slouken@1330: slouken@1330: /* Check a tree and its subtrees. */ slouken@1895: static void slouken@1895: do_check_tree(mstate m, tchunkptr t) slouken@1895: { slouken@1895: tchunkptr head = 0; slouken@1895: tchunkptr u = t; slouken@1895: bindex_t tindex = t->index; slouken@1895: size_t tsize = chunksize(t); slouken@1895: bindex_t idx; slouken@1895: compute_tree_index(tsize, idx); slouken@1895: assert(tindex == idx); slouken@1895: assert(tsize >= MIN_LARGE_SIZE); slouken@1895: assert(tsize >= minsize_for_tree_index(idx)); slouken@1895: assert((idx == NTREEBINS - 1) slouken@1895: || (tsize < minsize_for_tree_index((idx + 1)))); slouken@1895: slouken@1895: do { /* traverse through chain of same-sized nodes */ slouken@1895: do_check_any_chunk(m, ((mchunkptr) u)); slouken@1895: assert(u->index == tindex); slouken@1895: assert(chunksize(u) == tsize); slouken@1895: assert(!cinuse(u)); slouken@1895: assert(!next_pinuse(u)); slouken@1895: assert(u->fd->bk == u); slouken@1895: assert(u->bk->fd == u); slouken@1895: if (u->parent == 0) { slouken@1895: assert(u->child[0] == 0); slouken@1895: assert(u->child[1] == 0); slouken@1895: } else { slouken@1895: assert(head == 0); /* only one node on chain has parent */ slouken@1895: head = u; slouken@1895: assert(u->parent != u); slouken@1895: assert(u->parent->child[0] == u || slouken@1895: u->parent->child[1] == u || slouken@1895: *((tbinptr *) (u->parent)) == u); slouken@1895: if (u->child[0] != 0) { slouken@1895: assert(u->child[0]->parent == u); slouken@1895: assert(u->child[0] != u); slouken@1895: do_check_tree(m, u->child[0]); slouken@1895: } slouken@1895: if (u->child[1] != 0) { slouken@1895: assert(u->child[1]->parent == u); slouken@1895: assert(u->child[1] != u); slouken@1895: do_check_tree(m, u->child[1]); slouken@1895: } slouken@1895: if (u->child[0] != 0 && u->child[1] != 0) { slouken@1895: assert(chunksize(u->child[0]) < chunksize(u->child[1])); slouken@1895: } slouken@1895: } slouken@1895: u = u->fd; slouken@2203: } while (u != t); slouken@1895: assert(head != 0); slouken@1895: } slouken@1895: slouken@1895: /* Check all the chunks in a treebin. */ slouken@1895: static void slouken@1895: do_check_treebin(mstate m, bindex_t i) slouken@1895: { slouken@1895: tbinptr *tb = treebin_at(m, i); slouken@1895: tchunkptr t = *tb; slouken@1895: int empty = (m->treemap & (1U << i)) == 0; slouken@1895: if (t == 0) slouken@1895: assert(empty); slouken@1895: if (!empty) slouken@1895: do_check_tree(m, t); slouken@1895: } slouken@1895: slouken@1895: /* Check all the chunks in a smallbin. */ slouken@1895: static void slouken@1895: do_check_smallbin(mstate m, bindex_t i) slouken@1895: { slouken@1895: sbinptr b = smallbin_at(m, i); slouken@1895: mchunkptr p = b->bk; slouken@1895: unsigned int empty = (m->smallmap & (1U << i)) == 0; slouken@1895: if (p == b) slouken@1895: assert(empty); slouken@1895: if (!empty) { slouken@1895: for (; p != b; p = p->bk) { slouken@1895: size_t size = chunksize(p); slouken@1895: mchunkptr q; slouken@1895: /* each chunk claims to be free */ slouken@1895: do_check_free_chunk(m, p); slouken@1895: /* chunk belongs in bin */ slouken@1895: assert(small_index(size) == i); slouken@1895: assert(p->bk == b || chunksize(p->bk) == chunksize(p)); slouken@1895: /* chunk is followed by an inuse chunk */ slouken@1895: q = next_chunk(p); slouken@1895: if (q->head != FENCEPOST_HEAD) slouken@1895: do_check_inuse_chunk(m, q); slouken@1895: } slouken@1330: } slouken@1330: } slouken@1330: slouken@1895: /* Find x in a bin. Used in other check functions. */ slouken@1895: static int slouken@1895: bin_find(mstate m, mchunkptr x) slouken@1895: { slouken@1895: size_t size = chunksize(x); slouken@1895: if (is_small(size)) { slouken@1895: bindex_t sidx = small_index(size); slouken@1895: sbinptr b = smallbin_at(m, sidx); slouken@1895: if (smallmap_is_marked(m, sidx)) { slouken@1895: mchunkptr p = b; slouken@1895: do { slouken@1895: if (p == x) slouken@1895: return 1; slouken@2203: } while ((p = p->fd) != b); slouken@1895: } slouken@1895: } else { slouken@1895: bindex_t tidx; slouken@1895: compute_tree_index(size, tidx); slouken@1895: if (treemap_is_marked(m, tidx)) { slouken@1895: tchunkptr t = *treebin_at(m, tidx); slouken@1895: size_t sizebits = size << leftshift_for_tree_index(tidx); slouken@1895: while (t != 0 && chunksize(t) != size) { slouken@1895: t = t->child[(sizebits >> (SIZE_T_BITSIZE - SIZE_T_ONE)) & 1]; slouken@1895: sizebits <<= 1; slouken@1895: } slouken@1895: if (t != 0) { slouken@1895: tchunkptr u = t; slouken@1895: do { slouken@1895: if (u == (tchunkptr) x) slouken@1895: return 1; slouken@2203: } while ((u = u->fd) != t); slouken@1895: } slouken@1895: } slouken@1895: } slouken@1895: return 0; slouken@1330: } slouken@1330: slouken@1895: /* Traverse each chunk and check it; return total */ slouken@1895: static size_t slouken@1895: traverse_and_check(mstate m) slouken@1895: { slouken@1895: size_t sum = 0; slouken@1895: if (is_initialized(m)) { slouken@1895: msegmentptr s = &m->seg; slouken@1895: sum += m->topsize + TOP_FOOT_SIZE; slouken@1895: while (s != 0) { slouken@1895: mchunkptr q = align_as_chunk(s->base); slouken@1895: mchunkptr lastq = 0; slouken@1895: assert(pinuse(q)); slouken@1895: while (segment_holds(s, q) && slouken@1895: q != m->top && q->head != FENCEPOST_HEAD) { slouken@1895: sum += chunksize(q); slouken@1895: if (cinuse(q)) { slouken@1895: assert(!bin_find(m, q)); slouken@1895: do_check_inuse_chunk(m, q); slouken@1895: } else { slouken@1895: assert(q == m->dv || bin_find(m, q)); slouken@1895: assert(lastq == 0 || cinuse(lastq)); /* Not 2 consecutive free */ slouken@1895: do_check_free_chunk(m, q); slouken@1895: } slouken@1895: lastq = q; slouken@1895: q = next_chunk(q); slouken@1895: } slouken@1895: s = s->next; slouken@1895: } slouken@1330: } slouken@1895: return sum; slouken@1330: } slouken@1330: slouken@1895: /* Check all properties of malloc_state. */ slouken@1895: static void slouken@1895: do_check_malloc_state(mstate m) slouken@1895: { slouken@1895: bindex_t i; slouken@1895: size_t total; slouken@1895: /* check bins */ slouken@1895: for (i = 0; i < NSMALLBINS; ++i) slouken@1895: do_check_smallbin(m, i); slouken@1895: for (i = 0; i < NTREEBINS; ++i) slouken@1895: do_check_treebin(m, i); slouken@1895: slouken@1895: if (m->dvsize != 0) { /* check dv chunk */ slouken@1895: do_check_any_chunk(m, m->dv); slouken@1895: assert(m->dvsize == chunksize(m->dv)); slouken@1895: assert(m->dvsize >= MIN_CHUNK_SIZE); slouken@1895: assert(bin_find(m, m->dv) == 0); slouken@1330: } slouken@1895: slouken@1895: if (m->top != 0) { /* check top chunk */ slouken@1895: do_check_top_chunk(m, m->top); slouken@1895: assert(m->topsize == chunksize(m->top)); slouken@1895: assert(m->topsize > 0); slouken@1895: assert(bin_find(m, m->top) == 0); slouken@1330: } slouken@1895: slouken@1895: total = traverse_and_check(m); slouken@1895: assert(total <= m->footprint); slouken@1895: assert(m->footprint <= m->max_footprint); slouken@1330: } slouken@1330: #endif /* DEBUG */ slouken@1330: slouken@1330: /* ----------------------------- statistics ------------------------------ */ slouken@1330: slouken@1330: #if !NO_MALLINFO slouken@1895: static struct mallinfo slouken@1895: internal_mallinfo(mstate m) slouken@1895: { slouken@1895: struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; slouken@1895: if (!PREACTION(m)) { slouken@1895: check_malloc_state(m); slouken@1895: if (is_initialized(m)) { slouken@1895: size_t nfree = SIZE_T_ONE; /* top always free */ slouken@1895: size_t mfree = m->topsize + TOP_FOOT_SIZE; slouken@1895: size_t sum = mfree; slouken@1895: msegmentptr s = &m->seg; slouken@1895: while (s != 0) { slouken@1895: mchunkptr q = align_as_chunk(s->base); slouken@1895: while (segment_holds(s, q) && slouken@1895: q != m->top && q->head != FENCEPOST_HEAD) { slouken@1895: size_t sz = chunksize(q); slouken@1895: sum += sz; slouken@1895: if (!cinuse(q)) { slouken@1895: mfree += sz; slouken@1895: ++nfree; slouken@1895: } slouken@1895: q = next_chunk(q); slouken@1895: } slouken@1895: s = s->next; slouken@1895: } slouken@1895: slouken@1895: nm.arena = sum; slouken@1895: nm.ordblks = nfree; slouken@1895: nm.hblkhd = m->footprint - sum; slouken@1895: nm.usmblks = m->max_footprint; slouken@1895: nm.uordblks = m->footprint - mfree; slouken@1895: nm.fordblks = mfree; slouken@1895: nm.keepcost = m->topsize; slouken@1330: } slouken@1895: slouken@1895: POSTACTION(m); slouken@1330: } slouken@1895: return nm; slouken@1330: } slouken@1330: #endif /* !NO_MALLINFO */ slouken@1330: slouken@1895: static void slouken@1895: internal_malloc_stats(mstate m) slouken@1895: { slouken@1895: if (!PREACTION(m)) { slouken@1895: size_t maxfp = 0; slouken@1895: size_t fp = 0; slouken@1895: size_t used = 0; slouken@1895: check_malloc_state(m); slouken@1895: if (is_initialized(m)) { slouken@1895: msegmentptr s = &m->seg; slouken@1895: maxfp = m->max_footprint; slouken@1895: fp = m->footprint; slouken@1895: used = fp - (m->topsize + TOP_FOOT_SIZE); slouken@1895: slouken@1895: while (s != 0) { slouken@1895: mchunkptr q = align_as_chunk(s->base); slouken@1895: while (segment_holds(s, q) && slouken@1895: q != m->top && q->head != FENCEPOST_HEAD) { slouken@1895: if (!cinuse(q)) slouken@1895: used -= chunksize(q); slouken@1895: q = next_chunk(q); slouken@1895: } slouken@1895: s = s->next; slouken@1895: } slouken@1330: } slouken@1895: #ifndef LACKS_STDIO_H slouken@1895: fprintf(stderr, "max system bytes = %10lu\n", slouken@1895: (unsigned long) (maxfp)); slouken@1895: fprintf(stderr, "system bytes = %10lu\n", (unsigned long) (fp)); slouken@1895: fprintf(stderr, "in use bytes = %10lu\n", (unsigned long) (used)); slouken@1895: #endif slouken@1895: slouken@1895: POSTACTION(m); slouken@1330: } slouken@1330: } slouken@1330: slouken@1330: /* ----------------------- Operations on smallbins ----------------------- */ slouken@1330: slouken@1330: /* slouken@1330: Various forms of linking and unlinking are defined as macros. Even slouken@1330: the ones for trees, which are very long but have very short typical slouken@1330: paths. This is ugly but reduces reliance on inlining support of slouken@1330: compilers. slouken@1330: */ slouken@1330: slouken@1330: /* Link a free chunk into a smallbin */ slouken@1330: #define insert_small_chunk(M, P, S) {\ slouken@1330: bindex_t I = small_index(S);\ slouken@1330: mchunkptr B = smallbin_at(M, I);\ slouken@1330: mchunkptr F = B;\ slouken@1330: assert(S >= MIN_CHUNK_SIZE);\ slouken@1330: if (!smallmap_is_marked(M, I))\ slouken@1330: mark_smallmap(M, I);\ slouken@1330: else if (RTCHECK(ok_address(M, B->fd)))\ slouken@1330: F = B->fd;\ slouken@1330: else {\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: }\ slouken@1330: B->fd = P;\ slouken@1330: F->bk = P;\ slouken@1330: P->fd = F;\ slouken@1330: P->bk = B;\ slouken@1330: } slouken@1330: slouken@1330: /* Unlink a chunk from a smallbin */ slouken@1330: #define unlink_small_chunk(M, P, S) {\ slouken@1330: mchunkptr F = P->fd;\ slouken@1330: mchunkptr B = P->bk;\ slouken@1330: bindex_t I = small_index(S);\ slouken@1330: assert(P != B);\ slouken@1330: assert(P != F);\ slouken@1330: assert(chunksize(P) == small_index2size(I));\ slouken@1330: if (F == B)\ slouken@1330: clear_smallmap(M, I);\ slouken@1330: else if (RTCHECK((F == smallbin_at(M,I) || ok_address(M, F)) &&\ slouken@1330: (B == smallbin_at(M,I) || ok_address(M, B)))) {\ slouken@1330: F->bk = B;\ slouken@1330: B->fd = F;\ slouken@1330: }\ slouken@1330: else {\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: }\ slouken@1330: } slouken@1330: slouken@1330: /* Unlink the first chunk from a smallbin */ slouken@1330: #define unlink_first_small_chunk(M, B, P, I) {\ slouken@1330: mchunkptr F = P->fd;\ slouken@1330: assert(P != B);\ slouken@1330: assert(P != F);\ slouken@1330: assert(chunksize(P) == small_index2size(I));\ slouken@1330: if (B == F)\ slouken@1330: clear_smallmap(M, I);\ slouken@1330: else if (RTCHECK(ok_address(M, F))) {\ slouken@1330: B->fd = F;\ slouken@1330: F->bk = B;\ slouken@1330: }\ slouken@1330: else {\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: }\ slouken@1330: } slouken@1330: slouken@1330: /* Replace dv node, binning the old one */ slouken@1330: /* Used only when dvsize known to be small */ slouken@1330: #define replace_dv(M, P, S) {\ slouken@1330: size_t DVS = M->dvsize;\ slouken@1330: if (DVS != 0) {\ slouken@1330: mchunkptr DV = M->dv;\ slouken@1330: assert(is_small(DVS));\ slouken@1330: insert_small_chunk(M, DV, DVS);\ slouken@1330: }\ slouken@1330: M->dvsize = S;\ slouken@1330: M->dv = P;\ slouken@1330: } slouken@1330: slouken@1330: /* ------------------------- Operations on trees ------------------------- */ slouken@1330: slouken@1330: /* Insert chunk into tree */ slouken@1330: #define insert_large_chunk(M, X, S) {\ slouken@1330: tbinptr* H;\ slouken@1330: bindex_t I;\ slouken@1330: compute_tree_index(S, I);\ slouken@1330: H = treebin_at(M, I);\ slouken@1330: X->index = I;\ slouken@1330: X->child[0] = X->child[1] = 0;\ slouken@1330: if (!treemap_is_marked(M, I)) {\ slouken@1330: mark_treemap(M, I);\ slouken@1330: *H = X;\ slouken@1330: X->parent = (tchunkptr)H;\ slouken@1330: X->fd = X->bk = X;\ slouken@1330: }\ slouken@1330: else {\ slouken@1330: tchunkptr T = *H;\ slouken@1330: size_t K = S << leftshift_for_tree_index(I);\ slouken@1330: for (;;) {\ slouken@1330: if (chunksize(T) != S) {\ slouken@1330: tchunkptr* C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\ slouken@1330: K <<= 1;\ slouken@1330: if (*C != 0)\ slouken@1330: T = *C;\ slouken@1330: else if (RTCHECK(ok_address(M, C))) {\ slouken@1330: *C = X;\ slouken@1330: X->parent = T;\ slouken@1330: X->fd = X->bk = X;\ slouken@1330: break;\ slouken@1330: }\ slouken@1330: else {\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: break;\ slouken@1330: }\ slouken@1330: }\ slouken@1330: else {\ slouken@1330: tchunkptr F = T->fd;\ slouken@1330: if (RTCHECK(ok_address(M, T) && ok_address(M, F))) {\ slouken@1330: T->fd = F->bk = X;\ slouken@1330: X->fd = F;\ slouken@1330: X->bk = T;\ slouken@1330: X->parent = 0;\ slouken@1330: break;\ slouken@1330: }\ slouken@1330: else {\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: break;\ slouken@1330: }\ slouken@1330: }\ slouken@1330: }\ slouken@1330: }\ slouken@1330: } slouken@1330: slouken@1330: /* slouken@1330: Unlink steps: slouken@1330: slouken@1330: 1. If x is a chained node, unlink it from its same-sized fd/bk links slouken@1330: and choose its bk node as its replacement. slouken@1330: 2. If x was the last node of its size, but not a leaf node, it must slouken@1330: be replaced with a leaf node (not merely one with an open left or slouken@1330: right), to make sure that lefts and rights of descendents slouken@1330: correspond properly to bit masks. We use the rightmost descendent slouken@1330: of x. We could use any other leaf, but this is easy to locate and slouken@1330: tends to counteract removal of leftmosts elsewhere, and so keeps slouken@1330: paths shorter than minimally guaranteed. This doesn't loop much slouken@1330: because on average a node in a tree is near the bottom. slouken@1330: 3. If x is the base of a chain (i.e., has parent links) relink slouken@1330: x's parent and children to x's replacement (or null if none). slouken@1330: */ slouken@1330: slouken@1330: #define unlink_large_chunk(M, X) {\ slouken@1330: tchunkptr XP = X->parent;\ slouken@1330: tchunkptr R;\ slouken@1330: if (X->bk != X) {\ slouken@1330: tchunkptr F = X->fd;\ slouken@1330: R = X->bk;\ slouken@1330: if (RTCHECK(ok_address(M, F))) {\ slouken@1330: F->bk = R;\ slouken@1330: R->fd = F;\ slouken@1330: }\ slouken@1330: else {\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: }\ slouken@1330: }\ slouken@1330: else {\ slouken@1330: tchunkptr* RP;\ slouken@1330: if (((R = *(RP = &(X->child[1]))) != 0) ||\ slouken@1330: ((R = *(RP = &(X->child[0]))) != 0)) {\ slouken@1330: tchunkptr* CP;\ slouken@1330: while ((*(CP = &(R->child[1])) != 0) ||\ slouken@1330: (*(CP = &(R->child[0])) != 0)) {\ slouken@1330: R = *(RP = CP);\ slouken@1330: }\ slouken@1330: if (RTCHECK(ok_address(M, RP)))\ slouken@1330: *RP = 0;\ slouken@1330: else {\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: }\ slouken@1330: }\ slouken@1330: }\ slouken@1330: if (XP != 0) {\ slouken@1330: tbinptr* H = treebin_at(M, X->index);\ slouken@1330: if (X == *H) {\ slouken@1330: if ((*H = R) == 0) \ slouken@1330: clear_treemap(M, X->index);\ slouken@1330: }\ slouken@1330: else if (RTCHECK(ok_address(M, XP))) {\ slouken@1330: if (XP->child[0] == X) \ slouken@1330: XP->child[0] = R;\ slouken@1330: else \ slouken@1330: XP->child[1] = R;\ slouken@1330: }\ slouken@1330: else\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: if (R != 0) {\ slouken@1330: if (RTCHECK(ok_address(M, R))) {\ slouken@1330: tchunkptr C0, C1;\ slouken@1330: R->parent = XP;\ slouken@1330: if ((C0 = X->child[0]) != 0) {\ slouken@1330: if (RTCHECK(ok_address(M, C0))) {\ slouken@1330: R->child[0] = C0;\ slouken@1330: C0->parent = R;\ slouken@1330: }\ slouken@1330: else\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: }\ slouken@1330: if ((C1 = X->child[1]) != 0) {\ slouken@1330: if (RTCHECK(ok_address(M, C1))) {\ slouken@1330: R->child[1] = C1;\ slouken@1330: C1->parent = R;\ slouken@1330: }\ slouken@1330: else\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: }\ slouken@1330: }\ slouken@1330: else\ slouken@1330: CORRUPTION_ERROR_ACTION(M);\ slouken@1330: }\ slouken@1330: }\ slouken@1330: } slouken@1330: slouken@1330: /* Relays to large vs small bin operations */ slouken@1330: slouken@1330: #define insert_chunk(M, P, S)\ slouken@1330: if (is_small(S)) insert_small_chunk(M, P, S)\ slouken@1330: else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } slouken@1330: slouken@1330: #define unlink_chunk(M, P, S)\ slouken@1330: if (is_small(S)) unlink_small_chunk(M, P, S)\ slouken@1330: else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); } slouken@1330: slouken@1330: slouken@1330: /* Relays to internal calls to malloc/free from realloc, memalign etc */ slouken@1330: slouken@1330: #if ONLY_MSPACES slouken@1330: #define internal_malloc(m, b) mspace_malloc(m, b) slouken@1330: #define internal_free(m, mem) mspace_free(m,mem); slouken@1330: #else /* ONLY_MSPACES */ slouken@1330: #if MSPACES slouken@1330: #define internal_malloc(m, b)\ slouken@1330: (m == gm)? dlmalloc(b) : mspace_malloc(m, b) slouken@1330: #define internal_free(m, mem)\ slouken@1330: if (m == gm) dlfree(mem); else mspace_free(m,mem); slouken@1330: #else /* MSPACES */ slouken@1330: #define internal_malloc(m, b) dlmalloc(b) slouken@1330: #define internal_free(m, mem) dlfree(mem) slouken@1330: #endif /* MSPACES */ slouken@1330: #endif /* ONLY_MSPACES */ slouken@1330: slouken@1330: /* ----------------------- Direct-mmapping chunks ----------------------- */ slouken@1330: slouken@1330: /* slouken@1330: Directly mmapped chunks are set up with an offset to the start of slouken@1330: the mmapped region stored in the prev_foot field of the chunk. This slouken@1330: allows reconstruction of the required argument to MUNMAP when freed, slouken@1330: and also allows adjustment of the returned chunk to meet alignment slouken@1330: requirements (especially in memalign). There is also enough space slouken@1330: allocated to hold a fake next chunk of size SIZE_T_SIZE to maintain slouken@1330: the PINUSE bit so frees can be checked. slouken@1330: */ slouken@1330: slouken@1330: /* Malloc using mmap */ slouken@1895: static void * slouken@1895: mmap_alloc(mstate m, size_t nb) slouken@1895: { slouken@1895: size_t mmsize = slouken@1895: granularity_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); slouken@1895: if (mmsize > nb) { /* Check for wrap around 0 */ slouken@1895: char *mm = (char *) (DIRECT_MMAP(mmsize)); slouken@1895: if (mm != CMFAIL) { slouken@1895: size_t offset = align_offset(chunk2mem(mm)); slouken@1895: size_t psize = mmsize - offset - MMAP_FOOT_PAD; slouken@1895: mchunkptr p = (mchunkptr) (mm + offset); slouken@1895: p->prev_foot = offset | IS_MMAPPED_BIT; slouken@1895: (p)->head = (psize | CINUSE_BIT); slouken@1895: mark_inuse_foot(m, p, psize); slouken@1895: chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; slouken@1895: chunk_plus_offset(p, psize + SIZE_T_SIZE)->head = 0; slouken@1895: slouken@1895: if (mm < m->least_addr) slouken@1895: m->least_addr = mm; slouken@1895: if ((m->footprint += mmsize) > m->max_footprint) slouken@1895: m->max_footprint = m->footprint; slouken@1895: assert(is_aligned(chunk2mem(p))); slouken@1895: check_mmapped_chunk(m, p); slouken@1895: return chunk2mem(p); slouken@1895: } slouken@1330: } slouken@1895: return 0; slouken@1330: } slouken@1330: slouken@1330: /* Realloc using mmap */ slouken@1895: static mchunkptr slouken@1895: mmap_resize(mstate m, mchunkptr oldp, size_t nb) slouken@1895: { slouken@1895: size_t oldsize = chunksize(oldp); slouken@1895: if (is_small(nb)) /* Can't shrink mmap regions below small size */ slouken@1895: return 0; slouken@1895: /* Keep old chunk if big enough but not too big */ slouken@1895: if (oldsize >= nb + SIZE_T_SIZE && slouken@1895: (oldsize - nb) <= (mparams.granularity << 1)) slouken@1895: return oldp; slouken@1895: else { slouken@1895: size_t offset = oldp->prev_foot & ~IS_MMAPPED_BIT; slouken@1895: size_t oldmmsize = oldsize + offset + MMAP_FOOT_PAD; slouken@1895: size_t newmmsize = granularity_align(nb + SIX_SIZE_T_SIZES + slouken@1895: CHUNK_ALIGN_MASK); slouken@1895: char *cp = (char *) CALL_MREMAP((char *) oldp - offset, slouken@1895: oldmmsize, newmmsize, 1); slouken@1895: if (cp != CMFAIL) { slouken@1895: mchunkptr newp = (mchunkptr) (cp + offset); slouken@1895: size_t psize = newmmsize - offset - MMAP_FOOT_PAD; slouken@1895: newp->head = (psize | CINUSE_BIT); slouken@1895: mark_inuse_foot(m, newp, psize); slouken@1895: chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; slouken@1895: chunk_plus_offset(newp, psize + SIZE_T_SIZE)->head = 0; slouken@1895: slouken@1895: if (cp < m->least_addr) slouken@1895: m->least_addr = cp; slouken@1895: if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint) slouken@1895: m->max_footprint = m->footprint; slouken@1895: check_mmapped_chunk(m, newp); slouken@1895: return newp; slouken@1895: } slouken@1895: } slouken@1330: return 0; slouken@1895: } slouken@1895: slouken@1895: /* -------------------------- mspace management -------------------------- */ slouken@1895: slouken@1895: /* Initialize top chunk and its size */ slouken@1895: static void slouken@1895: init_top(mstate m, mchunkptr p, size_t psize) slouken@1895: { slouken@1895: /* Ensure alignment */ slouken@1895: size_t offset = align_offset(chunk2mem(p)); slouken@1895: p = (mchunkptr) ((char *) p + offset); slouken@1895: psize -= offset; slouken@1895: slouken@1895: m->top = p; slouken@1895: m->topsize = psize; slouken@1895: p->head = psize | PINUSE_BIT; slouken@1895: /* set size of fake trailing chunk holding overhead space only once */ slouken@1895: chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE; slouken@1895: m->trim_check = mparams.trim_threshold; /* reset on each update */ slouken@1895: } slouken@1895: slouken@1895: /* Initialize bins for a new mstate that is otherwise zeroed out */ slouken@1895: static void slouken@1895: init_bins(mstate m) slouken@1895: { slouken@1895: /* Establish circular links for smallbins */ slouken@1895: bindex_t i; slouken@1895: for (i = 0; i < NSMALLBINS; ++i) { slouken@1895: sbinptr bin = smallbin_at(m, i); slouken@1895: bin->fd = bin->bk = bin; slouken@1330: } slouken@1330: } slouken@1330: slouken@1330: #if PROCEED_ON_ERROR slouken@1330: slouken@1330: /* default corruption action */ slouken@1895: static void slouken@1895: reset_on_error(mstate m) slouken@1895: { slouken@1895: int i; slouken@1895: ++malloc_corruption_error_count; slouken@1895: /* Reinitialize fields to forget about all memory */ slouken@1895: m->smallbins = m->treebins = 0; slouken@1895: m->dvsize = m->topsize = 0; slouken@1895: m->seg.base = 0; slouken@1895: m->seg.size = 0; slouken@1895: m->seg.next = 0; slouken@1895: m->top = m->dv = 0; slouken@1895: for (i = 0; i < NTREEBINS; ++i) slouken@1895: *treebin_at(m, i) = 0; slouken@1895: init_bins(m); slouken@1330: } slouken@1330: #endif /* PROCEED_ON_ERROR */ slouken@1330: slouken@1330: /* Allocate chunk and prepend remainder with chunk in successor base. */ slouken@1895: static void * slouken@1895: prepend_alloc(mstate m, char *newbase, char *oldbase, size_t nb) slouken@1895: { slouken@1895: mchunkptr p = align_as_chunk(newbase); slouken@1895: mchunkptr oldfirst = align_as_chunk(oldbase); slouken@1895: size_t psize = (char *) oldfirst - (char *) p; slouken@1895: mchunkptr q = chunk_plus_offset(p, nb); slouken@1895: size_t qsize = psize - nb; slouken@1895: set_size_and_pinuse_of_inuse_chunk(m, p, nb); slouken@1895: slouken@1895: assert((char *) oldfirst > (char *) q); slouken@1895: assert(pinuse(oldfirst)); slouken@1895: assert(qsize >= MIN_CHUNK_SIZE); slouken@1895: slouken@1895: /* consolidate remainder with first chunk of old base */ slouken@1895: if (oldfirst == m->top) { slouken@1895: size_t tsize = m->topsize += qsize; slouken@1895: m->top = q; slouken@1895: q->head = tsize | PINUSE_BIT; slouken@1895: check_top_chunk(m, q); slouken@1895: } else if (oldfirst == m->dv) { slouken@1895: size_t dsize = m->dvsize += qsize; slouken@1895: m->dv = q; slouken@1895: set_size_and_pinuse_of_free_chunk(q, dsize); slouken@1895: } else { slouken@1895: if (!cinuse(oldfirst)) { slouken@1895: size_t nsize = chunksize(oldfirst); slouken@1895: unlink_chunk(m, oldfirst, nsize); slouken@1895: oldfirst = chunk_plus_offset(oldfirst, nsize); slouken@1895: qsize += nsize; slouken@1895: } slouken@1895: set_free_with_pinuse(q, qsize, oldfirst); slouken@1895: insert_chunk(m, q, qsize); slouken@1895: check_free_chunk(m, q); slouken@1330: } slouken@1895: slouken@1895: check_malloced_chunk(m, chunk2mem(p), nb); slouken@1895: return chunk2mem(p); slouken@1330: } slouken@1330: slouken@1330: slouken@1330: /* Add a segment to hold a new noncontiguous region */ slouken@1895: static void slouken@1895: add_segment(mstate m, char *tbase, size_t tsize, flag_t mmapped) slouken@1895: { slouken@1895: /* Determine locations and sizes of segment, fenceposts, old top */ slouken@1895: char *old_top = (char *) m->top; slouken@1895: msegmentptr oldsp = segment_holding(m, old_top); slouken@1895: char *old_end = oldsp->base + oldsp->size; slouken@1895: size_t ssize = pad_request(sizeof(struct malloc_segment)); slouken@1895: char *rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK); slouken@1895: size_t offset = align_offset(chunk2mem(rawsp)); slouken@1895: char *asp = rawsp + offset; slouken@1895: char *csp = (asp < (old_top + MIN_CHUNK_SIZE)) ? old_top : asp; slouken@1895: mchunkptr sp = (mchunkptr) csp; slouken@1895: msegmentptr ss = (msegmentptr) (chunk2mem(sp)); slouken@1895: mchunkptr tnext = chunk_plus_offset(sp, ssize); slouken@1895: mchunkptr p = tnext; slouken@1895: int nfences = 0; slouken@1895: slouken@1895: /* reset top to new space */ slouken@1895: init_top(m, (mchunkptr) tbase, tsize - TOP_FOOT_SIZE); slouken@1895: slouken@1895: /* Set up segment record */ slouken@1895: assert(is_aligned(ss)); slouken@1895: set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); slouken@1895: *ss = m->seg; /* Push current record */ slouken@1895: m->seg.base = tbase; slouken@1895: m->seg.size = tsize; slouken@1895: m->seg.sflags = mmapped; slouken@1895: m->seg.next = ss; slouken@1895: slouken@1895: /* Insert trailing fenceposts */ slouken@1895: for (;;) { slouken@1895: mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); slouken@1895: p->head = FENCEPOST_HEAD; slouken@1895: ++nfences; slouken@1895: if ((char *) (&(nextp->head)) < old_end) slouken@1895: p = nextp; slouken@1895: else slouken@1895: break; slouken@1895: } slouken@1895: assert(nfences >= 2); slouken@1895: slouken@1895: /* Insert the rest of old top into a bin as an ordinary free chunk */ slouken@1895: if (csp != old_top) { slouken@1895: mchunkptr q = (mchunkptr) old_top; slouken@1895: size_t psize = csp - old_top; slouken@1895: mchunkptr tn = chunk_plus_offset(q, psize); slouken@1895: set_free_with_pinuse(q, psize, tn); slouken@1895: insert_chunk(m, q, psize); slouken@1895: } slouken@1895: slouken@1895: check_top_chunk(m, m->top); slouken@1330: } slouken@1330: slouken@1330: /* -------------------------- System allocation -------------------------- */ slouken@1330: slouken@1330: /* Get memory from system using MORECORE or MMAP */ slouken@1895: static void * slouken@1895: sys_alloc(mstate m, size_t nb) slouken@1895: { slouken@1895: char *tbase = CMFAIL; slouken@1895: size_t tsize = 0; slouken@1895: flag_t mmap_flag = 0; slouken@1895: slouken@1895: init_mparams(); slouken@1895: slouken@1895: /* Directly map large chunks */ slouken@1895: if (use_mmap(m) && nb >= mparams.mmap_threshold) { slouken@1895: void *mem = mmap_alloc(m, nb); slouken@1895: if (mem != 0) slouken@1895: return mem; slouken@1895: } slouken@1895: slouken@1895: /* slouken@1895: Try getting memory in any of three ways (in most-preferred to slouken@1895: least-preferred order): slouken@1895: 1. A call to MORECORE that can normally contiguously extend memory. slouken@1330: (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or slouken@1330: or main space is mmapped or a previous contiguous call failed) slouken@1895: 2. A call to MMAP new space (disabled if not HAVE_MMAP). slouken@1330: Note that under the default settings, if MORECORE is unable to slouken@1330: fulfill a request, and HAVE_MMAP is true, then mmap is slouken@1330: used as a noncontiguous system allocator. This is a useful backup slouken@1330: strategy for systems with holes in address spaces -- in this case slouken@1330: sbrk cannot contiguously expand the heap, but mmap may be able to slouken@1330: find space. slouken@1895: 3. A call to MORECORE that cannot usually contiguously extend memory. slouken@1330: (disabled if not HAVE_MORECORE) slouken@1895: */ slouken@1895: slouken@1895: if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) { slouken@1895: char *br = CMFAIL; slouken@1895: msegmentptr ss = slouken@1895: (m->top == 0) ? 0 : segment_holding(m, (char *) m->top); slouken@1895: size_t asize = 0; slouken@1895: ACQUIRE_MORECORE_LOCK(); slouken@1895: slouken@1895: if (ss == 0) { /* First time through or recovery */ slouken@1895: char *base = (char *) CALL_MORECORE(0); slouken@1895: if (base != CMFAIL) { slouken@2203: asize = slouken@2203: granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + slouken@2203: SIZE_T_ONE); slouken@1895: /* Adjust to end on a page boundary */ slouken@1895: if (!is_page_aligned(base)) slouken@1895: asize += (page_align((size_t) base) - (size_t) base); slouken@1895: /* Can't call MORECORE if size is negative when treated as signed */ slouken@1895: if (asize < HALF_MAX_SIZE_T && slouken@1895: (br = (char *) (CALL_MORECORE(asize))) == base) { slouken@1895: tbase = base; slouken@1895: tsize = asize; slouken@1895: } slouken@1895: } slouken@1895: } else { slouken@1895: /* Subtract out existing available top space from MORECORE request. */ slouken@1895: asize = slouken@1895: granularity_align(nb - m->topsize + TOP_FOOT_SIZE + slouken@2203: MALLOC_ALIGNMENT + SIZE_T_ONE); slouken@1895: /* Use mem here only if it did continuously extend old space */ slouken@1895: if (asize < HALF_MAX_SIZE_T && slouken@1895: (br = slouken@1895: (char *) (CALL_MORECORE(asize))) == ss->base + ss->size) { slouken@1895: tbase = br; slouken@1895: tsize = asize; slouken@1895: } slouken@1330: } slouken@1895: slouken@1895: if (tbase == CMFAIL) { /* Cope with partial failure */ slouken@1895: if (br != CMFAIL) { /* Try to use/extend the space we did get */ slouken@1895: if (asize < HALF_MAX_SIZE_T && slouken@1895: asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) { slouken@1895: size_t esize = slouken@1895: granularity_align(nb + TOP_FOOT_SIZE + slouken@2203: MALLOC_ALIGNMENT + SIZE_T_ONE - slouken@2203: asize); slouken@1895: if (esize < HALF_MAX_SIZE_T) { slouken@1895: char *end = (char *) CALL_MORECORE(esize); slouken@1895: if (end != CMFAIL) slouken@1895: asize += esize; slouken@1895: else { /* Can't use; try to release */ slouken@1895: end = (char *) CALL_MORECORE(-asize); slouken@1895: br = CMFAIL; slouken@1895: } slouken@1895: } slouken@1895: } slouken@1895: } slouken@1895: if (br != CMFAIL) { /* Use the space we did get */ slouken@1895: tbase = br; slouken@1895: tsize = asize; slouken@1895: } else slouken@1895: disable_contiguous(m); /* Don't try contiguous path in the future */ slouken@1895: } slouken@1895: slouken@1895: RELEASE_MORECORE_LOCK(); slouken@1330: } slouken@1895: slouken@1895: if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */ slouken@2203: size_t req = nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE; slouken@1895: size_t rsize = granularity_align(req); slouken@1895: if (rsize > nb) { /* Fail if wraps around zero */ slouken@1895: char *mp = (char *) (CALL_MMAP(rsize)); slouken@1895: if (mp != CMFAIL) { slouken@1895: tbase = mp; slouken@1895: tsize = rsize; slouken@1895: mmap_flag = IS_MMAPPED_BIT; slouken@1895: } slouken@1895: } slouken@1330: } slouken@1330: slouken@1895: if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ slouken@2203: size_t asize = slouken@2203: granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + slouken@2203: SIZE_T_ONE); slouken@1895: if (asize < HALF_MAX_SIZE_T) { slouken@1895: char *br = CMFAIL; slouken@1895: char *end = CMFAIL; slouken@1895: ACQUIRE_MORECORE_LOCK(); slouken@1895: br = (char *) (CALL_MORECORE(asize)); slouken@1895: end = (char *) (CALL_MORECORE(0)); slouken@1895: RELEASE_MORECORE_LOCK(); slouken@1895: if (br != CMFAIL && end != CMFAIL && br < end) { slouken@1895: size_t ssize = end - br; slouken@1895: if (ssize > nb + TOP_FOOT_SIZE) { slouken@1895: tbase = br; slouken@1895: tsize = ssize; slouken@1895: } slouken@1330: } slouken@1330: } slouken@1330: } slouken@1330: slouken@1895: if (tbase != CMFAIL) { slouken@1895: slouken@1895: if ((m->footprint += tsize) > m->max_footprint) slouken@1895: m->max_footprint = m->footprint; slouken@1895: slouken@1895: if (!is_initialized(m)) { /* first-time initialization */ slouken@1895: m->seg.base = m->least_addr = tbase; slouken@1895: m->seg.size = tsize; slouken@1895: m->seg.sflags = mmap_flag; slouken@1895: m->magic = mparams.magic; slouken@1895: init_bins(m); slouken@1895: if (is_global(m)) slouken@1895: init_top(m, (mchunkptr) tbase, tsize - TOP_FOOT_SIZE); slouken@1895: else { slouken@1895: /* Offset top by embedded malloc_state */ slouken@1895: mchunkptr mn = next_chunk(mem2chunk(m)); slouken@1895: init_top(m, mn, slouken@1895: (size_t) ((tbase + tsize) - (char *) mn) - slouken@1895: TOP_FOOT_SIZE); slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: else { slouken@1895: /* Try to merge with an existing segment */ slouken@1895: msegmentptr sp = &m->seg; slouken@1895: while (sp != 0 && tbase != sp->base + sp->size) slouken@1895: sp = sp->next; slouken@1895: if (sp != 0 && !is_extern_segment(sp) && (sp->sflags & IS_MMAPPED_BIT) == mmap_flag && segment_holds(sp, m->top)) { /* append */ slouken@1895: sp->size += tsize; slouken@1895: init_top(m, m->top, m->topsize + tsize); slouken@1895: } else { slouken@1895: if (tbase < m->least_addr) slouken@1895: m->least_addr = tbase; slouken@1895: sp = &m->seg; slouken@1895: while (sp != 0 && sp->base != tbase + tsize) slouken@1895: sp = sp->next; slouken@1895: if (sp != 0 && slouken@1895: !is_extern_segment(sp) && slouken@1895: (sp->sflags & IS_MMAPPED_BIT) == mmap_flag) { slouken@1895: char *oldbase = sp->base; slouken@1895: sp->base = tbase; slouken@1895: sp->size += tsize; slouken@1895: return prepend_alloc(m, tbase, oldbase, nb); slouken@1895: } else slouken@1895: add_segment(m, tbase, tsize, mmap_flag); slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: if (nb < m->topsize) { /* Allocate from new or extended top space */ slouken@1895: size_t rsize = m->topsize -= nb; slouken@1895: mchunkptr p = m->top; slouken@1895: mchunkptr r = m->top = chunk_plus_offset(p, nb); slouken@1895: r->head = rsize | PINUSE_BIT; slouken@1895: set_size_and_pinuse_of_inuse_chunk(m, p, nb); slouken@1895: check_top_chunk(m, m->top); slouken@1895: check_malloced_chunk(m, chunk2mem(p), nb); slouken@1895: return chunk2mem(p); slouken@1895: } slouken@1330: } slouken@1895: slouken@1330: MALLOC_FAILURE_ACTION; slouken@1330: return 0; slouken@1895: } slouken@1895: slouken@1895: /* ----------------------- system deallocation -------------------------- */ slouken@1895: slouken@1895: /* Unmap and unlink any mmapped segments that don't contain used chunks */ slouken@1895: static size_t slouken@1895: release_unused_segments(mstate m) slouken@1895: { slouken@1895: size_t released = 0; slouken@1895: msegmentptr pred = &m->seg; slouken@1895: msegmentptr sp = pred->next; slouken@1895: while (sp != 0) { slouken@1895: char *base = sp->base; slouken@1895: size_t size = sp->size; slouken@1895: msegmentptr next = sp->next; slouken@1895: if (is_mmapped_segment(sp) && !is_extern_segment(sp)) { slouken@1895: mchunkptr p = align_as_chunk(base); slouken@1895: size_t psize = chunksize(p); slouken@1895: /* Can unmap if first chunk holds entire segment and not pinned */ slouken@1895: if (!cinuse(p) slouken@1895: && (char *) p + psize >= base + size - TOP_FOOT_SIZE) { slouken@1895: tchunkptr tp = (tchunkptr) p; slouken@1895: assert(segment_holds(sp, (char *) sp)); slouken@1895: if (p == m->dv) { slouken@1895: m->dv = 0; slouken@1895: m->dvsize = 0; slouken@1895: } else { slouken@1895: unlink_large_chunk(m, tp); slouken@1895: } slouken@1895: if (CALL_MUNMAP(base, size) == 0) { slouken@1895: released += size; slouken@1895: m->footprint -= size; slouken@1895: /* unlink obsoleted record */ slouken@1895: sp = pred; slouken@1895: sp->next = next; slouken@1895: } else { /* back out if cannot unmap */ slouken@1895: insert_large_chunk(m, tp, psize); slouken@1895: } slouken@1895: } slouken@1330: } slouken@1895: pred = sp; slouken@1895: sp = next; slouken@1330: } slouken@1895: return released; slouken@1895: } slouken@1895: slouken@1895: static int slouken@1895: sys_trim(mstate m, size_t pad) slouken@1895: { slouken@1895: size_t released = 0; slouken@1895: if (pad < MAX_REQUEST && is_initialized(m)) { slouken@1895: pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ slouken@1895: slouken@1895: if (m->topsize > pad) { slouken@1895: /* Shrink top space in granularity-size units, keeping at least one */ slouken@1895: size_t unit = mparams.granularity; slouken@2203: size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - slouken@2203: SIZE_T_ONE) * unit; slouken@1895: msegmentptr sp = segment_holding(m, (char *) m->top); slouken@1895: slouken@1895: if (!is_extern_segment(sp)) { slouken@1895: if (is_mmapped_segment(sp)) { slouken@1895: if (HAVE_MMAP && sp->size >= extra && !has_segment_link(m, sp)) { /* can't shrink if pinned */ slouken@1895: size_t newsize = sp->size - extra; slouken@1895: /* Prefer mremap, fall back to munmap */ slouken@2203: if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) != slouken@2203: MFAIL) slouken@2210: || (CALL_MUNMAP(sp->base + newsize, extra) == 0)) { slouken@1895: released = extra; slouken@1895: } slouken@1895: } slouken@1895: } else if (HAVE_MORECORE) { slouken@1895: if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */ slouken@1895: extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit; slouken@1895: ACQUIRE_MORECORE_LOCK(); slouken@1895: { slouken@1895: /* Make sure end of memory is where we last set it. */ slouken@1895: char *old_br = (char *) (CALL_MORECORE(0)); slouken@1895: if (old_br == sp->base + sp->size) { slouken@1895: char *rel_br = (char *) (CALL_MORECORE(-extra)); slouken@1895: char *new_br = (char *) (CALL_MORECORE(0)); slouken@1895: if (rel_br != CMFAIL && new_br < old_br) slouken@1895: released = old_br - new_br; slouken@1895: } slouken@1895: } slouken@1895: RELEASE_MORECORE_LOCK(); slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: if (released != 0) { slouken@1895: sp->size -= released; slouken@1895: m->footprint -= released; slouken@1895: init_top(m, m->top, m->topsize - released); slouken@1895: check_top_chunk(m, m->top); slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: /* Unmap any unused mmapped segments */ slouken@1895: if (HAVE_MMAP) slouken@1895: released += release_unused_segments(m); slouken@1895: slouken@1895: /* On failure, disable autotrim to avoid repeated failed future calls */ slouken@1895: if (released == 0) slouken@1895: m->trim_check = MAX_SIZE_T; slouken@1330: } slouken@1330: slouken@1895: return (released != 0) ? 1 : 0; slouken@1895: } slouken@1895: slouken@1895: /* ---------------------------- malloc support --------------------------- */ slouken@1895: slouken@1895: /* allocate a large request from the best fitting chunk in a treebin */ slouken@1895: static void * slouken@1895: tmalloc_large(mstate m, size_t nb) slouken@1895: { slouken@1895: tchunkptr v = 0; slouken@1895: size_t rsize = -nb; /* Unsigned negation */ slouken@1895: tchunkptr t; slouken@1895: bindex_t idx; slouken@1895: compute_tree_index(nb, idx); slouken@1895: slouken@1895: if ((t = *treebin_at(m, idx)) != 0) { slouken@1895: /* Traverse tree for this bin looking for node with size == nb */ slouken@1895: size_t sizebits = nb << leftshift_for_tree_index(idx); slouken@1895: tchunkptr rst = 0; /* The deepest untaken right subtree */ slouken@1895: for (;;) { slouken@1895: tchunkptr rt; slouken@1895: size_t trem = chunksize(t) - nb; slouken@1895: if (trem < rsize) { slouken@1895: v = t; slouken@1895: if ((rsize = trem) == 0) slouken@1895: break; slouken@1895: } slouken@1895: rt = t->child[1]; slouken@1895: t = t->child[(sizebits >> (SIZE_T_BITSIZE - SIZE_T_ONE)) & 1]; slouken@1895: if (rt != 0 && rt != t) slouken@1895: rst = rt; slouken@1895: if (t == 0) { slouken@1895: t = rst; /* set t to least subtree holding sizes > nb */ slouken@1895: break; slouken@1895: } slouken@1895: sizebits <<= 1; slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */ slouken@1895: binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap; slouken@1895: if (leftbits != 0) { slouken@1895: bindex_t i; slouken@1895: binmap_t leastbit = least_bit(leftbits); slouken@1895: compute_bit2idx(leastbit, i); slouken@1895: t = *treebin_at(m, i); slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: while (t != 0) { /* find smallest of tree or subtree */ slouken@1895: size_t trem = chunksize(t) - nb; slouken@1895: if (trem < rsize) { slouken@1895: rsize = trem; slouken@1895: v = t; slouken@1895: } slouken@1895: t = leftmost_child(t); slouken@1895: } slouken@1895: slouken@1895: /* If dv is a better fit, return 0 so malloc will use it */ slouken@1895: if (v != 0 && rsize < (size_t) (m->dvsize - nb)) { slouken@1895: if (RTCHECK(ok_address(m, v))) { /* split */ slouken@1895: mchunkptr r = chunk_plus_offset(v, nb); slouken@1895: assert(chunksize(v) == rsize + nb); slouken@1895: if (RTCHECK(ok_next(v, r))) { slouken@1895: unlink_large_chunk(m, v); slouken@1895: if (rsize < MIN_CHUNK_SIZE) slouken@1895: set_inuse_and_pinuse(m, v, (rsize + nb)); slouken@1895: else { slouken@1895: set_size_and_pinuse_of_inuse_chunk(m, v, nb); slouken@1895: set_size_and_pinuse_of_free_chunk(r, rsize); slouken@1895: insert_chunk(m, r, rsize); slouken@1895: } slouken@1895: return chunk2mem(v); slouken@1895: } slouken@1895: } slouken@1895: CORRUPTION_ERROR_ACTION(m); slouken@1895: } slouken@1895: return 0; slouken@1895: } slouken@1895: slouken@1895: /* allocate a small request from the best fitting chunk in a treebin */ slouken@1895: static void * slouken@1895: tmalloc_small(mstate m, size_t nb) slouken@1895: { slouken@1895: tchunkptr t, v; slouken@1895: size_t rsize; slouken@1895: bindex_t i; slouken@1895: binmap_t leastbit = least_bit(m->treemap); slouken@1895: compute_bit2idx(leastbit, i); slouken@1895: slouken@1895: v = t = *treebin_at(m, i); slouken@1895: rsize = chunksize(t) - nb; slouken@1895: slouken@1895: while ((t = leftmost_child(t)) != 0) { slouken@1895: size_t trem = chunksize(t) - nb; slouken@1895: if (trem < rsize) { slouken@1895: rsize = trem; slouken@1895: v = t; slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: if (RTCHECK(ok_address(m, v))) { slouken@1895: mchunkptr r = chunk_plus_offset(v, nb); slouken@1895: assert(chunksize(v) == rsize + nb); slouken@1895: if (RTCHECK(ok_next(v, r))) { slouken@1895: unlink_large_chunk(m, v); slouken@1895: if (rsize < MIN_CHUNK_SIZE) slouken@1895: set_inuse_and_pinuse(m, v, (rsize + nb)); slouken@1895: else { slouken@1895: set_size_and_pinuse_of_inuse_chunk(m, v, nb); slouken@1895: set_size_and_pinuse_of_free_chunk(r, rsize); slouken@1895: replace_dv(m, r, rsize); slouken@1895: } slouken@1895: return chunk2mem(v); slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: CORRUPTION_ERROR_ACTION(m); slouken@1895: return 0; slouken@1895: } slouken@1895: slouken@1895: /* --------------------------- realloc support --------------------------- */ slouken@1895: slouken@1895: static void * slouken@1895: internal_realloc(mstate m, void *oldmem, size_t bytes) slouken@1895: { slouken@1895: if (bytes >= MAX_REQUEST) { slouken@1895: MALLOC_FAILURE_ACTION; slouken@1895: return 0; slouken@1895: } slouken@1895: if (!PREACTION(m)) { slouken@1895: mchunkptr oldp = mem2chunk(oldmem); slouken@1895: size_t oldsize = chunksize(oldp); slouken@1895: mchunkptr next = chunk_plus_offset(oldp, oldsize); slouken@1895: mchunkptr newp = 0; slouken@1895: void *extra = 0; slouken@1895: slouken@1895: /* Try to either shrink or extend into top. Else malloc-copy-free */ slouken@1895: slouken@1895: if (RTCHECK(ok_address(m, oldp) && ok_cinuse(oldp) && slouken@1895: ok_next(oldp, next) && ok_pinuse(next))) { slouken@1895: size_t nb = request2size(bytes); slouken@1895: if (is_mmapped(oldp)) slouken@1895: newp = mmap_resize(m, oldp, nb); slouken@1895: else if (oldsize >= nb) { /* already big enough */ slouken@1895: size_t rsize = oldsize - nb; slouken@1895: newp = oldp; slouken@1895: if (rsize >= MIN_CHUNK_SIZE) { slouken@1895: mchunkptr remainder = chunk_plus_offset(newp, nb); slouken@1895: set_inuse(m, newp, nb); slouken@1895: set_inuse(m, remainder, rsize); slouken@1895: extra = chunk2mem(remainder); slouken@1895: } slouken@1895: } else if (next == m->top && oldsize + m->topsize > nb) { slouken@1895: /* Expand into top */ slouken@1895: size_t newsize = oldsize + m->topsize; slouken@1895: size_t newtopsize = newsize - nb; slouken@1895: mchunkptr newtop = chunk_plus_offset(oldp, nb); slouken@1895: set_inuse(m, oldp, nb); slouken@1895: newtop->head = newtopsize | PINUSE_BIT; slouken@1895: m->top = newtop; slouken@1895: m->topsize = newtopsize; slouken@1895: newp = oldp; slouken@1895: } slouken@1895: } else { slouken@1895: USAGE_ERROR_ACTION(m, oldmem); slouken@1895: POSTACTION(m); slouken@1895: return 0; slouken@1895: } slouken@1895: slouken@1895: POSTACTION(m); slouken@1895: slouken@1895: if (newp != 0) { slouken@1895: if (extra != 0) { slouken@1895: internal_free(m, extra); slouken@1895: } slouken@1895: check_inuse_chunk(m, newp); slouken@1895: return chunk2mem(newp); slouken@1895: } else { slouken@1895: void *newmem = internal_malloc(m, bytes); slouken@1895: if (newmem != 0) { slouken@1895: size_t oc = oldsize - overhead_for(oldp); icculus@2196: memcpy(newmem, oldmem, (oc < bytes) ? oc : bytes); slouken@1895: internal_free(m, oldmem); slouken@1895: } slouken@1895: return newmem; slouken@1895: } slouken@1895: } slouken@1895: return 0; slouken@1895: } slouken@1895: slouken@1895: /* --------------------------- memalign support -------------------------- */ slouken@1895: slouken@1895: static void * slouken@1895: internal_memalign(mstate m, size_t alignment, size_t bytes) slouken@1895: { slouken@1895: if (alignment <= MALLOC_ALIGNMENT) /* Can just use malloc */ slouken@1895: return internal_malloc(m, bytes); slouken@1895: if (alignment < MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */ slouken@1895: alignment = MIN_CHUNK_SIZE; slouken@1895: if ((alignment & (alignment - SIZE_T_ONE)) != 0) { /* Ensure a power of 2 */ slouken@1895: size_t a = MALLOC_ALIGNMENT << 1; slouken@1895: while (a < alignment) slouken@1895: a <<= 1; slouken@1895: alignment = a; slouken@1895: } slouken@1895: slouken@1895: if (bytes >= MAX_REQUEST - alignment) { slouken@1895: if (m != 0) { /* Test isn't needed but avoids compiler warning */ slouken@1895: MALLOC_FAILURE_ACTION; slouken@1895: } slouken@1895: } else { slouken@1895: size_t nb = request2size(bytes); slouken@1895: size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD; slouken@1895: char *mem = (char *) internal_malloc(m, req); slouken@1895: if (mem != 0) { slouken@1895: void *leader = 0; slouken@1895: void *trailer = 0; slouken@1895: mchunkptr p = mem2chunk(mem); slouken@1895: slouken@1895: if (PREACTION(m)) slouken@1895: return 0; slouken@1895: if ((((size_t) (mem)) % alignment) != 0) { /* misaligned */ slouken@1895: /* slouken@1895: Find an aligned spot inside chunk. Since we need to give slouken@1895: back leading space in a chunk of at least MIN_CHUNK_SIZE, if slouken@1895: the first calculation places us at a spot with less than slouken@1895: MIN_CHUNK_SIZE leader, we can move to the next aligned spot. slouken@1895: We've allocated enough total room so that this is always slouken@1895: possible. slouken@1895: */ slouken@2203: char *br = (char *) mem2chunk((size_t) (((size_t) (mem + slouken@2203: alignment - slouken@2203: SIZE_T_ONE)) slouken@2203: & -alignment)); slouken@1895: char *pos = slouken@1895: ((size_t) (br - (char *) (p)) >= slouken@1895: MIN_CHUNK_SIZE) ? br : br + alignment; slouken@1895: mchunkptr newp = (mchunkptr) pos; slouken@1895: size_t leadsize = pos - (char *) (p); slouken@1895: size_t newsize = chunksize(p) - leadsize; slouken@1895: slouken@1895: if (is_mmapped(p)) { /* For mmapped chunks, just adjust offset */ slouken@1895: newp->prev_foot = p->prev_foot + leadsize; slouken@1895: newp->head = (newsize | CINUSE_BIT); slouken@1895: } else { /* Otherwise, give back leader, use the rest */ slouken@1895: set_inuse(m, newp, newsize); slouken@1895: set_inuse(m, p, leadsize); slouken@1895: leader = chunk2mem(p); slouken@1895: } slouken@1895: p = newp; slouken@1895: } slouken@1895: slouken@1895: /* Give back spare room at the end */ slouken@1895: if (!is_mmapped(p)) { slouken@1895: size_t size = chunksize(p); slouken@1895: if (size > nb + MIN_CHUNK_SIZE) { slouken@1895: size_t remainder_size = size - nb; slouken@1895: mchunkptr remainder = chunk_plus_offset(p, nb); slouken@1895: set_inuse(m, p, nb); slouken@1895: set_inuse(m, remainder, remainder_size); slouken@1895: trailer = chunk2mem(remainder); slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: assert(chunksize(p) >= nb); slouken@1895: assert((((size_t) (chunk2mem(p))) % alignment) == 0); slouken@1895: check_inuse_chunk(m, p); slouken@1895: POSTACTION(m); slouken@1895: if (leader != 0) { slouken@1895: internal_free(m, leader); slouken@1895: } slouken@1895: if (trailer != 0) { slouken@1895: internal_free(m, trailer); slouken@1895: } slouken@1895: return chunk2mem(p); slouken@1895: } slouken@1895: } slouken@1895: return 0; slouken@1895: } slouken@1895: slouken@1895: /* ------------------------ comalloc/coalloc support --------------------- */ slouken@1895: slouken@1895: static void ** slouken@1895: ialloc(mstate m, size_t n_elements, size_t * sizes, int opts, void *chunks[]) slouken@1895: { slouken@1895: /* slouken@1895: This provides common support for independent_X routines, handling slouken@1895: all of the combinations that can result. slouken@1895: slouken@1895: The opts arg has: slouken@1895: bit 0 set if all elements are same size (using sizes[0]) slouken@1895: bit 1 set if elements should be zeroed slouken@1895: */ slouken@1895: slouken@1895: size_t element_size; /* chunksize of each element, if all same */ slouken@1895: size_t contents_size; /* total size of elements */ slouken@1895: size_t array_size; /* request size of pointer array */ slouken@1895: void *mem; /* malloced aggregate space */ slouken@1895: mchunkptr p; /* corresponding chunk */ slouken@1895: size_t remainder_size; /* remaining bytes while splitting */ slouken@1895: void **marray; /* either "chunks" or malloced ptr array */ slouken@1895: mchunkptr array_chunk; /* chunk for malloced ptr array */ slouken@1895: flag_t was_enabled; /* to disable mmap */ slouken@1895: size_t size; slouken@1895: size_t i; slouken@1895: slouken@1895: /* compute array length, if needed */ slouken@1895: if (chunks != 0) { slouken@1895: if (n_elements == 0) slouken@1895: return chunks; /* nothing to do */ slouken@1895: marray = chunks; slouken@1895: array_size = 0; slouken@1895: } else { slouken@1895: /* if empty req, must still return chunk representing empty array */ slouken@1895: if (n_elements == 0) slouken@1895: return (void **) internal_malloc(m, 0); slouken@1895: marray = 0; slouken@1895: array_size = request2size(n_elements * (sizeof(void *))); slouken@1895: } slouken@1895: slouken@1895: /* compute total element size */ slouken@1895: if (opts & 0x1) { /* all-same-size */ slouken@1895: element_size = request2size(*sizes); slouken@1895: contents_size = n_elements * element_size; slouken@1895: } else { /* add up all the sizes */ slouken@1895: element_size = 0; slouken@1895: contents_size = 0; slouken@1895: for (i = 0; i != n_elements; ++i) slouken@1895: contents_size += request2size(sizes[i]); slouken@1895: } slouken@1895: slouken@1895: size = contents_size + array_size; slouken@1895: slouken@1895: /* slouken@1895: Allocate the aggregate chunk. First disable direct-mmapping so slouken@1895: malloc won't use it, since we would not be able to later slouken@1895: free/realloc space internal to a segregated mmap region. slouken@1895: */ slouken@1895: was_enabled = use_mmap(m); slouken@1895: disable_mmap(m); slouken@1895: mem = internal_malloc(m, size - CHUNK_OVERHEAD); slouken@1895: if (was_enabled) slouken@1895: enable_mmap(m); slouken@1895: if (mem == 0) slouken@1895: return 0; slouken@1895: slouken@1895: if (PREACTION(m)) slouken@1895: return 0; slouken@1895: p = mem2chunk(mem); slouken@1895: remainder_size = chunksize(p); slouken@1895: slouken@1895: assert(!is_mmapped(p)); slouken@1895: slouken@1895: if (opts & 0x2) { /* optionally clear the elements */ slouken@1895: memset((size_t *) mem, 0, remainder_size - SIZE_T_SIZE - array_size); slouken@1895: } slouken@1895: slouken@1895: /* If not provided, allocate the pointer array as final part of chunk */ slouken@1895: if (marray == 0) { slouken@1895: size_t array_chunk_size; slouken@1895: array_chunk = chunk_plus_offset(p, contents_size); slouken@1895: array_chunk_size = remainder_size - contents_size; slouken@1895: marray = (void **) (chunk2mem(array_chunk)); slouken@1895: set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size); slouken@1895: remainder_size = contents_size; slouken@1895: } slouken@1895: slouken@1895: /* split out elements */ slouken@1895: for (i = 0;; ++i) { slouken@1895: marray[i] = chunk2mem(p); slouken@1895: if (i != n_elements - 1) { slouken@1895: if (element_size != 0) slouken@1895: size = element_size; slouken@1895: else slouken@1895: size = request2size(sizes[i]); slouken@1895: remainder_size -= size; slouken@1895: set_size_and_pinuse_of_inuse_chunk(m, p, size); slouken@1895: p = chunk_plus_offset(p, size); slouken@1895: } else { /* the final element absorbs any overallocation slop */ slouken@1895: set_size_and_pinuse_of_inuse_chunk(m, p, remainder_size); slouken@1895: break; slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: #if DEBUG slouken@1895: if (marray != chunks) { slouken@1895: /* final element must have exactly exhausted chunk */ slouken@1895: if (element_size != 0) { slouken@1895: assert(remainder_size == element_size); slouken@1895: } else { slouken@1895: assert(remainder_size == request2size(sizes[i])); slouken@1895: } slouken@1895: check_inuse_chunk(m, mem2chunk(marray)); slouken@1895: } slouken@1895: for (i = 0; i != n_elements; ++i) slouken@1895: check_inuse_chunk(m, mem2chunk(marray[i])); slouken@1895: slouken@1895: #endif /* DEBUG */ slouken@1895: slouken@1330: POSTACTION(m); slouken@1895: return marray; slouken@1330: } slouken@1330: slouken@1330: slouken@1330: /* -------------------------- public routines ---------------------------- */ slouken@1330: slouken@1330: #if !ONLY_MSPACES slouken@1330: slouken@1895: void * slouken@1895: dlmalloc(size_t bytes) slouken@1895: { slouken@1895: /* slouken@1895: Basic algorithm: slouken@1895: If a small request (< 256 bytes minus per-chunk overhead): slouken@1330: 1. If one exists, use a remainderless chunk in associated smallbin. slouken@1895: (Remainderless means that there are too few excess bytes to slouken@1895: represent as a chunk.) slouken@1330: 2. If it is big enough, use the dv chunk, which is normally the slouken@1895: chunk adjacent to the one used for the most recent small request. slouken@1330: 3. If one exists, split the smallest available chunk in a bin, slouken@1895: saving remainder in dv. slouken@1330: 4. If it is big enough, use the top chunk. slouken@1330: 5. If available, get memory from system and use it slouken@1895: Otherwise, for a large request: slouken@1330: 1. Find the smallest available binned chunk that fits, and use it slouken@1895: if it is better fitting than dv chunk, splitting if necessary. slouken@1330: 2. If better fitting than any binned chunk, use the dv chunk. slouken@1330: 3. If it is big enough, use the top chunk. slouken@1330: 4. If request size >= mmap threshold, try to directly mmap this chunk. slouken@1330: 5. If available, get memory from system and use it slouken@1330: slouken@1895: The ugly goto's here ensure that postaction occurs along all paths. slouken@1895: */ slouken@1895: slouken@1895: if (!PREACTION(gm)) { slouken@1895: void *mem; slouken@1895: size_t nb; slouken@1895: if (bytes <= MAX_SMALL_REQUEST) { slouken@1895: bindex_t idx; slouken@1895: binmap_t smallbits; slouken@1895: nb = (bytes < MIN_REQUEST) ? MIN_CHUNK_SIZE : pad_request(bytes); slouken@1895: idx = small_index(nb); slouken@1895: smallbits = gm->smallmap >> idx; slouken@1895: slouken@1895: if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ slouken@1895: mchunkptr b, p; slouken@1895: idx += ~smallbits & 1; /* Uses next bin if idx empty */ slouken@1895: b = smallbin_at(gm, idx); slouken@1895: p = b->fd; slouken@1895: assert(chunksize(p) == small_index2size(idx)); slouken@1895: unlink_first_small_chunk(gm, b, p, idx); slouken@1895: set_inuse_and_pinuse(gm, p, small_index2size(idx)); slouken@1895: mem = chunk2mem(p); slouken@1895: check_malloced_chunk(gm, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: slouken@1895: else if (nb > gm->dvsize) { slouken@1895: if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ slouken@1895: mchunkptr b, p, r; slouken@1895: size_t rsize; slouken@1895: bindex_t i; slouken@1895: binmap_t leftbits = slouken@1895: (smallbits << idx) & left_bits(idx2bit(idx)); slouken@1895: binmap_t leastbit = least_bit(leftbits); slouken@1895: compute_bit2idx(leastbit, i); slouken@1895: b = smallbin_at(gm, i); slouken@1895: p = b->fd; slouken@1895: assert(chunksize(p) == small_index2size(i)); slouken@1895: unlink_first_small_chunk(gm, b, p, i); slouken@1895: rsize = small_index2size(i) - nb; slouken@1895: /* Fit here cannot be remainderless if 4byte sizes */ slouken@1895: if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) slouken@1895: set_inuse_and_pinuse(gm, p, small_index2size(i)); slouken@1895: else { slouken@1895: set_size_and_pinuse_of_inuse_chunk(gm, p, nb); slouken@1895: r = chunk_plus_offset(p, nb); slouken@1895: set_size_and_pinuse_of_free_chunk(r, rsize); slouken@1895: replace_dv(gm, r, rsize); slouken@1895: } slouken@1895: mem = chunk2mem(p); slouken@1895: check_malloced_chunk(gm, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: slouken@1895: else if (gm->treemap != 0 slouken@1895: && (mem = tmalloc_small(gm, nb)) != 0) { slouken@1895: check_malloced_chunk(gm, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: } slouken@1895: } else if (bytes >= MAX_REQUEST) slouken@1895: nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ slouken@1895: else { slouken@1895: nb = pad_request(bytes); slouken@1895: if (gm->treemap != 0 && (mem = tmalloc_large(gm, nb)) != 0) { slouken@1895: check_malloced_chunk(gm, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: if (nb <= gm->dvsize) { slouken@1895: size_t rsize = gm->dvsize - nb; slouken@1895: mchunkptr p = gm->dv; slouken@1895: if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ slouken@1895: mchunkptr r = gm->dv = chunk_plus_offset(p, nb); slouken@1895: gm->dvsize = rsize; slouken@1895: set_size_and_pinuse_of_free_chunk(r, rsize); slouken@1895: set_size_and_pinuse_of_inuse_chunk(gm, p, nb); slouken@1895: } else { /* exhaust dv */ slouken@1895: size_t dvs = gm->dvsize; slouken@1895: gm->dvsize = 0; slouken@1895: gm->dv = 0; slouken@1895: set_inuse_and_pinuse(gm, p, dvs); slouken@1895: } slouken@1895: mem = chunk2mem(p); slouken@1895: check_malloced_chunk(gm, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: slouken@1895: else if (nb < gm->topsize) { /* Split top */ slouken@1895: size_t rsize = gm->topsize -= nb; slouken@1895: mchunkptr p = gm->top; slouken@1895: mchunkptr r = gm->top = chunk_plus_offset(p, nb); slouken@1895: r->head = rsize | PINUSE_BIT; slouken@1330: set_size_and_pinuse_of_inuse_chunk(gm, p, nb); slouken@1895: mem = chunk2mem(p); slouken@1895: check_top_chunk(gm, gm->top); slouken@1895: check_malloced_chunk(gm, mem, nb); slouken@1895: goto postaction; slouken@1330: } slouken@1330: slouken@1895: mem = sys_alloc(gm, nb); slouken@1895: slouken@1895: postaction: slouken@1895: POSTACTION(gm); slouken@1895: return mem; slouken@1895: } slouken@1895: slouken@1895: return 0; slouken@1895: } slouken@1895: slouken@1895: void slouken@1895: dlfree(void *mem) slouken@1895: { slouken@1895: /* slouken@1895: Consolidate freed chunks with preceeding or succeeding bordering slouken@1895: free chunks, if they exist, and then place in a bin. Intermixed slouken@1895: with special cases for top, dv, mmapped chunks, and usage errors. slouken@1895: */ slouken@1895: slouken@1895: if (mem != 0) { slouken@1895: mchunkptr p = mem2chunk(mem); slouken@1895: #if FOOTERS slouken@1895: mstate fm = get_mstate_for(p); slouken@1895: if (!ok_magic(fm)) { slouken@1895: USAGE_ERROR_ACTION(fm, p); slouken@1895: return; slouken@1330: } slouken@1330: #else /* FOOTERS */ slouken@1330: #define fm gm slouken@1330: #endif /* FOOTERS */ slouken@1895: if (!PREACTION(fm)) { slouken@1895: check_inuse_chunk(fm, p); slouken@1895: if (RTCHECK(ok_address(fm, p) && ok_cinuse(p))) { slouken@1895: size_t psize = chunksize(p); slouken@1895: mchunkptr next = chunk_plus_offset(p, psize); slouken@1895: if (!pinuse(p)) { slouken@1895: size_t prevsize = p->prev_foot; slouken@1895: if ((prevsize & IS_MMAPPED_BIT) != 0) { slouken@1895: prevsize &= ~IS_MMAPPED_BIT; slouken@1895: psize += prevsize + MMAP_FOOT_PAD; slouken@1895: if (CALL_MUNMAP((char *) p - prevsize, psize) == 0) slouken@1895: fm->footprint -= psize; slouken@1895: goto postaction; slouken@1895: } else { slouken@1895: mchunkptr prev = chunk_minus_offset(p, prevsize); slouken@1895: psize += prevsize; slouken@1895: p = prev; slouken@1895: if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */ slouken@1895: if (p != fm->dv) { slouken@1895: unlink_chunk(fm, p, prevsize); slouken@1895: } else if ((next->head & INUSE_BITS) == slouken@1895: INUSE_BITS) { slouken@1895: fm->dvsize = psize; slouken@1895: set_free_with_pinuse(p, psize, next); slouken@1895: goto postaction; slouken@1895: } slouken@1895: } else slouken@1895: goto erroraction; slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { slouken@1895: if (!cinuse(next)) { /* consolidate forward */ slouken@1895: if (next == fm->top) { slouken@1895: size_t tsize = fm->topsize += psize; slouken@1895: fm->top = p; slouken@1895: p->head = tsize | PINUSE_BIT; slouken@1895: if (p == fm->dv) { slouken@1895: fm->dv = 0; slouken@1895: fm->dvsize = 0; slouken@1895: } slouken@1895: if (should_trim(fm, tsize)) slouken@1895: sys_trim(fm, 0); slouken@1895: goto postaction; slouken@1895: } else if (next == fm->dv) { slouken@1895: size_t dsize = fm->dvsize += psize; slouken@1895: fm->dv = p; slouken@1895: set_size_and_pinuse_of_free_chunk(p, dsize); slouken@1895: goto postaction; slouken@1895: } else { slouken@1895: size_t nsize = chunksize(next); slouken@1895: psize += nsize; slouken@1895: unlink_chunk(fm, next, nsize); slouken@1895: set_size_and_pinuse_of_free_chunk(p, psize); slouken@1895: if (p == fm->dv) { slouken@1895: fm->dvsize = psize; slouken@1895: goto postaction; slouken@1895: } slouken@1895: } slouken@1895: } else slouken@1895: set_free_with_pinuse(p, psize, next); slouken@1895: insert_chunk(fm, p, psize); slouken@1895: check_free_chunk(fm, p); slouken@1895: goto postaction; slouken@1895: } slouken@1330: } slouken@1895: erroraction: slouken@1895: USAGE_ERROR_ACTION(fm, p); slouken@1895: postaction: slouken@1895: POSTACTION(fm); slouken@1330: } slouken@1330: } slouken@1330: #if !FOOTERS slouken@1330: #undef fm slouken@1330: #endif /* FOOTERS */ slouken@1330: } slouken@1330: slouken@1895: void * slouken@1895: dlcalloc(size_t n_elements, size_t elem_size) slouken@1895: { slouken@1895: void *mem; slouken@1895: size_t req = 0; slouken@1895: if (n_elements != 0) { slouken@1895: req = n_elements * elem_size; slouken@1895: if (((n_elements | elem_size) & ~(size_t) 0xffff) && slouken@1895: (req / n_elements != elem_size)) slouken@1895: req = MAX_SIZE_T; /* force downstream failure on overflow */ slouken@1895: } slouken@1895: mem = dlmalloc(req); slouken@1895: if (mem != 0 && calloc_must_clear(mem2chunk(mem))) slouken@1895: memset(mem, 0, req); slouken@1895: return mem; slouken@1330: } slouken@1330: slouken@1895: void * slouken@1895: dlrealloc(void *oldmem, size_t bytes) slouken@1895: { slouken@1895: if (oldmem == 0) slouken@1895: return dlmalloc(bytes); slouken@1330: #ifdef REALLOC_ZERO_BYTES_FREES slouken@1895: if (bytes == 0) { slouken@1895: dlfree(oldmem); slouken@1895: return 0; slouken@1895: } slouken@1330: #endif /* REALLOC_ZERO_BYTES_FREES */ slouken@1895: else { slouken@1330: #if ! FOOTERS slouken@1895: mstate m = gm; slouken@1330: #else /* FOOTERS */ slouken@1895: mstate m = get_mstate_for(mem2chunk(oldmem)); slouken@1895: if (!ok_magic(m)) { slouken@1895: USAGE_ERROR_ACTION(m, oldmem); slouken@1895: return 0; slouken@1895: } slouken@1895: #endif /* FOOTERS */ slouken@1895: return internal_realloc(m, oldmem, bytes); slouken@1330: } slouken@1330: } slouken@1330: slouken@1895: void * slouken@1895: dlmemalign(size_t alignment, size_t bytes) slouken@1895: { slouken@1895: return internal_memalign(gm, alignment, bytes); slouken@1330: } slouken@1330: slouken@1895: void ** slouken@1895: dlindependent_calloc(size_t n_elements, size_t elem_size, void *chunks[]) slouken@1895: { slouken@1895: size_t sz = elem_size; /* serves as 1-element array */ slouken@1895: return ialloc(gm, n_elements, &sz, 3, chunks); slouken@1330: } slouken@1330: slouken@1895: void ** slouken@1895: dlindependent_comalloc(size_t n_elements, size_t sizes[], void *chunks[]) slouken@1895: { slouken@1895: return ialloc(gm, n_elements, sizes, 0, chunks); slouken@1330: } slouken@1330: slouken@1895: void * slouken@1895: dlvalloc(size_t bytes) slouken@1895: { slouken@1895: size_t pagesz; slouken@1895: init_mparams(); slouken@1895: pagesz = mparams.page_size; slouken@1895: return dlmemalign(pagesz, bytes); slouken@1330: } slouken@1330: slouken@1895: void * slouken@1895: dlpvalloc(size_t bytes) slouken@1895: { slouken@1895: size_t pagesz; slouken@1895: init_mparams(); slouken@1895: pagesz = mparams.page_size; slouken@1895: return dlmemalign(pagesz, slouken@1895: (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE)); slouken@1330: } slouken@1330: slouken@1895: int slouken@1895: dlmalloc_trim(size_t pad) slouken@1895: { slouken@1895: int result = 0; slouken@1895: if (!PREACTION(gm)) { slouken@1895: result = sys_trim(gm, pad); slouken@1895: POSTACTION(gm); slouken@1895: } slouken@1895: return result; slouken@1330: } slouken@1330: slouken@1895: size_t slouken@1895: dlmalloc_footprint(void) slouken@1895: { slouken@1895: return gm->footprint; slouken@1330: } slouken@1330: slouken@1895: size_t slouken@1895: dlmalloc_max_footprint(void) slouken@1895: { slouken@1895: return gm->max_footprint; slouken@1330: } slouken@1330: slouken@1330: #if !NO_MALLINFO slouken@1895: struct mallinfo slouken@1895: dlmallinfo(void) slouken@1895: { slouken@1895: return internal_mallinfo(gm); slouken@1330: } slouken@1330: #endif /* NO_MALLINFO */ slouken@1330: slouken@1895: void slouken@1895: dlmalloc_stats() slouken@1895: { slouken@1895: internal_malloc_stats(gm); slouken@1330: } slouken@1330: slouken@1895: size_t slouken@1895: dlmalloc_usable_size(void *mem) slouken@1895: { slouken@1895: if (mem != 0) { slouken@1895: mchunkptr p = mem2chunk(mem); slouken@1895: if (cinuse(p)) slouken@1895: return chunksize(p) - overhead_for(p); slouken@1895: } slouken@1895: return 0; slouken@1330: } slouken@1330: slouken@1895: int slouken@1895: dlmallopt(int param_number, int value) slouken@1895: { slouken@1895: return change_mparam(param_number, value); slouken@1330: } slouken@1330: slouken@1330: #endif /* !ONLY_MSPACES */ slouken@1330: slouken@1330: /* ----------------------------- user mspaces ---------------------------- */ slouken@1330: slouken@1330: #if MSPACES slouken@1330: slouken@1895: static mstate slouken@1895: init_user_mstate(char *tbase, size_t tsize) slouken@1895: { slouken@1895: size_t msize = pad_request(sizeof(struct malloc_state)); slouken@1895: mchunkptr mn; slouken@1895: mchunkptr msp = align_as_chunk(tbase); slouken@1895: mstate m = (mstate) (chunk2mem(msp)); slouken@1895: memset(m, 0, msize); slouken@1895: INITIAL_LOCK(&m->mutex); slouken@1895: msp->head = (msize | PINUSE_BIT | CINUSE_BIT); slouken@1895: m->seg.base = m->least_addr = tbase; slouken@1895: m->seg.size = m->footprint = m->max_footprint = tsize; slouken@1895: m->magic = mparams.magic; slouken@1895: m->mflags = mparams.default_mflags; slouken@1895: disable_contiguous(m); slouken@1895: init_bins(m); slouken@1895: mn = next_chunk(mem2chunk(m)); slouken@1895: init_top(m, mn, (size_t) ((tbase + tsize) - (char *) mn) - TOP_FOOT_SIZE); slouken@1895: check_top_chunk(m, m->top); slouken@1895: return m; slouken@1330: } slouken@1330: slouken@1895: mspace slouken@1895: create_mspace(size_t capacity, int locked) slouken@1895: { slouken@1895: mstate m = 0; slouken@1895: size_t msize = pad_request(sizeof(struct malloc_state)); slouken@1895: init_mparams(); /* Ensure pagesize etc initialized */ slouken@1895: slouken@1895: if (capacity < (size_t) - (msize + TOP_FOOT_SIZE + mparams.page_size)) { slouken@1895: size_t rs = ((capacity == 0) ? mparams.granularity : slouken@1895: (capacity + TOP_FOOT_SIZE + msize)); slouken@1895: size_t tsize = granularity_align(rs); slouken@1895: char *tbase = (char *) (CALL_MMAP(tsize)); slouken@1895: if (tbase != CMFAIL) { slouken@1895: m = init_user_mstate(tbase, tsize); slouken@1895: m->seg.sflags = IS_MMAPPED_BIT; slouken@1895: set_lock(m, locked); slouken@1895: } slouken@1330: } slouken@1895: return (mspace) m; slouken@1330: } slouken@1330: slouken@1895: mspace slouken@1895: create_mspace_with_base(void *base, size_t capacity, int locked) slouken@1895: { slouken@1895: mstate m = 0; slouken@1895: size_t msize = pad_request(sizeof(struct malloc_state)); slouken@1895: init_mparams(); /* Ensure pagesize etc initialized */ slouken@1895: slouken@1895: if (capacity > msize + TOP_FOOT_SIZE && slouken@1895: capacity < (size_t) - (msize + TOP_FOOT_SIZE + mparams.page_size)) { slouken@1895: m = init_user_mstate((char *) base, capacity); slouken@1895: m->seg.sflags = EXTERN_BIT; slouken@1895: set_lock(m, locked); slouken@1895: } slouken@1895: return (mspace) m; slouken@1330: } slouken@1330: slouken@1895: size_t slouken@1895: destroy_mspace(mspace msp) slouken@1895: { slouken@1895: size_t freed = 0; slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (ok_magic(ms)) { slouken@1895: msegmentptr sp = &ms->seg; slouken@1895: while (sp != 0) { slouken@1895: char *base = sp->base; slouken@1895: size_t size = sp->size; slouken@1895: flag_t flag = sp->sflags; slouken@1895: sp = sp->next; slouken@1895: if ((flag & IS_MMAPPED_BIT) && !(flag & EXTERN_BIT) && slouken@1895: CALL_MUNMAP(base, size) == 0) slouken@1895: freed += size; slouken@1895: } slouken@1895: } else { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1330: } slouken@1895: return freed; slouken@1330: } slouken@1330: slouken@1330: /* slouken@1330: mspace versions of routines are near-clones of the global slouken@1330: versions. This is not so nice but better than the alternatives. slouken@1330: */ slouken@1330: slouken@1330: slouken@1895: void * slouken@1895: mspace_malloc(mspace msp, size_t bytes) slouken@1895: { slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (!ok_magic(ms)) { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: return 0; slouken@1895: } slouken@1895: if (!PREACTION(ms)) { slouken@1895: void *mem; slouken@1895: size_t nb; slouken@1895: if (bytes <= MAX_SMALL_REQUEST) { slouken@1895: bindex_t idx; slouken@1895: binmap_t smallbits; slouken@1895: nb = (bytes < MIN_REQUEST) ? MIN_CHUNK_SIZE : pad_request(bytes); slouken@1895: idx = small_index(nb); slouken@1895: smallbits = ms->smallmap >> idx; slouken@1895: slouken@1895: if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ slouken@1895: mchunkptr b, p; slouken@1895: idx += ~smallbits & 1; /* Uses next bin if idx empty */ slouken@1895: b = smallbin_at(ms, idx); slouken@1895: p = b->fd; slouken@1895: assert(chunksize(p) == small_index2size(idx)); slouken@1895: unlink_first_small_chunk(ms, b, p, idx); slouken@1895: set_inuse_and_pinuse(ms, p, small_index2size(idx)); slouken@1895: mem = chunk2mem(p); slouken@1895: check_malloced_chunk(ms, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: slouken@1895: else if (nb > ms->dvsize) { slouken@1895: if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ slouken@1895: mchunkptr b, p, r; slouken@1895: size_t rsize; slouken@1895: bindex_t i; slouken@1895: binmap_t leftbits = slouken@1895: (smallbits << idx) & left_bits(idx2bit(idx)); slouken@1895: binmap_t leastbit = least_bit(leftbits); slouken@1895: compute_bit2idx(leastbit, i); slouken@1895: b = smallbin_at(ms, i); slouken@1895: p = b->fd; slouken@1895: assert(chunksize(p) == small_index2size(i)); slouken@1895: unlink_first_small_chunk(ms, b, p, i); slouken@1895: rsize = small_index2size(i) - nb; slouken@1895: /* Fit here cannot be remainderless if 4byte sizes */ slouken@1895: if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) slouken@1895: set_inuse_and_pinuse(ms, p, small_index2size(i)); slouken@1895: else { slouken@1895: set_size_and_pinuse_of_inuse_chunk(ms, p, nb); slouken@1895: r = chunk_plus_offset(p, nb); slouken@1895: set_size_and_pinuse_of_free_chunk(r, rsize); slouken@1895: replace_dv(ms, r, rsize); slouken@1895: } slouken@1895: mem = chunk2mem(p); slouken@1895: check_malloced_chunk(ms, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: slouken@1895: else if (ms->treemap != 0 slouken@1895: && (mem = tmalloc_small(ms, nb)) != 0) { slouken@1895: check_malloced_chunk(ms, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: } slouken@1895: } else if (bytes >= MAX_REQUEST) slouken@1895: nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ slouken@1895: else { slouken@1895: nb = pad_request(bytes); slouken@1895: if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) { slouken@1895: check_malloced_chunk(ms, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: if (nb <= ms->dvsize) { slouken@1895: size_t rsize = ms->dvsize - nb; slouken@1895: mchunkptr p = ms->dv; slouken@1895: if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ slouken@1895: mchunkptr r = ms->dv = chunk_plus_offset(p, nb); slouken@1895: ms->dvsize = rsize; slouken@1895: set_size_and_pinuse_of_free_chunk(r, rsize); slouken@1895: set_size_and_pinuse_of_inuse_chunk(ms, p, nb); slouken@1895: } else { /* exhaust dv */ slouken@1895: size_t dvs = ms->dvsize; slouken@1895: ms->dvsize = 0; slouken@1895: ms->dv = 0; slouken@1895: set_inuse_and_pinuse(ms, p, dvs); slouken@1895: } slouken@1895: mem = chunk2mem(p); slouken@1895: check_malloced_chunk(ms, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: slouken@1895: else if (nb < ms->topsize) { /* Split top */ slouken@1895: size_t rsize = ms->topsize -= nb; slouken@1895: mchunkptr p = ms->top; slouken@1895: mchunkptr r = ms->top = chunk_plus_offset(p, nb); slouken@1895: r->head = rsize | PINUSE_BIT; slouken@1895: set_size_and_pinuse_of_inuse_chunk(ms, p, nb); slouken@1895: mem = chunk2mem(p); slouken@1895: check_top_chunk(ms, ms->top); slouken@1895: check_malloced_chunk(ms, mem, nb); slouken@1895: goto postaction; slouken@1895: } slouken@1895: slouken@1895: mem = sys_alloc(ms, nb); slouken@1895: slouken@1895: postaction: slouken@1895: POSTACTION(ms); slouken@1895: return mem; slouken@1895: } slouken@1895: slouken@1330: return 0; slouken@1895: } slouken@1895: slouken@1895: void slouken@1895: mspace_free(mspace msp, void *mem) slouken@1895: { slouken@1895: if (mem != 0) { slouken@1895: mchunkptr p = mem2chunk(mem); slouken@1895: #if FOOTERS slouken@1895: mstate fm = get_mstate_for(p); slouken@1895: #else /* FOOTERS */ slouken@1895: mstate fm = (mstate) msp; slouken@1895: #endif /* FOOTERS */ slouken@1895: if (!ok_magic(fm)) { slouken@1895: USAGE_ERROR_ACTION(fm, p); slouken@1895: return; slouken@1330: } slouken@1895: if (!PREACTION(fm)) { slouken@1895: check_inuse_chunk(fm, p); slouken@1895: if (RTCHECK(ok_address(fm, p) && ok_cinuse(p))) { slouken@1895: size_t psize = chunksize(p); slouken@1895: mchunkptr next = chunk_plus_offset(p, psize); slouken@1895: if (!pinuse(p)) { slouken@1895: size_t prevsize = p->prev_foot; slouken@1895: if ((prevsize & IS_MMAPPED_BIT) != 0) { slouken@1895: prevsize &= ~IS_MMAPPED_BIT; slouken@1895: psize += prevsize + MMAP_FOOT_PAD; slouken@1895: if (CALL_MUNMAP((char *) p - prevsize, psize) == 0) slouken@1895: fm->footprint -= psize; slouken@1895: goto postaction; slouken@1895: } else { slouken@1895: mchunkptr prev = chunk_minus_offset(p, prevsize); slouken@1895: psize += prevsize; slouken@1895: p = prev; slouken@1895: if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */ slouken@1895: if (p != fm->dv) { slouken@1895: unlink_chunk(fm, p, prevsize); slouken@1895: } else if ((next->head & INUSE_BITS) == slouken@1895: INUSE_BITS) { slouken@1895: fm->dvsize = psize; slouken@1895: set_free_with_pinuse(p, psize, next); slouken@1895: goto postaction; slouken@1895: } slouken@1895: } else slouken@1895: goto erroraction; slouken@1895: } slouken@1895: } slouken@1895: slouken@1895: if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { slouken@1895: if (!cinuse(next)) { /* consolidate forward */ slouken@1895: if (next == fm->top) { slouken@1895: size_t tsize = fm->topsize += psize; slouken@1895: fm->top = p; slouken@1895: p->head = tsize | PINUSE_BIT; slouken@1895: if (p == fm->dv) { slouken@1895: fm->dv = 0; slouken@1895: fm->dvsize = 0; slouken@1895: } slouken@1895: if (should_trim(fm, tsize)) slouken@1895: sys_trim(fm, 0); slouken@1895: goto postaction; slouken@1895: } else if (next == fm->dv) { slouken@1895: size_t dsize = fm->dvsize += psize; slouken@1895: fm->dv = p; slouken@1895: set_size_and_pinuse_of_free_chunk(p, dsize); slouken@1895: goto postaction; slouken@1895: } else { slouken@1895: size_t nsize = chunksize(next); slouken@1895: psize += nsize; slouken@1895: unlink_chunk(fm, next, nsize); slouken@1895: set_size_and_pinuse_of_free_chunk(p, psize); slouken@1895: if (p == fm->dv) { slouken@1895: fm->dvsize = psize; slouken@1895: goto postaction; slouken@1895: } slouken@1895: } slouken@1895: } else slouken@1895: set_free_with_pinuse(p, psize, next); slouken@1895: insert_chunk(fm, p, psize); slouken@1895: check_free_chunk(fm, p); slouken@1895: goto postaction; slouken@1895: } slouken@1895: } slouken@1895: erroraction: slouken@1895: USAGE_ERROR_ACTION(fm, p); slouken@1895: postaction: slouken@1895: POSTACTION(fm); slouken@1330: } slouken@1330: } slouken@1895: } slouken@1895: slouken@1895: void * slouken@1895: mspace_calloc(mspace msp, size_t n_elements, size_t elem_size) slouken@1895: { slouken@1895: void *mem; slouken@1895: size_t req = 0; slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (!ok_magic(ms)) { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: return 0; slouken@1895: } slouken@1895: if (n_elements != 0) { slouken@1895: req = n_elements * elem_size; slouken@1895: if (((n_elements | elem_size) & ~(size_t) 0xffff) && slouken@1895: (req / n_elements != elem_size)) slouken@1895: req = MAX_SIZE_T; /* force downstream failure on overflow */ slouken@1895: } slouken@1895: mem = internal_malloc(ms, req); slouken@1895: if (mem != 0 && calloc_must_clear(mem2chunk(mem))) slouken@1895: memset(mem, 0, req); slouken@1895: return mem; slouken@1895: } slouken@1895: slouken@1895: void * slouken@1895: mspace_realloc(mspace msp, void *oldmem, size_t bytes) slouken@1895: { slouken@1895: if (oldmem == 0) slouken@1895: return mspace_malloc(msp, bytes); slouken@1895: #ifdef REALLOC_ZERO_BYTES_FREES slouken@1895: if (bytes == 0) { slouken@1895: mspace_free(msp, oldmem); slouken@1895: return 0; slouken@1895: } slouken@1895: #endif /* REALLOC_ZERO_BYTES_FREES */ slouken@1330: else { slouken@1895: #if FOOTERS slouken@1895: mchunkptr p = mem2chunk(oldmem); slouken@1895: mstate ms = get_mstate_for(p); slouken@1895: #else /* FOOTERS */ slouken@1895: mstate ms = (mstate) msp; slouken@1895: #endif /* FOOTERS */ slouken@1895: if (!ok_magic(ms)) { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: return 0; slouken@1895: } slouken@1895: return internal_realloc(ms, oldmem, bytes); slouken@1330: } slouken@1895: } slouken@1895: slouken@1895: void * slouken@1895: mspace_memalign(mspace msp, size_t alignment, size_t bytes) slouken@1895: { slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (!ok_magic(ms)) { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: return 0; slouken@1330: } slouken@1895: return internal_memalign(ms, alignment, bytes); slouken@1895: } slouken@1895: slouken@1895: void ** slouken@1895: mspace_independent_calloc(mspace msp, size_t n_elements, slouken@1895: size_t elem_size, void *chunks[]) slouken@1895: { slouken@1895: size_t sz = elem_size; /* serves as 1-element array */ slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (!ok_magic(ms)) { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: return 0; slouken@1330: } slouken@1895: return ialloc(ms, n_elements, &sz, 3, chunks); slouken@1330: } slouken@1330: slouken@1895: void ** slouken@1895: mspace_independent_comalloc(mspace msp, size_t n_elements, slouken@1895: size_t sizes[], void *chunks[]) slouken@1895: { slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (!ok_magic(ms)) { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: return 0; slouken@1330: } slouken@1895: return ialloc(ms, n_elements, sizes, 0, chunks); slouken@1895: } slouken@1895: slouken@1895: int slouken@1895: mspace_trim(mspace msp, size_t pad) slouken@1895: { slouken@1895: int result = 0; slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (ok_magic(ms)) { slouken@1895: if (!PREACTION(ms)) { slouken@1895: result = sys_trim(ms, pad); slouken@1895: POSTACTION(ms); slouken@1330: } slouken@1895: } else { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1330: } slouken@1895: return result; slouken@1330: } slouken@1330: slouken@1895: void slouken@1895: mspace_malloc_stats(mspace msp) slouken@1895: { slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (ok_magic(ms)) { slouken@1895: internal_malloc_stats(ms); slouken@1895: } else { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: } slouken@1330: } slouken@1330: slouken@1895: size_t slouken@1895: mspace_footprint(mspace msp) slouken@1895: { slouken@1895: size_t result; slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (ok_magic(ms)) { slouken@1895: result = ms->footprint; slouken@1895: } slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: return result; slouken@1895: } slouken@1895: slouken@1895: slouken@1895: size_t slouken@1895: mspace_max_footprint(mspace msp) slouken@1895: { slouken@1895: size_t result; slouken@1895: mstate ms = (mstate) msp; slouken@1895: if (ok_magic(ms)) { slouken@1895: result = ms->max_footprint; slouken@1895: } slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1895: return result; slouken@1895: } slouken@1895: slouken@1895: slouken@1895: #if !NO_MALLINFO slouken@1895: struct mallinfo slouken@1895: mspace_mallinfo(mspace msp) slouken@1895: { slouken@1895: mstate ms = (mstate) msp; slouken@1330: if (!ok_magic(ms)) { slouken@1895: USAGE_ERROR_ACTION(ms, ms); slouken@1330: } slouken@1895: return internal_mallinfo(ms); slouken@1330: } slouken@1330: #endif /* NO_MALLINFO */ slouken@1330: slouken@1895: int slouken@1895: mspace_mallopt(int param_number, int value) slouken@1895: { slouken@1895: return change_mparam(param_number, value); slouken@1330: } slouken@1330: slouken@1330: #endif /* MSPACES */ slouken@1330: slouken@1330: /* -------------------- Alternative MORECORE functions ------------------- */ slouken@1330: slouken@1330: /* slouken@1330: Guidelines for creating a custom version of MORECORE: slouken@1330: slouken@1330: * For best performance, MORECORE should allocate in multiples of pagesize. slouken@1330: * MORECORE may allocate more memory than requested. (Or even less, slouken@1330: but this will usually result in a malloc failure.) slouken@1330: * MORECORE must not allocate memory when given argument zero, but slouken@1330: instead return one past the end address of memory from previous slouken@1330: nonzero call. slouken@1330: * For best performance, consecutive calls to MORECORE with positive slouken@1330: arguments should return increasing addresses, indicating that slouken@1330: space has been contiguously extended. slouken@1330: * Even though consecutive calls to MORECORE need not return contiguous slouken@1330: addresses, it must be OK for malloc'ed chunks to span multiple slouken@1330: regions in those cases where they do happen to be contiguous. slouken@1330: * MORECORE need not handle negative arguments -- it may instead slouken@1330: just return MFAIL when given negative arguments. slouken@1330: Negative arguments are always multiples of pagesize. MORECORE slouken@1330: must not misinterpret negative args as large positive unsigned slouken@1330: args. You can suppress all such calls from even occurring by defining slouken@1330: MORECORE_CANNOT_TRIM, slouken@1330: slouken@1330: As an example alternative MORECORE, here is a custom allocator slouken@1330: kindly contributed for pre-OSX macOS. It uses virtually but not slouken@1330: necessarily physically contiguous non-paged memory (locked in, slouken@1330: present and won't get swapped out). You can use it by uncommenting slouken@1330: this section, adding some #includes, and setting up the appropriate slouken@1330: defines above: slouken@1330: slouken@1330: #define MORECORE osMoreCore slouken@1330: slouken@1330: There is also a shutdown routine that should somehow be called for slouken@1330: cleanup upon program exit. slouken@1330: slouken@1330: #define MAX_POOL_ENTRIES 100 slouken@1330: #define MINIMUM_MORECORE_SIZE (64 * 1024U) slouken@1330: static int next_os_pool; slouken@1330: void *our_os_pools[MAX_POOL_ENTRIES]; slouken@1330: slouken@1330: void *osMoreCore(int size) slouken@1330: { slouken@1330: void *ptr = 0; slouken@1330: static void *sbrk_top = 0; slouken@1330: slouken@1330: if (size > 0) slouken@1330: { slouken@1330: if (size < MINIMUM_MORECORE_SIZE) slouken@1330: size = MINIMUM_MORECORE_SIZE; slouken@1330: if (CurrentExecutionLevel() == kTaskLevel) slouken@1330: ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0); slouken@1330: if (ptr == 0) slouken@1330: { slouken@1330: return (void *) MFAIL; slouken@1330: } slouken@1330: // save ptrs so they can be freed during cleanup slouken@1330: our_os_pools[next_os_pool] = ptr; slouken@1330: next_os_pool++; slouken@1330: ptr = (void *) ((((size_t) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK); slouken@1330: sbrk_top = (char *) ptr + size; slouken@1330: return ptr; slouken@1330: } slouken@1330: else if (size < 0) slouken@1330: { slouken@1330: // we don't currently support shrink behavior slouken@1330: return (void *) MFAIL; slouken@1330: } slouken@1330: else slouken@1330: { slouken@1330: return sbrk_top; slouken@1330: } slouken@1330: } slouken@1330: slouken@1330: // cleanup any allocated memory pools slouken@1330: // called as last thing before shutting down driver slouken@1330: slouken@1330: void osCleanupMem(void) slouken@1330: { slouken@1330: void **ptr; slouken@1330: slouken@1330: for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++) slouken@1330: if (*ptr) slouken@1330: { slouken@1330: PoolDeallocate(*ptr); slouken@1330: *ptr = 0; slouken@1330: } slouken@1330: } slouken@1330: slouken@1330: */ slouken@1330: slouken@1330: slouken@1330: /* ----------------------------------------------------------------------- slouken@1330: History: slouken@1330: V2.8.3 Thu Sep 22 11:16:32 2005 Doug Lea (dl at gee) slouken@1330: * Add max_footprint functions slouken@1330: * Ensure all appropriate literals are size_t slouken@1330: * Fix conditional compilation problem for some #define settings slouken@1330: * Avoid concatenating segments with the one provided slouken@1330: in create_mspace_with_base slouken@1330: * Rename some variables to avoid compiler shadowing warnings slouken@1330: * Use explicit lock initialization. slouken@1330: * Better handling of sbrk interference. slouken@1330: * Simplify and fix segment insertion, trimming and mspace_destroy slouken@1330: * Reinstate REALLOC_ZERO_BYTES_FREES option from 2.7.x slouken@1330: * Thanks especially to Dennis Flanagan for help on these. slouken@1330: slouken@1330: V2.8.2 Sun Jun 12 16:01:10 2005 Doug Lea (dl at gee) slouken@1330: * Fix memalign brace error. slouken@1330: slouken@1330: V2.8.1 Wed Jun 8 16:11:46 2005 Doug Lea (dl at gee) slouken@1330: * Fix improper #endif nesting in C++ slouken@1330: * Add explicit casts needed for C++ slouken@1330: slouken@1330: V2.8.0 Mon May 30 14:09:02 2005 Doug Lea (dl at gee) slouken@1330: * Use trees for large bins slouken@1330: * Support mspaces slouken@1330: * Use segments to unify sbrk-based and mmap-based system allocation, slouken@1330: removing need for emulation on most platforms without sbrk. slouken@1330: * Default safety checks slouken@1330: * Optional footer checks. Thanks to William Robertson for the idea. slouken@1330: * Internal code refactoring slouken@1330: * Incorporate suggestions and platform-specific changes. slouken@1330: Thanks to Dennis Flanagan, Colin Plumb, Niall Douglas, slouken@1330: Aaron Bachmann, Emery Berger, and others. slouken@1330: * Speed up non-fastbin processing enough to remove fastbins. slouken@1330: * Remove useless cfree() to avoid conflicts with other apps. slouken@1330: * Remove internal memcpy, memset. Compilers handle builtins better. slouken@1330: * Remove some options that no one ever used and rename others. slouken@1330: slouken@1330: V2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee) slouken@1330: * Fix malloc_state bitmap array misdeclaration slouken@1330: slouken@1330: V2.7.1 Thu Jul 25 10:58:03 2002 Doug Lea (dl at gee) slouken@1330: * Allow tuning of FIRST_SORTED_BIN_SIZE slouken@1330: * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte. slouken@1330: * Better detection and support for non-contiguousness of MORECORE. slouken@1330: Thanks to Andreas Mueller, Conal Walsh, and Wolfram Gloger slouken@1330: * Bypass most of malloc if no frees. Thanks To Emery Berger. slouken@1330: * Fix freeing of old top non-contiguous chunk im sysmalloc. slouken@1330: * Raised default trim and map thresholds to 256K. slouken@1330: * Fix mmap-related #defines. Thanks to Lubos Lunak. slouken@1330: * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield. slouken@1330: * Branch-free bin calculation slouken@1330: * Default trim and mmap thresholds now 256K. slouken@1330: slouken@1330: V2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee) slouken@1330: * Introduce independent_comalloc and independent_calloc. slouken@1330: Thanks to Michael Pachos for motivation and help. slouken@1330: * Make optional .h file available slouken@1330: * Allow > 2GB requests on 32bit systems. slouken@1330: * new WIN32 sbrk, mmap, munmap, lock code from . slouken@1330: Thanks also to Andreas Mueller , slouken@1330: and Anonymous. slouken@1330: * Allow override of MALLOC_ALIGNMENT (Thanks to Ruud Waij for slouken@1330: helping test this.) slouken@1330: * memalign: check alignment arg slouken@1330: * realloc: don't try to shift chunks backwards, since this slouken@1330: leads to more fragmentation in some programs and doesn't slouken@1330: seem to help in any others. slouken@1330: * Collect all cases in malloc requiring system memory into sysmalloc slouken@1330: * Use mmap as backup to sbrk slouken@1330: * Place all internal state in malloc_state slouken@1330: * Introduce fastbins (although similar to 2.5.1) slouken@1330: * Many minor tunings and cosmetic improvements slouken@1330: * Introduce USE_PUBLIC_MALLOC_WRAPPERS, USE_MALLOC_LOCK slouken@1330: * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS slouken@1330: Thanks to Tony E. Bennett and others. slouken@1330: * Include errno.h to support default failure action. slouken@1330: slouken@1330: V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee) slouken@1330: * return null for negative arguments slouken@1330: * Added Several WIN32 cleanups from Martin C. Fong slouken@1330: * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h' slouken@1330: (e.g. WIN32 platforms) slouken@1330: * Cleanup header file inclusion for WIN32 platforms slouken@1330: * Cleanup code to avoid Microsoft Visual C++ compiler complaints slouken@1330: * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing slouken@1330: memory allocation routines slouken@1330: * Set 'malloc_getpagesize' for WIN32 platforms (needs more work) slouken@1330: * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to slouken@1330: usage of 'assert' in non-WIN32 code slouken@1330: * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to slouken@1330: avoid infinite loop slouken@1330: * Always call 'fREe()' rather than 'free()' slouken@1330: slouken@1330: V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee) slouken@1330: * Fixed ordering problem with boundary-stamping slouken@1330: slouken@1330: V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee) slouken@1330: * Added pvalloc, as recommended by H.J. Liu slouken@1330: * Added 64bit pointer support mainly from Wolfram Gloger slouken@1330: * Added anonymously donated WIN32 sbrk emulation slouken@1330: * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen slouken@1330: * malloc_extend_top: fix mask error that caused wastage after slouken@1330: foreign sbrks slouken@1330: * Add linux mremap support code from HJ Liu slouken@1330: slouken@1330: V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee) slouken@1330: * Integrated most documentation with the code. slouken@1330: * Add support for mmap, with help from slouken@1330: Wolfram Gloger (Gloger@lrz.uni-muenchen.de). slouken@1330: * Use last_remainder in more cases. slouken@1330: * Pack bins using idea from colin@nyx10.cs.du.edu slouken@1330: * Use ordered bins instead of best-fit threshhold slouken@1330: * Eliminate block-local decls to simplify tracing and debugging. slouken@1330: * Support another case of realloc via move into top slouken@1330: * Fix error occuring when initial sbrk_base not word-aligned. slouken@1330: * Rely on page size for units instead of SBRK_UNIT to slouken@1330: avoid surprises about sbrk alignment conventions. slouken@1330: * Add mallinfo, mallopt. Thanks to Raymond Nijssen slouken@1330: (raymond@es.ele.tue.nl) for the suggestion. slouken@1330: * Add `pad' argument to malloc_trim and top_pad mallopt parameter. slouken@1330: * More precautions for cases where other routines call sbrk, slouken@1330: courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de). slouken@1330: * Added macros etc., allowing use in linux libc from slouken@1330: H.J. Lu (hjl@gnu.ai.mit.edu) slouken@1330: * Inverted this history list slouken@1330: slouken@1330: V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee) slouken@1330: * Re-tuned and fixed to behave more nicely with V2.6.0 changes. slouken@1330: * Removed all preallocation code since under current scheme slouken@1330: the work required to undo bad preallocations exceeds slouken@1330: the work saved in good cases for most test programs. slouken@1330: * No longer use return list or unconsolidated bins since slouken@1330: no scheme using them consistently outperforms those that don't slouken@1330: given above changes. slouken@1330: * Use best fit for very large chunks to prevent some worst-cases. slouken@1330: * Added some support for debugging slouken@1330: slouken@1330: V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee) slouken@1330: * Removed footers when chunks are in use. Thanks to slouken@1330: Paul Wilson (wilson@cs.texas.edu) for the suggestion. slouken@1330: slouken@1330: V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee) slouken@1330: * Added malloc_trim, with help from Wolfram Gloger slouken@1330: (wmglo@Dent.MED.Uni-Muenchen.DE). slouken@1330: slouken@1330: V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g) slouken@1330: slouken@1330: V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g) slouken@1330: * realloc: try to expand in both directions slouken@1330: * malloc: swap order of clean-bin strategy; slouken@1330: * realloc: only conditionally expand backwards slouken@1330: * Try not to scavenge used bins slouken@1330: * Use bin counts as a guide to preallocation slouken@1330: * Occasionally bin return list chunks in first scan slouken@1330: * Add a few optimizations from colin@nyx10.cs.du.edu slouken@1330: slouken@1330: V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g) slouken@1330: * faster bin computation & slightly different binning slouken@1330: * merged all consolidations to one part of malloc proper slouken@1330: (eliminating old malloc_find_space & malloc_clean_bin) slouken@1330: * Scan 2 returns chunks (not just 1) slouken@1330: * Propagate failure in realloc if malloc returns 0 slouken@1330: * Add stuff to allow compilation on non-ANSI compilers slouken@1330: from kpv@research.att.com slouken@1330: slouken@1330: V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu) slouken@1330: * removed potential for odd address access in prev_chunk slouken@1330: * removed dependency on getpagesize.h slouken@1330: * misc cosmetics and a bit more internal documentation slouken@1330: * anticosmetics: mangled names in macros to evade debugger strangeness slouken@1330: * tested on sparc, hp-700, dec-mips, rs6000 slouken@1330: with gcc & native cc (hp, dec only) allowing slouken@1330: Detlefs & Zorn comparison study (in SIGPLAN Notices.) slouken@1330: slouken@1330: Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu) slouken@1330: * Based loosely on libg++-1.2X malloc. (It retains some of the overall slouken@1330: structure of old version, but most details differ.) slouken@7191: slouken@1330: */ slouken@1330: slouken@1333: #endif /* !HAVE_MALLOC */ slouken@2203: slouken@1895: /* vi: set ts=4 sw=4 expandtab: */