Document the M_ARENA_* mallopt parameters
[glibc.git] / malloc / malloc.c
bloba849901fabb0ad48e7b3979b4a20553fe4e47037
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.
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.
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.
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/>. */
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.
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.
29 * Version ptmalloc2-20011215
30 based on:
31 VERSION 2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee)
33 * Quickstart
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".
44 * Why use this malloc?
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.
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.
62 For a longer but slightly out of date high-level description, see
63 http://gee.cs.oswego.edu/dl/html/malloc.html
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.
71 * Contents, described in more detail in "description of public routines" below.
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)
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();
92 * Vital statistics:
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
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.
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.
108 Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead)
109 8-byte ptrs: 24/32 bytes (including, 4/8 overhead)
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.
117 Even a request for zero bytes (i.e., malloc(0)) returns a
118 pointer to something of the minimum allocatable size.
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.
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
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.
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.)
147 Thread-safety: thread-safe
149 Compliance: I believe it is compliant with the 1997 Single Unix Specification
150 Also SVID/XPG, ANSI C, and probably others as well.
152 * Synopsis of compile-time options:
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.
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.)
167 OPTION DEFAULT VALUE
169 Compilation Environment options:
171 HAVE_MREMAP 0
173 Changing default word sizes:
175 INTERNAL_SIZE_T size_t
177 Configuration and functionality options:
179 USE_PUBLIC_MALLOC_WRAPPERS NOT defined
180 USE_MALLOC_LOCK NOT defined
181 MALLOC_DEBUG NOT defined
182 REALLOC_ZERO_BYTES_FREES 1
183 TRIM_FASTBINS 0
185 Options for customizing MORECORE:
187 MORECORE sbrk
188 MORECORE_FAILURE -1
189 MORECORE_CONTIGUOUS 1
190 MORECORE_CANNOT_TRIM NOT defined
191 MORECORE_CLEARS 1
192 MMAP_AS_MORECORE_SIZE (1024 * 1024)
194 Tuning options that are also dynamically changeable via mallopt:
196 DEFAULT_MXFAST 64 (for 32bit), 128 (for 64bit)
197 DEFAULT_TRIM_THRESHOLD 128 * 1024
198 DEFAULT_TOP_PAD 0
199 DEFAULT_MMAP_THRESHOLD 128 * 1024
200 DEFAULT_MMAP_MAX 65536
202 There are several other #defined constants and macros that you
203 probably don't want to touch unless you are extending or adapting malloc. */
206 void* is the pointer type that malloc should say it returns
209 #ifndef void
210 #define void void
211 #endif /*void*/
213 #include <stddef.h> /* for size_t */
214 #include <stdlib.h> /* for getenv(), abort() */
215 #include <unistd.h> /* for __libc_enable_secure */
217 #include <atomic.h>
218 #include <_itoa.h>
219 #include <bits/wordsize.h>
220 #include <sys/sysinfo.h>
222 #include <ldsodefs.h>
224 #include <unistd.h>
225 #include <stdio.h> /* needed for malloc_stats */
226 #include <errno.h>
228 #include <shlib-compat.h>
230 /* For uintptr_t. */
231 #include <stdint.h>
233 /* For va_arg, va_start, va_end. */
234 #include <stdarg.h>
236 /* For MIN, MAX, powerof2. */
237 #include <sys/param.h>
239 /* For ALIGN_UP et. al. */
240 #include <libc-internal.h>
242 #include <malloc/malloc-internal.h>
245 Debugging:
247 Because freed chunks may be overwritten with bookkeeping fields, this
248 malloc will often die when freed memory is overwritten by user
249 programs. This can be very effective (albeit in an annoying way)
250 in helping track down dangling pointers.
252 If you compile with -DMALLOC_DEBUG, a number of assertion checks are
253 enabled that will catch more memory errors. You probably won't be
254 able to make much sense of the actual assertion errors, but they
255 should help you locate incorrectly overwritten memory. The checking
256 is fairly extensive, and will slow down execution
257 noticeably. Calling malloc_stats or mallinfo with MALLOC_DEBUG set
258 will attempt to check every non-mmapped allocated and free chunk in
259 the course of computing the summmaries. (By nature, mmapped regions
260 cannot be checked very much automatically.)
262 Setting MALLOC_DEBUG may also be helpful if you are trying to modify
263 this code. The assertions in the check routines spell out in more
264 detail the assumptions and invariants underlying the algorithms.
266 Setting MALLOC_DEBUG does NOT provide an automated mechanism for
267 checking that all accesses to malloced memory stay within their
268 bounds. However, there are several add-ons and adaptations of this
269 or other mallocs available that do this.
272 #ifndef MALLOC_DEBUG
273 #define MALLOC_DEBUG 0
274 #endif
276 #ifdef NDEBUG
277 # define assert(expr) ((void) 0)
278 #else
279 # define assert(expr) \
280 ((expr) \
281 ? ((void) 0) \
282 : __malloc_assert (#expr, __FILE__, __LINE__, __func__))
284 extern const char *__progname;
286 static void
287 __malloc_assert (const char *assertion, const char *file, unsigned int line,
288 const char *function)
290 (void) __fxprintf (NULL, "%s%s%s:%u: %s%sAssertion `%s' failed.\n",
291 __progname, __progname[0] ? ": " : "",
292 file, line,
293 function ? function : "", function ? ": " : "",
294 assertion);
295 fflush (stderr);
296 abort ();
298 #endif
302 REALLOC_ZERO_BYTES_FREES should be set if a call to
303 realloc with zero bytes should be the same as a call to free.
304 This is required by the C standard. Otherwise, since this malloc
305 returns a unique pointer for malloc(0), so does realloc(p, 0).
308 #ifndef REALLOC_ZERO_BYTES_FREES
309 #define REALLOC_ZERO_BYTES_FREES 1
310 #endif
313 TRIM_FASTBINS controls whether free() of a very small chunk can
314 immediately lead to trimming. Setting to true (1) can reduce memory
315 footprint, but will almost always slow down programs that use a lot
316 of small chunks.
318 Define this only if you are willing to give up some speed to more
319 aggressively reduce system-level memory footprint when releasing
320 memory in programs that use many small chunks. You can get
321 essentially the same effect by setting MXFAST to 0, but this can
322 lead to even greater slowdowns in programs using many small chunks.
323 TRIM_FASTBINS is an in-between compile-time option, that disables
324 only those chunks bordering topmost memory from being placed in
325 fastbins.
328 #ifndef TRIM_FASTBINS
329 #define TRIM_FASTBINS 0
330 #endif
333 /* Definition for getting more memory from the OS. */
334 #define MORECORE (*__morecore)
335 #define MORECORE_FAILURE 0
336 void * __default_morecore (ptrdiff_t);
337 void *(*__morecore)(ptrdiff_t) = __default_morecore;
340 #include <string.h>
343 MORECORE-related declarations. By default, rely on sbrk
348 MORECORE is the name of the routine to call to obtain more memory
349 from the system. See below for general guidance on writing
350 alternative MORECORE functions, as well as a version for WIN32 and a
351 sample version for pre-OSX macos.
354 #ifndef MORECORE
355 #define MORECORE sbrk
356 #endif
359 MORECORE_FAILURE is the value returned upon failure of MORECORE
360 as well as mmap. Since it cannot be an otherwise valid memory address,
361 and must reflect values of standard sys calls, you probably ought not
362 try to redefine it.
365 #ifndef MORECORE_FAILURE
366 #define MORECORE_FAILURE (-1)
367 #endif
370 If MORECORE_CONTIGUOUS is true, take advantage of fact that
371 consecutive calls to MORECORE with positive arguments always return
372 contiguous increasing addresses. This is true of unix sbrk. Even
373 if not defined, when regions happen to be contiguous, malloc will
374 permit allocations spanning regions obtained from different
375 calls. But defining this when applicable enables some stronger
376 consistency checks and space efficiencies.
379 #ifndef MORECORE_CONTIGUOUS
380 #define MORECORE_CONTIGUOUS 1
381 #endif
384 Define MORECORE_CANNOT_TRIM if your version of MORECORE
385 cannot release space back to the system when given negative
386 arguments. This is generally necessary only if you are using
387 a hand-crafted MORECORE function that cannot handle negative arguments.
390 /* #define MORECORE_CANNOT_TRIM */
392 /* MORECORE_CLEARS (default 1)
393 The degree to which the routine mapped to MORECORE zeroes out
394 memory: never (0), only for newly allocated space (1) or always
395 (2). The distinction between (1) and (2) is necessary because on
396 some systems, if the application first decrements and then
397 increments the break value, the contents of the reallocated space
398 are unspecified.
401 #ifndef MORECORE_CLEARS
402 # define MORECORE_CLEARS 1
403 #endif
407 MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if
408 sbrk fails, and mmap is used as a backup. The value must be a
409 multiple of page size. This backup strategy generally applies only
410 when systems have "holes" in address space, so sbrk cannot perform
411 contiguous expansion, but there is still space available on system.
412 On systems for which this is known to be useful (i.e. most linux
413 kernels), this occurs only when programs allocate huge amounts of
414 memory. Between this, and the fact that mmap regions tend to be
415 limited, the size should be large, to avoid too many mmap calls and
416 thus avoid running out of kernel resources. */
418 #ifndef MMAP_AS_MORECORE_SIZE
419 #define MMAP_AS_MORECORE_SIZE (1024 * 1024)
420 #endif
423 Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
424 large blocks.
427 #ifndef HAVE_MREMAP
428 #define HAVE_MREMAP 0
429 #endif
431 /* We may need to support __malloc_initialize_hook for backwards
432 compatibility. */
434 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_24)
435 # define HAVE_MALLOC_INIT_HOOK 1
436 #else
437 # define HAVE_MALLOC_INIT_HOOK 0
438 #endif
442 This version of malloc supports the standard SVID/XPG mallinfo
443 routine that returns a struct containing usage properties and
444 statistics. It should work on any SVID/XPG compliant system that has
445 a /usr/include/malloc.h defining struct mallinfo. (If you'd like to
446 install such a thing yourself, cut out the preliminary declarations
447 as described above and below and save them in a malloc.h file. But
448 there's no compelling reason to bother to do this.)
450 The main declaration needed is the mallinfo struct that is returned
451 (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a
452 bunch of fields that are not even meaningful in this version of
453 malloc. These fields are are instead filled by mallinfo() with
454 other numbers that might be of interest.
458 /* ---------- description of public routines ------------ */
461 malloc(size_t n)
462 Returns a pointer to a newly allocated chunk of at least n bytes, or null
463 if no space is available. Additionally, on failure, errno is
464 set to ENOMEM on ANSI C systems.
466 If n is zero, malloc returns a minumum-sized chunk. (The minimum
467 size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
468 systems.) On most systems, size_t is an unsigned type, so calls
469 with negative arguments are interpreted as requests for huge amounts
470 of space, which will often fail. The maximum supported value of n
471 differs across systems, but is in all cases less than the maximum
472 representable value of a size_t.
474 void* __libc_malloc(size_t);
475 libc_hidden_proto (__libc_malloc)
478 free(void* p)
479 Releases the chunk of memory pointed to by p, that had been previously
480 allocated using malloc or a related routine such as realloc.
481 It has no effect if p is null. It can have arbitrary (i.e., bad!)
482 effects if p has already been freed.
484 Unless disabled (using mallopt), freeing very large spaces will
485 when possible, automatically trigger operations that give
486 back unused memory to the system, thus reducing program footprint.
488 void __libc_free(void*);
489 libc_hidden_proto (__libc_free)
492 calloc(size_t n_elements, size_t element_size);
493 Returns a pointer to n_elements * element_size bytes, with all locations
494 set to zero.
496 void* __libc_calloc(size_t, size_t);
499 realloc(void* p, size_t n)
500 Returns a pointer to a chunk of size n that contains the same data
501 as does chunk p up to the minimum of (n, p's size) bytes, or null
502 if no space is available.
504 The returned pointer may or may not be the same as p. The algorithm
505 prefers extending p when possible, otherwise it employs the
506 equivalent of a malloc-copy-free sequence.
508 If p is null, realloc is equivalent to malloc.
510 If space is not available, realloc returns null, errno is set (if on
511 ANSI) and p is NOT freed.
513 if n is for fewer bytes than already held by p, the newly unused
514 space is lopped off and freed if possible. Unless the #define
515 REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of
516 zero (re)allocates a minimum-sized chunk.
518 Large chunks that were internally obtained via mmap will always
519 be reallocated using malloc-copy-free sequences unless
520 the system supports MREMAP (currently only linux).
522 The old unix realloc convention of allowing the last-free'd chunk
523 to be used as an argument to realloc is not supported.
525 void* __libc_realloc(void*, size_t);
526 libc_hidden_proto (__libc_realloc)
529 memalign(size_t alignment, size_t n);
530 Returns a pointer to a newly allocated chunk of n bytes, aligned
531 in accord with the alignment argument.
533 The alignment argument should be a power of two. If the argument is
534 not a power of two, the nearest greater power is used.
535 8-byte alignment is guaranteed by normal malloc calls, so don't
536 bother calling memalign with an argument of 8 or less.
538 Overreliance on memalign is a sure way to fragment space.
540 void* __libc_memalign(size_t, size_t);
541 libc_hidden_proto (__libc_memalign)
544 valloc(size_t n);
545 Equivalent to memalign(pagesize, n), where pagesize is the page
546 size of the system. If the pagesize is unknown, 4096 is used.
548 void* __libc_valloc(size_t);
553 mallopt(int parameter_number, int parameter_value)
554 Sets tunable parameters The format is to provide a
555 (parameter-number, parameter-value) pair. mallopt then sets the
556 corresponding parameter to the argument value if it can (i.e., so
557 long as the value is meaningful), and returns 1 if successful else
558 0. SVID/XPG/ANSI defines four standard param numbers for mallopt,
559 normally defined in malloc.h. Only one of these (M_MXFAST) is used
560 in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
561 so setting them has no effect. But this malloc also supports four
562 other options in mallopt. See below for details. Briefly, supported
563 parameters are as follows (listed defaults are for "typical"
564 configurations).
566 Symbol param # default allowed param values
567 M_MXFAST 1 64 0-80 (0 disables fastbins)
568 M_TRIM_THRESHOLD -1 128*1024 any (-1U disables trimming)
569 M_TOP_PAD -2 0 any
570 M_MMAP_THRESHOLD -3 128*1024 any (or 0 if no MMAP support)
571 M_MMAP_MAX -4 65536 any (0 disables use of mmap)
573 int __libc_mallopt(int, int);
574 libc_hidden_proto (__libc_mallopt)
578 mallinfo()
579 Returns (by copy) a struct containing various summary statistics:
581 arena: current total non-mmapped bytes allocated from system
582 ordblks: the number of free chunks
583 smblks: the number of fastbin blocks (i.e., small chunks that
584 have been freed but not use resused or consolidated)
585 hblks: current number of mmapped regions
586 hblkhd: total bytes held in mmapped regions
587 usmblks: always 0
588 fsmblks: total bytes held in fastbin blocks
589 uordblks: current total allocated space (normal or mmapped)
590 fordblks: total free space
591 keepcost: the maximum number of bytes that could ideally be released
592 back to system via malloc_trim. ("ideally" means that
593 it ignores page restrictions etc.)
595 Because these fields are ints, but internal bookkeeping may
596 be kept as longs, the reported values may wrap around zero and
597 thus be inaccurate.
599 struct mallinfo __libc_mallinfo(void);
603 pvalloc(size_t n);
604 Equivalent to valloc(minimum-page-that-holds(n)), that is,
605 round up n to nearest pagesize.
607 void* __libc_pvalloc(size_t);
610 malloc_trim(size_t pad);
612 If possible, gives memory back to the system (via negative
613 arguments to sbrk) if there is unused memory at the `high' end of
614 the malloc pool. You can call this after freeing large blocks of
615 memory to potentially reduce the system-level memory requirements
616 of a program. However, it cannot guarantee to reduce memory. Under
617 some allocation patterns, some large free blocks of memory will be
618 locked between two used chunks, so they cannot be given back to
619 the system.
621 The `pad' argument to malloc_trim represents the amount of free
622 trailing space to leave untrimmed. If this argument is zero,
623 only the minimum amount of memory to maintain internal data
624 structures will be left (one page or less). Non-zero arguments
625 can be supplied to maintain enough trailing space to service
626 future expected allocations without having to re-obtain memory
627 from the system.
629 Malloc_trim returns 1 if it actually released any memory, else 0.
630 On systems that do not support "negative sbrks", it will always
631 return 0.
633 int __malloc_trim(size_t);
636 malloc_usable_size(void* p);
638 Returns the number of bytes you can actually use in
639 an allocated chunk, which may be more than you requested (although
640 often not) due to alignment and minimum size constraints.
641 You can use this many bytes without worrying about
642 overwriting other allocated objects. This is not a particularly great
643 programming practice. malloc_usable_size can be more useful in
644 debugging and assertions, for example:
646 p = malloc(n);
647 assert(malloc_usable_size(p) >= 256);
650 size_t __malloc_usable_size(void*);
653 malloc_stats();
654 Prints on stderr the amount of space obtained from the system (both
655 via sbrk and mmap), the maximum amount (which may be more than
656 current if malloc_trim and/or munmap got called), and the current
657 number of bytes allocated via malloc (or realloc, etc) but not yet
658 freed. Note that this is the number of bytes allocated, not the
659 number requested. It will be larger than the number requested
660 because of alignment and bookkeeping overhead. Because it includes
661 alignment wastage as being in use, this figure may be greater than
662 zero even when no user-level chunks are allocated.
664 The reported current and maximum system memory can be inaccurate if
665 a program makes other calls to system memory allocation functions
666 (normally sbrk) outside of malloc.
668 malloc_stats prints only the most commonly interesting statistics.
669 More information can be obtained by calling mallinfo.
672 void __malloc_stats(void);
675 malloc_get_state(void);
677 Returns the state of all malloc variables in an opaque data
678 structure.
680 void* __malloc_get_state(void);
683 malloc_set_state(void* state);
685 Restore the state of all malloc variables from data obtained with
686 malloc_get_state().
688 int __malloc_set_state(void*);
691 posix_memalign(void **memptr, size_t alignment, size_t size);
693 POSIX wrapper like memalign(), checking for validity of size.
695 int __posix_memalign(void **, size_t, size_t);
697 /* mallopt tuning options */
700 M_MXFAST is the maximum request size used for "fastbins", special bins
701 that hold returned chunks without consolidating their spaces. This
702 enables future requests for chunks of the same size to be handled
703 very quickly, but can increase fragmentation, and thus increase the
704 overall memory footprint of a program.
706 This malloc manages fastbins very conservatively yet still
707 efficiently, so fragmentation is rarely a problem for values less
708 than or equal to the default. The maximum supported value of MXFAST
709 is 80. You wouldn't want it any higher than this anyway. Fastbins
710 are designed especially for use with many small structs, objects or
711 strings -- the default handles structs/objects/arrays with sizes up
712 to 8 4byte fields, or small strings representing words, tokens,
713 etc. Using fastbins for larger objects normally worsens
714 fragmentation without improving speed.
716 M_MXFAST is set in REQUEST size units. It is internally used in
717 chunksize units, which adds padding and alignment. You can reduce
718 M_MXFAST to 0 to disable all use of fastbins. This causes the malloc
719 algorithm to be a closer approximation of fifo-best-fit in all cases,
720 not just for larger requests, but will generally cause it to be
721 slower.
725 /* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */
726 #ifndef M_MXFAST
727 #define M_MXFAST 1
728 #endif
730 #ifndef DEFAULT_MXFAST
731 #define DEFAULT_MXFAST (64 * SIZE_SZ / 4)
732 #endif
736 M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
737 to keep before releasing via malloc_trim in free().
739 Automatic trimming is mainly useful in long-lived programs.
740 Because trimming via sbrk can be slow on some systems, and can
741 sometimes be wasteful (in cases where programs immediately
742 afterward allocate more large chunks) the value should be high
743 enough so that your overall system performance would improve by
744 releasing this much memory.
746 The trim threshold and the mmap control parameters (see below)
747 can be traded off with one another. Trimming and mmapping are
748 two different ways of releasing unused memory back to the
749 system. Between these two, it is often possible to keep
750 system-level demands of a long-lived program down to a bare
751 minimum. For example, in one test suite of sessions measuring
752 the XF86 X server on Linux, using a trim threshold of 128K and a
753 mmap threshold of 192K led to near-minimal long term resource
754 consumption.
756 If you are using this malloc in a long-lived program, it should
757 pay to experiment with these values. As a rough guide, you
758 might set to a value close to the average size of a process
759 (program) running on your system. Releasing this much memory
760 would allow such a process to run in memory. Generally, it's
761 worth it to tune for trimming rather tham memory mapping when a
762 program undergoes phases where several large chunks are
763 allocated and released in ways that can reuse each other's
764 storage, perhaps mixed with phases where there are no such
765 chunks at all. And in well-behaved long-lived programs,
766 controlling release of large blocks via trimming versus mapping
767 is usually faster.
769 However, in most programs, these parameters serve mainly as
770 protection against the system-level effects of carrying around
771 massive amounts of unneeded memory. Since frequent calls to
772 sbrk, mmap, and munmap otherwise degrade performance, the default
773 parameters are set to relatively high values that serve only as
774 safeguards.
776 The trim value It must be greater than page size to have any useful
777 effect. To disable trimming completely, you can set to
778 (unsigned long)(-1)
780 Trim settings interact with fastbin (MXFAST) settings: Unless
781 TRIM_FASTBINS is defined, automatic trimming never takes place upon
782 freeing a chunk with size less than or equal to MXFAST. Trimming is
783 instead delayed until subsequent freeing of larger chunks. However,
784 you can still force an attempted trim by calling malloc_trim.
786 Also, trimming is not generally possible in cases where
787 the main arena is obtained via mmap.
789 Note that the trick some people use of mallocing a huge space and
790 then freeing it at program startup, in an attempt to reserve system
791 memory, doesn't have the intended effect under automatic trimming,
792 since that memory will immediately be returned to the system.
795 #define M_TRIM_THRESHOLD -1
797 #ifndef DEFAULT_TRIM_THRESHOLD
798 #define DEFAULT_TRIM_THRESHOLD (128 * 1024)
799 #endif
802 M_TOP_PAD is the amount of extra `padding' space to allocate or
803 retain whenever sbrk is called. It is used in two ways internally:
805 * When sbrk is called to extend the top of the arena to satisfy
806 a new malloc request, this much padding is added to the sbrk
807 request.
809 * When malloc_trim is called automatically from free(),
810 it is used as the `pad' argument.
812 In both cases, the actual amount of padding is rounded
813 so that the end of the arena is always a system page boundary.
815 The main reason for using padding is to avoid calling sbrk so
816 often. Having even a small pad greatly reduces the likelihood
817 that nearly every malloc request during program start-up (or
818 after trimming) will invoke sbrk, which needlessly wastes
819 time.
821 Automatic rounding-up to page-size units is normally sufficient
822 to avoid measurable overhead, so the default is 0. However, in
823 systems where sbrk is relatively slow, it can pay to increase
824 this value, at the expense of carrying around more memory than
825 the program needs.
828 #define M_TOP_PAD -2
830 #ifndef DEFAULT_TOP_PAD
831 #define DEFAULT_TOP_PAD (0)
832 #endif
835 MMAP_THRESHOLD_MAX and _MIN are the bounds on the dynamically
836 adjusted MMAP_THRESHOLD.
839 #ifndef DEFAULT_MMAP_THRESHOLD_MIN
840 #define DEFAULT_MMAP_THRESHOLD_MIN (128 * 1024)
841 #endif
843 #ifndef DEFAULT_MMAP_THRESHOLD_MAX
844 /* For 32-bit platforms we cannot increase the maximum mmap
845 threshold much because it is also the minimum value for the
846 maximum heap size and its alignment. Going above 512k (i.e., 1M
847 for new heaps) wastes too much address space. */
848 # if __WORDSIZE == 32
849 # define DEFAULT_MMAP_THRESHOLD_MAX (512 * 1024)
850 # else
851 # define DEFAULT_MMAP_THRESHOLD_MAX (4 * 1024 * 1024 * sizeof(long))
852 # endif
853 #endif
856 M_MMAP_THRESHOLD is the request size threshold for using mmap()
857 to service a request. Requests of at least this size that cannot
858 be allocated using already-existing space will be serviced via mmap.
859 (If enough normal freed space already exists it is used instead.)
861 Using mmap segregates relatively large chunks of memory so that
862 they can be individually obtained and released from the host
863 system. A request serviced through mmap is never reused by any
864 other request (at least not directly; the system may just so
865 happen to remap successive requests to the same locations).
867 Segregating space in this way has the benefits that:
869 1. Mmapped space can ALWAYS be individually released back
870 to the system, which helps keep the system level memory
871 demands of a long-lived program low.
872 2. Mapped memory can never become `locked' between
873 other chunks, as can happen with normally allocated chunks, which
874 means that even trimming via malloc_trim would not release them.
875 3. On some systems with "holes" in address spaces, mmap can obtain
876 memory that sbrk cannot.
878 However, it has the disadvantages that:
880 1. The space cannot be reclaimed, consolidated, and then
881 used to service later requests, as happens with normal chunks.
882 2. It can lead to more wastage because of mmap page alignment
883 requirements
884 3. It causes malloc performance to be more dependent on host
885 system memory management support routines which may vary in
886 implementation quality and may impose arbitrary
887 limitations. Generally, servicing a request via normal
888 malloc steps is faster than going through a system's mmap.
890 The advantages of mmap nearly always outweigh disadvantages for
891 "large" chunks, but the value of "large" varies across systems. The
892 default is an empirically derived value that works well in most
893 systems.
896 Update in 2006:
897 The above was written in 2001. Since then the world has changed a lot.
898 Memory got bigger. Applications got bigger. The virtual address space
899 layout in 32 bit linux changed.
901 In the new situation, brk() and mmap space is shared and there are no
902 artificial limits on brk size imposed by the kernel. What is more,
903 applications have started using transient allocations larger than the
904 128Kb as was imagined in 2001.
906 The price for mmap is also high now; each time glibc mmaps from the
907 kernel, the kernel is forced to zero out the memory it gives to the
908 application. Zeroing memory is expensive and eats a lot of cache and
909 memory bandwidth. This has nothing to do with the efficiency of the
910 virtual memory system, by doing mmap the kernel just has no choice but
911 to zero.
913 In 2001, the kernel had a maximum size for brk() which was about 800
914 megabytes on 32 bit x86, at that point brk() would hit the first
915 mmaped shared libaries and couldn't expand anymore. With current 2.6
916 kernels, the VA space layout is different and brk() and mmap
917 both can span the entire heap at will.
919 Rather than using a static threshold for the brk/mmap tradeoff,
920 we are now using a simple dynamic one. The goal is still to avoid
921 fragmentation. The old goals we kept are
922 1) try to get the long lived large allocations to use mmap()
923 2) really large allocations should always use mmap()
924 and we're adding now:
925 3) transient allocations should use brk() to avoid forcing the kernel
926 having to zero memory over and over again
928 The implementation works with a sliding threshold, which is by default
929 limited to go between 128Kb and 32Mb (64Mb for 64 bitmachines) and starts
930 out at 128Kb as per the 2001 default.
932 This allows us to satisfy requirement 1) under the assumption that long
933 lived allocations are made early in the process' lifespan, before it has
934 started doing dynamic allocations of the same size (which will
935 increase the threshold).
937 The upperbound on the threshold satisfies requirement 2)
939 The threshold goes up in value when the application frees memory that was
940 allocated with the mmap allocator. The idea is that once the application
941 starts freeing memory of a certain size, it's highly probable that this is
942 a size the application uses for transient allocations. This estimator
943 is there to satisfy the new third requirement.
947 #define M_MMAP_THRESHOLD -3
949 #ifndef DEFAULT_MMAP_THRESHOLD
950 #define DEFAULT_MMAP_THRESHOLD DEFAULT_MMAP_THRESHOLD_MIN
951 #endif
954 M_MMAP_MAX is the maximum number of requests to simultaneously
955 service using mmap. This parameter exists because
956 some systems have a limited number of internal tables for
957 use by mmap, and using more than a few of them may degrade
958 performance.
960 The default is set to a value that serves only as a safeguard.
961 Setting to 0 disables use of mmap for servicing large requests.
964 #define M_MMAP_MAX -4
966 #ifndef DEFAULT_MMAP_MAX
967 #define DEFAULT_MMAP_MAX (65536)
968 #endif
970 #include <malloc.h>
972 #ifndef RETURN_ADDRESS
973 #define RETURN_ADDRESS(X_) (NULL)
974 #endif
976 /* On some platforms we can compile internal, not exported functions better.
977 Let the environment provide a macro and define it to be empty if it
978 is not available. */
979 #ifndef internal_function
980 # define internal_function
981 #endif
983 /* Forward declarations. */
984 struct malloc_chunk;
985 typedef struct malloc_chunk* mchunkptr;
987 /* Internal routines. */
989 static void* _int_malloc(mstate, size_t);
990 static void _int_free(mstate, mchunkptr, int);
991 static void* _int_realloc(mstate, mchunkptr, INTERNAL_SIZE_T,
992 INTERNAL_SIZE_T);
993 static void* _int_memalign(mstate, size_t, size_t);
994 static void* _mid_memalign(size_t, size_t, void *);
996 static void malloc_printerr(int action, const char *str, void *ptr, mstate av);
998 static void* internal_function mem2mem_check(void *p, size_t sz);
999 static int internal_function top_check(void);
1000 static void internal_function munmap_chunk(mchunkptr p);
1001 #if HAVE_MREMAP
1002 static mchunkptr internal_function mremap_chunk(mchunkptr p, size_t new_size);
1003 #endif
1005 static void* malloc_check(size_t sz, const void *caller);
1006 static void free_check(void* mem, const void *caller);
1007 static void* realloc_check(void* oldmem, size_t bytes,
1008 const void *caller);
1009 static void* memalign_check(size_t alignment, size_t bytes,
1010 const void *caller);
1012 /* ------------------ MMAP support ------------------ */
1015 #include <fcntl.h>
1016 #include <sys/mman.h>
1018 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
1019 # define MAP_ANONYMOUS MAP_ANON
1020 #endif
1022 #ifndef MAP_NORESERVE
1023 # define MAP_NORESERVE 0
1024 #endif
1026 #define MMAP(addr, size, prot, flags) \
1027 __mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0)
1031 ----------------------- Chunk representations -----------------------
1036 This struct declaration is misleading (but accurate and necessary).
1037 It declares a "view" into memory allowing access to necessary
1038 fields at known offsets from a given base. See explanation below.
1041 struct malloc_chunk {
1043 INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
1044 INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
1046 struct malloc_chunk* fd; /* double links -- used only if free. */
1047 struct malloc_chunk* bk;
1049 /* Only used for large blocks: pointer to next larger size. */
1050 struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */
1051 struct malloc_chunk* bk_nextsize;
1056 malloc_chunk details:
1058 (The following includes lightly edited explanations by Colin Plumb.)
1060 Chunks of memory are maintained using a `boundary tag' method as
1061 described in e.g., Knuth or Standish. (See the paper by Paul
1062 Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
1063 survey of such techniques.) Sizes of free chunks are stored both
1064 in the front of each chunk and at the end. This makes
1065 consolidating fragmented chunks into bigger chunks very fast. The
1066 size fields also hold bits representing whether chunks are free or
1067 in use.
1069 An allocated chunk looks like this:
1072 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1073 | Size of previous chunk, if allocated | |
1074 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1075 | Size of chunk, in bytes |M|P|
1076 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1077 | User data starts here... .
1079 . (malloc_usable_size() bytes) .
1081 nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1082 | Size of chunk |
1083 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1086 Where "chunk" is the front of the chunk for the purpose of most of
1087 the malloc code, but "mem" is the pointer that is returned to the
1088 user. "Nextchunk" is the beginning of the next contiguous chunk.
1090 Chunks always begin on even word boundaries, so the mem portion
1091 (which is returned to the user) is also on an even word boundary, and
1092 thus at least double-word aligned.
1094 Free chunks are stored in circular doubly-linked lists, and look like this:
1096 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1097 | Size of previous chunk |
1098 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1099 `head:' | Size of chunk, in bytes |P|
1100 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1101 | Forward pointer to next chunk in list |
1102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1103 | Back pointer to previous chunk in list |
1104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1105 | Unused space (may be 0 bytes long) .
1108 nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1109 `foot:' | Size of chunk, in bytes |
1110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1112 The P (PREV_INUSE) bit, stored in the unused low-order bit of the
1113 chunk size (which is always a multiple of two words), is an in-use
1114 bit for the *previous* chunk. If that bit is *clear*, then the
1115 word before the current chunk size contains the previous chunk
1116 size, and can be used to find the front of the previous chunk.
1117 The very first chunk allocated always has this bit set,
1118 preventing access to non-existent (or non-owned) memory. If
1119 prev_inuse is set for any given chunk, then you CANNOT determine
1120 the size of the previous chunk, and might even get a memory
1121 addressing fault when trying to do so.
1123 Note that the `foot' of the current chunk is actually represented
1124 as the prev_size of the NEXT chunk. This makes it easier to
1125 deal with alignments etc but can be very confusing when trying
1126 to extend or adapt this code.
1128 The two exceptions to all this are
1130 1. The special chunk `top' doesn't bother using the
1131 trailing size field since there is no next contiguous chunk
1132 that would have to index off it. After initialization, `top'
1133 is forced to always exist. If it would become less than
1134 MINSIZE bytes long, it is replenished.
1136 2. Chunks allocated via mmap, which have the second-lowest-order
1137 bit M (IS_MMAPPED) set in their size fields. Because they are
1138 allocated one-by-one, each must contain its own trailing size field.
1143 ---------- Size and alignment checks and conversions ----------
1146 /* conversion from malloc headers to user pointers, and back */
1148 #define chunk2mem(p) ((void*)((char*)(p) + 2*SIZE_SZ))
1149 #define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
1151 /* The smallest possible chunk */
1152 #define MIN_CHUNK_SIZE (offsetof(struct malloc_chunk, fd_nextsize))
1154 /* The smallest size we can malloc is an aligned minimal chunk */
1156 #define MINSIZE \
1157 (unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
1159 /* Check if m has acceptable alignment */
1161 #define aligned_OK(m) (((unsigned long)(m) & MALLOC_ALIGN_MASK) == 0)
1163 #define misaligned_chunk(p) \
1164 ((uintptr_t)(MALLOC_ALIGNMENT == 2 * SIZE_SZ ? (p) : chunk2mem (p)) \
1165 & MALLOC_ALIGN_MASK)
1169 Check if a request is so large that it would wrap around zero when
1170 padded and aligned. To simplify some other code, the bound is made
1171 low enough so that adding MINSIZE will also not wrap around zero.
1174 #define REQUEST_OUT_OF_RANGE(req) \
1175 ((unsigned long) (req) >= \
1176 (unsigned long) (INTERNAL_SIZE_T) (-2 * MINSIZE))
1178 /* pad request bytes into a usable size -- internal version */
1180 #define request2size(req) \
1181 (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE) ? \
1182 MINSIZE : \
1183 ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
1185 /* Same, except also perform argument check */
1187 #define checked_request2size(req, sz) \
1188 if (REQUEST_OUT_OF_RANGE (req)) { \
1189 __set_errno (ENOMEM); \
1190 return 0; \
1192 (sz) = request2size (req);
1195 --------------- Physical chunk operations ---------------
1199 /* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
1200 #define PREV_INUSE 0x1
1202 /* extract inuse bit of previous chunk */
1203 #define prev_inuse(p) ((p)->size & PREV_INUSE)
1206 /* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
1207 #define IS_MMAPPED 0x2
1209 /* check for mmap()'ed chunk */
1210 #define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
1213 /* size field is or'ed with NON_MAIN_ARENA if the chunk was obtained
1214 from a non-main arena. This is only set immediately before handing
1215 the chunk to the user, if necessary. */
1216 #define NON_MAIN_ARENA 0x4
1218 /* check for chunk from non-main arena */
1219 #define chunk_non_main_arena(p) ((p)->size & NON_MAIN_ARENA)
1223 Bits to mask off when extracting size
1225 Note: IS_MMAPPED is intentionally not masked off from size field in
1226 macros for which mmapped chunks should never be seen. This should
1227 cause helpful core dumps to occur if it is tried by accident by
1228 people extending or adapting this malloc.
1230 #define SIZE_BITS (PREV_INUSE | IS_MMAPPED | NON_MAIN_ARENA)
1232 /* Get size, ignoring use bits */
1233 #define chunksize(p) ((p)->size & ~(SIZE_BITS))
1236 /* Ptr to next physical malloc_chunk. */
1237 #define next_chunk(p) ((mchunkptr) (((char *) (p)) + ((p)->size & ~SIZE_BITS)))
1239 /* Ptr to previous physical malloc_chunk */
1240 #define prev_chunk(p) ((mchunkptr) (((char *) (p)) - ((p)->prev_size)))
1242 /* Treat space at ptr + offset as a chunk */
1243 #define chunk_at_offset(p, s) ((mchunkptr) (((char *) (p)) + (s)))
1245 /* extract p's inuse bit */
1246 #define inuse(p) \
1247 ((((mchunkptr) (((char *) (p)) + ((p)->size & ~SIZE_BITS)))->size) & PREV_INUSE)
1249 /* set/clear chunk as being inuse without otherwise disturbing */
1250 #define set_inuse(p) \
1251 ((mchunkptr) (((char *) (p)) + ((p)->size & ~SIZE_BITS)))->size |= PREV_INUSE
1253 #define clear_inuse(p) \
1254 ((mchunkptr) (((char *) (p)) + ((p)->size & ~SIZE_BITS)))->size &= ~(PREV_INUSE)
1257 /* check/set/clear inuse bits in known places */
1258 #define inuse_bit_at_offset(p, s) \
1259 (((mchunkptr) (((char *) (p)) + (s)))->size & PREV_INUSE)
1261 #define set_inuse_bit_at_offset(p, s) \
1262 (((mchunkptr) (((char *) (p)) + (s)))->size |= PREV_INUSE)
1264 #define clear_inuse_bit_at_offset(p, s) \
1265 (((mchunkptr) (((char *) (p)) + (s)))->size &= ~(PREV_INUSE))
1268 /* Set size at head, without disturbing its use bit */
1269 #define set_head_size(p, s) ((p)->size = (((p)->size & SIZE_BITS) | (s)))
1271 /* Set size/use field */
1272 #define set_head(p, s) ((p)->size = (s))
1274 /* Set size at footer (only when chunk is not in use) */
1275 #define set_foot(p, s) (((mchunkptr) ((char *) (p) + (s)))->prev_size = (s))
1279 -------------------- Internal data structures --------------------
1281 All internal state is held in an instance of malloc_state defined
1282 below. There are no other static variables, except in two optional
1283 cases:
1284 * If USE_MALLOC_LOCK is defined, the mALLOC_MUTEx declared above.
1285 * If mmap doesn't support MAP_ANONYMOUS, a dummy file descriptor
1286 for mmap.
1288 Beware of lots of tricks that minimize the total bookkeeping space
1289 requirements. The result is a little over 1K bytes (for 4byte
1290 pointers and size_t.)
1294 Bins
1296 An array of bin headers for free chunks. Each bin is doubly
1297 linked. The bins are approximately proportionally (log) spaced.
1298 There are a lot of these bins (128). This may look excessive, but
1299 works very well in practice. Most bins hold sizes that are
1300 unusual as malloc request sizes, but are more usual for fragments
1301 and consolidated sets of chunks, which is what these bins hold, so
1302 they can be found quickly. All procedures maintain the invariant
1303 that no consolidated chunk physically borders another one, so each
1304 chunk in a list is known to be preceeded and followed by either
1305 inuse chunks or the ends of memory.
1307 Chunks in bins are kept in size order, with ties going to the
1308 approximately least recently used chunk. Ordering isn't needed
1309 for the small bins, which all contain the same-sized chunks, but
1310 facilitates best-fit allocation for larger chunks. These lists
1311 are just sequential. Keeping them in order almost never requires
1312 enough traversal to warrant using fancier ordered data
1313 structures.
1315 Chunks of the same size are linked with the most
1316 recently freed at the front, and allocations are taken from the
1317 back. This results in LRU (FIFO) allocation order, which tends
1318 to give each chunk an equal opportunity to be consolidated with
1319 adjacent freed chunks, resulting in larger free chunks and less
1320 fragmentation.
1322 To simplify use in double-linked lists, each bin header acts
1323 as a malloc_chunk. This avoids special-casing for headers.
1324 But to conserve space and improve locality, we allocate
1325 only the fd/bk pointers of bins, and then use repositioning tricks
1326 to treat these as the fields of a malloc_chunk*.
1329 typedef struct malloc_chunk *mbinptr;
1331 /* addressing -- note that bin_at(0) does not exist */
1332 #define bin_at(m, i) \
1333 (mbinptr) (((char *) &((m)->bins[((i) - 1) * 2])) \
1334 - offsetof (struct malloc_chunk, fd))
1336 /* analog of ++bin */
1337 #define next_bin(b) ((mbinptr) ((char *) (b) + (sizeof (mchunkptr) << 1)))
1339 /* Reminders about list directionality within bins */
1340 #define first(b) ((b)->fd)
1341 #define last(b) ((b)->bk)
1343 /* Take a chunk off a bin list */
1344 #define unlink(AV, P, BK, FD) { \
1345 FD = P->fd; \
1346 BK = P->bk; \
1347 if (__builtin_expect (FD->bk != P || BK->fd != P, 0)) \
1348 malloc_printerr (check_action, "corrupted double-linked list", P, AV); \
1349 else { \
1350 FD->bk = BK; \
1351 BK->fd = FD; \
1352 if (!in_smallbin_range (P->size) \
1353 && __builtin_expect (P->fd_nextsize != NULL, 0)) { \
1354 if (__builtin_expect (P->fd_nextsize->bk_nextsize != P, 0) \
1355 || __builtin_expect (P->bk_nextsize->fd_nextsize != P, 0)) \
1356 malloc_printerr (check_action, \
1357 "corrupted double-linked list (not small)", \
1358 P, AV); \
1359 if (FD->fd_nextsize == NULL) { \
1360 if (P->fd_nextsize == P) \
1361 FD->fd_nextsize = FD->bk_nextsize = FD; \
1362 else { \
1363 FD->fd_nextsize = P->fd_nextsize; \
1364 FD->bk_nextsize = P->bk_nextsize; \
1365 P->fd_nextsize->bk_nextsize = FD; \
1366 P->bk_nextsize->fd_nextsize = FD; \
1368 } else { \
1369 P->fd_nextsize->bk_nextsize = P->bk_nextsize; \
1370 P->bk_nextsize->fd_nextsize = P->fd_nextsize; \
1377 Indexing
1379 Bins for sizes < 512 bytes contain chunks of all the same size, spaced
1380 8 bytes apart. Larger bins are approximately logarithmically spaced:
1382 64 bins of size 8
1383 32 bins of size 64
1384 16 bins of size 512
1385 8 bins of size 4096
1386 4 bins of size 32768
1387 2 bins of size 262144
1388 1 bin of size what's left
1390 There is actually a little bit of slop in the numbers in bin_index
1391 for the sake of speed. This makes no difference elsewhere.
1393 The bins top out around 1MB because we expect to service large
1394 requests via mmap.
1396 Bin 0 does not exist. Bin 1 is the unordered list; if that would be
1397 a valid chunk size the small bins are bumped up one.
1400 #define NBINS 128
1401 #define NSMALLBINS 64
1402 #define SMALLBIN_WIDTH MALLOC_ALIGNMENT
1403 #define SMALLBIN_CORRECTION (MALLOC_ALIGNMENT > 2 * SIZE_SZ)
1404 #define MIN_LARGE_SIZE ((NSMALLBINS - SMALLBIN_CORRECTION) * SMALLBIN_WIDTH)
1406 #define in_smallbin_range(sz) \
1407 ((unsigned long) (sz) < (unsigned long) MIN_LARGE_SIZE)
1409 #define smallbin_index(sz) \
1410 ((SMALLBIN_WIDTH == 16 ? (((unsigned) (sz)) >> 4) : (((unsigned) (sz)) >> 3))\
1411 + SMALLBIN_CORRECTION)
1413 #define largebin_index_32(sz) \
1414 (((((unsigned long) (sz)) >> 6) <= 38) ? 56 + (((unsigned long) (sz)) >> 6) :\
1415 ((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\
1416 ((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\
1417 ((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\
1418 ((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\
1419 126)
1421 #define largebin_index_32_big(sz) \
1422 (((((unsigned long) (sz)) >> 6) <= 45) ? 49 + (((unsigned long) (sz)) >> 6) :\
1423 ((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\
1424 ((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\
1425 ((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\
1426 ((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\
1427 126)
1429 // XXX It remains to be seen whether it is good to keep the widths of
1430 // XXX the buckets the same or whether it should be scaled by a factor
1431 // XXX of two as well.
1432 #define largebin_index_64(sz) \
1433 (((((unsigned long) (sz)) >> 6) <= 48) ? 48 + (((unsigned long) (sz)) >> 6) :\
1434 ((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\
1435 ((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\
1436 ((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\
1437 ((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\
1438 126)
1440 #define largebin_index(sz) \
1441 (SIZE_SZ == 8 ? largebin_index_64 (sz) \
1442 : MALLOC_ALIGNMENT == 16 ? largebin_index_32_big (sz) \
1443 : largebin_index_32 (sz))
1445 #define bin_index(sz) \
1446 ((in_smallbin_range (sz)) ? smallbin_index (sz) : largebin_index (sz))
1450 Unsorted chunks
1452 All remainders from chunk splits, as well as all returned chunks,
1453 are first placed in the "unsorted" bin. They are then placed
1454 in regular bins after malloc gives them ONE chance to be used before
1455 binning. So, basically, the unsorted_chunks list acts as a queue,
1456 with chunks being placed on it in free (and malloc_consolidate),
1457 and taken off (to be either used or placed in bins) in malloc.
1459 The NON_MAIN_ARENA flag is never set for unsorted chunks, so it
1460 does not have to be taken into account in size comparisons.
1463 /* The otherwise unindexable 1-bin is used to hold unsorted chunks. */
1464 #define unsorted_chunks(M) (bin_at (M, 1))
1469 The top-most available chunk (i.e., the one bordering the end of
1470 available memory) is treated specially. It is never included in
1471 any bin, is used only if no other chunk is available, and is
1472 released back to the system if it is very large (see
1473 M_TRIM_THRESHOLD). Because top initially
1474 points to its own bin with initial zero size, thus forcing
1475 extension on the first malloc request, we avoid having any special
1476 code in malloc to check whether it even exists yet. But we still
1477 need to do so when getting memory from system, so we make
1478 initial_top treat the bin as a legal but unusable chunk during the
1479 interval between initialization and the first call to
1480 sysmalloc. (This is somewhat delicate, since it relies on
1481 the 2 preceding words to be zero during this interval as well.)
1484 /* Conveniently, the unsorted bin can be used as dummy top on first call */
1485 #define initial_top(M) (unsorted_chunks (M))
1488 Binmap
1490 To help compensate for the large number of bins, a one-level index
1491 structure is used for bin-by-bin searching. `binmap' is a
1492 bitvector recording whether bins are definitely empty so they can
1493 be skipped over during during traversals. The bits are NOT always
1494 cleared as soon as bins are empty, but instead only
1495 when they are noticed to be empty during traversal in malloc.
1498 /* Conservatively use 32 bits per map word, even if on 64bit system */
1499 #define BINMAPSHIFT 5
1500 #define BITSPERMAP (1U << BINMAPSHIFT)
1501 #define BINMAPSIZE (NBINS / BITSPERMAP)
1503 #define idx2block(i) ((i) >> BINMAPSHIFT)
1504 #define idx2bit(i) ((1U << ((i) & ((1U << BINMAPSHIFT) - 1))))
1506 #define mark_bin(m, i) ((m)->binmap[idx2block (i)] |= idx2bit (i))
1507 #define unmark_bin(m, i) ((m)->binmap[idx2block (i)] &= ~(idx2bit (i)))
1508 #define get_binmap(m, i) ((m)->binmap[idx2block (i)] & idx2bit (i))
1511 Fastbins
1513 An array of lists holding recently freed small chunks. Fastbins
1514 are not doubly linked. It is faster to single-link them, and
1515 since chunks are never removed from the middles of these lists,
1516 double linking is not necessary. Also, unlike regular bins, they
1517 are not even processed in FIFO order (they use faster LIFO) since
1518 ordering doesn't much matter in the transient contexts in which
1519 fastbins are normally used.
1521 Chunks in fastbins keep their inuse bit set, so they cannot
1522 be consolidated with other free chunks. malloc_consolidate
1523 releases all chunks in fastbins and consolidates them with
1524 other free chunks.
1527 typedef struct malloc_chunk *mfastbinptr;
1528 #define fastbin(ar_ptr, idx) ((ar_ptr)->fastbinsY[idx])
1530 /* offset 2 to use otherwise unindexable first 2 bins */
1531 #define fastbin_index(sz) \
1532 ((((unsigned int) (sz)) >> (SIZE_SZ == 8 ? 4 : 3)) - 2)
1535 /* The maximum fastbin request size we support */
1536 #define MAX_FAST_SIZE (80 * SIZE_SZ / 4)
1538 #define NFASTBINS (fastbin_index (request2size (MAX_FAST_SIZE)) + 1)
1541 FASTBIN_CONSOLIDATION_THRESHOLD is the size of a chunk in free()
1542 that triggers automatic consolidation of possibly-surrounding
1543 fastbin chunks. This is a heuristic, so the exact value should not
1544 matter too much. It is defined at half the default trim threshold as a
1545 compromise heuristic to only attempt consolidation if it is likely
1546 to lead to trimming. However, it is not dynamically tunable, since
1547 consolidation reduces fragmentation surrounding large chunks even
1548 if trimming is not used.
1551 #define FASTBIN_CONSOLIDATION_THRESHOLD (65536UL)
1554 Since the lowest 2 bits in max_fast don't matter in size comparisons,
1555 they are used as flags.
1559 FASTCHUNKS_BIT held in max_fast indicates that there are probably
1560 some fastbin chunks. It is set true on entering a chunk into any
1561 fastbin, and cleared only in malloc_consolidate.
1563 The truth value is inverted so that have_fastchunks will be true
1564 upon startup (since statics are zero-filled), simplifying
1565 initialization checks.
1568 #define FASTCHUNKS_BIT (1U)
1570 #define have_fastchunks(M) (((M)->flags & FASTCHUNKS_BIT) == 0)
1571 #define clear_fastchunks(M) catomic_or (&(M)->flags, FASTCHUNKS_BIT)
1572 #define set_fastchunks(M) catomic_and (&(M)->flags, ~FASTCHUNKS_BIT)
1575 NONCONTIGUOUS_BIT indicates that MORECORE does not return contiguous
1576 regions. Otherwise, contiguity is exploited in merging together,
1577 when possible, results from consecutive MORECORE calls.
1579 The initial value comes from MORECORE_CONTIGUOUS, but is
1580 changed dynamically if mmap is ever used as an sbrk substitute.
1583 #define NONCONTIGUOUS_BIT (2U)
1585 #define contiguous(M) (((M)->flags & NONCONTIGUOUS_BIT) == 0)
1586 #define noncontiguous(M) (((M)->flags & NONCONTIGUOUS_BIT) != 0)
1587 #define set_noncontiguous(M) ((M)->flags |= NONCONTIGUOUS_BIT)
1588 #define set_contiguous(M) ((M)->flags &= ~NONCONTIGUOUS_BIT)
1590 /* ARENA_CORRUPTION_BIT is set if a memory corruption was detected on the
1591 arena. Such an arena is no longer used to allocate chunks. Chunks
1592 allocated in that arena before detecting corruption are not freed. */
1594 #define ARENA_CORRUPTION_BIT (4U)
1596 #define arena_is_corrupt(A) (((A)->flags & ARENA_CORRUPTION_BIT))
1597 #define set_arena_corrupt(A) ((A)->flags |= ARENA_CORRUPTION_BIT)
1600 Set value of max_fast.
1601 Use impossibly small value if 0.
1602 Precondition: there are no existing fastbin chunks.
1603 Setting the value clears fastchunk bit but preserves noncontiguous bit.
1606 #define set_max_fast(s) \
1607 global_max_fast = (((s) == 0) \
1608 ? SMALLBIN_WIDTH : ((s + SIZE_SZ) & ~MALLOC_ALIGN_MASK))
1609 #define get_max_fast() global_max_fast
1613 ----------- Internal state representation and initialization -----------
1616 struct malloc_state
1618 /* Serialize access. */
1619 __libc_lock_define (, mutex);
1621 /* Flags (formerly in max_fast). */
1622 int flags;
1624 /* Fastbins */
1625 mfastbinptr fastbinsY[NFASTBINS];
1627 /* Base of the topmost chunk -- not otherwise kept in a bin */
1628 mchunkptr top;
1630 /* The remainder from the most recent split of a small request */
1631 mchunkptr last_remainder;
1633 /* Normal bins packed as described above */
1634 mchunkptr bins[NBINS * 2 - 2];
1636 /* Bitmap of bins */
1637 unsigned int binmap[BINMAPSIZE];
1639 /* Linked list */
1640 struct malloc_state *next;
1642 /* Linked list for free arenas. Access to this field is serialized
1643 by free_list_lock in arena.c. */
1644 struct malloc_state *next_free;
1646 /* Number of threads attached to this arena. 0 if the arena is on
1647 the free list. Access to this field is serialized by
1648 free_list_lock in arena.c. */
1649 INTERNAL_SIZE_T attached_threads;
1651 /* Memory allocated from the system in this arena. */
1652 INTERNAL_SIZE_T system_mem;
1653 INTERNAL_SIZE_T max_system_mem;
1656 struct malloc_par
1658 /* Tunable parameters */
1659 unsigned long trim_threshold;
1660 INTERNAL_SIZE_T top_pad;
1661 INTERNAL_SIZE_T mmap_threshold;
1662 INTERNAL_SIZE_T arena_test;
1663 INTERNAL_SIZE_T arena_max;
1665 /* Memory map support */
1666 int n_mmaps;
1667 int n_mmaps_max;
1668 int max_n_mmaps;
1669 /* the mmap_threshold is dynamic, until the user sets
1670 it manually, at which point we need to disable any
1671 dynamic behavior. */
1672 int no_dyn_threshold;
1674 /* Statistics */
1675 INTERNAL_SIZE_T mmapped_mem;
1676 INTERNAL_SIZE_T max_mmapped_mem;
1678 /* First address handed out by MORECORE/sbrk. */
1679 char *sbrk_base;
1682 /* There are several instances of this struct ("arenas") in this
1683 malloc. If you are adapting this malloc in a way that does NOT use
1684 a static or mmapped malloc_state, you MUST explicitly zero-fill it
1685 before using. This malloc relies on the property that malloc_state
1686 is initialized to all zeroes (as is true of C statics). */
1688 static struct malloc_state main_arena =
1690 .mutex = _LIBC_LOCK_INITIALIZER,
1691 .next = &main_arena,
1692 .attached_threads = 1
1695 /* These variables are used for undumping support. Chunked are marked
1696 as using mmap, but we leave them alone if they fall into this
1697 range. NB: The chunk size for these chunks only includes the
1698 initial size field (of SIZE_SZ bytes), there is no trailing size
1699 field (unlike with regular mmapped chunks). */
1700 static mchunkptr dumped_main_arena_start; /* Inclusive. */
1701 static mchunkptr dumped_main_arena_end; /* Exclusive. */
1703 /* True if the pointer falls into the dumped arena. Use this after
1704 chunk_is_mmapped indicates a chunk is mmapped. */
1705 #define DUMPED_MAIN_ARENA_CHUNK(p) \
1706 ((p) >= dumped_main_arena_start && (p) < dumped_main_arena_end)
1708 /* There is only one instance of the malloc parameters. */
1710 static struct malloc_par mp_ =
1712 .top_pad = DEFAULT_TOP_PAD,
1713 .n_mmaps_max = DEFAULT_MMAP_MAX,
1714 .mmap_threshold = DEFAULT_MMAP_THRESHOLD,
1715 .trim_threshold = DEFAULT_TRIM_THRESHOLD,
1716 #define NARENAS_FROM_NCORES(n) ((n) * (sizeof (long) == 4 ? 2 : 8))
1717 .arena_test = NARENAS_FROM_NCORES (1)
1721 #define M_ARENA_TEST -7
1722 #define M_ARENA_MAX -8
1725 /* Maximum size of memory handled in fastbins. */
1726 static INTERNAL_SIZE_T global_max_fast;
1729 Initialize a malloc_state struct.
1731 This is called only from within malloc_consolidate, which needs
1732 be called in the same contexts anyway. It is never called directly
1733 outside of malloc_consolidate because some optimizing compilers try
1734 to inline it at all call points, which turns out not to be an
1735 optimization at all. (Inlining it in malloc_consolidate is fine though.)
1738 static void
1739 malloc_init_state (mstate av)
1741 int i;
1742 mbinptr bin;
1744 /* Establish circular links for normal bins */
1745 for (i = 1; i < NBINS; ++i)
1747 bin = bin_at (av, i);
1748 bin->fd = bin->bk = bin;
1751 #if MORECORE_CONTIGUOUS
1752 if (av != &main_arena)
1753 #endif
1754 set_noncontiguous (av);
1755 if (av == &main_arena)
1756 set_max_fast (DEFAULT_MXFAST);
1757 av->flags |= FASTCHUNKS_BIT;
1759 av->top = initial_top (av);
1763 Other internal utilities operating on mstates
1766 static void *sysmalloc (INTERNAL_SIZE_T, mstate);
1767 static int systrim (size_t, mstate);
1768 static void malloc_consolidate (mstate);
1771 /* -------------- Early definitions for debugging hooks ---------------- */
1773 /* Define and initialize the hook variables. These weak definitions must
1774 appear before any use of the variables in a function (arena.c uses one). */
1775 #ifndef weak_variable
1776 /* In GNU libc we want the hook variables to be weak definitions to
1777 avoid a problem with Emacs. */
1778 # define weak_variable weak_function
1779 #endif
1781 /* Forward declarations. */
1782 static void *malloc_hook_ini (size_t sz,
1783 const void *caller) __THROW;
1784 static void *realloc_hook_ini (void *ptr, size_t sz,
1785 const void *caller) __THROW;
1786 static void *memalign_hook_ini (size_t alignment, size_t sz,
1787 const void *caller) __THROW;
1789 #if HAVE_MALLOC_INIT_HOOK
1790 void weak_variable (*__malloc_initialize_hook) (void) = NULL;
1791 compat_symbol (libc, __malloc_initialize_hook,
1792 __malloc_initialize_hook, GLIBC_2_0);
1793 #endif
1795 void weak_variable (*__free_hook) (void *__ptr,
1796 const void *) = NULL;
1797 void *weak_variable (*__malloc_hook)
1798 (size_t __size, const void *) = malloc_hook_ini;
1799 void *weak_variable (*__realloc_hook)
1800 (void *__ptr, size_t __size, const void *)
1801 = realloc_hook_ini;
1802 void *weak_variable (*__memalign_hook)
1803 (size_t __alignment, size_t __size, const void *)
1804 = memalign_hook_ini;
1805 void weak_variable (*__after_morecore_hook) (void) = NULL;
1808 /* ---------------- Error behavior ------------------------------------ */
1810 #ifndef DEFAULT_CHECK_ACTION
1811 # define DEFAULT_CHECK_ACTION 3
1812 #endif
1814 static int check_action = DEFAULT_CHECK_ACTION;
1817 /* ------------------ Testing support ----------------------------------*/
1819 static int perturb_byte;
1821 static void
1822 alloc_perturb (char *p, size_t n)
1824 if (__glibc_unlikely (perturb_byte))
1825 memset (p, perturb_byte ^ 0xff, n);
1828 static void
1829 free_perturb (char *p, size_t n)
1831 if (__glibc_unlikely (perturb_byte))
1832 memset (p, perturb_byte, n);
1837 #include <stap-probe.h>
1839 /* ------------------- Support for multiple arenas -------------------- */
1840 #include "arena.c"
1843 Debugging support
1845 These routines make a number of assertions about the states
1846 of data structures that should be true at all times. If any
1847 are not true, it's very likely that a user program has somehow
1848 trashed memory. (It's also possible that there is a coding error
1849 in malloc. In which case, please report it!)
1852 #if !MALLOC_DEBUG
1854 # define check_chunk(A, P)
1855 # define check_free_chunk(A, P)
1856 # define check_inuse_chunk(A, P)
1857 # define check_remalloced_chunk(A, P, N)
1858 # define check_malloced_chunk(A, P, N)
1859 # define check_malloc_state(A)
1861 #else
1863 # define check_chunk(A, P) do_check_chunk (A, P)
1864 # define check_free_chunk(A, P) do_check_free_chunk (A, P)
1865 # define check_inuse_chunk(A, P) do_check_inuse_chunk (A, P)
1866 # define check_remalloced_chunk(A, P, N) do_check_remalloced_chunk (A, P, N)
1867 # define check_malloced_chunk(A, P, N) do_check_malloced_chunk (A, P, N)
1868 # define check_malloc_state(A) do_check_malloc_state (A)
1871 Properties of all chunks
1874 static void
1875 do_check_chunk (mstate av, mchunkptr p)
1877 unsigned long sz = chunksize (p);
1878 /* min and max possible addresses assuming contiguous allocation */
1879 char *max_address = (char *) (av->top) + chunksize (av->top);
1880 char *min_address = max_address - av->system_mem;
1882 if (!chunk_is_mmapped (p))
1884 /* Has legal address ... */
1885 if (p != av->top)
1887 if (contiguous (av))
1889 assert (((char *) p) >= min_address);
1890 assert (((char *) p + sz) <= ((char *) (av->top)));
1893 else
1895 /* top size is always at least MINSIZE */
1896 assert ((unsigned long) (sz) >= MINSIZE);
1897 /* top predecessor always marked inuse */
1898 assert (prev_inuse (p));
1901 else if (!DUMPED_MAIN_ARENA_CHUNK (p))
1903 /* address is outside main heap */
1904 if (contiguous (av) && av->top != initial_top (av))
1906 assert (((char *) p) < min_address || ((char *) p) >= max_address);
1908 /* chunk is page-aligned */
1909 assert (((p->prev_size + sz) & (GLRO (dl_pagesize) - 1)) == 0);
1910 /* mem is aligned */
1911 assert (aligned_OK (chunk2mem (p)));
1916 Properties of free chunks
1919 static void
1920 do_check_free_chunk (mstate av, mchunkptr p)
1922 INTERNAL_SIZE_T sz = p->size & ~(PREV_INUSE | NON_MAIN_ARENA);
1923 mchunkptr next = chunk_at_offset (p, sz);
1925 do_check_chunk (av, p);
1927 /* Chunk must claim to be free ... */
1928 assert (!inuse (p));
1929 assert (!chunk_is_mmapped (p));
1931 /* Unless a special marker, must have OK fields */
1932 if ((unsigned long) (sz) >= MINSIZE)
1934 assert ((sz & MALLOC_ALIGN_MASK) == 0);
1935 assert (aligned_OK (chunk2mem (p)));
1936 /* ... matching footer field */
1937 assert (next->prev_size == sz);
1938 /* ... and is fully consolidated */
1939 assert (prev_inuse (p));
1940 assert (next == av->top || inuse (next));
1942 /* ... and has minimally sane links */
1943 assert (p->fd->bk == p);
1944 assert (p->bk->fd == p);
1946 else /* markers are always of size SIZE_SZ */
1947 assert (sz == SIZE_SZ);
1951 Properties of inuse chunks
1954 static void
1955 do_check_inuse_chunk (mstate av, mchunkptr p)
1957 mchunkptr next;
1959 do_check_chunk (av, p);
1961 if (chunk_is_mmapped (p))
1962 return; /* mmapped chunks have no next/prev */
1964 /* Check whether it claims to be in use ... */
1965 assert (inuse (p));
1967 next = next_chunk (p);
1969 /* ... and is surrounded by OK chunks.
1970 Since more things can be checked with free chunks than inuse ones,
1971 if an inuse chunk borders them and debug is on, it's worth doing them.
1973 if (!prev_inuse (p))
1975 /* Note that we cannot even look at prev unless it is not inuse */
1976 mchunkptr prv = prev_chunk (p);
1977 assert (next_chunk (prv) == p);
1978 do_check_free_chunk (av, prv);
1981 if (next == av->top)
1983 assert (prev_inuse (next));
1984 assert (chunksize (next) >= MINSIZE);
1986 else if (!inuse (next))
1987 do_check_free_chunk (av, next);
1991 Properties of chunks recycled from fastbins
1994 static void
1995 do_check_remalloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s)
1997 INTERNAL_SIZE_T sz = p->size & ~(PREV_INUSE | NON_MAIN_ARENA);
1999 if (!chunk_is_mmapped (p))
2001 assert (av == arena_for_chunk (p));
2002 if (chunk_non_main_arena (p))
2003 assert (av != &main_arena);
2004 else
2005 assert (av == &main_arena);
2008 do_check_inuse_chunk (av, p);
2010 /* Legal size ... */
2011 assert ((sz & MALLOC_ALIGN_MASK) == 0);
2012 assert ((unsigned long) (sz) >= MINSIZE);
2013 /* ... and alignment */
2014 assert (aligned_OK (chunk2mem (p)));
2015 /* chunk is less than MINSIZE more than request */
2016 assert ((long) (sz) - (long) (s) >= 0);
2017 assert ((long) (sz) - (long) (s + MINSIZE) < 0);
2021 Properties of nonrecycled chunks at the point they are malloced
2024 static void
2025 do_check_malloced_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T s)
2027 /* same as recycled case ... */
2028 do_check_remalloced_chunk (av, p, s);
2031 ... plus, must obey implementation invariant that prev_inuse is
2032 always true of any allocated chunk; i.e., that each allocated
2033 chunk borders either a previously allocated and still in-use
2034 chunk, or the base of its memory arena. This is ensured
2035 by making all allocations from the `lowest' part of any found
2036 chunk. This does not necessarily hold however for chunks
2037 recycled via fastbins.
2040 assert (prev_inuse (p));
2045 Properties of malloc_state.
2047 This may be useful for debugging malloc, as well as detecting user
2048 programmer errors that somehow write into malloc_state.
2050 If you are extending or experimenting with this malloc, you can
2051 probably figure out how to hack this routine to print out or
2052 display chunk addresses, sizes, bins, and other instrumentation.
2055 static void
2056 do_check_malloc_state (mstate av)
2058 int i;
2059 mchunkptr p;
2060 mchunkptr q;
2061 mbinptr b;
2062 unsigned int idx;
2063 INTERNAL_SIZE_T size;
2064 unsigned long total = 0;
2065 int max_fast_bin;
2067 /* internal size_t must be no wider than pointer type */
2068 assert (sizeof (INTERNAL_SIZE_T) <= sizeof (char *));
2070 /* alignment is a power of 2 */
2071 assert ((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT - 1)) == 0);
2073 /* cannot run remaining checks until fully initialized */
2074 if (av->top == 0 || av->top == initial_top (av))
2075 return;
2077 /* pagesize is a power of 2 */
2078 assert (powerof2(GLRO (dl_pagesize)));
2080 /* A contiguous main_arena is consistent with sbrk_base. */
2081 if (av == &main_arena && contiguous (av))
2082 assert ((char *) mp_.sbrk_base + av->system_mem ==
2083 (char *) av->top + chunksize (av->top));
2085 /* properties of fastbins */
2087 /* max_fast is in allowed range */
2088 assert ((get_max_fast () & ~1) <= request2size (MAX_FAST_SIZE));
2090 max_fast_bin = fastbin_index (get_max_fast ());
2092 for (i = 0; i < NFASTBINS; ++i)
2094 p = fastbin (av, i);
2096 /* The following test can only be performed for the main arena.
2097 While mallopt calls malloc_consolidate to get rid of all fast
2098 bins (especially those larger than the new maximum) this does
2099 only happen for the main arena. Trying to do this for any
2100 other arena would mean those arenas have to be locked and
2101 malloc_consolidate be called for them. This is excessive. And
2102 even if this is acceptable to somebody it still cannot solve
2103 the problem completely since if the arena is locked a
2104 concurrent malloc call might create a new arena which then
2105 could use the newly invalid fast bins. */
2107 /* all bins past max_fast are empty */
2108 if (av == &main_arena && i > max_fast_bin)
2109 assert (p == 0);
2111 while (p != 0)
2113 /* each chunk claims to be inuse */
2114 do_check_inuse_chunk (av, p);
2115 total += chunksize (p);
2116 /* chunk belongs in this bin */
2117 assert (fastbin_index (chunksize (p)) == i);
2118 p = p->fd;
2122 if (total != 0)
2123 assert (have_fastchunks (av));
2124 else if (!have_fastchunks (av))
2125 assert (total == 0);
2127 /* check normal bins */
2128 for (i = 1; i < NBINS; ++i)
2130 b = bin_at (av, i);
2132 /* binmap is accurate (except for bin 1 == unsorted_chunks) */
2133 if (i >= 2)
2135 unsigned int binbit = get_binmap (av, i);
2136 int empty = last (b) == b;
2137 if (!binbit)
2138 assert (empty);
2139 else if (!empty)
2140 assert (binbit);
2143 for (p = last (b); p != b; p = p->bk)
2145 /* each chunk claims to be free */
2146 do_check_free_chunk (av, p);
2147 size = chunksize (p);
2148 total += size;
2149 if (i >= 2)
2151 /* chunk belongs in bin */
2152 idx = bin_index (size);
2153 assert (idx == i);
2154 /* lists are sorted */
2155 assert (p->bk == b ||
2156 (unsigned long) chunksize (p->bk) >= (unsigned long) chunksize (p));
2158 if (!in_smallbin_range (size))
2160 if (p->fd_nextsize != NULL)
2162 if (p->fd_nextsize == p)
2163 assert (p->bk_nextsize == p);
2164 else
2166 if (p->fd_nextsize == first (b))
2167 assert (chunksize (p) < chunksize (p->fd_nextsize));
2168 else
2169 assert (chunksize (p) > chunksize (p->fd_nextsize));
2171 if (p == first (b))
2172 assert (chunksize (p) > chunksize (p->bk_nextsize));
2173 else
2174 assert (chunksize (p) < chunksize (p->bk_nextsize));
2177 else
2178 assert (p->bk_nextsize == NULL);
2181 else if (!in_smallbin_range (size))
2182 assert (p->fd_nextsize == NULL && p->bk_nextsize == NULL);
2183 /* chunk is followed by a legal chain of inuse chunks */
2184 for (q = next_chunk (p);
2185 (q != av->top && inuse (q) &&
2186 (unsigned long) (chunksize (q)) >= MINSIZE);
2187 q = next_chunk (q))
2188 do_check_inuse_chunk (av, q);
2192 /* top chunk is OK */
2193 check_chunk (av, av->top);
2195 #endif
2198 /* ----------------- Support for debugging hooks -------------------- */
2199 #include "hooks.c"
2202 /* ----------- Routines dealing with system allocation -------------- */
2205 sysmalloc handles malloc cases requiring more memory from the system.
2206 On entry, it is assumed that av->top does not have enough
2207 space to service request for nb bytes, thus requiring that av->top
2208 be extended or replaced.
2211 static void *
2212 sysmalloc (INTERNAL_SIZE_T nb, mstate av)
2214 mchunkptr old_top; /* incoming value of av->top */
2215 INTERNAL_SIZE_T old_size; /* its size */
2216 char *old_end; /* its end address */
2218 long size; /* arg to first MORECORE or mmap call */
2219 char *brk; /* return value from MORECORE */
2221 long correction; /* arg to 2nd MORECORE call */
2222 char *snd_brk; /* 2nd return val */
2224 INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of new space */
2225 INTERNAL_SIZE_T end_misalign; /* partial page left at end of new space */
2226 char *aligned_brk; /* aligned offset into brk */
2228 mchunkptr p; /* the allocated/returned chunk */
2229 mchunkptr remainder; /* remainder from allocation */
2230 unsigned long remainder_size; /* its size */
2233 size_t pagesize = GLRO (dl_pagesize);
2234 bool tried_mmap = false;
2238 If have mmap, and the request size meets the mmap threshold, and
2239 the system supports mmap, and there are few enough currently
2240 allocated mmapped regions, try to directly map this request
2241 rather than expanding top.
2244 if (av == NULL
2245 || ((unsigned long) (nb) >= (unsigned long) (mp_.mmap_threshold)
2246 && (mp_.n_mmaps < mp_.n_mmaps_max)))
2248 char *mm; /* return value from mmap call*/
2250 try_mmap:
2252 Round up size to nearest page. For mmapped chunks, the overhead
2253 is one SIZE_SZ unit larger than for normal chunks, because there
2254 is no following chunk whose prev_size field could be used.
2256 See the front_misalign handling below, for glibc there is no
2257 need for further alignments unless we have have high alignment.
2259 if (MALLOC_ALIGNMENT == 2 * SIZE_SZ)
2260 size = ALIGN_UP (nb + SIZE_SZ, pagesize);
2261 else
2262 size = ALIGN_UP (nb + SIZE_SZ + MALLOC_ALIGN_MASK, pagesize);
2263 tried_mmap = true;
2265 /* Don't try if size wraps around 0 */
2266 if ((unsigned long) (size) > (unsigned long) (nb))
2268 mm = (char *) (MMAP (0, size, PROT_READ | PROT_WRITE, 0));
2270 if (mm != MAP_FAILED)
2273 The offset to the start of the mmapped region is stored
2274 in the prev_size field of the chunk. This allows us to adjust
2275 returned start address to meet alignment requirements here
2276 and in memalign(), and still be able to compute proper
2277 address argument for later munmap in free() and realloc().
2280 if (MALLOC_ALIGNMENT == 2 * SIZE_SZ)
2282 /* For glibc, chunk2mem increases the address by 2*SIZE_SZ and
2283 MALLOC_ALIGN_MASK is 2*SIZE_SZ-1. Each mmap'ed area is page
2284 aligned and therefore definitely MALLOC_ALIGN_MASK-aligned. */
2285 assert (((INTERNAL_SIZE_T) chunk2mem (mm) & MALLOC_ALIGN_MASK) == 0);
2286 front_misalign = 0;
2288 else
2289 front_misalign = (INTERNAL_SIZE_T) chunk2mem (mm) & MALLOC_ALIGN_MASK;
2290 if (front_misalign > 0)
2292 correction = MALLOC_ALIGNMENT - front_misalign;
2293 p = (mchunkptr) (mm + correction);
2294 p->prev_size = correction;
2295 set_head (p, (size - correction) | IS_MMAPPED);
2297 else
2299 p = (mchunkptr) mm;
2300 set_head (p, size | IS_MMAPPED);
2303 /* update statistics */
2305 int new = atomic_exchange_and_add (&mp_.n_mmaps, 1) + 1;
2306 atomic_max (&mp_.max_n_mmaps, new);
2308 unsigned long sum;
2309 sum = atomic_exchange_and_add (&mp_.mmapped_mem, size) + size;
2310 atomic_max (&mp_.max_mmapped_mem, sum);
2312 check_chunk (av, p);
2314 return chunk2mem (p);
2319 /* There are no usable arenas and mmap also failed. */
2320 if (av == NULL)
2321 return 0;
2323 /* Record incoming configuration of top */
2325 old_top = av->top;
2326 old_size = chunksize (old_top);
2327 old_end = (char *) (chunk_at_offset (old_top, old_size));
2329 brk = snd_brk = (char *) (MORECORE_FAILURE);
2332 If not the first time through, we require old_size to be
2333 at least MINSIZE and to have prev_inuse set.
2336 assert ((old_top == initial_top (av) && old_size == 0) ||
2337 ((unsigned long) (old_size) >= MINSIZE &&
2338 prev_inuse (old_top) &&
2339 ((unsigned long) old_end & (pagesize - 1)) == 0));
2341 /* Precondition: not enough current space to satisfy nb request */
2342 assert ((unsigned long) (old_size) < (unsigned long) (nb + MINSIZE));
2345 if (av != &main_arena)
2347 heap_info *old_heap, *heap;
2348 size_t old_heap_size;
2350 /* First try to extend the current heap. */
2351 old_heap = heap_for_ptr (old_top);
2352 old_heap_size = old_heap->size;
2353 if ((long) (MINSIZE + nb - old_size) > 0
2354 && grow_heap (old_heap, MINSIZE + nb - old_size) == 0)
2356 av->system_mem += old_heap->size - old_heap_size;
2357 set_head (old_top, (((char *) old_heap + old_heap->size) - (char *) old_top)
2358 | PREV_INUSE);
2360 else if ((heap = new_heap (nb + (MINSIZE + sizeof (*heap)), mp_.top_pad)))
2362 /* Use a newly allocated heap. */
2363 heap->ar_ptr = av;
2364 heap->prev = old_heap;
2365 av->system_mem += heap->size;
2366 /* Set up the new top. */
2367 top (av) = chunk_at_offset (heap, sizeof (*heap));
2368 set_head (top (av), (heap->size - sizeof (*heap)) | PREV_INUSE);
2370 /* Setup fencepost and free the old top chunk with a multiple of
2371 MALLOC_ALIGNMENT in size. */
2372 /* The fencepost takes at least MINSIZE bytes, because it might
2373 become the top chunk again later. Note that a footer is set
2374 up, too, although the chunk is marked in use. */
2375 old_size = (old_size - MINSIZE) & ~MALLOC_ALIGN_MASK;
2376 set_head (chunk_at_offset (old_top, old_size + 2 * SIZE_SZ), 0 | PREV_INUSE);
2377 if (old_size >= MINSIZE)
2379 set_head (chunk_at_offset (old_top, old_size), (2 * SIZE_SZ) | PREV_INUSE);
2380 set_foot (chunk_at_offset (old_top, old_size), (2 * SIZE_SZ));
2381 set_head (old_top, old_size | PREV_INUSE | NON_MAIN_ARENA);
2382 _int_free (av, old_top, 1);
2384 else
2386 set_head (old_top, (old_size + 2 * SIZE_SZ) | PREV_INUSE);
2387 set_foot (old_top, (old_size + 2 * SIZE_SZ));
2390 else if (!tried_mmap)
2391 /* We can at least try to use to mmap memory. */
2392 goto try_mmap;
2394 else /* av == main_arena */
2397 { /* Request enough space for nb + pad + overhead */
2398 size = nb + mp_.top_pad + MINSIZE;
2401 If contiguous, we can subtract out existing space that we hope to
2402 combine with new space. We add it back later only if
2403 we don't actually get contiguous space.
2406 if (contiguous (av))
2407 size -= old_size;
2410 Round to a multiple of page size.
2411 If MORECORE is not contiguous, this ensures that we only call it
2412 with whole-page arguments. And if MORECORE is contiguous and
2413 this is not first time through, this preserves page-alignment of
2414 previous calls. Otherwise, we correct to page-align below.
2417 size = ALIGN_UP (size, pagesize);
2420 Don't try to call MORECORE if argument is so big as to appear
2421 negative. Note that since mmap takes size_t arg, it may succeed
2422 below even if we cannot call MORECORE.
2425 if (size > 0)
2427 brk = (char *) (MORECORE (size));
2428 LIBC_PROBE (memory_sbrk_more, 2, brk, size);
2431 if (brk != (char *) (MORECORE_FAILURE))
2433 /* Call the `morecore' hook if necessary. */
2434 void (*hook) (void) = atomic_forced_read (__after_morecore_hook);
2435 if (__builtin_expect (hook != NULL, 0))
2436 (*hook)();
2438 else
2441 If have mmap, try using it as a backup when MORECORE fails or
2442 cannot be used. This is worth doing on systems that have "holes" in
2443 address space, so sbrk cannot extend to give contiguous space, but
2444 space is available elsewhere. Note that we ignore mmap max count
2445 and threshold limits, since the space will not be used as a
2446 segregated mmap region.
2449 /* Cannot merge with old top, so add its size back in */
2450 if (contiguous (av))
2451 size = ALIGN_UP (size + old_size, pagesize);
2453 /* If we are relying on mmap as backup, then use larger units */
2454 if ((unsigned long) (size) < (unsigned long) (MMAP_AS_MORECORE_SIZE))
2455 size = MMAP_AS_MORECORE_SIZE;
2457 /* Don't try if size wraps around 0 */
2458 if ((unsigned long) (size) > (unsigned long) (nb))
2460 char *mbrk = (char *) (MMAP (0, size, PROT_READ | PROT_WRITE, 0));
2462 if (mbrk != MAP_FAILED)
2464 /* We do not need, and cannot use, another sbrk call to find end */
2465 brk = mbrk;
2466 snd_brk = brk + size;
2469 Record that we no longer have a contiguous sbrk region.
2470 After the first time mmap is used as backup, we do not
2471 ever rely on contiguous space since this could incorrectly
2472 bridge regions.
2474 set_noncontiguous (av);
2479 if (brk != (char *) (MORECORE_FAILURE))
2481 if (mp_.sbrk_base == 0)
2482 mp_.sbrk_base = brk;
2483 av->system_mem += size;
2486 If MORECORE extends previous space, we can likewise extend top size.
2489 if (brk == old_end && snd_brk == (char *) (MORECORE_FAILURE))
2490 set_head (old_top, (size + old_size) | PREV_INUSE);
2492 else if (contiguous (av) && old_size && brk < old_end)
2494 /* Oops! Someone else killed our space.. Can't touch anything. */
2495 malloc_printerr (3, "break adjusted to free malloc space", brk,
2496 av);
2500 Otherwise, make adjustments:
2502 * If the first time through or noncontiguous, we need to call sbrk
2503 just to find out where the end of memory lies.
2505 * We need to ensure that all returned chunks from malloc will meet
2506 MALLOC_ALIGNMENT
2508 * If there was an intervening foreign sbrk, we need to adjust sbrk
2509 request size to account for fact that we will not be able to
2510 combine new space with existing space in old_top.
2512 * Almost all systems internally allocate whole pages at a time, in
2513 which case we might as well use the whole last page of request.
2514 So we allocate enough more memory to hit a page boundary now,
2515 which in turn causes future contiguous calls to page-align.
2518 else
2520 front_misalign = 0;
2521 end_misalign = 0;
2522 correction = 0;
2523 aligned_brk = brk;
2525 /* handle contiguous cases */
2526 if (contiguous (av))
2528 /* Count foreign sbrk as system_mem. */
2529 if (old_size)
2530 av->system_mem += brk - old_end;
2532 /* Guarantee alignment of first new chunk made from this space */
2534 front_misalign = (INTERNAL_SIZE_T) chunk2mem (brk) & MALLOC_ALIGN_MASK;
2535 if (front_misalign > 0)
2538 Skip over some bytes to arrive at an aligned position.
2539 We don't need to specially mark these wasted front bytes.
2540 They will never be accessed anyway because
2541 prev_inuse of av->top (and any chunk created from its start)
2542 is always true after initialization.
2545 correction = MALLOC_ALIGNMENT - front_misalign;
2546 aligned_brk += correction;
2550 If this isn't adjacent to existing space, then we will not
2551 be able to merge with old_top space, so must add to 2nd request.
2554 correction += old_size;
2556 /* Extend the end address to hit a page boundary */
2557 end_misalign = (INTERNAL_SIZE_T) (brk + size + correction);
2558 correction += (ALIGN_UP (end_misalign, pagesize)) - end_misalign;
2560 assert (correction >= 0);
2561 snd_brk = (char *) (MORECORE (correction));
2564 If can't allocate correction, try to at least find out current
2565 brk. It might be enough to proceed without failing.
2567 Note that if second sbrk did NOT fail, we assume that space
2568 is contiguous with first sbrk. This is a safe assumption unless
2569 program is multithreaded but doesn't use locks and a foreign sbrk
2570 occurred between our first and second calls.
2573 if (snd_brk == (char *) (MORECORE_FAILURE))
2575 correction = 0;
2576 snd_brk = (char *) (MORECORE (0));
2578 else
2580 /* Call the `morecore' hook if necessary. */
2581 void (*hook) (void) = atomic_forced_read (__after_morecore_hook);
2582 if (__builtin_expect (hook != NULL, 0))
2583 (*hook)();
2587 /* handle non-contiguous cases */
2588 else
2590 if (MALLOC_ALIGNMENT == 2 * SIZE_SZ)
2591 /* MORECORE/mmap must correctly align */
2592 assert (((unsigned long) chunk2mem (brk) & MALLOC_ALIGN_MASK) == 0);
2593 else
2595 front_misalign = (INTERNAL_SIZE_T) chunk2mem (brk) & MALLOC_ALIGN_MASK;
2596 if (front_misalign > 0)
2599 Skip over some bytes to arrive at an aligned position.
2600 We don't need to specially mark these wasted front bytes.
2601 They will never be accessed anyway because
2602 prev_inuse of av->top (and any chunk created from its start)
2603 is always true after initialization.
2606 aligned_brk += MALLOC_ALIGNMENT - front_misalign;
2610 /* Find out current end of memory */
2611 if (snd_brk == (char *) (MORECORE_FAILURE))
2613 snd_brk = (char *) (MORECORE (0));
2617 /* Adjust top based on results of second sbrk */
2618 if (snd_brk != (char *) (MORECORE_FAILURE))
2620 av->top = (mchunkptr) aligned_brk;
2621 set_head (av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
2622 av->system_mem += correction;
2625 If not the first time through, we either have a
2626 gap due to foreign sbrk or a non-contiguous region. Insert a
2627 double fencepost at old_top to prevent consolidation with space
2628 we don't own. These fenceposts are artificial chunks that are
2629 marked as inuse and are in any case too small to use. We need
2630 two to make sizes and alignments work out.
2633 if (old_size != 0)
2636 Shrink old_top to insert fenceposts, keeping size a
2637 multiple of MALLOC_ALIGNMENT. We know there is at least
2638 enough space in old_top to do this.
2640 old_size = (old_size - 4 * SIZE_SZ) & ~MALLOC_ALIGN_MASK;
2641 set_head (old_top, old_size | PREV_INUSE);
2644 Note that the following assignments completely overwrite
2645 old_top when old_size was previously MINSIZE. This is
2646 intentional. We need the fencepost, even if old_top otherwise gets
2647 lost.
2649 chunk_at_offset (old_top, old_size)->size =
2650 (2 * SIZE_SZ) | PREV_INUSE;
2652 chunk_at_offset (old_top, old_size + 2 * SIZE_SZ)->size =
2653 (2 * SIZE_SZ) | PREV_INUSE;
2655 /* If possible, release the rest. */
2656 if (old_size >= MINSIZE)
2658 _int_free (av, old_top, 1);
2664 } /* if (av != &main_arena) */
2666 if ((unsigned long) av->system_mem > (unsigned long) (av->max_system_mem))
2667 av->max_system_mem = av->system_mem;
2668 check_malloc_state (av);
2670 /* finally, do the allocation */
2671 p = av->top;
2672 size = chunksize (p);
2674 /* check that one of the above allocation paths succeeded */
2675 if ((unsigned long) (size) >= (unsigned long) (nb + MINSIZE))
2677 remainder_size = size - nb;
2678 remainder = chunk_at_offset (p, nb);
2679 av->top = remainder;
2680 set_head (p, nb | PREV_INUSE | (av != &main_arena ? NON_MAIN_ARENA : 0));
2681 set_head (remainder, remainder_size | PREV_INUSE);
2682 check_malloced_chunk (av, p, nb);
2683 return chunk2mem (p);
2686 /* catch all failure paths */
2687 __set_errno (ENOMEM);
2688 return 0;
2693 systrim is an inverse of sorts to sysmalloc. It gives memory back
2694 to the system (via negative arguments to sbrk) if there is unused
2695 memory at the `high' end of the malloc pool. It is called
2696 automatically by free() when top space exceeds the trim
2697 threshold. It is also called by the public malloc_trim routine. It
2698 returns 1 if it actually released any memory, else 0.
2701 static int
2702 systrim (size_t pad, mstate av)
2704 long top_size; /* Amount of top-most memory */
2705 long extra; /* Amount to release */
2706 long released; /* Amount actually released */
2707 char *current_brk; /* address returned by pre-check sbrk call */
2708 char *new_brk; /* address returned by post-check sbrk call */
2709 size_t pagesize;
2710 long top_area;
2712 pagesize = GLRO (dl_pagesize);
2713 top_size = chunksize (av->top);
2715 top_area = top_size - MINSIZE - 1;
2716 if (top_area <= pad)
2717 return 0;
2719 /* Release in pagesize units and round down to the nearest page. */
2720 extra = ALIGN_DOWN(top_area - pad, pagesize);
2722 if (extra == 0)
2723 return 0;
2726 Only proceed if end of memory is where we last set it.
2727 This avoids problems if there were foreign sbrk calls.
2729 current_brk = (char *) (MORECORE (0));
2730 if (current_brk == (char *) (av->top) + top_size)
2733 Attempt to release memory. We ignore MORECORE return value,
2734 and instead call again to find out where new end of memory is.
2735 This avoids problems if first call releases less than we asked,
2736 of if failure somehow altered brk value. (We could still
2737 encounter problems if it altered brk in some very bad way,
2738 but the only thing we can do is adjust anyway, which will cause
2739 some downstream failure.)
2742 MORECORE (-extra);
2743 /* Call the `morecore' hook if necessary. */
2744 void (*hook) (void) = atomic_forced_read (__after_morecore_hook);
2745 if (__builtin_expect (hook != NULL, 0))
2746 (*hook)();
2747 new_brk = (char *) (MORECORE (0));
2749 LIBC_PROBE (memory_sbrk_less, 2, new_brk, extra);
2751 if (new_brk != (char *) MORECORE_FAILURE)
2753 released = (long) (current_brk - new_brk);
2755 if (released != 0)
2757 /* Success. Adjust top. */
2758 av->system_mem -= released;
2759 set_head (av->top, (top_size - released) | PREV_INUSE);
2760 check_malloc_state (av);
2761 return 1;
2765 return 0;
2768 static void
2769 internal_function
2770 munmap_chunk (mchunkptr p)
2772 INTERNAL_SIZE_T size = chunksize (p);
2774 assert (chunk_is_mmapped (p));
2776 /* Do nothing if the chunk is a faked mmapped chunk in the dumped
2777 main arena. We never free this memory. */
2778 if (DUMPED_MAIN_ARENA_CHUNK (p))
2779 return;
2781 uintptr_t block = (uintptr_t) p - p->prev_size;
2782 size_t total_size = p->prev_size + size;
2783 /* Unfortunately we have to do the compilers job by hand here. Normally
2784 we would test BLOCK and TOTAL-SIZE separately for compliance with the
2785 page size. But gcc does not recognize the optimization possibility
2786 (in the moment at least) so we combine the two values into one before
2787 the bit test. */
2788 if (__builtin_expect (((block | total_size) & (GLRO (dl_pagesize) - 1)) != 0, 0))
2790 malloc_printerr (check_action, "munmap_chunk(): invalid pointer",
2791 chunk2mem (p), NULL);
2792 return;
2795 atomic_decrement (&mp_.n_mmaps);
2796 atomic_add (&mp_.mmapped_mem, -total_size);
2798 /* If munmap failed the process virtual memory address space is in a
2799 bad shape. Just leave the block hanging around, the process will
2800 terminate shortly anyway since not much can be done. */
2801 __munmap ((char *) block, total_size);
2804 #if HAVE_MREMAP
2806 static mchunkptr
2807 internal_function
2808 mremap_chunk (mchunkptr p, size_t new_size)
2810 size_t pagesize = GLRO (dl_pagesize);
2811 INTERNAL_SIZE_T offset = p->prev_size;
2812 INTERNAL_SIZE_T size = chunksize (p);
2813 char *cp;
2815 assert (chunk_is_mmapped (p));
2816 assert (((size + offset) & (GLRO (dl_pagesize) - 1)) == 0);
2818 /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
2819 new_size = ALIGN_UP (new_size + offset + SIZE_SZ, pagesize);
2821 /* No need to remap if the number of pages does not change. */
2822 if (size + offset == new_size)
2823 return p;
2825 cp = (char *) __mremap ((char *) p - offset, size + offset, new_size,
2826 MREMAP_MAYMOVE);
2828 if (cp == MAP_FAILED)
2829 return 0;
2831 p = (mchunkptr) (cp + offset);
2833 assert (aligned_OK (chunk2mem (p)));
2835 assert ((p->prev_size == offset));
2836 set_head (p, (new_size - offset) | IS_MMAPPED);
2838 INTERNAL_SIZE_T new;
2839 new = atomic_exchange_and_add (&mp_.mmapped_mem, new_size - size - offset)
2840 + new_size - size - offset;
2841 atomic_max (&mp_.max_mmapped_mem, new);
2842 return p;
2844 #endif /* HAVE_MREMAP */
2846 /*------------------------ Public wrappers. --------------------------------*/
2848 void *
2849 __libc_malloc (size_t bytes)
2851 mstate ar_ptr;
2852 void *victim;
2854 void *(*hook) (size_t, const void *)
2855 = atomic_forced_read (__malloc_hook);
2856 if (__builtin_expect (hook != NULL, 0))
2857 return (*hook)(bytes, RETURN_ADDRESS (0));
2859 arena_get (ar_ptr, bytes);
2861 victim = _int_malloc (ar_ptr, bytes);
2862 /* Retry with another arena only if we were able to find a usable arena
2863 before. */
2864 if (!victim && ar_ptr != NULL)
2866 LIBC_PROBE (memory_malloc_retry, 1, bytes);
2867 ar_ptr = arena_get_retry (ar_ptr, bytes);
2868 victim = _int_malloc (ar_ptr, bytes);
2871 if (ar_ptr != NULL)
2872 __libc_lock_unlock (ar_ptr->mutex);
2874 assert (!victim || chunk_is_mmapped (mem2chunk (victim)) ||
2875 ar_ptr == arena_for_chunk (mem2chunk (victim)));
2876 return victim;
2878 libc_hidden_def (__libc_malloc)
2880 void
2881 __libc_free (void *mem)
2883 mstate ar_ptr;
2884 mchunkptr p; /* chunk corresponding to mem */
2886 void (*hook) (void *, const void *)
2887 = atomic_forced_read (__free_hook);
2888 if (__builtin_expect (hook != NULL, 0))
2890 (*hook)(mem, RETURN_ADDRESS (0));
2891 return;
2894 if (mem == 0) /* free(0) has no effect */
2895 return;
2897 p = mem2chunk (mem);
2899 if (chunk_is_mmapped (p)) /* release mmapped memory. */
2901 /* See if the dynamic brk/mmap threshold needs adjusting.
2902 Dumped fake mmapped chunks do not affect the threshold. */
2903 if (!mp_.no_dyn_threshold
2904 && p->size > mp_.mmap_threshold
2905 && p->size <= DEFAULT_MMAP_THRESHOLD_MAX
2906 && !DUMPED_MAIN_ARENA_CHUNK (p))
2908 mp_.mmap_threshold = chunksize (p);
2909 mp_.trim_threshold = 2 * mp_.mmap_threshold;
2910 LIBC_PROBE (memory_mallopt_free_dyn_thresholds, 2,
2911 mp_.mmap_threshold, mp_.trim_threshold);
2913 munmap_chunk (p);
2914 return;
2917 ar_ptr = arena_for_chunk (p);
2918 _int_free (ar_ptr, p, 0);
2920 libc_hidden_def (__libc_free)
2922 void *
2923 __libc_realloc (void *oldmem, size_t bytes)
2925 mstate ar_ptr;
2926 INTERNAL_SIZE_T nb; /* padded request size */
2928 void *newp; /* chunk to return */
2930 void *(*hook) (void *, size_t, const void *) =
2931 atomic_forced_read (__realloc_hook);
2932 if (__builtin_expect (hook != NULL, 0))
2933 return (*hook)(oldmem, bytes, RETURN_ADDRESS (0));
2935 #if REALLOC_ZERO_BYTES_FREES
2936 if (bytes == 0 && oldmem != NULL)
2938 __libc_free (oldmem); return 0;
2940 #endif
2942 /* realloc of null is supposed to be same as malloc */
2943 if (oldmem == 0)
2944 return __libc_malloc (bytes);
2946 /* chunk corresponding to oldmem */
2947 const mchunkptr oldp = mem2chunk (oldmem);
2948 /* its size */
2949 const INTERNAL_SIZE_T oldsize = chunksize (oldp);
2951 if (chunk_is_mmapped (oldp))
2952 ar_ptr = NULL;
2953 else
2954 ar_ptr = arena_for_chunk (oldp);
2956 /* Little security check which won't hurt performance: the allocator
2957 never wrapps around at the end of the address space. Therefore
2958 we can exclude some size values which might appear here by
2959 accident or by "design" from some intruder. We need to bypass
2960 this check for dumped fake mmap chunks from the old main arena
2961 because the new malloc may provide additional alignment. */
2962 if ((__builtin_expect ((uintptr_t) oldp > (uintptr_t) -oldsize, 0)
2963 || __builtin_expect (misaligned_chunk (oldp), 0))
2964 && !DUMPED_MAIN_ARENA_CHUNK (oldp))
2966 malloc_printerr (check_action, "realloc(): invalid pointer", oldmem,
2967 ar_ptr);
2968 return NULL;
2971 checked_request2size (bytes, nb);
2973 if (chunk_is_mmapped (oldp))
2975 /* If this is a faked mmapped chunk from the dumped main arena,
2976 always make a copy (and do not free the old chunk). */
2977 if (DUMPED_MAIN_ARENA_CHUNK (oldp))
2979 /* Must alloc, copy, free. */
2980 void *newmem = __libc_malloc (bytes);
2981 if (newmem == 0)
2982 return NULL;
2983 /* Copy as many bytes as are available from the old chunk
2984 and fit into the new size. NB: The overhead for faked
2985 mmapped chunks is only SIZE_SZ, not 2 * SIZE_SZ as for
2986 regular mmapped chunks. */
2987 if (bytes > oldsize - SIZE_SZ)
2988 bytes = oldsize - SIZE_SZ;
2989 memcpy (newmem, oldmem, bytes);
2990 return newmem;
2993 void *newmem;
2995 #if HAVE_MREMAP
2996 newp = mremap_chunk (oldp, nb);
2997 if (newp)
2998 return chunk2mem (newp);
2999 #endif
3000 /* Note the extra SIZE_SZ overhead. */
3001 if (oldsize - SIZE_SZ >= nb)
3002 return oldmem; /* do nothing */
3004 /* Must alloc, copy, free. */
3005 newmem = __libc_malloc (bytes);
3006 if (newmem == 0)
3007 return 0; /* propagate failure */
3009 memcpy (newmem, oldmem, oldsize - 2 * SIZE_SZ);
3010 munmap_chunk (oldp);
3011 return newmem;
3014 __libc_lock_lock (ar_ptr->mutex);
3016 newp = _int_realloc (ar_ptr, oldp, oldsize, nb);
3018 __libc_lock_unlock (ar_ptr->mutex);
3019 assert (!newp || chunk_is_mmapped (mem2chunk (newp)) ||
3020 ar_ptr == arena_for_chunk (mem2chunk (newp)));
3022 if (newp == NULL)
3024 /* Try harder to allocate memory in other arenas. */
3025 LIBC_PROBE (memory_realloc_retry, 2, bytes, oldmem);
3026 newp = __libc_malloc (bytes);
3027 if (newp != NULL)
3029 memcpy (newp, oldmem, oldsize - SIZE_SZ);
3030 _int_free (ar_ptr, oldp, 0);
3034 return newp;
3036 libc_hidden_def (__libc_realloc)
3038 void *
3039 __libc_memalign (size_t alignment, size_t bytes)
3041 void *address = RETURN_ADDRESS (0);
3042 return _mid_memalign (alignment, bytes, address);
3045 static void *
3046 _mid_memalign (size_t alignment, size_t bytes, void *address)
3048 mstate ar_ptr;
3049 void *p;
3051 void *(*hook) (size_t, size_t, const void *) =
3052 atomic_forced_read (__memalign_hook);
3053 if (__builtin_expect (hook != NULL, 0))
3054 return (*hook)(alignment, bytes, address);
3056 /* If we need less alignment than we give anyway, just relay to malloc. */
3057 if (alignment <= MALLOC_ALIGNMENT)
3058 return __libc_malloc (bytes);
3060 /* Otherwise, ensure that it is at least a minimum chunk size */
3061 if (alignment < MINSIZE)
3062 alignment = MINSIZE;
3064 /* If the alignment is greater than SIZE_MAX / 2 + 1 it cannot be a
3065 power of 2 and will cause overflow in the check below. */
3066 if (alignment > SIZE_MAX / 2 + 1)
3068 __set_errno (EINVAL);
3069 return 0;
3072 /* Check for overflow. */
3073 if (bytes > SIZE_MAX - alignment - MINSIZE)
3075 __set_errno (ENOMEM);
3076 return 0;
3080 /* Make sure alignment is power of 2. */
3081 if (!powerof2 (alignment))
3083 size_t a = MALLOC_ALIGNMENT * 2;
3084 while (a < alignment)
3085 a <<= 1;
3086 alignment = a;
3089 arena_get (ar_ptr, bytes + alignment + MINSIZE);
3091 p = _int_memalign (ar_ptr, alignment, bytes);
3092 if (!p && ar_ptr != NULL)
3094 LIBC_PROBE (memory_memalign_retry, 2, bytes, alignment);
3095 ar_ptr = arena_get_retry (ar_ptr, bytes);
3096 p = _int_memalign (ar_ptr, alignment, bytes);
3099 if (ar_ptr != NULL)
3100 __libc_lock_unlock (ar_ptr->mutex);
3102 assert (!p || chunk_is_mmapped (mem2chunk (p)) ||
3103 ar_ptr == arena_for_chunk (mem2chunk (p)));
3104 return p;
3106 /* For ISO C11. */
3107 weak_alias (__libc_memalign, aligned_alloc)
3108 libc_hidden_def (__libc_memalign)
3110 void *
3111 __libc_valloc (size_t bytes)
3113 if (__malloc_initialized < 0)
3114 ptmalloc_init ();
3116 void *address = RETURN_ADDRESS (0);
3117 size_t pagesize = GLRO (dl_pagesize);
3118 return _mid_memalign (pagesize, bytes, address);
3121 void *
3122 __libc_pvalloc (size_t bytes)
3124 if (__malloc_initialized < 0)
3125 ptmalloc_init ();
3127 void *address = RETURN_ADDRESS (0);
3128 size_t pagesize = GLRO (dl_pagesize);
3129 size_t rounded_bytes = ALIGN_UP (bytes, pagesize);
3131 /* Check for overflow. */
3132 if (bytes > SIZE_MAX - 2 * pagesize - MINSIZE)
3134 __set_errno (ENOMEM);
3135 return 0;
3138 return _mid_memalign (pagesize, rounded_bytes, address);
3141 void *
3142 __libc_calloc (size_t n, size_t elem_size)
3144 mstate av;
3145 mchunkptr oldtop, p;
3146 INTERNAL_SIZE_T bytes, sz, csz, oldtopsize;
3147 void *mem;
3148 unsigned long clearsize;
3149 unsigned long nclears;
3150 INTERNAL_SIZE_T *d;
3152 /* size_t is unsigned so the behavior on overflow is defined. */
3153 bytes = n * elem_size;
3154 #define HALF_INTERNAL_SIZE_T \
3155 (((INTERNAL_SIZE_T) 1) << (8 * sizeof (INTERNAL_SIZE_T) / 2))
3156 if (__builtin_expect ((n | elem_size) >= HALF_INTERNAL_SIZE_T, 0))
3158 if (elem_size != 0 && bytes / elem_size != n)
3160 __set_errno (ENOMEM);
3161 return 0;
3165 void *(*hook) (size_t, const void *) =
3166 atomic_forced_read (__malloc_hook);
3167 if (__builtin_expect (hook != NULL, 0))
3169 sz = bytes;
3170 mem = (*hook)(sz, RETURN_ADDRESS (0));
3171 if (mem == 0)
3172 return 0;
3174 return memset (mem, 0, sz);
3177 sz = bytes;
3179 arena_get (av, sz);
3180 if (av)
3182 /* Check if we hand out the top chunk, in which case there may be no
3183 need to clear. */
3184 #if MORECORE_CLEARS
3185 oldtop = top (av);
3186 oldtopsize = chunksize (top (av));
3187 # if MORECORE_CLEARS < 2
3188 /* Only newly allocated memory is guaranteed to be cleared. */
3189 if (av == &main_arena &&
3190 oldtopsize < mp_.sbrk_base + av->max_system_mem - (char *) oldtop)
3191 oldtopsize = (mp_.sbrk_base + av->max_system_mem - (char *) oldtop);
3192 # endif
3193 if (av != &main_arena)
3195 heap_info *heap = heap_for_ptr (oldtop);
3196 if (oldtopsize < (char *) heap + heap->mprotect_size - (char *) oldtop)
3197 oldtopsize = (char *) heap + heap->mprotect_size - (char *) oldtop;
3199 #endif
3201 else
3203 /* No usable arenas. */
3204 oldtop = 0;
3205 oldtopsize = 0;
3207 mem = _int_malloc (av, sz);
3210 assert (!mem || chunk_is_mmapped (mem2chunk (mem)) ||
3211 av == arena_for_chunk (mem2chunk (mem)));
3213 if (mem == 0 && av != NULL)
3215 LIBC_PROBE (memory_calloc_retry, 1, sz);
3216 av = arena_get_retry (av, sz);
3217 mem = _int_malloc (av, sz);
3220 if (av != NULL)
3221 __libc_lock_unlock (av->mutex);
3223 /* Allocation failed even after a retry. */
3224 if (mem == 0)
3225 return 0;
3227 p = mem2chunk (mem);
3229 /* Two optional cases in which clearing not necessary */
3230 if (chunk_is_mmapped (p))
3232 if (__builtin_expect (perturb_byte, 0))
3233 return memset (mem, 0, sz);
3235 return mem;
3238 csz = chunksize (p);
3240 #if MORECORE_CLEARS
3241 if (perturb_byte == 0 && (p == oldtop && csz > oldtopsize))
3243 /* clear only the bytes from non-freshly-sbrked memory */
3244 csz = oldtopsize;
3246 #endif
3248 /* Unroll clear of <= 36 bytes (72 if 8byte sizes). We know that
3249 contents have an odd number of INTERNAL_SIZE_T-sized words;
3250 minimally 3. */
3251 d = (INTERNAL_SIZE_T *) mem;
3252 clearsize = csz - SIZE_SZ;
3253 nclears = clearsize / sizeof (INTERNAL_SIZE_T);
3254 assert (nclears >= 3);
3256 if (nclears > 9)
3257 return memset (d, 0, clearsize);
3259 else
3261 *(d + 0) = 0;
3262 *(d + 1) = 0;
3263 *(d + 2) = 0;
3264 if (nclears > 4)
3266 *(d + 3) = 0;
3267 *(d + 4) = 0;
3268 if (nclears > 6)
3270 *(d + 5) = 0;
3271 *(d + 6) = 0;
3272 if (nclears > 8)
3274 *(d + 7) = 0;
3275 *(d + 8) = 0;
3281 return mem;
3285 ------------------------------ malloc ------------------------------
3288 static void *
3289 _int_malloc (mstate av, size_t bytes)
3291 INTERNAL_SIZE_T nb; /* normalized request size */
3292 unsigned int idx; /* associated bin index */
3293 mbinptr bin; /* associated bin */
3295 mchunkptr victim; /* inspected/selected chunk */
3296 INTERNAL_SIZE_T size; /* its size */
3297 int victim_index; /* its bin index */
3299 mchunkptr remainder; /* remainder from a split */
3300 unsigned long remainder_size; /* its size */
3302 unsigned int block; /* bit map traverser */
3303 unsigned int bit; /* bit map traverser */
3304 unsigned int map; /* current word of binmap */
3306 mchunkptr fwd; /* misc temp for linking */
3307 mchunkptr bck; /* misc temp for linking */
3309 const char *errstr = NULL;
3312 Convert request size to internal form by adding SIZE_SZ bytes
3313 overhead plus possibly more to obtain necessary alignment and/or
3314 to obtain a size of at least MINSIZE, the smallest allocatable
3315 size. Also, checked_request2size traps (returning 0) request sizes
3316 that are so large that they wrap around zero when padded and
3317 aligned.
3320 checked_request2size (bytes, nb);
3322 /* There are no usable arenas. Fall back to sysmalloc to get a chunk from
3323 mmap. */
3324 if (__glibc_unlikely (av == NULL))
3326 void *p = sysmalloc (nb, av);
3327 if (p != NULL)
3328 alloc_perturb (p, bytes);
3329 return p;
3333 If the size qualifies as a fastbin, first check corresponding bin.
3334 This code is safe to execute even if av is not yet initialized, so we
3335 can try it without checking, which saves some time on this fast path.
3338 if ((unsigned long) (nb) <= (unsigned long) (get_max_fast ()))
3340 idx = fastbin_index (nb);
3341 mfastbinptr *fb = &fastbin (av, idx);
3342 mchunkptr pp = *fb;
3345 victim = pp;
3346 if (victim == NULL)
3347 break;
3349 while ((pp = catomic_compare_and_exchange_val_acq (fb, victim->fd, victim))
3350 != victim);
3351 if (victim != 0)
3353 if (__builtin_expect (fastbin_index (chunksize (victim)) != idx, 0))
3355 errstr = "malloc(): memory corruption (fast)";
3356 errout:
3357 malloc_printerr (check_action, errstr, chunk2mem (victim), av);
3358 return NULL;
3360 check_remalloced_chunk (av, victim, nb);
3361 void *p = chunk2mem (victim);
3362 alloc_perturb (p, bytes);
3363 return p;
3368 If a small request, check regular bin. Since these "smallbins"
3369 hold one size each, no searching within bins is necessary.
3370 (For a large request, we need to wait until unsorted chunks are
3371 processed to find best fit. But for small ones, fits are exact
3372 anyway, so we can check now, which is faster.)
3375 if (in_smallbin_range (nb))
3377 idx = smallbin_index (nb);
3378 bin = bin_at (av, idx);
3380 if ((victim = last (bin)) != bin)
3382 if (victim == 0) /* initialization check */
3383 malloc_consolidate (av);
3384 else
3386 bck = victim->bk;
3387 if (__glibc_unlikely (bck->fd != victim))
3389 errstr = "malloc(): smallbin double linked list corrupted";
3390 goto errout;
3392 set_inuse_bit_at_offset (victim, nb);
3393 bin->bk = bck;
3394 bck->fd = bin;
3396 if (av != &main_arena)
3397 victim->size |= NON_MAIN_ARENA;
3398 check_malloced_chunk (av, victim, nb);
3399 void *p = chunk2mem (victim);
3400 alloc_perturb (p, bytes);
3401 return p;
3407 If this is a large request, consolidate fastbins before continuing.
3408 While it might look excessive to kill all fastbins before
3409 even seeing if there is space available, this avoids
3410 fragmentation problems normally associated with fastbins.
3411 Also, in practice, programs tend to have runs of either small or
3412 large requests, but less often mixtures, so consolidation is not
3413 invoked all that often in most programs. And the programs that
3414 it is called frequently in otherwise tend to fragment.
3417 else
3419 idx = largebin_index (nb);
3420 if (have_fastchunks (av))
3421 malloc_consolidate (av);
3425 Process recently freed or remaindered chunks, taking one only if
3426 it is exact fit, or, if this a small request, the chunk is remainder from
3427 the most recent non-exact fit. Place other traversed chunks in
3428 bins. Note that this step is the only place in any routine where
3429 chunks are placed in bins.
3431 The outer loop here is needed because we might not realize until
3432 near the end of malloc that we should have consolidated, so must
3433 do so and retry. This happens at most once, and only when we would
3434 otherwise need to expand memory to service a "small" request.
3437 for (;; )
3439 int iters = 0;
3440 while ((victim = unsorted_chunks (av)->bk) != unsorted_chunks (av))
3442 bck = victim->bk;
3443 if (__builtin_expect (victim->size <= 2 * SIZE_SZ, 0)
3444 || __builtin_expect (victim->size > av->system_mem, 0))
3445 malloc_printerr (check_action, "malloc(): memory corruption",
3446 chunk2mem (victim), av);
3447 size = chunksize (victim);
3450 If a small request, try to use last remainder if it is the
3451 only chunk in unsorted bin. This helps promote locality for
3452 runs of consecutive small requests. This is the only
3453 exception to best-fit, and applies only when there is
3454 no exact fit for a small chunk.
3457 if (in_smallbin_range (nb) &&
3458 bck == unsorted_chunks (av) &&
3459 victim == av->last_remainder &&
3460 (unsigned long) (size) > (unsigned long) (nb + MINSIZE))
3462 /* split and reattach remainder */
3463 remainder_size = size - nb;
3464 remainder = chunk_at_offset (victim, nb);
3465 unsorted_chunks (av)->bk = unsorted_chunks (av)->fd = remainder;
3466 av->last_remainder = remainder;
3467 remainder->bk = remainder->fd = unsorted_chunks (av);
3468 if (!in_smallbin_range (remainder_size))
3470 remainder->fd_nextsize = NULL;
3471 remainder->bk_nextsize = NULL;
3474 set_head (victim, nb | PREV_INUSE |
3475 (av != &main_arena ? NON_MAIN_ARENA : 0));
3476 set_head (remainder, remainder_size | PREV_INUSE);
3477 set_foot (remainder, remainder_size);
3479 check_malloced_chunk (av, victim, nb);
3480 void *p = chunk2mem (victim);
3481 alloc_perturb (p, bytes);
3482 return p;
3485 /* remove from unsorted list */
3486 unsorted_chunks (av)->bk = bck;
3487 bck->fd = unsorted_chunks (av);
3489 /* Take now instead of binning if exact fit */
3491 if (size == nb)
3493 set_inuse_bit_at_offset (victim, size);
3494 if (av != &main_arena)
3495 victim->size |= NON_MAIN_ARENA;
3496 check_malloced_chunk (av, victim, nb);
3497 void *p = chunk2mem (victim);
3498 alloc_perturb (p, bytes);
3499 return p;
3502 /* place chunk in bin */
3504 if (in_smallbin_range (size))
3506 victim_index = smallbin_index (size);
3507 bck = bin_at (av, victim_index);
3508 fwd = bck->fd;
3510 else
3512 victim_index = largebin_index (size);
3513 bck = bin_at (av, victim_index);
3514 fwd = bck->fd;
3516 /* maintain large bins in sorted order */
3517 if (fwd != bck)
3519 /* Or with inuse bit to speed comparisons */
3520 size |= PREV_INUSE;
3521 /* if smaller than smallest, bypass loop below */
3522 assert ((bck->bk->size & NON_MAIN_ARENA) == 0);
3523 if ((unsigned long) (size) < (unsigned long) (bck->bk->size))
3525 fwd = bck;
3526 bck = bck->bk;
3528 victim->fd_nextsize = fwd->fd;
3529 victim->bk_nextsize = fwd->fd->bk_nextsize;
3530 fwd->fd->bk_nextsize = victim->bk_nextsize->fd_nextsize = victim;
3532 else
3534 assert ((fwd->size & NON_MAIN_ARENA) == 0);
3535 while ((unsigned long) size < fwd->size)
3537 fwd = fwd->fd_nextsize;
3538 assert ((fwd->size & NON_MAIN_ARENA) == 0);
3541 if ((unsigned long) size == (unsigned long) fwd->size)
3542 /* Always insert in the second position. */
3543 fwd = fwd->fd;
3544 else
3546 victim->fd_nextsize = fwd;
3547 victim->bk_nextsize = fwd->bk_nextsize;
3548 fwd->bk_nextsize = victim;
3549 victim->bk_nextsize->fd_nextsize = victim;
3551 bck = fwd->bk;
3554 else
3555 victim->fd_nextsize = victim->bk_nextsize = victim;
3558 mark_bin (av, victim_index);
3559 victim->bk = bck;
3560 victim->fd = fwd;
3561 fwd->bk = victim;
3562 bck->fd = victim;
3564 #define MAX_ITERS 10000
3565 if (++iters >= MAX_ITERS)
3566 break;
3570 If a large request, scan through the chunks of current bin in
3571 sorted order to find smallest that fits. Use the skip list for this.
3574 if (!in_smallbin_range (nb))
3576 bin = bin_at (av, idx);
3578 /* skip scan if empty or largest chunk is too small */
3579 if ((victim = first (bin)) != bin &&
3580 (unsigned long) (victim->size) >= (unsigned long) (nb))
3582 victim = victim->bk_nextsize;
3583 while (((unsigned long) (size = chunksize (victim)) <
3584 (unsigned long) (nb)))
3585 victim = victim->bk_nextsize;
3587 /* Avoid removing the first entry for a size so that the skip
3588 list does not have to be rerouted. */
3589 if (victim != last (bin) && victim->size == victim->fd->size)
3590 victim = victim->fd;
3592 remainder_size = size - nb;
3593 unlink (av, victim, bck, fwd);
3595 /* Exhaust */
3596 if (remainder_size < MINSIZE)
3598 set_inuse_bit_at_offset (victim, size);
3599 if (av != &main_arena)
3600 victim->size |= NON_MAIN_ARENA;
3602 /* Split */
3603 else
3605 remainder = chunk_at_offset (victim, nb);
3606 /* We cannot assume the unsorted list is empty and therefore
3607 have to perform a complete insert here. */
3608 bck = unsorted_chunks (av);
3609 fwd = bck->fd;
3610 if (__glibc_unlikely (fwd->bk != bck))
3612 errstr = "malloc(): corrupted unsorted chunks";
3613 goto errout;
3615 remainder->bk = bck;
3616 remainder->fd = fwd;
3617 bck->fd = remainder;
3618 fwd->bk = remainder;
3619 if (!in_smallbin_range (remainder_size))
3621 remainder->fd_nextsize = NULL;
3622 remainder->bk_nextsize = NULL;
3624 set_head (victim, nb | PREV_INUSE |
3625 (av != &main_arena ? NON_MAIN_ARENA : 0));
3626 set_head (remainder, remainder_size | PREV_INUSE);
3627 set_foot (remainder, remainder_size);
3629 check_malloced_chunk (av, victim, nb);
3630 void *p = chunk2mem (victim);
3631 alloc_perturb (p, bytes);
3632 return p;
3637 Search for a chunk by scanning bins, starting with next largest
3638 bin. This search is strictly by best-fit; i.e., the smallest
3639 (with ties going to approximately the least recently used) chunk
3640 that fits is selected.
3642 The bitmap avoids needing to check that most blocks are nonempty.
3643 The particular case of skipping all bins during warm-up phases
3644 when no chunks have been returned yet is faster than it might look.
3647 ++idx;
3648 bin = bin_at (av, idx);
3649 block = idx2block (idx);
3650 map = av->binmap[block];
3651 bit = idx2bit (idx);
3653 for (;; )
3655 /* Skip rest of block if there are no more set bits in this block. */
3656 if (bit > map || bit == 0)
3660 if (++block >= BINMAPSIZE) /* out of bins */
3661 goto use_top;
3663 while ((map = av->binmap[block]) == 0);
3665 bin = bin_at (av, (block << BINMAPSHIFT));
3666 bit = 1;
3669 /* Advance to bin with set bit. There must be one. */
3670 while ((bit & map) == 0)
3672 bin = next_bin (bin);
3673 bit <<= 1;
3674 assert (bit != 0);
3677 /* Inspect the bin. It is likely to be non-empty */
3678 victim = last (bin);
3680 /* If a false alarm (empty bin), clear the bit. */
3681 if (victim == bin)
3683 av->binmap[block] = map &= ~bit; /* Write through */
3684 bin = next_bin (bin);
3685 bit <<= 1;
3688 else
3690 size = chunksize (victim);
3692 /* We know the first chunk in this bin is big enough to use. */
3693 assert ((unsigned long) (size) >= (unsigned long) (nb));
3695 remainder_size = size - nb;
3697 /* unlink */
3698 unlink (av, victim, bck, fwd);
3700 /* Exhaust */
3701 if (remainder_size < MINSIZE)
3703 set_inuse_bit_at_offset (victim, size);
3704 if (av != &main_arena)
3705 victim->size |= NON_MAIN_ARENA;
3708 /* Split */
3709 else
3711 remainder = chunk_at_offset (victim, nb);
3713 /* We cannot assume the unsorted list is empty and therefore
3714 have to perform a complete insert here. */
3715 bck = unsorted_chunks (av);
3716 fwd = bck->fd;
3717 if (__glibc_unlikely (fwd->bk != bck))
3719 errstr = "malloc(): corrupted unsorted chunks 2";
3720 goto errout;
3722 remainder->bk = bck;
3723 remainder->fd = fwd;
3724 bck->fd = remainder;
3725 fwd->bk = remainder;
3727 /* advertise as last remainder */
3728 if (in_smallbin_range (nb))
3729 av->last_remainder = remainder;
3730 if (!in_smallbin_range (remainder_size))
3732 remainder->fd_nextsize = NULL;
3733 remainder->bk_nextsize = NULL;
3735 set_head (victim, nb | PREV_INUSE |
3736 (av != &main_arena ? NON_MAIN_ARENA : 0));
3737 set_head (remainder, remainder_size | PREV_INUSE);
3738 set_foot (remainder, remainder_size);
3740 check_malloced_chunk (av, victim, nb);
3741 void *p = chunk2mem (victim);
3742 alloc_perturb (p, bytes);
3743 return p;
3747 use_top:
3749 If large enough, split off the chunk bordering the end of memory
3750 (held in av->top). Note that this is in accord with the best-fit
3751 search rule. In effect, av->top is treated as larger (and thus
3752 less well fitting) than any other available chunk since it can
3753 be extended to be as large as necessary (up to system
3754 limitations).
3756 We require that av->top always exists (i.e., has size >=
3757 MINSIZE) after initialization, so if it would otherwise be
3758 exhausted by current request, it is replenished. (The main
3759 reason for ensuring it exists is that we may need MINSIZE space
3760 to put in fenceposts in sysmalloc.)
3763 victim = av->top;
3764 size = chunksize (victim);
3766 if ((unsigned long) (size) >= (unsigned long) (nb + MINSIZE))
3768 remainder_size = size - nb;
3769 remainder = chunk_at_offset (victim, nb);
3770 av->top = remainder;
3771 set_head (victim, nb | PREV_INUSE |
3772 (av != &main_arena ? NON_MAIN_ARENA : 0));
3773 set_head (remainder, remainder_size | PREV_INUSE);
3775 check_malloced_chunk (av, victim, nb);
3776 void *p = chunk2mem (victim);
3777 alloc_perturb (p, bytes);
3778 return p;
3781 /* When we are using atomic ops to free fast chunks we can get
3782 here for all block sizes. */
3783 else if (have_fastchunks (av))
3785 malloc_consolidate (av);
3786 /* restore original bin index */
3787 if (in_smallbin_range (nb))
3788 idx = smallbin_index (nb);
3789 else
3790 idx = largebin_index (nb);
3794 Otherwise, relay to handle system-dependent cases
3796 else
3798 void *p = sysmalloc (nb, av);
3799 if (p != NULL)
3800 alloc_perturb (p, bytes);
3801 return p;
3807 ------------------------------ free ------------------------------
3810 static void
3811 _int_free (mstate av, mchunkptr p, int have_lock)
3813 INTERNAL_SIZE_T size; /* its size */
3814 mfastbinptr *fb; /* associated fastbin */
3815 mchunkptr nextchunk; /* next contiguous chunk */
3816 INTERNAL_SIZE_T nextsize; /* its size */
3817 int nextinuse; /* true if nextchunk is used */
3818 INTERNAL_SIZE_T prevsize; /* size of previous contiguous chunk */
3819 mchunkptr bck; /* misc temp for linking */
3820 mchunkptr fwd; /* misc temp for linking */
3822 const char *errstr = NULL;
3823 int locked = 0;
3825 size = chunksize (p);
3827 /* Little security check which won't hurt performance: the
3828 allocator never wrapps around at the end of the address space.
3829 Therefore we can exclude some size values which might appear
3830 here by accident or by "design" from some intruder. */
3831 if (__builtin_expect ((uintptr_t) p > (uintptr_t) -size, 0)
3832 || __builtin_expect (misaligned_chunk (p), 0))
3834 errstr = "free(): invalid pointer";
3835 errout:
3836 if (!have_lock && locked)
3837 __libc_lock_unlock (av->mutex);
3838 malloc_printerr (check_action, errstr, chunk2mem (p), av);
3839 return;
3841 /* We know that each chunk is at least MINSIZE bytes in size or a
3842 multiple of MALLOC_ALIGNMENT. */
3843 if (__glibc_unlikely (size < MINSIZE || !aligned_OK (size)))
3845 errstr = "free(): invalid size";
3846 goto errout;
3849 check_inuse_chunk(av, p);
3852 If eligible, place chunk on a fastbin so it can be found
3853 and used quickly in malloc.
3856 if ((unsigned long)(size) <= (unsigned long)(get_max_fast ())
3858 #if TRIM_FASTBINS
3860 If TRIM_FASTBINS set, don't place chunks
3861 bordering top into fastbins
3863 && (chunk_at_offset(p, size) != av->top)
3864 #endif
3867 if (__builtin_expect (chunk_at_offset (p, size)->size <= 2 * SIZE_SZ, 0)
3868 || __builtin_expect (chunksize (chunk_at_offset (p, size))
3869 >= av->system_mem, 0))
3871 /* We might not have a lock at this point and concurrent modifications
3872 of system_mem might have let to a false positive. Redo the test
3873 after getting the lock. */
3874 if (have_lock
3875 || ({ assert (locked == 0);
3876 __libc_lock_lock (av->mutex);
3877 locked = 1;
3878 chunk_at_offset (p, size)->size <= 2 * SIZE_SZ
3879 || chunksize (chunk_at_offset (p, size)) >= av->system_mem;
3882 errstr = "free(): invalid next size (fast)";
3883 goto errout;
3885 if (! have_lock)
3887 __libc_lock_unlock (av->mutex);
3888 locked = 0;
3892 free_perturb (chunk2mem(p), size - 2 * SIZE_SZ);
3894 set_fastchunks(av);
3895 unsigned int idx = fastbin_index(size);
3896 fb = &fastbin (av, idx);
3898 /* Atomically link P to its fastbin: P->FD = *FB; *FB = P; */
3899 mchunkptr old = *fb, old2;
3900 unsigned int old_idx = ~0u;
3903 /* Check that the top of the bin is not the record we are going to add
3904 (i.e., double free). */
3905 if (__builtin_expect (old == p, 0))
3907 errstr = "double free or corruption (fasttop)";
3908 goto errout;
3910 /* Check that size of fastbin chunk at the top is the same as
3911 size of the chunk that we are adding. We can dereference OLD
3912 only if we have the lock, otherwise it might have already been
3913 deallocated. See use of OLD_IDX below for the actual check. */
3914 if (have_lock && old != NULL)
3915 old_idx = fastbin_index(chunksize(old));
3916 p->fd = old2 = old;
3918 while ((old = catomic_compare_and_exchange_val_rel (fb, p, old2)) != old2);
3920 if (have_lock && old != NULL && __builtin_expect (old_idx != idx, 0))
3922 errstr = "invalid fastbin entry (free)";
3923 goto errout;
3928 Consolidate other non-mmapped chunks as they arrive.
3931 else if (!chunk_is_mmapped(p)) {
3932 if (! have_lock) {
3933 __libc_lock_lock (av->mutex);
3934 locked = 1;
3937 nextchunk = chunk_at_offset(p, size);
3939 /* Lightweight tests: check whether the block is already the
3940 top block. */
3941 if (__glibc_unlikely (p == av->top))
3943 errstr = "double free or corruption (top)";
3944 goto errout;
3946 /* Or whether the next chunk is beyond the boundaries of the arena. */
3947 if (__builtin_expect (contiguous (av)
3948 && (char *) nextchunk
3949 >= ((char *) av->top + chunksize(av->top)), 0))
3951 errstr = "double free or corruption (out)";
3952 goto errout;
3954 /* Or whether the block is actually not marked used. */
3955 if (__glibc_unlikely (!prev_inuse(nextchunk)))
3957 errstr = "double free or corruption (!prev)";
3958 goto errout;
3961 nextsize = chunksize(nextchunk);
3962 if (__builtin_expect (nextchunk->size <= 2 * SIZE_SZ, 0)
3963 || __builtin_expect (nextsize >= av->system_mem, 0))
3965 errstr = "free(): invalid next size (normal)";
3966 goto errout;
3969 free_perturb (chunk2mem(p), size - 2 * SIZE_SZ);
3971 /* consolidate backward */
3972 if (!prev_inuse(p)) {
3973 prevsize = p->prev_size;
3974 size += prevsize;
3975 p = chunk_at_offset(p, -((long) prevsize));
3976 unlink(av, p, bck, fwd);
3979 if (nextchunk != av->top) {
3980 /* get and clear inuse bit */
3981 nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
3983 /* consolidate forward */
3984 if (!nextinuse) {
3985 unlink(av, nextchunk, bck, fwd);
3986 size += nextsize;
3987 } else
3988 clear_inuse_bit_at_offset(nextchunk, 0);
3991 Place the chunk in unsorted chunk list. Chunks are
3992 not placed into regular bins until after they have
3993 been given one chance to be used in malloc.
3996 bck = unsorted_chunks(av);
3997 fwd = bck->fd;
3998 if (__glibc_unlikely (fwd->bk != bck))
4000 errstr = "free(): corrupted unsorted chunks";
4001 goto errout;
4003 p->fd = fwd;
4004 p->bk = bck;
4005 if (!in_smallbin_range(size))
4007 p->fd_nextsize = NULL;
4008 p->bk_nextsize = NULL;
4010 bck->fd = p;
4011 fwd->bk = p;
4013 set_head(p, size | PREV_INUSE);
4014 set_foot(p, size);
4016 check_free_chunk(av, p);
4020 If the chunk borders the current high end of memory,
4021 consolidate into top
4024 else {
4025 size += nextsize;
4026 set_head(p, size | PREV_INUSE);
4027 av->top = p;
4028 check_chunk(av, p);
4032 If freeing a large space, consolidate possibly-surrounding
4033 chunks. Then, if the total unused topmost memory exceeds trim
4034 threshold, ask malloc_trim to reduce top.
4036 Unless max_fast is 0, we don't know if there are fastbins
4037 bordering top, so we cannot tell for sure whether threshold
4038 has been reached unless fastbins are consolidated. But we
4039 don't want to consolidate on each free. As a compromise,
4040 consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
4041 is reached.
4044 if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) {
4045 if (have_fastchunks(av))
4046 malloc_consolidate(av);
4048 if (av == &main_arena) {
4049 #ifndef MORECORE_CANNOT_TRIM
4050 if ((unsigned long)(chunksize(av->top)) >=
4051 (unsigned long)(mp_.trim_threshold))
4052 systrim(mp_.top_pad, av);
4053 #endif
4054 } else {
4055 /* Always try heap_trim(), even if the top chunk is not
4056 large, because the corresponding heap might go away. */
4057 heap_info *heap = heap_for_ptr(top(av));
4059 assert(heap->ar_ptr == av);
4060 heap_trim(heap, mp_.top_pad);
4064 if (! have_lock) {
4065 assert (locked);
4066 __libc_lock_unlock (av->mutex);
4070 If the chunk was allocated via mmap, release via munmap().
4073 else {
4074 munmap_chunk (p);
4079 ------------------------- malloc_consolidate -------------------------
4081 malloc_consolidate is a specialized version of free() that tears
4082 down chunks held in fastbins. Free itself cannot be used for this
4083 purpose since, among other things, it might place chunks back onto
4084 fastbins. So, instead, we need to use a minor variant of the same
4085 code.
4087 Also, because this routine needs to be called the first time through
4088 malloc anyway, it turns out to be the perfect place to trigger
4089 initialization code.
4092 static void malloc_consolidate(mstate av)
4094 mfastbinptr* fb; /* current fastbin being consolidated */
4095 mfastbinptr* maxfb; /* last fastbin (for loop control) */
4096 mchunkptr p; /* current chunk being consolidated */
4097 mchunkptr nextp; /* next chunk to consolidate */
4098 mchunkptr unsorted_bin; /* bin header */
4099 mchunkptr first_unsorted; /* chunk to link to */
4101 /* These have same use as in free() */
4102 mchunkptr nextchunk;
4103 INTERNAL_SIZE_T size;
4104 INTERNAL_SIZE_T nextsize;
4105 INTERNAL_SIZE_T prevsize;
4106 int nextinuse;
4107 mchunkptr bck;
4108 mchunkptr fwd;
4111 If max_fast is 0, we know that av hasn't
4112 yet been initialized, in which case do so below
4115 if (get_max_fast () != 0) {
4116 clear_fastchunks(av);
4118 unsorted_bin = unsorted_chunks(av);
4121 Remove each chunk from fast bin and consolidate it, placing it
4122 then in unsorted bin. Among other reasons for doing this,
4123 placing in unsorted bin avoids needing to calculate actual bins
4124 until malloc is sure that chunks aren't immediately going to be
4125 reused anyway.
4128 maxfb = &fastbin (av, NFASTBINS - 1);
4129 fb = &fastbin (av, 0);
4130 do {
4131 p = atomic_exchange_acq (fb, NULL);
4132 if (p != 0) {
4133 do {
4134 check_inuse_chunk(av, p);
4135 nextp = p->fd;
4137 /* Slightly streamlined version of consolidation code in free() */
4138 size = p->size & ~(PREV_INUSE|NON_MAIN_ARENA);
4139 nextchunk = chunk_at_offset(p, size);
4140 nextsize = chunksize(nextchunk);
4142 if (!prev_inuse(p)) {
4143 prevsize = p->prev_size;
4144 size += prevsize;
4145 p = chunk_at_offset(p, -((long) prevsize));
4146 unlink(av, p, bck, fwd);
4149 if (nextchunk != av->top) {
4150 nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
4152 if (!nextinuse) {
4153 size += nextsize;
4154 unlink(av, nextchunk, bck, fwd);
4155 } else
4156 clear_inuse_bit_at_offset(nextchunk, 0);
4158 first_unsorted = unsorted_bin->fd;
4159 unsorted_bin->fd = p;
4160 first_unsorted->bk = p;
4162 if (!in_smallbin_range (size)) {
4163 p->fd_nextsize = NULL;
4164 p->bk_nextsize = NULL;
4167 set_head(p, size | PREV_INUSE);
4168 p->bk = unsorted_bin;
4169 p->fd = first_unsorted;
4170 set_foot(p, size);
4173 else {
4174 size += nextsize;
4175 set_head(p, size | PREV_INUSE);
4176 av->top = p;
4179 } while ( (p = nextp) != 0);
4182 } while (fb++ != maxfb);
4184 else {
4185 malloc_init_state(av);
4186 check_malloc_state(av);
4191 ------------------------------ realloc ------------------------------
4194 void*
4195 _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
4196 INTERNAL_SIZE_T nb)
4198 mchunkptr newp; /* chunk to return */
4199 INTERNAL_SIZE_T newsize; /* its size */
4200 void* newmem; /* corresponding user mem */
4202 mchunkptr next; /* next contiguous chunk after oldp */
4204 mchunkptr remainder; /* extra space at end of newp */
4205 unsigned long remainder_size; /* its size */
4207 mchunkptr bck; /* misc temp for linking */
4208 mchunkptr fwd; /* misc temp for linking */
4210 unsigned long copysize; /* bytes to copy */
4211 unsigned int ncopies; /* INTERNAL_SIZE_T words to copy */
4212 INTERNAL_SIZE_T* s; /* copy source */
4213 INTERNAL_SIZE_T* d; /* copy destination */
4215 const char *errstr = NULL;
4217 /* oldmem size */
4218 if (__builtin_expect (oldp->size <= 2 * SIZE_SZ, 0)
4219 || __builtin_expect (oldsize >= av->system_mem, 0))
4221 errstr = "realloc(): invalid old size";
4222 errout:
4223 malloc_printerr (check_action, errstr, chunk2mem (oldp), av);
4224 return NULL;
4227 check_inuse_chunk (av, oldp);
4229 /* All callers already filter out mmap'ed chunks. */
4230 assert (!chunk_is_mmapped (oldp));
4232 next = chunk_at_offset (oldp, oldsize);
4233 INTERNAL_SIZE_T nextsize = chunksize (next);
4234 if (__builtin_expect (next->size <= 2 * SIZE_SZ, 0)
4235 || __builtin_expect (nextsize >= av->system_mem, 0))
4237 errstr = "realloc(): invalid next size";
4238 goto errout;
4241 if ((unsigned long) (oldsize) >= (unsigned long) (nb))
4243 /* already big enough; split below */
4244 newp = oldp;
4245 newsize = oldsize;
4248 else
4250 /* Try to expand forward into top */
4251 if (next == av->top &&
4252 (unsigned long) (newsize = oldsize + nextsize) >=
4253 (unsigned long) (nb + MINSIZE))
4255 set_head_size (oldp, nb | (av != &main_arena ? NON_MAIN_ARENA : 0));
4256 av->top = chunk_at_offset (oldp, nb);
4257 set_head (av->top, (newsize - nb) | PREV_INUSE);
4258 check_inuse_chunk (av, oldp);
4259 return chunk2mem (oldp);
4262 /* Try to expand forward into next chunk; split off remainder below */
4263 else if (next != av->top &&
4264 !inuse (next) &&
4265 (unsigned long) (newsize = oldsize + nextsize) >=
4266 (unsigned long) (nb))
4268 newp = oldp;
4269 unlink (av, next, bck, fwd);
4272 /* allocate, copy, free */
4273 else
4275 newmem = _int_malloc (av, nb - MALLOC_ALIGN_MASK);
4276 if (newmem == 0)
4277 return 0; /* propagate failure */
4279 newp = mem2chunk (newmem);
4280 newsize = chunksize (newp);
4283 Avoid copy if newp is next chunk after oldp.
4285 if (newp == next)
4287 newsize += oldsize;
4288 newp = oldp;
4290 else
4293 Unroll copy of <= 36 bytes (72 if 8byte sizes)
4294 We know that contents have an odd number of
4295 INTERNAL_SIZE_T-sized words; minimally 3.
4298 copysize = oldsize - SIZE_SZ;
4299 s = (INTERNAL_SIZE_T *) (chunk2mem (oldp));
4300 d = (INTERNAL_SIZE_T *) (newmem);
4301 ncopies = copysize / sizeof (INTERNAL_SIZE_T);
4302 assert (ncopies >= 3);
4304 if (ncopies > 9)
4305 memcpy (d, s, copysize);
4307 else
4309 *(d + 0) = *(s + 0);
4310 *(d + 1) = *(s + 1);
4311 *(d + 2) = *(s + 2);
4312 if (ncopies > 4)
4314 *(d + 3) = *(s + 3);
4315 *(d + 4) = *(s + 4);
4316 if (ncopies > 6)
4318 *(d + 5) = *(s + 5);
4319 *(d + 6) = *(s + 6);
4320 if (ncopies > 8)
4322 *(d + 7) = *(s + 7);
4323 *(d + 8) = *(s + 8);
4329 _int_free (av, oldp, 1);
4330 check_inuse_chunk (av, newp);
4331 return chunk2mem (newp);
4336 /* If possible, free extra space in old or extended chunk */
4338 assert ((unsigned long) (newsize) >= (unsigned long) (nb));
4340 remainder_size = newsize - nb;
4342 if (remainder_size < MINSIZE) /* not enough extra to split off */
4344 set_head_size (newp, newsize | (av != &main_arena ? NON_MAIN_ARENA : 0));
4345 set_inuse_bit_at_offset (newp, newsize);
4347 else /* split remainder */
4349 remainder = chunk_at_offset (newp, nb);
4350 set_head_size (newp, nb | (av != &main_arena ? NON_MAIN_ARENA : 0));
4351 set_head (remainder, remainder_size | PREV_INUSE |
4352 (av != &main_arena ? NON_MAIN_ARENA : 0));
4353 /* Mark remainder as inuse so free() won't complain */
4354 set_inuse_bit_at_offset (remainder, remainder_size);
4355 _int_free (av, remainder, 1);
4358 check_inuse_chunk (av, newp);
4359 return chunk2mem (newp);
4363 ------------------------------ memalign ------------------------------
4366 static void *
4367 _int_memalign (mstate av, size_t alignment, size_t bytes)
4369 INTERNAL_SIZE_T nb; /* padded request size */
4370 char *m; /* memory returned by malloc call */
4371 mchunkptr p; /* corresponding chunk */
4372 char *brk; /* alignment point within p */
4373 mchunkptr newp; /* chunk to return */
4374 INTERNAL_SIZE_T newsize; /* its size */
4375 INTERNAL_SIZE_T leadsize; /* leading space before alignment point */
4376 mchunkptr remainder; /* spare room at end to split off */
4377 unsigned long remainder_size; /* its size */
4378 INTERNAL_SIZE_T size;
4382 checked_request2size (bytes, nb);
4385 Strategy: find a spot within that chunk that meets the alignment
4386 request, and then possibly free the leading and trailing space.
4390 /* Call malloc with worst case padding to hit alignment. */
4392 m = (char *) (_int_malloc (av, nb + alignment + MINSIZE));
4394 if (m == 0)
4395 return 0; /* propagate failure */
4397 p = mem2chunk (m);
4399 if ((((unsigned long) (m)) % alignment) != 0) /* misaligned */
4401 { /*
4402 Find an aligned spot inside chunk. Since we need to give back
4403 leading space in a chunk of at least MINSIZE, if the first
4404 calculation places us at a spot with less than MINSIZE leader,
4405 we can move to the next aligned spot -- we've allocated enough
4406 total room so that this is always possible.
4408 brk = (char *) mem2chunk (((unsigned long) (m + alignment - 1)) &
4409 - ((signed long) alignment));
4410 if ((unsigned long) (brk - (char *) (p)) < MINSIZE)
4411 brk += alignment;
4413 newp = (mchunkptr) brk;
4414 leadsize = brk - (char *) (p);
4415 newsize = chunksize (p) - leadsize;
4417 /* For mmapped chunks, just adjust offset */
4418 if (chunk_is_mmapped (p))
4420 newp->prev_size = p->prev_size + leadsize;
4421 set_head (newp, newsize | IS_MMAPPED);
4422 return chunk2mem (newp);
4425 /* Otherwise, give back leader, use the rest */
4426 set_head (newp, newsize | PREV_INUSE |
4427 (av != &main_arena ? NON_MAIN_ARENA : 0));
4428 set_inuse_bit_at_offset (newp, newsize);
4429 set_head_size (p, leadsize | (av != &main_arena ? NON_MAIN_ARENA : 0));
4430 _int_free (av, p, 1);
4431 p = newp;
4433 assert (newsize >= nb &&
4434 (((unsigned long) (chunk2mem (p))) % alignment) == 0);
4437 /* Also give back spare room at the end */
4438 if (!chunk_is_mmapped (p))
4440 size = chunksize (p);
4441 if ((unsigned long) (size) > (unsigned long) (nb + MINSIZE))
4443 remainder_size = size - nb;
4444 remainder = chunk_at_offset (p, nb);
4445 set_head (remainder, remainder_size | PREV_INUSE |
4446 (av != &main_arena ? NON_MAIN_ARENA : 0));
4447 set_head_size (p, nb);
4448 _int_free (av, remainder, 1);
4452 check_inuse_chunk (av, p);
4453 return chunk2mem (p);
4458 ------------------------------ malloc_trim ------------------------------
4461 static int
4462 mtrim (mstate av, size_t pad)
4464 /* Don't touch corrupt arenas. */
4465 if (arena_is_corrupt (av))
4466 return 0;
4468 /* Ensure initialization/consolidation */
4469 malloc_consolidate (av);
4471 const size_t ps = GLRO (dl_pagesize);
4472 int psindex = bin_index (ps);
4473 const size_t psm1 = ps - 1;
4475 int result = 0;
4476 for (int i = 1; i < NBINS; ++i)
4477 if (i == 1 || i >= psindex)
4479 mbinptr bin = bin_at (av, i);
4481 for (mchunkptr p = last (bin); p != bin; p = p->bk)
4483 INTERNAL_SIZE_T size = chunksize (p);
4485 if (size > psm1 + sizeof (struct malloc_chunk))
4487 /* See whether the chunk contains at least one unused page. */
4488 char *paligned_mem = (char *) (((uintptr_t) p
4489 + sizeof (struct malloc_chunk)
4490 + psm1) & ~psm1);
4492 assert ((char *) chunk2mem (p) + 4 * SIZE_SZ <= paligned_mem);
4493 assert ((char *) p + size > paligned_mem);
4495 /* This is the size we could potentially free. */
4496 size -= paligned_mem - (char *) p;
4498 if (size > psm1)
4500 #if MALLOC_DEBUG
4501 /* When debugging we simulate destroying the memory
4502 content. */
4503 memset (paligned_mem, 0x89, size & ~psm1);
4504 #endif
4505 __madvise (paligned_mem, size & ~psm1, MADV_DONTNEED);
4507 result = 1;
4513 #ifndef MORECORE_CANNOT_TRIM
4514 return result | (av == &main_arena ? systrim (pad, av) : 0);
4516 #else
4517 return result;
4518 #endif
4523 __malloc_trim (size_t s)
4525 int result = 0;
4527 if (__malloc_initialized < 0)
4528 ptmalloc_init ();
4530 mstate ar_ptr = &main_arena;
4533 __libc_lock_lock (ar_ptr->mutex);
4534 result |= mtrim (ar_ptr, s);
4535 __libc_lock_unlock (ar_ptr->mutex);
4537 ar_ptr = ar_ptr->next;
4539 while (ar_ptr != &main_arena);
4541 return result;
4546 ------------------------- malloc_usable_size -------------------------
4549 static size_t
4550 musable (void *mem)
4552 mchunkptr p;
4553 if (mem != 0)
4555 p = mem2chunk (mem);
4557 if (__builtin_expect (using_malloc_checking == 1, 0))
4558 return malloc_check_get_size (p);
4560 if (chunk_is_mmapped (p))
4562 if (DUMPED_MAIN_ARENA_CHUNK (p))
4563 return chunksize (p) - SIZE_SZ;
4564 else
4565 return chunksize (p) - 2 * SIZE_SZ;
4567 else if (inuse (p))
4568 return chunksize (p) - SIZE_SZ;
4570 return 0;
4574 size_t
4575 __malloc_usable_size (void *m)
4577 size_t result;
4579 result = musable (m);
4580 return result;
4584 ------------------------------ mallinfo ------------------------------
4585 Accumulate malloc statistics for arena AV into M.
4588 static void
4589 int_mallinfo (mstate av, struct mallinfo *m)
4591 size_t i;
4592 mbinptr b;
4593 mchunkptr p;
4594 INTERNAL_SIZE_T avail;
4595 INTERNAL_SIZE_T fastavail;
4596 int nblocks;
4597 int nfastblocks;
4599 /* Ensure initialization */
4600 if (av->top == 0)
4601 malloc_consolidate (av);
4603 check_malloc_state (av);
4605 /* Account for top */
4606 avail = chunksize (av->top);
4607 nblocks = 1; /* top always exists */
4609 /* traverse fastbins */
4610 nfastblocks = 0;
4611 fastavail = 0;
4613 for (i = 0; i < NFASTBINS; ++i)
4615 for (p = fastbin (av, i); p != 0; p = p->fd)
4617 ++nfastblocks;
4618 fastavail += chunksize (p);
4622 avail += fastavail;
4624 /* traverse regular bins */
4625 for (i = 1; i < NBINS; ++i)
4627 b = bin_at (av, i);
4628 for (p = last (b); p != b; p = p->bk)
4630 ++nblocks;
4631 avail += chunksize (p);
4635 m->smblks += nfastblocks;
4636 m->ordblks += nblocks;
4637 m->fordblks += avail;
4638 m->uordblks += av->system_mem - avail;
4639 m->arena += av->system_mem;
4640 m->fsmblks += fastavail;
4641 if (av == &main_arena)
4643 m->hblks = mp_.n_mmaps;
4644 m->hblkhd = mp_.mmapped_mem;
4645 m->usmblks = 0;
4646 m->keepcost = chunksize (av->top);
4651 struct mallinfo
4652 __libc_mallinfo (void)
4654 struct mallinfo m;
4655 mstate ar_ptr;
4657 if (__malloc_initialized < 0)
4658 ptmalloc_init ();
4660 memset (&m, 0, sizeof (m));
4661 ar_ptr = &main_arena;
4664 __libc_lock_lock (ar_ptr->mutex);
4665 int_mallinfo (ar_ptr, &m);
4666 __libc_lock_unlock (ar_ptr->mutex);
4668 ar_ptr = ar_ptr->next;
4670 while (ar_ptr != &main_arena);
4672 return m;
4676 ------------------------------ malloc_stats ------------------------------
4679 void
4680 __malloc_stats (void)
4682 int i;
4683 mstate ar_ptr;
4684 unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b;
4686 if (__malloc_initialized < 0)
4687 ptmalloc_init ();
4688 _IO_flockfile (stderr);
4689 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
4690 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
4691 for (i = 0, ar_ptr = &main_arena;; i++)
4693 struct mallinfo mi;
4695 memset (&mi, 0, sizeof (mi));
4696 __libc_lock_lock (ar_ptr->mutex);
4697 int_mallinfo (ar_ptr, &mi);
4698 fprintf (stderr, "Arena %d:\n", i);
4699 fprintf (stderr, "system bytes = %10u\n", (unsigned int) mi.arena);
4700 fprintf (stderr, "in use bytes = %10u\n", (unsigned int) mi.uordblks);
4701 #if MALLOC_DEBUG > 1
4702 if (i > 0)
4703 dump_heap (heap_for_ptr (top (ar_ptr)));
4704 #endif
4705 system_b += mi.arena;
4706 in_use_b += mi.uordblks;
4707 __libc_lock_unlock (ar_ptr->mutex);
4708 ar_ptr = ar_ptr->next;
4709 if (ar_ptr == &main_arena)
4710 break;
4712 fprintf (stderr, "Total (incl. mmap):\n");
4713 fprintf (stderr, "system bytes = %10u\n", system_b);
4714 fprintf (stderr, "in use bytes = %10u\n", in_use_b);
4715 fprintf (stderr, "max mmap regions = %10u\n", (unsigned int) mp_.max_n_mmaps);
4716 fprintf (stderr, "max mmap bytes = %10lu\n",
4717 (unsigned long) mp_.max_mmapped_mem);
4718 ((_IO_FILE *) stderr)->_flags2 |= old_flags2;
4719 _IO_funlockfile (stderr);
4724 ------------------------------ mallopt ------------------------------
4728 __libc_mallopt (int param_number, int value)
4730 mstate av = &main_arena;
4731 int res = 1;
4733 if (__malloc_initialized < 0)
4734 ptmalloc_init ();
4735 __libc_lock_lock (av->mutex);
4736 /* Ensure initialization/consolidation */
4737 malloc_consolidate (av);
4739 LIBC_PROBE (memory_mallopt, 2, param_number, value);
4741 switch (param_number)
4743 case M_MXFAST:
4744 if (value >= 0 && value <= MAX_FAST_SIZE)
4746 LIBC_PROBE (memory_mallopt_mxfast, 2, value, get_max_fast ());
4747 set_max_fast (value);
4749 else
4750 res = 0;
4751 break;
4753 case M_TRIM_THRESHOLD:
4754 LIBC_PROBE (memory_mallopt_trim_threshold, 3, value,
4755 mp_.trim_threshold, mp_.no_dyn_threshold);
4756 mp_.trim_threshold = value;
4757 mp_.no_dyn_threshold = 1;
4758 break;
4760 case M_TOP_PAD:
4761 LIBC_PROBE (memory_mallopt_top_pad, 3, value,
4762 mp_.top_pad, mp_.no_dyn_threshold);
4763 mp_.top_pad = value;
4764 mp_.no_dyn_threshold = 1;
4765 break;
4767 case M_MMAP_THRESHOLD:
4768 /* Forbid setting the threshold too high. */
4769 if ((unsigned long) value > HEAP_MAX_SIZE / 2)
4770 res = 0;
4771 else
4773 LIBC_PROBE (memory_mallopt_mmap_threshold, 3, value,
4774 mp_.mmap_threshold, mp_.no_dyn_threshold);
4775 mp_.mmap_threshold = value;
4776 mp_.no_dyn_threshold = 1;
4778 break;
4780 case M_MMAP_MAX:
4781 LIBC_PROBE (memory_mallopt_mmap_max, 3, value,
4782 mp_.n_mmaps_max, mp_.no_dyn_threshold);
4783 mp_.n_mmaps_max = value;
4784 mp_.no_dyn_threshold = 1;
4785 break;
4787 case M_CHECK_ACTION:
4788 LIBC_PROBE (memory_mallopt_check_action, 2, value, check_action);
4789 check_action = value;
4790 break;
4792 case M_PERTURB:
4793 LIBC_PROBE (memory_mallopt_perturb, 2, value, perturb_byte);
4794 perturb_byte = value;
4795 break;
4797 case M_ARENA_TEST:
4798 if (value > 0)
4800 LIBC_PROBE (memory_mallopt_arena_test, 2, value, mp_.arena_test);
4801 mp_.arena_test = value;
4803 break;
4805 case M_ARENA_MAX:
4806 if (value > 0)
4808 LIBC_PROBE (memory_mallopt_arena_max, 2, value, mp_.arena_max);
4809 mp_.arena_max = value;
4811 break;
4813 __libc_lock_unlock (av->mutex);
4814 return res;
4816 libc_hidden_def (__libc_mallopt)
4820 -------------------- Alternative MORECORE functions --------------------
4825 General Requirements for MORECORE.
4827 The MORECORE function must have the following properties:
4829 If MORECORE_CONTIGUOUS is false:
4831 * MORECORE must allocate in multiples of pagesize. It will
4832 only be called with arguments that are multiples of pagesize.
4834 * MORECORE(0) must return an address that is at least
4835 MALLOC_ALIGNMENT aligned. (Page-aligning always suffices.)
4837 else (i.e. If MORECORE_CONTIGUOUS is true):
4839 * Consecutive calls to MORECORE with positive arguments
4840 return increasing addresses, indicating that space has been
4841 contiguously extended.
4843 * MORECORE need not allocate in multiples of pagesize.
4844 Calls to MORECORE need not have args of multiples of pagesize.
4846 * MORECORE need not page-align.
4848 In either case:
4850 * MORECORE may allocate more memory than requested. (Or even less,
4851 but this will generally result in a malloc failure.)
4853 * MORECORE must not allocate memory when given argument zero, but
4854 instead return one past the end address of memory from previous
4855 nonzero call. This malloc does NOT call MORECORE(0)
4856 until at least one call with positive arguments is made, so
4857 the initial value returned is not important.
4859 * Even though consecutive calls to MORECORE need not return contiguous
4860 addresses, it must be OK for malloc'ed chunks to span multiple
4861 regions in those cases where they do happen to be contiguous.
4863 * MORECORE need not handle negative arguments -- it may instead
4864 just return MORECORE_FAILURE when given negative arguments.
4865 Negative arguments are always multiples of pagesize. MORECORE
4866 must not misinterpret negative args as large positive unsigned
4867 args. You can suppress all such calls from even occurring by defining
4868 MORECORE_CANNOT_TRIM,
4870 There is some variation across systems about the type of the
4871 argument to sbrk/MORECORE. If size_t is unsigned, then it cannot
4872 actually be size_t, because sbrk supports negative args, so it is
4873 normally the signed type of the same width as size_t (sometimes
4874 declared as "intptr_t", and sometimes "ptrdiff_t"). It doesn't much
4875 matter though. Internally, we use "long" as arguments, which should
4876 work across all reasonable possibilities.
4878 Additionally, if MORECORE ever returns failure for a positive
4879 request, then mmap is used as a noncontiguous system allocator. This
4880 is a useful backup strategy for systems with holes in address spaces
4881 -- in this case sbrk cannot contiguously expand the heap, but mmap
4882 may be able to map noncontiguous space.
4884 If you'd like mmap to ALWAYS be used, you can define MORECORE to be
4885 a function that always returns MORECORE_FAILURE.
4887 If you are using this malloc with something other than sbrk (or its
4888 emulation) to supply memory regions, you probably want to set
4889 MORECORE_CONTIGUOUS as false. As an example, here is a custom
4890 allocator kindly contributed for pre-OSX macOS. It uses virtually
4891 but not necessarily physically contiguous non-paged memory (locked
4892 in, present and won't get swapped out). You can use it by
4893 uncommenting this section, adding some #includes, and setting up the
4894 appropriate defines above:
4896 *#define MORECORE osMoreCore
4897 *#define MORECORE_CONTIGUOUS 0
4899 There is also a shutdown routine that should somehow be called for
4900 cleanup upon program exit.
4902 *#define MAX_POOL_ENTRIES 100
4903 *#define MINIMUM_MORECORE_SIZE (64 * 1024)
4904 static int next_os_pool;
4905 void *our_os_pools[MAX_POOL_ENTRIES];
4907 void *osMoreCore(int size)
4909 void *ptr = 0;
4910 static void *sbrk_top = 0;
4912 if (size > 0)
4914 if (size < MINIMUM_MORECORE_SIZE)
4915 size = MINIMUM_MORECORE_SIZE;
4916 if (CurrentExecutionLevel() == kTaskLevel)
4917 ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0);
4918 if (ptr == 0)
4920 return (void *) MORECORE_FAILURE;
4922 // save ptrs so they can be freed during cleanup
4923 our_os_pools[next_os_pool] = ptr;
4924 next_os_pool++;
4925 ptr = (void *) ((((unsigned long) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK);
4926 sbrk_top = (char *) ptr + size;
4927 return ptr;
4929 else if (size < 0)
4931 // we don't currently support shrink behavior
4932 return (void *) MORECORE_FAILURE;
4934 else
4936 return sbrk_top;
4940 // cleanup any allocated memory pools
4941 // called as last thing before shutting down driver
4943 void osCleanupMem(void)
4945 void **ptr;
4947 for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++)
4948 if (*ptr)
4950 PoolDeallocate(*ptr);
4951 * ptr = 0;
4958 /* Helper code. */
4960 extern char **__libc_argv attribute_hidden;
4962 static void
4963 malloc_printerr (int action, const char *str, void *ptr, mstate ar_ptr)
4965 /* Avoid using this arena in future. We do not attempt to synchronize this
4966 with anything else because we minimally want to ensure that __libc_message
4967 gets its resources safely without stumbling on the current corruption. */
4968 if (ar_ptr)
4969 set_arena_corrupt (ar_ptr);
4971 if ((action & 5) == 5)
4972 __libc_message (action & 2, "%s\n", str);
4973 else if (action & 1)
4975 char buf[2 * sizeof (uintptr_t) + 1];
4977 buf[sizeof (buf) - 1] = '\0';
4978 char *cp = _itoa_word ((uintptr_t) ptr, &buf[sizeof (buf) - 1], 16, 0);
4979 while (cp > buf)
4980 *--cp = '0';
4982 __libc_message (action & 2, "*** Error in `%s': %s: 0x%s ***\n",
4983 __libc_argv[0] ? : "<unknown>", str, cp);
4985 else if (action & 2)
4986 abort ();
4989 /* We need a wrapper function for one of the additions of POSIX. */
4991 __posix_memalign (void **memptr, size_t alignment, size_t size)
4993 void *mem;
4995 /* Test whether the SIZE argument is valid. It must be a power of
4996 two multiple of sizeof (void *). */
4997 if (alignment % sizeof (void *) != 0
4998 || !powerof2 (alignment / sizeof (void *))
4999 || alignment == 0)
5000 return EINVAL;
5003 void *address = RETURN_ADDRESS (0);
5004 mem = _mid_memalign (alignment, size, address);
5006 if (mem != NULL)
5008 *memptr = mem;
5009 return 0;
5012 return ENOMEM;
5014 weak_alias (__posix_memalign, posix_memalign)
5018 __malloc_info (int options, FILE *fp)
5020 /* For now, at least. */
5021 if (options != 0)
5022 return EINVAL;
5024 int n = 0;
5025 size_t total_nblocks = 0;
5026 size_t total_nfastblocks = 0;
5027 size_t total_avail = 0;
5028 size_t total_fastavail = 0;
5029 size_t total_system = 0;
5030 size_t total_max_system = 0;
5031 size_t total_aspace = 0;
5032 size_t total_aspace_mprotect = 0;
5036 if (__malloc_initialized < 0)
5037 ptmalloc_init ();
5039 fputs ("<malloc version=\"1\">\n", fp);
5041 /* Iterate over all arenas currently in use. */
5042 mstate ar_ptr = &main_arena;
5045 fprintf (fp, "<heap nr=\"%d\">\n<sizes>\n", n++);
5047 size_t nblocks = 0;
5048 size_t nfastblocks = 0;
5049 size_t avail = 0;
5050 size_t fastavail = 0;
5051 struct
5053 size_t from;
5054 size_t to;
5055 size_t total;
5056 size_t count;
5057 } sizes[NFASTBINS + NBINS - 1];
5058 #define nsizes (sizeof (sizes) / sizeof (sizes[0]))
5060 __libc_lock_lock (ar_ptr->mutex);
5062 for (size_t i = 0; i < NFASTBINS; ++i)
5064 mchunkptr p = fastbin (ar_ptr, i);
5065 if (p != NULL)
5067 size_t nthissize = 0;
5068 size_t thissize = chunksize (p);
5070 while (p != NULL)
5072 ++nthissize;
5073 p = p->fd;
5076 fastavail += nthissize * thissize;
5077 nfastblocks += nthissize;
5078 sizes[i].from = thissize - (MALLOC_ALIGNMENT - 1);
5079 sizes[i].to = thissize;
5080 sizes[i].count = nthissize;
5082 else
5083 sizes[i].from = sizes[i].to = sizes[i].count = 0;
5085 sizes[i].total = sizes[i].count * sizes[i].to;
5089 mbinptr bin;
5090 struct malloc_chunk *r;
5092 for (size_t i = 1; i < NBINS; ++i)
5094 bin = bin_at (ar_ptr, i);
5095 r = bin->fd;
5096 sizes[NFASTBINS - 1 + i].from = ~((size_t) 0);
5097 sizes[NFASTBINS - 1 + i].to = sizes[NFASTBINS - 1 + i].total
5098 = sizes[NFASTBINS - 1 + i].count = 0;
5100 if (r != NULL)
5101 while (r != bin)
5103 ++sizes[NFASTBINS - 1 + i].count;
5104 sizes[NFASTBINS - 1 + i].total += r->size;
5105 sizes[NFASTBINS - 1 + i].from
5106 = MIN (sizes[NFASTBINS - 1 + i].from, r->size);
5107 sizes[NFASTBINS - 1 + i].to = MAX (sizes[NFASTBINS - 1 + i].to,
5108 r->size);
5110 r = r->fd;
5113 if (sizes[NFASTBINS - 1 + i].count == 0)
5114 sizes[NFASTBINS - 1 + i].from = 0;
5115 nblocks += sizes[NFASTBINS - 1 + i].count;
5116 avail += sizes[NFASTBINS - 1 + i].total;
5119 __libc_lock_unlock (ar_ptr->mutex);
5121 total_nfastblocks += nfastblocks;
5122 total_fastavail += fastavail;
5124 total_nblocks += nblocks;
5125 total_avail += avail;
5127 for (size_t i = 0; i < nsizes; ++i)
5128 if (sizes[i].count != 0 && i != NFASTBINS)
5129 fprintf (fp, " \
5130 <size from=\"%zu\" to=\"%zu\" total=\"%zu\" count=\"%zu\"/>\n",
5131 sizes[i].from, sizes[i].to, sizes[i].total, sizes[i].count);
5133 if (sizes[NFASTBINS].count != 0)
5134 fprintf (fp, "\
5135 <unsorted from=\"%zu\" to=\"%zu\" total=\"%zu\" count=\"%zu\"/>\n",
5136 sizes[NFASTBINS].from, sizes[NFASTBINS].to,
5137 sizes[NFASTBINS].total, sizes[NFASTBINS].count);
5139 total_system += ar_ptr->system_mem;
5140 total_max_system += ar_ptr->max_system_mem;
5142 fprintf (fp,
5143 "</sizes>\n<total type=\"fast\" count=\"%zu\" size=\"%zu\"/>\n"
5144 "<total type=\"rest\" count=\"%zu\" size=\"%zu\"/>\n"
5145 "<system type=\"current\" size=\"%zu\"/>\n"
5146 "<system type=\"max\" size=\"%zu\"/>\n",
5147 nfastblocks, fastavail, nblocks, avail,
5148 ar_ptr->system_mem, ar_ptr->max_system_mem);
5150 if (ar_ptr != &main_arena)
5152 heap_info *heap = heap_for_ptr (top (ar_ptr));
5153 fprintf (fp,
5154 "<aspace type=\"total\" size=\"%zu\"/>\n"
5155 "<aspace type=\"mprotect\" size=\"%zu\"/>\n",
5156 heap->size, heap->mprotect_size);
5157 total_aspace += heap->size;
5158 total_aspace_mprotect += heap->mprotect_size;
5160 else
5162 fprintf (fp,
5163 "<aspace type=\"total\" size=\"%zu\"/>\n"
5164 "<aspace type=\"mprotect\" size=\"%zu\"/>\n",
5165 ar_ptr->system_mem, ar_ptr->system_mem);
5166 total_aspace += ar_ptr->system_mem;
5167 total_aspace_mprotect += ar_ptr->system_mem;
5170 fputs ("</heap>\n", fp);
5171 ar_ptr = ar_ptr->next;
5173 while (ar_ptr != &main_arena);
5175 fprintf (fp,
5176 "<total type=\"fast\" count=\"%zu\" size=\"%zu\"/>\n"
5177 "<total type=\"rest\" count=\"%zu\" size=\"%zu\"/>\n"
5178 "<total type=\"mmap\" count=\"%d\" size=\"%zu\"/>\n"
5179 "<system type=\"current\" size=\"%zu\"/>\n"
5180 "<system type=\"max\" size=\"%zu\"/>\n"
5181 "<aspace type=\"total\" size=\"%zu\"/>\n"
5182 "<aspace type=\"mprotect\" size=\"%zu\"/>\n"
5183 "</malloc>\n",
5184 total_nfastblocks, total_fastavail, total_nblocks, total_avail,
5185 mp_.n_mmaps, mp_.mmapped_mem,
5186 total_system, total_max_system,
5187 total_aspace, total_aspace_mprotect);
5189 return 0;
5191 weak_alias (__malloc_info, malloc_info)
5194 strong_alias (__libc_calloc, __calloc) weak_alias (__libc_calloc, calloc)
5195 strong_alias (__libc_free, __cfree) weak_alias (__libc_free, cfree)
5196 strong_alias (__libc_free, __free) strong_alias (__libc_free, free)
5197 strong_alias (__libc_malloc, __malloc) strong_alias (__libc_malloc, malloc)
5198 strong_alias (__libc_memalign, __memalign)
5199 weak_alias (__libc_memalign, memalign)
5200 strong_alias (__libc_realloc, __realloc) strong_alias (__libc_realloc, realloc)
5201 strong_alias (__libc_valloc, __valloc) weak_alias (__libc_valloc, valloc)
5202 strong_alias (__libc_pvalloc, __pvalloc) weak_alias (__libc_pvalloc, pvalloc)
5203 strong_alias (__libc_mallinfo, __mallinfo)
5204 weak_alias (__libc_mallinfo, mallinfo)
5205 strong_alias (__libc_mallopt, __mallopt) weak_alias (__libc_mallopt, mallopt)
5207 weak_alias (__malloc_stats, malloc_stats)
5208 weak_alias (__malloc_usable_size, malloc_usable_size)
5209 weak_alias (__malloc_trim, malloc_trim)
5210 weak_alias (__malloc_get_state, malloc_get_state)
5211 weak_alias (__malloc_set_state, malloc_set_state)
5214 /* ------------------------------------------------------------
5215 History:
5217 [see ftp://g.oswego.edu/pub/misc/malloc.c for the history of dlmalloc]
5221 * Local variables:
5222 * c-basic-offset: 2
5223 * End: