00001 /* 00002 Off-the-shelf glibc 2.3.2 malloc routine: useful because it's 00003 - Portable (should compile everywhere) 00004 - Compact (uses mmap for big buffers, can shift back sbrk) 00005 - Self-contained (this file is it) 00006 00007 This is slightly hacked as: 00008 - Use mm_* routine names, as defined below. 00009 - Maps charm config. macros to local macros, below. 00010 - Renamed malloc.h memory-gnu.h 00011 - Renamed threads-m.h memory-gnu-threads.h 00012 - Removed error-prone "memcpy" prototypes (use system header). 00013 */ 00014 00015 #define malloc mm_malloc 00016 #define free mm_free 00017 #define calloc mm_calloc 00018 #define cfree mm_cfree 00019 #define realloc mm_realloc 00020 #define memalign mm_memalign 00021 #define valloc mm_valloc 00022 00023 extern CMK_TYPEDEF_UINT8 memory_allocated; 00024 extern CMK_TYPEDEF_UINT8 memory_allocated_max; 00025 extern CMK_TYPEDEF_UINT8 memory_allocated_min; 00026 00027 #define HAVE_MMAP CMK_HAS_MMAP 00028 #ifndef __USE_GNU 00029 # define __USE_GNU 1 /* enables MREMAP_MAYMOVE in linux mman.h (!) */ 00030 #endif 00031 00032 #define attribute_hidden /* empty, needed by memory-gnu.h */ 00033 00034 #if 0 /* fixes from 2.2.5 version (now obsolete) */ 00035 00036 /* Crashing bug caused by circular dependence: 00037 calloc calls arena_get2 calls pthread_setspecific calls calloc... 00038 #ifdef CMK_SHARED_VARS_POSIX_THREADS_SMP 00039 # define MALLOC_HOOKS 00040 # define USE_PTHREADS 00041 #endif 00042 */ 00043 #define RETURN_ADDRESS(x) (void *)x 00044 00045 #endif 00046 00047 /* Malloc implementation for multiple threads without lock contention. 00048 Copyright (C) 1996,1997,1998,1999,2000,01,02 Free Software Foundation, Inc. 00049 This file is part of the GNU C Library. 00050 Contributed by Wolfram Gloger <wg@malloc.de> 00051 and Doug Lea <dl@cs.oswego.edu>, 2001. 00052 00053 The GNU C Library is free software; you can redistribute it and/or 00054 modify it under the terms of the GNU Lesser General Public License as 00055 published by the Free Software Foundation; either version 2.1 of the 00056 License, or (at your option) any later version. 00057 00058 The GNU C Library is distributed in the hope that it will be useful, 00059 but WITHOUT ANY WARRANTY; without even the implied warranty of 00060 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00061 Lesser General Public License for more details. 00062 00063 You should have received a copy of the GNU Lesser General Public 00064 License along with the GNU C Library; see the file COPYING.LIB. If not, 00065 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00066 Boston, MA 02111-1307, USA. */ 00067 00068 /* 00069 This is a version (aka ptmalloc2) of malloc/free/realloc written by 00070 Doug Lea and adapted to multiple threads/arenas by Wolfram Gloger. 00071 00072 * Version ptmalloc2-20011215 00073 $Id: memory-gnu.c,v 2.21 2006-04-21 21:02:51 yanshi Exp $ 00074 based on: 00075 VERSION 2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee) 00076 00077 Note: There may be an updated version of this malloc obtainable at 00078 http://www.malloc.de/malloc/ptmalloc2.tar.gz 00079 Check before installing! 00080 00081 * Quickstart 00082 00083 In order to compile this implementation, a Makefile is provided with 00084 the ptmalloc2 distribution, which has pre-defined targets for some 00085 popular systems (e.g. "make posix" for Posix threads). All that is 00086 typically required with regard to compiler flags is the selection of 00087 the thread package via defining one out of USE_PTHREADS, USE_THR or 00088 USE_SPROC. Check the thread-m.h file for what effects this has. 00089 Many/most systems will additionally require USE_TSD_DATA_HACK to be 00090 defined, so this is the default for "make posix". 00091 00092 * Why use this malloc? 00093 00094 This is not the fastest, most space-conserving, most portable, or 00095 most tunable malloc ever written. However it is among the fastest 00096 while also being among the most space-conserving, portable and tunable. 00097 Consistent balance across these factors results in a good general-purpose 00098 allocator for malloc-intensive programs. 00099 00100 The main properties of the algorithms are: 00101 * For large (>= 512 bytes) requests, it is a pure best-fit allocator, 00102 with ties normally decided via FIFO (i.e. least recently used). 00103 * For small (<= 64 bytes by default) requests, it is a caching 00104 allocator, that maintains pools of quickly recycled chunks. 00105 * In between, and for combinations of large and small requests, it does 00106 the best it can trying to meet both goals at once. 00107 * For very large requests (>= 128KB by default), it relies on system 00108 memory mapping facilities, if supported. 00109 00110 For a longer but slightly out of date high-level description, see 00111 http://gee.cs.oswego.edu/dl/html/malloc.html 00112 00113 You may already by default be using a C library containing a malloc 00114 that is based on some version of this malloc (for example in 00115 linux). You might still want to use the one in this file in order to 00116 customize settings or to avoid overheads associated with library 00117 versions. 00118 00119 * Contents, described in more detail in "description of public routines" below. 00120 00121 Standard (ANSI/SVID/...) functions: 00122 malloc(size_t n); 00123 calloc(size_t n_elements, size_t element_size); 00124 free(Void_t* p); 00125 realloc(Void_t* p, size_t n); 00126 memalign(size_t alignment, size_t n); 00127 valloc(size_t n); 00128 mallinfo() 00129 mallopt(int parameter_number, int parameter_value) 00130 00131 Additional functions: 00132 independent_calloc(size_t n_elements, size_t size, Void_t* chunks[]); 00133 independent_comalloc(size_t n_elements, size_t sizes[], Void_t* chunks[]); 00134 pvalloc(size_t n); 00135 cfree(Void_t* p); 00136 malloc_trim(size_t pad); 00137 malloc_usable_size(Void_t* p); 00138 malloc_stats(); 00139 00140 * Vital statistics: 00141 00142 Supported pointer representation: 4 or 8 bytes 00143 Supported size_t representation: 4 or 8 bytes 00144 Note that size_t is allowed to be 4 bytes even if pointers are 8. 00145 You can adjust this by defining INTERNAL_SIZE_T 00146 00147 Alignment: 2 * sizeof(size_t) (default) 00148 (i.e., 8 byte alignment with 4byte size_t). This suffices for 00149 nearly all current machines and C compilers. However, you can 00150 define MALLOC_ALIGNMENT to be wider than this if necessary. 00151 00152 Minimum overhead per allocated chunk: 4 or 8 bytes 00153 Each malloced chunk has a hidden word of overhead holding size 00154 and status information. 00155 00156 Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead) 00157 8-byte ptrs: 24/32 bytes (including, 4/8 overhead) 00158 00159 When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte 00160 ptrs but 4 byte size) or 24 (for 8/8) additional bytes are 00161 needed; 4 (8) for a trailing size field and 8 (16) bytes for 00162 free list pointers. Thus, the minimum allocatable size is 00163 16/24/32 bytes. 00164 00165 Even a request for zero bytes (i.e., malloc(0)) returns a 00166 pointer to something of the minimum allocatable size. 00167 00168 The maximum overhead wastage (i.e., number of extra bytes 00169 allocated than were requested in malloc) is less than or equal 00170 to the minimum size, except for requests >= mmap_threshold that 00171 are serviced via mmap(), where the worst case wastage is 2 * 00172 sizeof(size_t) bytes plus the remainder from a system page (the 00173 minimal mmap unit); typically 4096 or 8192 bytes. 00174 00175 Maximum allocated size: 4-byte size_t: 2^32 minus about two pages 00176 8-byte size_t: 2^64 minus about two pages 00177 00178 It is assumed that (possibly signed) size_t values suffice to 00179 represent chunk sizes. `Possibly signed' is due to the fact 00180 that `size_t' may be defined on a system as either a signed or 00181 an unsigned type. The ISO C standard says that it must be 00182 unsigned, but a few systems are known not to adhere to this. 00183 Additionally, even when size_t is unsigned, sbrk (which is by 00184 default used to obtain memory from system) accepts signed 00185 arguments, and may not be able to handle size_t-wide arguments 00186 with negative sign bit. Generally, values that would 00187 appear as negative after accounting for overhead and alignment 00188 are supported only via mmap(), which does not have this 00189 limitation. 00190 00191 Requests for sizes outside the allowed range will perform an optional 00192 failure action and then return null. (Requests may also 00193 also fail because a system is out of memory.) 00194 00195 Thread-safety: thread-safe unless NO_THREADS is defined 00196 00197 Compliance: I believe it is compliant with the 1997 Single Unix Specification 00198 (See http://www.opennc.org). Also SVID/XPG, ANSI C, and probably 00199 others as well. 00200 00201 * Synopsis of compile-time options: 00202 00203 People have reported using previous versions of this malloc on all 00204 versions of Unix, sometimes by tweaking some of the defines 00205 below. It has been tested most extensively on Solaris and 00206 Linux. It is also reported to work on WIN32 platforms. 00207 People also report using it in stand-alone embedded systems. 00208 00209 The implementation is in straight, hand-tuned ANSI C. It is not 00210 at all modular. (Sorry!) It uses a lot of macros. To be at all 00211 usable, this code should be compiled using an optimizing compiler 00212 (for example gcc -O3) that can simplify expressions and control 00213 paths. (FAQ: some macros import variables as arguments rather than 00214 declare locals because people reported that some debuggers 00215 otherwise get confused.) 00216 00217 OPTION DEFAULT VALUE 00218 00219 Compilation Environment options: 00220 00221 __STD_C derived from C compiler defines 00222 WIN32 NOT defined 00223 HAVE_MEMCPY defined 00224 USE_MEMCPY 1 if HAVE_MEMCPY is defined 00225 HAVE_MMAP defined as 1 00226 MMAP_CLEARS 1 00227 HAVE_MREMAP 0 unless linux defined 00228 USE_ARENAS the same as HAVE_MMAP 00229 malloc_getpagesize derived from system #includes, or 4096 if not 00230 HAVE_USR_INCLUDE_MALLOC_H NOT defined 00231 LACKS_UNISTD_H NOT defined unless WIN32 00232 LACKS_SYS_PARAM_H NOT defined unless WIN32 00233 LACKS_SYS_MMAN_H NOT defined unless WIN32 00234 00235 Changing default word sizes: 00236 00237 INTERNAL_SIZE_T size_t 00238 MALLOC_ALIGNMENT 2 * sizeof(INTERNAL_SIZE_T) 00239 00240 Configuration and functionality options: 00241 00242 USE_DL_PREFIX NOT defined 00243 USE_PUBLIC_MALLOC_WRAPPERS NOT defined 00244 USE_MALLOC_LOCK NOT defined 00245 MALLOC_DEBUG NOT defined 00246 REALLOC_ZERO_BYTES_FREES 1 00247 MALLOC_FAILURE_ACTION errno = ENOMEM, if __STD_C defined, else no-op 00248 TRIM_FASTBINS 0 00249 00250 Options for customizing MORECORE: 00251 00252 MORECORE sbrk 00253 MORECORE_FAILURE -1 00254 MORECORE_CONTIGUOUS 1 00255 MORECORE_CANNOT_TRIM NOT defined 00256 MORECORE_CLEARS 1 00257 MMAP_AS_MORECORE_SIZE (1024 * 1024) 00258 00259 Tuning options that are also dynamically changeable via mallopt: 00260 00261 DEFAULT_MXFAST 64 00262 DEFAULT_TRIM_THRESHOLD 128 * 1024 00263 DEFAULT_TOP_PAD 0 00264 DEFAULT_MMAP_THRESHOLD 128 * 1024 00265 DEFAULT_MMAP_MAX 65536 00266 00267 There are several other #defined constants and macros that you 00268 probably don't want to touch unless you are extending or adapting malloc. */ 00269 00270 /* 00271 __STD_C should be nonzero if using ANSI-standard C compiler, a C++ 00272 compiler, or a C compiler sufficiently close to ANSI to get away 00273 with it. 00274 */ 00275 00276 #ifndef __STD_C 00277 #if defined(__STDC__) || defined(__cplusplus) 00278 #define __STD_C 1 00279 #else 00280 #define __STD_C 0 00281 #endif 00282 #endif /*__STD_C*/ 00283 00284 #if __STD_C && !__STDC__ 00285 #define __STDC__ 1 00286 #endif 00287 00288 /* 00289 Void_t* is the pointer type that malloc should say it returns 00290 */ 00291 00292 #ifndef Void_t 00293 #if (__STD_C || defined(WIN32)) 00294 #define Void_t void 00295 #else 00296 #define Void_t char 00297 #endif 00298 #endif /*Void_t*/ 00299 00300 #if __STD_C 00301 #include <stddef.h> /* for size_t */ 00302 #include <stdlib.h> /* for getenv(), abort() */ 00303 #else 00304 #include <sys/types.h> 00305 #endif 00306 00307 #ifdef __cplusplus 00308 extern "C" { 00309 #endif 00310 00311 /* define LACKS_UNISTD_H if your system does not have a <unistd.h>. */ 00312 00313 /* #define LACKS_UNISTD_H */ 00314 00315 #ifndef LACKS_UNISTD_H 00316 #include <unistd.h> 00317 #endif 00318 00319 /* define LACKS_SYS_PARAM_H if your system does not have a <sys/param.h>. */ 00320 00321 /* #define LACKS_SYS_PARAM_H */ 00322 00323 00324 #include <stdio.h> /* needed for malloc_stats */ 00325 #include <errno.h> /* needed for optional MALLOC_FAILURE_ACTION */ 00326 00327 00328 /* 00329 Debugging: 00330 00331 Because freed chunks may be overwritten with bookkeeping fields, this 00332 malloc will often die when freed memory is overwritten by user 00333 programs. This can be very effective (albeit in an annoying way) 00334 in helping track down dangling pointers. 00335 00336 If you compile with -DMALLOC_DEBUG, a number of assertion checks are 00337 enabled that will catch more memory errors. You probably won't be 00338 able to make much sense of the actual assertion errors, but they 00339 should help you locate incorrectly overwritten memory. The checking 00340 is fairly extensive, and will slow down execution 00341 noticeably. Calling malloc_stats or mallinfo with MALLOC_DEBUG set 00342 will attempt to check every non-mmapped allocated and free chunk in 00343 the course of computing the summmaries. (By nature, mmapped regions 00344 cannot be checked very much automatically.) 00345 00346 Setting MALLOC_DEBUG may also be helpful if you are trying to modify 00347 this code. The assertions in the check routines spell out in more 00348 detail the assumptions and invariants underlying the algorithms. 00349 00350 Setting MALLOC_DEBUG does NOT provide an automated mechanism for 00351 checking that all accesses to malloced memory stay within their 00352 bounds. However, there are several add-ons and adaptations of this 00353 or other mallocs available that do this. 00354 */ 00355 00356 #if MALLOC_DEBUG 00357 #include <assert.h> 00358 #else 00359 #undef assert 00360 #define assert(x) ((void)0) 00361 #endif 00362 00363 00364 /* 00365 INTERNAL_SIZE_T is the word-size used for internal bookkeeping 00366 of chunk sizes. 00367 00368 The default version is the same as size_t. 00369 00370 While not strictly necessary, it is best to define this as an 00371 unsigned type, even if size_t is a signed type. This may avoid some 00372 artificial size limitations on some systems. 00373 00374 On a 64-bit machine, you may be able to reduce malloc overhead by 00375 defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the 00376 expense of not being able to handle more than 2^32 of malloced 00377 space. If this limitation is acceptable, you are encouraged to set 00378 this unless you are on a platform requiring 16byte alignments. In 00379 this case the alignment requirements turn out to negate any 00380 potential advantages of decreasing size_t word size. 00381 00382 Implementors: Beware of the possible combinations of: 00383 - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits, 00384 and might be the same width as int or as long 00385 - size_t might have different width and signedness as INTERNAL_SIZE_T 00386 - int and long might be 32 or 64 bits, and might be the same width 00387 To deal with this, most comparisons and difference computations 00388 among INTERNAL_SIZE_Ts should cast them to unsigned long, being 00389 aware of the fact that casting an unsigned int to a wider long does 00390 not sign-extend. (This also makes checking for negative numbers 00391 awkward.) Some of these casts result in harmless compiler warnings 00392 on some systems. 00393 */ 00394 00395 #ifndef INTERNAL_SIZE_T 00396 #define INTERNAL_SIZE_T size_t 00397 #endif 00398 00399 /* The corresponding word size */ 00400 #define SIZE_SZ (sizeof(INTERNAL_SIZE_T)) 00401 00402 00403 /* 00404 MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks. 00405 It must be a power of two at least 2 * SIZE_SZ, even on machines 00406 for which smaller alignments would suffice. It may be defined as 00407 larger than this though. Note however that code and data structures 00408 are optimized for the case of 8-byte alignment. 00409 */ 00410 00411 00412 #ifndef MALLOC_ALIGNMENT 00413 #define MALLOC_ALIGNMENT (4 * SIZE_SZ) 00414 #endif 00415 00416 /* The corresponding bit mask value */ 00417 #define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1) 00418 00419 00420 00421 /* 00422 REALLOC_ZERO_BYTES_FREES should be set if a call to 00423 realloc with zero bytes should be the same as a call to free. 00424 This is required by the C standard. Otherwise, since this malloc 00425 returns a unique pointer for malloc(0), so does realloc(p, 0). 00426 */ 00427 00428 #ifndef REALLOC_ZERO_BYTES_FREES 00429 #define REALLOC_ZERO_BYTES_FREES 1 00430 #endif 00431 00432 /* 00433 TRIM_FASTBINS controls whether free() of a very small chunk can 00434 immediately lead to trimming. Setting to true (1) can reduce memory 00435 footprint, but will almost always slow down programs that use a lot 00436 of small chunks. 00437 00438 Define this only if you are willing to give up some speed to more 00439 aggressively reduce system-level memory footprint when releasing 00440 memory in programs that use many small chunks. You can get 00441 essentially the same effect by setting MXFAST to 0, but this can 00442 lead to even greater slowdowns in programs using many small chunks. 00443 TRIM_FASTBINS is an in-between compile-time option, that disables 00444 only those chunks bordering topmost memory from being placed in 00445 fastbins. 00446 */ 00447 00448 #ifndef TRIM_FASTBINS 00449 #define TRIM_FASTBINS 0 00450 #endif 00451 00452 00453 /* 00454 USE_DL_PREFIX will prefix all public routines with the string 'dl'. 00455 This is necessary when you only want to use this malloc in one part 00456 of a program, using your regular system malloc elsewhere. 00457 */ 00458 00459 /* #define USE_DL_PREFIX */ 00460 00461 00462 /* 00463 Two-phase name translation. 00464 All of the actual routines are given mangled names. 00465 When wrappers are used, they become the public callable versions. 00466 When DL_PREFIX is used, the callable names are prefixed. 00467 */ 00468 00469 #ifdef USE_DL_PREFIX 00470 #define public_cALLOc dlcalloc 00471 #define public_fREe dlfree 00472 #define public_cFREe dlcfree 00473 #define public_mALLOc dlmalloc 00474 #define public_mEMALIGn dlmemalign 00475 #define public_rEALLOc dlrealloc 00476 #define public_vALLOc dlvalloc 00477 #define public_pVALLOc dlpvalloc 00478 #define public_mALLINFo dlmallinfo 00479 #define public_mALLOPt dlmallopt 00480 #define public_mTRIm dlmalloc_trim 00481 #define public_mSTATs dlmalloc_stats 00482 #define public_mUSABLe dlmalloc_usable_size 00483 #define public_iCALLOc dlindependent_calloc 00484 #define public_iCOMALLOc dlindependent_comalloc 00485 #define public_gET_STATe dlget_state 00486 #define public_sET_STATe dlset_state 00487 #else /* USE_DL_PREFIX */ 00488 #ifdef _LIBC 00489 00490 /* Special defines for the GNU C library. */ 00491 #define public_cALLOc __libc_calloc 00492 #define public_fREe __libc_free 00493 #define public_cFREe __libc_cfree 00494 #define public_mALLOc __libc_malloc 00495 #define public_mEMALIGn __libc_memalign 00496 #define public_rEALLOc __libc_realloc 00497 #define public_vALLOc __libc_valloc 00498 #define public_pVALLOc __libc_pvalloc 00499 #define public_mALLINFo __libc_mallinfo 00500 #define public_mALLOPt __libc_mallopt 00501 #define public_mTRIm __malloc_trim 00502 #define public_mSTATs __malloc_stats 00503 #define public_mUSABLe __malloc_usable_size 00504 #define public_iCALLOc __libc_independent_calloc 00505 #define public_iCOMALLOc __libc_independent_comalloc 00506 #define public_gET_STATe __malloc_get_state 00507 #define public_sET_STATe __malloc_set_state 00508 #define malloc_getpagesize __getpagesize() 00509 #define open __open 00510 #define mmap __mmap 00511 #define munmap __munmap 00512 #define mremap __mremap 00513 #define mprotect __mprotect 00514 #define MORECORE (*__morecore) 00515 #define MORECORE_FAILURE 0 00516 00517 Void_t * __default_morecore (ptrdiff_t); 00518 Void_t *(*__morecore)(ptrdiff_t) = __default_morecore; 00519 00520 #else /* !_LIBC */ 00521 #define public_cALLOc calloc 00522 #define public_fREe free 00523 #define public_cFREe cfree 00524 #define public_mALLOc malloc 00525 #define public_mEMALIGn memalign 00526 #define public_rEALLOc realloc 00527 #define public_vALLOc valloc 00528 #define public_pVALLOc pvalloc 00529 #define public_mALLINFo mallinfo 00530 #define public_mALLOPt mallopt 00531 #define public_mTRIm malloc_trim 00532 #define public_mSTATs malloc_stats 00533 #define public_mUSABLe malloc_usable_size 00534 #define public_iCALLOc independent_calloc 00535 #define public_iCOMALLOc independent_comalloc 00536 #define public_gET_STATe malloc_get_state 00537 #define public_sET_STATe malloc_set_state 00538 #endif /* _LIBC */ 00539 #endif /* USE_DL_PREFIX */ 00540 00541 #ifndef _LIBC 00542 #define __builtin_expect(expr, val) (expr) 00543 00544 #define fwrite(buf, size, count, fp) _IO_fwrite (buf, size, count, fp) 00545 #endif 00546 00547 /* 00548 HAVE_MEMCPY should be defined if you are not otherwise using 00549 ANSI STD C, but still have memcpy and memset in your C library 00550 and want to use them in calloc and realloc. Otherwise simple 00551 macro versions are defined below. 00552 00553 USE_MEMCPY should be defined as 1 if you actually want to 00554 have memset and memcpy called. People report that the macro 00555 versions are faster than libc versions on some systems. 00556 00557 Even if USE_MEMCPY is set to 1, loops to copy/clear small chunks 00558 (of <= 36 bytes) are manually unrolled in realloc and calloc. 00559 */ 00560 00561 #include <string.h> 00562 00563 /* 00564 MALLOC_FAILURE_ACTION is the action to take before "return 0" when 00565 malloc fails to be able to return memory, either because memory is 00566 exhausted or because of illegal arguments. 00567 00568 By default, sets errno if running on STD_C platform, else does nothing. 00569 */ 00570 00571 #ifndef MALLOC_FAILURE_ACTION 00572 #if __STD_C 00573 #define MALLOC_FAILURE_ACTION \ 00574 errno = ENOMEM; 00575 00576 #else 00577 #define MALLOC_FAILURE_ACTION 00578 #endif 00579 #endif 00580 00581 /* 00582 MORECORE-related declarations. By default, rely on sbrk 00583 */ 00584 00585 00586 #ifdef LACKS_UNISTD_H 00587 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) 00588 #if __STD_C 00589 extern Void_t* sbrk(ptrdiff_t); 00590 #else 00591 extern Void_t* sbrk(); 00592 #endif 00593 #endif 00594 #endif 00595 00596 /* 00597 MORECORE is the name of the routine to call to obtain more memory 00598 from the system. See below for general guidance on writing 00599 alternative MORECORE functions, as well as a version for WIN32 and a 00600 sample version for pre-OSX macos. 00601 */ 00602 00603 #ifndef MORECORE 00604 #define MORECORE sbrk 00605 #endif 00606 00607 /* 00608 MORECORE_FAILURE is the value returned upon failure of MORECORE 00609 as well as mmap. Since it cannot be an otherwise valid memory address, 00610 and must reflect values of standard sys calls, you probably ought not 00611 try to redefine it. 00612 */ 00613 00614 #ifndef MORECORE_FAILURE 00615 #define MORECORE_FAILURE (-1) 00616 #endif 00617 00618 /* 00619 If MORECORE_CONTIGUOUS is true, take advantage of fact that 00620 consecutive calls to MORECORE with positive arguments always return 00621 contiguous increasing addresses. This is true of unix sbrk. Even 00622 if not defined, when regions happen to be contiguous, malloc will 00623 permit allocations spanning regions obtained from different 00624 calls. But defining this when applicable enables some stronger 00625 consistency checks and space efficiencies. 00626 */ 00627 00628 #ifndef MORECORE_CONTIGUOUS 00629 #define MORECORE_CONTIGUOUS 1 00630 #endif 00631 00632 /* 00633 Define MORECORE_CANNOT_TRIM if your version of MORECORE 00634 cannot release space back to the system when given negative 00635 arguments. This is generally necessary only if you are using 00636 a hand-crafted MORECORE function that cannot handle negative arguments. 00637 */ 00638 00639 /* #define MORECORE_CANNOT_TRIM */ 00640 00641 /* MORECORE_CLEARS (default 1) 00642 The degree to which the routine mapped to MORECORE zeroes out 00643 memory: never (0), only for newly allocated space (1) or always 00644 (2). The distinction between (1) and (2) is necessary because on 00645 some systems, if the application first decrements and then 00646 increments the break value, the contents of the reallocated space 00647 are unspecified. 00648 */ 00649 00650 #ifndef MORECORE_CLEARS 00651 #define MORECORE_CLEARS 1 00652 #endif 00653 00654 00655 /* 00656 Define HAVE_MMAP as true to optionally make malloc() use mmap() to 00657 allocate very large blocks. These will be returned to the 00658 operating system immediately after a free(). Also, if mmap 00659 is available, it is used as a backup strategy in cases where 00660 MORECORE fails to provide space from system. 00661 00662 This malloc is best tuned to work with mmap for large requests. 00663 If you do not have mmap, operations involving very large chunks (1MB 00664 or so) may be slower than you'd like. 00665 */ 00666 00667 #ifndef HAVE_MMAP 00668 #define HAVE_MMAP 1 00669 00670 /* 00671 Standard unix mmap using /dev/zero clears memory so calloc doesn't 00672 need to. 00673 */ 00674 00675 #ifndef MMAP_CLEARS 00676 #define MMAP_CLEARS 1 00677 #endif 00678 00679 #else /* no mmap */ 00680 #ifndef MMAP_CLEARS 00681 #define MMAP_CLEARS 0 00682 #endif 00683 #endif 00684 00685 00686 /* 00687 MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if 00688 sbrk fails, and mmap is used as a backup (which is done only if 00689 HAVE_MMAP). The value must be a multiple of page size. This 00690 backup strategy generally applies only when systems have "holes" in 00691 address space, so sbrk cannot perform contiguous expansion, but 00692 there is still space available on system. On systems for which 00693 this is known to be useful (i.e. most linux kernels), this occurs 00694 only when programs allocate huge amounts of memory. Between this, 00695 and the fact that mmap regions tend to be limited, the size should 00696 be large, to avoid too many mmap calls and thus avoid running out 00697 of kernel resources. 00698 */ 00699 00700 #ifndef MMAP_AS_MORECORE_SIZE 00701 #define MMAP_AS_MORECORE_SIZE (1024 * 1024) 00702 #endif 00703 00704 /* 00705 Define HAVE_MREMAP to make realloc() use mremap() to re-allocate 00706 large blocks. This is currently only possible on Linux with 00707 kernel versions newer than 1.3.77. 00708 */ 00709 00710 #ifndef HAVE_MREMAP 00711 #ifdef linux 00712 #define HAVE_MREMAP 1 00713 #else 00714 #define HAVE_MREMAP 0 00715 #endif 00716 00717 #endif /* HAVE_MMAP */ 00718 00719 /* Define USE_ARENAS to enable support for multiple `arenas'. These 00720 are allocated using mmap(), are necessary for threads and 00721 occasionally useful to overcome address space limitations affecting 00722 sbrk(). */ 00723 00724 #ifndef USE_ARENAS 00725 #define USE_ARENAS HAVE_MMAP 00726 #endif 00727 00728 00729 /* 00730 The system page size. To the extent possible, this malloc manages 00731 memory from the system in page-size units. Note that this value is 00732 cached during initialization into a field of malloc_state. So even 00733 if malloc_getpagesize is a function, it is only called once. 00734 00735 The following mechanics for getpagesize were adapted from bsd/gnu 00736 getpagesize.h. If none of the system-probes here apply, a value of 00737 4096 is used, which should be OK: If they don't apply, then using 00738 the actual value probably doesn't impact performance. 00739 */ 00740 00741 00742 #ifndef malloc_getpagesize 00743 00744 #ifndef LACKS_UNISTD_H 00745 # include <unistd.h> 00746 #endif 00747 00748 # ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */ 00749 # ifndef _SC_PAGE_SIZE 00750 # define _SC_PAGE_SIZE _SC_PAGESIZE 00751 # endif 00752 # endif 00753 00754 # ifdef _SC_PAGE_SIZE 00755 # define malloc_getpagesize sysconf(_SC_PAGE_SIZE) 00756 # else 00757 # if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE) 00758 extern size_t getpagesize(); 00759 # define malloc_getpagesize getpagesize() 00760 # else 00761 # ifdef WIN32 /* use supplied emulation of getpagesize */ 00762 # define malloc_getpagesize getpagesize() 00763 # else 00764 # ifndef LACKS_SYS_PARAM_H 00765 # include <sys/param.h> 00766 # endif 00767 # ifdef EXEC_PAGESIZE 00768 # define malloc_getpagesize EXEC_PAGESIZE 00769 # else 00770 # ifdef NBPG 00771 # ifndef CLSIZE 00772 # define malloc_getpagesize NBPG 00773 # else 00774 # define malloc_getpagesize (NBPG * CLSIZE) 00775 # endif 00776 # else 00777 # ifdef NBPC 00778 # define malloc_getpagesize NBPC 00779 # else 00780 # ifdef PAGESIZE 00781 # define malloc_getpagesize PAGESIZE 00782 # else /* just guess */ 00783 # define malloc_getpagesize (4096) 00784 # endif 00785 # endif 00786 # endif 00787 # endif 00788 # endif 00789 # endif 00790 # endif 00791 #endif 00792 00793 /* 00794 This version of malloc supports the standard SVID/XPG mallinfo 00795 routine that returns a struct containing usage properties and 00796 statistics. It should work on any SVID/XPG compliant system that has 00797 a /usr/include/malloc.h defining struct mallinfo. (If you'd like to 00798 install such a thing yourself, cut out the preliminary declarations 00799 as described above and below and save them in a malloc.h file. But 00800 there's no compelling reason to bother to do this.) 00801 00802 The main declaration needed is the mallinfo struct that is returned 00803 (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a 00804 bunch of fields that are not even meaningful in this version of 00805 malloc. These fields are are instead filled by mallinfo() with 00806 other numbers that might be of interest. 00807 00808 HAVE_USR_INCLUDE_MALLOC_H should be set if you have a 00809 /usr/include/malloc.h file that includes a declaration of struct 00810 mallinfo. If so, it is included; else an SVID2/XPG2 compliant 00811 version is declared below. These must be precisely the same for 00812 mallinfo() to work. The original SVID version of this struct, 00813 defined on most systems with mallinfo, declares all fields as 00814 ints. But some others define as unsigned long. If your system 00815 defines the fields using a type of different width than listed here, 00816 you must #include your system version and #define 00817 HAVE_USR_INCLUDE_MALLOC_H. 00818 */ 00819 00820 /* #define HAVE_USR_INCLUDE_MALLOC_H */ 00821 00822 #ifdef HAVE_USR_INCLUDE_MALLOC_H 00823 #include "/usr/include/malloc.h" 00824 #endif 00825 00826 00827 /* ---------- description of public routines ------------ */ 00828 00829 /* 00830 malloc(size_t n) 00831 Returns a pointer to a newly allocated chunk of at least n bytes, or null 00832 if no space is available. Additionally, on failure, errno is 00833 set to ENOMEM on ANSI C systems. 00834 00835 If n is zero, malloc returns a minumum-sized chunk. (The minimum 00836 size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit 00837 systems.) On most systems, size_t is an unsigned type, so calls 00838 with negative arguments are interpreted as requests for huge amounts 00839 of space, which will often fail. The maximum supported value of n 00840 differs across systems, but is in all cases less than the maximum 00841 representable value of a size_t. 00842 */ 00843 #if __STD_C 00844 Void_t* public_mALLOc(size_t); 00845 #else 00846 Void_t* public_mALLOc(); 00847 #endif 00848 00849 /* 00850 free(Void_t* p) 00851 Releases the chunk of memory pointed to by p, that had been previously 00852 allocated using malloc or a related routine such as realloc. 00853 It has no effect if p is null. It can have arbitrary (i.e., bad!) 00854 effects if p has already been freed. 00855 00856 Unless disabled (using mallopt), freeing very large spaces will 00857 when possible, automatically trigger operations that give 00858 back unused memory to the system, thus reducing program footprint. 00859 */ 00860 #if __STD_C 00861 void public_fREe(Void_t*); 00862 #else 00863 void public_fREe(); 00864 #endif 00865 00866 /* 00867 calloc(size_t n_elements, size_t element_size); 00868 Returns a pointer to n_elements * element_size bytes, with all locations 00869 set to zero. 00870 */ 00871 #if __STD_C 00872 Void_t* public_cALLOc(size_t, size_t); 00873 #else 00874 Void_t* public_cALLOc(); 00875 #endif 00876 00877 /* 00878 realloc(Void_t* p, size_t n) 00879 Returns a pointer to a chunk of size n that contains the same data 00880 as does chunk p up to the minimum of (n, p's size) bytes, or null 00881 if no space is available. 00882 00883 The returned pointer may or may not be the same as p. The algorithm 00884 prefers extending p when possible, otherwise it employs the 00885 equivalent of a malloc-copy-free sequence. 00886 00887 If p is null, realloc is equivalent to malloc. 00888 00889 If space is not available, realloc returns null, errno is set (if on 00890 ANSI) and p is NOT freed. 00891 00892 if n is for fewer bytes than already held by p, the newly unused 00893 space is lopped off and freed if possible. Unless the #define 00894 REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of 00895 zero (re)allocates a minimum-sized chunk. 00896 00897 Large chunks that were internally obtained via mmap will always 00898 be reallocated using malloc-copy-free sequences unless 00899 the system supports MREMAP (currently only linux). 00900 00901 The old unix realloc convention of allowing the last-free'd chunk 00902 to be used as an argument to realloc is not supported. 00903 */ 00904 #if __STD_C 00905 Void_t* public_rEALLOc(Void_t*, size_t); 00906 #else 00907 Void_t* public_rEALLOc(); 00908 #endif 00909 00910 /* 00911 memalign(size_t alignment, size_t n); 00912 Returns a pointer to a newly allocated chunk of n bytes, aligned 00913 in accord with the alignment argument. 00914 00915 The alignment argument should be a power of two. If the argument is 00916 not a power of two, the nearest greater power is used. 00917 8-byte alignment is guaranteed by normal malloc calls, so don't 00918 bother calling memalign with an argument of 8 or less. 00919 00920 Overreliance on memalign is a sure way to fragment space. 00921 */ 00922 #if __STD_C 00923 Void_t* public_mEMALIGn(size_t, size_t); 00924 #else 00925 Void_t* public_mEMALIGn(); 00926 #endif 00927 00928 /* 00929 valloc(size_t n); 00930 Equivalent to memalign(pagesize, n), where pagesize is the page 00931 size of the system. If the pagesize is unknown, 4096 is used. 00932 */ 00933 #if __STD_C 00934 Void_t* public_vALLOc(size_t); 00935 #else 00936 Void_t* public_vALLOc(); 00937 #endif 00938 00939 00940 00941 /* 00942 mallopt(int parameter_number, int parameter_value) 00943 Sets tunable parameters The format is to provide a 00944 (parameter-number, parameter-value) pair. mallopt then sets the 00945 corresponding parameter to the argument value if it can (i.e., so 00946 long as the value is meaningful), and returns 1 if successful else 00947 0. SVID/XPG/ANSI defines four standard param numbers for mallopt, 00948 normally defined in malloc.h. Only one of these (M_MXFAST) is used 00949 in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply, 00950 so setting them has no effect. But this malloc also supports four 00951 other options in mallopt. See below for details. Briefly, supported 00952 parameters are as follows (listed defaults are for "typical" 00953 configurations). 00954 00955 Symbol param # default allowed param values 00956 M_MXFAST 1 64 0-80 (0 disables fastbins) 00957 M_TRIM_THRESHOLD -1 128*1024 any (-1U disables trimming) 00958 M_TOP_PAD -2 0 any 00959 M_MMAP_THRESHOLD -3 128*1024 any (or 0 if no MMAP support) 00960 M_MMAP_MAX -4 65536 any (0 disables use of mmap) 00961 */ 00962 #if __STD_C 00963 int public_mALLOPt(int, int); 00964 #else 00965 int public_mALLOPt(); 00966 #endif 00967 00968 00969 /* 00970 mallinfo() 00971 Returns (by copy) a struct containing various summary statistics: 00972 00973 arena: current total non-mmapped bytes allocated from system 00974 ordblks: the number of free chunks 00975 smblks: the number of fastbin blocks (i.e., small chunks that 00976 have been freed but not use resused or consolidated) 00977 hblks: current number of mmapped regions 00978 hblkhd: total bytes held in mmapped regions 00979 usmblks: the maximum total allocated space. This will be greater 00980 than current total if trimming has occurred. 00981 fsmblks: total bytes held in fastbin blocks 00982 uordblks: current total allocated space (normal or mmapped) 00983 fordblks: total free space 00984 keepcost: the maximum number of bytes that could ideally be released 00985 back to system via malloc_trim. ("ideally" means that 00986 it ignores page restrictions etc.) 00987 00988 Because these fields are ints, but internal bookkeeping may 00989 be kept as longs, the reported values may wrap around zero and 00990 thus be inaccurate. 00991 */ 00992 #if __STD_C 00993 struct mallinfo public_mALLINFo(void); 00994 #else 00995 struct mallinfo public_mALLINFo(); 00996 #endif 00997 00998 /* 00999 independent_calloc(size_t n_elements, size_t element_size, Void_t* chunks[]); 01000 01001 independent_calloc is similar to calloc, but instead of returning a 01002 single cleared space, it returns an array of pointers to n_elements 01003 independent elements that can hold contents of size elem_size, each 01004 of which starts out cleared, and can be independently freed, 01005 realloc'ed etc. The elements are guaranteed to be adjacently 01006 allocated (this is not guaranteed to occur with multiple callocs or 01007 mallocs), which may also improve cache locality in some 01008 applications. 01009 01010 The "chunks" argument is optional (i.e., may be null, which is 01011 probably the most typical usage). If it is null, the returned array 01012 is itself dynamically allocated and should also be freed when it is 01013 no longer needed. Otherwise, the chunks array must be of at least 01014 n_elements in length. It is filled in with the pointers to the 01015 chunks. 01016 01017 In either case, independent_calloc returns this pointer array, or 01018 null if the allocation failed. If n_elements is zero and "chunks" 01019 is null, it returns a chunk representing an array with zero elements 01020 (which should be freed if not wanted). 01021 01022 Each element must be individually freed when it is no longer 01023 needed. If you'd like to instead be able to free all at once, you 01024 should instead use regular calloc and assign pointers into this 01025 space to represent elements. (In this case though, you cannot 01026 independently free elements.) 01027 01028 independent_calloc simplifies and speeds up implementations of many 01029 kinds of pools. It may also be useful when constructing large data 01030 structures that initially have a fixed number of fixed-sized nodes, 01031 but the number is not known at compile time, and some of the nodes 01032 may later need to be freed. For example: 01033 01034 struct Node { int item; struct Node* next; }; 01035 01036 struct Node* build_list() { 01037 struct Node** pool; 01038 int n = read_number_of_nodes_needed(); 01039 if (n <= 0) return 0; 01040 pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0); 01041 if (pool == 0) die(); 01042 // organize into a linked list... 01043 struct Node* first = pool[0]; 01044 for (i = 0; i < n-1; ++i) 01045 pool[i]->next = pool[i+1]; 01046 free(pool); // Can now free the array (or not, if it is needed later) 01047 return first; 01048 } 01049 */ 01050 #if __STD_C 01051 Void_t** public_iCALLOc(size_t, size_t, Void_t**); 01052 #else 01053 Void_t** public_iCALLOc(); 01054 #endif 01055 01056 /* 01057 independent_comalloc(size_t n_elements, size_t sizes[], Void_t* chunks[]); 01058 01059 independent_comalloc allocates, all at once, a set of n_elements 01060 chunks with sizes indicated in the "sizes" array. It returns 01061 an array of pointers to these elements, each of which can be 01062 independently freed, realloc'ed etc. The elements are guaranteed to 01063 be adjacently allocated (this is not guaranteed to occur with 01064 multiple callocs or mallocs), which may also improve cache locality 01065 in some applications. 01066 01067 The "chunks" argument is optional (i.e., may be null). If it is null 01068 the returned array is itself dynamically allocated and should also 01069 be freed when it is no longer needed. Otherwise, the chunks array 01070 must be of at least n_elements in length. It is filled in with the 01071 pointers to the chunks. 01072 01073 In either case, independent_comalloc returns this pointer array, or 01074 null if the allocation failed. If n_elements is zero and chunks is 01075 null, it returns a chunk representing an array with zero elements 01076 (which should be freed if not wanted). 01077 01078 Each element must be individually freed when it is no longer 01079 needed. If you'd like to instead be able to free all at once, you 01080 should instead use a single regular malloc, and assign pointers at 01081 particular offsets in the aggregate space. (In this case though, you 01082 cannot independently free elements.) 01083 01084 independent_comallac differs from independent_calloc in that each 01085 element may have a different size, and also that it does not 01086 automatically clear elements. 01087 01088 independent_comalloc can be used to speed up allocation in cases 01089 where several structs or objects must always be allocated at the 01090 same time. For example: 01091 01092 struct Head { ... } 01093 struct Foot { ... } 01094 01095 void send_message(char* msg) { 01096 int msglen = strlen(msg); 01097 size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; 01098 void* chunks[3]; 01099 if (independent_comalloc(3, sizes, chunks) == 0) 01100 die(); 01101 struct Head* head = (struct Head*)(chunks[0]); 01102 char* body = (char*)(chunks[1]); 01103 struct Foot* foot = (struct Foot*)(chunks[2]); 01104 // ... 01105 } 01106 01107 In general though, independent_comalloc is worth using only for 01108 larger values of n_elements. For small values, you probably won't 01109 detect enough difference from series of malloc calls to bother. 01110 01111 Overuse of independent_comalloc can increase overall memory usage, 01112 since it cannot reuse existing noncontiguous small chunks that 01113 might be available for some of the elements. 01114 */ 01115 #if __STD_C 01116 Void_t** public_iCOMALLOc(size_t, size_t*, Void_t**); 01117 #else 01118 Void_t** public_iCOMALLOc(); 01119 #endif 01120 01121 01122 /* 01123 pvalloc(size_t n); 01124 Equivalent to valloc(minimum-page-that-holds(n)), that is, 01125 round up n to nearest pagesize. 01126 */ 01127 #if __STD_C 01128 Void_t* public_pVALLOc(size_t); 01129 #else 01130 Void_t* public_pVALLOc(); 01131 #endif 01132 01133 /* 01134 cfree(Void_t* p); 01135 Equivalent to free(p). 01136 01137 cfree is needed/defined on some systems that pair it with calloc, 01138 for odd historical reasons (such as: cfree is used in example 01139 code in the first edition of K&R). 01140 */ 01141 #if __STD_C 01142 void public_cFREe(Void_t*); 01143 #else 01144 void public_cFREe(); 01145 #endif 01146 01147 /* 01148 malloc_trim(size_t pad); 01149 01150 If possible, gives memory back to the system (via negative 01151 arguments to sbrk) if there is unused memory at the `high' end of 01152 the malloc pool. You can call this after freeing large blocks of 01153 memory to potentially reduce the system-level memory requirements 01154 of a program. However, it cannot guarantee to reduce memory. Under 01155 some allocation patterns, some large free blocks of memory will be 01156 locked between two used chunks, so they cannot be given back to 01157 the system. 01158 01159 The `pad' argument to malloc_trim represents the amount of free 01160 trailing space to leave untrimmed. If this argument is zero, 01161 only the minimum amount of memory to maintain internal data 01162 structures will be left (one page or less). Non-zero arguments 01163 can be supplied to maintain enough trailing space to service 01164 future expected allocations without having to re-obtain memory 01165 from the system. 01166 01167 Malloc_trim returns 1 if it actually released any memory, else 0. 01168 On systems that do not support "negative sbrks", it will always 01169 rreturn 0. 01170 */ 01171 #if __STD_C 01172 int public_mTRIm(size_t); 01173 #else 01174 int public_mTRIm(); 01175 #endif 01176 01177 /* 01178 malloc_usable_size(Void_t* p); 01179 01180 Returns the number of bytes you can actually use in 01181 an allocated chunk, which may be more than you requested (although 01182 often not) due to alignment and minimum size constraints. 01183 You can use this many bytes without worrying about 01184 overwriting other allocated objects. This is not a particularly great 01185 programming practice. malloc_usable_size can be more useful in 01186 debugging and assertions, for example: 01187 01188 p = malloc(n); 01189 assert(malloc_usable_size(p) >= 256); 01190 01191 */ 01192 #if __STD_C 01193 size_t public_mUSABLe(Void_t*); 01194 #else 01195 size_t public_mUSABLe(); 01196 #endif 01197 01198 /* 01199 malloc_stats(); 01200 Prints on stderr the amount of space obtained from the system (both 01201 via sbrk and mmap), the maximum amount (which may be more than 01202 current if malloc_trim and/or munmap got called), and the current 01203 number of bytes allocated via malloc (or realloc, etc) but not yet 01204 freed. Note that this is the number of bytes allocated, not the 01205 number requested. It will be larger than the number requested 01206 because of alignment and bookkeeping overhead. Because it includes 01207 alignment wastage as being in use, this figure may be greater than 01208 zero even when no user-level chunks are allocated. 01209 01210 The reported current and maximum system memory can be inaccurate if 01211 a program makes other calls to system memory allocation functions 01212 (normally sbrk) outside of malloc. 01213 01214 malloc_stats prints only the most commonly interesting statistics. 01215 More information can be obtained by calling mallinfo. 01216 01217 */ 01218 #if __STD_C 01219 void public_mSTATs(void); 01220 #else 01221 void public_mSTATs(); 01222 #endif 01223 01224 /* 01225 malloc_get_state(void); 01226 01227 Returns the state of all malloc variables in an opaque data 01228 structure. 01229 */ 01230 #if __STD_C 01231 Void_t* public_gET_STATe(void); 01232 #else 01233 Void_t* public_gET_STATe(); 01234 #endif 01235 01236 /* 01237 malloc_set_state(Void_t* state); 01238 01239 Restore the state of all malloc variables from data obtained with 01240 malloc_get_state(). 01241 */ 01242 #if __STD_C 01243 int public_sET_STATe(Void_t*); 01244 #else 01245 int public_sET_STATe(); 01246 #endif 01247 01248 #ifdef _LIBC 01249 /* 01250 posix_memalign(void **memptr, size_t alignment, size_t size); 01251 01252 POSIX wrapper like memalign(), checking for validity of size. 01253 */ 01254 int __posix_memalign(void **, size_t, size_t); 01255 #endif 01256 01257 /* mallopt tuning options */ 01258 01259 /* 01260 M_MXFAST is the maximum request size used for "fastbins", special bins 01261 that hold returned chunks without consolidating their spaces. This 01262 enables future requests for chunks of the same size to be handled 01263 very quickly, but can increase fragmentation, and thus increase the 01264 overall memory footprint of a program. 01265 01266 This malloc manages fastbins very conservatively yet still 01267 efficiently, so fragmentation is rarely a problem for values less 01268 than or equal to the default. The maximum supported value of MXFAST 01269 is 80. You wouldn't want it any higher than this anyway. Fastbins 01270 are designed especially for use with many small structs, objects or 01271 strings -- the default handles structs/objects/arrays with sizes up 01272 to 8 4byte fields, or small strings representing words, tokens, 01273 etc. Using fastbins for larger objects normally worsens 01274 fragmentation without improving speed. 01275 01276 M_MXFAST is set in REQUEST size units. It is internally used in 01277 chunksize units, which adds padding and alignment. You can reduce 01278 M_MXFAST to 0 to disable all use of fastbins. This causes the malloc 01279 algorithm to be a closer approximation of fifo-best-fit in all cases, 01280 not just for larger requests, but will generally cause it to be 01281 slower. 01282 */ 01283 01284 01285 /* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */ 01286 #ifndef M_MXFAST 01287 #define M_MXFAST 1 01288 #endif 01289 01290 #ifndef DEFAULT_MXFAST 01291 #define DEFAULT_MXFAST 128 01292 #endif 01293 01294 01295 /* 01296 M_TRIM_THRESHOLD is the maximum amount of unused top-most