| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Malloc implementation for multiple threads without lock contention. | 
|  | 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 | Contributed by Wolfram Gloger <wg@malloc.de> | 
|  | 5 | and Doug Lea <dl@cs.oswego.edu>, 2001. | 
|  | 6 |  | 
|  | 7 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 8 | modify it under the terms of the GNU Lesser General Public License as | 
|  | 9 | published by the Free Software Foundation; either version 2.1 of the | 
|  | 10 | License, or (at your option) any later version. | 
|  | 11 |  | 
|  | 12 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 15 | Lesser General Public License for more details. | 
|  | 16 |  | 
|  | 17 | You should have received a copy of the GNU Lesser General Public | 
|  | 18 | License along with the GNU C Library; see the file COPYING.LIB.  If | 
|  | 19 | not, see <http://www.gnu.org/licenses/>.  */ | 
|  | 20 |  | 
|  | 21 | /* | 
|  | 22 | This is a version (aka ptmalloc2) of malloc/free/realloc written by | 
|  | 23 | Doug Lea and adapted to multiple threads/arenas by Wolfram Gloger. | 
|  | 24 |  | 
|  | 25 | There have been substantial changes made after the integration into | 
|  | 26 | glibc in all parts of the code.  Do not look for much commonality | 
|  | 27 | with the ptmalloc2 version. | 
|  | 28 |  | 
|  | 29 | * Version ptmalloc2-20011215 | 
|  | 30 | based on: | 
|  | 31 | VERSION 2.7.0 Sun Mar 11 14:14:06 2001  Doug Lea  (dl at gee) | 
|  | 32 |  | 
|  | 33 | * Quickstart | 
|  | 34 |  | 
|  | 35 | In order to compile this implementation, a Makefile is provided with | 
|  | 36 | the ptmalloc2 distribution, which has pre-defined targets for some | 
|  | 37 | popular systems (e.g. "make posix" for Posix threads).  All that is | 
|  | 38 | typically required with regard to compiler flags is the selection of | 
|  | 39 | the thread package via defining one out of USE_PTHREADS, USE_THR or | 
|  | 40 | USE_SPROC.  Check the thread-m.h file for what effects this has. | 
|  | 41 | Many/most systems will additionally require USE_TSD_DATA_HACK to be | 
|  | 42 | defined, so this is the default for "make posix". | 
|  | 43 |  | 
|  | 44 | * Why use this malloc? | 
|  | 45 |  | 
|  | 46 | This is not the fastest, most space-conserving, most portable, or | 
|  | 47 | most tunable malloc ever written. However it is among the fastest | 
|  | 48 | while also being among the most space-conserving, portable and tunable. | 
|  | 49 | Consistent balance across these factors results in a good general-purpose | 
|  | 50 | allocator for malloc-intensive programs. | 
|  | 51 |  | 
|  | 52 | The main properties of the algorithms are: | 
|  | 53 | * For large (>= 512 bytes) requests, it is a pure best-fit allocator, | 
|  | 54 | with ties normally decided via FIFO (i.e. least recently used). | 
|  | 55 | * For small (<= 64 bytes by default) requests, it is a caching | 
|  | 56 | allocator, that maintains pools of quickly recycled chunks. | 
|  | 57 | * In between, and for combinations of large and small requests, it does | 
|  | 58 | the best it can trying to meet both goals at once. | 
|  | 59 | * For very large requests (>= 128KB by default), it relies on system | 
|  | 60 | memory mapping facilities, if supported. | 
|  | 61 |  | 
|  | 62 | For a longer but slightly out of date high-level description, see | 
|  | 63 | http://gee.cs.oswego.edu/dl/html/malloc.html | 
|  | 64 |  | 
|  | 65 | You may already by default be using a C library containing a malloc | 
|  | 66 | that is  based on some version of this malloc (for example in | 
|  | 67 | linux). You might still want to use the one in this file in order to | 
|  | 68 | customize settings or to avoid overheads associated with library | 
|  | 69 | versions. | 
|  | 70 |  | 
|  | 71 | * Contents, described in more detail in "description of public routines" below. | 
|  | 72 |  | 
|  | 73 | Standard (ANSI/SVID/...)  functions: | 
|  | 74 | malloc(size_t n); | 
|  | 75 | calloc(size_t n_elements, size_t element_size); | 
|  | 76 | free(void* p); | 
|  | 77 | realloc(void* p, size_t n); | 
|  | 78 | memalign(size_t alignment, size_t n); | 
|  | 79 | valloc(size_t n); | 
|  | 80 | mallinfo() | 
|  | 81 | mallopt(int parameter_number, int parameter_value) | 
|  | 82 |  | 
|  | 83 | Additional functions: | 
|  | 84 | independent_calloc(size_t n_elements, size_t size, void* chunks[]); | 
|  | 85 | independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); | 
|  | 86 | pvalloc(size_t n); | 
|  | 87 | cfree(void* p); | 
|  | 88 | malloc_trim(size_t pad); | 
|  | 89 | malloc_usable_size(void* p); | 
|  | 90 | malloc_stats(); | 
|  | 91 |  | 
|  | 92 | * Vital statistics: | 
|  | 93 |  | 
|  | 94 | Supported pointer representation:       4 or 8 bytes | 
|  | 95 | Supported size_t  representation:       4 or 8 bytes | 
|  | 96 | Note that size_t is allowed to be 4 bytes even if pointers are 8. | 
|  | 97 | You can adjust this by defining INTERNAL_SIZE_T | 
|  | 98 |  | 
|  | 99 | Alignment:                              2 * sizeof(size_t) (default) | 
|  | 100 | (i.e., 8 byte alignment with 4byte size_t). This suffices for | 
|  | 101 | nearly all current machines and C compilers. However, you can | 
|  | 102 | define MALLOC_ALIGNMENT to be wider than this if necessary. | 
|  | 103 |  | 
|  | 104 | Minimum overhead per allocated chunk:   4 or 8 bytes | 
|  | 105 | Each malloced chunk has a hidden word of overhead holding size | 
|  | 106 | and status information. | 
|  | 107 |  | 
|  | 108 | Minimum allocated size: 4-byte ptrs:  16 bytes    (including 4 overhead) | 
|  | 109 | 8-byte ptrs:  24/32 bytes (including, 4/8 overhead) | 
|  | 110 |  | 
|  | 111 | When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte | 
|  | 112 | ptrs but 4 byte size) or 24 (for 8/8) additional bytes are | 
|  | 113 | needed; 4 (8) for a trailing size field and 8 (16) bytes for | 
|  | 114 | free list pointers. Thus, the minimum allocatable size is | 
|  | 115 | 16/24/32 bytes. | 
|  | 116 |  | 
|  | 117 | Even a request for zero bytes (i.e., malloc(0)) returns a | 
|  | 118 | pointer to something of the minimum allocatable size. | 
|  | 119 |  | 
|  | 120 | The maximum overhead wastage (i.e., number of extra bytes | 
|  | 121 | allocated than were requested in malloc) is less than or equal | 
|  | 122 | to the minimum size, except for requests >= mmap_threshold that | 
|  | 123 | are serviced via mmap(), where the worst case wastage is 2 * | 
|  | 124 | sizeof(size_t) bytes plus the remainder from a system page (the | 
|  | 125 | minimal mmap unit); typically 4096 or 8192 bytes. | 
|  | 126 |  | 
|  | 127 | Maximum allocated size:  4-byte size_t: 2^32 minus about two pages | 
|  | 128 | 8-byte size_t: 2^64 minus about two pages | 
|  | 129 |  | 
|  | 130 | It is assumed that (possibly signed) size_t values suffice to | 
|  | 131 | represent chunk sizes. `Possibly signed' is due to the fact | 
|  | 132 | that `size_t' may be defined on a system as either a signed or | 
|  | 133 | an unsigned type. The ISO C standard says that it must be | 
|  | 134 | unsigned, but a few systems are known not to adhere to this. | 
|  | 135 | Additionally, even when size_t is unsigned, sbrk (which is by | 
|  | 136 | default used to obtain memory from system) accepts signed | 
|  | 137 | arguments, and may not be able to handle size_t-wide arguments | 
|  | 138 | with negative sign bit.  Generally, values that would | 
|  | 139 | appear as negative after accounting for overhead and alignment | 
|  | 140 | are supported only via mmap(), which does not have this | 
|  | 141 | limitation. | 
|  | 142 |  | 
|  | 143 | Requests for sizes outside the allowed range will perform an optional | 
|  | 144 | failure action and then return null. (Requests may also | 
|  | 145 | also fail because a system is out of memory.) | 
|  | 146 |  | 
|  | 147 | Thread-safety: thread-safe | 
|  | 148 |  | 
|  | 149 | Compliance: I believe it is compliant with the 1997 Single Unix Specification | 
|  | 150 | Also SVID/XPG, ANSI C, and probably others as well. | 
|  | 151 |  | 
|  | 152 | * Synopsis of compile-time options: | 
|  | 153 |  | 
|  | 154 | People have reported using previous versions of this malloc on all | 
|  | 155 | versions of Unix, sometimes by tweaking some of the defines | 
|  | 156 | below. It has been tested most extensively on Solaris and Linux. | 
|  | 157 | People also report using it in stand-alone embedded systems. | 
|  | 158 |  | 
|  | 159 | The implementation is in straight, hand-tuned ANSI C.  It is not | 
|  | 160 | at all modular. (Sorry!)  It uses a lot of macros.  To be at all | 
|  | 161 | usable, this code should be compiled using an optimizing compiler | 
|  | 162 | (for example gcc -O3) that can simplify expressions and control | 
|  | 163 | paths. (FAQ: some macros import variables as arguments rather than | 
|  | 164 | declare locals because people reported that some debuggers | 
|  | 165 | otherwise get confused.) | 
|  | 166 |  | 
|  | 167 | OPTION                     DEFAULT VALUE | 
|  | 168 |  | 
|  | 169 | Compilation Environment options: | 
|  | 170 |  | 
|  | 171 | HAVE_MREMAP                0 | 
|  | 172 |  | 
|  | 173 | Changing default word sizes: | 
|  | 174 |  | 
|  | 175 | INTERNAL_SIZE_T            size_t | 
|  | 176 | MALLOC_ALIGNMENT           MAX (2 * sizeof(INTERNAL_SIZE_T), | 
|  | 177 | __alignof__ (long double)) | 
|  | 178 |  | 
|  | 179 | Configuration and functionality options: | 
|  | 180 |  | 
|  | 181 | USE_PUBLIC_MALLOC_WRAPPERS NOT defined | 
|  | 182 | USE_MALLOC_LOCK            NOT defined | 
|  | 183 | MALLOC_DEBUG               NOT defined | 
|  | 184 | REALLOC_ZERO_BYTES_FREES   1 | 
|  | 185 | TRIM_FASTBINS              0 | 
|  | 186 |  | 
|  | 187 | Options for customizing MORECORE: | 
|  | 188 |  | 
|  | 189 | MORECORE                   sbrk | 
|  | 190 | MORECORE_FAILURE           -1 | 
|  | 191 | MORECORE_CONTIGUOUS        1 | 
|  | 192 | MORECORE_CANNOT_TRIM       NOT defined | 
|  | 193 | MORECORE_CLEARS            1 | 
|  | 194 | MMAP_AS_MORECORE_SIZE      (1024 * 1024) | 
|  | 195 |  | 
|  | 196 | Tuning options that are also dynamically changeable via mallopt: | 
|  | 197 |  | 
|  | 198 | DEFAULT_MXFAST             64 (for 32bit), 128 (for 64bit) | 
|  | 199 | DEFAULT_TRIM_THRESHOLD     128 * 1024 | 
|  | 200 | DEFAULT_TOP_PAD            0 | 
|  | 201 | DEFAULT_MMAP_THRESHOLD     128 * 1024 | 
|  | 202 | DEFAULT_MMAP_MAX           65536 | 
|  | 203 |  | 
|  | 204 | There are several other #defined constants and macros that you | 
|  | 205 | probably don't want to touch unless you are extending or adapting malloc.  */ | 
|  | 206 |  | 
|  | 207 | /* | 
|  | 208 | void* is the pointer type that malloc should say it returns | 
|  | 209 | */ | 
|  | 210 |  | 
|  | 211 | #ifndef void | 
|  | 212 | #define void      void | 
|  | 213 | #endif /*void*/ | 
|  | 214 |  | 
|  | 215 | #include <stddef.h>   /* for size_t */ | 
|  | 216 | #include <stdlib.h>   /* for getenv(), abort() */ | 
|  | 217 | #include <unistd.h>   /* for __libc_enable_secure */ | 
|  | 218 |  | 
|  | 219 | #include <malloc-machine.h> | 
|  | 220 | #include <malloc-sysdep.h> | 
|  | 221 |  | 
|  | 222 | #include <atomic.h> | 
|  | 223 | #include <_itoa.h> | 
|  | 224 | #include <bits/wordsize.h> | 
|  | 225 | #include <sys/sysinfo.h> | 
|  | 226 |  | 
|  | 227 | #include <ldsodefs.h> | 
|  | 228 |  | 
|  | 229 | #include <unistd.h> | 
|  | 230 | #include <stdio.h>    /* needed for malloc_stats */ | 
|  | 231 | #include <errno.h> | 
|  | 232 |  | 
|  | 233 | #include <shlib-compat.h> | 
|  | 234 |  | 
|  | 235 | /* For uintptr_t.  */ | 
|  | 236 | #include <stdint.h> | 
|  | 237 |  | 
|  | 238 | /* For va_arg, va_start, va_end.  */ | 
|  | 239 | #include <stdarg.h> | 
|  | 240 |  | 
|  | 241 | /* For MIN, MAX, powerof2.  */ | 
|  | 242 | #include <sys/param.h> | 
|  | 243 |  | 
|  | 244 | /* For ALIGN_UP et. al.  */ | 
|  | 245 | #include <libc-internal.h> | 
|  | 246 |  | 
|  | 247 |  | 
|  | 248 | /* | 
|  | 249 | Debugging: | 
|  | 250 |  | 
|  | 251 | Because freed chunks may be overwritten with bookkeeping fields, this | 
|  | 252 | malloc will often die when freed memory is overwritten by user | 
|  | 253 | programs.  This can be very effective (albeit in an annoying way) | 
|  | 254 | in helping track down dangling pointers. | 
|  | 255 |  | 
|  | 256 | If you compile with -DMALLOC_DEBUG, a number of assertion checks are | 
|  | 257 | enabled that will catch more memory errors. You probably won't be | 
|  | 258 | able to make much sense of the actual assertion errors, but they | 
|  | 259 | should help you locate incorrectly overwritten memory.  The checking | 
|  | 260 | is fairly extensive, and will slow down execution | 
|  | 261 | noticeably. Calling malloc_stats or mallinfo with MALLOC_DEBUG set | 
|  | 262 | will attempt to check every non-mmapped allocated and free chunk in | 
|  | 263 | the course of computing the summmaries. (By nature, mmapped regions | 
|  | 264 | cannot be checked very much automatically.) | 
|  | 265 |  | 
|  | 266 | Setting MALLOC_DEBUG may also be helpful if you are trying to modify | 
|  | 267 | this code. The assertions in the check routines spell out in more | 
|  | 268 | detail the assumptions and invariants underlying the algorithms. | 
|  | 269 |  | 
|  | 270 | Setting MALLOC_DEBUG does NOT provide an automated mechanism for | 
|  | 271 | checking that all accesses to malloced memory stay within their | 
|  | 272 | bounds. However, there are several add-ons and adaptations of this | 
|  | 273 | or other mallocs available that do this. | 
|  | 274 | */ | 
|  | 275 |  | 
|  | 276 | #ifndef MALLOC_DEBUG | 
|  | 277 | #define MALLOC_DEBUG 0 | 
|  | 278 | #endif | 
|  | 279 |  | 
|  | 280 | #ifdef NDEBUG | 
|  | 281 | # define assert(expr) ((void) 0) | 
|  | 282 | #else | 
|  | 283 | # define assert(expr) \ | 
|  | 284 | ((expr)								      \ | 
|  | 285 | ? ((void) 0)								      \ | 
|  | 286 | : __malloc_assert (#expr, __FILE__, __LINE__, __func__)) | 
|  | 287 |  | 
|  | 288 | extern const char *__progname; | 
|  | 289 |  | 
|  | 290 | static void | 
|  | 291 | __malloc_assert (const char *assertion, const char *file, unsigned int line, | 
|  | 292 | const char *function) | 
|  | 293 | { | 
|  | 294 | (void) __fxprintf (NULL, "%s%s%s:%u: %s%sAssertion `%s' failed.\n", | 
|  | 295 | __progname, __progname[0] ? ": " : "", | 
|  | 296 | file, line, | 
|  | 297 | function ? function : "", function ? ": " : "", | 
|  | 298 | assertion); | 
|  | 299 | fflush (stderr); | 
|  | 300 | abort (); | 
|  | 301 | } | 
|  | 302 | #endif | 
|  | 303 |  | 
|  | 304 |  | 
|  | 305 | /* | 
|  | 306 | INTERNAL_SIZE_T is the word-size used for internal bookkeeping | 
|  | 307 | of chunk sizes. | 
|  | 308 |  | 
|  | 309 | The default version is the same as size_t. | 
|  | 310 |  | 
|  | 311 | While not strictly necessary, it is best to define this as an | 
|  | 312 | unsigned type, even if size_t is a signed type. This may avoid some | 
|  | 313 | artificial size limitations on some systems. | 
|  | 314 |  | 
|  | 315 | On a 64-bit machine, you may be able to reduce malloc overhead by | 
|  | 316 | defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the | 
|  | 317 | expense of not being able to handle more than 2^32 of malloced | 
|  | 318 | space. If this limitation is acceptable, you are encouraged to set | 
|  | 319 | this unless you are on a platform requiring 16byte alignments. In | 
|  | 320 | this case the alignment requirements turn out to negate any | 
|  | 321 | potential advantages of decreasing size_t word size. | 
|  | 322 |  | 
|  | 323 | Implementors: Beware of the possible combinations of: | 
|  | 324 | - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits, | 
|  | 325 | and might be the same width as int or as long | 
|  | 326 | - size_t might have different width and signedness as INTERNAL_SIZE_T | 
|  | 327 | - int and long might be 32 or 64 bits, and might be the same width | 
|  | 328 | To deal with this, most comparisons and difference computations | 
|  | 329 | among INTERNAL_SIZE_Ts should cast them to unsigned long, being | 
|  | 330 | aware of the fact that casting an unsigned int to a wider long does | 
|  | 331 | not sign-extend. (This also makes checking for negative numbers | 
|  | 332 | awkward.) Some of these casts result in harmless compiler warnings | 
|  | 333 | on some systems. | 
|  | 334 | */ | 
|  | 335 |  | 
|  | 336 | #ifndef INTERNAL_SIZE_T | 
|  | 337 | #define INTERNAL_SIZE_T size_t | 
|  | 338 | #endif | 
|  | 339 |  | 
|  | 340 | /* The corresponding word size */ | 
|  | 341 | #define SIZE_SZ                (sizeof(INTERNAL_SIZE_T)) | 
|  | 342 |  | 
|  | 343 |  | 
|  | 344 | /* | 
|  | 345 | MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks. | 
|  | 346 | It must be a power of two at least 2 * SIZE_SZ, even on machines | 
|  | 347 | for which smaller alignments would suffice. It may be defined as | 
|  | 348 | larger than this though. Note however that code and data structures | 
|  | 349 | are optimized for the case of 8-byte alignment. | 
|  | 350 | */ | 
|  | 351 |  | 
|  | 352 |  | 
|  | 353 | #ifndef MALLOC_ALIGNMENT | 
|  | 354 | # if !SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_16) | 
|  | 355 | /* This is the correct definition when there is no past ABI to constrain it. | 
|  | 356 |  | 
|  | 357 | Among configurations with a past ABI constraint, it differs from | 
|  | 358 | 2*SIZE_SZ only on powerpc32.  For the time being, changing this is | 
|  | 359 | causing more compatibility problems due to malloc_get_state and | 
|  | 360 | malloc_set_state than will returning blocks not adequately aligned for | 
|  | 361 | long double objects under -mlong-double-128.  */ | 
|  | 362 |  | 
|  | 363 | #  define MALLOC_ALIGNMENT       (2 *SIZE_SZ < __alignof__ (long double)      \ | 
|  | 364 | ? __alignof__ (long double) : 2 *SIZE_SZ) | 
|  | 365 | # else | 
|  | 366 | #  define MALLOC_ALIGNMENT       (2 *SIZE_SZ) | 
|  | 367 | # endif | 
|  | 368 | #endif | 
|  | 369 |  | 
|  | 370 | /* The corresponding bit mask value */ | 
|  | 371 | #define MALLOC_ALIGN_MASK      (MALLOC_ALIGNMENT - 1) | 
|  | 372 |  | 
|  | 373 |  | 
|  | 374 |  | 
|  | 375 | /* | 
|  | 376 | REALLOC_ZERO_BYTES_FREES should be set if a call to | 
|  | 377 | realloc with zero bytes should be the same as a call to free. | 
|  | 378 | This is required by the C standard. Otherwise, since this malloc | 
|  | 379 | returns a unique pointer for malloc(0), so does realloc(p, 0). | 
|  | 380 | */ | 
|  | 381 |  | 
|  | 382 | #ifndef REALLOC_ZERO_BYTES_FREES | 
|  | 383 | #define REALLOC_ZERO_BYTES_FREES 1 | 
|  | 384 | #endif | 
|  | 385 |  | 
|  | 386 | /* | 
|  | 387 | TRIM_FASTBINS controls whether free() of a very small chunk can | 
|  | 388 | immediately lead to trimming. Setting to true (1) can reduce memory | 
|  | 389 | footprint, but will almost always slow down programs that use a lot | 
|  | 390 | of small chunks. | 
|  | 391 |  | 
|  | 392 | Define this only if you are willing to give up some speed to more | 
|  | 393 | aggressively reduce system-level memory footprint when releasing | 
|  | 394 | memory in programs that use many small chunks.  You can get | 
|  | 395 | essentially the same effect by setting MXFAST to 0, but this can | 
|  | 396 | lead to even greater slowdowns in programs using many small chunks. | 
|  | 397 | TRIM_FASTBINS is an in-between compile-time option, that disables | 
|  | 398 | only those chunks bordering topmost memory from being placed in | 
|  | 399 | fastbins. | 
|  | 400 | */ | 
|  | 401 |  | 
|  | 402 | #ifndef TRIM_FASTBINS | 
|  | 403 | #define TRIM_FASTBINS  0 | 
|  | 404 | #endif | 
|  | 405 |  | 
|  | 406 |  | 
|  | 407 | /* Definition for getting more memory from the OS.  */ | 
|  | 408 | #define MORECORE         (*__morecore) | 
|  | 409 | #define MORECORE_FAILURE 0 | 
|  | 410 | void * __default_morecore (ptrdiff_t); | 
|  | 411 | void *(*__morecore)(ptrdiff_t) = __default_morecore; | 
|  | 412 |  | 
|  | 413 |  | 
|  | 414 | #include <string.h> | 
|  | 415 |  | 
|  | 416 | /* | 
|  | 417 | MORECORE-related declarations. By default, rely on sbrk | 
|  | 418 | */ | 
|  | 419 |  | 
|  | 420 |  | 
|  | 421 | /* | 
|  | 422 | MORECORE is the name of the routine to call to obtain more memory | 
|  | 423 | from the system.  See below for general guidance on writing | 
|  | 424 | alternative MORECORE functions, as well as a version for WIN32 and a | 
|  | 425 | sample version for pre-OSX macos. | 
|  | 426 | */ | 
|  | 427 |  | 
|  | 428 | #ifndef MORECORE | 
|  | 429 | #define MORECORE sbrk | 
|  | 430 | #endif | 
|  | 431 |  | 
|  | 432 | /* | 
|  | 433 | MORECORE_FAILURE is the value returned upon failure of MORECORE | 
|  | 434 | as well as mmap. Since it cannot be an otherwise valid memory address, | 
|  | 435 | and must reflect values of standard sys calls, you probably ought not | 
|  | 436 | try to redefine it. | 
|  | 437 | */ | 
|  | 438 |  | 
|  | 439 | #ifndef MORECORE_FAILURE | 
|  | 440 | #define MORECORE_FAILURE (-1) | 
|  | 441 | #endif | 
|  | 442 |  | 
|  | 443 | /* | 
|  | 444 | If MORECORE_CONTIGUOUS is true, take advantage of fact that | 
|  | 445 | consecutive calls to MORECORE with positive arguments always return | 
|  | 446 | contiguous increasing addresses.  This is true of unix sbrk.  Even | 
|  | 447 | if not defined, when regions happen to be contiguous, malloc will | 
|  | 448 | permit allocations spanning regions obtained from different | 
|  | 449 | calls. But defining this when applicable enables some stronger | 
|  | 450 | consistency checks and space efficiencies. | 
|  | 451 | */ | 
|  | 452 |  | 
|  | 453 | #ifndef MORECORE_CONTIGUOUS | 
|  | 454 | #define MORECORE_CONTIGUOUS 1 | 
|  | 455 | #endif | 
|  | 456 |  | 
|  | 457 | /* | 
|  | 458 | Define MORECORE_CANNOT_TRIM if your version of MORECORE | 
|  | 459 | cannot release space back to the system when given negative | 
|  | 460 | arguments. This is generally necessary only if you are using | 
|  | 461 | a hand-crafted MORECORE function that cannot handle negative arguments. | 
|  | 462 | */ | 
|  | 463 |  | 
|  | 464 | /* #define MORECORE_CANNOT_TRIM */ | 
|  | 465 |  | 
|  | 466 | /*  MORECORE_CLEARS           (default 1) | 
|  | 467 | The degree to which the routine mapped to MORECORE zeroes out | 
|  | 468 | memory: never (0), only for newly allocated space (1) or always | 
|  | 469 | (2).  The distinction between (1) and (2) is necessary because on | 
|  | 470 | some systems, if the application first decrements and then | 
|  | 471 | increments the break value, the contents of the reallocated space | 
|  | 472 | are unspecified. | 
|  | 473 | */ | 
|  | 474 |  | 
|  | 475 | #ifndef MORECORE_CLEARS | 
|  | 476 | # define MORECORE_CLEARS 1 | 
|  | 477 | #endif | 
|  | 478 |  | 
|  | 479 |  | 
|  | 480 | /* | 
|  | 481 | MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if | 
|  | 482 | sbrk fails, and mmap is used as a backup.  The value must be a | 
|  | 483 | multiple of page size.  This backup strategy generally applies only | 
|  | 484 | when systems have "holes" in address space, so sbrk cannot perform | 
|  | 485 | contiguous expansion, but there is still space available on system. | 
|  | 486 | On systems for which this is known to be useful (i.e. most linux | 
|  | 487 | kernels), this occurs only when programs allocate huge amounts of | 
|  | 488 | memory.  Between this, and the fact that mmap regions tend to be | 
|  | 489 | limited, the size should be large, to avoid too many mmap calls and | 
|  | 490 | thus avoid running out of kernel resources.  */ | 
|  | 491 |  | 
|  | 492 | #ifndef MMAP_AS_MORECORE_SIZE | 
|  | 493 | #define MMAP_AS_MORECORE_SIZE (1024 * 1024) | 
|  | 494 | #endif | 
|  | 495 |  | 
|  | 496 | /* | 
|  | 497 | Define HAVE_MREMAP to make realloc() use mremap() to re-allocate | 
|  | 498 | large blocks. | 
|  | 499 | */ | 
|  | 500 |  | 
|  | 501 | #ifndef HAVE_MREMAP | 
|  | 502 | #define HAVE_MREMAP 0 | 
|  | 503 | #endif | 
|  | 504 |  | 
|  | 505 |  | 
|  | 506 | /* | 
|  | 507 | This version of malloc supports the standard SVID/XPG mallinfo | 
|  | 508 | routine that returns a struct containing usage properties and | 
|  | 509 | statistics. It should work on any SVID/XPG compliant system that has | 
|  | 510 | a /usr/include/malloc.h defining struct mallinfo. (If you'd like to | 
|  | 511 | install such a thing yourself, cut out the preliminary declarations | 
|  | 512 | as described above and below and save them in a malloc.h file. But | 
|  | 513 | there's no compelling reason to bother to do this.) | 
|  | 514 |  | 
|  | 515 | The main declaration needed is the mallinfo struct that is returned | 
|  | 516 | (by-copy) by mallinfo().  The SVID/XPG malloinfo struct contains a | 
|  | 517 | bunch of fields that are not even meaningful in this version of | 
|  | 518 | malloc.  These fields are are instead filled by mallinfo() with | 
|  | 519 | other numbers that might be of interest. | 
|  | 520 | */ | 
|  | 521 |  | 
|  | 522 |  | 
|  | 523 | /* ---------- description of public routines ------------ */ | 
|  | 524 |  | 
|  | 525 | /* | 
|  | 526 | malloc(size_t n) | 
|  | 527 | Returns a pointer to a newly allocated chunk of at least n bytes, or null | 
|  | 528 | if no space is available. Additionally, on failure, errno is | 
|  | 529 | set to ENOMEM on ANSI C systems. | 
|  | 530 |  | 
|  | 531 | If n is zero, malloc returns a minumum-sized chunk. (The minimum | 
|  | 532 | size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit | 
|  | 533 | systems.)  On most systems, size_t is an unsigned type, so calls | 
|  | 534 | with negative arguments are interpreted as requests for huge amounts | 
|  | 535 | of space, which will often fail. The maximum supported value of n | 
|  | 536 | differs across systems, but is in all cases less than the maximum | 
|  | 537 | representable value of a size_t. | 
|  | 538 | */ | 
|  | 539 | void*  __libc_malloc(size_t); | 
|  | 540 | libc_hidden_proto (__libc_malloc) | 
|  | 541 |  | 
|  | 542 | /* | 
|  | 543 | free(void* p) | 
|  | 544 | Releases the chunk of memory pointed to by p, that had been previously | 
|  | 545 | allocated using malloc or a related routine such as realloc. | 
|  | 546 | It has no effect if p is null. It can have arbitrary (i.e., bad!) | 
|  | 547 | effects if p has already been freed. | 
|  | 548 |  | 
|  | 549 | Unless disabled (using mallopt), freeing very large spaces will | 
|  | 550 | when possible, automatically trigger operations that give | 
|  | 551 | back unused memory to the system, thus reducing program footprint. | 
|  | 552 | */ | 
|  | 553 | void     __libc_free(void*); | 
|  | 554 | libc_hidden_proto (__libc_free) | 
|  | 555 |  | 
|  | 556 | /* | 
|  | 557 | calloc(size_t n_elements, size_t element_size); | 
|  | 558 | Returns a pointer to n_elements * element_size bytes, with all locations | 
|  | 559 | set to zero. | 
|  | 560 | */ | 
|  | 561 | void*  __libc_calloc(size_t, size_t); | 
|  | 562 |  | 
|  | 563 | /* | 
|  | 564 | realloc(void* p, size_t n) | 
|  | 565 | Returns a pointer to a chunk of size n that contains the same data | 
|  | 566 | as does chunk p up to the minimum of (n, p's size) bytes, or null | 
|  | 567 | if no space is available. | 
|  | 568 |  | 
|  | 569 | The returned pointer may or may not be the same as p. The algorithm | 
|  | 570 | prefers extending p when possible, otherwise it employs the | 
|  | 571 | equivalent of a malloc-copy-free sequence. | 
|  | 572 |  | 
|  | 573 | If p is null, realloc is equivalent to malloc. | 
|  | 574 |  | 
|  | 575 | If space is not available, realloc returns null, errno is set (if on | 
|  | 576 | ANSI) and p is NOT freed. | 
|  | 577 |  | 
|  | 578 | if n is for fewer bytes than already held by p, the newly unused | 
|  | 579 | space is lopped off and freed if possible.  Unless the #define | 
|  | 580 | REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of | 
|  | 581 | zero (re)allocates a minimum-sized chunk. | 
|  | 582 |  | 
|  | 583 | Large chunks that were internally obtained via mmap will always | 
|  | 584 | be reallocated using malloc-copy-free sequences unless | 
|  | 585 | the system supports MREMAP (currently only linux). | 
|  | 586 |  | 
|  | 587 | The old unix realloc convention of allowing the last-free'd chunk | 
|  | 588 | to be used as an argument to realloc is not supported. | 
|  | 589 | */ | 
|  | 590 | void*  __libc_realloc(void*, size_t); | 
|  | 591 | libc_hidden_proto (__libc_realloc) | 
|  | 592 |  | 
|  | 593 | /* | 
|  | 594 | memalign(size_t alignment, size_t n); | 
|  | 595 | Returns a pointer to a newly allocated chunk of n bytes, aligned | 
|  | 596 | in accord with the alignment argument. | 
|  | 597 |  | 
|  | 598 | The alignment argument should be a power of two. If the argument is | 
|  | 599 | not a power of two, the nearest greater power is used. | 
|  | 600 | 8-byte alignment is guaranteed by normal malloc calls, so don't | 
|  | 601 | bother calling memalign with an argument of 8 or less. | 
|  | 602 |  | 
|  | 603 | Overreliance on memalign is a sure way to fragment space. | 
|  | 604 | */ | 
|  | 605 | void*  __libc_memalign(size_t, size_t); | 
|  | 606 | libc_hidden_proto (__libc_memalign) | 
|  | 607 |  | 
|  | 608 | /* | 
|  | 609 | valloc(size_t n); | 
|  | 610 | Equivalent to memalign(pagesize, n), where pagesize is the page | 
|  | 611 | size of the system. If the pagesize is unknown, 4096 is used. | 
|  | 612 | */ | 
|  | 613 | void*  __libc_valloc(size_t); | 
|  | 614 |  | 
|  | 615 |  | 
|  | 616 |  | 
|  | 617 | /* | 
|  | 618 | mallopt(int parameter_number, int parameter_value) | 
|  | 619 | Sets tunable parameters The format is to provide a | 
|  | 620 | (parameter-number, parameter-value) pair.  mallopt then sets the | 
|  | 621 | corresponding parameter to the argument value if it can (i.e., so | 
|  | 622 | long as the value is meaningful), and returns 1 if successful else | 
|  | 623 | 0.  SVID/XPG/ANSI defines four standard param numbers for mallopt, | 
|  | 624 | normally defined in malloc.h.  Only one of these (M_MXFAST) is used | 
|  | 625 | in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply, | 
|  | 626 | so setting them has no effect. But this malloc also supports four | 
|  | 627 | other options in mallopt. See below for details.  Briefly, supported | 
|  | 628 | parameters are as follows (listed defaults are for "typical" | 
|  | 629 | configurations). | 
|  | 630 |  | 
|  | 631 | Symbol            param #   default    allowed param values | 
|  | 632 | M_MXFAST          1         64         0-80  (0 disables fastbins) | 
|  | 633 | M_TRIM_THRESHOLD -1         128*1024   any   (-1U disables trimming) | 
|  | 634 | M_TOP_PAD        -2         0          any | 
|  | 635 | M_MMAP_THRESHOLD -3         128*1024   any   (or 0 if no MMAP support) | 
|  | 636 | M_MMAP_MAX       -4         65536      any   (0 disables use of mmap) | 
|  | 637 | */ | 
|  | 638 | int      __libc_mallopt(int, int); | 
|  | 639 | libc_hidden_proto (__libc_mallopt) | 
|  | 640 |  | 
|  | 641 |  | 
|  | 642 | /* | 
|  | 643 | mallinfo() | 
|  | 644 | Returns (by copy) a struct containing various summary statistics: | 
|  | 645 |  | 
|  | 646 | arena:     current total non-mmapped bytes allocated from system | 
|  | 647 | ordblks:   the number of free chunks | 
|  | 648 | smblks:    the number of fastbin blocks (i.e., small chunks that | 
|  | 649 | have been freed but not use resused or consolidated) | 
|  | 650 | hblks:     current number of mmapped regions | 
|  | 651 | hblkhd:    total bytes held in mmapped regions | 
|  | 652 | usmblks:   the maximum total allocated space. This will be greater | 
|  | 653 | than current total if trimming has occurred. | 
|  | 654 | fsmblks:   total bytes held in fastbin blocks | 
|  | 655 | uordblks:  current total allocated space (normal or mmapped) | 
|  | 656 | fordblks:  total free space | 
|  | 657 | keepcost:  the maximum number of bytes that could ideally be released | 
|  | 658 | back to system via malloc_trim. ("ideally" means that | 
|  | 659 | it ignores page restrictions etc.) | 
|  | 660 |  | 
|  | 661 | Because these fields are ints, but internal bookkeeping may | 
|  | 662 | be kept as longs, the reported values may wrap around zero and | 
|  | 663 | thus be inaccurate. | 
|  | 664 | */ | 
|  | 665 | struct mallinfo __libc_mallinfo(void); | 
|  | 666 |  | 
|  | 667 |  | 
|  | 668 | /* | 
|  | 669 | pvalloc(size_t n); | 
|  | 670 | Equivalent to valloc(minimum-page-that-holds(n)), that is, | 
|  | 671 | round up n to nearest pagesize. | 
|  | 672 | */ | 
|  | 673 | void*  __libc_pvalloc(size_t); | 
|  | 674 |  | 
|  | 675 | /* | 
|  | 676 | malloc_trim(size_t pad); | 
|  | 677 |  | 
|  | 678 | If possible, gives memory back to the system (via negative | 
|  | 679 | arguments to sbrk) if there is unused memory at the `high' end of | 
|  | 680 | the malloc pool. You can call this after freeing large blocks of | 
|  | 681 | memory to potentially reduce the system-level memory requirements | 
|  | 682 | of a program. However, it cannot guarantee to reduce memory. Under | 
|  | 683 | some allocation patterns, some large free blocks of memory will be | 
|  | 684 | locked between two used chunks, so they cannot be given back to | 
|  | 685 | the system. | 
|  | 686 |  | 
|  | 687 | The `pad' argument to malloc_trim represents the amount of free | 
|  | 688 | trailing space to leave untrimmed. If this argument is zero, | 
|  | 689 | only the minimum amount of memory to maintain internal data | 
|  | 690 | structures will be left (one page or less). Non-zero arguments | 
|  | 691 | can be supplied to maintain enough trailing space to service | 
|  | 692 | future expected allocations without having to re-obtain memory | 
|  | 693 | from the system. | 
|  | 694 |  | 
|  | 695 | Malloc_trim returns 1 if it actually released any memory, else 0. | 
|  | 696 | On systems that do not support "negative sbrks", it will always | 
|  | 697 | return 0. | 
|  | 698 | */ | 
|  | 699 | int      __malloc_trim(size_t); | 
|  | 700 |  | 
|  | 701 | /* | 
|  | 702 | malloc_usable_size(void* p); | 
|  | 703 |  | 
|  | 704 | Returns the number of bytes you can actually use in | 
|  | 705 | an allocated chunk, which may be more than you requested (although | 
|  | 706 | often not) due to alignment and minimum size constraints. | 
|  | 707 | You can use this many bytes without worrying about | 
|  | 708 | overwriting other allocated objects. This is not a particularly great | 
|  | 709 | programming practice. malloc_usable_size can be more useful in | 
|  | 710 | debugging and assertions, for example: | 
|  | 711 |  | 
|  | 712 | p = malloc(n); | 
|  | 713 | assert(malloc_usable_size(p) >= 256); | 
|  | 714 |  | 
|  | 715 | */ | 
|  | 716 | size_t   __malloc_usable_size(void*); | 
|  | 717 |  | 
|  | 718 | /* | 
|  | 719 | malloc_stats(); | 
|  | 720 | Prints on stderr the amount of space obtained from the system (both | 
|  | 721 | via sbrk and mmap), the maximum amount (which may be more than | 
|  | 722 | current if malloc_trim and/or munmap got called), and the current | 
|  | 723 | number of bytes allocated via malloc (or realloc, etc) but not yet | 
|  | 724 | freed. Note that this is the number of bytes allocated, not the | 
|  | 725 | number requested. It will be larger than the number requested | 
|  | 726 | because of alignment and bookkeeping overhead. Because it includes | 
|  | 727 | alignment wastage as being in use, this figure may be greater than | 
|  | 728 | zero even when no user-level chunks are allocated. | 
|  | 729 |  | 
|  | 730 | The reported current and maximum system memory can be inaccurate if | 
|  | 731 | a program makes other calls to system memory allocation functions | 
|  | 732 | (normally sbrk) outside of malloc. | 
|  | 733 |  | 
|  | 734 | malloc_stats prints only the most commonly interesting statistics. | 
|  | 735 | More information can be obtained by calling mallinfo. | 
|  | 736 |  | 
|  | 737 | */ | 
|  | 738 | void     __malloc_stats(void); | 
|  | 739 |  | 
|  | 740 | /* | 
|  | 741 | malloc_get_state(void); | 
|  | 742 |  | 
|  | 743 | Returns the state of all malloc variables in an opaque data | 
|  | 744 | structure. | 
|  | 745 | */ | 
|  | 746 | void*  __malloc_get_state(void); | 
|  | 747 |  | 
|  | 748 | /* | 
|  | 749 | malloc_set_state(void* state); | 
|  | 750 |  | 
|  | 751 | Restore the state of all malloc variables from data obtained with | 
|  | 752 | malloc_get_state(). | 
|  | 753 | */ | 
|  | 754 | int      __malloc_set_state(void*); | 
|  | 755 |  | 
|  | 756 | /* | 
|  | 757 | posix_memalign(void **memptr, size_t alignment, size_t size); | 
|  | 758 |  | 
|  | 759 | POSIX wrapper like memalign(), checking for validity of size. | 
|  | 760 | */ | 
|  | 761 | int      __posix_memalign(void **, size_t, size_t); | 
|  | 762 |  | 
|  | 763 | /* mallopt tuning options */ | 
|  | 764 |  | 
|  | 765 | /* | 
|  | 766 | M_MXFAST is the maximum request size used for "fastbins", special bins | 
|  | 767 | that hold returned chunks without consolidating their spaces. This | 
|  | 768 | enables future requests for chunks of the same size to be handled | 
|  | 769 | very quickly, but can increase fragmentation, and thus increase the | 
|  | 770 | overall memory footprint of a program. | 
|  | 771 |  | 
|  | 772 | This malloc manages fastbins very conservatively yet still | 
|  | 773 | efficiently, so fragmentation is rarely a problem for values less | 
|  | 774 | than or equal to the default.  The maximum supported value of MXFAST | 
|  | 775 | is 80. You wouldn't want it any higher than this anyway.  Fastbins | 
|  | 776 | are designed especially for use with many small structs, objects or | 
|  | 777 | strings -- the default handles structs/objects/arrays with sizes up | 
|  | 778 | to 8 4byte fields, or small strings representing words, tokens, | 
|  | 779 | etc. Using fastbins for larger objects normally worsens | 
|  | 780 | fragmentation without improving speed. | 
|  | 781 |  | 
|  | 782 | M_MXFAST is set in REQUEST size units. It is internally used in | 
|  | 783 | chunksize units, which adds padding and alignment.  You can reduce | 
|  | 784 | M_MXFAST to 0 to disable all use of fastbins.  This causes the malloc | 
|  | 785 | algorithm to be a closer approximation of fifo-best-fit in all cases, | 
|  | 786 | not just for larger requests, but will generally cause it to be | 
|  | 787 | slower. | 
|  | 788 | */ | 
|  | 789 |  | 
|  | 790 |  | 
|  | 791 | /* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */ | 
|  | 792 | #ifndef M_MXFAST | 
|  | 793 | #define M_MXFAST            1 | 
|  | 794 | #endif | 
|  | 795 |  | 
|  | 796 | #ifndef DEFAULT_MXFAST | 
|  | 797 | #define DEFAULT_MXFAST     (64 * SIZE_SZ / 4) | 
|  | 798 | #endif | 
|  | 799 |  | 
|  | 800 |  | 
|  | 801 | /* | 
|  | 802 | M_TRIM_THRESHOLD is the maximum amount of unused top-most memory | 
|  | 803 | to keep before releasing via malloc_trim in free(). | 
|  | 804 |  | 
|  | 805 | Automatic trimming is mainly useful in long-lived programs. | 
|  | 806 | Because trimming via sbrk can be slow on some systems, and can | 
|  | 807 | sometimes be wasteful (in cases where programs immediately | 
|  | 808 | afterward allocate more large chunks) the value should be high | 
|  | 809 | enough so that your overall system performance would improve by | 
|  | 810 | releasing this much memory. | 
|  | 811 |  | 
|  | 812 | The trim threshold and the mmap control parameters (see below) | 
|  | 813 | can be traded off with one another. Trimming and mmapping are | 
|  | 814 | two different ways of releasing unused memory back to the | 
|  | 815 | system. Between these two, it is often possible to keep | 
|  | 816 | system-level demands of a long-lived program down to a bare | 
|  | 817 | minimum. For example, in one test suite of sessions measuring | 
|  | 818 | the XF86 X server on Linux, using a trim threshold of 128K and a | 
|  | 819 | mmap threshold of 192K led to near-minimal long term resource | 
|  | 820 | consumption. | 
|  | 821 |  | 
|  | 822 | If you are using this malloc in a long-lived program, it should | 
|  | 823 | pay to experiment with these values.  As a rough guide, you | 
|  | 824 | might set to a value close to the average size of a process | 
|  | 825 | (program) running on your system.  Releasing this much memory | 
|  | 826 | would allow such a process to run in memory.  Generally, it's | 
|  | 827 | worth it to tune for trimming rather tham memory mapping when a | 
|  | 828 | program undergoes phases where several large chunks are | 
|  | 829 | allocated and released in ways that can reuse each other's | 
|  | 830 | storage, perhaps mixed with phases where there are no such | 
|  | 831 | chunks at all.  And in well-behaved long-lived programs, | 
|  | 832 | controlling release of large blocks via trimming versus mapping | 
|  | 833 | is usually faster. | 
|  | 834 |  | 
|  | 835 | However, in most programs, these parameters serve mainly as | 
|  | 836 | protection against the system-level effects of carrying around | 
|  | 837 | massive amounts of unneeded memory. Since frequent calls to | 
|  | 838 | sbrk, mmap, and munmap otherwise degrade performance, the default | 
|  | 839 | parameters are set to relatively high values that serve only as | 
|  | 840 | safeguards. | 
|  | 841 |  | 
|  | 842 | The trim value It must be greater than page size to have any useful | 
|  | 843 | effect.  To disable trimming completely, you can set to | 
|  | 844 | (unsigned long)(-1) | 
|  | 845 |  | 
|  | 846 | Trim settings interact with fastbin (MXFAST) settings: Unless | 
|  | 847 | TRIM_FASTBINS is defined, automatic trimming never takes place upon | 
|  | 848 | freeing a chunk with size less than or equal to MXFAST. Trimming is | 
|  | 849 | instead delayed until subsequent freeing of larger chunks. However, | 
|  | 850 | you can still force an attempted trim by calling malloc_trim. | 
|  | 851 |  | 
|  | 852 | Also, trimming is not generally possible in cases where | 
|  | 853 | the main arena is obtained via mmap. | 
|  | 854 |  | 
|  | 855 | Note that the trick some people use of mallocing a huge space and | 
|  | 856 | then freeing it at program startup, in an attempt to reserve system | 
|  | 857 | memory, doesn't have the intended effect under automatic trimming, | 
|  | 858 | since that memory will immediately be returned to the system. | 
|  | 859 | */ | 
|  | 860 |  | 
|  | 861 | #define M_TRIM_THRESHOLD       -1 | 
|  | 862 |  | 
|  | 863 | #ifndef DEFAULT_TRIM_THRESHOLD | 
|  | 864 | #define DEFAULT_TRIM_THRESHOLD (128 * 1024) | 
|  | 865 | #endif | 
|  | 866 |  | 
|  | 867 | /* | 
|  | 868 | M_TOP_PAD is the amount of extra `padding' space to allocate or | 
|  | 869 | retain whenever sbrk is called. It is used in two ways internally: | 
|  | 870 |  | 
|  | 871 | * When sbrk is called to extend the top of the arena to satisfy | 
|  | 872 | a new malloc request, this much padding is added to the sbrk | 
|  | 873 | request. | 
|  | 874 |  | 
|  | 875 | * When malloc_trim is called automatically from free(), | 
|  | 876 | it is used as the `pad' argument. | 
|  | 877 |  | 
|  | 878 | In both cases, the actual amount of padding is rounded | 
|  | 879 | so that the end of the arena is always a system page boundary. | 
|  | 880 |  | 
|  | 881 | The main reason for using padding is to avoid calling sbrk so | 
|  | 882 | often. Having even a small pad greatly reduces the likelihood | 
|  | 883 | that nearly every malloc request during program start-up (or | 
|  | 884 | after trimming) will invoke sbrk, which needlessly wastes | 
|  | 885 | time. | 
|  | 886 |  | 
|  | 887 | Automatic rounding-up to page-size units is normally sufficient | 
|  | 888 | to avoid measurable overhead, so the default is 0.  However, in | 
|  | 889 | systems where sbrk is relatively slow, it can pay to increase | 
|  | 890 | this value, at the expense of carrying around more memory than | 
|  | 891 | the program needs. | 
|  | 892 | */ | 
|  | 893 |  | 
|  | 894 | #define M_TOP_PAD              -2 | 
|  | 895 |  | 
|  | 896 | #ifndef DEFAULT_TOP_PAD | 
|  | 897 | #define DEFAULT_TOP_PAD        (0) | 
|  | 898 | #endif | 
|  | 899 |  | 
|  | 900 | /* | 
|  | 901 | MMAP_THRESHOLD_MAX and _MIN are the bounds on the dynamically | 
|  | 902 | adjusted MMAP_THRESHOLD. | 
|  | 903 | */ | 
|  | 904 |  | 
|  | 905 | #ifndef DEFAULT_MMAP_THRESHOLD_MIN | 
|  | 906 | #define DEFAULT_MMAP_THRESHOLD_MIN (128 * 1024) | 
|  | 907 | #endif | 
|  | 908 |  | 
|  | 909 | #ifndef DEFAULT_MMAP_THRESHOLD_MAX | 
|  | 910 | /* For 32-bit platforms we cannot increase the maximum mmap | 
|  | 911 | threshold much because it is also the minimum value for the | 
|  | 912 | maximum heap size and its alignment.  Going above 512k (i.e., 1M | 
|  | 913 | for new heaps) wastes too much address space.  */ | 
|  | 914 | # if __WORDSIZE == 32 | 
|  | 915 | #  define DEFAULT_MMAP_THRESHOLD_MAX (512 * 1024) | 
|  | 916 | # else | 
|  | 917 | #  define DEFAULT_MMAP_THRESHOLD_MAX (4 * 1024 * 1024 * sizeof(long)) | 
|  | 918 | # endif | 
|  | 919 | #endif | 
|  | 920 |  | 
|  | 921 | /* | 
|  | 922 | M_MMAP_THRESHOLD is the request size threshold for using mmap() | 
|  | 923 | to service a request. Requests of at least this size that cannot | 
|  | 924 | be allocated using already-existing space will be serviced via mmap. | 
|  | 925 | (If enough normal freed space already exists it is used instead.) | 
|  | 926 |  | 
|  | 927 | Using mmap segregates relatively large chunks of memory so that | 
|  | 928 | they can be individually obtained and released from the host | 
|  | 929 | system. A request serviced through mmap is never reused by any | 
|  | 930 | other request (at least not directly; the system may just so | 
|  | 931 | happen to remap successive requests to the same locations). | 
|  | 932 |  | 
|  | 933 | Segregating space in this way has the benefits that: | 
|  | 934 |  | 
|  | 935 | 1. Mmapped space can ALWAYS be individually released back | 
|  | 936 | to the system, which helps keep the system level memory | 
|  | 937 | demands of a long-lived program low. | 
|  | 938 | 2. Mapped memory can never become `locked' between | 
|  | 939 | other chunks, as can happen with normally allocated chunks, which | 
|  | 940 | means that even trimming via malloc_trim would not release them. | 
|  | 941 | 3. On some systems with "holes" in address spaces, mmap can obtain | 
|  | 942 | memory that sbrk cannot. | 
|  | 943 |  | 
|  | 944 | However, it has the disadvantages that: | 
|  | 945 |  | 
|  | 946 | 1. The space cannot be reclaimed, consolidated, and then | 
|  | 947 | used to service later requests, as happens with normal chunks. | 
|  | 948 | 2. It can lead to more wastage because of mmap page alignment | 
|  | 949 | requirements | 
|  | 950 | 3. It causes malloc performance to be more dependent on host | 
|  | 951 | system memory management support routines which may vary in | 
|  | 952 | implementation quality and may impose arbitrary | 
|  | 953 | limitations. Generally, servicing a request via normal | 
|  | 954 | malloc steps is faster than going through a system's mmap. | 
|  | 955 |  | 
|  | 956 | The advantages of mmap nearly always outweigh disadvantages for | 
|  | 957 | "large" chunks, but the value of "large" varies across systems.  The | 
|  | 958 | default is an empirically derived value that works well in most | 
|  | 959 | systems. | 
|  | 960 |  | 
|  | 961 |  | 
|  | 962 | Update in 2006: | 
|  | 963 | The above was written in 2001. Since then the world has changed a lot. | 
|  | 964 | Memory got bigger. Applications got bigger. The virtual address space | 
|  | 965 | layout in 32 bit linux changed. | 
|  | 966 |  | 
|  | 967 | In the new situation, brk() and mmap space is shared and there are no | 
|  | 968 | artificial limits on brk size imposed by the kernel. What is more, | 
|  | 969 | applications have started using transient allocations larger than the | 
|  | 970 | 128Kb as was imagined in 2001. | 
|  | 971 |  | 
|  | 972 | The price for mmap is also high now; each time glibc mmaps from the | 
|  | 973 | kernel, the kernel is forced to zero out the memory it gives to the | 
|  | 974 | application. Zeroing memory is expensive and eats a lot of cache and | 
|  | 975 | memory bandwidth. This has nothing to do with the efficiency of the | 
|  | 976 | virtual memory system, by doing mmap the kernel just has no choice but | 
|  | 977 | to zero. | 
|  | 978 |  | 
|  | 979 | In 2001, the kernel had a maximum size for brk() which was about 800 | 
|  | 980 | megabytes on 32 bit x86, at that point brk() would hit the first | 
|  | 981 | mmaped shared libaries and couldn't expand anymore. With current 2.6 | 
|  | 982 | kernels, the VA space layout is different and brk() and mmap | 
|  | 983 | both can span the entire heap at will. | 
|  | 984 |  | 
|  | 985 | Rather than using a static threshold for the brk/mmap tradeoff, | 
|  | 986 | we are now using a simple dynamic one. The goal is still to avoid | 
|  | 987 | fragmentation. The old goals we kept are | 
|  | 988 | 1) try to get the long lived large allocations to use mmap() | 
|  | 989 | 2) really large allocations should always use mmap() | 
|  | 990 | and we're adding now: | 
|  | 991 | 3) transient allocations should use brk() to avoid forcing the kernel | 
|  | 992 | having to zero memory over and over again | 
|  | 993 |  | 
|  | 994 | The implementation works with a sliding threshold, which is by default | 
|  | 995 | limited to go between 128Kb and 32Mb (64Mb for 64 bitmachines) and starts | 
|  | 996 | out at 128Kb as per the 2001 default. | 
|  | 997 |  | 
|  | 998 | This allows us to satisfy requirement 1) under the assumption that long | 
|  | 999 | lived allocations are made early in the process' lifespan, before it has | 
|  | 1000 | started doing dynamic allocations of the same size (which will | 
|  | 1001 | increase the threshold). | 
|  | 1002 |  | 
|  | 1003 | The upperbound on the threshold satisfies requirement 2) | 
|  | 1004 |  | 
|  | 1005 | The threshold goes up in value when the application frees memory that was | 
|  | 1006 | allocated with the mmap allocator. The idea is that once the application | 
|  | 1007 | starts freeing memory of a certain size, it's highly probable that this is | 
|  | 1008 | a size the application uses for transient allocations. This estimator | 
|  | 1009 | is there to satisfy the new third requirement. | 
|  | 1010 |  | 
|  | 1011 | */ | 
|  | 1012 |  | 
|  | 1013 | #define M_MMAP_THRESHOLD      -3 | 
|  | 1014 |  | 
|  | 1015 | #ifndef DEFAULT_MMAP_THRESHOLD | 
|  | 1016 | #define DEFAULT_MMAP_THRESHOLD DEFAULT_MMAP_THRESHOLD_MIN | 
|  | 1017 | #endif | 
|  | 1018 |  | 
|  | 1019 | /* | 
|  | 1020 | M_MMAP_MAX is the maximum number of requests to simultaneously | 
|  | 1021 | service using mmap. This parameter exists because | 
|  | 1022 | some systems have a limited number of internal tables for | 
|  | 1023 | use by mmap, and using more than a few of them may degrade | 
|  | 1024 | performance. | 
|  | 1025 |  | 
|  | 1026 | The default is set to a value that serves only as a safeguard. | 
|  | 1027 | Setting to 0 disables use of mmap for servicing large requests. | 
|  | 1028 | */ | 
|  | 1029 |  | 
|  | 1030 | #define M_MMAP_MAX             -4 | 
|  | 1031 |  | 
|  | 1032 | #ifndef DEFAULT_MMAP_MAX | 
|  | 1033 | #define DEFAULT_MMAP_MAX       (65536) | 
|  | 1034 | #endif | 
|  | 1035 |  | 
|  | 1036 | #include <malloc.h> | 
|  | 1037 |  | 
|  | 1038 | #ifndef RETURN_ADDRESS | 
|  | 1039 | #define RETURN_ADDRESS(X_) (NULL) | 
|  | 1040 | #endif | 
|  | 1041 |  | 
|  | 1042 | /* On some platforms we can compile internal, not exported functions better. | 
|  | 1043 | Let the environment provide a macro and define it to be empty if it | 
|  | 1044 | is not available.  */ | 
|  | 1045 | #ifndef internal_function | 
|  | 1046 | # define internal_function | 
|  | 1047 | #endif | 
|  | 1048 |  | 
|  | 1049 | /* Forward declarations.  */ | 
|  | 1050 | struct malloc_chunk; | 
|  | 1051 | typedef struct malloc_chunk* mchunkptr; | 
|  | 1052 |  | 
|  | 1053 | /* Internal routines.  */ | 
|  | 1054 |  | 
|  | 1055 | static void*  _int_malloc(mstate, size_t); | 
|  | 1056 | static void     _int_free(mstate, mchunkptr, int); | 
|  | 1057 | static void*  _int_realloc(mstate, mchunkptr, INTERNAL_SIZE_T, | 
|  | 1058 | INTERNAL_SIZE_T); | 
|  | 1059 | static void*  _int_memalign(mstate, size_t, size_t); | 
|  | 1060 | static void*  _mid_memalign(size_t, size_t, void *); | 
|  | 1061 |  | 
|  | 1062 | static void malloc_printerr(int action, const char *str, void *ptr, mstate av); | 
|  | 1063 |  | 
|  | 1064 | static void* internal_function mem2mem_check(void *p, size_t sz); | 
|  | 1065 | static int internal_function top_check(void); | 
|  | 1066 | static void internal_function munmap_chunk(mchunkptr p); | 
|  | 1067 | #if HAVE_MREMAP | 
|  | 1068 | static mchunkptr internal_function mremap_chunk(mchunkptr p, size_t new_size); | 
|  | 1069 | #endif | 
|  | 1070 |  | 
|  | 1071 | static void*   malloc_check(size_t sz, const void *caller); | 
|  | 1072 | static void      free_check(void* mem, const void *caller); | 
|  | 1073 | static void*   realloc_check(void* oldmem, size_t bytes, | 
|  | 1074 | const void *caller); | 
|  | 1075 | static void*   memalign_check(size_t alignment, size_t bytes, | 
|  | 1076 | const void *caller); | 
|  | 1077 | #ifndef NO_THREADS | 
|  | 1078 | static void*   malloc_atfork(size_t sz, const void *caller); | 
|  | 1079 | static void      free_atfork(void* mem, const void *caller); | 
|  | 1080 | #endif | 
|  | 1081 |  | 
|  | 1082 | /* ------------------ MMAP support ------------------  */ | 
|  | 1083 |  | 
|  | 1084 |  | 
|  | 1085 | #include <fcntl.h> | 
|  | 1086 | #include <sys/mman.h> | 
|  | 1087 |  | 
|  | 1088 | #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) | 
|  | 1089 | # define MAP_ANONYMOUS MAP_ANON | 
|  | 1090 | #endif | 
|  | 1091 |  | 
|  | 1092 | #ifndef MAP_NORESERVE | 
|  | 1093 | # define MAP_NORESERVE 0 | 
|  | 1094 | #endif | 
|  | 1095 |  | 
|  | 1096 | #define MMAP(addr, size, prot, flags) \ | 
|  | 1097 | __mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0) | 
|  | 1098 |  | 
|  | 1099 |  | 
|  | 1100 | /* | 
|  | 1101 | -----------------------  Chunk representations ----------------------- | 
|  | 1102 | */ | 
|  | 1103 |  | 
|  | 1104 |  | 
|  | 1105 | /* | 
|  | 1106 | This struct declaration is misleading (but accurate and necessary). | 
|  | 1107 | It declares a "view" into memory allowing access to necessary | 
|  | 1108 | fields at known offsets from a given base. See explanation below. | 
|  | 1109 | */ | 
|  | 1110 |  | 
|  | 1111 | struct malloc_chunk { | 
|  | 1112 |  | 
|  | 1113 | INTERNAL_SIZE_T      prev_size;  /* Size of previous chunk (if free).  */ | 
|  | 1114 | INTERNAL_SIZE_T      size;       /* Size in bytes, including overhead. */ | 
|  | 1115 |  | 
|  | 1116 | struct malloc_chunk* fd;         /* double links -- used only if free. */ | 
|  | 1117 | struct malloc_chunk* bk; | 
|  | 1118 |  | 
|  | 1119 | /* Only used for large blocks: pointer to next larger size.  */ | 
|  | 1120 | struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */ | 
|  | 1121 | struct malloc_chunk* bk_nextsize; | 
|  | 1122 | }; | 
|  | 1123 |  | 
|  | 1124 |  | 
|  | 1125 | /* | 
|  | 1126 | malloc_chunk details: | 
|  | 1127 |  | 
|  | 1128 | (The following includes lightly edited explanations by Colin Plumb.) | 
|  | 1129 |  | 
|  | 1130 | Chunks of memory are maintained using a `boundary tag' method as | 
|  | 1131 | described in e.g., Knuth or Standish.  (See the paper by Paul | 
|  | 1132 | Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a | 
|  | 1133 | survey of such techniques.)  Sizes of free chunks are stored both | 
|  | 1134 | in the front of each chunk and at the end.  This makes | 
|  | 1135 | consolidating fragmented chunks into bigger chunks very fast.  The | 
|  | 1136 | size fields also hold bits representing whether chunks are free or | 
|  | 1137 | in use. | 
|  | 1138 |  | 
|  | 1139 | An allocated chunk looks like this: | 
|  | 1140 |  | 
|  | 1141 |  | 
|  | 1142 | chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1143 | |             Size of previous chunk, if allocated            | | | 
|  | 1144 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1145 | |             Size of chunk, in bytes                       |M|P| | 
|  | 1146 | mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1147 | |             User data starts here...                          . | 
|  | 1148 | .                                                               . | 
|  | 1149 | .             (malloc_usable_size() bytes)                      . | 
|  | 1150 | .                                                               | | 
|  | 1151 | nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1152 | |             Size of chunk                                     | | 
|  | 1153 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1154 |  | 
|  | 1155 |  | 
|  | 1156 | Where "chunk" is the front of the chunk for the purpose of most of | 
|  | 1157 | the malloc code, but "mem" is the pointer that is returned to the | 
|  | 1158 | user.  "Nextchunk" is the beginning of the next contiguous chunk. | 
|  | 1159 |  | 
|  | 1160 | Chunks always begin on even word boundaries, so the mem portion | 
|  | 1161 | (which is returned to the user) is also on an even word boundary, and | 
|  | 1162 | thus at least double-word aligned. | 
|  | 1163 |  | 
|  | 1164 | Free chunks are stored in circular doubly-linked lists, and look like this: | 
|  | 1165 |  | 
|  | 1166 | chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1167 | |             Size of previous chunk                            | | 
|  | 1168 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1169 | `head:' |             Size of chunk, in bytes                         |P| | 
|  | 1170 | mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1171 | |             Forward pointer to next chunk in list             | | 
|  | 1172 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1173 | |             Back pointer to previous chunk in list            | | 
|  | 1174 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1175 | |             Unused space (may be 0 bytes long)                . | 
|  | 1176 | .                                                               . | 
|  | 1177 | .                                                               | | 
|  | 1178 | nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1179 | `foot:' |             Size of chunk, in bytes                           | | 
|  | 1180 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 
|  | 1181 |  | 
|  | 1182 | The P (PREV_INUSE) bit, stored in the unused low-order bit of the | 
|  | 1183 | chunk size (which is always a multiple of two words), is an in-use | 
|  | 1184 | bit for the *previous* chunk.  If that bit is *clear*, then the | 
|  | 1185 | word before the current chunk size contains the previous chunk | 
|  | 1186 | size, and can be used to find the front of the previous chunk. | 
|  | 1187 | The very first chunk allocated always has this bit set, | 
|  | 1188 | preventing access to non-existent (or non-owned) memory. If | 
|  | 1189 | prev_inuse is set for any given chunk, then you CANNOT determine | 
|  | 1190 | the size of the previous chunk, and might even get a memory | 
|  | 1191 | addressing fault when trying to do so. | 
|  | 1192 |  | 
|  | 1193 | Note that the `foot' of the current chunk is actually represented | 
|  | 1194 | as the prev_size of the NEXT chunk. This makes it easier to | 
|  | 1195 | deal with alignments etc but can be very confusing when trying | 
|  | 1196 | to extend or adapt this code. | 
|  | 1197 |  | 
|  | 1198 | The two exceptions to all this are | 
|  | 1199 |  | 
|  | 1200 | 1. The special chunk `top' doesn't bother using the | 
|  | 1201 | trailing size field since there is no next contiguous chunk | 
|  | 1202 | that would have to index off it. After initialization, `top' | 
|  | 1203 | is forced to always exist.  If it would become less than | 
|  | 1204 | MINSIZE bytes long, it is replenished. | 
|  | 1205 |  | 
|  | 1206 | 2. Chunks allocated via mmap, which have the second-lowest-order | 
|  | 1207 | bit M (IS_MMAPPED) set in their size fields.  Because they are | 
|  | 1208 | allocated one-by-one, each must contain its own trailing size field. | 
|  | 1209 |  | 
|  | 1210 | */ | 
|  | 1211 |  | 
|  | 1212 | /* | 
|  | 1213 | ---------- Size and alignment checks and conversions ---------- | 
|  | 1214 | */ | 
|  | 1215 |  | 
|  | 1216 | /* conversion from malloc headers to user pointers, and back */ | 
|  | 1217 |  | 
|  | 1218 | #define chunk2mem(p)   ((void*)((char*)(p) + 2*SIZE_SZ)) | 
|  | 1219 | #define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ)) | 
|  | 1220 |  | 
|  | 1221 | /* The smallest possible chunk */ | 
|  | 1222 | #define MIN_CHUNK_SIZE        (offsetof(struct malloc_chunk, fd_nextsize)) | 
|  | 1223 |  | 
|  | 1224 | /* The smallest size we can malloc is an aligned minimal chunk */ | 
|  | 1225 |  | 
|  | 1226 | #define MINSIZE  \ | 
|  | 1227 | (unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)) | 
|  | 1228 |  | 
|  | 1229 | /* Check if m has acceptable alignment */ | 
|  | 1230 |  | 
|  | 1231 | #define aligned_OK(m)  (((unsigned long)(m) & MALLOC_ALIGN_MASK) == 0) | 
|  | 1232 |  | 
|  | 1233 | #define misaligned_chunk(p) \ | 
|  | 1234 | ((uintptr_t)(MALLOC_ALIGNMENT == 2 * SIZE_SZ ? (p) : chunk2mem (p)) \ | 
|  | 1235 | & MALLOC_ALIGN_MASK) | 
|  | 1236 |  | 
|  | 1237 |  | 
|  | 1238 | /* | 
|  | 1239 | Check if a request is so large that it would wrap around zero when | 
|  | 1240 | padded and aligned. To simplify some other code, the bound is made | 
|  | 1241 | low enough so that adding MINSIZE will also not wrap around zero. | 
|  | 1242 | */ | 
|  | 1243 |  | 
|  | 1244 | #define REQUEST_OUT_OF_RANGE(req)                                 \ | 
|  | 1245 | ((unsigned long) (req) >=						      \ | 
|  | 1246 | (unsigned long) (INTERNAL_SIZE_T) (-2 * MINSIZE)) | 
|  | 1247 |  | 
|  | 1248 | /* pad request bytes into a usable size -- internal version */ | 
|  | 1249 |  | 
|  | 1250 | #define request2size(req)                                         \ | 
|  | 1251 | (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE)  ?             \ | 
|  | 1252 | MINSIZE :                                                      \ | 
|  | 1253 | ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK) | 
|  | 1254 |  | 
| xf.li | f233062 | 2024-05-15 18:17:18 -0700 | [diff] [blame] | 1255 | /* Same, except also perform an argument and result check.  First, we check | 
|  | 1256 | that the padding done by request2size didn't result in an integer | 
|  | 1257 | overflow.  Then we check (using REQUEST_OUT_OF_RANGE) that the resulting | 
|  | 1258 | size isn't so large that a later alignment would lead to another integer | 
|  | 1259 | overflow.  */ | 
|  | 1260 | #define checked_request2size(req, sz) \ | 
|  | 1261 | ({                                 \ | 
|  | 1262 | (sz) = request2size (req);       \ | 
|  | 1263 | if (((sz) < (req))               \ | 
|  | 1264 | || REQUEST_OUT_OF_RANGE (sz)) \ | 
|  | 1265 | {                              \ | 
|  | 1266 | __set_errno (ENOMEM);        \ | 
|  | 1267 | return 0;                            \ | 
|  | 1268 | }                              \ | 
|  | 1269 | }) | 
| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1270 |  | 
|  | 1271 | /* | 
|  | 1272 | --------------- Physical chunk operations --------------- | 
|  | 1273 | */ | 
|  | 1274 |  | 
|  | 1275 |  | 
|  | 1276 | /* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */ | 
|  | 1277 | #define PREV_INUSE 0x1 | 
|  | 1278 |  | 
|  | 1279 | /* extract inuse bit of previous chunk */ | 
|  | 1280 | #define prev_inuse(p)       ((p)->size & PREV_INUSE) | 
|  | 1281 |  | 
|  | 1282 |  | 
|  | 1283 | /* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */ | 
|  | 1284 | #define IS_MMAPPED 0x2 | 
|  | 1285 |  | 
|  | 1286 | /* check for mmap()'ed chunk */ | 
|  | 1287 | #define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED) | 
|  | 1288 |  | 
|  | 1289 |  | 
|  | 1290 | /* size field is or'ed with NON_MAIN_ARENA if the chunk was obtained | 
|  | 1291 | from a non-main arena.  This is only set immediately before handing | 
|  | 1292 | the chunk to the user, if necessary.  */ | 
|  | 1293 | #define NON_MAIN_ARENA 0x4 | 
|  | 1294 |  | 
|  | 1295 | /* check for chunk from non-main arena */ | 
|  | 1296 | #define chunk_non_main_arena(p) ((p)->size & NON_MAIN_ARENA) | 
|  | 1297 |  | 
|  | 1298 |  | 
|  | 1299 | /* | 
|  | 1300 | Bits to mask off when extracting size | 
|  | 1301 |  | 
|  | 1302 | Note: IS_MMAPPED is intentionally not masked off from size field in | 
|  | 1303 | macros for which mmapped chunks should never be seen. This should | 
|  | 1304 | cause helpful core dumps to occur if it is tried by accident by | 
|  | 1305 | people extending or adapting this malloc. | 
|  | 1306 | */ | 
|  | 1307 | #define SIZE_BITS (PREV_INUSE | IS_MMAPPED | NON_MAIN_ARENA) | 
|  | 1308 |  | 
|  | 1309 | /* Get size, ignoring use bits */ | 
|  | 1310 | #define chunksize(p)         ((p)->size & ~(SIZE_BITS)) | 
|  | 1311 |  | 
|  | 1312 |  | 
|  | 1313 | /* Ptr to next physical malloc_chunk. */ | 
|  | 1314 | #define next_chunk(p) ((mchunkptr) (((char *) (p)) + ((p)->size & ~SIZE_BITS))) | 
|  | 1315 |  | 
|  | 1316 | /* Ptr to previous physical malloc_chunk */ | 
|  | 1317 | #define prev_chunk(p) ((mchunkptr) (((char *) (p)) - ((p)->prev_size))) | 
|  | 1318 |  | 
|  | 1319 | /* Treat space at ptr + offset as a chunk */ | 
|  | 1320 | #define chunk_at_offset(p, s)  ((mchunkptr) (((char *) (p)) + (s))) | 
|  | 1321 |  | 
|  | 1322 | /* extract p's inuse bit */ | 
|  | 1323 | #define inuse(p)							      \ | 
|  | 1324 | ((((mchunkptr) (((char *) (p)) + ((p)->size & ~SIZE_BITS)))->size) & PREV_INUSE) | 
|  | 1325 |  | 
|  | 1326 | /* set/clear chunk as being inuse without otherwise disturbing */ | 
|  | 1327 | #define set_inuse(p)							      \ | 
|  | 1328 | ((mchunkptr) (((char *) (p)) + ((p)->size & ~SIZE_BITS)))->size |= PREV_INUSE | 
|  | 1329 |  | 
|  | 1330 | #define clear_inuse(p)							      \ | 
|  | 1331 | ((mchunkptr) (((char *) (p)) + ((p)->size & ~SIZE_BITS)))->size &= ~(PREV_INUSE) | 
|  | 1332 |  | 
|  | 1333 |  | 
|  | 1334 | /* check/set/clear inuse bits in known places */ | 
|  | 1335 | #define inuse_bit_at_offset(p, s)					      \ | 
|  | 1336 | (((mchunkptr) (((char *) (p)) + (s)))->size & PREV_INUSE) | 
|  | 1337 |  | 
|  | 1338 | #define set_inuse_bit_at_offset(p, s)					      \ | 
|  | 1339 | (((mchunkptr) (((char *) (p)) + (s)))->size |= PREV_INUSE) | 
|  | 1340 |  | 
|  | 1341 | #define clear_inuse_bit_at_offset(p, s)					      \ | 
|  | 1342 | (((mchunkptr) (((char *) (p)) + (s)))->size &= ~(PREV_INUSE)) | 
|  | 1343 |  | 
|  | 1344 |  | 
|  | 1345 | /* Set size at head, without disturbing its use bit */ | 
|  | 1346 | #define set_head_size(p, s)  ((p)->size = (((p)->size & SIZE_BITS) | (s))) | 
|  | 1347 |  | 
|  | 1348 | /* Set size/use field */ | 
|  | 1349 | #define set_head(p, s)       ((p)->size = (s)) | 
|  | 1350 |  | 
|  | 1351 | /* Set size at footer (only when chunk is not in use) */ | 
|  | 1352 | #define set_foot(p, s)       (((mchunkptr) ((char *) (p) + (s)))->prev_size = (s)) | 
|  | 1353 |  | 
|  | 1354 |  | 
|  | 1355 | /* | 
|  | 1356 | -------------------- Internal data structures -------------------- | 
|  | 1357 |  | 
|  | 1358 | All internal state is held in an instance of malloc_state defined | 
|  | 1359 | below. There are no other static variables, except in two optional | 
|  | 1360 | cases: | 
|  | 1361 | * If USE_MALLOC_LOCK is defined, the mALLOC_MUTEx declared above. | 
|  | 1362 | * If mmap doesn't support MAP_ANONYMOUS, a dummy file descriptor | 
|  | 1363 | for mmap. | 
|  | 1364 |  | 
|  | 1365 | Beware of lots of tricks that minimize the total bookkeeping space | 
|  | 1366 | requirements. The result is a little over 1K bytes (for 4byte | 
|  | 1367 | pointers and size_t.) | 
|  | 1368 | */ | 
|  | 1369 |  | 
|  | 1370 | /* | 
|  | 1371 | Bins | 
|  | 1372 |  | 
|  | 1373 | An array of bin headers for free chunks. Each bin is doubly | 
|  | 1374 | linked.  The bins are approximately proportionally (log) spaced. | 
|  | 1375 | There are a lot of these bins (128). This may look excessive, but | 
|  | 1376 | works very well in practice.  Most bins hold sizes that are | 
|  | 1377 | unusual as malloc request sizes, but are more usual for fragments | 
|  | 1378 | and consolidated sets of chunks, which is what these bins hold, so | 
|  | 1379 | they can be found quickly.  All procedures maintain the invariant | 
|  | 1380 | that no consolidated chunk physically borders another one, so each | 
|  | 1381 | chunk in a list is known to be preceeded and followed by either | 
|  | 1382 | inuse chunks or the ends of memory. | 
|  | 1383 |  | 
|  | 1384 | Chunks in bins are kept in size order, with ties going to the | 
|  | 1385 | approximately least recently used chunk. Ordering isn't needed | 
|  | 1386 | for the small bins, which all contain the same-sized chunks, but | 
|  | 1387 | facilitates best-fit allocation for larger chunks. These lists | 
|  | 1388 | are just sequential. Keeping them in order almost never requires | 
|  | 1389 | enough traversal to warrant using fancier ordered data | 
|  | 1390 | structures. | 
|  | 1391 |  | 
|  | 1392 | Chunks of the same size are linked with the most | 
|  | 1393 | recently freed at the front, and allocations are taken from the | 
|  | 1394 | back.  This results in LRU (FIFO) allocation order, which tends | 
|  | 1395 | to give each chunk an equal opportunity to be consolidated with | 
|  | 1396 | adjacent freed chunks, resulting in larger free chunks and less | 
|  | 1397 | fragmentation. | 
|  | 1398 |  | 
|  | 1399 | To simplify use in double-linked lists, each bin header acts | 
|  | 1400 | as a malloc_chunk. This avoids special-casing for headers. | 
|  | 1401 | But to conserve space and improve locality, we allocate | 
|  | 1402 | only the fd/bk pointers of bins, and then use repositioning tricks | 
|  | 1403 | to treat these as the fields of a malloc_chunk*. | 
|  | 1404 | */ | 
|  | 1405 |  | 
|  | 1406 | typedef struct malloc_chunk *mbinptr; | 
|  | 1407 |  | 
|  | 1408 | /* addressing -- note that bin_at(0) does not exist */ | 
|  | 1409 | #define bin_at(m, i) \ | 
|  | 1410 | (mbinptr) (((char *) &((m)->bins[((i) - 1) * 2]))			      \ | 
|  | 1411 | - offsetof (struct malloc_chunk, fd)) | 
|  | 1412 |  | 
|  | 1413 | /* analog of ++bin */ | 
|  | 1414 | #define next_bin(b)  ((mbinptr) ((char *) (b) + (sizeof (mchunkptr) << 1))) | 
|  | 1415 |  | 
|  | 1416 | /* Reminders about list directionality within bins */ | 
|  | 1417 | #define first(b)     ((b)->fd) | 
|  | 1418 | #define last(b)      ((b)->bk) | 
|  | 1419 |  | 
|  | 1420 | /* Take a chunk off a bin list */ | 
|  | 1421 | #define unlink(AV, P, BK, FD) {                                            \ | 
|  | 1422 | FD = P->fd;								      \ | 
|  | 1423 | BK = P->bk;								      \ | 
|  | 1424 | if (__builtin_expect (FD->bk != P || BK->fd != P, 0))		      \ | 
|  | 1425 | malloc_printerr (check_action, "corrupted double-linked list", P, AV);  \ | 
|  | 1426 | else {								      \ | 
|  | 1427 | FD->bk = BK;							      \ | 
|  | 1428 | BK->fd = FD;							      \ | 
|  | 1429 | if (!in_smallbin_range (P->size)				      \ | 
|  | 1430 | && __builtin_expect (P->fd_nextsize != NULL, 0)) {		      \ | 
|  | 1431 | if (__builtin_expect (P->fd_nextsize->bk_nextsize != P, 0)	      \ | 
|  | 1432 | || __builtin_expect (P->bk_nextsize->fd_nextsize != P, 0))    \ | 
|  | 1433 | malloc_printerr (check_action,				      \ | 
|  | 1434 | "corrupted double-linked list (not small)",    \ | 
|  | 1435 | P, AV);					      \ | 
|  | 1436 | if (FD->fd_nextsize == NULL) {				      \ | 
|  | 1437 | if (P->fd_nextsize == P)				      \ | 
|  | 1438 | FD->fd_nextsize = FD->bk_nextsize = FD;		      \ | 
|  | 1439 | else {							      \ | 
|  | 1440 | FD->fd_nextsize = P->fd_nextsize;			      \ | 
|  | 1441 | FD->bk_nextsize = P->bk_nextsize;			      \ | 
|  | 1442 | P->fd_nextsize->bk_nextsize = FD;			      \ | 
|  | 1443 | P->bk_nextsize->fd_nextsize = FD;			      \ | 
|  | 1444 | }							      \ | 
|  | 1445 | } else {							      \ | 
|  | 1446 | P->fd_nextsize->bk_nextsize = P->bk_nextsize;		      \ | 
|  | 1447 | P->bk_nextsize->fd_nextsize = P->fd_nextsize;		      \ | 
|  | 1448 | }								      \ | 
|  | 1449 | }								      \ | 
|  | 1450 | }									      \ | 
|  | 1451 | } | 
|  | 1452 |  | 
|  | 1453 | /* | 
|  | 1454 | Indexing | 
|  | 1455 |  | 
|  | 1456 | Bins for sizes < 512 bytes contain chunks of all the same size, spaced | 
|  | 1457 | 8 bytes apart. Larger bins are approximately logarithmically spaced: | 
|  | 1458 |  | 
|  | 1459 | 64 bins of size       8 | 
|  | 1460 | 32 bins of size      64 | 
|  | 1461 | 16 bins of size     512 | 
|  | 1462 | 8 bins of size    4096 | 
|  | 1463 | 4 bins of size   32768 | 
|  | 1464 | 2 bins of size  262144 | 
|  | 1465 | 1 bin  of size what's left | 
|  | 1466 |  | 
|  | 1467 | There is actually a little bit of slop in the numbers in bin_index | 
|  | 1468 | for the sake of speed. This makes no difference elsewhere. | 
|  | 1469 |  | 
|  | 1470 | The bins top out around 1MB because we expect to service large | 
|  | 1471 | requests via mmap. | 
|  | 1472 |  | 
|  | 1473 | Bin 0 does not exist.  Bin 1 is the unordered list; if that would be | 
|  | 1474 | a valid chunk size the small bins are bumped up one. | 
|  | 1475 | */ | 
|  | 1476 |  | 
|  | 1477 | #define NBINS             128 | 
|  | 1478 | #define NSMALLBINS         64 | 
|  | 1479 | #define SMALLBIN_WIDTH    MALLOC_ALIGNMENT | 
|  | 1480 | #define SMALLBIN_CORRECTION (MALLOC_ALIGNMENT > 2 * SIZE_SZ) | 
|  | 1481 | #define MIN_LARGE_SIZE    ((NSMALLBINS - SMALLBIN_CORRECTION) * SMALLBIN_WIDTH) | 
|  | 1482 |  | 
|  | 1483 | #define in_smallbin_range(sz)  \ | 
|  | 1484 | ((unsigned long) (sz) < (unsigned long) MIN_LARGE_SIZE) | 
|  | 1485 |  | 
|  | 1486 | #define smallbin_index(sz) \ | 
|  | 1487 | ((SMALLBIN_WIDTH == 16 ? (((unsigned) (sz)) >> 4) : (((unsigned) (sz)) >> 3))\ | 
|  | 1488 | + SMALLBIN_CORRECTION) | 
|  | 1489 |  | 
|  | 1490 | #define largebin_index_32(sz)                                                \ | 
|  | 1491 | (((((unsigned long) (sz)) >> 6) <= 38) ?  56 + (((unsigned long) (sz)) >> 6) :\ | 
|  | 1492 | ((((unsigned long) (sz)) >> 9) <= 20) ?  91 + (((unsigned long) (sz)) >> 9) :\ | 
|  | 1493 | ((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\ | 
|  | 1494 | ((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\ | 
|  | 1495 | ((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\ | 
|  | 1496 | 126) | 
|  | 1497 |  | 
|  | 1498 | #define largebin_index_32_big(sz)                                            \ | 
|  | 1499 | (((((unsigned long) (sz)) >> 6) <= 45) ?  49 + (((unsigned long) (sz)) >> 6) :\ | 
|  | 1500 | ((((unsigned long) (sz)) >> 9) <= 20) ?  91 + (((unsigned long) (sz)) >> 9) :\ | 
|  | 1501 | ((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\ | 
|  | 1502 | ((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\ | 
|  | 1503 | ((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\ | 
|  | 1504 | 126) | 
|  | 1505 |  | 
|  | 1506 | // XXX It remains to be seen whether it is good to keep the widths of | 
|  | 1507 | // XXX the buckets the same or whether it should be scaled by a factor | 
|  | 1508 | // XXX of two as well. | 
|  | 1509 | #define largebin_index_64(sz)                                                \ | 
|  | 1510 | (((((unsigned long) (sz)) >> 6) <= 48) ?  48 + (((unsigned long) (sz)) >> 6) :\ | 
|  | 1511 | ((((unsigned long) (sz)) >> 9) <= 20) ?  91 + (((unsigned long) (sz)) >> 9) :\ | 
|  | 1512 | ((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\ | 
|  | 1513 | ((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\ | 
|  | 1514 | ((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\ | 
|  | 1515 | 126) | 
|  | 1516 |  | 
|  | 1517 | #define largebin_index(sz) \ | 
|  | 1518 | (SIZE_SZ == 8 ? largebin_index_64 (sz)                                     \ | 
|  | 1519 | : MALLOC_ALIGNMENT == 16 ? largebin_index_32_big (sz)                     \ | 
|  | 1520 | : largebin_index_32 (sz)) | 
|  | 1521 |  | 
|  | 1522 | #define bin_index(sz) \ | 
|  | 1523 | ((in_smallbin_range (sz)) ? smallbin_index (sz) : largebin_index (sz)) | 
|  | 1524 |  | 
|  | 1525 |  | 
|  | 1526 | /* | 
|  | 1527 | Unsorted chunks | 
|  | 1528 |  | 
|  | 1529 | All remainders from chunk splits, as well as all returned chunks, | 
|  | 1530 | are first placed in the "unsorted" bin. They are then placed | 
|  | 1531 | in regular bins after malloc gives them ONE chance to be used before | 
|  | 1532 | binning. So, basically, the unsorted_chunks list acts as a queue, | 
|  | 1533 | with chunks being placed on it in free (and malloc_consolidate), | 
|  | 1534 | and taken off (to be either used or placed in bins) in malloc. | 
|  | 1535 |  | 
|  | 1536 | The NON_MAIN_ARENA flag is never set for unsorted chunks, so it | 
|  | 1537 | does not have to be taken into account in size comparisons. | 
|  | 1538 | */ | 
|  | 1539 |  | 
|  | 1540 | /* The otherwise unindexable 1-bin is used to hold unsorted chunks. */ | 
|  | 1541 | #define unsorted_chunks(M)          (bin_at (M, 1)) | 
|  | 1542 |  | 
|  | 1543 | /* | 
|  | 1544 | Top | 
|  | 1545 |  | 
|  | 1546 | The top-most available chunk (i.e., the one bordering the end of | 
|  | 1547 | available memory) is treated specially. It is never included in | 
|  | 1548 | any bin, is used only if no other chunk is available, and is | 
|  | 1549 | released back to the system if it is very large (see | 
|  | 1550 | M_TRIM_THRESHOLD).  Because top initially | 
|  | 1551 | points to its own bin with initial zero size, thus forcing | 
|  | 1552 | extension on the first malloc request, we avoid having any special | 
|  | 1553 | code in malloc to check whether it even exists yet. But we still | 
|  | 1554 | need to do so when getting memory from system, so we make | 
|  | 1555 | initial_top treat the bin as a legal but unusable chunk during the | 
|  | 1556 | interval between initialization and the first call to | 
|  | 1557 | sysmalloc. (This is somewhat delicate, since it relies on | 
|  | 1558 | the 2 preceding words to be zero during this interval as well.) | 
|  | 1559 | */ | 
|  | 1560 |  | 
|  | 1561 | /* Conveniently, the unsorted bin can be used as dummy top on first call */ | 
|  | 1562 | #define initial_top(M)              (unsorted_chunks (M)) | 
|  | 1563 |  | 
|  | 1564 | /* | 
|  | 1565 | Binmap | 
|  | 1566 |  | 
|  | 1567 | To help compensate for the large number of bins, a one-level index | 
|  | 1568 | structure is used for bin-by-bin searching.  `binmap' is a | 
|  | 1569 | bitvector recording whether bins are definitely empty so they can | 
|  | 1570 | be skipped over during during traversals.  The bits are NOT always | 
|  | 1571 | cleared as soon as bins are empty, but instead only | 
|  | 1572 | when they are noticed to be empty during traversal in malloc. | 
|  | 1573 | */ | 
|  | 1574 |  | 
|  | 1575 | /* Conservatively use 32 bits per map word, even if on 64bit system */ | 
|  | 1576 | #define BINMAPSHIFT      5 | 
|  | 1577 | #define BITSPERMAP       (1U << BINMAPSHIFT) | 
|  | 1578 | #define BINMAPSIZE       (NBINS / BITSPERMAP) | 
|  | 1579 |  | 
|  | 1580 | #define idx2block(i)     ((i) >> BINMAPSHIFT) | 
|  | 1581 | #define idx2bit(i)       ((1U << ((i) & ((1U << BINMAPSHIFT) - 1)))) | 
|  | 1582 |  | 
|  | 1583 | #define mark_bin(m, i)    ((m)->binmap[idx2block (i)] |= idx2bit (i)) | 
|  | 1584 | #define unmark_bin(m, i)  ((m)->binmap[idx2block (i)] &= ~(idx2bit (i))) | 
|  | 1585 | #define get_binmap(m, i)  ((m)->binmap[idx2block (i)] & idx2bit (i)) | 
|  | 1586 |  | 
|  | 1587 | /* | 
|  | 1588 | Fastbins | 
|  | 1589 |  | 
|  | 1590 | An array of lists holding recently freed small chunks.  Fastbins | 
|  | 1591 | are not doubly linked.  It is faster to single-link them, and | 
|  | 1592 | since chunks are never removed from the middles of these lists, | 
|  | 1593 | double linking is not necessary. Also, unlike regular bins, they | 
|  | 1594 | are not even processed in FIFO order (they use faster LIFO) since | 
|  | 1595 | ordering doesn't much matter in the transient contexts in which | 
|  | 1596 | fastbins are normally used. | 
|  | 1597 |  | 
|  | 1598 | Chunks in fastbins keep their inuse bit set, so they cannot | 
|  | 1599 | be consolidated with other free chunks. malloc_consolidate | 
|  | 1600 | releases all chunks in fastbins and consolidates them with | 
|  | 1601 | other free chunks. | 
|  | 1602 | */ | 
|  | 1603 |  | 
|  | 1604 | typedef struct malloc_chunk *mfastbinptr; | 
|  | 1605 | #define fastbin(ar_ptr, idx) ((ar_ptr)->fastbinsY[idx]) | 
|  | 1606 |  | 
|  | 1607 | /* offset 2 to use otherwise unindexable first 2 bins */ | 
|  | 1608 | #define fastbin_index(sz) \ | 
|  | 1609 | ((((unsigned int) (sz)) >> (SIZE_SZ == 8 ? 4 : 3)) - 2) | 
|  | 1610 |  | 
|  | 1611 |  | 
|  | 1612 | /* The maximum fastbin request size we support */ | 
|  | 1613 | #define MAX_FAST_SIZE     (80 * SIZE_SZ / 4) | 
|  | 1614 |  | 
|  | 1615 | #define NFASTBINS  (fastbin_index (request2size (MAX_FAST_SIZE)) + 1) | 
|  | 1616 |  | 
|  | 1617 | /* | 
|  | 1618 | FASTBIN_CONSOLIDATION_THRESHOLD is the size of a chunk in free() | 
|  | 1619 | that triggers automatic consolidation of possibly-surrounding | 
|  | 1620 | fastbin chunks. This is a heuristic, so the exact value should not | 
|  | 1621 | matter too much. It is defined at half the default trim threshold as a | 
|  | 1622 | compromise heuristic to only attempt consolidation if it is likely | 
|  | 1623 | to lead to trimming. However, it is not dynamically tunable, since | 
|  | 1624 | consolidation reduces fragmentation surrounding large chunks even | 
|  | 1625 | if trimming is not used. | 
|  | 1626 | */ | 
|  | 1627 |  | 
|  | 1628 | #define FASTBIN_CONSOLIDATION_THRESHOLD  (65536UL) | 
|  | 1629 |  | 
|  | 1630 | /* | 
|  | 1631 | Since the lowest 2 bits in max_fast don't matter in size comparisons, | 
|  | 1632 | they are used as flags. | 
|  | 1633 | */ | 
|  | 1634 |  | 
|  | 1635 | /* | 
|  | 1636 | FASTCHUNKS_BIT held in max_fast indicates that there are probably | 
|  | 1637 | some fastbin chunks. It is set true on entering a chunk into any | 
|  | 1638 | fastbin, and cleared only in malloc_consolidate. | 
|  | 1639 |  | 
|  | 1640 | The truth value is inverted so that have_fastchunks will be true | 
|  | 1641 | upon startup (since statics are zero-filled), simplifying | 
|  | 1642 | initialization checks. | 
|  | 1643 | */ | 
|  | 1644 |  | 
|  | 1645 | #define FASTCHUNKS_BIT        (1U) | 
|  | 1646 |  | 
|  | 1647 | #define have_fastchunks(M)     (((M)->flags & FASTCHUNKS_BIT) == 0) | 
|  | 1648 | #define clear_fastchunks(M)    catomic_or (&(M)->flags, FASTCHUNKS_BIT) | 
|  | 1649 | #define set_fastchunks(M)      catomic_and (&(M)->flags, ~FASTCHUNKS_BIT) | 
|  | 1650 |  | 
|  | 1651 | /* | 
|  | 1652 | NONCONTIGUOUS_BIT indicates that MORECORE does not return contiguous | 
|  | 1653 | regions.  Otherwise, contiguity is exploited in merging together, | 
|  | 1654 | when possible, results from consecutive MORECORE calls. | 
|  | 1655 |  | 
|  | 1656 | The initial value comes from MORECORE_CONTIGUOUS, but is | 
|  | 1657 | changed dynamically if mmap is ever used as an sbrk substitute. | 
|  | 1658 | */ | 
|  | 1659 |  | 
|  | 1660 | #define NONCONTIGUOUS_BIT     (2U) | 
|  | 1661 |  | 
|  | 1662 | #define contiguous(M)          (((M)->flags & NONCONTIGUOUS_BIT) == 0) | 
|  | 1663 | #define noncontiguous(M)       (((M)->flags & NONCONTIGUOUS_BIT) != 0) | 
|  | 1664 | #define set_noncontiguous(M)   ((M)->flags |= NONCONTIGUOUS_BIT) | 
|  | 1665 | #define set_contiguous(M)      ((M)->flags &= ~NONCONTIGUOUS_BIT) | 
|  | 1666 |  | 
|  | 1667 | /* ARENA_CORRUPTION_BIT is set if a memory corruption was detected on the | 
|  | 1668 | arena.  Such an arena is no longer used to allocate chunks.  Chunks | 
|  | 1669 | allocated in that arena before detecting corruption are not freed.  */ | 
|  | 1670 |  | 
|  | 1671 | #define ARENA_CORRUPTION_BIT (4U) | 
|  | 1672 |  | 
|  | 1673 | #define arena_is_corrupt(A)	(((A)->flags & ARENA_CORRUPTION_BIT)) | 
|  | 1674 | #define set_arena_corrupt(A)	((A)->flags |= ARENA_CORRUPTION_BIT) | 
|  | 1675 |  | 
|  | 1676 | /* | 
|  | 1677 | Set value of max_fast. | 
|  | 1678 | Use impossibly small value if 0. | 
|  | 1679 | Precondition: there are no existing fastbin chunks. | 
|  | 1680 | Setting the value clears fastchunk bit but preserves noncontiguous bit. | 
|  | 1681 | */ | 
|  | 1682 |  | 
|  | 1683 | #define set_max_fast(s) \ | 
|  | 1684 | global_max_fast = (((s) == 0)						      \ | 
|  | 1685 | ? SMALLBIN_WIDTH : ((s + SIZE_SZ) & ~MALLOC_ALIGN_MASK)) | 
|  | 1686 | #define get_max_fast() global_max_fast | 
|  | 1687 |  | 
|  | 1688 |  | 
|  | 1689 | /* | 
|  | 1690 | ----------- Internal state representation and initialization ----------- | 
|  | 1691 | */ | 
|  | 1692 |  | 
|  | 1693 | struct malloc_state | 
|  | 1694 | { | 
|  | 1695 | /* Serialize access.  */ | 
|  | 1696 | mutex_t mutex; | 
|  | 1697 |  | 
|  | 1698 | /* Flags (formerly in max_fast).  */ | 
|  | 1699 | int flags; | 
|  | 1700 |  | 
|  | 1701 | /* Fastbins */ | 
|  | 1702 | mfastbinptr fastbinsY[NFASTBINS]; | 
|  | 1703 |  | 
|  | 1704 | /* Base of the topmost chunk -- not otherwise kept in a bin */ | 
|  | 1705 | mchunkptr top; | 
|  | 1706 |  | 
|  | 1707 | /* The remainder from the most recent split of a small request */ | 
|  | 1708 | mchunkptr last_remainder; | 
|  | 1709 |  | 
|  | 1710 | /* Normal bins packed as described above */ | 
|  | 1711 | mchunkptr bins[NBINS * 2 - 2]; | 
|  | 1712 |  | 
|  | 1713 | /* Bitmap of bins */ | 
|  | 1714 | unsigned int binmap[BINMAPSIZE]; | 
|  | 1715 |  | 
|  | 1716 | /* Linked list */ | 
|  | 1717 | struct malloc_state *next; | 
|  | 1718 |  | 
|  | 1719 | /* Linked list for free arenas.  Access to this field is serialized | 
|  | 1720 | by free_list_lock in arena.c.  */ | 
|  | 1721 | struct malloc_state *next_free; | 
|  | 1722 |  | 
|  | 1723 | /* Number of threads attached to this arena.  0 if the arena is on | 
|  | 1724 | the free list.  Access to this field is serialized by | 
|  | 1725 | free_list_lock in arena.c.  */ | 
|  | 1726 | INTERNAL_SIZE_T attached_threads; | 
|  | 1727 |  | 
|  | 1728 | /* Memory allocated from the system in this arena.  */ | 
|  | 1729 | INTERNAL_SIZE_T system_mem; | 
|  | 1730 | INTERNAL_SIZE_T max_system_mem; | 
|  | 1731 | }; | 
|  | 1732 |  | 
|  | 1733 | struct malloc_par | 
|  | 1734 | { | 
|  | 1735 | /* Tunable parameters */ | 
|  | 1736 | unsigned long trim_threshold; | 
|  | 1737 | INTERNAL_SIZE_T top_pad; | 
|  | 1738 | INTERNAL_SIZE_T mmap_threshold; | 
|  | 1739 | INTERNAL_SIZE_T arena_test; | 
|  | 1740 | INTERNAL_SIZE_T arena_max; | 
|  | 1741 |  | 
|  | 1742 | /* Memory map support */ | 
|  | 1743 | int n_mmaps; | 
|  | 1744 | int n_mmaps_max; | 
|  | 1745 | int max_n_mmaps; | 
|  | 1746 | /* the mmap_threshold is dynamic, until the user sets | 
|  | 1747 | it manually, at which point we need to disable any | 
|  | 1748 | dynamic behavior. */ | 
|  | 1749 | int no_dyn_threshold; | 
|  | 1750 |  | 
|  | 1751 | /* Statistics */ | 
|  | 1752 | INTERNAL_SIZE_T mmapped_mem; | 
|  | 1753 | /*INTERNAL_SIZE_T  sbrked_mem;*/ | 
|  | 1754 | /*INTERNAL_SIZE_T  max_sbrked_mem;*/ | 
|  | 1755 | INTERNAL_SIZE_T max_mmapped_mem; | 
|  | 1756 | INTERNAL_SIZE_T max_total_mem;  /* only kept for NO_THREADS */ | 
|  | 1757 |  | 
|  | 1758 | /* First address handed out by MORECORE/sbrk.  */ | 
|  | 1759 | char *sbrk_base; | 
|  | 1760 | }; | 
|  | 1761 |  | 
|  | 1762 | /* There are several instances of this struct ("arenas") in this | 
|  | 1763 | malloc.  If you are adapting this malloc in a way that does NOT use | 
|  | 1764 | a static or mmapped malloc_state, you MUST explicitly zero-fill it | 
|  | 1765 | before using. This malloc relies on the property that malloc_state | 
|  | 1766 | is initialized to all zeroes (as is true of C statics).  */ | 
|  | 1767 |  | 
|  | 1768 | static struct malloc_state main_arena = | 
|  | 1769 | { | 
|  | 1770 | .mutex = _LIBC_LOCK_INITIALIZER, | 
|  | 1771 | .next = &main_arena, | 
|  | 1772 | .attached_threads = 1 | 
|  | 1773 | }; | 
|  | 1774 |  | 
|  | 1775 | /* There is only one instance of the malloc parameters.  */ | 
|  | 1776 |  | 
|  | 1777 | static struct malloc_par mp_ = | 
|  | 1778 | { | 
|  | 1779 | .top_pad = DEFAULT_TOP_PAD, | 
|  | 1780 | .n_mmaps_max = DEFAULT_MMAP_MAX, | 
|  | 1781 | .mmap_threshold = DEFAULT_MMAP_THRESHOLD, | 
|  | 1782 | .trim_threshold = DEFAULT_TRIM_THRESHOLD, | 
|  | 1783 | #define NARENAS_FROM_NCORES(n) ((n) * (sizeof (long) == 4 ? 2 : 8)) | 
|  | 1784 | .arena_test = NARENAS_FROM_NCORES (1) | 
|  | 1785 | }; | 
|  | 1786 |  | 
|  | 1787 |  | 
|  | 1788 | /*  Non public mallopt parameters.  */ | 
|  | 1789 | #define M_ARENA_TEST -7 | 
|  | 1790 | #define M_ARENA_MAX  -8 | 
|  | 1791 |  | 
|  | 1792 |  | 
|  | 1793 | /* Maximum size of memory handled in fastbins.  */ | 
|  | 1794 | static INTERNAL_SIZE_T global_max_fast; | 
|  | 1795 |  | 
|  | 1796 | /* | 
|  | 1797 | Initialize a malloc_state struct. | 
|  | 1798 |  | 
|  | 1799 | This is called only from within malloc_consolidate, which needs | 
|  | 1800 | be called in the same contexts anyway.  It is never called directly | 
|  | 1801 | outside of malloc_consolidate because some optimizing compilers try | 
|  | 1802 | to inline it at all call points, which turns out not to be an | 
|  | 1803 | optimization at all. (Inlining it in malloc_consolidate is fine though.) | 
|  | 1804 | */ | 
|  | 1805 |  | 
|  | 1806 | static void | 
|  | 1807 | malloc_init_state (mstate av) | 
|  | 1808 | { | 
|  | 1809 | int i; | 
|  | 1810 | mbinptr bin; | 
|  | 1811 |  | 
|  | 1812 | /* Establish circular links for normal bins */ | 
|  | 1813 | for (i = 1; i < NBINS; ++i) | 
|  | 1814 | { | 
|  | 1815 | bin = bin_at (av, i); | 
|  | 1816 | bin->fd = bin->bk = bin; | 
|  | 1817 | } | 
|  | 1818 |  | 
|  | 1819 | #if MORECORE_CONTIGUOUS | 
|  | 1820 | if (av != &main_arena) | 
|  | 1821 | #endif | 
|  | 1822 | set_noncontiguous (av); | 
|  | 1823 | if (av == &main_arena) | 
|  | 1824 | set_max_fast (DEFAULT_MXFAST); | 
|  | 1825 | av->flags |= FASTCHUNKS_BIT; | 
|  | 1826 |  | 
|  | 1827 | av->top = initial_top (av); | 
|  | 1828 | } | 
|  | 1829 |  | 
|  | 1830 | /* | 
|  | 1831 | Other internal utilities operating on mstates | 
|  | 1832 | */ | 
|  | 1833 |  | 
|  | 1834 | static void *sysmalloc (INTERNAL_SIZE_T, mstate); | 
|  | 1835 | static int      systrim (size_t, mstate); | 
|  | 1836 | static void     malloc_consolidate (mstate); | 
|  | 1837 |  | 
|  | 1838 |  | 
|  | 1839 | /* -------------- Early definitions for debugging hooks ---------------- */ | 
|  | 1840 |  | 
|  | 1841 | /* Define and initialize the hook variables.  These weak definitions must | 
|  | 1842 | appear before any use of the variables in a function (arena.c uses one).  */ | 
|  | 1843 | #ifndef weak_variable | 
|  | 1844 | /* In GNU libc we want the hook variables to be weak definitions to | 
|  | 1845 | avoid a problem with Emacs.  */ | 
|  | 1846 | # define weak_variable weak_function | 
|  | 1847 | #endif | 
|  | 1848 |  | 
|  | 1849 | /* Forward declarations.  */ | 
|  | 1850 | static void *malloc_hook_ini (size_t sz, | 
|  | 1851 | const void *caller) __THROW; | 
|  | 1852 | static void *realloc_hook_ini (void *ptr, size_t sz, | 
|  | 1853 | const void *caller) __THROW; | 
|  | 1854 | static void *memalign_hook_ini (size_t alignment, size_t sz, | 
|  | 1855 | const void *caller) __THROW; | 
|  | 1856 |  | 
|  | 1857 | void weak_variable (*__malloc_initialize_hook) (void) = NULL; | 
|  | 1858 | void weak_variable (*__free_hook) (void *__ptr, | 
|  | 1859 | const void *) = NULL; | 
|  | 1860 | void *weak_variable (*__malloc_hook) | 
|  | 1861 | (size_t __size, const void *) = malloc_hook_ini; | 
|  | 1862 | void *weak_variable (*__realloc_hook) | 
|  | 1863 | (void *__ptr, size_t __size, const void *) | 
|  | 1864 | = realloc_hook_ini; | 
|  | 1865 | void *weak_variable (*__memalign_hook) | 
|  | 1866 | (size_t __alignment, size_t __size, const void *) | 
|  | 1867 | = memalign_hook_ini; | 
|  | 1868 | void weak_variable (*__after_morecore_hook) (void) = NULL; | 
|  | 1869 |  | 
|  | 1870 |  | 
|  | 1871 | /* ---------------- Error behavior ------------------------------------ */ | 
|  | 1872 |  | 
|  | 1873 | #ifndef DEFAULT_CHECK_ACTION | 
|  | 1874 | # define DEFAULT_CHECK_ACTION 3 | 
|  | 1875 | #endif | 
|  | 1876 |  | 
|  | 1877 | static int check_action = DEFAULT_CHECK_ACTION; | 
|  | 1878 |  | 
|  | 1879 |  | 
|  | 1880 | /* ------------------ Testing support ----------------------------------*/ | 
|  | 1881 |  | 
|  | 1882 | static int perturb_byte; | 
|  | 1883 |  | 
|  | 1884 | static void | 
|  | 1885 | alloc_perturb (char *p, size_t n) | 
|  | 1886 | { | 
|  | 1887 | if (__glibc_unlikely (perturb_byte)) | 
|  | 1888 | memset (p, perturb_byte ^ 0xff, n); | 
|  | 1889 | } | 
|  | 1890 |  | 
|  | 1891 | static void | 
|  | 1892 | free_perturb (char *p, size_t n) | 
|  | 1893 | { | 
|  | 1894 | if (__glibc_unlikely (perturb_byte)) | 
|  | 1895 | memset (p, perturb_byte, n); | 
|  | 1896 | } | 
|  | 1897 |  | 
|  | 1898 |  | 
|  | 1899 |  | 
|  | 1900 | #include <stap-probe.h> | 
|  | 1901 |  | 
|  | 1902 | /* ------------------- Support for multiple arenas -------------------- */ | 
|  | 1903 | #include "arena.c" | 
|  | 1904 |  | 
|  | 1905 | /* | 
|  | 1906 | Debugging support | 
|  | 1907 |  | 
|  | 1908 | These routines make a number of assertions about the states | 
|  | 1909 | of data structures that should be true at all times. If any | 
|  | 1910 | are not true, it's very likely that a user program has somehow | 
|  | 1911 | trashed memory. (It's also possible that there is a coding error | 
|  | 1912 | in malloc. In which case, please report it!) | 
|  | 1913 | */ | 
|  | 1914 |  | 
|  | 1915 | #if !MALLOC_DEBUG | 
|  | 1916 |  | 
|  | 1917 | # define check_chunk(A, P) | 
|  | 1918 | # define check_free_chunk(A, P) | 
|  | 1919 | # define check_inuse_chunk(A, P) | 
|  | 1920 | # define check_remalloced_chunk(A, P, N) | 
|  | 1921 | # define check_malloced_chunk(A, P, N) | 
|  | 1922 | # define check_malloc_state(A) | 
|  | 1923 |  | 
|  | 1924 | #else | 
|  | 1925 |  | 
|  | 1926 | # define check_chunk(A, P)              do_check_chunk (A, P) | 
|  | 1927 | # define check_free_chunk(A, P)         do_check_free_chunk (A, P) | 
|  | 1928 | # define check_inuse_chunk(A, P)        do_check_inuse_chunk (A, P) | 
|  | 1929 | # define check_remalloced_chunk(A, P, N) do_check_remalloced_chunk (A, P, N) | 
|  | 1930 | # define check_malloced_chunk(A, P, N)   do_check_malloced_chunk (A, P, N) | 
|  | 1931 | # define check_malloc_state(A)         do_check_malloc_state (A) | 
|  | 1932 |  | 
|  | 1933 | /* | 
|  | 1934 | Properties of all chunks | 
|  | 1935 | */ | 
|  | 1936 |  | 
|  | 1937 | static void | 
|  | 1938 | do_check_chunk (mstate av, mchunkptr p) | 
|  | 1939 | { | 
|  | 1940 | unsigned long sz = chunksize (p); | 
|  | 1941 | /* min and max possible addresses assuming contiguous allocation */ | 
|  | 1942 | char *max_address = (char *) (av->top) + chunksize (av->top); | 
|  | 1943 | char *min_address = max_address - av->system_mem; | 
|  | 1944 |  | 
|  | 1945 | if (!chunk_is_mmapped (p)) | 
|  | 1946 | { | 
|  | 1947 | /* Has legal address ... */ | 
|  | 1948 | if (p != av->top) | 
|  | 1949 | { | 
|  | 1950 | if (contiguous (av)) | 
|  | 1951 | { | 
|  | 1952 | assert (((char *) p) >= min_address); | 
|  | 1953 | assert (((char *) p + sz) <= ((char *) (av->top))); | 
|  | 1954 | } | 
|  | 1955 | } | 
|  | 1956 | else | 
|  | 1957 | { | 
|  | 1958 | /* top size is always at least MINSIZE */ | 
|  | 1959 | assert ((unsigned long) (sz) >= MINSIZE); | 
|  | 1960 | /* top predecessor always marked inuse */ | 
|  | 1961 | assert (prev_inuse (p)); | 
|  | 1962 | } | 
|  | 1963 | } | 
|  | 1964 | else | 
|  | 1965 | { | 
|  | 1966 | /* address is outside main heap  */ | 
|  | 1967 | if (contiguous (av) && av->top != initial_top (av)) | 
|  | 1968 | { | 
|  | 1969 | assert (((char *) p) < min_address || ((char *) p) >= max_address); | 
|  | 1970 | } | 
|  | 1971 | /* chunk is page-aligned */ | 
|  | 1972 | assert (((p->prev_size + sz) & (GLRO (dl_pagesize) - 1)) == 0); | 
|  | 1973 | /* mem is aligned */ | 
|  | 1974 | assert (aligned_OK (chunk2mem (p))); | 
|  | 1975 | } | 
|  | 1976 | } | 
|  | 1977 |  | 
|  | 1978 | /* | 
|  | 1979 | Properties of free chunks | 
|  | 1980 | */ | 
|  | 1981 |  | 
|  | 1982 | static void | 
|  | 1983 | do_check_free_chunk (mstate av, mchunkptr p) | 
|  | 1984 | { | 
|  | 1985 | INTERNAL_SIZE_T sz = p->size & ~(PREV_INUSE | NON_MAIN_ARENA); | 
|  | 1986 | mchunkptr next = chunk_at_offset (p, sz); | 
|  | 1987 |  | 
|  | 1988 | do_check_chunk (av, p); | 
|  | 1989 |  | 
|  | 1990 | /* Chunk must claim to be free ... */ | 
|  | 1991 | assert (!inuse (p)); | 
|  | 1992 | assert (!chunk_is_mmapped (p)); | 
|  | 1993 |  | 
|  | 1994 | /* Unless a special marker, must have OK fields */ | 
|  | 1995 | if ((unsigned long) (sz) >= MINSIZE) | 
|  | 1996 | { | 
|  | 1997 | assert ((sz & MALLOC_ALIGN_MASK) == 0); | 
|  | 1998 | assert (aligned_OK (chunk2mem (p))); | 
|  | 1999 | /* ... matching footer field */ | 
|  | 2000 | assert (next->prev_size == sz); | 
|  | 2001 | /* ... and is fully consolidated */ | 
|  | 2002 | assert (prev_inuse (p)); | 
|  | 2003 | assert (next == av->top || inuse (next)); | 
|  | 2004 |  | 
|  | 2005 | /* ... and has minimally sane links */ | 
|  | 2006 | assert (p->fd->bk == p); | 
|  | 2007 | assert (p->bk->fd == p); | 
|  | 2008 | } | 
|  | 2009 | else /* markers are always of size SIZE_SZ */ | 
|  | 2010 | assert (sz == SIZE_SZ); | 
|  | 2011 | } | 
|  | 2012 |  | 
|  | 2013 | /* | 
|  | 2014 | Properties of inuse chunks | 
|  | 2015 | */ | 
|  | 2016 |  | 
|  | 2017 | static void | 
|  | 2018 | do_check_inuse_chunk (mstate av, mchunkptr p) | 
|  | 2019 | { | 
|  | 2020 | mchunkptr next; | 
|  | 2021 |  | 
|  | 2022 | do_check_chunk (av, p); | 
|  | 2023 |  | 
|  | 2024 | if (chunk_is_mmapped (p)) | 
|  | 2025 | return; /* mmapped chunks have no next/prev */ | 
|  | 2026 |  | 
|  | 2027 | /* Check whether it claims to be in use ... */ | 
|  | 2028 | assert (inuse (p)); | 
|  | 2029 |  | 
|  | 2030 | next = next_chunk (p); | 
|  | 2031 |  | 
|  | 2032 | /* ... and is surrounded by OK chunks. | 
|  | 2033 | Since more things can be checked with free chunks than inuse ones, | 
|  | 2034 | if an inuse chunk borders them and debug is on, it's worth doing them. | 
|  | 2035 | */ | 
|  | 2036 | if (!prev_inuse (p)) | 
|  | 2037 | { | 
|  | 2038 | /* Note that we cannot even look at prev unless it is not inuse */ | 
|  | 2039 | mchunkptr prv = prev_chunk (p); | 
|  | 2040 | assert (next_chunk (prv) == p); | 
|  | 2041 | do_check_free_chunk (av, prv); | 
|  | 2042 | } | 
|  | 2043 |  | 
|  | 2044 | if (next == av->top) | 
|  | 2045 | { | 
|  | 2046 | assert (prev_inuse (next)); | 
|  | 2047 | assert (chunksize (next) >= MINSIZE); | 
|  | 2048 | } | 
|  | 2049 | else if (!inuse (next)) | 
|  | 2050 | do_check_free_chunk (av, next); | 
|  | 2051 | } | 
|  | 2052 |  | 
|  | 2053 | /* | 
|  | 2054 | Properties of chunks recycled from fastbins | 
|  | 2055 | */ | 
|  | 2056 |  | 
|  | 2057 | static void | 
|  | 2058 | do_check_remalloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s) | 
|  | 2059 | { | 
|  | 2060 | INTERNAL_SIZE_T sz = p->size & ~(PREV_INUSE | NON_MAIN_ARENA); | 
|  | 2061 |  | 
|  | 2062 | if (!chunk_is_mmapped (p)) | 
|  | 2063 | { | 
|  | 2064 | assert (av == arena_for_chunk (p)); | 
|  | 2065 | if (chunk_non_main_arena (p)) | 
|  | 2066 | assert (av != &main_arena); | 
|  | 2067 | else | 
|  | 2068 | assert (av == &main_arena); | 
|  | 2069 | } | 
|  | 2070 |  | 
|  | 2071 | do_check_inuse_chunk (av, p); | 
|  | 2072 |  | 
|  | 2073 | /* Legal size ... */ | 
|  | 2074 | assert ((sz & MALLOC_ALIGN_MASK) == 0); | 
|  | 2075 | assert ((unsigned long) (sz) >= MINSIZE); | 
|  | 2076 | /* ... and alignment */ | 
|  | 2077 | assert (aligned_OK (chunk2mem (p))); | 
|  | 2078 | /* chunk is less than MINSIZE more than request */ | 
|  | 2079 | assert ((long) (sz) - (long) (s) >= 0); | 
|  | 2080 | assert ((long) (sz) - (long) (s + MINSIZE) < 0); | 
|  | 2081 | } | 
|  | 2082 |  | 
|  | 2083 | /* | 
|  | 2084 | Properties of nonrecycled chunks at the point they are malloced | 
|  | 2085 | */ | 
|  | 2086 |  | 
|  | 2087 | static void | 
|  | 2088 | do_check_malloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s) | 
|  | 2089 | { | 
|  | 2090 | /* same as recycled case ... */ | 
|  | 2091 | do_check_remalloced_chunk (av, p, s); | 
|  | 2092 |  | 
|  | 2093 | /* | 
|  | 2094 | ... plus,  must obey implementation invariant that prev_inuse is | 
|  | 2095 | always true of any allocated chunk; i.e., that each allocated | 
|  | 2096 | chunk borders either a previously allocated and still in-use | 
|  | 2097 | chunk, or the base of its memory arena. This is ensured | 
|  | 2098 | by making all allocations from the `lowest' part of any found | 
|  | 2099 | chunk.  This does not necessarily hold however for chunks | 
|  | 2100 | recycled via fastbins. | 
|  | 2101 | */ | 
|  | 2102 |  | 
|  | 2103 | assert (prev_inuse (p)); | 
|  | 2104 | } | 
|  | 2105 |  | 
|  | 2106 |  | 
|  | 2107 | /* | 
|  | 2108 | Properties of malloc_state. | 
|  | 2109 |  | 
|  | 2110 | This may be useful for debugging malloc, as well as detecting user | 
|  | 2111 | programmer errors that somehow write into malloc_state. | 
|  | 2112 |  | 
|  | 2113 | If you are extending or experimenting with this malloc, you can | 
|  | 2114 | probably figure out how to hack this routine to print out or | 
|  | 2115 | display chunk addresses, sizes, bins, and other instrumentation. | 
|  | 2116 | */ | 
|  | 2117 |  | 
|  | 2118 | static void | 
|  | 2119 | do_check_malloc_state (mstate av) | 
|  | 2120 | { | 
|  | 2121 | int i; | 
|  | 2122 | mchunkptr p; | 
|  | 2123 | mchunkptr q; | 
|  | 2124 | mbinptr b; | 
|  | 2125 | unsigned int idx; | 
|  | 2126 | INTERNAL_SIZE_T size; | 
|  | 2127 | unsigned long total = 0; | 
|  | 2128 | int max_fast_bin; | 
|  | 2129 |  | 
|  | 2130 | /* internal size_t must be no wider than pointer type */ | 
|  | 2131 | assert (sizeof (INTERNAL_SIZE_T) <= sizeof (char *)); | 
|  | 2132 |  | 
|  | 2133 | /* alignment is a power of 2 */ | 
|  | 2134 | assert ((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT - 1)) == 0); | 
|  | 2135 |  | 
|  | 2136 | /* cannot run remaining checks until fully initialized */ | 
|  | 2137 | if (av->top == 0 || av->top == initial_top (av)) | 
|  | 2138 | return; | 
|  | 2139 |  | 
|  | 2140 | /* pagesize is a power of 2 */ | 
|  | 2141 | assert (powerof2(GLRO (dl_pagesize))); | 
|  | 2142 |  | 
|  | 2143 | /* A contiguous main_arena is consistent with sbrk_base.  */ | 
|  | 2144 | if (av == &main_arena && contiguous (av)) | 
|  | 2145 | assert ((char *) mp_.sbrk_base + av->system_mem == | 
|  | 2146 | (char *) av->top + chunksize (av->top)); | 
|  | 2147 |  | 
|  | 2148 | /* properties of fastbins */ | 
|  | 2149 |  | 
|  | 2150 | /* max_fast is in allowed range */ | 
|  | 2151 | assert ((get_max_fast () & ~1) <= request2size (MAX_FAST_SIZE)); | 
|  | 2152 |  | 
|  | 2153 | max_fast_bin = fastbin_index (get_max_fast ()); | 
|  | 2154 |  | 
|  | 2155 | for (i = 0; i < NFASTBINS; ++i) | 
|  | 2156 | { | 
|  | 2157 | p = fastbin (av, i); | 
|  | 2158 |  | 
|  | 2159 | /* The following test can only be performed for the main arena. | 
|  | 2160 | While mallopt calls malloc_consolidate to get rid of all fast | 
|  | 2161 | bins (especially those larger than the new maximum) this does | 
|  | 2162 | only happen for the main arena.  Trying to do this for any | 
|  | 2163 | other arena would mean those arenas have to be locked and | 
|  | 2164 | malloc_consolidate be called for them.  This is excessive.  And | 
|  | 2165 | even if this is acceptable to somebody it still cannot solve | 
|  | 2166 | the problem completely since if the arena is locked a | 
|  | 2167 | concurrent malloc call might create a new arena which then | 
|  | 2168 | could use the newly invalid fast bins.  */ | 
|  | 2169 |  | 
|  | 2170 | /* all bins past max_fast are empty */ | 
|  | 2171 | if (av == &main_arena && i > max_fast_bin) | 
|  | 2172 | assert (p == 0); | 
|  | 2173 |  | 
|  | 2174 | while (p != 0) | 
|  | 2175 | { | 
|  | 2176 | /* each chunk claims to be inuse */ | 
|  | 2177 | do_check_inuse_chunk (av, p); | 
|  | 2178 | total += chunksize (p); | 
|  | 2179 | /* chunk belongs in this bin */ | 
|  | 2180 | assert (fastbin_index (chunksize (p)) == i); | 
|  | 2181 | p = p->fd; | 
|  | 2182 | } | 
|  | 2183 | } | 
|  | 2184 |  | 
|  | 2185 | if (total != 0) | 
|  | 2186 | assert (have_fastchunks (av)); | 
|  | 2187 | else if (!have_fastchunks (av)) | 
|  | 2188 | assert (total == 0); | 
|  | 2189 |  | 
|  | 2190 | /* check normal bins */ | 
|  | 2191 | for (i = 1; i < NBINS; ++i) | 
|  | 2192 | { | 
|  | 2193 | b = bin_at (av, i); | 
|  | 2194 |  | 
|  | 2195 | /* binmap is accurate (except for bin 1 == unsorted_chunks) */ | 
|  | 2196 | if (i >= 2) | 
|  | 2197 | { | 
|  | 2198 | unsigned int binbit = get_binmap (av, i); | 
|  | 2199 | int empty = last (b) == b; | 
|  | 2200 | if (!binbit) | 
|  | 2201 | assert (empty); | 
|  | 2202 | else if (!empty) | 
|  | 2203 | assert (binbit); | 
|  | 2204 | } | 
|  | 2205 |  | 
|  | 2206 | for (p = last (b); p != b; p = p->bk) | 
|  | 2207 | { | 
|  | 2208 | /* each chunk claims to be free */ | 
|  | 2209 | do_check_free_chunk (av, p); | 
|  | 2210 | size = chunksize (p); | 
|  | 2211 | total += size; | 
|  | 2212 | if (i >= 2) | 
|  | 2213 | { | 
|  | 2214 | /* chunk belongs in bin */ | 
|  | 2215 | idx = bin_index (size); | 
|  | 2216 | assert (idx == i); | 
|  | 2217 | /* lists are sorted */ | 
|  | 2218 | assert (p->bk == b || | 
|  | 2219 | (unsigned long) chunksize (p->bk) >= (unsigned long) chunksize (p)); | 
|  | 2220 |  | 
|  | 2221 | if (!in_smallbin_range (size)) | 
|  | 2222 | { | 
|  | 2223 | if (p->fd_nextsize != NULL) | 
|  | 2224 | { | 
|  | 2225 | if (p->fd_nextsize == p) | 
|  | 2226 | assert (p->bk_nextsize == p); | 
|  | 2227 | else | 
|  | 2228 | { | 
|  | 2229 | if (p->fd_nextsize == first (b)) | 
|  | 2230 | assert (chunksize (p) < chunksize (p->fd_nextsize)); | 
|  | 2231 | else | 
|  | 2232 | assert (chunksize (p) > chunksize (p->fd_nextsize)); | 
|  | 2233 |  | 
|  | 2234 | if (p == first (b)) | 
|  | 2235 | assert (chunksize (p) > chunksize (p->bk_nextsize)); | 
|  | 2236 | else | 
|  | 2237 | assert (chunksize (p) < chunksize (p->bk_nextsize)); | 
|  | 2238 | } | 
|  | 2239 | } | 
|  | 2240 | else | 
|  | 2241 | assert (p->bk_nextsize == NULL); | 
|  | 2242 | } | 
|  | 2243 | } | 
|  | 2244 | else if (!in_smallbin_range (size)) | 
|  | 2245 | assert (p->fd_nextsize == NULL && p->bk_nextsize == NULL); | 
|  | 2246 | /* chunk is followed by a legal chain of inuse chunks */ | 
|  | 2247 | for (q = next_chunk (p); | 
|  | 2248 | (q != av->top && inuse (q) && | 
|  | 2249 | (unsigned long) (chunksize (q)) >= MINSIZE); | 
|  | 2250 | q = next_chunk (q)) | 
|  | 2251 | do_check_inuse_chunk (av, q); | 
|  | 2252 | } | 
|  | 2253 | } | 
|  | 2254 |  | 
|  | 2255 | /* top chunk is OK */ | 
|  | 2256 | check_chunk (av, av->top); | 
|  | 2257 | } | 
|  | 2258 | #endif | 
|  | 2259 |  | 
|  | 2260 |  | 
|  | 2261 | /* ----------------- Support for debugging hooks -------------------- */ | 
|  | 2262 | #include "hooks.c" | 
|  | 2263 |  | 
|  | 2264 |  | 
|  | 2265 | /* ----------- Routines dealing with system allocation -------------- */ | 
|  | 2266 |  | 
|  | 2267 | /* | 
|  | 2268 | sysmalloc handles malloc cases requiring more memory from the system. | 
|  | 2269 | On entry, it is assumed that av->top does not have enough | 
|  | 2270 | space to service request for nb bytes, thus requiring that av->top | 
|  | 2271 | be extended or replaced. | 
|  | 2272 | */ | 
|  | 2273 |  | 
|  | 2274 | static void * | 
|  | 2275 | sysmalloc (INTERNAL_SIZE_T nb, mstate av) | 
|  | 2276 | { | 
|  | 2277 | mchunkptr old_top;              /* incoming value of av->top */ | 
|  | 2278 | INTERNAL_SIZE_T old_size;       /* its size */ | 
|  | 2279 | char *old_end;                  /* its end address */ | 
|  | 2280 |  | 
|  | 2281 | long size;                      /* arg to first MORECORE or mmap call */ | 
|  | 2282 | char *brk;                      /* return value from MORECORE */ | 
|  | 2283 |  | 
|  | 2284 | long correction;                /* arg to 2nd MORECORE call */ | 
|  | 2285 | char *snd_brk;                  /* 2nd return val */ | 
|  | 2286 |  | 
|  | 2287 | INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of new space */ | 
|  | 2288 | INTERNAL_SIZE_T end_misalign;   /* partial page left at end of new space */ | 
|  | 2289 | char *aligned_brk;              /* aligned offset into brk */ | 
|  | 2290 |  | 
|  | 2291 | mchunkptr p;                    /* the allocated/returned chunk */ | 
|  | 2292 | mchunkptr remainder;            /* remainder from allocation */ | 
|  | 2293 | unsigned long remainder_size;   /* its size */ | 
|  | 2294 |  | 
|  | 2295 |  | 
|  | 2296 | size_t pagesize = GLRO (dl_pagesize); | 
|  | 2297 | bool tried_mmap = false; | 
|  | 2298 |  | 
|  | 2299 |  | 
|  | 2300 | /* | 
|  | 2301 | If have mmap, and the request size meets the mmap threshold, and | 
|  | 2302 | the system supports mmap, and there are few enough currently | 
|  | 2303 | allocated mmapped regions, try to directly map this request | 
|  | 2304 | rather than expanding top. | 
|  | 2305 | */ | 
|  | 2306 |  | 
|  | 2307 | if (av == NULL | 
|  | 2308 | || ((unsigned long) (nb) >= (unsigned long) (mp_.mmap_threshold) | 
|  | 2309 | && (mp_.n_mmaps < mp_.n_mmaps_max))) | 
|  | 2310 | { | 
|  | 2311 | char *mm;           /* return value from mmap call*/ | 
|  | 2312 |  | 
|  | 2313 | try_mmap: | 
|  | 2314 | /* | 
|  | 2315 | Round up size to nearest page.  For mmapped chunks, the overhead | 
|  | 2316 | is one SIZE_SZ unit larger than for normal chunks, because there | 
|  | 2317 | is no following chunk whose prev_size field could be used. | 
|  | 2318 |  | 
|  | 2319 | See the front_misalign handling below, for glibc there is no | 
|  | 2320 | need for further alignments unless we have have high alignment. | 
|  | 2321 | */ | 
|  | 2322 | if (MALLOC_ALIGNMENT == 2 * SIZE_SZ) | 
|  | 2323 | size = ALIGN_UP (nb + SIZE_SZ, pagesize); | 
|  | 2324 | else | 
|  | 2325 | size = ALIGN_UP (nb + SIZE_SZ + MALLOC_ALIGN_MASK, pagesize); | 
|  | 2326 | tried_mmap = true; | 
|  | 2327 |  | 
|  | 2328 | /* Don't try if size wraps around 0 */ | 
|  | 2329 | if ((unsigned long) (size) > (unsigned long) (nb)) | 
|  | 2330 | { | 
|  | 2331 | mm = (char *) (MMAP (0, size, PROT_READ | PROT_WRITE, 0)); | 
|  | 2332 |  | 
|  | 2333 | if (mm != MAP_FAILED) | 
|  | 2334 | { | 
|  | 2335 | /* | 
|  | 2336 | The offset to the start of the mmapped region is stored | 
|  | 2337 | in the prev_size field of the chunk. This allows us to adjust | 
|  | 2338 | returned start address to meet alignment requirements here | 
|  | 2339 | and in memalign(), and still be able to compute proper | 
|  | 2340 | address argument for later munmap in free() and realloc(). | 
|  | 2341 | */ | 
|  | 2342 |  | 
|  | 2343 | if (MALLOC_ALIGNMENT == 2 * SIZE_SZ) | 
|  | 2344 | { | 
|  | 2345 | /* For glibc, chunk2mem increases the address by 2*SIZE_SZ and | 
|  | 2346 | MALLOC_ALIGN_MASK is 2*SIZE_SZ-1.  Each mmap'ed area is page | 
|  | 2347 | aligned and therefore definitely MALLOC_ALIGN_MASK-aligned.  */ | 
|  | 2348 | assert (((INTERNAL_SIZE_T) chunk2mem (mm) & MALLOC_ALIGN_MASK) == 0); | 
|  | 2349 | front_misalign = 0; | 
|  | 2350 | } | 
|  | 2351 | else | 
|  | 2352 | front_misalign = (INTERNAL_SIZE_T) chunk2mem (mm) & MALLOC_ALIGN_MASK; | 
|  | 2353 | if (front_misalign > 0) | 
|  | 2354 | { | 
|  | 2355 | correction = MALLOC_ALIGNMENT - front_misalign; | 
|  | 2356 | p = (mchunkptr) (mm + correction); | 
|  | 2357 | p->prev_size = correction; | 
|  | 2358 | set_head (p, (size - correction) | IS_MMAPPED); | 
|  | 2359 | } | 
|  | 2360 | else | 
|  | 2361 | { | 
|  | 2362 | p = (mchunkptr) mm; | 
|  | 2363 | set_head (p, size | IS_MMAPPED); | 
|  | 2364 | } | 
|  | 2365 |  | 
|  | 2366 | /* update statistics */ | 
|  | 2367 |  | 
|  | 2368 | int new = atomic_exchange_and_add (&mp_.n_mmaps, 1) + 1; | 
|  | 2369 | atomic_max (&mp_.max_n_mmaps, new); | 
|  | 2370 |  | 
|  | 2371 | unsigned long sum; | 
|  | 2372 | sum = atomic_exchange_and_add (&mp_.mmapped_mem, size) + size; | 
|  | 2373 | atomic_max (&mp_.max_mmapped_mem, sum); | 
|  | 2374 |  | 
|  | 2375 | check_chunk (av, p); | 
|  | 2376 |  | 
|  | 2377 | return chunk2mem (p); | 
|  | 2378 | } | 
|  | 2379 | } | 
|  | 2380 | } | 
|  | 2381 |  | 
|  | 2382 | /* There are no usable arenas and mmap also failed.  */ | 
|  | 2383 | if (av == NULL) | 
|  | 2384 | return 0; | 
|  | 2385 |  | 
|  | 2386 | /* Record incoming configuration of top */ | 
|  | 2387 |  | 
|  | 2388 | old_top = av->top; | 
|  | 2389 | old_size = chunksize (old_top); | 
|  | 2390 | old_end = (char *) (chunk_at_offset (old_top, old_size)); | 
|  | 2391 |  | 
|  | 2392 | brk = snd_brk = (char *) (MORECORE_FAILURE); | 
|  | 2393 |  | 
|  | 2394 | /* | 
|  | 2395 | If not the first time through, we require old_size to be | 
|  | 2396 | at least MINSIZE and to have prev_inuse set. | 
|  | 2397 | */ | 
|  | 2398 |  | 
|  | 2399 | assert ((old_top == initial_top (av) && old_size == 0) || | 
|  | 2400 | ((unsigned long) (old_size) >= MINSIZE && | 
|  | 2401 | prev_inuse (old_top) && | 
|  | 2402 | ((unsigned long) old_end & (pagesize - 1)) == 0)); | 
|  | 2403 |  | 
|  | 2404 | /* Precondition: not enough current space to satisfy nb request */ | 
|  | 2405 | assert ((unsigned long) (old_size) < (unsigned long) (nb + MINSIZE)); | 
|  | 2406 |  | 
|  | 2407 |  | 
|  | 2408 | if (av != &main_arena) | 
|  | 2409 | { | 
|  | 2410 | heap_info *old_heap, *heap; | 
|  | 2411 | size_t old_heap_size; | 
|  | 2412 |  | 
|  | 2413 | /* First try to extend the current heap. */ | 
|  | 2414 | old_heap = heap_for_ptr (old_top); | 
|  | 2415 | old_heap_size = old_heap->size; | 
|  | 2416 | if ((long) (MINSIZE + nb - old_size) > 0 | 
|  | 2417 | && grow_heap (old_heap, MINSIZE + nb - old_size) == 0) | 
|  | 2418 | { | 
|  | 2419 | av->system_mem += old_heap->size - old_heap_size; | 
|  | 2420 | arena_mem += old_heap->size - old_heap_size; | 
|  | 2421 | set_head (old_top, (((char *) old_heap + old_heap->size) - (char *) old_top) | 
|  | 2422 | | PREV_INUSE); | 
|  | 2423 | } | 
|  | 2424 | else if ((heap = new_heap (nb + (MINSIZE + sizeof (*heap)), mp_.top_pad))) | 
|  | 2425 | { | 
|  | 2426 | /* Use a newly allocated heap.  */ | 
|  | 2427 | heap->ar_ptr = av; | 
|  | 2428 | heap->prev = old_heap; | 
|  | 2429 | av->system_mem += heap->size; | 
|  | 2430 | arena_mem += heap->size; | 
|  | 2431 | /* Set up the new top.  */ | 
|  | 2432 | top (av) = chunk_at_offset (heap, sizeof (*heap)); | 
|  | 2433 | set_head (top (av), (heap->size - sizeof (*heap)) | PREV_INUSE); | 
|  | 2434 |  | 
|  | 2435 | /* Setup fencepost and free the old top chunk with a multiple of | 
|  | 2436 | MALLOC_ALIGNMENT in size. */ | 
|  | 2437 | /* The fencepost takes at least MINSIZE bytes, because it might | 
|  | 2438 | become the top chunk again later.  Note that a footer is set | 
|  | 2439 | up, too, although the chunk is marked in use. */ | 
|  | 2440 | old_size = (old_size - MINSIZE) & ~MALLOC_ALIGN_MASK; | 
|  | 2441 | set_head (chunk_at_offset (old_top, old_size + 2 * SIZE_SZ), 0 | PREV_INUSE); | 
|  | 2442 | if (old_size >= MINSIZE) | 
|  | 2443 | { | 
|  | 2444 | set_head (chunk_at_offset (old_top, old_size), (2 * SIZE_SZ) | PREV_INUSE); | 
|  | 2445 | set_foot (chunk_at_offset (old_top, old_size), (2 * SIZE_SZ)); | 
|  | 2446 | set_head (old_top, old_size | PREV_INUSE | NON_MAIN_ARENA); | 
|  | 2447 | _int_free (av, old_top, 1); | 
|  | 2448 | } | 
|  | 2449 | else | 
|  | 2450 | { | 
|  | 2451 | set_head (old_top, (old_size + 2 * SIZE_SZ) | PREV_INUSE); | 
|  | 2452 | set_foot (old_top, (old_size + 2 * SIZE_SZ)); | 
|  | 2453 | } | 
|  | 2454 | } | 
|  | 2455 | else if (!tried_mmap) | 
|  | 2456 | /* We can at least try to use to mmap memory.  */ | 
|  | 2457 | goto try_mmap; | 
|  | 2458 | } | 
|  | 2459 | else     /* av == main_arena */ | 
|  | 2460 |  | 
|  | 2461 |  | 
|  | 2462 | { /* Request enough space for nb + pad + overhead */ | 
|  | 2463 | size = nb + mp_.top_pad + MINSIZE; | 
|  | 2464 |  | 
|  | 2465 | /* | 
|  | 2466 | If contiguous, we can subtract out existing space that we hope to | 
|  | 2467 | combine with new space. We add it back later only if | 
|  | 2468 | we don't actually get contiguous space. | 
|  | 2469 | */ | 
|  | 2470 |  | 
|  | 2471 | if (contiguous (av)) | 
|  | 2472 | size -= old_size; | 
|  | 2473 |  | 
|  | 2474 | /* | 
|  | 2475 | Round to a multiple of page size. | 
|  | 2476 | If MORECORE is not contiguous, this ensures that we only call it | 
|  | 2477 | with whole-page arguments.  And if MORECORE is contiguous and | 
|  | 2478 | this is not first time through, this preserves page-alignment of | 
|  | 2479 | previous calls. Otherwise, we correct to page-align below. | 
|  | 2480 | */ | 
|  | 2481 |  | 
|  | 2482 | size = ALIGN_UP (size, pagesize); | 
|  | 2483 |  | 
|  | 2484 | /* | 
|  | 2485 | Don't try to call MORECORE if argument is so big as to appear | 
|  | 2486 | negative. Note that since mmap takes size_t arg, it may succeed | 
|  | 2487 | below even if we cannot call MORECORE. | 
|  | 2488 | */ | 
|  | 2489 |  | 
|  | 2490 | if (size > 0) | 
|  | 2491 | { | 
|  | 2492 | brk = (char *) (MORECORE (size)); | 
|  | 2493 | LIBC_PROBE (memory_sbrk_more, 2, brk, size); | 
|  | 2494 | } | 
|  | 2495 |  | 
|  | 2496 | if (brk != (char *) (MORECORE_FAILURE)) | 
|  | 2497 | { | 
|  | 2498 | /* Call the `morecore' hook if necessary.  */ | 
|  | 2499 | void (*hook) (void) = atomic_forced_read (__after_morecore_hook); | 
|  | 2500 | if (__builtin_expect (hook != NULL, 0)) | 
|  | 2501 | (*hook)(); | 
|  | 2502 | } | 
|  | 2503 | else | 
|  | 2504 | { | 
|  | 2505 | /* | 
|  | 2506 | If have mmap, try using it as a backup when MORECORE fails or | 
|  | 2507 | cannot be used. This is worth doing on systems that have "holes" in | 
|  | 2508 | address space, so sbrk cannot extend to give contiguous space, but | 
|  | 2509 | space is available elsewhere.  Note that we ignore mmap max count | 
|  | 2510 | and threshold limits, since the space will not be used as a | 
|  | 2511 | segregated mmap region. | 
|  | 2512 | */ | 
|  | 2513 |  | 
|  | 2514 | /* Cannot merge with old top, so add its size back in */ | 
|  | 2515 | if (contiguous (av)) | 
|  | 2516 | size = ALIGN_UP (size + old_size, pagesize); | 
|  | 2517 |  | 
|  | 2518 | /* If we are relying on mmap as backup, then use larger units */ | 
|  | 2519 | if ((unsigned long) (size) < (unsigned long) (MMAP_AS_MORECORE_SIZE)) | 
|  | 2520 | size = MMAP_AS_MORECORE_SIZE; | 
|  | 2521 |  | 
|  | 2522 | /* Don't try if size wraps around 0 */ | 
|  | 2523 | if ((unsigned long) (size) > (unsigned long) (nb)) | 
|  | 2524 | { | 
|  | 2525 | char *mbrk = (char *) (MMAP (0, size, PROT_READ | PROT_WRITE, 0)); | 
|  | 2526 |  | 
|  | 2527 | if (mbrk != MAP_FAILED) | 
|  | 2528 | { | 
|  | 2529 | /* We do not need, and cannot use, another sbrk call to find end */ | 
|  | 2530 | brk = mbrk; | 
|  | 2531 | snd_brk = brk + size; | 
|  | 2532 |  | 
|  | 2533 | /* | 
|  | 2534 | Record that we no longer have a contiguous sbrk region. | 
|  | 2535 | After the first time mmap is used as backup, we do not | 
|  | 2536 | ever rely on contiguous space since this could incorrectly | 
|  | 2537 | bridge regions. | 
|  | 2538 | */ | 
|  | 2539 | set_noncontiguous (av); | 
|  | 2540 | } | 
|  | 2541 | } | 
|  | 2542 | } | 
|  | 2543 |  | 
|  | 2544 | if (brk != (char *) (MORECORE_FAILURE)) | 
|  | 2545 | { | 
|  | 2546 | if (mp_.sbrk_base == 0) | 
|  | 2547 | mp_.sbrk_base = brk; | 
|  | 2548 | av->system_mem += size; | 
|  | 2549 |  | 
|  | 2550 | /* | 
|  | 2551 | If MORECORE extends previous space, we can likewise extend top size. | 
|  | 2552 | */ | 
|  | 2553 |  | 
|  | 2554 | if (brk == old_end && snd_brk == (char *) (MORECORE_FAILURE)) | 
|  | 2555 | set_head (old_top, (size + old_size) | PREV_INUSE); | 
|  | 2556 |  | 
|  | 2557 | else if (contiguous (av) && old_size && brk < old_end) | 
|  | 2558 | { | 
|  | 2559 | /* Oops!  Someone else killed our space..  Can't touch anything.  */ | 
|  | 2560 | malloc_printerr (3, "break adjusted to free malloc space", brk, | 
|  | 2561 | av); | 
|  | 2562 | } | 
|  | 2563 |  | 
|  | 2564 | /* | 
|  | 2565 | Otherwise, make adjustments: | 
|  | 2566 |  | 
|  | 2567 | * If the first time through or noncontiguous, we need to call sbrk | 
|  | 2568 | just to find out where the end of memory lies. | 
|  | 2569 |  | 
|  | 2570 | * We need to ensure that all returned chunks from malloc will meet | 
|  | 2571 | MALLOC_ALIGNMENT | 
|  | 2572 |  | 
|  | 2573 | * If there was an intervening foreign sbrk, we need to adjust sbrk | 
|  | 2574 | request size to account for fact that we will not be able to | 
|  | 2575 | combine new space with existing space in old_top. | 
|  | 2576 |  | 
|  | 2577 | * Almost all systems internally allocate whole pages at a time, in | 
|  | 2578 | which case we might as well use the whole last page of request. | 
|  | 2579 | So we allocate enough more memory to hit a page boundary now, | 
|  | 2580 | which in turn causes future contiguous calls to page-align. | 
|  | 2581 | */ | 
|  | 2582 |  | 
|  | 2583 | else | 
|  | 2584 | { | 
|  | 2585 | front_misalign = 0; | 
|  | 2586 | end_misalign = 0; | 
|  | 2587 | correction = 0; | 
|  | 2588 | aligned_brk = brk; | 
|  | 2589 |  | 
|  | 2590 | /* handle contiguous cases */ | 
|  | 2591 | if (contiguous (av)) | 
|  | 2592 | { | 
|  | 2593 | /* Count foreign sbrk as system_mem.  */ | 
|  | 2594 | if (old_size) | 
|  | 2595 | av->system_mem += brk - old_end; | 
|  | 2596 |  | 
|  | 2597 | /* Guarantee alignment of first new chunk made from this space */ | 
|  | 2598 |  | 
|  | 2599 | front_misalign = (INTERNAL_SIZE_T) chunk2mem (brk) & MALLOC_ALIGN_MASK; | 
|  | 2600 | if (front_misalign > 0) | 
|  | 2601 | { | 
|  | 2602 | /* | 
|  | 2603 | Skip over some bytes to arrive at an aligned position. | 
|  | 2604 | We don't need to specially mark these wasted front bytes. | 
|  | 2605 | They will never be accessed anyway because | 
|  | 2606 | prev_inuse of av->top (and any chunk created from its start) | 
|  | 2607 | is always true after initialization. | 
|  | 2608 | */ | 
|  | 2609 |  | 
|  | 2610 | correction = MALLOC_ALIGNMENT - front_misalign; | 
|  | 2611 | aligned_brk += correction; | 
|  | 2612 | } | 
|  | 2613 |  | 
|  | 2614 | /* | 
|  | 2615 | If this isn't adjacent to existing space, then we will not | 
|  | 2616 | be able to merge with old_top space, so must add to 2nd request. | 
|  | 2617 | */ | 
|  | 2618 |  | 
|  | 2619 | correction += old_size; | 
|  | 2620 |  | 
|  | 2621 | /* Extend the end address to hit a page boundary */ | 
|  | 2622 | end_misalign = (INTERNAL_SIZE_T) (brk + size + correction); | 
|  | 2623 | correction += (ALIGN_UP (end_misalign, pagesize)) - end_misalign; | 
|  | 2624 |  | 
|  | 2625 | assert (correction >= 0); | 
|  | 2626 | snd_brk = (char *) (MORECORE (correction)); | 
|  | 2627 |  | 
|  | 2628 | /* | 
|  | 2629 | If can't allocate correction, try to at least find out current | 
|  | 2630 | brk.  It might be enough to proceed without failing. | 
|  | 2631 |  | 
|  | 2632 | Note that if second sbrk did NOT fail, we assume that space | 
|  | 2633 | is contiguous with first sbrk. This is a safe assumption unless | 
|  | 2634 | program is multithreaded but doesn't use locks and a foreign sbrk | 
|  | 2635 | occurred between our first and second calls. | 
|  | 2636 | */ | 
|  | 2637 |  | 
|  | 2638 | if (snd_brk == (char *) (MORECORE_FAILURE)) | 
|  | 2639 | { | 
|  | 2640 | correction = 0; | 
|  | 2641 | snd_brk = (char *) (MORECORE (0)); | 
|  | 2642 | } | 
|  | 2643 | else | 
|  | 2644 | { | 
|  | 2645 | /* Call the `morecore' hook if necessary.  */ | 
|  | 2646 | void (*hook) (void) = atomic_forced_read (__after_morecore_hook); | 
|  | 2647 | if (__builtin_expect (hook != NULL, 0)) | 
|  | 2648 | (*hook)(); | 
|  | 2649 | } | 
|  | 2650 | } | 
|  | 2651 |  | 
|  | 2652 | /* handle non-contiguous cases */ | 
|  | 2653 | else | 
|  | 2654 | { | 
|  | 2655 | if (MALLOC_ALIGNMENT == 2 * SIZE_SZ) | 
|  | 2656 | /* MORECORE/mmap must correctly align */ | 
|  | 2657 | assert (((unsigned long) chunk2mem (brk) & MALLOC_ALIGN_MASK) == 0); | 
|  | 2658 | else | 
|  | 2659 | { | 
|  | 2660 | front_misalign = (INTERNAL_SIZE_T) chunk2mem (brk) & MALLOC_ALIGN_MASK; | 
|  | 2661 | if (front_misalign > 0) | 
|  | 2662 | { | 
|  | 2663 | /* | 
|  | 2664 | Skip over some bytes to arrive at an aligned position. | 
|  | 2665 | We don't need to specially mark these wasted front bytes. | 
|  | 2666 | They will never be accessed anyway because | 
|  | 2667 | prev_inuse of av->top (and any chunk created from its start) | 
|  | 2668 | is always true after initialization. | 
|  | 2669 | */ | 
|  | 2670 |  | 
|  | 2671 | aligned_brk += MALLOC_ALIGNMENT - front_misalign; | 
|  | 2672 | } | 
|  | 2673 | } | 
|  | 2674 |  | 
|  | 2675 | /* Find out current end of memory */ | 
|  | 2676 | if (snd_brk == (char *) (MORECORE_FAILURE)) | 
|  | 2677 | { | 
|  | 2678 | snd_brk = (char *) (MORECORE (0)); | 
|  | 2679 | } | 
|  | 2680 | } | 
|  | 2681 |  | 
|  | 2682 | /* Adjust top based on results of second sbrk */ | 
|  | 2683 | if (snd_brk != (char *) (MORECORE_FAILURE)) | 
|  | 2684 | { | 
|  | 2685 | av->top = (mchunkptr) aligned_brk; | 
|  | 2686 | set_head (av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE); | 
|  | 2687 | av->system_mem += correction; | 
|  | 2688 |  | 
|  | 2689 | /* | 
|  | 2690 | If not the first time through, we either have a | 
|  | 2691 | gap due to foreign sbrk or a non-contiguous region.  Insert a | 
|  | 2692 | double fencepost at old_top to prevent consolidation with space | 
|  | 2693 | we don't own. These fenceposts are artificial chunks that are | 
|  | 2694 | marked as inuse and are in any case too small to use.  We need | 
|  | 2695 | two to make sizes and alignments work out. | 
|  | 2696 | */ | 
|  | 2697 |  | 
|  | 2698 | if (old_size != 0) | 
|  | 2699 | { | 
|  | 2700 | /* | 
|  | 2701 | Shrink old_top to insert fenceposts, keeping size a | 
|  | 2702 | multiple of MALLOC_ALIGNMENT. We know there is at least | 
|  | 2703 | enough space in old_top to do this. | 
|  | 2704 | */ | 
|  | 2705 | old_size = (old_size - 4 * SIZE_SZ) & ~MALLOC_ALIGN_MASK; | 
|  | 2706 | set_head (old_top, old_size | PREV_INUSE); | 
|  | 2707 |  | 
|  | 2708 | /* | 
|  | 2709 | Note that the following assignments completely overwrite | 
|  | 2710 | old_top when old_size was previously MINSIZE.  This is | 
|  | 2711 | intentional. We need the fencepost, even if old_top otherwise gets | 
|  | 2712 | lost. | 
|  | 2713 | */ | 
|  | 2714 | chunk_at_offset (old_top, old_size)->size = | 
|  | 2715 | (2 * SIZE_SZ) | PREV_INUSE; | 
|  | 2716 |  | 
|  | 2717 | chunk_at_offset (old_top, old_size + 2 * SIZE_SZ)->size = | 
|  | 2718 | (2 * SIZE_SZ) | PREV_INUSE; | 
|  | 2719 |  | 
|  | 2720 | /* If possible, release the rest. */ | 
|  | 2721 | if (old_size >= MINSIZE) | 
|  | 2722 | { | 
|  | 2723 | _int_free (av, old_top, 1); | 
|  | 2724 | } | 
|  | 2725 | } | 
|  | 2726 | } | 
|  | 2727 | } | 
|  | 2728 | } | 
|  | 2729 | } /* if (av !=  &main_arena) */ | 
|  | 2730 |  | 
|  | 2731 | if ((unsigned long) av->system_mem > (unsigned long) (av->max_system_mem)) | 
|  | 2732 | av->max_system_mem = av->system_mem; | 
|  | 2733 | check_malloc_state (av); | 
|  | 2734 |  | 
|  | 2735 | /* finally, do the allocation */ | 
|  | 2736 | p = av->top; | 
|  | 2737 | size = chunksize (p); | 
|  | 2738 |  | 
|  | 2739 | /* check that one of the above allocation paths succeeded */ | 
|  | 2740 | if ((unsigned long) (size) >= (unsigned long) (nb + MINSIZE)) | 
|  | 2741 | { | 
|  | 2742 | remainder_size = size - nb; | 
|  | 2743 | remainder = chunk_at_offset (p, nb); | 
|  | 2744 | av->top = remainder; | 
|  | 2745 | set_head (p, nb | PREV_INUSE | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 2746 | set_head (remainder, remainder_size | PREV_INUSE); | 
|  | 2747 | check_malloced_chunk (av, p, nb); | 
|  | 2748 | return chunk2mem (p); | 
|  | 2749 | } | 
|  | 2750 |  | 
|  | 2751 | /* catch all failure paths */ | 
|  | 2752 | __set_errno (ENOMEM); | 
|  | 2753 | return 0; | 
|  | 2754 | } | 
|  | 2755 |  | 
|  | 2756 |  | 
|  | 2757 | /* | 
|  | 2758 | systrim is an inverse of sorts to sysmalloc.  It gives memory back | 
|  | 2759 | to the system (via negative arguments to sbrk) if there is unused | 
|  | 2760 | memory at the `high' end of the malloc pool. It is called | 
|  | 2761 | automatically by free() when top space exceeds the trim | 
|  | 2762 | threshold. It is also called by the public malloc_trim routine.  It | 
|  | 2763 | returns 1 if it actually released any memory, else 0. | 
|  | 2764 | */ | 
|  | 2765 |  | 
|  | 2766 | static int | 
|  | 2767 | systrim (size_t pad, mstate av) | 
|  | 2768 | { | 
|  | 2769 | long top_size;         /* Amount of top-most memory */ | 
|  | 2770 | long extra;            /* Amount to release */ | 
|  | 2771 | long released;         /* Amount actually released */ | 
|  | 2772 | char *current_brk;     /* address returned by pre-check sbrk call */ | 
|  | 2773 | char *new_brk;         /* address returned by post-check sbrk call */ | 
|  | 2774 | size_t pagesize; | 
|  | 2775 | long top_area; | 
|  | 2776 |  | 
|  | 2777 | pagesize = GLRO (dl_pagesize); | 
|  | 2778 | top_size = chunksize (av->top); | 
|  | 2779 |  | 
|  | 2780 | top_area = top_size - MINSIZE - 1; | 
|  | 2781 | if (top_area <= pad) | 
|  | 2782 | return 0; | 
|  | 2783 |  | 
|  | 2784 | /* Release in pagesize units and round down to the nearest page.  */ | 
|  | 2785 | extra = ALIGN_DOWN(top_area - pad, pagesize); | 
|  | 2786 |  | 
|  | 2787 | if (extra == 0) | 
|  | 2788 | return 0; | 
|  | 2789 |  | 
|  | 2790 | /* | 
|  | 2791 | Only proceed if end of memory is where we last set it. | 
|  | 2792 | This avoids problems if there were foreign sbrk calls. | 
|  | 2793 | */ | 
|  | 2794 | current_brk = (char *) (MORECORE (0)); | 
|  | 2795 | if (current_brk == (char *) (av->top) + top_size) | 
|  | 2796 | { | 
|  | 2797 | /* | 
|  | 2798 | Attempt to release memory. We ignore MORECORE return value, | 
|  | 2799 | and instead call again to find out where new end of memory is. | 
|  | 2800 | This avoids problems if first call releases less than we asked, | 
|  | 2801 | of if failure somehow altered brk value. (We could still | 
|  | 2802 | encounter problems if it altered brk in some very bad way, | 
|  | 2803 | but the only thing we can do is adjust anyway, which will cause | 
|  | 2804 | some downstream failure.) | 
|  | 2805 | */ | 
|  | 2806 |  | 
|  | 2807 | MORECORE (-extra); | 
|  | 2808 | /* Call the `morecore' hook if necessary.  */ | 
|  | 2809 | void (*hook) (void) = atomic_forced_read (__after_morecore_hook); | 
|  | 2810 | if (__builtin_expect (hook != NULL, 0)) | 
|  | 2811 | (*hook)(); | 
|  | 2812 | new_brk = (char *) (MORECORE (0)); | 
|  | 2813 |  | 
|  | 2814 | LIBC_PROBE (memory_sbrk_less, 2, new_brk, extra); | 
|  | 2815 |  | 
|  | 2816 | if (new_brk != (char *) MORECORE_FAILURE) | 
|  | 2817 | { | 
|  | 2818 | released = (long) (current_brk - new_brk); | 
|  | 2819 |  | 
|  | 2820 | if (released != 0) | 
|  | 2821 | { | 
|  | 2822 | /* Success. Adjust top. */ | 
|  | 2823 | av->system_mem -= released; | 
|  | 2824 | set_head (av->top, (top_size - released) | PREV_INUSE); | 
|  | 2825 | check_malloc_state (av); | 
|  | 2826 | return 1; | 
|  | 2827 | } | 
|  | 2828 | } | 
|  | 2829 | } | 
|  | 2830 | return 0; | 
|  | 2831 | } | 
|  | 2832 |  | 
|  | 2833 | static void | 
|  | 2834 | internal_function | 
|  | 2835 | munmap_chunk (mchunkptr p) | 
|  | 2836 | { | 
|  | 2837 | INTERNAL_SIZE_T size = chunksize (p); | 
|  | 2838 |  | 
|  | 2839 | assert (chunk_is_mmapped (p)); | 
|  | 2840 |  | 
|  | 2841 | uintptr_t block = (uintptr_t) p - p->prev_size; | 
|  | 2842 | size_t total_size = p->prev_size + size; | 
|  | 2843 | /* Unfortunately we have to do the compilers job by hand here.  Normally | 
|  | 2844 | we would test BLOCK and TOTAL-SIZE separately for compliance with the | 
|  | 2845 | page size.  But gcc does not recognize the optimization possibility | 
|  | 2846 | (in the moment at least) so we combine the two values into one before | 
|  | 2847 | the bit test.  */ | 
|  | 2848 | if (__builtin_expect (((block | total_size) & (GLRO (dl_pagesize) - 1)) != 0, 0)) | 
|  | 2849 | { | 
|  | 2850 | malloc_printerr (check_action, "munmap_chunk(): invalid pointer", | 
|  | 2851 | chunk2mem (p), NULL); | 
|  | 2852 | return; | 
|  | 2853 | } | 
|  | 2854 |  | 
|  | 2855 | atomic_decrement (&mp_.n_mmaps); | 
|  | 2856 | atomic_add (&mp_.mmapped_mem, -total_size); | 
|  | 2857 |  | 
|  | 2858 | /* If munmap failed the process virtual memory address space is in a | 
|  | 2859 | bad shape.  Just leave the block hanging around, the process will | 
|  | 2860 | terminate shortly anyway since not much can be done.  */ | 
|  | 2861 | __munmap ((char *) block, total_size); | 
|  | 2862 | } | 
|  | 2863 |  | 
|  | 2864 | #if HAVE_MREMAP | 
|  | 2865 |  | 
|  | 2866 | static mchunkptr | 
|  | 2867 | internal_function | 
|  | 2868 | mremap_chunk (mchunkptr p, size_t new_size) | 
|  | 2869 | { | 
|  | 2870 | size_t pagesize = GLRO (dl_pagesize); | 
|  | 2871 | INTERNAL_SIZE_T offset = p->prev_size; | 
|  | 2872 | INTERNAL_SIZE_T size = chunksize (p); | 
|  | 2873 | char *cp; | 
|  | 2874 |  | 
|  | 2875 | assert (chunk_is_mmapped (p)); | 
|  | 2876 | assert (((size + offset) & (GLRO (dl_pagesize) - 1)) == 0); | 
|  | 2877 |  | 
|  | 2878 | /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */ | 
|  | 2879 | new_size = ALIGN_UP (new_size + offset + SIZE_SZ, pagesize); | 
|  | 2880 |  | 
|  | 2881 | /* No need to remap if the number of pages does not change.  */ | 
|  | 2882 | if (size + offset == new_size) | 
|  | 2883 | return p; | 
|  | 2884 |  | 
|  | 2885 | cp = (char *) __mremap ((char *) p - offset, size + offset, new_size, | 
|  | 2886 | MREMAP_MAYMOVE); | 
|  | 2887 |  | 
|  | 2888 | if (cp == MAP_FAILED) | 
|  | 2889 | return 0; | 
|  | 2890 |  | 
|  | 2891 | p = (mchunkptr) (cp + offset); | 
|  | 2892 |  | 
|  | 2893 | assert (aligned_OK (chunk2mem (p))); | 
|  | 2894 |  | 
|  | 2895 | assert ((p->prev_size == offset)); | 
|  | 2896 | set_head (p, (new_size - offset) | IS_MMAPPED); | 
|  | 2897 |  | 
|  | 2898 | INTERNAL_SIZE_T new; | 
|  | 2899 | new = atomic_exchange_and_add (&mp_.mmapped_mem, new_size - size - offset) | 
|  | 2900 | + new_size - size - offset; | 
|  | 2901 | atomic_max (&mp_.max_mmapped_mem, new); | 
|  | 2902 | return p; | 
|  | 2903 | } | 
|  | 2904 | #endif /* HAVE_MREMAP */ | 
|  | 2905 |  | 
|  | 2906 | /*------------------------ Public wrappers. --------------------------------*/ | 
|  | 2907 |  | 
|  | 2908 | void * | 
|  | 2909 | __libc_malloc (size_t bytes) | 
|  | 2910 | { | 
|  | 2911 | mstate ar_ptr; | 
|  | 2912 | void *victim; | 
|  | 2913 |  | 
|  | 2914 | void *(*hook) (size_t, const void *) | 
|  | 2915 | = atomic_forced_read (__malloc_hook); | 
|  | 2916 | if (__builtin_expect (hook != NULL, 0)) | 
|  | 2917 | return (*hook)(bytes, RETURN_ADDRESS (0)); | 
|  | 2918 |  | 
|  | 2919 | arena_get (ar_ptr, bytes); | 
|  | 2920 |  | 
|  | 2921 | victim = _int_malloc (ar_ptr, bytes); | 
|  | 2922 | /* Retry with another arena only if we were able to find a usable arena | 
|  | 2923 | before.  */ | 
|  | 2924 | if (!victim && ar_ptr != NULL) | 
|  | 2925 | { | 
|  | 2926 | LIBC_PROBE (memory_malloc_retry, 1, bytes); | 
|  | 2927 | ar_ptr = arena_get_retry (ar_ptr, bytes); | 
|  | 2928 | victim = _int_malloc (ar_ptr, bytes); | 
|  | 2929 | } | 
|  | 2930 |  | 
|  | 2931 | if (ar_ptr != NULL) | 
|  | 2932 | (void) mutex_unlock (&ar_ptr->mutex); | 
|  | 2933 |  | 
|  | 2934 | assert (!victim || chunk_is_mmapped (mem2chunk (victim)) || | 
|  | 2935 | ar_ptr == arena_for_chunk (mem2chunk (victim))); | 
|  | 2936 | return victim; | 
|  | 2937 | } | 
|  | 2938 | libc_hidden_def (__libc_malloc) | 
|  | 2939 |  | 
|  | 2940 | void | 
|  | 2941 | __libc_free (void *mem) | 
|  | 2942 | { | 
|  | 2943 | mstate ar_ptr; | 
|  | 2944 | mchunkptr p;                          /* chunk corresponding to mem */ | 
|  | 2945 |  | 
|  | 2946 | void (*hook) (void *, const void *) | 
|  | 2947 | = atomic_forced_read (__free_hook); | 
|  | 2948 | if (__builtin_expect (hook != NULL, 0)) | 
|  | 2949 | { | 
|  | 2950 | (*hook)(mem, RETURN_ADDRESS (0)); | 
|  | 2951 | return; | 
|  | 2952 | } | 
|  | 2953 |  | 
|  | 2954 | if (mem == 0)                              /* free(0) has no effect */ | 
|  | 2955 | return; | 
|  | 2956 |  | 
|  | 2957 | p = mem2chunk (mem); | 
|  | 2958 |  | 
|  | 2959 | if (chunk_is_mmapped (p))                       /* release mmapped memory. */ | 
|  | 2960 | { | 
|  | 2961 | /* see if the dynamic brk/mmap threshold needs adjusting */ | 
|  | 2962 | if (!mp_.no_dyn_threshold | 
|  | 2963 | && p->size > mp_.mmap_threshold | 
|  | 2964 | && p->size <= DEFAULT_MMAP_THRESHOLD_MAX) | 
|  | 2965 | { | 
|  | 2966 | mp_.mmap_threshold = chunksize (p); | 
|  | 2967 | mp_.trim_threshold = 2 * mp_.mmap_threshold; | 
|  | 2968 | LIBC_PROBE (memory_mallopt_free_dyn_thresholds, 2, | 
|  | 2969 | mp_.mmap_threshold, mp_.trim_threshold); | 
|  | 2970 | } | 
|  | 2971 | munmap_chunk (p); | 
|  | 2972 | return; | 
|  | 2973 | } | 
|  | 2974 |  | 
|  | 2975 | ar_ptr = arena_for_chunk (p); | 
|  | 2976 | _int_free (ar_ptr, p, 0); | 
|  | 2977 | } | 
|  | 2978 | libc_hidden_def (__libc_free) | 
|  | 2979 |  | 
|  | 2980 | void * | 
|  | 2981 | __libc_realloc (void *oldmem, size_t bytes) | 
|  | 2982 | { | 
|  | 2983 | mstate ar_ptr; | 
|  | 2984 | INTERNAL_SIZE_T nb;         /* padded request size */ | 
|  | 2985 |  | 
|  | 2986 | void *newp;             /* chunk to return */ | 
|  | 2987 |  | 
|  | 2988 | void *(*hook) (void *, size_t, const void *) = | 
|  | 2989 | atomic_forced_read (__realloc_hook); | 
|  | 2990 | if (__builtin_expect (hook != NULL, 0)) | 
|  | 2991 | return (*hook)(oldmem, bytes, RETURN_ADDRESS (0)); | 
|  | 2992 |  | 
|  | 2993 | #if REALLOC_ZERO_BYTES_FREES | 
|  | 2994 | if (bytes == 0 && oldmem != NULL) | 
|  | 2995 | { | 
|  | 2996 | __libc_free (oldmem); return 0; | 
|  | 2997 | } | 
|  | 2998 | #endif | 
|  | 2999 |  | 
|  | 3000 | /* realloc of null is supposed to be same as malloc */ | 
|  | 3001 | if (oldmem == 0) | 
|  | 3002 | return __libc_malloc (bytes); | 
|  | 3003 |  | 
|  | 3004 | /* chunk corresponding to oldmem */ | 
|  | 3005 | const mchunkptr oldp = mem2chunk (oldmem); | 
|  | 3006 | /* its size */ | 
|  | 3007 | const INTERNAL_SIZE_T oldsize = chunksize (oldp); | 
|  | 3008 |  | 
|  | 3009 | if (chunk_is_mmapped (oldp)) | 
|  | 3010 | ar_ptr = NULL; | 
|  | 3011 | else | 
|  | 3012 | ar_ptr = arena_for_chunk (oldp); | 
|  | 3013 |  | 
|  | 3014 | /* Little security check which won't hurt performance: the | 
|  | 3015 | allocator never wrapps around at the end of the address space. | 
|  | 3016 | Therefore we can exclude some size values which might appear | 
|  | 3017 | here by accident or by "design" from some intruder.  */ | 
|  | 3018 | if (__builtin_expect ((uintptr_t) oldp > (uintptr_t) -oldsize, 0) | 
|  | 3019 | || __builtin_expect (misaligned_chunk (oldp), 0)) | 
|  | 3020 | { | 
|  | 3021 | malloc_printerr (check_action, "realloc(): invalid pointer", oldmem, | 
|  | 3022 | ar_ptr); | 
|  | 3023 | return NULL; | 
|  | 3024 | } | 
|  | 3025 |  | 
|  | 3026 | checked_request2size (bytes, nb); | 
|  | 3027 |  | 
|  | 3028 | if (chunk_is_mmapped (oldp)) | 
|  | 3029 | { | 
|  | 3030 | void *newmem; | 
|  | 3031 |  | 
|  | 3032 | #if HAVE_MREMAP | 
|  | 3033 | newp = mremap_chunk (oldp, nb); | 
|  | 3034 | if (newp) | 
|  | 3035 | return chunk2mem (newp); | 
|  | 3036 | #endif | 
|  | 3037 | /* Note the extra SIZE_SZ overhead. */ | 
|  | 3038 | if (oldsize - SIZE_SZ >= nb) | 
|  | 3039 | return oldmem;                         /* do nothing */ | 
|  | 3040 |  | 
|  | 3041 | /* Must alloc, copy, free. */ | 
|  | 3042 | newmem = __libc_malloc (bytes); | 
|  | 3043 | if (newmem == 0) | 
|  | 3044 | return 0;              /* propagate failure */ | 
|  | 3045 |  | 
|  | 3046 | memcpy (newmem, oldmem, oldsize - 2 * SIZE_SZ); | 
|  | 3047 | munmap_chunk (oldp); | 
|  | 3048 | return newmem; | 
|  | 3049 | } | 
|  | 3050 |  | 
|  | 3051 | (void) mutex_lock (&ar_ptr->mutex); | 
|  | 3052 |  | 
|  | 3053 | newp = _int_realloc (ar_ptr, oldp, oldsize, nb); | 
|  | 3054 |  | 
|  | 3055 | (void) mutex_unlock (&ar_ptr->mutex); | 
|  | 3056 | assert (!newp || chunk_is_mmapped (mem2chunk (newp)) || | 
|  | 3057 | ar_ptr == arena_for_chunk (mem2chunk (newp))); | 
|  | 3058 |  | 
|  | 3059 | if (newp == NULL) | 
|  | 3060 | { | 
|  | 3061 | /* Try harder to allocate memory in other arenas.  */ | 
|  | 3062 | LIBC_PROBE (memory_realloc_retry, 2, bytes, oldmem); | 
|  | 3063 | newp = __libc_malloc (bytes); | 
|  | 3064 | if (newp != NULL) | 
|  | 3065 | { | 
|  | 3066 | memcpy (newp, oldmem, oldsize - SIZE_SZ); | 
|  | 3067 | _int_free (ar_ptr, oldp, 0); | 
|  | 3068 | } | 
|  | 3069 | } | 
|  | 3070 |  | 
|  | 3071 | return newp; | 
|  | 3072 | } | 
|  | 3073 | libc_hidden_def (__libc_realloc) | 
|  | 3074 |  | 
|  | 3075 | void * | 
|  | 3076 | __libc_memalign (size_t alignment, size_t bytes) | 
|  | 3077 | { | 
|  | 3078 | void *address = RETURN_ADDRESS (0); | 
|  | 3079 | return _mid_memalign (alignment, bytes, address); | 
|  | 3080 | } | 
|  | 3081 |  | 
|  | 3082 | static void * | 
|  | 3083 | _mid_memalign (size_t alignment, size_t bytes, void *address) | 
|  | 3084 | { | 
|  | 3085 | mstate ar_ptr; | 
|  | 3086 | void *p; | 
|  | 3087 |  | 
|  | 3088 | void *(*hook) (size_t, size_t, const void *) = | 
|  | 3089 | atomic_forced_read (__memalign_hook); | 
|  | 3090 | if (__builtin_expect (hook != NULL, 0)) | 
|  | 3091 | return (*hook)(alignment, bytes, address); | 
|  | 3092 |  | 
|  | 3093 | /* If we need less alignment than we give anyway, just relay to malloc.  */ | 
|  | 3094 | if (alignment <= MALLOC_ALIGNMENT) | 
|  | 3095 | return __libc_malloc (bytes); | 
|  | 3096 |  | 
|  | 3097 | /* Otherwise, ensure that it is at least a minimum chunk size */ | 
|  | 3098 | if (alignment < MINSIZE) | 
|  | 3099 | alignment = MINSIZE; | 
|  | 3100 |  | 
|  | 3101 | /* If the alignment is greater than SIZE_MAX / 2 + 1 it cannot be a | 
|  | 3102 | power of 2 and will cause overflow in the check below.  */ | 
|  | 3103 | if (alignment > SIZE_MAX / 2 + 1) | 
|  | 3104 | { | 
|  | 3105 | __set_errno (EINVAL); | 
|  | 3106 | return 0; | 
|  | 3107 | } | 
|  | 3108 |  | 
|  | 3109 | /* Check for overflow.  */ | 
|  | 3110 | if (bytes > SIZE_MAX - alignment - MINSIZE) | 
|  | 3111 | { | 
|  | 3112 | __set_errno (ENOMEM); | 
|  | 3113 | return 0; | 
|  | 3114 | } | 
|  | 3115 |  | 
|  | 3116 |  | 
|  | 3117 | /* Make sure alignment is power of 2.  */ | 
|  | 3118 | if (!powerof2 (alignment)) | 
|  | 3119 | { | 
|  | 3120 | size_t a = MALLOC_ALIGNMENT * 2; | 
|  | 3121 | while (a < alignment) | 
|  | 3122 | a <<= 1; | 
|  | 3123 | alignment = a; | 
|  | 3124 | } | 
|  | 3125 |  | 
|  | 3126 | arena_get (ar_ptr, bytes + alignment + MINSIZE); | 
|  | 3127 |  | 
|  | 3128 | p = _int_memalign (ar_ptr, alignment, bytes); | 
|  | 3129 | if (!p && ar_ptr != NULL) | 
|  | 3130 | { | 
|  | 3131 | LIBC_PROBE (memory_memalign_retry, 2, bytes, alignment); | 
|  | 3132 | ar_ptr = arena_get_retry (ar_ptr, bytes); | 
|  | 3133 | p = _int_memalign (ar_ptr, alignment, bytes); | 
|  | 3134 | } | 
|  | 3135 |  | 
|  | 3136 | if (ar_ptr != NULL) | 
|  | 3137 | (void) mutex_unlock (&ar_ptr->mutex); | 
|  | 3138 |  | 
|  | 3139 | assert (!p || chunk_is_mmapped (mem2chunk (p)) || | 
|  | 3140 | ar_ptr == arena_for_chunk (mem2chunk (p))); | 
|  | 3141 | return p; | 
|  | 3142 | } | 
|  | 3143 | /* For ISO C11.  */ | 
|  | 3144 | weak_alias (__libc_memalign, aligned_alloc) | 
|  | 3145 | libc_hidden_def (__libc_memalign) | 
|  | 3146 |  | 
|  | 3147 | void * | 
|  | 3148 | __libc_valloc (size_t bytes) | 
|  | 3149 | { | 
|  | 3150 | if (__malloc_initialized < 0) | 
|  | 3151 | ptmalloc_init (); | 
|  | 3152 |  | 
|  | 3153 | void *address = RETURN_ADDRESS (0); | 
|  | 3154 | size_t pagesize = GLRO (dl_pagesize); | 
|  | 3155 | return _mid_memalign (pagesize, bytes, address); | 
|  | 3156 | } | 
|  | 3157 |  | 
|  | 3158 | void * | 
|  | 3159 | __libc_pvalloc (size_t bytes) | 
|  | 3160 | { | 
|  | 3161 | if (__malloc_initialized < 0) | 
|  | 3162 | ptmalloc_init (); | 
|  | 3163 |  | 
|  | 3164 | void *address = RETURN_ADDRESS (0); | 
|  | 3165 | size_t pagesize = GLRO (dl_pagesize); | 
|  | 3166 | size_t rounded_bytes = ALIGN_UP (bytes, pagesize); | 
|  | 3167 |  | 
|  | 3168 | /* Check for overflow.  */ | 
|  | 3169 | if (bytes > SIZE_MAX - 2 * pagesize - MINSIZE) | 
|  | 3170 | { | 
|  | 3171 | __set_errno (ENOMEM); | 
|  | 3172 | return 0; | 
|  | 3173 | } | 
|  | 3174 |  | 
|  | 3175 | return _mid_memalign (pagesize, rounded_bytes, address); | 
|  | 3176 | } | 
|  | 3177 |  | 
|  | 3178 | void * | 
|  | 3179 | __libc_calloc (size_t n, size_t elem_size) | 
|  | 3180 | { | 
|  | 3181 | mstate av; | 
|  | 3182 | mchunkptr oldtop, p; | 
|  | 3183 | INTERNAL_SIZE_T bytes, sz, csz, oldtopsize; | 
|  | 3184 | void *mem; | 
|  | 3185 | unsigned long clearsize; | 
|  | 3186 | unsigned long nclears; | 
|  | 3187 | INTERNAL_SIZE_T *d; | 
|  | 3188 |  | 
|  | 3189 | /* size_t is unsigned so the behavior on overflow is defined.  */ | 
|  | 3190 | bytes = n * elem_size; | 
|  | 3191 | #define HALF_INTERNAL_SIZE_T \ | 
|  | 3192 | (((INTERNAL_SIZE_T) 1) << (8 * sizeof (INTERNAL_SIZE_T) / 2)) | 
|  | 3193 | if (__builtin_expect ((n | elem_size) >= HALF_INTERNAL_SIZE_T, 0)) | 
|  | 3194 | { | 
|  | 3195 | if (elem_size != 0 && bytes / elem_size != n) | 
|  | 3196 | { | 
|  | 3197 | __set_errno (ENOMEM); | 
|  | 3198 | return 0; | 
|  | 3199 | } | 
|  | 3200 | } | 
|  | 3201 |  | 
|  | 3202 | void *(*hook) (size_t, const void *) = | 
|  | 3203 | atomic_forced_read (__malloc_hook); | 
|  | 3204 | if (__builtin_expect (hook != NULL, 0)) | 
|  | 3205 | { | 
|  | 3206 | sz = bytes; | 
|  | 3207 | mem = (*hook)(sz, RETURN_ADDRESS (0)); | 
|  | 3208 | if (mem == 0) | 
|  | 3209 | return 0; | 
|  | 3210 |  | 
|  | 3211 | return memset (mem, 0, sz); | 
|  | 3212 | } | 
|  | 3213 |  | 
|  | 3214 | sz = bytes; | 
|  | 3215 |  | 
|  | 3216 | arena_get (av, sz); | 
|  | 3217 | if (av) | 
|  | 3218 | { | 
|  | 3219 | /* Check if we hand out the top chunk, in which case there may be no | 
|  | 3220 | need to clear. */ | 
|  | 3221 | #if MORECORE_CLEARS | 
|  | 3222 | oldtop = top (av); | 
|  | 3223 | oldtopsize = chunksize (top (av)); | 
|  | 3224 | # if MORECORE_CLEARS < 2 | 
|  | 3225 | /* Only newly allocated memory is guaranteed to be cleared.  */ | 
|  | 3226 | if (av == &main_arena && | 
|  | 3227 | oldtopsize < mp_.sbrk_base + av->max_system_mem - (char *) oldtop) | 
|  | 3228 | oldtopsize = (mp_.sbrk_base + av->max_system_mem - (char *) oldtop); | 
|  | 3229 | # endif | 
|  | 3230 | if (av != &main_arena) | 
|  | 3231 | { | 
|  | 3232 | heap_info *heap = heap_for_ptr (oldtop); | 
|  | 3233 | if (oldtopsize < (char *) heap + heap->mprotect_size - (char *) oldtop) | 
|  | 3234 | oldtopsize = (char *) heap + heap->mprotect_size - (char *) oldtop; | 
|  | 3235 | } | 
|  | 3236 | #endif | 
|  | 3237 | } | 
|  | 3238 | else | 
|  | 3239 | { | 
|  | 3240 | /* No usable arenas.  */ | 
|  | 3241 | oldtop = 0; | 
|  | 3242 | oldtopsize = 0; | 
|  | 3243 | } | 
|  | 3244 | mem = _int_malloc (av, sz); | 
|  | 3245 |  | 
|  | 3246 |  | 
|  | 3247 | assert (!mem || chunk_is_mmapped (mem2chunk (mem)) || | 
|  | 3248 | av == arena_for_chunk (mem2chunk (mem))); | 
|  | 3249 |  | 
|  | 3250 | if (mem == 0 && av != NULL) | 
|  | 3251 | { | 
|  | 3252 | LIBC_PROBE (memory_calloc_retry, 1, sz); | 
|  | 3253 | av = arena_get_retry (av, sz); | 
|  | 3254 | mem = _int_malloc (av, sz); | 
|  | 3255 | } | 
|  | 3256 |  | 
|  | 3257 | if (av != NULL) | 
|  | 3258 | (void) mutex_unlock (&av->mutex); | 
|  | 3259 |  | 
|  | 3260 | /* Allocation failed even after a retry.  */ | 
|  | 3261 | if (mem == 0) | 
|  | 3262 | return 0; | 
|  | 3263 |  | 
|  | 3264 | p = mem2chunk (mem); | 
|  | 3265 |  | 
|  | 3266 | /* Two optional cases in which clearing not necessary */ | 
|  | 3267 | if (chunk_is_mmapped (p)) | 
|  | 3268 | { | 
|  | 3269 | if (__builtin_expect (perturb_byte, 0)) | 
|  | 3270 | return memset (mem, 0, sz); | 
|  | 3271 |  | 
|  | 3272 | return mem; | 
|  | 3273 | } | 
|  | 3274 |  | 
|  | 3275 | csz = chunksize (p); | 
|  | 3276 |  | 
|  | 3277 | #if MORECORE_CLEARS | 
|  | 3278 | if (perturb_byte == 0 && (p == oldtop && csz > oldtopsize)) | 
|  | 3279 | { | 
|  | 3280 | /* clear only the bytes from non-freshly-sbrked memory */ | 
|  | 3281 | csz = oldtopsize; | 
|  | 3282 | } | 
|  | 3283 | #endif | 
|  | 3284 |  | 
|  | 3285 | /* Unroll clear of <= 36 bytes (72 if 8byte sizes).  We know that | 
|  | 3286 | contents have an odd number of INTERNAL_SIZE_T-sized words; | 
|  | 3287 | minimally 3.  */ | 
|  | 3288 | d = (INTERNAL_SIZE_T *) mem; | 
|  | 3289 | clearsize = csz - SIZE_SZ; | 
|  | 3290 | nclears = clearsize / sizeof (INTERNAL_SIZE_T); | 
|  | 3291 | assert (nclears >= 3); | 
|  | 3292 |  | 
|  | 3293 | if (nclears > 9) | 
|  | 3294 | return memset (d, 0, clearsize); | 
|  | 3295 |  | 
|  | 3296 | else | 
|  | 3297 | { | 
|  | 3298 | *(d + 0) = 0; | 
|  | 3299 | *(d + 1) = 0; | 
|  | 3300 | *(d + 2) = 0; | 
|  | 3301 | if (nclears > 4) | 
|  | 3302 | { | 
|  | 3303 | *(d + 3) = 0; | 
|  | 3304 | *(d + 4) = 0; | 
|  | 3305 | if (nclears > 6) | 
|  | 3306 | { | 
|  | 3307 | *(d + 5) = 0; | 
|  | 3308 | *(d + 6) = 0; | 
|  | 3309 | if (nclears > 8) | 
|  | 3310 | { | 
|  | 3311 | *(d + 7) = 0; | 
|  | 3312 | *(d + 8) = 0; | 
|  | 3313 | } | 
|  | 3314 | } | 
|  | 3315 | } | 
|  | 3316 | } | 
|  | 3317 |  | 
|  | 3318 | return mem; | 
|  | 3319 | } | 
|  | 3320 |  | 
|  | 3321 | /* | 
|  | 3322 | ------------------------------ malloc ------------------------------ | 
|  | 3323 | */ | 
|  | 3324 |  | 
|  | 3325 | static void * | 
|  | 3326 | _int_malloc (mstate av, size_t bytes) | 
|  | 3327 | { | 
|  | 3328 | INTERNAL_SIZE_T nb;               /* normalized request size */ | 
|  | 3329 | unsigned int idx;                 /* associated bin index */ | 
|  | 3330 | mbinptr bin;                      /* associated bin */ | 
|  | 3331 |  | 
|  | 3332 | mchunkptr victim;                 /* inspected/selected chunk */ | 
|  | 3333 | INTERNAL_SIZE_T size;             /* its size */ | 
|  | 3334 | int victim_index;                 /* its bin index */ | 
|  | 3335 |  | 
|  | 3336 | mchunkptr remainder;              /* remainder from a split */ | 
|  | 3337 | unsigned long remainder_size;     /* its size */ | 
|  | 3338 |  | 
|  | 3339 | unsigned int block;               /* bit map traverser */ | 
|  | 3340 | unsigned int bit;                 /* bit map traverser */ | 
|  | 3341 | unsigned int map;                 /* current word of binmap */ | 
|  | 3342 |  | 
|  | 3343 | mchunkptr fwd;                    /* misc temp for linking */ | 
|  | 3344 | mchunkptr bck;                    /* misc temp for linking */ | 
|  | 3345 |  | 
|  | 3346 | const char *errstr = NULL; | 
|  | 3347 |  | 
|  | 3348 | /* | 
|  | 3349 | Convert request size to internal form by adding SIZE_SZ bytes | 
|  | 3350 | overhead plus possibly more to obtain necessary alignment and/or | 
|  | 3351 | to obtain a size of at least MINSIZE, the smallest allocatable | 
|  | 3352 | size. Also, checked_request2size traps (returning 0) request sizes | 
|  | 3353 | that are so large that they wrap around zero when padded and | 
|  | 3354 | aligned. | 
|  | 3355 | */ | 
|  | 3356 |  | 
|  | 3357 | checked_request2size (bytes, nb); | 
|  | 3358 |  | 
|  | 3359 | /* There are no usable arenas.  Fall back to sysmalloc to get a chunk from | 
|  | 3360 | mmap.  */ | 
|  | 3361 | if (__glibc_unlikely (av == NULL)) | 
|  | 3362 | { | 
|  | 3363 | void *p = sysmalloc (nb, av); | 
|  | 3364 | if (p != NULL) | 
|  | 3365 | alloc_perturb (p, bytes); | 
|  | 3366 | return p; | 
|  | 3367 | } | 
|  | 3368 |  | 
|  | 3369 | /* | 
|  | 3370 | If the size qualifies as a fastbin, first check corresponding bin. | 
|  | 3371 | This code is safe to execute even if av is not yet initialized, so we | 
|  | 3372 | can try it without checking, which saves some time on this fast path. | 
|  | 3373 | */ | 
|  | 3374 |  | 
|  | 3375 | if ((unsigned long) (nb) <= (unsigned long) (get_max_fast ())) | 
|  | 3376 | { | 
|  | 3377 | idx = fastbin_index (nb); | 
|  | 3378 | mfastbinptr *fb = &fastbin (av, idx); | 
|  | 3379 | mchunkptr pp = *fb; | 
|  | 3380 | do | 
|  | 3381 | { | 
|  | 3382 | victim = pp; | 
|  | 3383 | if (victim == NULL) | 
|  | 3384 | break; | 
|  | 3385 | } | 
|  | 3386 | while ((pp = catomic_compare_and_exchange_val_acq (fb, victim->fd, victim)) | 
|  | 3387 | != victim); | 
|  | 3388 | if (victim != 0) | 
|  | 3389 | { | 
|  | 3390 | if (__builtin_expect (fastbin_index (chunksize (victim)) != idx, 0)) | 
|  | 3391 | { | 
|  | 3392 | errstr = "malloc(): memory corruption (fast)"; | 
|  | 3393 | errout: | 
|  | 3394 | malloc_printerr (check_action, errstr, chunk2mem (victim), av); | 
|  | 3395 | return NULL; | 
|  | 3396 | } | 
|  | 3397 | check_remalloced_chunk (av, victim, nb); | 
|  | 3398 | void *p = chunk2mem (victim); | 
|  | 3399 | alloc_perturb (p, bytes); | 
|  | 3400 | return p; | 
|  | 3401 | } | 
|  | 3402 | } | 
|  | 3403 |  | 
|  | 3404 | /* | 
|  | 3405 | If a small request, check regular bin.  Since these "smallbins" | 
|  | 3406 | hold one size each, no searching within bins is necessary. | 
|  | 3407 | (For a large request, we need to wait until unsorted chunks are | 
|  | 3408 | processed to find best fit. But for small ones, fits are exact | 
|  | 3409 | anyway, so we can check now, which is faster.) | 
|  | 3410 | */ | 
|  | 3411 |  | 
|  | 3412 | if (in_smallbin_range (nb)) | 
|  | 3413 | { | 
|  | 3414 | idx = smallbin_index (nb); | 
|  | 3415 | bin = bin_at (av, idx); | 
|  | 3416 |  | 
|  | 3417 | if ((victim = last (bin)) != bin) | 
|  | 3418 | { | 
|  | 3419 | if (victim == 0) /* initialization check */ | 
|  | 3420 | malloc_consolidate (av); | 
|  | 3421 | else | 
|  | 3422 | { | 
|  | 3423 | bck = victim->bk; | 
|  | 3424 | if (__glibc_unlikely (bck->fd != victim)) | 
|  | 3425 | { | 
|  | 3426 | errstr = "malloc(): smallbin double linked list corrupted"; | 
|  | 3427 | goto errout; | 
|  | 3428 | } | 
|  | 3429 | set_inuse_bit_at_offset (victim, nb); | 
|  | 3430 | bin->bk = bck; | 
|  | 3431 | bck->fd = bin; | 
|  | 3432 |  | 
|  | 3433 | if (av != &main_arena) | 
|  | 3434 | victim->size |= NON_MAIN_ARENA; | 
|  | 3435 | check_malloced_chunk (av, victim, nb); | 
|  | 3436 | void *p = chunk2mem (victim); | 
|  | 3437 | alloc_perturb (p, bytes); | 
|  | 3438 | return p; | 
|  | 3439 | } | 
|  | 3440 | } | 
|  | 3441 | } | 
|  | 3442 |  | 
|  | 3443 | /* | 
|  | 3444 | If this is a large request, consolidate fastbins before continuing. | 
|  | 3445 | While it might look excessive to kill all fastbins before | 
|  | 3446 | even seeing if there is space available, this avoids | 
|  | 3447 | fragmentation problems normally associated with fastbins. | 
|  | 3448 | Also, in practice, programs tend to have runs of either small or | 
|  | 3449 | large requests, but less often mixtures, so consolidation is not | 
|  | 3450 | invoked all that often in most programs. And the programs that | 
|  | 3451 | it is called frequently in otherwise tend to fragment. | 
|  | 3452 | */ | 
|  | 3453 |  | 
|  | 3454 | else | 
|  | 3455 | { | 
|  | 3456 | idx = largebin_index (nb); | 
|  | 3457 | if (have_fastchunks (av)) | 
|  | 3458 | malloc_consolidate (av); | 
|  | 3459 | } | 
|  | 3460 |  | 
|  | 3461 | /* | 
|  | 3462 | Process recently freed or remaindered chunks, taking one only if | 
|  | 3463 | it is exact fit, or, if this a small request, the chunk is remainder from | 
|  | 3464 | the most recent non-exact fit.  Place other traversed chunks in | 
|  | 3465 | bins.  Note that this step is the only place in any routine where | 
|  | 3466 | chunks are placed in bins. | 
|  | 3467 |  | 
|  | 3468 | The outer loop here is needed because we might not realize until | 
|  | 3469 | near the end of malloc that we should have consolidated, so must | 
|  | 3470 | do so and retry. This happens at most once, and only when we would | 
|  | 3471 | otherwise need to expand memory to service a "small" request. | 
|  | 3472 | */ | 
|  | 3473 |  | 
|  | 3474 | for (;; ) | 
|  | 3475 | { | 
|  | 3476 | int iters = 0; | 
|  | 3477 | while ((victim = unsorted_chunks (av)->bk) != unsorted_chunks (av)) | 
|  | 3478 | { | 
|  | 3479 | bck = victim->bk; | 
|  | 3480 | if (__builtin_expect (victim->size <= 2 * SIZE_SZ, 0) | 
|  | 3481 | || __builtin_expect (victim->size > av->system_mem, 0)) | 
|  | 3482 | malloc_printerr (check_action, "malloc(): memory corruption", | 
|  | 3483 | chunk2mem (victim), av); | 
|  | 3484 | size = chunksize (victim); | 
|  | 3485 |  | 
|  | 3486 | /* | 
|  | 3487 | If a small request, try to use last remainder if it is the | 
|  | 3488 | only chunk in unsorted bin.  This helps promote locality for | 
|  | 3489 | runs of consecutive small requests. This is the only | 
|  | 3490 | exception to best-fit, and applies only when there is | 
|  | 3491 | no exact fit for a small chunk. | 
|  | 3492 | */ | 
|  | 3493 |  | 
|  | 3494 | if (in_smallbin_range (nb) && | 
|  | 3495 | bck == unsorted_chunks (av) && | 
|  | 3496 | victim == av->last_remainder && | 
|  | 3497 | (unsigned long) (size) > (unsigned long) (nb + MINSIZE)) | 
|  | 3498 | { | 
|  | 3499 | /* split and reattach remainder */ | 
|  | 3500 | remainder_size = size - nb; | 
|  | 3501 | remainder = chunk_at_offset (victim, nb); | 
|  | 3502 | unsorted_chunks (av)->bk = unsorted_chunks (av)->fd = remainder; | 
|  | 3503 | av->last_remainder = remainder; | 
|  | 3504 | remainder->bk = remainder->fd = unsorted_chunks (av); | 
|  | 3505 | if (!in_smallbin_range (remainder_size)) | 
|  | 3506 | { | 
|  | 3507 | remainder->fd_nextsize = NULL; | 
|  | 3508 | remainder->bk_nextsize = NULL; | 
|  | 3509 | } | 
|  | 3510 |  | 
|  | 3511 | set_head (victim, nb | PREV_INUSE | | 
|  | 3512 | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 3513 | set_head (remainder, remainder_size | PREV_INUSE); | 
|  | 3514 | set_foot (remainder, remainder_size); | 
|  | 3515 |  | 
|  | 3516 | check_malloced_chunk (av, victim, nb); | 
|  | 3517 | void *p = chunk2mem (victim); | 
|  | 3518 | alloc_perturb (p, bytes); | 
|  | 3519 | return p; | 
|  | 3520 | } | 
|  | 3521 |  | 
|  | 3522 | /* remove from unsorted list */ | 
|  | 3523 | unsorted_chunks (av)->bk = bck; | 
|  | 3524 | bck->fd = unsorted_chunks (av); | 
|  | 3525 |  | 
|  | 3526 | /* Take now instead of binning if exact fit */ | 
|  | 3527 |  | 
|  | 3528 | if (size == nb) | 
|  | 3529 | { | 
|  | 3530 | set_inuse_bit_at_offset (victim, size); | 
|  | 3531 | if (av != &main_arena) | 
|  | 3532 | victim->size |= NON_MAIN_ARENA; | 
|  | 3533 | check_malloced_chunk (av, victim, nb); | 
|  | 3534 | void *p = chunk2mem (victim); | 
|  | 3535 | alloc_perturb (p, bytes); | 
|  | 3536 | return p; | 
|  | 3537 | } | 
|  | 3538 |  | 
|  | 3539 | /* place chunk in bin */ | 
|  | 3540 |  | 
|  | 3541 | if (in_smallbin_range (size)) | 
|  | 3542 | { | 
|  | 3543 | victim_index = smallbin_index (size); | 
|  | 3544 | bck = bin_at (av, victim_index); | 
|  | 3545 | fwd = bck->fd; | 
|  | 3546 | } | 
|  | 3547 | else | 
|  | 3548 | { | 
|  | 3549 | victim_index = largebin_index (size); | 
|  | 3550 | bck = bin_at (av, victim_index); | 
|  | 3551 | fwd = bck->fd; | 
|  | 3552 |  | 
|  | 3553 | /* maintain large bins in sorted order */ | 
|  | 3554 | if (fwd != bck) | 
|  | 3555 | { | 
|  | 3556 | /* Or with inuse bit to speed comparisons */ | 
|  | 3557 | size |= PREV_INUSE; | 
|  | 3558 | /* if smaller than smallest, bypass loop below */ | 
|  | 3559 | assert ((bck->bk->size & NON_MAIN_ARENA) == 0); | 
|  | 3560 | if ((unsigned long) (size) < (unsigned long) (bck->bk->size)) | 
|  | 3561 | { | 
|  | 3562 | fwd = bck; | 
|  | 3563 | bck = bck->bk; | 
|  | 3564 |  | 
|  | 3565 | victim->fd_nextsize = fwd->fd; | 
|  | 3566 | victim->bk_nextsize = fwd->fd->bk_nextsize; | 
|  | 3567 | fwd->fd->bk_nextsize = victim->bk_nextsize->fd_nextsize = victim; | 
|  | 3568 | } | 
|  | 3569 | else | 
|  | 3570 | { | 
|  | 3571 | assert ((fwd->size & NON_MAIN_ARENA) == 0); | 
|  | 3572 | while ((unsigned long) size < fwd->size) | 
|  | 3573 | { | 
|  | 3574 | fwd = fwd->fd_nextsize; | 
|  | 3575 | assert ((fwd->size & NON_MAIN_ARENA) == 0); | 
|  | 3576 | } | 
|  | 3577 |  | 
|  | 3578 | if ((unsigned long) size == (unsigned long) fwd->size) | 
|  | 3579 | /* Always insert in the second position.  */ | 
|  | 3580 | fwd = fwd->fd; | 
|  | 3581 | else | 
|  | 3582 | { | 
|  | 3583 | victim->fd_nextsize = fwd; | 
|  | 3584 | victim->bk_nextsize = fwd->bk_nextsize; | 
|  | 3585 | fwd->bk_nextsize = victim; | 
|  | 3586 | victim->bk_nextsize->fd_nextsize = victim; | 
|  | 3587 | } | 
|  | 3588 | bck = fwd->bk; | 
|  | 3589 | } | 
|  | 3590 | } | 
|  | 3591 | else | 
|  | 3592 | victim->fd_nextsize = victim->bk_nextsize = victim; | 
|  | 3593 | } | 
|  | 3594 |  | 
|  | 3595 | mark_bin (av, victim_index); | 
|  | 3596 | victim->bk = bck; | 
|  | 3597 | victim->fd = fwd; | 
|  | 3598 | fwd->bk = victim; | 
|  | 3599 | bck->fd = victim; | 
|  | 3600 |  | 
|  | 3601 | #define MAX_ITERS       10000 | 
|  | 3602 | if (++iters >= MAX_ITERS) | 
|  | 3603 | break; | 
|  | 3604 | } | 
|  | 3605 |  | 
|  | 3606 | /* | 
|  | 3607 | If a large request, scan through the chunks of current bin in | 
|  | 3608 | sorted order to find smallest that fits.  Use the skip list for this. | 
|  | 3609 | */ | 
|  | 3610 |  | 
|  | 3611 | if (!in_smallbin_range (nb)) | 
|  | 3612 | { | 
|  | 3613 | bin = bin_at (av, idx); | 
|  | 3614 |  | 
|  | 3615 | /* skip scan if empty or largest chunk is too small */ | 
|  | 3616 | if ((victim = first (bin)) != bin && | 
|  | 3617 | (unsigned long) (victim->size) >= (unsigned long) (nb)) | 
|  | 3618 | { | 
|  | 3619 | victim = victim->bk_nextsize; | 
|  | 3620 | while (((unsigned long) (size = chunksize (victim)) < | 
|  | 3621 | (unsigned long) (nb))) | 
|  | 3622 | victim = victim->bk_nextsize; | 
|  | 3623 |  | 
|  | 3624 | /* Avoid removing the first entry for a size so that the skip | 
|  | 3625 | list does not have to be rerouted.  */ | 
|  | 3626 | if (victim != last (bin) && victim->size == victim->fd->size) | 
|  | 3627 | victim = victim->fd; | 
|  | 3628 |  | 
|  | 3629 | remainder_size = size - nb; | 
|  | 3630 | unlink (av, victim, bck, fwd); | 
|  | 3631 |  | 
|  | 3632 | /* Exhaust */ | 
|  | 3633 | if (remainder_size < MINSIZE) | 
|  | 3634 | { | 
|  | 3635 | set_inuse_bit_at_offset (victim, size); | 
|  | 3636 | if (av != &main_arena) | 
|  | 3637 | victim->size |= NON_MAIN_ARENA; | 
|  | 3638 | } | 
|  | 3639 | /* Split */ | 
|  | 3640 | else | 
|  | 3641 | { | 
|  | 3642 | remainder = chunk_at_offset (victim, nb); | 
|  | 3643 | /* We cannot assume the unsorted list is empty and therefore | 
|  | 3644 | have to perform a complete insert here.  */ | 
|  | 3645 | bck = unsorted_chunks (av); | 
|  | 3646 | fwd = bck->fd; | 
|  | 3647 | if (__glibc_unlikely (fwd->bk != bck)) | 
|  | 3648 | { | 
|  | 3649 | errstr = "malloc(): corrupted unsorted chunks"; | 
|  | 3650 | goto errout; | 
|  | 3651 | } | 
|  | 3652 | remainder->bk = bck; | 
|  | 3653 | remainder->fd = fwd; | 
|  | 3654 | bck->fd = remainder; | 
|  | 3655 | fwd->bk = remainder; | 
|  | 3656 | if (!in_smallbin_range (remainder_size)) | 
|  | 3657 | { | 
|  | 3658 | remainder->fd_nextsize = NULL; | 
|  | 3659 | remainder->bk_nextsize = NULL; | 
|  | 3660 | } | 
|  | 3661 | set_head (victim, nb | PREV_INUSE | | 
|  | 3662 | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 3663 | set_head (remainder, remainder_size | PREV_INUSE); | 
|  | 3664 | set_foot (remainder, remainder_size); | 
|  | 3665 | } | 
|  | 3666 | check_malloced_chunk (av, victim, nb); | 
|  | 3667 | void *p = chunk2mem (victim); | 
|  | 3668 | alloc_perturb (p, bytes); | 
|  | 3669 | return p; | 
|  | 3670 | } | 
|  | 3671 | } | 
|  | 3672 |  | 
|  | 3673 | /* | 
|  | 3674 | Search for a chunk by scanning bins, starting with next largest | 
|  | 3675 | bin. This search is strictly by best-fit; i.e., the smallest | 
|  | 3676 | (with ties going to approximately the least recently used) chunk | 
|  | 3677 | that fits is selected. | 
|  | 3678 |  | 
|  | 3679 | The bitmap avoids needing to check that most blocks are nonempty. | 
|  | 3680 | The particular case of skipping all bins during warm-up phases | 
|  | 3681 | when no chunks have been returned yet is faster than it might look. | 
|  | 3682 | */ | 
|  | 3683 |  | 
|  | 3684 | ++idx; | 
|  | 3685 | bin = bin_at (av, idx); | 
|  | 3686 | block = idx2block (idx); | 
|  | 3687 | map = av->binmap[block]; | 
|  | 3688 | bit = idx2bit (idx); | 
|  | 3689 |  | 
|  | 3690 | for (;; ) | 
|  | 3691 | { | 
|  | 3692 | /* Skip rest of block if there are no more set bits in this block.  */ | 
|  | 3693 | if (bit > map || bit == 0) | 
|  | 3694 | { | 
|  | 3695 | do | 
|  | 3696 | { | 
|  | 3697 | if (++block >= BINMAPSIZE) /* out of bins */ | 
|  | 3698 | goto use_top; | 
|  | 3699 | } | 
|  | 3700 | while ((map = av->binmap[block]) == 0); | 
|  | 3701 |  | 
|  | 3702 | bin = bin_at (av, (block << BINMAPSHIFT)); | 
|  | 3703 | bit = 1; | 
|  | 3704 | } | 
|  | 3705 |  | 
|  | 3706 | /* Advance to bin with set bit. There must be one. */ | 
|  | 3707 | while ((bit & map) == 0) | 
|  | 3708 | { | 
|  | 3709 | bin = next_bin (bin); | 
|  | 3710 | bit <<= 1; | 
|  | 3711 | assert (bit != 0); | 
|  | 3712 | } | 
|  | 3713 |  | 
|  | 3714 | /* Inspect the bin. It is likely to be non-empty */ | 
|  | 3715 | victim = last (bin); | 
|  | 3716 |  | 
|  | 3717 | /*  If a false alarm (empty bin), clear the bit. */ | 
|  | 3718 | if (victim == bin) | 
|  | 3719 | { | 
|  | 3720 | av->binmap[block] = map &= ~bit; /* Write through */ | 
|  | 3721 | bin = next_bin (bin); | 
|  | 3722 | bit <<= 1; | 
|  | 3723 | } | 
|  | 3724 |  | 
|  | 3725 | else | 
|  | 3726 | { | 
|  | 3727 | size = chunksize (victim); | 
|  | 3728 |  | 
|  | 3729 | /*  We know the first chunk in this bin is big enough to use. */ | 
|  | 3730 | assert ((unsigned long) (size) >= (unsigned long) (nb)); | 
|  | 3731 |  | 
|  | 3732 | remainder_size = size - nb; | 
|  | 3733 |  | 
|  | 3734 | /* unlink */ | 
|  | 3735 | unlink (av, victim, bck, fwd); | 
|  | 3736 |  | 
|  | 3737 | /* Exhaust */ | 
|  | 3738 | if (remainder_size < MINSIZE) | 
|  | 3739 | { | 
|  | 3740 | set_inuse_bit_at_offset (victim, size); | 
|  | 3741 | if (av != &main_arena) | 
|  | 3742 | victim->size |= NON_MAIN_ARENA; | 
|  | 3743 | } | 
|  | 3744 |  | 
|  | 3745 | /* Split */ | 
|  | 3746 | else | 
|  | 3747 | { | 
|  | 3748 | remainder = chunk_at_offset (victim, nb); | 
|  | 3749 |  | 
|  | 3750 | /* We cannot assume the unsorted list is empty and therefore | 
|  | 3751 | have to perform a complete insert here.  */ | 
|  | 3752 | bck = unsorted_chunks (av); | 
|  | 3753 | fwd = bck->fd; | 
|  | 3754 | if (__glibc_unlikely (fwd->bk != bck)) | 
|  | 3755 | { | 
|  | 3756 | errstr = "malloc(): corrupted unsorted chunks 2"; | 
|  | 3757 | goto errout; | 
|  | 3758 | } | 
|  | 3759 | remainder->bk = bck; | 
|  | 3760 | remainder->fd = fwd; | 
|  | 3761 | bck->fd = remainder; | 
|  | 3762 | fwd->bk = remainder; | 
|  | 3763 |  | 
|  | 3764 | /* advertise as last remainder */ | 
|  | 3765 | if (in_smallbin_range (nb)) | 
|  | 3766 | av->last_remainder = remainder; | 
|  | 3767 | if (!in_smallbin_range (remainder_size)) | 
|  | 3768 | { | 
|  | 3769 | remainder->fd_nextsize = NULL; | 
|  | 3770 | remainder->bk_nextsize = NULL; | 
|  | 3771 | } | 
|  | 3772 | set_head (victim, nb | PREV_INUSE | | 
|  | 3773 | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 3774 | set_head (remainder, remainder_size | PREV_INUSE); | 
|  | 3775 | set_foot (remainder, remainder_size); | 
|  | 3776 | } | 
|  | 3777 | check_malloced_chunk (av, victim, nb); | 
|  | 3778 | void *p = chunk2mem (victim); | 
|  | 3779 | alloc_perturb (p, bytes); | 
|  | 3780 | return p; | 
|  | 3781 | } | 
|  | 3782 | } | 
|  | 3783 |  | 
|  | 3784 | use_top: | 
|  | 3785 | /* | 
|  | 3786 | If large enough, split off the chunk bordering the end of memory | 
|  | 3787 | (held in av->top). Note that this is in accord with the best-fit | 
|  | 3788 | search rule.  In effect, av->top is treated as larger (and thus | 
|  | 3789 | less well fitting) than any other available chunk since it can | 
|  | 3790 | be extended to be as large as necessary (up to system | 
|  | 3791 | limitations). | 
|  | 3792 |  | 
|  | 3793 | We require that av->top always exists (i.e., has size >= | 
|  | 3794 | MINSIZE) after initialization, so if it would otherwise be | 
|  | 3795 | exhausted by current request, it is replenished. (The main | 
|  | 3796 | reason for ensuring it exists is that we may need MINSIZE space | 
|  | 3797 | to put in fenceposts in sysmalloc.) | 
|  | 3798 | */ | 
|  | 3799 |  | 
|  | 3800 | victim = av->top; | 
|  | 3801 | size = chunksize (victim); | 
|  | 3802 |  | 
|  | 3803 | if ((unsigned long) (size) >= (unsigned long) (nb + MINSIZE)) | 
|  | 3804 | { | 
|  | 3805 | remainder_size = size - nb; | 
|  | 3806 | remainder = chunk_at_offset (victim, nb); | 
|  | 3807 | av->top = remainder; | 
|  | 3808 | set_head (victim, nb | PREV_INUSE | | 
|  | 3809 | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 3810 | set_head (remainder, remainder_size | PREV_INUSE); | 
|  | 3811 |  | 
|  | 3812 | check_malloced_chunk (av, victim, nb); | 
|  | 3813 | void *p = chunk2mem (victim); | 
|  | 3814 | alloc_perturb (p, bytes); | 
|  | 3815 | return p; | 
|  | 3816 | } | 
|  | 3817 |  | 
|  | 3818 | /* When we are using atomic ops to free fast chunks we can get | 
|  | 3819 | here for all block sizes.  */ | 
|  | 3820 | else if (have_fastchunks (av)) | 
|  | 3821 | { | 
|  | 3822 | malloc_consolidate (av); | 
|  | 3823 | /* restore original bin index */ | 
|  | 3824 | if (in_smallbin_range (nb)) | 
|  | 3825 | idx = smallbin_index (nb); | 
|  | 3826 | else | 
|  | 3827 | idx = largebin_index (nb); | 
|  | 3828 | } | 
|  | 3829 |  | 
|  | 3830 | /* | 
|  | 3831 | Otherwise, relay to handle system-dependent cases | 
|  | 3832 | */ | 
|  | 3833 | else | 
|  | 3834 | { | 
|  | 3835 | void *p = sysmalloc (nb, av); | 
|  | 3836 | if (p != NULL) | 
|  | 3837 | alloc_perturb (p, bytes); | 
|  | 3838 | return p; | 
|  | 3839 | } | 
|  | 3840 | } | 
|  | 3841 | } | 
|  | 3842 |  | 
|  | 3843 | /* | 
|  | 3844 | ------------------------------ free ------------------------------ | 
|  | 3845 | */ | 
|  | 3846 |  | 
|  | 3847 | static void | 
|  | 3848 | _int_free (mstate av, mchunkptr p, int have_lock) | 
|  | 3849 | { | 
|  | 3850 | INTERNAL_SIZE_T size;        /* its size */ | 
|  | 3851 | mfastbinptr *fb;             /* associated fastbin */ | 
|  | 3852 | mchunkptr nextchunk;         /* next contiguous chunk */ | 
|  | 3853 | INTERNAL_SIZE_T nextsize;    /* its size */ | 
|  | 3854 | int nextinuse;               /* true if nextchunk is used */ | 
|  | 3855 | INTERNAL_SIZE_T prevsize;    /* size of previous contiguous chunk */ | 
|  | 3856 | mchunkptr bck;               /* misc temp for linking */ | 
|  | 3857 | mchunkptr fwd;               /* misc temp for linking */ | 
|  | 3858 |  | 
|  | 3859 | const char *errstr = NULL; | 
|  | 3860 | int locked = 0; | 
|  | 3861 |  | 
|  | 3862 | size = chunksize (p); | 
|  | 3863 |  | 
|  | 3864 | /* Little security check which won't hurt performance: the | 
|  | 3865 | allocator never wrapps around at the end of the address space. | 
|  | 3866 | Therefore we can exclude some size values which might appear | 
|  | 3867 | here by accident or by "design" from some intruder.  */ | 
|  | 3868 | if (__builtin_expect ((uintptr_t) p > (uintptr_t) -size, 0) | 
|  | 3869 | || __builtin_expect (misaligned_chunk (p), 0)) | 
|  | 3870 | { | 
|  | 3871 | errstr = "free(): invalid pointer"; | 
|  | 3872 | errout: | 
|  | 3873 | if (!have_lock && locked) | 
|  | 3874 | (void) mutex_unlock (&av->mutex); | 
|  | 3875 | malloc_printerr (check_action, errstr, chunk2mem (p), av); | 
|  | 3876 | return; | 
|  | 3877 | } | 
|  | 3878 | /* We know that each chunk is at least MINSIZE bytes in size or a | 
|  | 3879 | multiple of MALLOC_ALIGNMENT.  */ | 
|  | 3880 | if (__glibc_unlikely (size < MINSIZE || !aligned_OK (size))) | 
|  | 3881 | { | 
|  | 3882 | errstr = "free(): invalid size"; | 
|  | 3883 | goto errout; | 
|  | 3884 | } | 
|  | 3885 |  | 
|  | 3886 | check_inuse_chunk(av, p); | 
|  | 3887 |  | 
|  | 3888 | /* | 
|  | 3889 | If eligible, place chunk on a fastbin so it can be found | 
|  | 3890 | and used quickly in malloc. | 
|  | 3891 | */ | 
|  | 3892 |  | 
|  | 3893 | if ((unsigned long)(size) <= (unsigned long)(get_max_fast ()) | 
|  | 3894 |  | 
|  | 3895 | #if TRIM_FASTBINS | 
|  | 3896 | /* | 
|  | 3897 | If TRIM_FASTBINS set, don't place chunks | 
|  | 3898 | bordering top into fastbins | 
|  | 3899 | */ | 
|  | 3900 | && (chunk_at_offset(p, size) != av->top) | 
|  | 3901 | #endif | 
|  | 3902 | ) { | 
|  | 3903 |  | 
|  | 3904 | if (__builtin_expect (chunk_at_offset (p, size)->size <= 2 * SIZE_SZ, 0) | 
|  | 3905 | || __builtin_expect (chunksize (chunk_at_offset (p, size)) | 
|  | 3906 | >= av->system_mem, 0)) | 
|  | 3907 | { | 
|  | 3908 | /* We might not have a lock at this point and concurrent modifications | 
|  | 3909 | of system_mem might have let to a false positive.  Redo the test | 
|  | 3910 | after getting the lock.  */ | 
|  | 3911 | if (have_lock | 
|  | 3912 | || ({ assert (locked == 0); | 
|  | 3913 | mutex_lock(&av->mutex); | 
|  | 3914 | locked = 1; | 
|  | 3915 | chunk_at_offset (p, size)->size <= 2 * SIZE_SZ | 
|  | 3916 | || chunksize (chunk_at_offset (p, size)) >= av->system_mem; | 
|  | 3917 | })) | 
|  | 3918 | { | 
|  | 3919 | errstr = "free(): invalid next size (fast)"; | 
|  | 3920 | goto errout; | 
|  | 3921 | } | 
|  | 3922 | if (! have_lock) | 
|  | 3923 | { | 
|  | 3924 | (void)mutex_unlock(&av->mutex); | 
|  | 3925 | locked = 0; | 
|  | 3926 | } | 
|  | 3927 | } | 
|  | 3928 |  | 
|  | 3929 | free_perturb (chunk2mem(p), size - 2 * SIZE_SZ); | 
|  | 3930 |  | 
|  | 3931 | set_fastchunks(av); | 
|  | 3932 | unsigned int idx = fastbin_index(size); | 
|  | 3933 | fb = &fastbin (av, idx); | 
|  | 3934 |  | 
|  | 3935 | /* Atomically link P to its fastbin: P->FD = *FB; *FB = P;  */ | 
|  | 3936 | mchunkptr old = *fb, old2; | 
|  | 3937 | unsigned int old_idx = ~0u; | 
|  | 3938 | do | 
|  | 3939 | { | 
|  | 3940 | /* Check that the top of the bin is not the record we are going to add | 
|  | 3941 | (i.e., double free).  */ | 
|  | 3942 | if (__builtin_expect (old == p, 0)) | 
|  | 3943 | { | 
|  | 3944 | errstr = "double free or corruption (fasttop)"; | 
|  | 3945 | goto errout; | 
|  | 3946 | } | 
|  | 3947 | /* Check that size of fastbin chunk at the top is the same as | 
|  | 3948 | size of the chunk that we are adding.  We can dereference OLD | 
|  | 3949 | only if we have the lock, otherwise it might have already been | 
|  | 3950 | deallocated.  See use of OLD_IDX below for the actual check.  */ | 
|  | 3951 | if (have_lock && old != NULL) | 
|  | 3952 | old_idx = fastbin_index(chunksize(old)); | 
|  | 3953 | p->fd = old2 = old; | 
|  | 3954 | } | 
|  | 3955 | while ((old = catomic_compare_and_exchange_val_rel (fb, p, old2)) != old2); | 
|  | 3956 |  | 
|  | 3957 | if (have_lock && old != NULL && __builtin_expect (old_idx != idx, 0)) | 
|  | 3958 | { | 
|  | 3959 | errstr = "invalid fastbin entry (free)"; | 
|  | 3960 | goto errout; | 
|  | 3961 | } | 
|  | 3962 | } | 
|  | 3963 |  | 
|  | 3964 | /* | 
|  | 3965 | Consolidate other non-mmapped chunks as they arrive. | 
|  | 3966 | */ | 
|  | 3967 |  | 
|  | 3968 | else if (!chunk_is_mmapped(p)) { | 
|  | 3969 | if (! have_lock) { | 
|  | 3970 | (void)mutex_lock(&av->mutex); | 
|  | 3971 | locked = 1; | 
|  | 3972 | } | 
|  | 3973 |  | 
|  | 3974 | nextchunk = chunk_at_offset(p, size); | 
|  | 3975 |  | 
|  | 3976 | /* Lightweight tests: check whether the block is already the | 
|  | 3977 | top block.  */ | 
|  | 3978 | if (__glibc_unlikely (p == av->top)) | 
|  | 3979 | { | 
|  | 3980 | errstr = "double free or corruption (top)"; | 
|  | 3981 | goto errout; | 
|  | 3982 | } | 
|  | 3983 | /* Or whether the next chunk is beyond the boundaries of the arena.  */ | 
|  | 3984 | if (__builtin_expect (contiguous (av) | 
|  | 3985 | && (char *) nextchunk | 
|  | 3986 | >= ((char *) av->top + chunksize(av->top)), 0)) | 
|  | 3987 | { | 
|  | 3988 | errstr = "double free or corruption (out)"; | 
|  | 3989 | goto errout; | 
|  | 3990 | } | 
|  | 3991 | /* Or whether the block is actually not marked used.  */ | 
|  | 3992 | if (__glibc_unlikely (!prev_inuse(nextchunk))) | 
|  | 3993 | { | 
|  | 3994 | errstr = "double free or corruption (!prev)"; | 
|  | 3995 | goto errout; | 
|  | 3996 | } | 
|  | 3997 |  | 
|  | 3998 | nextsize = chunksize(nextchunk); | 
|  | 3999 | if (__builtin_expect (nextchunk->size <= 2 * SIZE_SZ, 0) | 
|  | 4000 | || __builtin_expect (nextsize >= av->system_mem, 0)) | 
|  | 4001 | { | 
|  | 4002 | errstr = "free(): invalid next size (normal)"; | 
|  | 4003 | goto errout; | 
|  | 4004 | } | 
|  | 4005 |  | 
|  | 4006 | free_perturb (chunk2mem(p), size - 2 * SIZE_SZ); | 
|  | 4007 |  | 
|  | 4008 | /* consolidate backward */ | 
|  | 4009 | if (!prev_inuse(p)) { | 
|  | 4010 | prevsize = p->prev_size; | 
|  | 4011 | size += prevsize; | 
|  | 4012 | p = chunk_at_offset(p, -((long) prevsize)); | 
|  | 4013 | unlink(av, p, bck, fwd); | 
|  | 4014 | } | 
|  | 4015 |  | 
|  | 4016 | if (nextchunk != av->top) { | 
|  | 4017 | /* get and clear inuse bit */ | 
|  | 4018 | nextinuse = inuse_bit_at_offset(nextchunk, nextsize); | 
|  | 4019 |  | 
|  | 4020 | /* consolidate forward */ | 
|  | 4021 | if (!nextinuse) { | 
|  | 4022 | unlink(av, nextchunk, bck, fwd); | 
|  | 4023 | size += nextsize; | 
|  | 4024 | } else | 
|  | 4025 | clear_inuse_bit_at_offset(nextchunk, 0); | 
|  | 4026 |  | 
|  | 4027 | /* | 
|  | 4028 | Place the chunk in unsorted chunk list. Chunks are | 
|  | 4029 | not placed into regular bins until after they have | 
|  | 4030 | been given one chance to be used in malloc. | 
|  | 4031 | */ | 
|  | 4032 |  | 
|  | 4033 | bck = unsorted_chunks(av); | 
|  | 4034 | fwd = bck->fd; | 
|  | 4035 | if (__glibc_unlikely (fwd->bk != bck)) | 
|  | 4036 | { | 
|  | 4037 | errstr = "free(): corrupted unsorted chunks"; | 
|  | 4038 | goto errout; | 
|  | 4039 | } | 
|  | 4040 | p->fd = fwd; | 
|  | 4041 | p->bk = bck; | 
|  | 4042 | if (!in_smallbin_range(size)) | 
|  | 4043 | { | 
|  | 4044 | p->fd_nextsize = NULL; | 
|  | 4045 | p->bk_nextsize = NULL; | 
|  | 4046 | } | 
|  | 4047 | bck->fd = p; | 
|  | 4048 | fwd->bk = p; | 
|  | 4049 |  | 
|  | 4050 | set_head(p, size | PREV_INUSE); | 
|  | 4051 | set_foot(p, size); | 
|  | 4052 |  | 
|  | 4053 | check_free_chunk(av, p); | 
|  | 4054 | } | 
|  | 4055 |  | 
|  | 4056 | /* | 
|  | 4057 | If the chunk borders the current high end of memory, | 
|  | 4058 | consolidate into top | 
|  | 4059 | */ | 
|  | 4060 |  | 
|  | 4061 | else { | 
|  | 4062 | size += nextsize; | 
|  | 4063 | set_head(p, size | PREV_INUSE); | 
|  | 4064 | av->top = p; | 
|  | 4065 | check_chunk(av, p); | 
|  | 4066 | } | 
|  | 4067 |  | 
|  | 4068 | /* | 
|  | 4069 | If freeing a large space, consolidate possibly-surrounding | 
|  | 4070 | chunks. Then, if the total unused topmost memory exceeds trim | 
|  | 4071 | threshold, ask malloc_trim to reduce top. | 
|  | 4072 |  | 
|  | 4073 | Unless max_fast is 0, we don't know if there are fastbins | 
|  | 4074 | bordering top, so we cannot tell for sure whether threshold | 
|  | 4075 | has been reached unless fastbins are consolidated.  But we | 
|  | 4076 | don't want to consolidate on each free.  As a compromise, | 
|  | 4077 | consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD | 
|  | 4078 | is reached. | 
|  | 4079 | */ | 
|  | 4080 |  | 
|  | 4081 | if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) { | 
|  | 4082 | if (have_fastchunks(av)) | 
|  | 4083 | malloc_consolidate(av); | 
|  | 4084 |  | 
|  | 4085 | if (av == &main_arena) { | 
|  | 4086 | #ifndef MORECORE_CANNOT_TRIM | 
|  | 4087 | if ((unsigned long)(chunksize(av->top)) >= | 
|  | 4088 | (unsigned long)(mp_.trim_threshold)) | 
|  | 4089 | systrim(mp_.top_pad, av); | 
|  | 4090 | #endif | 
|  | 4091 | } else { | 
|  | 4092 | /* Always try heap_trim(), even if the top chunk is not | 
|  | 4093 | large, because the corresponding heap might go away.  */ | 
|  | 4094 | heap_info *heap = heap_for_ptr(top(av)); | 
|  | 4095 |  | 
|  | 4096 | assert(heap->ar_ptr == av); | 
|  | 4097 | heap_trim(heap, mp_.top_pad); | 
|  | 4098 | } | 
|  | 4099 | } | 
|  | 4100 |  | 
|  | 4101 | if (! have_lock) { | 
|  | 4102 | assert (locked); | 
|  | 4103 | (void)mutex_unlock(&av->mutex); | 
|  | 4104 | } | 
|  | 4105 | } | 
|  | 4106 | /* | 
|  | 4107 | If the chunk was allocated via mmap, release via munmap(). | 
|  | 4108 | */ | 
|  | 4109 |  | 
|  | 4110 | else { | 
|  | 4111 | munmap_chunk (p); | 
|  | 4112 | } | 
|  | 4113 | } | 
|  | 4114 |  | 
|  | 4115 | /* | 
|  | 4116 | ------------------------- malloc_consolidate ------------------------- | 
|  | 4117 |  | 
|  | 4118 | malloc_consolidate is a specialized version of free() that tears | 
|  | 4119 | down chunks held in fastbins.  Free itself cannot be used for this | 
|  | 4120 | purpose since, among other things, it might place chunks back onto | 
|  | 4121 | fastbins.  So, instead, we need to use a minor variant of the same | 
|  | 4122 | code. | 
|  | 4123 |  | 
|  | 4124 | Also, because this routine needs to be called the first time through | 
|  | 4125 | malloc anyway, it turns out to be the perfect place to trigger | 
|  | 4126 | initialization code. | 
|  | 4127 | */ | 
|  | 4128 |  | 
|  | 4129 | static void malloc_consolidate(mstate av) | 
|  | 4130 | { | 
|  | 4131 | mfastbinptr*    fb;                 /* current fastbin being consolidated */ | 
|  | 4132 | mfastbinptr*    maxfb;              /* last fastbin (for loop control) */ | 
|  | 4133 | mchunkptr       p;                  /* current chunk being consolidated */ | 
|  | 4134 | mchunkptr       nextp;              /* next chunk to consolidate */ | 
|  | 4135 | mchunkptr       unsorted_bin;       /* bin header */ | 
|  | 4136 | mchunkptr       first_unsorted;     /* chunk to link to */ | 
|  | 4137 |  | 
|  | 4138 | /* These have same use as in free() */ | 
|  | 4139 | mchunkptr       nextchunk; | 
|  | 4140 | INTERNAL_SIZE_T size; | 
|  | 4141 | INTERNAL_SIZE_T nextsize; | 
|  | 4142 | INTERNAL_SIZE_T prevsize; | 
|  | 4143 | int             nextinuse; | 
|  | 4144 | mchunkptr       bck; | 
|  | 4145 | mchunkptr       fwd; | 
|  | 4146 |  | 
|  | 4147 | /* | 
|  | 4148 | If max_fast is 0, we know that av hasn't | 
|  | 4149 | yet been initialized, in which case do so below | 
|  | 4150 | */ | 
|  | 4151 |  | 
|  | 4152 | if (get_max_fast () != 0) { | 
|  | 4153 | clear_fastchunks(av); | 
|  | 4154 |  | 
|  | 4155 | unsorted_bin = unsorted_chunks(av); | 
|  | 4156 |  | 
|  | 4157 | /* | 
|  | 4158 | Remove each chunk from fast bin and consolidate it, placing it | 
|  | 4159 | then in unsorted bin. Among other reasons for doing this, | 
|  | 4160 | placing in unsorted bin avoids needing to calculate actual bins | 
|  | 4161 | until malloc is sure that chunks aren't immediately going to be | 
|  | 4162 | reused anyway. | 
|  | 4163 | */ | 
|  | 4164 |  | 
|  | 4165 | maxfb = &fastbin (av, NFASTBINS - 1); | 
|  | 4166 | fb = &fastbin (av, 0); | 
|  | 4167 | do { | 
|  | 4168 | p = atomic_exchange_acq (fb, 0); | 
|  | 4169 | if (p != 0) { | 
|  | 4170 | do { | 
|  | 4171 | check_inuse_chunk(av, p); | 
|  | 4172 | nextp = p->fd; | 
|  | 4173 |  | 
|  | 4174 | /* Slightly streamlined version of consolidation code in free() */ | 
|  | 4175 | size = p->size & ~(PREV_INUSE|NON_MAIN_ARENA); | 
|  | 4176 | nextchunk = chunk_at_offset(p, size); | 
|  | 4177 | nextsize = chunksize(nextchunk); | 
|  | 4178 |  | 
|  | 4179 | if (!prev_inuse(p)) { | 
|  | 4180 | prevsize = p->prev_size; | 
|  | 4181 | size += prevsize; | 
|  | 4182 | p = chunk_at_offset(p, -((long) prevsize)); | 
|  | 4183 | unlink(av, p, bck, fwd); | 
|  | 4184 | } | 
|  | 4185 |  | 
|  | 4186 | if (nextchunk != av->top) { | 
|  | 4187 | nextinuse = inuse_bit_at_offset(nextchunk, nextsize); | 
|  | 4188 |  | 
|  | 4189 | if (!nextinuse) { | 
|  | 4190 | size += nextsize; | 
|  | 4191 | unlink(av, nextchunk, bck, fwd); | 
|  | 4192 | } else | 
|  | 4193 | clear_inuse_bit_at_offset(nextchunk, 0); | 
|  | 4194 |  | 
|  | 4195 | first_unsorted = unsorted_bin->fd; | 
|  | 4196 | unsorted_bin->fd = p; | 
|  | 4197 | first_unsorted->bk = p; | 
|  | 4198 |  | 
|  | 4199 | if (!in_smallbin_range (size)) { | 
|  | 4200 | p->fd_nextsize = NULL; | 
|  | 4201 | p->bk_nextsize = NULL; | 
|  | 4202 | } | 
|  | 4203 |  | 
|  | 4204 | set_head(p, size | PREV_INUSE); | 
|  | 4205 | p->bk = unsorted_bin; | 
|  | 4206 | p->fd = first_unsorted; | 
|  | 4207 | set_foot(p, size); | 
|  | 4208 | } | 
|  | 4209 |  | 
|  | 4210 | else { | 
|  | 4211 | size += nextsize; | 
|  | 4212 | set_head(p, size | PREV_INUSE); | 
|  | 4213 | av->top = p; | 
|  | 4214 | } | 
|  | 4215 |  | 
|  | 4216 | } while ( (p = nextp) != 0); | 
|  | 4217 |  | 
|  | 4218 | } | 
|  | 4219 | } while (fb++ != maxfb); | 
|  | 4220 | } | 
|  | 4221 | else { | 
|  | 4222 | malloc_init_state(av); | 
|  | 4223 | check_malloc_state(av); | 
|  | 4224 | } | 
|  | 4225 | } | 
|  | 4226 |  | 
|  | 4227 | /* | 
|  | 4228 | ------------------------------ realloc ------------------------------ | 
|  | 4229 | */ | 
|  | 4230 |  | 
|  | 4231 | void* | 
|  | 4232 | _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize, | 
|  | 4233 | INTERNAL_SIZE_T nb) | 
|  | 4234 | { | 
|  | 4235 | mchunkptr        newp;            /* chunk to return */ | 
|  | 4236 | INTERNAL_SIZE_T  newsize;         /* its size */ | 
|  | 4237 | void*          newmem;          /* corresponding user mem */ | 
|  | 4238 |  | 
|  | 4239 | mchunkptr        next;            /* next contiguous chunk after oldp */ | 
|  | 4240 |  | 
|  | 4241 | mchunkptr        remainder;       /* extra space at end of newp */ | 
|  | 4242 | unsigned long    remainder_size;  /* its size */ | 
|  | 4243 |  | 
|  | 4244 | mchunkptr        bck;             /* misc temp for linking */ | 
|  | 4245 | mchunkptr        fwd;             /* misc temp for linking */ | 
|  | 4246 |  | 
|  | 4247 | unsigned long    copysize;        /* bytes to copy */ | 
|  | 4248 | unsigned int     ncopies;         /* INTERNAL_SIZE_T words to copy */ | 
|  | 4249 | INTERNAL_SIZE_T* s;               /* copy source */ | 
|  | 4250 | INTERNAL_SIZE_T* d;               /* copy destination */ | 
|  | 4251 |  | 
|  | 4252 | const char *errstr = NULL; | 
|  | 4253 |  | 
|  | 4254 | /* oldmem size */ | 
|  | 4255 | if (__builtin_expect (oldp->size <= 2 * SIZE_SZ, 0) | 
|  | 4256 | || __builtin_expect (oldsize >= av->system_mem, 0)) | 
|  | 4257 | { | 
|  | 4258 | errstr = "realloc(): invalid old size"; | 
|  | 4259 | errout: | 
|  | 4260 | malloc_printerr (check_action, errstr, chunk2mem (oldp), av); | 
|  | 4261 | return NULL; | 
|  | 4262 | } | 
|  | 4263 |  | 
|  | 4264 | check_inuse_chunk (av, oldp); | 
|  | 4265 |  | 
|  | 4266 | /* All callers already filter out mmap'ed chunks.  */ | 
|  | 4267 | assert (!chunk_is_mmapped (oldp)); | 
|  | 4268 |  | 
|  | 4269 | next = chunk_at_offset (oldp, oldsize); | 
|  | 4270 | INTERNAL_SIZE_T nextsize = chunksize (next); | 
|  | 4271 | if (__builtin_expect (next->size <= 2 * SIZE_SZ, 0) | 
|  | 4272 | || __builtin_expect (nextsize >= av->system_mem, 0)) | 
|  | 4273 | { | 
|  | 4274 | errstr = "realloc(): invalid next size"; | 
|  | 4275 | goto errout; | 
|  | 4276 | } | 
|  | 4277 |  | 
|  | 4278 | if ((unsigned long) (oldsize) >= (unsigned long) (nb)) | 
|  | 4279 | { | 
|  | 4280 | /* already big enough; split below */ | 
|  | 4281 | newp = oldp; | 
|  | 4282 | newsize = oldsize; | 
|  | 4283 | } | 
|  | 4284 |  | 
|  | 4285 | else | 
|  | 4286 | { | 
|  | 4287 | /* Try to expand forward into top */ | 
|  | 4288 | if (next == av->top && | 
|  | 4289 | (unsigned long) (newsize = oldsize + nextsize) >= | 
|  | 4290 | (unsigned long) (nb + MINSIZE)) | 
|  | 4291 | { | 
|  | 4292 | set_head_size (oldp, nb | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 4293 | av->top = chunk_at_offset (oldp, nb); | 
|  | 4294 | set_head (av->top, (newsize - nb) | PREV_INUSE); | 
|  | 4295 | check_inuse_chunk (av, oldp); | 
|  | 4296 | return chunk2mem (oldp); | 
|  | 4297 | } | 
|  | 4298 |  | 
|  | 4299 | /* Try to expand forward into next chunk;  split off remainder below */ | 
|  | 4300 | else if (next != av->top && | 
|  | 4301 | !inuse (next) && | 
|  | 4302 | (unsigned long) (newsize = oldsize + nextsize) >= | 
|  | 4303 | (unsigned long) (nb)) | 
|  | 4304 | { | 
|  | 4305 | newp = oldp; | 
|  | 4306 | unlink (av, next, bck, fwd); | 
|  | 4307 | } | 
|  | 4308 |  | 
|  | 4309 | /* allocate, copy, free */ | 
|  | 4310 | else | 
|  | 4311 | { | 
|  | 4312 | newmem = _int_malloc (av, nb - MALLOC_ALIGN_MASK); | 
|  | 4313 | if (newmem == 0) | 
|  | 4314 | return 0; /* propagate failure */ | 
|  | 4315 |  | 
|  | 4316 | newp = mem2chunk (newmem); | 
|  | 4317 | newsize = chunksize (newp); | 
|  | 4318 |  | 
|  | 4319 | /* | 
|  | 4320 | Avoid copy if newp is next chunk after oldp. | 
|  | 4321 | */ | 
|  | 4322 | if (newp == next) | 
|  | 4323 | { | 
|  | 4324 | newsize += oldsize; | 
|  | 4325 | newp = oldp; | 
|  | 4326 | } | 
|  | 4327 | else | 
|  | 4328 | { | 
|  | 4329 | /* | 
|  | 4330 | Unroll copy of <= 36 bytes (72 if 8byte sizes) | 
|  | 4331 | We know that contents have an odd number of | 
|  | 4332 | INTERNAL_SIZE_T-sized words; minimally 3. | 
|  | 4333 | */ | 
|  | 4334 |  | 
|  | 4335 | copysize = oldsize - SIZE_SZ; | 
|  | 4336 | s = (INTERNAL_SIZE_T *) (chunk2mem (oldp)); | 
|  | 4337 | d = (INTERNAL_SIZE_T *) (newmem); | 
|  | 4338 | ncopies = copysize / sizeof (INTERNAL_SIZE_T); | 
|  | 4339 | assert (ncopies >= 3); | 
|  | 4340 |  | 
|  | 4341 | if (ncopies > 9) | 
|  | 4342 | memcpy (d, s, copysize); | 
|  | 4343 |  | 
|  | 4344 | else | 
|  | 4345 | { | 
|  | 4346 | *(d + 0) = *(s + 0); | 
|  | 4347 | *(d + 1) = *(s + 1); | 
|  | 4348 | *(d + 2) = *(s + 2); | 
|  | 4349 | if (ncopies > 4) | 
|  | 4350 | { | 
|  | 4351 | *(d + 3) = *(s + 3); | 
|  | 4352 | *(d + 4) = *(s + 4); | 
|  | 4353 | if (ncopies > 6) | 
|  | 4354 | { | 
|  | 4355 | *(d + 5) = *(s + 5); | 
|  | 4356 | *(d + 6) = *(s + 6); | 
|  | 4357 | if (ncopies > 8) | 
|  | 4358 | { | 
|  | 4359 | *(d + 7) = *(s + 7); | 
|  | 4360 | *(d + 8) = *(s + 8); | 
|  | 4361 | } | 
|  | 4362 | } | 
|  | 4363 | } | 
|  | 4364 | } | 
|  | 4365 |  | 
|  | 4366 | _int_free (av, oldp, 1); | 
|  | 4367 | check_inuse_chunk (av, newp); | 
|  | 4368 | return chunk2mem (newp); | 
|  | 4369 | } | 
|  | 4370 | } | 
|  | 4371 | } | 
|  | 4372 |  | 
|  | 4373 | /* If possible, free extra space in old or extended chunk */ | 
|  | 4374 |  | 
|  | 4375 | assert ((unsigned long) (newsize) >= (unsigned long) (nb)); | 
|  | 4376 |  | 
|  | 4377 | remainder_size = newsize - nb; | 
|  | 4378 |  | 
|  | 4379 | if (remainder_size < MINSIZE)   /* not enough extra to split off */ | 
|  | 4380 | { | 
|  | 4381 | set_head_size (newp, newsize | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 4382 | set_inuse_bit_at_offset (newp, newsize); | 
|  | 4383 | } | 
|  | 4384 | else   /* split remainder */ | 
|  | 4385 | { | 
|  | 4386 | remainder = chunk_at_offset (newp, nb); | 
|  | 4387 | set_head_size (newp, nb | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 4388 | set_head (remainder, remainder_size | PREV_INUSE | | 
|  | 4389 | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 4390 | /* Mark remainder as inuse so free() won't complain */ | 
|  | 4391 | set_inuse_bit_at_offset (remainder, remainder_size); | 
|  | 4392 | _int_free (av, remainder, 1); | 
|  | 4393 | } | 
|  | 4394 |  | 
|  | 4395 | check_inuse_chunk (av, newp); | 
|  | 4396 | return chunk2mem (newp); | 
|  | 4397 | } | 
|  | 4398 |  | 
|  | 4399 | /* | 
|  | 4400 | ------------------------------ memalign ------------------------------ | 
|  | 4401 | */ | 
|  | 4402 |  | 
|  | 4403 | static void * | 
|  | 4404 | _int_memalign (mstate av, size_t alignment, size_t bytes) | 
|  | 4405 | { | 
|  | 4406 | INTERNAL_SIZE_T nb;             /* padded  request size */ | 
|  | 4407 | char *m;                        /* memory returned by malloc call */ | 
|  | 4408 | mchunkptr p;                    /* corresponding chunk */ | 
|  | 4409 | char *brk;                      /* alignment point within p */ | 
|  | 4410 | mchunkptr newp;                 /* chunk to return */ | 
|  | 4411 | INTERNAL_SIZE_T newsize;        /* its size */ | 
|  | 4412 | INTERNAL_SIZE_T leadsize;       /* leading space before alignment point */ | 
|  | 4413 | mchunkptr remainder;            /* spare room at end to split off */ | 
|  | 4414 | unsigned long remainder_size;   /* its size */ | 
|  | 4415 | INTERNAL_SIZE_T size; | 
|  | 4416 |  | 
|  | 4417 |  | 
|  | 4418 |  | 
|  | 4419 | checked_request2size (bytes, nb); | 
|  | 4420 |  | 
|  | 4421 | /* | 
|  | 4422 | Strategy: find a spot within that chunk that meets the alignment | 
|  | 4423 | request, and then possibly free the leading and trailing space. | 
|  | 4424 | */ | 
| xf.li | f233062 | 2024-05-15 18:17:18 -0700 | [diff] [blame] | 4425 | /* Check for overflow.  */ | 
|  | 4426 | if (nb > SIZE_MAX - alignment - MINSIZE) | 
|  | 4427 | { | 
|  | 4428 | __set_errno (ENOMEM); | 
|  | 4429 | return 0; | 
|  | 4430 | } | 
| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 4431 |  | 
|  | 4432 | /* Call malloc with worst case padding to hit alignment. */ | 
|  | 4433 |  | 
|  | 4434 | m = (char *) (_int_malloc (av, nb + alignment + MINSIZE)); | 
|  | 4435 |  | 
|  | 4436 | if (m == 0) | 
|  | 4437 | return 0;           /* propagate failure */ | 
|  | 4438 |  | 
|  | 4439 | p = mem2chunk (m); | 
|  | 4440 |  | 
|  | 4441 | if ((((unsigned long) (m)) % alignment) != 0)   /* misaligned */ | 
|  | 4442 |  | 
|  | 4443 | { /* | 
|  | 4444 | Find an aligned spot inside chunk.  Since we need to give back | 
|  | 4445 | leading space in a chunk of at least MINSIZE, if the first | 
|  | 4446 | calculation places us at a spot with less than MINSIZE leader, | 
|  | 4447 | we can move to the next aligned spot -- we've allocated enough | 
|  | 4448 | total room so that this is always possible. | 
|  | 4449 | */ | 
|  | 4450 | brk = (char *) mem2chunk (((unsigned long) (m + alignment - 1)) & | 
|  | 4451 | - ((signed long) alignment)); | 
|  | 4452 | if ((unsigned long) (brk - (char *) (p)) < MINSIZE) | 
|  | 4453 | brk += alignment; | 
|  | 4454 |  | 
|  | 4455 | newp = (mchunkptr) brk; | 
|  | 4456 | leadsize = brk - (char *) (p); | 
|  | 4457 | newsize = chunksize (p) - leadsize; | 
|  | 4458 |  | 
|  | 4459 | /* For mmapped chunks, just adjust offset */ | 
|  | 4460 | if (chunk_is_mmapped (p)) | 
|  | 4461 | { | 
|  | 4462 | newp->prev_size = p->prev_size + leadsize; | 
|  | 4463 | set_head (newp, newsize | IS_MMAPPED); | 
|  | 4464 | return chunk2mem (newp); | 
|  | 4465 | } | 
|  | 4466 |  | 
|  | 4467 | /* Otherwise, give back leader, use the rest */ | 
|  | 4468 | set_head (newp, newsize | PREV_INUSE | | 
|  | 4469 | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 4470 | set_inuse_bit_at_offset (newp, newsize); | 
|  | 4471 | set_head_size (p, leadsize | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 4472 | _int_free (av, p, 1); | 
|  | 4473 | p = newp; | 
|  | 4474 |  | 
|  | 4475 | assert (newsize >= nb && | 
|  | 4476 | (((unsigned long) (chunk2mem (p))) % alignment) == 0); | 
|  | 4477 | } | 
|  | 4478 |  | 
|  | 4479 | /* Also give back spare room at the end */ | 
|  | 4480 | if (!chunk_is_mmapped (p)) | 
|  | 4481 | { | 
|  | 4482 | size = chunksize (p); | 
|  | 4483 | if ((unsigned long) (size) > (unsigned long) (nb + MINSIZE)) | 
|  | 4484 | { | 
|  | 4485 | remainder_size = size - nb; | 
|  | 4486 | remainder = chunk_at_offset (p, nb); | 
|  | 4487 | set_head (remainder, remainder_size | PREV_INUSE | | 
|  | 4488 | (av != &main_arena ? NON_MAIN_ARENA : 0)); | 
|  | 4489 | set_head_size (p, nb); | 
|  | 4490 | _int_free (av, remainder, 1); | 
|  | 4491 | } | 
|  | 4492 | } | 
|  | 4493 |  | 
|  | 4494 | check_inuse_chunk (av, p); | 
|  | 4495 | return chunk2mem (p); | 
|  | 4496 | } | 
|  | 4497 |  | 
|  | 4498 |  | 
|  | 4499 | /* | 
|  | 4500 | ------------------------------ malloc_trim ------------------------------ | 
|  | 4501 | */ | 
|  | 4502 |  | 
|  | 4503 | static int | 
|  | 4504 | mtrim (mstate av, size_t pad) | 
|  | 4505 | { | 
|  | 4506 | /* Don't touch corrupt arenas.  */ | 
|  | 4507 | if (arena_is_corrupt (av)) | 
|  | 4508 | return 0; | 
|  | 4509 |  | 
|  | 4510 | /* Ensure initialization/consolidation */ | 
|  | 4511 | malloc_consolidate (av); | 
|  | 4512 |  | 
|  | 4513 | const size_t ps = GLRO (dl_pagesize); | 
|  | 4514 | int psindex = bin_index (ps); | 
|  | 4515 | const size_t psm1 = ps - 1; | 
|  | 4516 |  | 
|  | 4517 | int result = 0; | 
|  | 4518 | for (int i = 1; i < NBINS; ++i) | 
|  | 4519 | if (i == 1 || i >= psindex) | 
|  | 4520 | { | 
|  | 4521 | mbinptr bin = bin_at (av, i); | 
|  | 4522 |  | 
|  | 4523 | for (mchunkptr p = last (bin); p != bin; p = p->bk) | 
|  | 4524 | { | 
|  | 4525 | INTERNAL_SIZE_T size = chunksize (p); | 
|  | 4526 |  | 
|  | 4527 | if (size > psm1 + sizeof (struct malloc_chunk)) | 
|  | 4528 | { | 
|  | 4529 | /* See whether the chunk contains at least one unused page.  */ | 
|  | 4530 | char *paligned_mem = (char *) (((uintptr_t) p | 
|  | 4531 | + sizeof (struct malloc_chunk) | 
|  | 4532 | + psm1) & ~psm1); | 
|  | 4533 |  | 
|  | 4534 | assert ((char *) chunk2mem (p) + 4 * SIZE_SZ <= paligned_mem); | 
|  | 4535 | assert ((char *) p + size > paligned_mem); | 
|  | 4536 |  | 
|  | 4537 | /* This is the size we could potentially free.  */ | 
|  | 4538 | size -= paligned_mem - (char *) p; | 
|  | 4539 |  | 
|  | 4540 | if (size > psm1) | 
|  | 4541 | { | 
|  | 4542 | #if MALLOC_DEBUG | 
|  | 4543 | /* When debugging we simulate destroying the memory | 
|  | 4544 | content.  */ | 
|  | 4545 | memset (paligned_mem, 0x89, size & ~psm1); | 
|  | 4546 | #endif | 
|  | 4547 | __madvise (paligned_mem, size & ~psm1, MADV_DONTNEED); | 
|  | 4548 |  | 
|  | 4549 | result = 1; | 
|  | 4550 | } | 
|  | 4551 | } | 
|  | 4552 | } | 
|  | 4553 | } | 
|  | 4554 |  | 
|  | 4555 | #ifndef MORECORE_CANNOT_TRIM | 
|  | 4556 | return result | (av == &main_arena ? systrim (pad, av) : 0); | 
|  | 4557 |  | 
|  | 4558 | #else | 
|  | 4559 | return result; | 
|  | 4560 | #endif | 
|  | 4561 | } | 
|  | 4562 |  | 
|  | 4563 |  | 
|  | 4564 | int | 
|  | 4565 | __malloc_trim (size_t s) | 
|  | 4566 | { | 
|  | 4567 | int result = 0; | 
|  | 4568 |  | 
|  | 4569 | if (__malloc_initialized < 0) | 
|  | 4570 | ptmalloc_init (); | 
|  | 4571 |  | 
|  | 4572 | mstate ar_ptr = &main_arena; | 
|  | 4573 | do | 
|  | 4574 | { | 
|  | 4575 | (void) mutex_lock (&ar_ptr->mutex); | 
|  | 4576 | result |= mtrim (ar_ptr, s); | 
|  | 4577 | (void) mutex_unlock (&ar_ptr->mutex); | 
|  | 4578 |  | 
|  | 4579 | ar_ptr = ar_ptr->next; | 
|  | 4580 | } | 
|  | 4581 | while (ar_ptr != &main_arena); | 
|  | 4582 |  | 
|  | 4583 | return result; | 
|  | 4584 | } | 
|  | 4585 |  | 
|  | 4586 |  | 
|  | 4587 | /* | 
|  | 4588 | ------------------------- malloc_usable_size ------------------------- | 
|  | 4589 | */ | 
|  | 4590 |  | 
|  | 4591 | static size_t | 
|  | 4592 | musable (void *mem) | 
|  | 4593 | { | 
|  | 4594 | mchunkptr p; | 
|  | 4595 | if (mem != 0) | 
|  | 4596 | { | 
|  | 4597 | p = mem2chunk (mem); | 
|  | 4598 |  | 
|  | 4599 | if (__builtin_expect (using_malloc_checking == 1, 0)) | 
|  | 4600 | return malloc_check_get_size (p); | 
|  | 4601 |  | 
|  | 4602 | if (chunk_is_mmapped (p)) | 
|  | 4603 | return chunksize (p) - 2 * SIZE_SZ; | 
|  | 4604 | else if (inuse (p)) | 
|  | 4605 | return chunksize (p) - SIZE_SZ; | 
|  | 4606 | } | 
|  | 4607 | return 0; | 
|  | 4608 | } | 
|  | 4609 |  | 
|  | 4610 |  | 
|  | 4611 | size_t | 
|  | 4612 | __malloc_usable_size (void *m) | 
|  | 4613 | { | 
|  | 4614 | size_t result; | 
|  | 4615 |  | 
|  | 4616 | result = musable (m); | 
|  | 4617 | return result; | 
|  | 4618 | } | 
|  | 4619 |  | 
|  | 4620 | /* | 
|  | 4621 | ------------------------------ mallinfo ------------------------------ | 
|  | 4622 | Accumulate malloc statistics for arena AV into M. | 
|  | 4623 | */ | 
|  | 4624 |  | 
|  | 4625 | static void | 
|  | 4626 | int_mallinfo (mstate av, struct mallinfo *m) | 
|  | 4627 | { | 
|  | 4628 | size_t i; | 
|  | 4629 | mbinptr b; | 
|  | 4630 | mchunkptr p; | 
|  | 4631 | INTERNAL_SIZE_T avail; | 
|  | 4632 | INTERNAL_SIZE_T fastavail; | 
|  | 4633 | int nblocks; | 
|  | 4634 | int nfastblocks; | 
|  | 4635 |  | 
|  | 4636 | /* Ensure initialization */ | 
|  | 4637 | if (av->top == 0) | 
|  | 4638 | malloc_consolidate (av); | 
|  | 4639 |  | 
|  | 4640 | check_malloc_state (av); | 
|  | 4641 |  | 
|  | 4642 | /* Account for top */ | 
|  | 4643 | avail = chunksize (av->top); | 
|  | 4644 | nblocks = 1;  /* top always exists */ | 
|  | 4645 |  | 
|  | 4646 | /* traverse fastbins */ | 
|  | 4647 | nfastblocks = 0; | 
|  | 4648 | fastavail = 0; | 
|  | 4649 |  | 
|  | 4650 | for (i = 0; i < NFASTBINS; ++i) | 
|  | 4651 | { | 
|  | 4652 | for (p = fastbin (av, i); p != 0; p = p->fd) | 
|  | 4653 | { | 
|  | 4654 | ++nfastblocks; | 
|  | 4655 | fastavail += chunksize (p); | 
|  | 4656 | } | 
|  | 4657 | } | 
|  | 4658 |  | 
|  | 4659 | avail += fastavail; | 
|  | 4660 |  | 
|  | 4661 | /* traverse regular bins */ | 
|  | 4662 | for (i = 1; i < NBINS; ++i) | 
|  | 4663 | { | 
|  | 4664 | b = bin_at (av, i); | 
|  | 4665 | for (p = last (b); p != b; p = p->bk) | 
|  | 4666 | { | 
|  | 4667 | ++nblocks; | 
|  | 4668 | avail += chunksize (p); | 
|  | 4669 | } | 
|  | 4670 | } | 
|  | 4671 |  | 
|  | 4672 | m->smblks += nfastblocks; | 
|  | 4673 | m->ordblks += nblocks; | 
|  | 4674 | m->fordblks += avail; | 
|  | 4675 | m->uordblks += av->system_mem - avail; | 
|  | 4676 | m->arena += av->system_mem; | 
|  | 4677 | m->fsmblks += fastavail; | 
|  | 4678 | if (av == &main_arena) | 
|  | 4679 | { | 
|  | 4680 | m->hblks = mp_.n_mmaps; | 
|  | 4681 | m->hblkhd = mp_.mmapped_mem; | 
|  | 4682 | m->usmblks = mp_.max_total_mem; | 
|  | 4683 | m->keepcost = chunksize (av->top); | 
|  | 4684 | } | 
|  | 4685 | } | 
|  | 4686 |  | 
|  | 4687 |  | 
|  | 4688 | struct mallinfo | 
|  | 4689 | __libc_mallinfo (void) | 
|  | 4690 | { | 
|  | 4691 | struct mallinfo m; | 
|  | 4692 | mstate ar_ptr; | 
|  | 4693 |  | 
|  | 4694 | if (__malloc_initialized < 0) | 
|  | 4695 | ptmalloc_init (); | 
|  | 4696 |  | 
|  | 4697 | memset (&m, 0, sizeof (m)); | 
|  | 4698 | ar_ptr = &main_arena; | 
|  | 4699 | do | 
|  | 4700 | { | 
|  | 4701 | (void) mutex_lock (&ar_ptr->mutex); | 
|  | 4702 | int_mallinfo (ar_ptr, &m); | 
|  | 4703 | (void) mutex_unlock (&ar_ptr->mutex); | 
|  | 4704 |  | 
|  | 4705 | ar_ptr = ar_ptr->next; | 
|  | 4706 | } | 
|  | 4707 | while (ar_ptr != &main_arena); | 
|  | 4708 |  | 
|  | 4709 | return m; | 
|  | 4710 | } | 
|  | 4711 |  | 
|  | 4712 | /* | 
|  | 4713 | ------------------------------ malloc_stats ------------------------------ | 
|  | 4714 | */ | 
|  | 4715 |  | 
|  | 4716 | void | 
|  | 4717 | __malloc_stats (void) | 
|  | 4718 | { | 
|  | 4719 | int i; | 
|  | 4720 | mstate ar_ptr; | 
|  | 4721 | unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b; | 
|  | 4722 |  | 
|  | 4723 | if (__malloc_initialized < 0) | 
|  | 4724 | ptmalloc_init (); | 
|  | 4725 | _IO_flockfile (stderr); | 
|  | 4726 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 
|  | 4727 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; | 
|  | 4728 | for (i = 0, ar_ptr = &main_arena;; i++) | 
|  | 4729 | { | 
|  | 4730 | struct mallinfo mi; | 
|  | 4731 |  | 
|  | 4732 | memset (&mi, 0, sizeof (mi)); | 
|  | 4733 | (void) mutex_lock (&ar_ptr->mutex); | 
|  | 4734 | int_mallinfo (ar_ptr, &mi); | 
|  | 4735 | fprintf (stderr, "Arena %d:\n", i); | 
|  | 4736 | fprintf (stderr, "system bytes     = %10u\n", (unsigned int) mi.arena); | 
|  | 4737 | fprintf (stderr, "in use bytes     = %10u\n", (unsigned int) mi.uordblks); | 
|  | 4738 | #if MALLOC_DEBUG > 1 | 
|  | 4739 | if (i > 0) | 
|  | 4740 | dump_heap (heap_for_ptr (top (ar_ptr))); | 
|  | 4741 | #endif | 
|  | 4742 | system_b += mi.arena; | 
|  | 4743 | in_use_b += mi.uordblks; | 
|  | 4744 | (void) mutex_unlock (&ar_ptr->mutex); | 
|  | 4745 | ar_ptr = ar_ptr->next; | 
|  | 4746 | if (ar_ptr == &main_arena) | 
|  | 4747 | break; | 
|  | 4748 | } | 
|  | 4749 | fprintf (stderr, "Total (incl. mmap):\n"); | 
|  | 4750 | fprintf (stderr, "system bytes     = %10u\n", system_b); | 
|  | 4751 | fprintf (stderr, "in use bytes     = %10u\n", in_use_b); | 
|  | 4752 | fprintf (stderr, "max mmap regions = %10u\n", (unsigned int) mp_.max_n_mmaps); | 
|  | 4753 | fprintf (stderr, "max mmap bytes   = %10lu\n", | 
|  | 4754 | (unsigned long) mp_.max_mmapped_mem); | 
|  | 4755 | ((_IO_FILE *) stderr)->_flags2 |= old_flags2; | 
|  | 4756 | _IO_funlockfile (stderr); | 
|  | 4757 | } | 
|  | 4758 |  | 
|  | 4759 |  | 
|  | 4760 | /* | 
|  | 4761 | ------------------------------ mallopt ------------------------------ | 
|  | 4762 | */ | 
|  | 4763 |  | 
|  | 4764 | int | 
|  | 4765 | __libc_mallopt (int param_number, int value) | 
|  | 4766 | { | 
|  | 4767 | mstate av = &main_arena; | 
|  | 4768 | int res = 1; | 
|  | 4769 |  | 
|  | 4770 | if (__malloc_initialized < 0) | 
|  | 4771 | ptmalloc_init (); | 
|  | 4772 | (void) mutex_lock (&av->mutex); | 
|  | 4773 | /* Ensure initialization/consolidation */ | 
|  | 4774 | malloc_consolidate (av); | 
|  | 4775 |  | 
|  | 4776 | LIBC_PROBE (memory_mallopt, 2, param_number, value); | 
|  | 4777 |  | 
|  | 4778 | switch (param_number) | 
|  | 4779 | { | 
|  | 4780 | case M_MXFAST: | 
|  | 4781 | if (value >= 0 && value <= MAX_FAST_SIZE) | 
|  | 4782 | { | 
|  | 4783 | LIBC_PROBE (memory_mallopt_mxfast, 2, value, get_max_fast ()); | 
|  | 4784 | set_max_fast (value); | 
|  | 4785 | } | 
|  | 4786 | else | 
|  | 4787 | res = 0; | 
|  | 4788 | break; | 
|  | 4789 |  | 
|  | 4790 | case M_TRIM_THRESHOLD: | 
|  | 4791 | LIBC_PROBE (memory_mallopt_trim_threshold, 3, value, | 
|  | 4792 | mp_.trim_threshold, mp_.no_dyn_threshold); | 
|  | 4793 | mp_.trim_threshold = value; | 
|  | 4794 | mp_.no_dyn_threshold = 1; | 
|  | 4795 | break; | 
|  | 4796 |  | 
|  | 4797 | case M_TOP_PAD: | 
|  | 4798 | LIBC_PROBE (memory_mallopt_top_pad, 3, value, | 
|  | 4799 | mp_.top_pad, mp_.no_dyn_threshold); | 
|  | 4800 | mp_.top_pad = value; | 
|  | 4801 | mp_.no_dyn_threshold = 1; | 
|  | 4802 | break; | 
|  | 4803 |  | 
|  | 4804 | case M_MMAP_THRESHOLD: | 
|  | 4805 | /* Forbid setting the threshold too high. */ | 
|  | 4806 | if ((unsigned long) value > HEAP_MAX_SIZE / 2) | 
|  | 4807 | res = 0; | 
|  | 4808 | else | 
|  | 4809 | { | 
|  | 4810 | LIBC_PROBE (memory_mallopt_mmap_threshold, 3, value, | 
|  | 4811 | mp_.mmap_threshold, mp_.no_dyn_threshold); | 
|  | 4812 | mp_.mmap_threshold = value; | 
|  | 4813 | mp_.no_dyn_threshold = 1; | 
|  | 4814 | } | 
|  | 4815 | break; | 
|  | 4816 |  | 
|  | 4817 | case M_MMAP_MAX: | 
|  | 4818 | LIBC_PROBE (memory_mallopt_mmap_max, 3, value, | 
|  | 4819 | mp_.n_mmaps_max, mp_.no_dyn_threshold); | 
|  | 4820 | mp_.n_mmaps_max = value; | 
|  | 4821 | mp_.no_dyn_threshold = 1; | 
|  | 4822 | break; | 
|  | 4823 |  | 
|  | 4824 | case M_CHECK_ACTION: | 
|  | 4825 | LIBC_PROBE (memory_mallopt_check_action, 2, value, check_action); | 
|  | 4826 | check_action = value; | 
|  | 4827 | break; | 
|  | 4828 |  | 
|  | 4829 | case M_PERTURB: | 
|  | 4830 | LIBC_PROBE (memory_mallopt_perturb, 2, value, perturb_byte); | 
|  | 4831 | perturb_byte = value; | 
|  | 4832 | break; | 
|  | 4833 |  | 
|  | 4834 | case M_ARENA_TEST: | 
|  | 4835 | if (value > 0) | 
|  | 4836 | { | 
|  | 4837 | LIBC_PROBE (memory_mallopt_arena_test, 2, value, mp_.arena_test); | 
|  | 4838 | mp_.arena_test = value; | 
|  | 4839 | } | 
|  | 4840 | break; | 
|  | 4841 |  | 
|  | 4842 | case M_ARENA_MAX: | 
|  | 4843 | if (value > 0) | 
|  | 4844 | { | 
|  | 4845 | LIBC_PROBE (memory_mallopt_arena_max, 2, value, mp_.arena_max); | 
|  | 4846 | mp_.arena_max = value; | 
|  | 4847 | } | 
|  | 4848 | break; | 
|  | 4849 | } | 
|  | 4850 | (void) mutex_unlock (&av->mutex); | 
|  | 4851 | return res; | 
|  | 4852 | } | 
|  | 4853 | libc_hidden_def (__libc_mallopt) | 
|  | 4854 |  | 
|  | 4855 |  | 
|  | 4856 | /* | 
|  | 4857 | -------------------- Alternative MORECORE functions -------------------- | 
|  | 4858 | */ | 
|  | 4859 |  | 
|  | 4860 |  | 
|  | 4861 | /* | 
|  | 4862 | General Requirements for MORECORE. | 
|  | 4863 |  | 
|  | 4864 | The MORECORE function must have the following properties: | 
|  | 4865 |  | 
|  | 4866 | If MORECORE_CONTIGUOUS is false: | 
|  | 4867 |  | 
|  | 4868 | * MORECORE must allocate in multiples of pagesize. It will | 
|  | 4869 | only be called with arguments that are multiples of pagesize. | 
|  | 4870 |  | 
|  | 4871 | * MORECORE(0) must return an address that is at least | 
|  | 4872 | MALLOC_ALIGNMENT aligned. (Page-aligning always suffices.) | 
|  | 4873 |  | 
|  | 4874 | else (i.e. If MORECORE_CONTIGUOUS is true): | 
|  | 4875 |  | 
|  | 4876 | * Consecutive calls to MORECORE with positive arguments | 
|  | 4877 | return increasing addresses, indicating that space has been | 
|  | 4878 | contiguously extended. | 
|  | 4879 |  | 
|  | 4880 | * MORECORE need not allocate in multiples of pagesize. | 
|  | 4881 | Calls to MORECORE need not have args of multiples of pagesize. | 
|  | 4882 |  | 
|  | 4883 | * MORECORE need not page-align. | 
|  | 4884 |  | 
|  | 4885 | In either case: | 
|  | 4886 |  | 
|  | 4887 | * MORECORE may allocate more memory than requested. (Or even less, | 
|  | 4888 | but this will generally result in a malloc failure.) | 
|  | 4889 |  | 
|  | 4890 | * MORECORE must not allocate memory when given argument zero, but | 
|  | 4891 | instead return one past the end address of memory from previous | 
|  | 4892 | nonzero call. This malloc does NOT call MORECORE(0) | 
|  | 4893 | until at least one call with positive arguments is made, so | 
|  | 4894 | the initial value returned is not important. | 
|  | 4895 |  | 
|  | 4896 | * Even though consecutive calls to MORECORE need not return contiguous | 
|  | 4897 | addresses, it must be OK for malloc'ed chunks to span multiple | 
|  | 4898 | regions in those cases where they do happen to be contiguous. | 
|  | 4899 |  | 
|  | 4900 | * MORECORE need not handle negative arguments -- it may instead | 
|  | 4901 | just return MORECORE_FAILURE when given negative arguments. | 
|  | 4902 | Negative arguments are always multiples of pagesize. MORECORE | 
|  | 4903 | must not misinterpret negative args as large positive unsigned | 
|  | 4904 | args. You can suppress all such calls from even occurring by defining | 
|  | 4905 | MORECORE_CANNOT_TRIM, | 
|  | 4906 |  | 
|  | 4907 | There is some variation across systems about the type of the | 
|  | 4908 | argument to sbrk/MORECORE. If size_t is unsigned, then it cannot | 
|  | 4909 | actually be size_t, because sbrk supports negative args, so it is | 
|  | 4910 | normally the signed type of the same width as size_t (sometimes | 
|  | 4911 | declared as "intptr_t", and sometimes "ptrdiff_t").  It doesn't much | 
|  | 4912 | matter though. Internally, we use "long" as arguments, which should | 
|  | 4913 | work across all reasonable possibilities. | 
|  | 4914 |  | 
|  | 4915 | Additionally, if MORECORE ever returns failure for a positive | 
|  | 4916 | request, then mmap is used as a noncontiguous system allocator. This | 
|  | 4917 | is a useful backup strategy for systems with holes in address spaces | 
|  | 4918 | -- in this case sbrk cannot contiguously expand the heap, but mmap | 
|  | 4919 | may be able to map noncontiguous space. | 
|  | 4920 |  | 
|  | 4921 | If you'd like mmap to ALWAYS be used, you can define MORECORE to be | 
|  | 4922 | a function that always returns MORECORE_FAILURE. | 
|  | 4923 |  | 
|  | 4924 | If you are using this malloc with something other than sbrk (or its | 
|  | 4925 | emulation) to supply memory regions, you probably want to set | 
|  | 4926 | MORECORE_CONTIGUOUS as false.  As an example, here is a custom | 
|  | 4927 | allocator kindly contributed for pre-OSX macOS.  It uses virtually | 
|  | 4928 | but not necessarily physically contiguous non-paged memory (locked | 
|  | 4929 | in, present and won't get swapped out).  You can use it by | 
|  | 4930 | uncommenting this section, adding some #includes, and setting up the | 
|  | 4931 | appropriate defines above: | 
|  | 4932 |  | 
|  | 4933 | *#define MORECORE osMoreCore | 
|  | 4934 | *#define MORECORE_CONTIGUOUS 0 | 
|  | 4935 |  | 
|  | 4936 | There is also a shutdown routine that should somehow be called for | 
|  | 4937 | cleanup upon program exit. | 
|  | 4938 |  | 
|  | 4939 | *#define MAX_POOL_ENTRIES 100 | 
|  | 4940 | *#define MINIMUM_MORECORE_SIZE  (64 * 1024) | 
|  | 4941 | static int next_os_pool; | 
|  | 4942 | void *our_os_pools[MAX_POOL_ENTRIES]; | 
|  | 4943 |  | 
|  | 4944 | void *osMoreCore(int size) | 
|  | 4945 | { | 
|  | 4946 | void *ptr = 0; | 
|  | 4947 | static void *sbrk_top = 0; | 
|  | 4948 |  | 
|  | 4949 | if (size > 0) | 
|  | 4950 | { | 
|  | 4951 | if (size < MINIMUM_MORECORE_SIZE) | 
|  | 4952 | size = MINIMUM_MORECORE_SIZE; | 
|  | 4953 | if (CurrentExecutionLevel() == kTaskLevel) | 
|  | 4954 | ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0); | 
|  | 4955 | if (ptr == 0) | 
|  | 4956 | { | 
|  | 4957 | return (void *) MORECORE_FAILURE; | 
|  | 4958 | } | 
|  | 4959 | // save ptrs so they can be freed during cleanup | 
|  | 4960 | our_os_pools[next_os_pool] = ptr; | 
|  | 4961 | next_os_pool++; | 
|  | 4962 | ptr = (void *) ((((unsigned long) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK); | 
|  | 4963 | sbrk_top = (char *) ptr + size; | 
|  | 4964 | return ptr; | 
|  | 4965 | } | 
|  | 4966 | else if (size < 0) | 
|  | 4967 | { | 
|  | 4968 | // we don't currently support shrink behavior | 
|  | 4969 | return (void *) MORECORE_FAILURE; | 
|  | 4970 | } | 
|  | 4971 | else | 
|  | 4972 | { | 
|  | 4973 | return sbrk_top; | 
|  | 4974 | } | 
|  | 4975 | } | 
|  | 4976 |  | 
|  | 4977 | // cleanup any allocated memory pools | 
|  | 4978 | // called as last thing before shutting down driver | 
|  | 4979 |  | 
|  | 4980 | void osCleanupMem(void) | 
|  | 4981 | { | 
|  | 4982 | void **ptr; | 
|  | 4983 |  | 
|  | 4984 | for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++) | 
|  | 4985 | if (*ptr) | 
|  | 4986 | { | 
|  | 4987 | PoolDeallocate(*ptr); | 
|  | 4988 | * ptr = 0; | 
|  | 4989 | } | 
|  | 4990 | } | 
|  | 4991 |  | 
|  | 4992 | */ | 
|  | 4993 |  | 
|  | 4994 |  | 
|  | 4995 | /* Helper code.  */ | 
|  | 4996 |  | 
|  | 4997 | extern char **__libc_argv attribute_hidden; | 
|  | 4998 |  | 
|  | 4999 | static void | 
|  | 5000 | malloc_printerr (int action, const char *str, void *ptr, mstate ar_ptr) | 
|  | 5001 | { | 
|  | 5002 | /* Avoid using this arena in future.  We do not attempt to synchronize this | 
|  | 5003 | with anything else because we minimally want to ensure that __libc_message | 
|  | 5004 | gets its resources safely without stumbling on the current corruption.  */ | 
|  | 5005 | if (ar_ptr) | 
|  | 5006 | set_arena_corrupt (ar_ptr); | 
|  | 5007 |  | 
|  | 5008 | if ((action & 5) == 5) | 
|  | 5009 | __libc_message (action & 2, "%s\n", str); | 
|  | 5010 | else if (action & 1) | 
|  | 5011 | { | 
|  | 5012 | char buf[2 * sizeof (uintptr_t) + 1]; | 
|  | 5013 |  | 
|  | 5014 | buf[sizeof (buf) - 1] = '\0'; | 
|  | 5015 | char *cp = _itoa_word ((uintptr_t) ptr, &buf[sizeof (buf) - 1], 16, 0); | 
|  | 5016 | while (cp > buf) | 
|  | 5017 | *--cp = '0'; | 
|  | 5018 |  | 
|  | 5019 | __libc_message (action & 2, "*** Error in `%s': %s: 0x%s ***\n", | 
|  | 5020 | __libc_argv[0] ? : "<unknown>", str, cp); | 
|  | 5021 | } | 
|  | 5022 | else if (action & 2) | 
|  | 5023 | abort (); | 
|  | 5024 | } | 
|  | 5025 |  | 
|  | 5026 | /* We need a wrapper function for one of the additions of POSIX.  */ | 
|  | 5027 | int | 
|  | 5028 | __posix_memalign (void **memptr, size_t alignment, size_t size) | 
|  | 5029 | { | 
|  | 5030 | void *mem; | 
|  | 5031 |  | 
|  | 5032 | /* Test whether the SIZE argument is valid.  It must be a power of | 
|  | 5033 | two multiple of sizeof (void *).  */ | 
|  | 5034 | if (alignment % sizeof (void *) != 0 | 
|  | 5035 | || !powerof2 (alignment / sizeof (void *)) | 
|  | 5036 | || alignment == 0) | 
|  | 5037 | return EINVAL; | 
|  | 5038 |  | 
|  | 5039 |  | 
|  | 5040 | void *address = RETURN_ADDRESS (0); | 
|  | 5041 | mem = _mid_memalign (alignment, size, address); | 
|  | 5042 |  | 
|  | 5043 | if (mem != NULL) | 
|  | 5044 | { | 
|  | 5045 | *memptr = mem; | 
|  | 5046 | return 0; | 
|  | 5047 | } | 
|  | 5048 |  | 
|  | 5049 | return ENOMEM; | 
|  | 5050 | } | 
|  | 5051 | weak_alias (__posix_memalign, posix_memalign) | 
|  | 5052 |  | 
|  | 5053 |  | 
|  | 5054 | int | 
|  | 5055 | __malloc_info (int options, FILE *fp) | 
|  | 5056 | { | 
|  | 5057 | /* For now, at least.  */ | 
|  | 5058 | if (options != 0) | 
|  | 5059 | return EINVAL; | 
|  | 5060 |  | 
|  | 5061 | int n = 0; | 
|  | 5062 | size_t total_nblocks = 0; | 
|  | 5063 | size_t total_nfastblocks = 0; | 
|  | 5064 | size_t total_avail = 0; | 
|  | 5065 | size_t total_fastavail = 0; | 
|  | 5066 | size_t total_system = 0; | 
|  | 5067 | size_t total_max_system = 0; | 
|  | 5068 | size_t total_aspace = 0; | 
|  | 5069 | size_t total_aspace_mprotect = 0; | 
|  | 5070 |  | 
|  | 5071 |  | 
|  | 5072 |  | 
|  | 5073 | if (__malloc_initialized < 0) | 
|  | 5074 | ptmalloc_init (); | 
|  | 5075 |  | 
|  | 5076 | fputs ("<malloc version=\"1\">\n", fp); | 
|  | 5077 |  | 
|  | 5078 | /* Iterate over all arenas currently in use.  */ | 
|  | 5079 | mstate ar_ptr = &main_arena; | 
|  | 5080 | do | 
|  | 5081 | { | 
|  | 5082 | fprintf (fp, "<heap nr=\"%d\">\n<sizes>\n", n++); | 
|  | 5083 |  | 
|  | 5084 | size_t nblocks = 0; | 
|  | 5085 | size_t nfastblocks = 0; | 
|  | 5086 | size_t avail = 0; | 
|  | 5087 | size_t fastavail = 0; | 
|  | 5088 | struct | 
|  | 5089 | { | 
|  | 5090 | size_t from; | 
|  | 5091 | size_t to; | 
|  | 5092 | size_t total; | 
|  | 5093 | size_t count; | 
|  | 5094 | } sizes[NFASTBINS + NBINS - 1]; | 
|  | 5095 | #define nsizes (sizeof (sizes) / sizeof (sizes[0])) | 
|  | 5096 |  | 
|  | 5097 | mutex_lock (&ar_ptr->mutex); | 
|  | 5098 |  | 
|  | 5099 | for (size_t i = 0; i < NFASTBINS; ++i) | 
|  | 5100 | { | 
|  | 5101 | mchunkptr p = fastbin (ar_ptr, i); | 
|  | 5102 | if (p != NULL) | 
|  | 5103 | { | 
|  | 5104 | size_t nthissize = 0; | 
|  | 5105 | size_t thissize = chunksize (p); | 
|  | 5106 |  | 
|  | 5107 | while (p != NULL) | 
|  | 5108 | { | 
|  | 5109 | ++nthissize; | 
|  | 5110 | p = p->fd; | 
|  | 5111 | } | 
|  | 5112 |  | 
|  | 5113 | fastavail += nthissize * thissize; | 
|  | 5114 | nfastblocks += nthissize; | 
|  | 5115 | sizes[i].from = thissize - (MALLOC_ALIGNMENT - 1); | 
|  | 5116 | sizes[i].to = thissize; | 
|  | 5117 | sizes[i].count = nthissize; | 
|  | 5118 | } | 
|  | 5119 | else | 
|  | 5120 | sizes[i].from = sizes[i].to = sizes[i].count = 0; | 
|  | 5121 |  | 
|  | 5122 | sizes[i].total = sizes[i].count * sizes[i].to; | 
|  | 5123 | } | 
|  | 5124 |  | 
|  | 5125 |  | 
|  | 5126 | mbinptr bin; | 
|  | 5127 | struct malloc_chunk *r; | 
|  | 5128 |  | 
|  | 5129 | for (size_t i = 1; i < NBINS; ++i) | 
|  | 5130 | { | 
|  | 5131 | bin = bin_at (ar_ptr, i); | 
|  | 5132 | r = bin->fd; | 
|  | 5133 | sizes[NFASTBINS - 1 + i].from = ~((size_t) 0); | 
|  | 5134 | sizes[NFASTBINS - 1 + i].to = sizes[NFASTBINS - 1 + i].total | 
|  | 5135 | = sizes[NFASTBINS - 1 + i].count = 0; | 
|  | 5136 |  | 
|  | 5137 | if (r != NULL) | 
|  | 5138 | while (r != bin) | 
|  | 5139 | { | 
|  | 5140 | ++sizes[NFASTBINS - 1 + i].count; | 
|  | 5141 | sizes[NFASTBINS - 1 + i].total += r->size; | 
|  | 5142 | sizes[NFASTBINS - 1 + i].from | 
|  | 5143 | = MIN (sizes[NFASTBINS - 1 + i].from, r->size); | 
|  | 5144 | sizes[NFASTBINS - 1 + i].to = MAX (sizes[NFASTBINS - 1 + i].to, | 
|  | 5145 | r->size); | 
|  | 5146 |  | 
|  | 5147 | r = r->fd; | 
|  | 5148 | } | 
|  | 5149 |  | 
|  | 5150 | if (sizes[NFASTBINS - 1 + i].count == 0) | 
|  | 5151 | sizes[NFASTBINS - 1 + i].from = 0; | 
|  | 5152 | nblocks += sizes[NFASTBINS - 1 + i].count; | 
|  | 5153 | avail += sizes[NFASTBINS - 1 + i].total; | 
|  | 5154 | } | 
|  | 5155 |  | 
|  | 5156 | mutex_unlock (&ar_ptr->mutex); | 
|  | 5157 |  | 
|  | 5158 | total_nfastblocks += nfastblocks; | 
|  | 5159 | total_fastavail += fastavail; | 
|  | 5160 |  | 
|  | 5161 | total_nblocks += nblocks; | 
|  | 5162 | total_avail += avail; | 
|  | 5163 |  | 
|  | 5164 | for (size_t i = 0; i < nsizes; ++i) | 
|  | 5165 | if (sizes[i].count != 0 && i != NFASTBINS) | 
|  | 5166 | fprintf (fp, "							      \ | 
|  | 5167 | <size from=\"%zu\" to=\"%zu\" total=\"%zu\" count=\"%zu\"/>\n", | 
|  | 5168 | sizes[i].from, sizes[i].to, sizes[i].total, sizes[i].count); | 
|  | 5169 |  | 
|  | 5170 | if (sizes[NFASTBINS].count != 0) | 
|  | 5171 | fprintf (fp, "\ | 
|  | 5172 | <unsorted from=\"%zu\" to=\"%zu\" total=\"%zu\" count=\"%zu\"/>\n", | 
|  | 5173 | sizes[NFASTBINS].from, sizes[NFASTBINS].to, | 
|  | 5174 | sizes[NFASTBINS].total, sizes[NFASTBINS].count); | 
|  | 5175 |  | 
|  | 5176 | total_system += ar_ptr->system_mem; | 
|  | 5177 | total_max_system += ar_ptr->max_system_mem; | 
|  | 5178 |  | 
|  | 5179 | fprintf (fp, | 
|  | 5180 | "</sizes>\n<total type=\"fast\" count=\"%zu\" size=\"%zu\"/>\n" | 
|  | 5181 | "<total type=\"rest\" count=\"%zu\" size=\"%zu\"/>\n" | 
|  | 5182 | "<system type=\"current\" size=\"%zu\"/>\n" | 
|  | 5183 | "<system type=\"max\" size=\"%zu\"/>\n", | 
|  | 5184 | nfastblocks, fastavail, nblocks, avail, | 
|  | 5185 | ar_ptr->system_mem, ar_ptr->max_system_mem); | 
|  | 5186 |  | 
|  | 5187 | if (ar_ptr != &main_arena) | 
|  | 5188 | { | 
|  | 5189 | heap_info *heap = heap_for_ptr (top (ar_ptr)); | 
|  | 5190 | fprintf (fp, | 
|  | 5191 | "<aspace type=\"total\" size=\"%zu\"/>\n" | 
|  | 5192 | "<aspace type=\"mprotect\" size=\"%zu\"/>\n", | 
|  | 5193 | heap->size, heap->mprotect_size); | 
|  | 5194 | total_aspace += heap->size; | 
|  | 5195 | total_aspace_mprotect += heap->mprotect_size; | 
|  | 5196 | } | 
|  | 5197 | else | 
|  | 5198 | { | 
|  | 5199 | fprintf (fp, | 
|  | 5200 | "<aspace type=\"total\" size=\"%zu\"/>\n" | 
|  | 5201 | "<aspace type=\"mprotect\" size=\"%zu\"/>\n", | 
|  | 5202 | ar_ptr->system_mem, ar_ptr->system_mem); | 
|  | 5203 | total_aspace += ar_ptr->system_mem; | 
|  | 5204 | total_aspace_mprotect += ar_ptr->system_mem; | 
|  | 5205 | } | 
|  | 5206 |  | 
|  | 5207 | fputs ("</heap>\n", fp); | 
|  | 5208 | ar_ptr = ar_ptr->next; | 
|  | 5209 | } | 
|  | 5210 | while (ar_ptr != &main_arena); | 
|  | 5211 |  | 
|  | 5212 | fprintf (fp, | 
|  | 5213 | "<total type=\"fast\" count=\"%zu\" size=\"%zu\"/>\n" | 
|  | 5214 | "<total type=\"rest\" count=\"%zu\" size=\"%zu\"/>\n" | 
|  | 5215 | "<total type=\"mmap\" count=\"%d\" size=\"%zu\"/>\n" | 
|  | 5216 | "<system type=\"current\" size=\"%zu\"/>\n" | 
|  | 5217 | "<system type=\"max\" size=\"%zu\"/>\n" | 
|  | 5218 | "<aspace type=\"total\" size=\"%zu\"/>\n" | 
|  | 5219 | "<aspace type=\"mprotect\" size=\"%zu\"/>\n" | 
|  | 5220 | "</malloc>\n", | 
|  | 5221 | total_nfastblocks, total_fastavail, total_nblocks, total_avail, | 
|  | 5222 | mp_.n_mmaps, mp_.mmapped_mem, | 
|  | 5223 | total_system, total_max_system, | 
|  | 5224 | total_aspace, total_aspace_mprotect); | 
|  | 5225 |  | 
|  | 5226 | return 0; | 
|  | 5227 | } | 
|  | 5228 | weak_alias (__malloc_info, malloc_info) | 
|  | 5229 |  | 
|  | 5230 |  | 
|  | 5231 | strong_alias (__libc_calloc, __calloc) weak_alias (__libc_calloc, calloc) | 
|  | 5232 | strong_alias (__libc_free, __cfree) weak_alias (__libc_free, cfree) | 
|  | 5233 | strong_alias (__libc_free, __free) strong_alias (__libc_free, free) | 
|  | 5234 | strong_alias (__libc_malloc, __malloc) strong_alias (__libc_malloc, malloc) | 
|  | 5235 | strong_alias (__libc_memalign, __memalign) | 
|  | 5236 | weak_alias (__libc_memalign, memalign) | 
|  | 5237 | strong_alias (__libc_realloc, __realloc) strong_alias (__libc_realloc, realloc) | 
|  | 5238 | strong_alias (__libc_valloc, __valloc) weak_alias (__libc_valloc, valloc) | 
|  | 5239 | strong_alias (__libc_pvalloc, __pvalloc) weak_alias (__libc_pvalloc, pvalloc) | 
|  | 5240 | strong_alias (__libc_mallinfo, __mallinfo) | 
|  | 5241 | weak_alias (__libc_mallinfo, mallinfo) | 
|  | 5242 | strong_alias (__libc_mallopt, __mallopt) weak_alias (__libc_mallopt, mallopt) | 
|  | 5243 |  | 
|  | 5244 | weak_alias (__malloc_stats, malloc_stats) | 
|  | 5245 | weak_alias (__malloc_usable_size, malloc_usable_size) | 
|  | 5246 | weak_alias (__malloc_trim, malloc_trim) | 
|  | 5247 | weak_alias (__malloc_get_state, malloc_get_state) | 
|  | 5248 | weak_alias (__malloc_set_state, malloc_set_state) | 
|  | 5249 |  | 
|  | 5250 |  | 
|  | 5251 | /* ------------------------------------------------------------ | 
|  | 5252 | History: | 
|  | 5253 |  | 
|  | 5254 | [see ftp://g.oswego.edu/pub/misc/malloc.c for the history of dlmalloc] | 
|  | 5255 |  | 
|  | 5256 | */ | 
|  | 5257 | /* | 
|  | 5258 | * Local variables: | 
|  | 5259 | * c-basic-offset: 2 | 
|  | 5260 | * End: | 
|  | 5261 | */ |