Simplify perturb_byte logic.
[glibc.git] / malloc / malloc.c
blobac8c3f6631a703e22c1355885513d61dda6d3ca7
1 /* Malloc implementation for multiple threads without lock contention.
2 Copyright (C) 1996-2013 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 changesmade 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
176 MALLOC_ALIGNMENT MAX (2 * sizeof(INTERNAL_SIZE_T),
177 __alignof__ (long double))
179 Configuration and functionality options:
181 USE_PUBLIC_MALLOC_WRAPPERS NOT defined
182 USE_MALLOC_LOCK NOT defined
183 MALLOC_DEBUG NOT defined
184 REALLOC_ZERO_BYTES_FREES 1
185 TRIM_FASTBINS 0
187 Options for customizing MORECORE:
189 MORECORE sbrk
190 MORECORE_FAILURE -1
191 MORECORE_CONTIGUOUS 1
192 MORECORE_CANNOT_TRIM NOT defined
193 MORECORE_CLEARS 1
194 MMAP_AS_MORECORE_SIZE (1024 * 1024)
196 Tuning options that are also dynamically changeable via mallopt:
198 DEFAULT_MXFAST 64 (for 32bit), 128 (for 64bit)
199 DEFAULT_TRIM_THRESHOLD 128 * 1024
200 DEFAULT_TOP_PAD 0
201 DEFAULT_MMAP_THRESHOLD 128 * 1024
202 DEFAULT_MMAP_MAX 65536
204 There are several other #defined constants and macros that you
205 probably don't want to touch unless you are extending or adapting malloc. */
208 void* is the pointer type that malloc should say it returns
211 #ifndef void
212 #define void void
213 #endif /*void*/
215 #include <stddef.h> /* for size_t */
216 #include <stdlib.h> /* for getenv(), abort() */
217 #include <unistd.h> /* for __libc_enable_secure */
219 #include <malloc-machine.h>
220 #include <malloc-sysdep.h>
222 #include <atomic.h>
223 #include <_itoa.h>
224 #include <bits/wordsize.h>
225 #include <sys/sysinfo.h>
227 #include <ldsodefs.h>
229 #include <unistd.h>
230 #include <stdio.h> /* needed for malloc_stats */
231 #include <errno.h>
233 #include <shlib-compat.h>
235 /* For uintptr_t. */
236 #include <stdint.h>
238 /* For va_arg, va_start, va_end. */
239 #include <stdarg.h>
241 /* For MIN, MAX, powerof2. */
242 #include <sys/param.h>
246 Debugging:
248 Because freed chunks may be overwritten with bookkeeping fields, this
249 malloc will often die when freed memory is overwritten by user
250 programs. This can be very effective (albeit in an annoying way)
251 in helping track down dangling pointers.
253 If you compile with -DMALLOC_DEBUG, a number of assertion checks are
254 enabled that will catch more memory errors. You probably won't be
255 able to make much sense of the actual assertion errors, but they
256 should help you locate incorrectly overwritten memory. The checking
257 is fairly extensive, and will slow down execution
258 noticeably. Calling malloc_stats or mallinfo with MALLOC_DEBUG set
259 will attempt to check every non-mmapped allocated and free chunk in
260 the course of computing the summmaries. (By nature, mmapped regions
261 cannot be checked very much automatically.)
263 Setting MALLOC_DEBUG may also be helpful if you are trying to modify
264 this code. The assertions in the check routines spell out in more
265 detail the assumptions and invariants underlying the algorithms.
267 Setting MALLOC_DEBUG does NOT provide an automated mechanism for
268 checking that all accesses to malloced memory stay within their
269 bounds. However, there are several add-ons and adaptations of this
270 or other mallocs available that do this.
273 #ifdef NDEBUG
274 # define assert(expr) ((void) 0)
275 #else
276 # define assert(expr) \
277 ((expr) \
278 ? ((void) 0) \
279 : __malloc_assert (__STRING (expr), __FILE__, __LINE__, __func__))
281 extern const char *__progname;
283 static void
284 __malloc_assert (const char *assertion, const char *file, unsigned int line,
285 const char *function)
287 (void) __fxprintf (NULL, "%s%s%s:%u: %s%sAssertion `%s' failed.\n",
288 __progname, __progname[0] ? ": " : "",
289 file, line,
290 function ? function : "", function ? ": " : "",
291 assertion);
292 fflush (stderr);
293 abort ();
295 #endif
299 INTERNAL_SIZE_T is the word-size used for internal bookkeeping
300 of chunk sizes.
302 The default version is the same as size_t.
304 While not strictly necessary, it is best to define this as an
305 unsigned type, even if size_t is a signed type. This may avoid some
306 artificial size limitations on some systems.
308 On a 64-bit machine, you may be able to reduce malloc overhead by
309 defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
310 expense of not being able to handle more than 2^32 of malloced
311 space. If this limitation is acceptable, you are encouraged to set
312 this unless you are on a platform requiring 16byte alignments. In
313 this case the alignment requirements turn out to negate any
314 potential advantages of decreasing size_t word size.
316 Implementors: Beware of the possible combinations of:
317 - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
318 and might be the same width as int or as long
319 - size_t might have different width and signedness as INTERNAL_SIZE_T
320 - int and long might be 32 or 64 bits, and might be the same width
321 To deal with this, most comparisons and difference computations
322 among INTERNAL_SIZE_Ts should cast them to unsigned long, being
323 aware of the fact that casting an unsigned int to a wider long does
324 not sign-extend. (This also makes checking for negative numbers
325 awkward.) Some of these casts result in harmless compiler warnings
326 on some systems.
329 #ifndef INTERNAL_SIZE_T
330 #define INTERNAL_SIZE_T size_t
331 #endif
333 /* The corresponding word size */
334 #define SIZE_SZ (sizeof(INTERNAL_SIZE_T))
338 MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks.
339 It must be a power of two at least 2 * SIZE_SZ, even on machines
340 for which smaller alignments would suffice. It may be defined as
341 larger than this though. Note however that code and data structures
342 are optimized for the case of 8-byte alignment.
346 #ifndef MALLOC_ALIGNMENT
347 # if !SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_16)
348 /* This is the correct definition when there is no past ABI to constrain it.
350 Among configurations with a past ABI constraint, it differs from
351 2*SIZE_SZ only on powerpc32. For the time being, changing this is
352 causing more compatibility problems due to malloc_get_state and
353 malloc_set_state than will returning blocks not adequately aligned for
354 long double objects under -mlong-double-128. */
356 # define MALLOC_ALIGNMENT (2 * SIZE_SZ < __alignof__ (long double) \
357 ? __alignof__ (long double) : 2 * SIZE_SZ)
358 # else
359 # define MALLOC_ALIGNMENT (2 * SIZE_SZ)
360 # endif
361 #endif
363 /* The corresponding bit mask value */
364 #define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
369 REALLOC_ZERO_BYTES_FREES should be set if a call to
370 realloc with zero bytes should be the same as a call to free.
371 This is required by the C standard. Otherwise, since this malloc
372 returns a unique pointer for malloc(0), so does realloc(p, 0).
375 #ifndef REALLOC_ZERO_BYTES_FREES
376 #define REALLOC_ZERO_BYTES_FREES 1
377 #endif
380 TRIM_FASTBINS controls whether free() of a very small chunk can
381 immediately lead to trimming. Setting to true (1) can reduce memory
382 footprint, but will almost always slow down programs that use a lot
383 of small chunks.
385 Define this only if you are willing to give up some speed to more
386 aggressively reduce system-level memory footprint when releasing
387 memory in programs that use many small chunks. You can get
388 essentially the same effect by setting MXFAST to 0, but this can
389 lead to even greater slowdowns in programs using many small chunks.
390 TRIM_FASTBINS is an in-between compile-time option, that disables
391 only those chunks bordering topmost memory from being placed in
392 fastbins.
395 #ifndef TRIM_FASTBINS
396 #define TRIM_FASTBINS 0
397 #endif
400 /* Definition for getting more memory from the OS. */
401 #define MORECORE (*__morecore)
402 #define MORECORE_FAILURE 0
403 void * __default_morecore (ptrdiff_t);
404 void *(*__morecore)(ptrdiff_t) = __default_morecore;
407 #include <string.h>
410 MORECORE-related declarations. By default, rely on sbrk
415 MORECORE is the name of the routine to call to obtain more memory
416 from the system. See below for general guidance on writing
417 alternative MORECORE functions, as well as a version for WIN32 and a
418 sample version for pre-OSX macos.
421 #ifndef MORECORE
422 #define MORECORE sbrk
423 #endif
426 MORECORE_FAILURE is the value returned upon failure of MORECORE
427 as well as mmap. Since it cannot be an otherwise valid memory address,
428 and must reflect values of standard sys calls, you probably ought not
429 try to redefine it.
432 #ifndef MORECORE_FAILURE
433 #define MORECORE_FAILURE (-1)
434 #endif
437 If MORECORE_CONTIGUOUS is true, take advantage of fact that
438 consecutive calls to MORECORE with positive arguments always return
439 contiguous increasing addresses. This is true of unix sbrk. Even
440 if not defined, when regions happen to be contiguous, malloc will
441 permit allocations spanning regions obtained from different
442 calls. But defining this when applicable enables some stronger
443 consistency checks and space efficiencies.
446 #ifndef MORECORE_CONTIGUOUS
447 #define MORECORE_CONTIGUOUS 1
448 #endif
451 Define MORECORE_CANNOT_TRIM if your version of MORECORE
452 cannot release space back to the system when given negative
453 arguments. This is generally necessary only if you are using
454 a hand-crafted MORECORE function that cannot handle negative arguments.
457 /* #define MORECORE_CANNOT_TRIM */
459 /* MORECORE_CLEARS (default 1)
460 The degree to which the routine mapped to MORECORE zeroes out
461 memory: never (0), only for newly allocated space (1) or always
462 (2). The distinction between (1) and (2) is necessary because on
463 some systems, if the application first decrements and then
464 increments the break value, the contents of the reallocated space
465 are unspecified.
468 #ifndef MORECORE_CLEARS
469 #define MORECORE_CLEARS 1
470 #endif
474 MMAP_AS_MORECORE_SIZE is the minimum mmap size argument to use if
475 sbrk fails, and mmap is used as a backup. The value must be a
476 multiple of page size. This backup strategy generally applies only
477 when systems have "holes" in address space, so sbrk cannot perform
478 contiguous expansion, but there is still space available on system.
479 On systems for which this is known to be useful (i.e. most linux
480 kernels), this occurs only when programs allocate huge amounts of
481 memory. Between this, and the fact that mmap regions tend to be
482 limited, the size should be large, to avoid too many mmap calls and
483 thus avoid running out of kernel resources. */
485 #ifndef MMAP_AS_MORECORE_SIZE
486 #define MMAP_AS_MORECORE_SIZE (1024 * 1024)
487 #endif
490 Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
491 large blocks.
494 #ifndef HAVE_MREMAP
495 #define HAVE_MREMAP 0
496 #endif
500 This version of malloc supports the standard SVID/XPG mallinfo
501 routine that returns a struct containing usage properties and
502 statistics. It should work on any SVID/XPG compliant system that has
503 a /usr/include/malloc.h defining struct mallinfo. (If you'd like to
504 install such a thing yourself, cut out the preliminary declarations
505 as described above and below and save them in a malloc.h file. But
506 there's no compelling reason to bother to do this.)
508 The main declaration needed is the mallinfo struct that is returned
509 (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a
510 bunch of fields that are not even meaningful in this version of
511 malloc. These fields are are instead filled by mallinfo() with
512 other numbers that might be of interest.
516 /* ---------- description of public routines ------------ */
519 malloc(size_t n)
520 Returns a pointer to a newly allocated chunk of at least n bytes, or null
521 if no space is available. Additionally, on failure, errno is
522 set to ENOMEM on ANSI C systems.
524 If n is zero, malloc returns a minumum-sized chunk. (The minimum
525 size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
526 systems.) On most systems, size_t is an unsigned type, so calls
527 with negative arguments are interpreted as requests for huge amounts
528 of space, which will often fail. The maximum supported value of n
529 differs across systems, but is in all cases less than the maximum
530 representable value of a size_t.
532 void* __libc_malloc(size_t);
533 libc_hidden_proto (__libc_malloc)
536 free(void* p)
537 Releases the chunk of memory pointed to by p, that had been previously
538 allocated using malloc or a related routine such as realloc.
539 It has no effect if p is null. It can have arbitrary (i.e., bad!)
540 effects if p has already been freed.
542 Unless disabled (using mallopt), freeing very large spaces will
543 when possible, automatically trigger operations that give
544 back unused memory to the system, thus reducing program footprint.
546 void __libc_free(void*);
547 libc_hidden_proto (__libc_free)
550 calloc(size_t n_elements, size_t element_size);
551 Returns a pointer to n_elements * element_size bytes, with all locations
552 set to zero.
554 void* __libc_calloc(size_t, size_t);
557 realloc(void* p, size_t n)
558 Returns a pointer to a chunk of size n that contains the same data
559 as does chunk p up to the minimum of (n, p's size) bytes, or null
560 if no space is available.
562 The returned pointer may or may not be the same as p. The algorithm
563 prefers extending p when possible, otherwise it employs the
564 equivalent of a malloc-copy-free sequence.
566 If p is null, realloc is equivalent to malloc.
568 If space is not available, realloc returns null, errno is set (if on
569 ANSI) and p is NOT freed.
571 if n is for fewer bytes than already held by p, the newly unused
572 space is lopped off and freed if possible. Unless the #define
573 REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of
574 zero (re)allocates a minimum-sized chunk.
576 Large chunks that were internally obtained via mmap will always
577 be reallocated using malloc-copy-free sequences unless
578 the system supports MREMAP (currently only linux).
580 The old unix realloc convention of allowing the last-free'd chunk
581 to be used as an argument to realloc is not supported.
583 void* __libc_realloc(void*, size_t);
584 libc_hidden_proto (__libc_realloc)
587 memalign(size_t alignment, size_t n);
588 Returns a pointer to a newly allocated chunk of n bytes, aligned
589 in accord with the alignment argument.
591 The alignment argument should be a power of two. If the argument is
592 not a power of two, the nearest greater power is used.
593 8-byte alignment is guaranteed by normal malloc calls, so don't
594 bother calling memalign with an argument of 8 or less.
596 Overreliance on memalign is a sure way to fragment space.
598 void* __libc_memalign(size_t, size_t);
599 libc_hidden_proto (__libc_memalign)
602 valloc(size_t n);
603 Equivalent to memalign(pagesize, n), where pagesize is the page
604 size of the system. If the pagesize is unknown, 4096 is used.
606 void* __libc_valloc(size_t);
611 mallopt(int parameter_number, int parameter_value)
612 Sets tunable parameters The format is to provide a
613 (parameter-number, parameter-value) pair. mallopt then sets the
614 corresponding parameter to the argument value if it can (i.e., so
615 long as the value is meaningful), and returns 1 if successful else
616 0. SVID/XPG/ANSI defines four standard param numbers for mallopt,
617 normally defined in malloc.h. Only one of these (M_MXFAST) is used
618 in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
619 so setting them has no effect. But this malloc also supports four
620 other options in mallopt. See below for details. Briefly, supported
621 parameters are as follows (listed defaults are for "typical"
622 configurations).
624 Symbol param # default allowed param values
625 M_MXFAST 1 64 0-80 (0 disables fastbins)
626 M_TRIM_THRESHOLD -1 128*1024 any (-1U disables trimming)
627 M_TOP_PAD -2 0 any
628 M_MMAP_THRESHOLD -3 128*1024 any (or 0 if no MMAP support)
629 M_MMAP_MAX -4 65536 any (0 disables use of mmap)
631 int __libc_mallopt(int, int);
632 libc_hidden_proto (__libc_mallopt)
636 mallinfo()
637 Returns (by copy) a struct containing various summary statistics:
639 arena: current total non-mmapped bytes allocated from system
640 ordblks: the number of free chunks
641 smblks: the number of fastbin blocks (i.e., small chunks that
642 have been freed but not use resused or consolidated)
643 hblks: current number of mmapped regions
644 hblkhd: total bytes held in mmapped regions
645 usmblks: the maximum total allocated space. This will be greater
646 than current total if trimming has occurred.
647 fsmblks: total bytes held in fastbin blocks
648 uordblks: current total allocated space (normal or mmapped)
649 fordblks: total free space
650 keepcost: the maximum number of bytes that could ideally be released
651 back to system via malloc_trim. ("ideally" means that
652 it ignores page restrictions etc.)
654 Because these fields are ints, but internal bookkeeping may
655 be kept as longs, the reported values may wrap around zero and
656 thus be inaccurate.
658 struct mallinfo __libc_mallinfo(void);
662 pvalloc(size_t n);
663 Equivalent to valloc(minimum-page-that-holds(n)), that is,
664 round up n to nearest pagesize.
666 void* __libc_pvalloc(size_t);
669 malloc_trim(size_t pad);
671 If possible, gives memory back to the system (via negative
672 arguments to sbrk) if there is unused memory at the `high' end of
673 the malloc pool. You can call this after freeing large blocks of
674 memory to potentially reduce the system-level memory requirements
675 of a program. However, it cannot guarantee to reduce memory. Under
676 some allocation patterns, some large free blocks of memory will be
677 locked between two used chunks, so they cannot be given back to
678 the system.
680 The `pad' argument to malloc_trim represents the amount of free
681 trailing space to leave untrimmed. If this argument is zero,
682 only the minimum amount of memory to maintain internal data
683 structures will be left (one page or less). Non-zero arguments
684 can be supplied to maintain enough trailing space to service
685 future expected allocations without having to re-obtain memory
686 from the system.
688 Malloc_trim returns 1 if it actually released any memory, else 0.
689 On systems that do not support "negative sbrks", it will always
690 return 0.
692 int __malloc_trim(size_t);
695 malloc_usable_size(void* p);
697 Returns the number of bytes you can actually use in
698 an allocated chunk, which may be more than you requested (although
699 often not) due to alignment and minimum size constraints.
700 You can use this many bytes without worrying about
701 overwriting other allocated objects. This is not a particularly great
702 programming practice. malloc_usable_size can be more useful in
703 debugging and assertions, for example:
705 p = malloc(n);
706 assert(malloc_usable_size(p) >= 256);
709 size_t __malloc_usable_size(void*);
712 malloc_stats();
713 Prints on stderr the amount of space obtained from the system (both
714 via sbrk and mmap), the maximum amount (which may be more than
715 current if malloc_trim and/or munmap got called), and the current
716 number of bytes allocated via malloc (or realloc, etc) but not yet
717 freed. Note that this is the number of bytes allocated, not the
718 number requested. It will be larger than the number requested
719 because of alignment and bookkeeping overhead. Because it includes
720 alignment wastage as being in use, this figure may be greater than
721 zero even when no user-level chunks are allocated.
723 The reported current and maximum system memory can be inaccurate if
724 a program makes other calls to system memory allocation functions
725 (normally sbrk) outside of malloc.
727 malloc_stats prints only the most commonly interesting statistics.
728 More information can be obtained by calling mallinfo.
731 void __malloc_stats(void);
734 malloc_get_state(void);
736 Returns the state of all malloc variables in an opaque data
737 structure.
739 void* __malloc_get_state(void);
742 malloc_set_state(void* state);
744 Restore the state of all malloc variables from data obtained with
745 malloc_get_state().
747 int __malloc_set_state(void*);
750 posix_memalign(void **memptr, size_t alignment, size_t size);
752 POSIX wrapper like memalign(), checking for validity of size.
754 int __posix_memalign(void **, size_t, size_t);
756 /* mallopt tuning options */
759 M_MXFAST is the maximum request size used for "fastbins", special bins
760 that hold returned chunks without consolidating their spaces. This
761 enables future requests for chunks of the same size to be handled
762 very quickly, but can increase fragmentation, and thus increase the
763 overall memory footprint of a program.
765 This malloc manages fastbins very conservatively yet still
766 efficiently, so fragmentation is rarely a problem for values less
767 than or equal to the default. The maximum supported value of MXFAST
768 is 80. You wouldn't want it any higher than this anyway. Fastbins
769 are designed especially for use with many small structs, objects or
770 strings -- the default handles structs/objects/arrays with sizes up
771 to 8 4byte fields, or small strings representing words, tokens,
772 etc. Using fastbins for larger objects normally worsens
773 fragmentation without improving speed.
775 M_MXFAST is set in REQUEST size units. It is internally used in
776 chunksize units, which adds padding and alignment. You can reduce
777 M_MXFAST to 0 to disable all use of fastbins. This causes the malloc
778 algorithm to be a closer approximation of fifo-best-fit in all cases,
779 not just for larger requests, but will generally cause it to be
780 slower.
784 /* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */
785 #ifndef M_MXFAST
786 #define M_MXFAST 1
787 #endif
789 #ifndef DEFAULT_MXFAST
790 #define DEFAULT_MXFAST (64 * SIZE_SZ / 4)
791 #endif
795 M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
796 to keep before releasing via malloc_trim in free().
798 Automatic trimming is mainly useful in long-lived programs.
799 Because trimming via sbrk can be slow on some systems, and can
800 sometimes be wasteful (in cases where programs immediately
801 afterward allocate more large chunks) the value should be high
802 enough so that your overall system performance would improve by
803 releasing this much memory.
805 The trim threshold and the mmap control parameters (see below)
806 can be traded off with one another. Trimming and mmapping are
807 two different ways of releasing unused memory back to the
808 system. Between these two, it is often possible to keep
809 system-level demands of a long-lived program down to a bare
810 minimum. For example, in one test suite of sessions measuring
811 the XF86 X server on Linux, using a trim threshold of 128K and a
812 mmap threshold of 192K led to near-minimal long term resource
813 consumption.
815 If you are using this malloc in a long-lived program, it should
816 pay to experiment with these values. As a rough guide, you
817 might set to a value close to the average size of a process
818 (program) running on your system. Releasing this much memory
819 would allow such a process to run in memory. Generally, it's
820 worth it to tune for trimming rather tham memory mapping when a
821 program undergoes phases where several large chunks are
822 allocated and released in ways that can reuse each other's
823 storage, perhaps mixed with phases where there are no such
824 chunks at all. And in well-behaved long-lived programs,
825 controlling release of large blocks via trimming versus mapping
826 is usually faster.
828 However, in most programs, these parameters serve mainly as
829 protection against the system-level effects of carrying around
830 massive amounts of unneeded memory. Since frequent calls to
831 sbrk, mmap, and munmap otherwise degrade performance, the default
832 parameters are set to relatively high values that serve only as
833 safeguards.
835 The trim value It must be greater than page size to have any useful
836 effect. To disable trimming completely, you can set to
837 (unsigned long)(-1)
839 Trim settings interact with fastbin (MXFAST) settings: Unless
840 TRIM_FASTBINS is defined, automatic trimming never takes place upon
841 freeing a chunk with size less than or equal to MXFAST. Trimming is
842 instead delayed until subsequent freeing of larger chunks. However,
843 you can still force an attempted trim by calling malloc_trim.
845 Also, trimming is not generally possible in cases where
846 the main arena is obtained via mmap.
848 Note that the trick some people use of mallocing a huge space and
849 then freeing it at program startup, in an attempt to reserve system
850 memory, doesn't have the intended effect under automatic trimming,
851 since that memory will immediately be returned to the system.
854 #define M_TRIM_THRESHOLD -1
856 #ifndef DEFAULT_TRIM_THRESHOLD
857 #define DEFAULT_TRIM_THRESHOLD (128 * 1024)
858 #endif
861 M_TOP_PAD is the amount of extra `padding' space to allocate or
862 retain whenever sbrk is called. It is used in two ways internally:
864 * When sbrk is called to extend the top of the arena to satisfy
865 a new malloc request, this much padding is added to the sbrk
866 request.
868 * When malloc_trim is called automatically from free(),
869 it is used as the `pad' argument.
871 In both cases, the actual amount of padding is rounded
872 so that the end of the arena is always a system page boundary.
874 The main reason for using padding is to avoid calling sbrk so
875 often. Having even a small pad greatly reduces the likelihood
876 that nearly every malloc request during program start-up (or
877 after trimming) will invoke sbrk, which needlessly wastes
878 time.
880 Automatic rounding-up to page-size units is normally sufficient
881 to avoid measurable overhead, so the default is 0. However, in
882 systems where sbrk is relatively slow, it can pay to increase
883 this value, at the expense of carrying around more memory than
884 the program needs.
887 #define M_TOP_PAD -2
889 #ifndef DEFAULT_TOP_PAD
890 #define DEFAULT_TOP_PAD (0)
891 #endif
894 MMAP_THRESHOLD_MAX and _MIN are the bounds on the dynamically
895 adjusted MMAP_THRESHOLD.
898 #ifndef DEFAULT_MMAP_THRESHOLD_MIN
899 #define DEFAULT_MMAP_THRESHOLD_MIN (128 * 1024)
900 #endif
902 #ifndef DEFAULT_MMAP_THRESHOLD_MAX
903 /* For 32-bit platforms we cannot increase the maximum mmap
904 threshold much because it is also the minimum value for the
905 maximum heap size and its alignment. Going above 512k (i.e., 1M
906 for new heaps) wastes too much address space. */
907 # if __WORDSIZE == 32
908 # define DEFAULT_MMAP_THRESHOLD_MAX (512 * 1024)
909 # else
910 # define DEFAULT_MMAP_THRESHOLD_MAX (4 * 1024 * 1024 * sizeof(long))
911 # endif
912 #endif
915 M_MMAP_THRESHOLD is the request size threshold for using mmap()
916 to service a request. Requests of at least this size that cannot
917 be allocated using already-existing space will be serviced via mmap.
918 (If enough normal freed space already exists it is used instead.)
920 Using mmap segregates relatively large chunks of memory so that
921 they can be individually obtained and released from the host
922 system. A request serviced through mmap is never reused by any
923 other request (at least not directly; the system may just so
924 happen to remap successive requests to the same locations).
926 Segregating space in this way has the benefits that:
928 1. Mmapped space can ALWAYS be individually released back
929 to the system, which helps keep the system level memory
930 demands of a long-lived program low.
931 2. Mapped memory can never become `locked' between
932 other chunks, as can happen with normally allocated chunks, which
933 means that even trimming via malloc_trim would not release them.
934 3. On some systems with "holes" in address spaces, mmap can obtain
935 memory that sbrk cannot.
937 However, it has the disadvantages that:
939 1. The space cannot be reclaimed, consolidated, and then
940 used to service later requests, as happens with normal chunks.
941 2. It can lead to more wastage because of mmap page alignment
942 requirements
943 3. It causes malloc performance to be more dependent on host
944 system memory management support routines which may vary in
945 implementation quality and may impose arbitrary
946 limitations. Generally, servicing a request via normal
947 malloc steps is faster than going through a system's mmap.
949 The advantages of mmap nearly always outweigh disadvantages for
950 "large" chunks, but the value of "large" varies across systems. The
951 default is an empirically derived value that works well in most
952 systems.
955 Update in 2006:
956 The above was written in 2001. Since then the world has changed a lot.
957 Memory got bigger. Applications got bigger. The virtual address space
958 layout in 32 bit linux changed.
960 In the new situation, brk() and mmap space is shared and there are no
961 artificial limits on brk size imposed by the kernel. What is more,
962 applications have started using transient allocations larger than the
963 128Kb as was imagined in 2001.
965 The price for mmap is also high now; each time glibc mmaps from the
966 kernel, the kernel is forced to zero out the memory it gives to the
967 application. Zeroing memory is expensive and eats a lot of cache and
968 memory bandwidth. This has nothing to do with the efficiency of the
969 virtual memory system, by doing mmap the kernel just has no choice but
970 to zero.
972 In 2001, the kernel had a maximum size for brk() which was about 800
973 megabytes on 32 bit x86, at that point brk() would hit the first
974 mmaped shared libaries and couldn't expand anymore. With current 2.6
975 kernels, the VA space layout is different and brk() and mmap
976 both can span the entire heap at will.
978 Rather than using a static threshold for the brk/mmap tradeoff,
979 we are now using a simple dynamic one. The goal is still to avoid
980 fragmentation. The old goals we kept are
981 1) try to get the long lived large allocations to use mmap()
982 2) really large allocations should always use mmap()
983 and we're adding now:
984 3) transient allocations should use brk() to avoid forcing the kernel
985 having to zero memory over and over again
987 The implementation works with a sliding threshold, which is by default
988 limited to go between 128Kb and 32Mb (64Mb for 64 bitmachines) and starts
989 out at 128Kb as per the 2001 default.
991 This allows us to satisfy requirement 1) under the assumption that long
992 lived allocations are made early in the process' lifespan, before it has
993 started doing dynamic allocations of the same size (which will
994 increase the threshold).
996 The upperbound on the threshold satisfies requirement 2)
998 The threshold goes up in value when the application frees memory that was
999 allocated with the mmap allocator. The idea is that once the application
1000 starts freeing memory of a certain size, it's highly probable that this is
1001 a size the application uses for transient allocations. This estimator
1002 is there to satisfy the new third requirement.
1006 #define M_MMAP_THRESHOLD -3
1008 #ifndef DEFAULT_MMAP_THRESHOLD
1009 #define DEFAULT_MMAP_THRESHOLD DEFAULT_MMAP_THRESHOLD_MIN
1010 #endif
1013 M_MMAP_MAX is the maximum number of requests to simultaneously
1014 service using mmap. This parameter exists because
1015 some systems have a limited number of internal tables for
1016 use by mmap, and using more than a few of them may degrade
1017 performance.
1019 The default is set to a value that serves only as a safeguard.
1020 Setting to 0 disables use of mmap for servicing large requests.
1023 #define M_MMAP_MAX -4
1025 #ifndef DEFAULT_MMAP_MAX
1026 #define DEFAULT_MMAP_MAX (65536)
1027 #endif
1029 #include <malloc.h>
1031 #ifndef RETURN_ADDRESS
1032 #define RETURN_ADDRESS(X_) (NULL)
1033 #endif
1035 /* On some platforms we can compile internal, not exported functions better.
1036 Let the environment provide a macro and define it to be empty if it
1037 is not available. */
1038 #ifndef internal_function
1039 # define internal_function
1040 #endif
1042 /* Forward declarations. */
1043 struct malloc_chunk;
1044 typedef struct malloc_chunk* mchunkptr;
1046 /* Internal routines. */
1048 static void* _int_malloc(mstate, size_t);
1049 static void _int_free(mstate, mchunkptr, int);
1050 static void* _int_realloc(mstate, mchunkptr, INTERNAL_SIZE_T,
1051 INTERNAL_SIZE_T);
1052 static void* _int_memalign(mstate, size_t, size_t);
1053 static void* _mid_memalign(size_t, size_t, void *);
1055 static void malloc_printerr(int action, const char *str, void *ptr);
1057 static void* internal_function mem2mem_check(void *p, size_t sz);
1058 static int internal_function top_check(void);
1059 static void internal_function munmap_chunk(mchunkptr p);
1060 #if HAVE_MREMAP
1061 static mchunkptr internal_function mremap_chunk(mchunkptr p, size_t new_size);
1062 #endif
1064 static void* malloc_check(size_t sz, const void *caller);
1065 static void free_check(void* mem, const void *caller);
1066 static void* realloc_check(void* oldmem, size_t bytes,
1067 const void *caller);
1068 static void* memalign_check(size_t alignment, size_t bytes,
1069 const void *caller);
1070 #ifndef NO_THREADS
1071 static void* malloc_atfork(size_t sz, const void *caller);
1072 static void free_atfork(void* mem, const void *caller);
1073 #endif
1076 /* ------------- Optional versions of memcopy ---------------- */
1080 Note: memcpy is ONLY invoked with non-overlapping regions,
1081 so the (usually slower) memmove is not needed.
1084 #define MALLOC_COPY(dest, src, nbytes) memcpy(dest, src, nbytes)
1085 #define MALLOC_ZERO(dest, nbytes) memset(dest, 0, nbytes)
1088 /* ------------------ MMAP support ------------------ */
1091 #include <fcntl.h>
1092 #include <sys/mman.h>
1094 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
1095 # define MAP_ANONYMOUS MAP_ANON
1096 #endif
1098 #ifndef MAP_NORESERVE
1099 # define MAP_NORESERVE 0
1100 #endif
1102 #define MMAP(addr, size, prot, flags) \
1103 __mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0)
1107 ----------------------- Chunk representations -----------------------
1112 This struct declaration is misleading (but accurate and necessary).
1113 It declares a "view" into memory allowing access to necessary
1114 fields at known offsets from a given base. See explanation below.
1117 struct malloc_chunk {
1119 INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
1120 INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
1122 struct malloc_chunk* fd; /* double links -- used only if free. */
1123 struct malloc_chunk* bk;
1125 /* Only used for large blocks: pointer to next larger size. */
1126 struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */
1127 struct malloc_chunk* bk_nextsize;
1132 malloc_chunk details:
1134 (The following includes lightly edited explanations by Colin Plumb.)
1136 Chunks of memory are maintained using a `boundary tag' method as
1137 described in e.g., Knuth or Standish. (See the paper by Paul
1138 Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
1139 survey of such techniques.) Sizes of free chunks are stored both
1140 in the front of each chunk and at the end. This makes
1141 consolidating fragmented chunks into bigger chunks very fast. The
1142 size fields also hold bits representing whether chunks are free or
1143 in use.
1145 An allocated chunk looks like this:
1148 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1149 | Size of previous chunk, if allocated | |
1150 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1151 | Size of chunk, in bytes |M|P|
1152 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1153 | User data starts here... .
1155 . (malloc_usable_size() bytes) .
1157 nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1158 | Size of chunk |
1159 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1162 Where "chunk" is the front of the chunk for the purpose of most of
1163 the malloc code, but "mem" is the pointer that is returned to the
1164 user. "Nextchunk" is the beginning of the next contiguous chunk.
1166 Chunks always begin on even word boundaries, so the mem portion
1167 (which is returned to the user) is also on an even word boundary, and
1168 thus at least double-word aligned.
1170 Free chunks are stored in circular doubly-linked lists, and look like this:
1172 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1173 | Size of previous chunk |
1174 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1175 `head:' | Size of chunk, in bytes |P|
1176 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1177 | Forward pointer to next chunk in list |
1178 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1179 | Back pointer to previous chunk in list |
1180 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1181 | Unused space (may be 0 bytes long) .
1184 nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1185 `foot:' | Size of chunk, in bytes |
1186 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1188 The P (PREV_INUSE) bit, stored in the unused low-order bit of the
1189 chunk size (which is always a multiple of two words), is an in-use
1190 bit for the *previous* chunk. If that bit is *clear*, then the
1191 word before the current chunk size contains the previous chunk
1192 size, and can be used to find the front of the previous chunk.
1193 The very first chunk allocated always has this bit set,
1194 preventing access to non-existent (or non-owned) memory. If
1195 prev_inuse is set for any given chunk, then you CANNOT determine
1196 the size of the previous chunk, and might even get a memory
1197 addressing fault when trying to do so.
1199 Note that the `foot' of the current chunk is actually represented
1200 as the prev_size of the NEXT chunk. This makes it easier to
1201 deal with alignments etc but can be very confusing when trying
1202 to extend or adapt this code.
1204 The two exceptions to all this are
1206 1. The special chunk `top' doesn't bother using the
1207 trailing size field since there is no next contiguous chunk
1208 that would have to index off it. After initialization, `top'
1209 is forced to always exist. If it would become less than
1210 MINSIZE bytes long, it is replenished.
1212 2. Chunks allocated via mmap, which have the second-lowest-order
1213 bit M (IS_MMAPPED) set in their size fields. Because they are
1214 allocated one-by-one, each must contain its own trailing size field.
1219 ---------- Size and alignment checks and conversions ----------
1222 /* conversion from malloc headers to user pointers, and back */
1224 #define chunk2mem(p) ((void*)((char*)(p) + 2*SIZE_SZ))
1225 #define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
1227 /* The smallest possible chunk */
1228 #define MIN_CHUNK_SIZE (offsetof(struct malloc_chunk, fd_nextsize))
1230 /* The smallest size we can malloc is an aligned minimal chunk */
1232 #define MINSIZE \
1233 (unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
1235 /* Check if m has acceptable alignment */
1237 #define aligned_OK(m) (((unsigned long)(m) & MALLOC_ALIGN_MASK) == 0)
1239 #define misaligned_chunk(p) \
1240 ((uintptr_t)(MALLOC_ALIGNMENT == 2 * SIZE_SZ ? (p) : chunk2mem (p)) \
1241 & MALLOC_ALIGN_MASK)
1245 Check if a request is so large that it would wrap around zero when
1246 padded and aligned. To simplify some other code, the bound is made
1247 low enough so that adding MINSIZE will also not wrap around zero.
1250 #define REQUEST_OUT_OF_RANGE(req) \
1251 ((unsigned long)(req) >= \
1252 (unsigned long)(INTERNAL_SIZE_T)(-2 * MINSIZE))
1254 /* pad request bytes into a usable size -- internal version */
1256 #define request2size(req) \
1257 (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE) ? \
1258 MINSIZE : \
1259 ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
1261 /* Same, except also perform argument check */
1263 #define checked_request2size(req, sz) \
1264 if (REQUEST_OUT_OF_RANGE(req)) { \
1265 __set_errno (ENOMEM); \
1266 return 0; \
1268 (sz) = request2size(req);
1271 --------------- Physical chunk operations ---------------
1275 /* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
1276 #define PREV_INUSE 0x1
1278 /* extract inuse bit of previous chunk */
1279 #define prev_inuse(p) ((p)->size & PREV_INUSE)
1282 /* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
1283 #define IS_MMAPPED 0x2
1285 /* check for mmap()'ed chunk */
1286 #define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
1289 /* size field is or'ed with NON_MAIN_ARENA if the chunk was obtained
1290 from a non-main arena. This is only set immediately before handing
1291 the chunk to the user, if necessary. */
1292 #define NON_MAIN_ARENA 0x4
1294 /* check for chunk from non-main arena */
1295 #define chunk_non_main_arena(p) ((p)->size & NON_MAIN_ARENA)
1299 Bits to mask off when extracting size
1301 Note: IS_MMAPPED is intentionally not masked off from size field in
1302 macros for which mmapped chunks should never be seen. This should
1303 cause helpful core dumps to occur if it is tried by accident by
1304 people extending or adapting this malloc.
1306 #define SIZE_BITS (PREV_INUSE|IS_MMAPPED|NON_MAIN_ARENA)
1308 /* Get size, ignoring use bits */
1309 #define chunksize(p) ((p)->size & ~(SIZE_BITS))
1312 /* Ptr to next physical malloc_chunk. */
1313 #define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~SIZE_BITS) ))
1315 /* Ptr to previous physical malloc_chunk */
1316 #define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
1318 /* Treat space at ptr + offset as a chunk */
1319 #define chunk_at_offset(p, s) ((mchunkptr)(((char*)(p)) + (s)))
1321 /* extract p's inuse bit */
1322 #define inuse(p)\
1323 ((((mchunkptr)(((char*)(p))+((p)->size & ~SIZE_BITS)))->size) & PREV_INUSE)
1325 /* set/clear chunk as being inuse without otherwise disturbing */
1326 #define set_inuse(p)\
1327 ((mchunkptr)(((char*)(p)) + ((p)->size & ~SIZE_BITS)))->size |= PREV_INUSE
1329 #define clear_inuse(p)\
1330 ((mchunkptr)(((char*)(p)) + ((p)->size & ~SIZE_BITS)))->size &= ~(PREV_INUSE)
1333 /* check/set/clear inuse bits in known places */
1334 #define inuse_bit_at_offset(p, s)\
1335 (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
1337 #define set_inuse_bit_at_offset(p, s)\
1338 (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
1340 #define clear_inuse_bit_at_offset(p, s)\
1341 (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
1344 /* Set size at head, without disturbing its use bit */
1345 #define set_head_size(p, s) ((p)->size = (((p)->size & SIZE_BITS) | (s)))
1347 /* Set size/use field */
1348 #define set_head(p, s) ((p)->size = (s))
1350 /* Set size at footer (only when chunk is not in use) */
1351 #define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
1355 -------------------- Internal data structures --------------------
1357 All internal state is held in an instance of malloc_state defined
1358 below. There are no other static variables, except in two optional
1359 cases:
1360 * If USE_MALLOC_LOCK is defined, the mALLOC_MUTEx declared above.
1361 * If mmap doesn't support MAP_ANONYMOUS, a dummy file descriptor
1362 for mmap.
1364 Beware of lots of tricks that minimize the total bookkeeping space
1365 requirements. The result is a little over 1K bytes (for 4byte
1366 pointers and size_t.)
1370 Bins
1372 An array of bin headers for free chunks. Each bin is doubly
1373 linked. The bins are approximately proportionally (log) spaced.
1374 There are a lot of these bins (128). This may look excessive, but
1375 works very well in practice. Most bins hold sizes that are
1376 unusual as malloc request sizes, but are more usual for fragments
1377 and consolidated sets of chunks, which is what these bins hold, so
1378 they can be found quickly. All procedures maintain the invariant
1379 that no consolidated chunk physically borders another one, so each
1380 chunk in a list is known to be preceeded and followed by either
1381 inuse chunks or the ends of memory.
1383 Chunks in bins are kept in size order, with ties going to the
1384 approximately least recently used chunk. Ordering isn't needed
1385 for the small bins, which all contain the same-sized chunks, but
1386 facilitates best-fit allocation for larger chunks. These lists
1387 are just sequential. Keeping them in order almost never requires
1388 enough traversal to warrant using fancier ordered data
1389 structures.
1391 Chunks of the same size are linked with the most
1392 recently freed at the front, and allocations are taken from the
1393 back. This results in LRU (FIFO) allocation order, which tends
1394 to give each chunk an equal opportunity to be consolidated with
1395 adjacent freed chunks, resulting in larger free chunks and less
1396 fragmentation.
1398 To simplify use in double-linked lists, each bin header acts
1399 as a malloc_chunk. This avoids special-casing for headers.
1400 But to conserve space and improve locality, we allocate
1401 only the fd/bk pointers of bins, and then use repositioning tricks
1402 to treat these as the fields of a malloc_chunk*.
1405 typedef struct malloc_chunk* mbinptr;
1407 /* addressing -- note that bin_at(0) does not exist */
1408 #define bin_at(m, i) \
1409 (mbinptr) (((char *) &((m)->bins[((i) - 1) * 2])) \
1410 - offsetof (struct malloc_chunk, fd))
1412 /* analog of ++bin */
1413 #define next_bin(b) ((mbinptr)((char*)(b) + (sizeof(mchunkptr)<<1)))
1415 /* Reminders about list directionality within bins */
1416 #define first(b) ((b)->fd)
1417 #define last(b) ((b)->bk)
1419 /* Take a chunk off a bin list */
1420 #define unlink(P, BK, FD) { \
1421 FD = P->fd; \
1422 BK = P->bk; \
1423 if (__builtin_expect (FD->bk != P || BK->fd != P, 0)) \
1424 malloc_printerr (check_action, "corrupted double-linked list", P); \
1425 else { \
1426 FD->bk = BK; \
1427 BK->fd = FD; \
1428 if (!in_smallbin_range (P->size) \
1429 && __builtin_expect (P->fd_nextsize != NULL, 0)) { \
1430 assert (P->fd_nextsize->bk_nextsize == P); \
1431 assert (P->bk_nextsize->fd_nextsize == P); \
1432 if (FD->fd_nextsize == NULL) { \
1433 if (P->fd_nextsize == P) \
1434 FD->fd_nextsize = FD->bk_nextsize = FD; \
1435 else { \
1436 FD->fd_nextsize = P->fd_nextsize; \
1437 FD->bk_nextsize = P->bk_nextsize; \
1438 P->fd_nextsize->bk_nextsize = FD; \
1439 P->bk_nextsize->fd_nextsize = FD; \
1441 } else { \
1442 P->fd_nextsize->bk_nextsize = P->bk_nextsize; \
1443 P->bk_nextsize->fd_nextsize = P->fd_nextsize; \
1450 Indexing
1452 Bins for sizes < 512 bytes contain chunks of all the same size, spaced
1453 8 bytes apart. Larger bins are approximately logarithmically spaced:
1455 64 bins of size 8
1456 32 bins of size 64
1457 16 bins of size 512
1458 8 bins of size 4096
1459 4 bins of size 32768
1460 2 bins of size 262144
1461 1 bin of size what's left
1463 There is actually a little bit of slop in the numbers in bin_index
1464 for the sake of speed. This makes no difference elsewhere.
1466 The bins top out around 1MB because we expect to service large
1467 requests via mmap.
1469 Bin 0 does not exist. Bin 1 is the unordered list; if that would be
1470 a valid chunk size the small bins are bumped up one.
1473 #define NBINS 128
1474 #define NSMALLBINS 64
1475 #define SMALLBIN_WIDTH MALLOC_ALIGNMENT
1476 #define SMALLBIN_CORRECTION (MALLOC_ALIGNMENT > 2 * SIZE_SZ)
1477 #define MIN_LARGE_SIZE ((NSMALLBINS - SMALLBIN_CORRECTION) * SMALLBIN_WIDTH)
1479 #define in_smallbin_range(sz) \
1480 ((unsigned long)(sz) < (unsigned long)MIN_LARGE_SIZE)
1482 #define smallbin_index(sz) \
1483 ((SMALLBIN_WIDTH == 16 ? (((unsigned)(sz)) >> 4) : (((unsigned)(sz)) >> 3)) \
1484 + SMALLBIN_CORRECTION)
1486 #define largebin_index_32(sz) \
1487 (((((unsigned long)(sz)) >> 6) <= 38)? 56 + (((unsigned long)(sz)) >> 6): \
1488 ((((unsigned long)(sz)) >> 9) <= 20)? 91 + (((unsigned long)(sz)) >> 9): \
1489 ((((unsigned long)(sz)) >> 12) <= 10)? 110 + (((unsigned long)(sz)) >> 12): \
1490 ((((unsigned long)(sz)) >> 15) <= 4)? 119 + (((unsigned long)(sz)) >> 15): \
1491 ((((unsigned long)(sz)) >> 18) <= 2)? 124 + (((unsigned long)(sz)) >> 18): \
1492 126)
1494 #define largebin_index_32_big(sz) \
1495 (((((unsigned long)(sz)) >> 6) <= 45)? 49 + (((unsigned long)(sz)) >> 6): \
1496 ((((unsigned long)(sz)) >> 9) <= 20)? 91 + (((unsigned long)(sz)) >> 9): \
1497 ((((unsigned long)(sz)) >> 12) <= 10)? 110 + (((unsigned long)(sz)) >> 12): \
1498 ((((unsigned long)(sz)) >> 15) <= 4)? 119 + (((unsigned long)(sz)) >> 15): \
1499 ((((unsigned long)(sz)) >> 18) <= 2)? 124 + (((unsigned long)(sz)) >> 18): \
1500 126)
1502 // XXX It remains to be seen whether it is good to keep the widths of
1503 // XXX the buckets the same or whether it should be scaled by a factor
1504 // XXX of two as well.
1505 #define largebin_index_64(sz) \
1506 (((((unsigned long)(sz)) >> 6) <= 48)? 48 + (((unsigned long)(sz)) >> 6): \
1507 ((((unsigned long)(sz)) >> 9) <= 20)? 91 + (((unsigned long)(sz)) >> 9): \
1508 ((((unsigned long)(sz)) >> 12) <= 10)? 110 + (((unsigned long)(sz)) >> 12): \
1509 ((((unsigned long)(sz)) >> 15) <= 4)? 119 + (((unsigned long)(sz)) >> 15): \
1510 ((((unsigned long)(sz)) >> 18) <= 2)? 124 + (((unsigned long)(sz)) >> 18): \
1511 126)
1513 #define largebin_index(sz) \
1514 (SIZE_SZ == 8 ? largebin_index_64 (sz) \
1515 : MALLOC_ALIGNMENT == 16 ? largebin_index_32_big (sz) \
1516 : largebin_index_32 (sz))
1518 #define bin_index(sz) \
1519 ((in_smallbin_range(sz)) ? smallbin_index(sz) : largebin_index(sz))
1523 Unsorted chunks
1525 All remainders from chunk splits, as well as all returned chunks,
1526 are first placed in the "unsorted" bin. They are then placed
1527 in regular bins after malloc gives them ONE chance to be used before
1528 binning. So, basically, the unsorted_chunks list acts as a queue,
1529 with chunks being placed on it in free (and malloc_consolidate),
1530 and taken off (to be either used or placed in bins) in malloc.
1532 The NON_MAIN_ARENA flag is never set for unsorted chunks, so it
1533 does not have to be taken into account in size comparisons.
1536 /* The otherwise unindexable 1-bin is used to hold unsorted chunks. */
1537 #define unsorted_chunks(M) (bin_at(M, 1))
1542 The top-most available chunk (i.e., the one bordering the end of
1543 available memory) is treated specially. It is never included in
1544 any bin, is used only if no other chunk is available, and is
1545 released back to the system if it is very large (see
1546 M_TRIM_THRESHOLD). Because top initially
1547 points to its own bin with initial zero size, thus forcing
1548 extension on the first malloc request, we avoid having any special
1549 code in malloc to check whether it even exists yet. But we still
1550 need to do so when getting memory from system, so we make
1551 initial_top treat the bin as a legal but unusable chunk during the
1552 interval between initialization and the first call to
1553 sysmalloc. (This is somewhat delicate, since it relies on
1554 the 2 preceding words to be zero during this interval as well.)
1557 /* Conveniently, the unsorted bin can be used as dummy top on first call */
1558 #define initial_top(M) (unsorted_chunks(M))
1561 Binmap
1563 To help compensate for the large number of bins, a one-level index
1564 structure is used for bin-by-bin searching. `binmap' is a
1565 bitvector recording whether bins are definitely empty so they can
1566 be skipped over during during traversals. The bits are NOT always
1567 cleared as soon as bins are empty, but instead only
1568 when they are noticed to be empty during traversal in malloc.
1571 /* Conservatively use 32 bits per map word, even if on 64bit system */
1572 #define BINMAPSHIFT 5
1573 #define BITSPERMAP (1U << BINMAPSHIFT)
1574 #define BINMAPSIZE (NBINS / BITSPERMAP)
1576 #define idx2block(i) ((i) >> BINMAPSHIFT)
1577 #define idx2bit(i) ((1U << ((i) & ((1U << BINMAPSHIFT)-1))))
1579 #define mark_bin(m,i) ((m)->binmap[idx2block(i)] |= idx2bit(i))
1580 #define unmark_bin(m,i) ((m)->binmap[idx2block(i)] &= ~(idx2bit(i)))
1581 #define get_binmap(m,i) ((m)->binmap[idx2block(i)] & idx2bit(i))
1584 Fastbins
1586 An array of lists holding recently freed small chunks. Fastbins
1587 are not doubly linked. It is faster to single-link them, and
1588 since chunks are never removed from the middles of these lists,
1589 double linking is not necessary. Also, unlike regular bins, they
1590 are not even processed in FIFO order (they use faster LIFO) since
1591 ordering doesn't much matter in the transient contexts in which
1592 fastbins are normally used.
1594 Chunks in fastbins keep their inuse bit set, so they cannot
1595 be consolidated with other free chunks. malloc_consolidate
1596 releases all chunks in fastbins and consolidates them with
1597 other free chunks.
1600 typedef struct malloc_chunk* mfastbinptr;
1601 #define fastbin(ar_ptr, idx) ((ar_ptr)->fastbinsY[idx])
1603 /* offset 2 to use otherwise unindexable first 2 bins */
1604 #define fastbin_index(sz) \
1605 ((((unsigned int)(sz)) >> (SIZE_SZ == 8 ? 4 : 3)) - 2)
1608 /* The maximum fastbin request size we support */
1609 #define MAX_FAST_SIZE (80 * SIZE_SZ / 4)
1611 #define NFASTBINS (fastbin_index(request2size(MAX_FAST_SIZE))+1)
1614 FASTBIN_CONSOLIDATION_THRESHOLD is the size of a chunk in free()
1615 that triggers automatic consolidation of possibly-surrounding
1616 fastbin chunks. This is a heuristic, so the exact value should not
1617 matter too much. It is defined at half the default trim threshold as a
1618 compromise heuristic to only attempt consolidation if it is likely
1619 to lead to trimming. However, it is not dynamically tunable, since
1620 consolidation reduces fragmentation surrounding large chunks even
1621 if trimming is not used.
1624 #define FASTBIN_CONSOLIDATION_THRESHOLD (65536UL)
1627 Since the lowest 2 bits in max_fast don't matter in size comparisons,
1628 they are used as flags.
1632 FASTCHUNKS_BIT held in max_fast indicates that there are probably
1633 some fastbin chunks. It is set true on entering a chunk into any
1634 fastbin, and cleared only in malloc_consolidate.
1636 The truth value is inverted so that have_fastchunks will be true
1637 upon startup (since statics are zero-filled), simplifying
1638 initialization checks.
1641 #define FASTCHUNKS_BIT (1U)
1643 #define have_fastchunks(M) (((M)->flags & FASTCHUNKS_BIT) == 0)
1644 #define clear_fastchunks(M) catomic_or (&(M)->flags, FASTCHUNKS_BIT)
1645 #define set_fastchunks(M) catomic_and (&(M)->flags, ~FASTCHUNKS_BIT)
1648 NONCONTIGUOUS_BIT indicates that MORECORE does not return contiguous
1649 regions. Otherwise, contiguity is exploited in merging together,
1650 when possible, results from consecutive MORECORE calls.
1652 The initial value comes from MORECORE_CONTIGUOUS, but is
1653 changed dynamically if mmap is ever used as an sbrk substitute.
1656 #define NONCONTIGUOUS_BIT (2U)
1658 #define contiguous(M) (((M)->flags & NONCONTIGUOUS_BIT) == 0)
1659 #define noncontiguous(M) (((M)->flags & NONCONTIGUOUS_BIT) != 0)
1660 #define set_noncontiguous(M) ((M)->flags |= NONCONTIGUOUS_BIT)
1661 #define set_contiguous(M) ((M)->flags &= ~NONCONTIGUOUS_BIT)
1664 Set value of max_fast.
1665 Use impossibly small value if 0.
1666 Precondition: there are no existing fastbin chunks.
1667 Setting the value clears fastchunk bit but preserves noncontiguous bit.
1670 #define set_max_fast(s) \
1671 global_max_fast = (((s) == 0) \
1672 ? SMALLBIN_WIDTH: ((s + SIZE_SZ) & ~MALLOC_ALIGN_MASK))
1673 #define get_max_fast() global_max_fast
1677 ----------- Internal state representation and initialization -----------
1680 struct malloc_state {
1681 /* Serialize access. */
1682 mutex_t mutex;
1684 /* Flags (formerly in max_fast). */
1685 int flags;
1687 #if THREAD_STATS
1688 /* Statistics for locking. Only used if THREAD_STATS is defined. */
1689 long stat_lock_direct, stat_lock_loop, stat_lock_wait;
1690 #endif
1692 /* Fastbins */
1693 mfastbinptr fastbinsY[NFASTBINS];
1695 /* Base of the topmost chunk -- not otherwise kept in a bin */
1696 mchunkptr top;
1698 /* The remainder from the most recent split of a small request */
1699 mchunkptr last_remainder;
1701 /* Normal bins packed as described above */
1702 mchunkptr bins[NBINS * 2 - 2];
1704 /* Bitmap of bins */
1705 unsigned int binmap[BINMAPSIZE];
1707 /* Linked list */
1708 struct malloc_state *next;
1710 #ifdef PER_THREAD
1711 /* Linked list for free arenas. */
1712 struct malloc_state *next_free;
1713 #endif
1715 /* Memory allocated from the system in this arena. */
1716 INTERNAL_SIZE_T system_mem;
1717 INTERNAL_SIZE_T max_system_mem;
1720 struct malloc_par {
1721 /* Tunable parameters */
1722 unsigned long trim_threshold;
1723 INTERNAL_SIZE_T top_pad;
1724 INTERNAL_SIZE_T mmap_threshold;
1725 #ifdef PER_THREAD
1726 INTERNAL_SIZE_T arena_test;
1727 INTERNAL_SIZE_T arena_max;
1728 #endif
1730 /* Memory map support */
1731 int n_mmaps;
1732 int n_mmaps_max;
1733 int max_n_mmaps;
1734 /* the mmap_threshold is dynamic, until the user sets
1735 it manually, at which point we need to disable any
1736 dynamic behavior. */
1737 int no_dyn_threshold;
1739 /* Statistics */
1740 INTERNAL_SIZE_T mmapped_mem;
1741 /*INTERNAL_SIZE_T sbrked_mem;*/
1742 /*INTERNAL_SIZE_T max_sbrked_mem;*/
1743 INTERNAL_SIZE_T max_mmapped_mem;
1744 INTERNAL_SIZE_T max_total_mem; /* only kept for NO_THREADS */
1746 /* First address handed out by MORECORE/sbrk. */
1747 char* sbrk_base;
1750 /* There are several instances of this struct ("arenas") in this
1751 malloc. If you are adapting this malloc in a way that does NOT use
1752 a static or mmapped malloc_state, you MUST explicitly zero-fill it
1753 before using. This malloc relies on the property that malloc_state
1754 is initialized to all zeroes (as is true of C statics). */
1756 static struct malloc_state main_arena =
1758 .mutex = MUTEX_INITIALIZER,
1759 .next = &main_arena
1762 /* There is only one instance of the malloc parameters. */
1764 static struct malloc_par mp_ =
1766 .top_pad = DEFAULT_TOP_PAD,
1767 .n_mmaps_max = DEFAULT_MMAP_MAX,
1768 .mmap_threshold = DEFAULT_MMAP_THRESHOLD,
1769 .trim_threshold = DEFAULT_TRIM_THRESHOLD,
1770 #ifdef PER_THREAD
1771 # define NARENAS_FROM_NCORES(n) ((n) * (sizeof(long) == 4 ? 2 : 8))
1772 .arena_test = NARENAS_FROM_NCORES (1)
1773 #endif
1777 #ifdef PER_THREAD
1778 /* Non public mallopt parameters. */
1779 #define M_ARENA_TEST -7
1780 #define M_ARENA_MAX -8
1781 #endif
1784 /* Maximum size of memory handled in fastbins. */
1785 static INTERNAL_SIZE_T global_max_fast;
1788 Initialize a malloc_state struct.
1790 This is called only from within malloc_consolidate, which needs
1791 be called in the same contexts anyway. It is never called directly
1792 outside of malloc_consolidate because some optimizing compilers try
1793 to inline it at all call points, which turns out not to be an
1794 optimization at all. (Inlining it in malloc_consolidate is fine though.)
1797 static void malloc_init_state(mstate av)
1799 int i;
1800 mbinptr bin;
1802 /* Establish circular links for normal bins */
1803 for (i = 1; i < NBINS; ++i) {
1804 bin = bin_at(av,i);
1805 bin->fd = bin->bk = bin;
1808 #if MORECORE_CONTIGUOUS
1809 if (av != &main_arena)
1810 #endif
1811 set_noncontiguous(av);
1812 if (av == &main_arena)
1813 set_max_fast(DEFAULT_MXFAST);
1814 av->flags |= FASTCHUNKS_BIT;
1816 av->top = initial_top(av);
1820 Other internal utilities operating on mstates
1823 static void* sysmalloc(INTERNAL_SIZE_T, mstate);
1824 static int systrim(size_t, mstate);
1825 static void malloc_consolidate(mstate);
1828 /* -------------- Early definitions for debugging hooks ---------------- */
1830 /* Define and initialize the hook variables. These weak definitions must
1831 appear before any use of the variables in a function (arena.c uses one). */
1832 #ifndef weak_variable
1833 /* In GNU libc we want the hook variables to be weak definitions to
1834 avoid a problem with Emacs. */
1835 # define weak_variable weak_function
1836 #endif
1838 /* Forward declarations. */
1839 static void* malloc_hook_ini (size_t sz,
1840 const void *caller) __THROW;
1841 static void* realloc_hook_ini (void* ptr, size_t sz,
1842 const void *caller) __THROW;
1843 static void* memalign_hook_ini (size_t alignment, size_t sz,
1844 const void *caller) __THROW;
1846 void weak_variable (*__malloc_initialize_hook) (void) = NULL;
1847 void weak_variable (*__free_hook) (void *__ptr,
1848 const void *) = NULL;
1849 void *weak_variable (*__malloc_hook)
1850 (size_t __size, const void *) = malloc_hook_ini;
1851 void *weak_variable (*__realloc_hook)
1852 (void *__ptr, size_t __size, const void *)
1853 = realloc_hook_ini;
1854 void *weak_variable (*__memalign_hook)
1855 (size_t __alignment, size_t __size, const void *)
1856 = memalign_hook_ini;
1857 void weak_variable (*__after_morecore_hook) (void) = NULL;
1860 /* ---------------- Error behavior ------------------------------------ */
1862 #ifndef DEFAULT_CHECK_ACTION
1863 #define DEFAULT_CHECK_ACTION 3
1864 #endif
1866 static int check_action = DEFAULT_CHECK_ACTION;
1869 /* ------------------ Testing support ----------------------------------*/
1871 static int perturb_byte;
1873 static inline void
1874 alloc_perturb (char *p, size_t n)
1876 if (__glibc_unlikely (perturb_byte))
1877 memset (p, perturb_byte ^ 0xff, n);
1880 static inline void
1881 free_perturb (char *p, size_t n)
1883 if (__glibc_unlikely (perturb_byte))
1884 memset (p, perturb_byte, n);
1889 #include <stap-probe.h>
1891 /* ------------------- Support for multiple arenas -------------------- */
1892 #include "arena.c"
1895 Debugging support
1897 These routines make a number of assertions about the states
1898 of data structures that should be true at all times. If any
1899 are not true, it's very likely that a user program has somehow
1900 trashed memory. (It's also possible that there is a coding error
1901 in malloc. In which case, please report it!)
1904 #if ! MALLOC_DEBUG
1906 #define check_chunk(A,P)
1907 #define check_free_chunk(A,P)
1908 #define check_inuse_chunk(A,P)
1909 #define check_remalloced_chunk(A,P,N)
1910 #define check_malloced_chunk(A,P,N)
1911 #define check_malloc_state(A)
1913 #else
1915 #define check_chunk(A,P) do_check_chunk(A,P)
1916 #define check_free_chunk(A,P) do_check_free_chunk(A,P)
1917 #define check_inuse_chunk(A,P) do_check_inuse_chunk(A,P)
1918 #define check_remalloced_chunk(A,P,N) do_check_remalloced_chunk(A,P,N)
1919 #define check_malloced_chunk(A,P,N) do_check_malloced_chunk(A,P,N)
1920 #define check_malloc_state(A) do_check_malloc_state(A)
1923 Properties of all chunks
1926 static void do_check_chunk(mstate av, mchunkptr p)
1928 unsigned long sz = chunksize(p);
1929 /* min and max possible addresses assuming contiguous allocation */
1930 char* max_address = (char*)(av->top) + chunksize(av->top);
1931 char* min_address = max_address - av->system_mem;
1933 if (!chunk_is_mmapped(p)) {
1935 /* Has legal address ... */
1936 if (p != av->top) {
1937 if (contiguous(av)) {
1938 assert(((char*)p) >= min_address);
1939 assert(((char*)p + sz) <= ((char*)(av->top)));
1942 else {
1943 /* top size is always at least MINSIZE */
1944 assert((unsigned long)(sz) >= MINSIZE);
1945 /* top predecessor always marked inuse */
1946 assert(prev_inuse(p));
1950 else {
1951 /* address is outside main heap */
1952 if (contiguous(av) && av->top != initial_top(av)) {
1953 assert(((char*)p) < min_address || ((char*)p) >= max_address);
1955 /* chunk is page-aligned */
1956 assert(((p->prev_size + sz) & (GLRO(dl_pagesize)-1)) == 0);
1957 /* mem is aligned */
1958 assert(aligned_OK(chunk2mem(p)));
1963 Properties of free chunks
1966 static void do_check_free_chunk(mstate av, mchunkptr p)
1968 INTERNAL_SIZE_T sz = p->size & ~(PREV_INUSE|NON_MAIN_ARENA);
1969 mchunkptr next = chunk_at_offset(p, sz);
1971 do_check_chunk(av, p);
1973 /* Chunk must claim to be free ... */
1974 assert(!inuse(p));
1975 assert (!chunk_is_mmapped(p));
1977 /* Unless a special marker, must have OK fields */
1978 if ((unsigned long)(sz) >= MINSIZE)
1980 assert((sz & MALLOC_ALIGN_MASK) == 0);
1981 assert(aligned_OK(chunk2mem(p)));
1982 /* ... matching footer field */
1983 assert(next->prev_size == sz);
1984 /* ... and is fully consolidated */
1985 assert(prev_inuse(p));
1986 assert (next == av->top || inuse(next));
1988 /* ... and has minimally sane links */
1989 assert(p->fd->bk == p);
1990 assert(p->bk->fd == p);
1992 else /* markers are always of size SIZE_SZ */
1993 assert(sz == SIZE_SZ);
1997 Properties of inuse chunks
2000 static void do_check_inuse_chunk(mstate av, mchunkptr p)
2002 mchunkptr next;
2004 do_check_chunk(av, p);
2006 if (chunk_is_mmapped(p))
2007 return; /* mmapped chunks have no next/prev */
2009 /* Check whether it claims to be in use ... */
2010 assert(inuse(p));
2012 next = next_chunk(p);
2014 /* ... and is surrounded by OK chunks.
2015 Since more things can be checked with free chunks than inuse ones,
2016 if an inuse chunk borders them and debug is on, it's worth doing them.
2018 if (!prev_inuse(p)) {
2019 /* Note that we cannot even look at prev unless it is not inuse */
2020 mchunkptr prv = prev_chunk(p);
2021 assert(next_chunk(prv) == p);
2022 do_check_free_chunk(av, prv);
2025 if (next == av->top) {
2026 assert(prev_inuse(next));
2027 assert(chunksize(next) >= MINSIZE);
2029 else if (!inuse(next))
2030 do_check_free_chunk(av, next);
2034 Properties of chunks recycled from fastbins
2037 static void do_check_remalloced_chunk(mstate av, mchunkptr p, INTERNAL_SIZE_T s)
2039 INTERNAL_SIZE_T sz = p->size & ~(PREV_INUSE|NON_MAIN_ARENA);
2041 if (!chunk_is_mmapped(p)) {
2042 assert(av == arena_for_chunk(p));
2043 if (chunk_non_main_arena(p))
2044 assert(av != &main_arena);
2045 else
2046 assert(av == &main_arena);
2049 do_check_inuse_chunk(av, p);
2051 /* Legal size ... */
2052 assert((sz & MALLOC_ALIGN_MASK) == 0);
2053 assert((unsigned long)(sz) >= MINSIZE);
2054 /* ... and alignment */
2055 assert(aligned_OK(chunk2mem(p)));
2056 /* chunk is less than MINSIZE more than request */
2057 assert((long)(sz) - (long)(s) >= 0);
2058 assert((long)(sz) - (long)(s + MINSIZE) < 0);
2062 Properties of nonrecycled chunks at the point they are malloced
2065 static void do_check_malloced_chunk(mstate av, mchunkptr p, INTERNAL_SIZE_T s)
2067 /* same as recycled case ... */
2068 do_check_remalloced_chunk(av, p, s);
2071 ... plus, must obey implementation invariant that prev_inuse is
2072 always true of any allocated chunk; i.e., that each allocated
2073 chunk borders either a previously allocated and still in-use
2074 chunk, or the base of its memory arena. This is ensured
2075 by making all allocations from the `lowest' part of any found
2076 chunk. This does not necessarily hold however for chunks
2077 recycled via fastbins.
2080 assert(prev_inuse(p));
2085 Properties of malloc_state.
2087 This may be useful for debugging malloc, as well as detecting user
2088 programmer errors that somehow write into malloc_state.
2090 If you are extending or experimenting with this malloc, you can
2091 probably figure out how to hack this routine to print out or
2092 display chunk addresses, sizes, bins, and other instrumentation.
2095 static void do_check_malloc_state(mstate av)
2097 int i;
2098 mchunkptr p;
2099 mchunkptr q;
2100 mbinptr b;
2101 unsigned int idx;
2102 INTERNAL_SIZE_T size;
2103 unsigned long total = 0;
2104 int max_fast_bin;
2106 /* internal size_t must be no wider than pointer type */
2107 assert(sizeof(INTERNAL_SIZE_T) <= sizeof(char*));
2109 /* alignment is a power of 2 */
2110 assert((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-1)) == 0);
2112 /* cannot run remaining checks until fully initialized */
2113 if (av->top == 0 || av->top == initial_top(av))
2114 return;
2116 /* pagesize is a power of 2 */
2117 assert((GLRO(dl_pagesize) & (GLRO(dl_pagesize)-1)) == 0);
2119 /* A contiguous main_arena is consistent with sbrk_base. */
2120 if (av == &main_arena && contiguous(av))
2121 assert((char*)mp_.sbrk_base + av->system_mem ==
2122 (char*)av->top + chunksize(av->top));
2124 /* properties of fastbins */
2126 /* max_fast is in allowed range */
2127 assert((get_max_fast () & ~1) <= request2size(MAX_FAST_SIZE));
2129 max_fast_bin = fastbin_index(get_max_fast ());
2131 for (i = 0; i < NFASTBINS; ++i) {
2132 p = fastbin (av, i);
2134 /* The following test can only be performed for the main arena.
2135 While mallopt calls malloc_consolidate to get rid of all fast
2136 bins (especially those larger than the new maximum) this does
2137 only happen for the main arena. Trying to do this for any
2138 other arena would mean those arenas have to be locked and
2139 malloc_consolidate be called for them. This is excessive. And
2140 even if this is acceptable to somebody it still cannot solve
2141 the problem completely since if the arena is locked a
2142 concurrent malloc call might create a new arena which then
2143 could use the newly invalid fast bins. */
2145 /* all bins past max_fast are empty */
2146 if (av == &main_arena && i > max_fast_bin)
2147 assert(p == 0);
2149 while (p != 0) {
2150 /* each chunk claims to be inuse */
2151 do_check_inuse_chunk(av, p);
2152 total += chunksize(p);
2153 /* chunk belongs in this bin */
2154 assert(fastbin_index(chunksize(p)) == i);
2155 p = p->fd;
2159 if (total != 0)
2160 assert(have_fastchunks(av));
2161 else if (!have_fastchunks(av))
2162 assert(total == 0);
2164 /* check normal bins */
2165 for (i = 1; i < NBINS; ++i) {
2166 b = bin_at(av,i);
2168 /* binmap is accurate (except for bin 1 == unsorted_chunks) */
2169 if (i >= 2) {
2170 unsigned int binbit = get_binmap(av,i);
2171 int empty = last(b) == b;
2172 if (!binbit)
2173 assert(empty);
2174 else if (!empty)
2175 assert(binbit);
2178 for (p = last(b); p != b; p = p->bk) {
2179 /* each chunk claims to be free */
2180 do_check_free_chunk(av, p);
2181 size = chunksize(p);
2182 total += size;
2183 if (i >= 2) {
2184 /* chunk belongs in bin */
2185 idx = bin_index(size);
2186 assert(idx == i);
2187 /* lists are sorted */
2188 assert(p->bk == b ||
2189 (unsigned long)chunksize(p->bk) >= (unsigned long)chunksize(p));
2191 if (!in_smallbin_range(size))
2193 if (p->fd_nextsize != NULL)
2195 if (p->fd_nextsize == p)
2196 assert (p->bk_nextsize == p);
2197 else
2199 if (p->fd_nextsize == first (b))
2200 assert (chunksize (p) < chunksize (p->fd_nextsize));
2201 else
2202 assert (chunksize (p) > chunksize (p->fd_nextsize));
2204 if (p == first (b))
2205 assert (chunksize (p) > chunksize (p->bk_nextsize));
2206 else
2207 assert (chunksize (p) < chunksize (p->bk_nextsize));
2210 else
2211 assert (p->bk_nextsize == NULL);
2213 } else if (!in_smallbin_range(size))
2214 assert (p->fd_nextsize == NULL && p->bk_nextsize == NULL);
2215 /* chunk is followed by a legal chain of inuse chunks */
2216 for (q = next_chunk(p);
2217 (q != av->top && inuse(q) &&
2218 (unsigned long)(chunksize(q)) >= MINSIZE);
2219 q = next_chunk(q))
2220 do_check_inuse_chunk(av, q);
2224 /* top chunk is OK */
2225 check_chunk(av, av->top);
2228 #endif
2231 /* ----------------- Support for debugging hooks -------------------- */
2232 #include "hooks.c"
2235 /* ----------- Routines dealing with system allocation -------------- */
2238 sysmalloc handles malloc cases requiring more memory from the system.
2239 On entry, it is assumed that av->top does not have enough
2240 space to service request for nb bytes, thus requiring that av->top
2241 be extended or replaced.
2244 static void* sysmalloc(INTERNAL_SIZE_T nb, mstate av)
2246 mchunkptr old_top; /* incoming value of av->top */
2247 INTERNAL_SIZE_T old_size; /* its size */
2248 char* old_end; /* its end address */
2250 long size; /* arg to first MORECORE or mmap call */
2251 char* brk; /* return value from MORECORE */
2253 long correction; /* arg to 2nd MORECORE call */
2254 char* snd_brk; /* 2nd return val */
2256 INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of new space */
2257 INTERNAL_SIZE_T end_misalign; /* partial page left at end of new space */
2258 char* aligned_brk; /* aligned offset into brk */
2260 mchunkptr p; /* the allocated/returned chunk */
2261 mchunkptr remainder; /* remainder from allocation */
2262 unsigned long remainder_size; /* its size */
2265 size_t pagemask = GLRO(dl_pagesize) - 1;
2266 bool tried_mmap = false;
2270 If have mmap, and the request size meets the mmap threshold, and
2271 the system supports mmap, and there are few enough currently
2272 allocated mmapped regions, try to directly map this request
2273 rather than expanding top.
2276 if ((unsigned long)(nb) >= (unsigned long)(mp_.mmap_threshold) &&
2277 (mp_.n_mmaps < mp_.n_mmaps_max)) {
2279 char* mm; /* return value from mmap call*/
2281 try_mmap:
2283 Round up size to nearest page. For mmapped chunks, the overhead
2284 is one SIZE_SZ unit larger than for normal chunks, because there
2285 is no following chunk whose prev_size field could be used.
2287 See the front_misalign handling below, for glibc there is no
2288 need for further alignments unless we have have high alignment.
2290 if (MALLOC_ALIGNMENT == 2 * SIZE_SZ)
2291 size = (nb + SIZE_SZ + pagemask) & ~pagemask;
2292 else
2293 size = (nb + SIZE_SZ + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
2294 tried_mmap = true;
2296 /* Don't try if size wraps around 0 */
2297 if ((unsigned long)(size) > (unsigned long)(nb)) {
2299 mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE, 0));
2301 if (mm != MAP_FAILED) {
2304 The offset to the start of the mmapped region is stored
2305 in the prev_size field of the chunk. This allows us to adjust
2306 returned start address to meet alignment requirements here
2307 and in memalign(), and still be able to compute proper
2308 address argument for later munmap in free() and realloc().
2311 if (MALLOC_ALIGNMENT == 2 * SIZE_SZ)
2313 /* For glibc, chunk2mem increases the address by 2*SIZE_SZ and
2314 MALLOC_ALIGN_MASK is 2*SIZE_SZ-1. Each mmap'ed area is page
2315 aligned and therefore definitely MALLOC_ALIGN_MASK-aligned. */
2316 assert (((INTERNAL_SIZE_T)chunk2mem(mm) & MALLOC_ALIGN_MASK) == 0);
2317 front_misalign = 0;
2319 else
2320 front_misalign = (INTERNAL_SIZE_T)chunk2mem(mm) & MALLOC_ALIGN_MASK;
2321 if (front_misalign > 0) {
2322 correction = MALLOC_ALIGNMENT - front_misalign;
2323 p = (mchunkptr)(mm + correction);
2324 p->prev_size = correction;
2325 set_head(p, (size - correction) |IS_MMAPPED);
2327 else
2329 p = (mchunkptr)mm;
2330 set_head(p, size|IS_MMAPPED);
2333 /* update statistics */
2335 int new = atomic_exchange_and_add (&mp_.n_mmaps, 1) + 1;
2336 atomic_max (&mp_.max_n_mmaps, new);
2338 unsigned long sum;
2339 sum = atomic_exchange_and_add(&mp_.mmapped_mem, size) + size;
2340 atomic_max (&mp_.max_mmapped_mem, sum);
2342 check_chunk(av, p);
2344 return chunk2mem(p);
2349 /* Record incoming configuration of top */
2351 old_top = av->top;
2352 old_size = chunksize(old_top);
2353 old_end = (char*)(chunk_at_offset(old_top, old_size));
2355 brk = snd_brk = (char*)(MORECORE_FAILURE);
2358 If not the first time through, we require old_size to be
2359 at least MINSIZE and to have prev_inuse set.
2362 assert((old_top == initial_top(av) && old_size == 0) ||
2363 ((unsigned long) (old_size) >= MINSIZE &&
2364 prev_inuse(old_top) &&
2365 ((unsigned long)old_end & pagemask) == 0));
2367 /* Precondition: not enough current space to satisfy nb request */
2368 assert((unsigned long)(old_size) < (unsigned long)(nb + MINSIZE));
2371 if (av != &main_arena) {
2373 heap_info *old_heap, *heap;
2374 size_t old_heap_size;
2376 /* First try to extend the current heap. */
2377 old_heap = heap_for_ptr(old_top);
2378 old_heap_size = old_heap->size;
2379 if ((long) (MINSIZE + nb - old_size) > 0
2380 && grow_heap(old_heap, MINSIZE + nb - old_size) == 0) {
2381 av->system_mem += old_heap->size - old_heap_size;
2382 arena_mem += old_heap->size - old_heap_size;
2383 set_head(old_top, (((char *)old_heap + old_heap->size) - (char *)old_top)
2384 | PREV_INUSE);
2386 else if ((heap = new_heap(nb + (MINSIZE + sizeof(*heap)), mp_.top_pad))) {
2387 /* Use a newly allocated heap. */
2388 heap->ar_ptr = av;
2389 heap->prev = old_heap;
2390 av->system_mem += heap->size;
2391 arena_mem += heap->size;
2392 /* Set up the new top. */
2393 top(av) = chunk_at_offset(heap, sizeof(*heap));
2394 set_head(top(av), (heap->size - sizeof(*heap)) | PREV_INUSE);
2396 /* Setup fencepost and free the old top chunk with a multiple of
2397 MALLOC_ALIGNMENT in size. */
2398 /* The fencepost takes at least MINSIZE bytes, because it might
2399 become the top chunk again later. Note that a footer is set
2400 up, too, although the chunk is marked in use. */
2401 old_size = (old_size - MINSIZE) & ~MALLOC_ALIGN_MASK;
2402 set_head(chunk_at_offset(old_top, old_size + 2*SIZE_SZ), 0|PREV_INUSE);
2403 if (old_size >= MINSIZE) {
2404 set_head(chunk_at_offset(old_top, old_size), (2*SIZE_SZ)|PREV_INUSE);
2405 set_foot(chunk_at_offset(old_top, old_size), (2*SIZE_SZ));
2406 set_head(old_top, old_size|PREV_INUSE|NON_MAIN_ARENA);
2407 _int_free(av, old_top, 1);
2408 } else {
2409 set_head(old_top, (old_size + 2*SIZE_SZ)|PREV_INUSE);
2410 set_foot(old_top, (old_size + 2*SIZE_SZ));
2413 else if (!tried_mmap)
2414 /* We can at least try to use to mmap memory. */
2415 goto try_mmap;
2417 } else { /* av == main_arena */
2420 /* Request enough space for nb + pad + overhead */
2422 size = nb + mp_.top_pad + MINSIZE;
2425 If contiguous, we can subtract out existing space that we hope to
2426 combine with new space. We add it back later only if
2427 we don't actually get contiguous space.
2430 if (contiguous(av))
2431 size -= old_size;
2434 Round to a multiple of page size.
2435 If MORECORE is not contiguous, this ensures that we only call it
2436 with whole-page arguments. And if MORECORE is contiguous and
2437 this is not first time through, this preserves page-alignment of
2438 previous calls. Otherwise, we correct to page-align below.
2441 size = (size + pagemask) & ~pagemask;
2444 Don't try to call MORECORE if argument is so big as to appear
2445 negative. Note that since mmap takes size_t arg, it may succeed
2446 below even if we cannot call MORECORE.
2449 if (size > 0) {
2450 brk = (char*)(MORECORE(size));
2451 LIBC_PROBE (memory_sbrk_more, 2, brk, size);
2454 if (brk != (char*)(MORECORE_FAILURE)) {
2455 /* Call the `morecore' hook if necessary. */
2456 void (*hook) (void) = atomic_forced_read (__after_morecore_hook);
2457 if (__builtin_expect (hook != NULL, 0))
2458 (*hook) ();
2459 } else {
2461 If have mmap, try using it as a backup when MORECORE fails or
2462 cannot be used. This is worth doing on systems that have "holes" in
2463 address space, so sbrk cannot extend to give contiguous space, but
2464 space is available elsewhere. Note that we ignore mmap max count
2465 and threshold limits, since the space will not be used as a
2466 segregated mmap region.
2469 /* Cannot merge with old top, so add its size back in */
2470 if (contiguous(av))
2471 size = (size + old_size + pagemask) & ~pagemask;
2473 /* If we are relying on mmap as backup, then use larger units */
2474 if ((unsigned long)(size) < (unsigned long)(MMAP_AS_MORECORE_SIZE))
2475 size = MMAP_AS_MORECORE_SIZE;
2477 /* Don't try if size wraps around 0 */
2478 if ((unsigned long)(size) > (unsigned long)(nb)) {
2480 char *mbrk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE, 0));
2482 if (mbrk != MAP_FAILED) {
2484 /* We do not need, and cannot use, another sbrk call to find end */
2485 brk = mbrk;
2486 snd_brk = brk + size;
2489 Record that we no longer have a contiguous sbrk region.
2490 After the first time mmap is used as backup, we do not
2491 ever rely on contiguous space since this could incorrectly
2492 bridge regions.
2494 set_noncontiguous(av);
2499 if (brk != (char*)(MORECORE_FAILURE)) {
2500 if (mp_.sbrk_base == 0)
2501 mp_.sbrk_base = brk;
2502 av->system_mem += size;
2505 If MORECORE extends previous space, we can likewise extend top size.
2508 if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE))
2509 set_head(old_top, (size + old_size) | PREV_INUSE);
2511 else if (contiguous(av) && old_size && brk < old_end) {
2512 /* Oops! Someone else killed our space.. Can't touch anything. */
2513 malloc_printerr (3, "break adjusted to free malloc space", brk);
2517 Otherwise, make adjustments:
2519 * If the first time through or noncontiguous, we need to call sbrk
2520 just to find out where the end of memory lies.
2522 * We need to ensure that all returned chunks from malloc will meet
2523 MALLOC_ALIGNMENT
2525 * If there was an intervening foreign sbrk, we need to adjust sbrk
2526 request size to account for fact that we will not be able to
2527 combine new space with existing space in old_top.
2529 * Almost all systems internally allocate whole pages at a time, in
2530 which case we might as well use the whole last page of request.
2531 So we allocate enough more memory to hit a page boundary now,
2532 which in turn causes future contiguous calls to page-align.
2535 else {
2536 front_misalign = 0;
2537 end_misalign = 0;
2538 correction = 0;
2539 aligned_brk = brk;
2541 /* handle contiguous cases */
2542 if (contiguous(av)) {
2544 /* Count foreign sbrk as system_mem. */
2545 if (old_size)
2546 av->system_mem += brk - old_end;
2548 /* Guarantee alignment of first new chunk made from this space */
2550 front_misalign = (INTERNAL_SIZE_T)chunk2mem(brk) & MALLOC_ALIGN_MASK;
2551 if (front_misalign > 0) {
2554 Skip over some bytes to arrive at an aligned position.
2555 We don't need to specially mark these wasted front bytes.
2556 They will never be accessed anyway because
2557 prev_inuse of av->top (and any chunk created from its start)
2558 is always true after initialization.
2561 correction = MALLOC_ALIGNMENT - front_misalign;
2562 aligned_brk += correction;
2566 If this isn't adjacent to existing space, then we will not
2567 be able to merge with old_top space, so must add to 2nd request.
2570 correction += old_size;
2572 /* Extend the end address to hit a page boundary */
2573 end_misalign = (INTERNAL_SIZE_T)(brk + size + correction);
2574 correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
2576 assert(correction >= 0);
2577 snd_brk = (char*)(MORECORE(correction));
2580 If can't allocate correction, try to at least find out current
2581 brk. It might be enough to proceed without failing.
2583 Note that if second sbrk did NOT fail, we assume that space
2584 is contiguous with first sbrk. This is a safe assumption unless
2585 program is multithreaded but doesn't use locks and a foreign sbrk
2586 occurred between our first and second calls.
2589 if (snd_brk == (char*)(MORECORE_FAILURE)) {
2590 correction = 0;
2591 snd_brk = (char*)(MORECORE(0));
2592 } else {
2593 /* Call the `morecore' hook if necessary. */
2594 void (*hook) (void) = atomic_forced_read (__after_morecore_hook);
2595 if (__builtin_expect (hook != NULL, 0))
2596 (*hook) ();
2600 /* handle non-contiguous cases */
2601 else {
2602 if (MALLOC_ALIGNMENT == 2 * SIZE_SZ)
2603 /* MORECORE/mmap must correctly align */
2604 assert(((unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK) == 0);
2605 else {
2606 front_misalign = (INTERNAL_SIZE_T)chunk2mem(brk) & MALLOC_ALIGN_MASK;
2607 if (front_misalign > 0) {
2610 Skip over some bytes to arrive at an aligned position.
2611 We don't need to specially mark these wasted front bytes.
2612 They will never be accessed anyway because
2613 prev_inuse of av->top (and any chunk created from its start)
2614 is always true after initialization.
2617 aligned_brk += MALLOC_ALIGNMENT - front_misalign;
2621 /* Find out current end of memory */
2622 if (snd_brk == (char*)(MORECORE_FAILURE)) {
2623 snd_brk = (char*)(MORECORE(0));
2627 /* Adjust top based on results of second sbrk */
2628 if (snd_brk != (char*)(MORECORE_FAILURE)) {
2629 av->top = (mchunkptr)aligned_brk;
2630 set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
2631 av->system_mem += correction;
2634 If not the first time through, we either have a
2635 gap due to foreign sbrk or a non-contiguous region. Insert a
2636 double fencepost at old_top to prevent consolidation with space
2637 we don't own. These fenceposts are artificial chunks that are
2638 marked as inuse and are in any case too small to use. We need
2639 two to make sizes and alignments work out.
2642 if (old_size != 0) {
2644 Shrink old_top to insert fenceposts, keeping size a
2645 multiple of MALLOC_ALIGNMENT. We know there is at least
2646 enough space in old_top to do this.
2648 old_size = (old_size - 4*SIZE_SZ) & ~MALLOC_ALIGN_MASK;
2649 set_head(old_top, old_size | PREV_INUSE);
2652 Note that the following assignments completely overwrite
2653 old_top when old_size was previously MINSIZE. This is
2654 intentional. We need the fencepost, even if old_top otherwise gets
2655 lost.
2657 chunk_at_offset(old_top, old_size )->size =
2658 (2*SIZE_SZ)|PREV_INUSE;
2660 chunk_at_offset(old_top, old_size + 2*SIZE_SZ)->size =
2661 (2*SIZE_SZ)|PREV_INUSE;
2663 /* If possible, release the rest. */
2664 if (old_size >= MINSIZE) {
2665 _int_free(av, old_top, 1);
2673 } /* if (av != &main_arena) */
2675 if ((unsigned long)av->system_mem > (unsigned long)(av->max_system_mem))
2676 av->max_system_mem = av->system_mem;
2677 check_malloc_state(av);
2679 /* finally, do the allocation */
2680 p = av->top;
2681 size = chunksize(p);
2683 /* check that one of the above allocation paths succeeded */
2684 if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
2685 remainder_size = size - nb;
2686 remainder = chunk_at_offset(p, nb);
2687 av->top = remainder;
2688 set_head(p, nb | PREV_INUSE | (av != &main_arena ? NON_MAIN_ARENA : 0));
2689 set_head(remainder, remainder_size | PREV_INUSE);
2690 check_malloced_chunk(av, p, nb);
2691 return chunk2mem(p);
2694 /* catch all failure paths */
2695 __set_errno (ENOMEM);
2696 return 0;
2701 systrim is an inverse of sorts to sysmalloc. It gives memory back
2702 to the system (via negative arguments to sbrk) if there is unused
2703 memory at the `high' end of the malloc pool. It is called
2704 automatically by free() when top space exceeds the trim
2705 threshold. It is also called by the public malloc_trim routine. It
2706 returns 1 if it actually released any memory, else 0.
2709 static int systrim(size_t pad, mstate av)
2711 long top_size; /* Amount of top-most memory */
2712 long extra; /* Amount to release */
2713 long released; /* Amount actually released */
2714 char* current_brk; /* address returned by pre-check sbrk call */
2715 char* new_brk; /* address returned by post-check sbrk call */
2716 size_t pagesz;
2717 long top_area;
2719 pagesz = GLRO(dl_pagesize);
2720 top_size = chunksize(av->top);
2722 top_area = top_size - MINSIZE - 1;
2723 if (top_area <= pad)
2724 return 0;
2726 /* Release in pagesize units, keeping at least one page */
2727 extra = (top_area - pad) & ~(pagesz - 1);
2730 Only proceed if end of memory is where we last set it.
2731 This avoids problems if there were foreign sbrk calls.
2733 current_brk = (char*)(MORECORE(0));
2734 if (current_brk == (char*)(av->top) + top_size) {
2737 Attempt to release memory. We ignore MORECORE return value,
2738 and instead call again to find out where new end of memory is.
2739 This avoids problems if first call releases less than we asked,
2740 of if failure somehow altered brk value. (We could still
2741 encounter problems if it altered brk in some very bad way,
2742 but the only thing we can do is adjust anyway, which will cause
2743 some downstream failure.)
2746 MORECORE(-extra);
2747 /* Call the `morecore' hook if necessary. */
2748 void (*hook) (void) = atomic_forced_read (__after_morecore_hook);
2749 if (__builtin_expect (hook != NULL, 0))
2750 (*hook) ();
2751 new_brk = (char*)(MORECORE(0));
2753 LIBC_PROBE (memory_sbrk_less, 2, new_brk, extra);
2755 if (new_brk != (char*)MORECORE_FAILURE) {
2756 released = (long)(current_brk - new_brk);
2758 if (released != 0) {
2759 /* Success. Adjust top. */
2760 av->system_mem -= released;
2761 set_head(av->top, (top_size - released) | PREV_INUSE);
2762 check_malloc_state(av);
2763 return 1;
2767 return 0;
2770 static void
2771 internal_function
2772 munmap_chunk(mchunkptr p)
2774 INTERNAL_SIZE_T size = chunksize(p);
2776 assert (chunk_is_mmapped(p));
2778 uintptr_t block = (uintptr_t) p - p->prev_size;
2779 size_t total_size = p->prev_size + size;
2780 /* Unfortunately we have to do the compilers job by hand here. Normally
2781 we would test BLOCK and TOTAL-SIZE separately for compliance with the
2782 page size. But gcc does not recognize the optimization possibility
2783 (in the moment at least) so we combine the two values into one before
2784 the bit test. */
2785 if (__builtin_expect (((block | total_size) & (GLRO(dl_pagesize) - 1)) != 0, 0))
2787 malloc_printerr (check_action, "munmap_chunk(): invalid pointer",
2788 chunk2mem (p));
2789 return;
2792 atomic_decrement (&mp_.n_mmaps);
2793 atomic_add (&mp_.mmapped_mem, -total_size);
2795 /* If munmap failed the process virtual memory address space is in a
2796 bad shape. Just leave the block hanging around, the process will
2797 terminate shortly anyway since not much can be done. */
2798 __munmap((char *)block, total_size);
2801 #if HAVE_MREMAP
2803 static mchunkptr
2804 internal_function
2805 mremap_chunk(mchunkptr p, size_t new_size)
2807 size_t page_mask = GLRO(dl_pagesize) - 1;
2808 INTERNAL_SIZE_T offset = p->prev_size;
2809 INTERNAL_SIZE_T size = chunksize(p);
2810 char *cp;
2812 assert (chunk_is_mmapped(p));
2813 assert(((size + offset) & (GLRO(dl_pagesize)-1)) == 0);
2815 /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
2816 new_size = (new_size + offset + SIZE_SZ + page_mask) & ~page_mask;
2818 /* No need to remap if the number of pages does not change. */
2819 if (size + offset == new_size)
2820 return p;
2822 cp = (char *)__mremap((char *)p - offset, size + offset, new_size,
2823 MREMAP_MAYMOVE);
2825 if (cp == MAP_FAILED) return 0;
2827 p = (mchunkptr)(cp + offset);
2829 assert(aligned_OK(chunk2mem(p)));
2831 assert((p->prev_size == offset));
2832 set_head(p, (new_size - offset)|IS_MMAPPED);
2834 INTERNAL_SIZE_T new;
2835 new = atomic_exchange_and_add (&mp_.mmapped_mem, new_size - size - offset)
2836 + new_size - size - offset;
2837 atomic_max (&mp_.max_mmapped_mem, new);
2838 return p;
2841 #endif /* HAVE_MREMAP */
2843 /*------------------------ Public wrappers. --------------------------------*/
2845 void*
2846 __libc_malloc(size_t bytes)
2848 mstate ar_ptr;
2849 void *victim;
2851 void *(*hook) (size_t, const void *)
2852 = atomic_forced_read (__malloc_hook);
2853 if (__builtin_expect (hook != NULL, 0))
2854 return (*hook)(bytes, RETURN_ADDRESS (0));
2856 arena_lookup(ar_ptr);
2858 arena_lock(ar_ptr, bytes);
2859 if(!ar_ptr)
2860 return 0;
2861 victim = _int_malloc(ar_ptr, bytes);
2862 if(!victim) {
2863 LIBC_PROBE (memory_malloc_retry, 1, bytes);
2864 ar_ptr = arena_get_retry(ar_ptr, bytes);
2865 if (__builtin_expect(ar_ptr != NULL, 1)) {
2866 victim = _int_malloc(ar_ptr, bytes);
2867 (void)mutex_unlock(&ar_ptr->mutex);
2869 } else
2870 (void)mutex_unlock(&ar_ptr->mutex);
2871 assert(!victim || chunk_is_mmapped(mem2chunk(victim)) ||
2872 ar_ptr == arena_for_chunk(mem2chunk(victim)));
2873 return victim;
2875 libc_hidden_def(__libc_malloc)
2877 void
2878 __libc_free(void* mem)
2880 mstate ar_ptr;
2881 mchunkptr p; /* chunk corresponding to mem */
2883 void (*hook) (void *, const void *)
2884 = atomic_forced_read (__free_hook);
2885 if (__builtin_expect (hook != NULL, 0)) {
2886 (*hook)(mem, RETURN_ADDRESS (0));
2887 return;
2890 if (mem == 0) /* free(0) has no effect */
2891 return;
2893 p = mem2chunk(mem);
2895 if (chunk_is_mmapped(p)) /* release mmapped memory. */
2897 /* see if the dynamic brk/mmap threshold needs adjusting */
2898 if (!mp_.no_dyn_threshold
2899 && p->size > mp_.mmap_threshold
2900 && p->size <= DEFAULT_MMAP_THRESHOLD_MAX)
2902 mp_.mmap_threshold = chunksize (p);
2903 mp_.trim_threshold = 2 * mp_.mmap_threshold;
2904 LIBC_PROBE (memory_mallopt_free_dyn_thresholds, 2,
2905 mp_.mmap_threshold, mp_.trim_threshold);
2907 munmap_chunk(p);
2908 return;
2911 ar_ptr = arena_for_chunk(p);
2912 _int_free(ar_ptr, p, 0);
2914 libc_hidden_def (__libc_free)
2916 void*
2917 __libc_realloc(void* oldmem, size_t bytes)
2919 mstate ar_ptr;
2920 INTERNAL_SIZE_T nb; /* padded request size */
2922 void* newp; /* chunk to return */
2924 void *(*hook) (void *, size_t, const void *) =
2925 atomic_forced_read (__realloc_hook);
2926 if (__builtin_expect (hook != NULL, 0))
2927 return (*hook)(oldmem, bytes, RETURN_ADDRESS (0));
2929 #if REALLOC_ZERO_BYTES_FREES
2930 if (bytes == 0 && oldmem != NULL) { __libc_free(oldmem); return 0; }
2931 #endif
2933 /* realloc of null is supposed to be same as malloc */
2934 if (oldmem == 0) return __libc_malloc(bytes);
2936 /* chunk corresponding to oldmem */
2937 const mchunkptr oldp = mem2chunk(oldmem);
2938 /* its size */
2939 const INTERNAL_SIZE_T oldsize = chunksize(oldp);
2941 /* Little security check which won't hurt performance: the
2942 allocator never wrapps around at the end of the address space.
2943 Therefore we can exclude some size values which might appear
2944 here by accident or by "design" from some intruder. */
2945 if (__builtin_expect ((uintptr_t) oldp > (uintptr_t) -oldsize, 0)
2946 || __builtin_expect (misaligned_chunk (oldp), 0))
2948 malloc_printerr (check_action, "realloc(): invalid pointer", oldmem);
2949 return NULL;
2952 checked_request2size(bytes, nb);
2954 if (chunk_is_mmapped(oldp))
2956 void* newmem;
2958 #if HAVE_MREMAP
2959 newp = mremap_chunk(oldp, nb);
2960 if(newp) return chunk2mem(newp);
2961 #endif
2962 /* Note the extra SIZE_SZ overhead. */
2963 if(oldsize - SIZE_SZ >= nb) return oldmem; /* do nothing */
2964 /* Must alloc, copy, free. */
2965 newmem = __libc_malloc(bytes);
2966 if (newmem == 0) return 0; /* propagate failure */
2967 MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
2968 munmap_chunk(oldp);
2969 return newmem;
2972 ar_ptr = arena_for_chunk(oldp);
2973 #if THREAD_STATS
2974 if(!mutex_trylock(&ar_ptr->mutex))
2975 ++(ar_ptr->stat_lock_direct);
2976 else {
2977 (void)mutex_lock(&ar_ptr->mutex);
2978 ++(ar_ptr->stat_lock_wait);
2980 #else
2981 (void)mutex_lock(&ar_ptr->mutex);
2982 #endif
2984 #if !defined PER_THREAD
2985 LIBC_PROBE (memory_arena_reuse_realloc, 1, ar_ptr);
2986 /* As in malloc(), remember this arena for the next allocation. */
2987 tsd_setspecific(arena_key, (void *)ar_ptr);
2988 #endif
2990 newp = _int_realloc(ar_ptr, oldp, oldsize, nb);
2992 (void)mutex_unlock(&ar_ptr->mutex);
2993 assert(!newp || chunk_is_mmapped(mem2chunk(newp)) ||
2994 ar_ptr == arena_for_chunk(mem2chunk(newp)));
2996 if (newp == NULL)
2998 /* Try harder to allocate memory in other arenas. */
2999 LIBC_PROBE (memory_realloc_retry, 2, bytes, oldmem);
3000 newp = __libc_malloc(bytes);
3001 if (newp != NULL)
3003 MALLOC_COPY (newp, oldmem, oldsize - SIZE_SZ);
3004 _int_free(ar_ptr, oldp, 0);
3008 return newp;
3010 libc_hidden_def (__libc_realloc)
3012 void*
3013 __libc_memalign(size_t alignment, size_t bytes)
3015 void *address = RETURN_ADDRESS (0);
3016 return _mid_memalign (alignment, bytes, address);
3019 static void *
3020 _mid_memalign (size_t alignment, size_t bytes, void *address)
3022 mstate ar_ptr;
3023 void *p;
3025 void *(*hook) (size_t, size_t, const void *) =
3026 atomic_forced_read (__memalign_hook);
3027 if (__builtin_expect (hook != NULL, 0))
3028 return (*hook)(alignment, bytes, address);
3030 /* If we need less alignment than we give anyway, just relay to malloc. */
3031 if (alignment <= MALLOC_ALIGNMENT) return __libc_malloc(bytes);
3033 /* Otherwise, ensure that it is at least a minimum chunk size */
3034 if (alignment < MINSIZE) alignment = MINSIZE;
3036 /* If the alignment is greater than SIZE_MAX / 2 + 1 it cannot be a
3037 power of 2 and will cause overflow in the check below. */
3038 if (alignment > SIZE_MAX / 2 + 1)
3040 __set_errno (EINVAL);
3041 return 0;
3044 /* Check for overflow. */
3045 if (bytes > SIZE_MAX - alignment - MINSIZE)
3047 __set_errno (ENOMEM);
3048 return 0;
3052 /* Make sure alignment is power of 2. */
3053 if (!powerof2(alignment)) {
3054 size_t a = MALLOC_ALIGNMENT * 2;
3055 while (a < alignment) a <<= 1;
3056 alignment = a;
3059 arena_get(ar_ptr, bytes + alignment + MINSIZE);
3060 if(!ar_ptr)
3061 return 0;
3062 p = _int_memalign(ar_ptr, alignment, bytes);
3063 if(!p) {
3064 LIBC_PROBE (memory_memalign_retry, 2, bytes, alignment);
3065 ar_ptr = arena_get_retry (ar_ptr, bytes);
3066 if (__builtin_expect(ar_ptr != NULL, 1)) {
3067 p = _int_memalign(ar_ptr, alignment, bytes);
3068 (void)mutex_unlock(&ar_ptr->mutex);
3070 } else
3071 (void)mutex_unlock(&ar_ptr->mutex);
3072 assert(!p || chunk_is_mmapped(mem2chunk(p)) ||
3073 ar_ptr == arena_for_chunk(mem2chunk(p)));
3074 return p;
3076 /* For ISO C11. */
3077 weak_alias (__libc_memalign, aligned_alloc)
3078 libc_hidden_def (__libc_memalign)
3080 void*
3081 __libc_valloc(size_t bytes)
3083 if(__malloc_initialized < 0)
3084 ptmalloc_init ();
3086 void *address = RETURN_ADDRESS (0);
3087 size_t pagesz = GLRO(dl_pagesize);
3088 return _mid_memalign (pagesz, bytes, address);
3091 void*
3092 __libc_pvalloc(size_t bytes)
3095 if(__malloc_initialized < 0)
3096 ptmalloc_init ();
3098 void *address = RETURN_ADDRESS (0);
3099 size_t pagesz = GLRO(dl_pagesize);
3100 size_t page_mask = GLRO(dl_pagesize) - 1;
3101 size_t rounded_bytes = (bytes + page_mask) & ~(page_mask);
3103 /* Check for overflow. */
3104 if (bytes > SIZE_MAX - 2*pagesz - MINSIZE)
3106 __set_errno (ENOMEM);
3107 return 0;
3110 return _mid_memalign (pagesz, rounded_bytes, address);
3113 void*
3114 __libc_calloc(size_t n, size_t elem_size)
3116 mstate av;
3117 mchunkptr oldtop, p;
3118 INTERNAL_SIZE_T bytes, sz, csz, oldtopsize;
3119 void* mem;
3120 unsigned long clearsize;
3121 unsigned long nclears;
3122 INTERNAL_SIZE_T* d;
3124 /* size_t is unsigned so the behavior on overflow is defined. */
3125 bytes = n * elem_size;
3126 #define HALF_INTERNAL_SIZE_T \
3127 (((INTERNAL_SIZE_T) 1) << (8 * sizeof (INTERNAL_SIZE_T) / 2))
3128 if (__builtin_expect ((n | elem_size) >= HALF_INTERNAL_SIZE_T, 0)) {
3129 if (elem_size != 0 && bytes / elem_size != n) {
3130 __set_errno (ENOMEM);
3131 return 0;
3135 void *(*hook) (size_t, const void *) =
3136 atomic_forced_read (__malloc_hook);
3137 if (__builtin_expect (hook != NULL, 0)) {
3138 sz = bytes;
3139 mem = (*hook)(sz, RETURN_ADDRESS (0));
3140 if(mem == 0)
3141 return 0;
3142 return memset(mem, 0, sz);
3145 sz = bytes;
3147 arena_get(av, sz);
3148 if(!av)
3149 return 0;
3151 /* Check if we hand out the top chunk, in which case there may be no
3152 need to clear. */
3153 #if MORECORE_CLEARS
3154 oldtop = top(av);
3155 oldtopsize = chunksize(top(av));
3156 #if MORECORE_CLEARS < 2
3157 /* Only newly allocated memory is guaranteed to be cleared. */
3158 if (av == &main_arena &&
3159 oldtopsize < mp_.sbrk_base + av->max_system_mem - (char *)oldtop)
3160 oldtopsize = (mp_.sbrk_base + av->max_system_mem - (char *)oldtop);
3161 #endif
3162 if (av != &main_arena)
3164 heap_info *heap = heap_for_ptr (oldtop);
3165 if (oldtopsize < (char *) heap + heap->mprotect_size - (char *) oldtop)
3166 oldtopsize = (char *) heap + heap->mprotect_size - (char *) oldtop;
3168 #endif
3169 mem = _int_malloc(av, sz);
3172 assert(!mem || chunk_is_mmapped(mem2chunk(mem)) ||
3173 av == arena_for_chunk(mem2chunk(mem)));
3175 if (mem == 0) {
3176 LIBC_PROBE (memory_calloc_retry, 1, sz);
3177 av = arena_get_retry (av, sz);
3178 if (__builtin_expect(av != NULL, 1)) {
3179 mem = _int_malloc(av, sz);
3180 (void)mutex_unlock(&av->mutex);
3182 if (mem == 0) return 0;
3183 } else
3184 (void)mutex_unlock(&av->mutex);
3185 p = mem2chunk(mem);
3187 /* Two optional cases in which clearing not necessary */
3188 if (chunk_is_mmapped (p))
3190 if (__builtin_expect (perturb_byte, 0))
3191 return MALLOC_ZERO (mem, sz);
3192 return mem;
3195 csz = chunksize(p);
3197 #if MORECORE_CLEARS
3198 if (perturb_byte == 0 && (p == oldtop && csz > oldtopsize)) {
3199 /* clear only the bytes from non-freshly-sbrked memory */
3200 csz = oldtopsize;
3202 #endif
3204 /* Unroll clear of <= 36 bytes (72 if 8byte sizes). We know that
3205 contents have an odd number of INTERNAL_SIZE_T-sized words;
3206 minimally 3. */
3207 d = (INTERNAL_SIZE_T*)mem;
3208 clearsize = csz - SIZE_SZ;
3209 nclears = clearsize / sizeof(INTERNAL_SIZE_T);
3210 assert(nclears >= 3);
3212 if (nclears > 9)
3213 return MALLOC_ZERO(d, clearsize);
3215 else {
3216 *(d+0) = 0;
3217 *(d+1) = 0;
3218 *(d+2) = 0;
3219 if (nclears > 4) {
3220 *(d+3) = 0;
3221 *(d+4) = 0;
3222 if (nclears > 6) {
3223 *(d+5) = 0;
3224 *(d+6) = 0;
3225 if (nclears > 8) {
3226 *(d+7) = 0;
3227 *(d+8) = 0;
3233 return mem;
3237 ------------------------------ malloc ------------------------------
3240 static void*
3241 _int_malloc(mstate av, size_t bytes)
3243 INTERNAL_SIZE_T nb; /* normalized request size */
3244 unsigned int idx; /* associated bin index */
3245 mbinptr bin; /* associated bin */
3247 mchunkptr victim; /* inspected/selected chunk */
3248 INTERNAL_SIZE_T size; /* its size */
3249 int victim_index; /* its bin index */
3251 mchunkptr remainder; /* remainder from a split */
3252 unsigned long remainder_size; /* its size */
3254 unsigned int block; /* bit map traverser */
3255 unsigned int bit; /* bit map traverser */
3256 unsigned int map; /* current word of binmap */
3258 mchunkptr fwd; /* misc temp for linking */
3259 mchunkptr bck; /* misc temp for linking */
3261 const char *errstr = NULL;
3264 Convert request size to internal form by adding SIZE_SZ bytes
3265 overhead plus possibly more to obtain necessary alignment and/or
3266 to obtain a size of at least MINSIZE, the smallest allocatable
3267 size. Also, checked_request2size traps (returning 0) request sizes
3268 that are so large that they wrap around zero when padded and
3269 aligned.
3272 checked_request2size(bytes, nb);
3275 If the size qualifies as a fastbin, first check corresponding bin.
3276 This code is safe to execute even if av is not yet initialized, so we
3277 can try it without checking, which saves some time on this fast path.
3280 if ((unsigned long)(nb) <= (unsigned long)(get_max_fast ())) {
3281 idx = fastbin_index(nb);
3282 mfastbinptr* fb = &fastbin (av, idx);
3283 mchunkptr pp = *fb;
3286 victim = pp;
3287 if (victim == NULL)
3288 break;
3290 while ((pp = catomic_compare_and_exchange_val_acq (fb, victim->fd, victim))
3291 != victim);
3292 if (victim != 0) {
3293 if (__builtin_expect (fastbin_index (chunksize (victim)) != idx, 0))
3295 errstr = "malloc(): memory corruption (fast)";
3296 errout:
3297 malloc_printerr (check_action, errstr, chunk2mem (victim));
3298 return NULL;
3300 check_remalloced_chunk(av, victim, nb);
3301 void *p = chunk2mem(victim);
3302 alloc_perturb (p, bytes);
3303 return p;
3308 If a small request, check regular bin. Since these "smallbins"
3309 hold one size each, no searching within bins is necessary.
3310 (For a large request, we need to wait until unsorted chunks are
3311 processed to find best fit. But for small ones, fits are exact
3312 anyway, so we can check now, which is faster.)
3315 if (in_smallbin_range(nb)) {
3316 idx = smallbin_index(nb);
3317 bin = bin_at(av,idx);
3319 if ( (victim = last(bin)) != bin) {
3320 if (victim == 0) /* initialization check */
3321 malloc_consolidate(av);
3322 else {
3323 bck = victim->bk;
3324 if (__builtin_expect (bck->fd != victim, 0))
3326 errstr = "malloc(): smallbin double linked list corrupted";
3327 goto errout;
3329 set_inuse_bit_at_offset(victim, nb);
3330 bin->bk = bck;
3331 bck->fd = bin;
3333 if (av != &main_arena)
3334 victim->size |= NON_MAIN_ARENA;
3335 check_malloced_chunk(av, victim, nb);
3336 void *p = chunk2mem(victim);
3337 alloc_perturb (p, bytes);
3338 return p;
3344 If this is a large request, consolidate fastbins before continuing.
3345 While it might look excessive to kill all fastbins before
3346 even seeing if there is space available, this avoids
3347 fragmentation problems normally associated with fastbins.
3348 Also, in practice, programs tend to have runs of either small or
3349 large requests, but less often mixtures, so consolidation is not
3350 invoked all that often in most programs. And the programs that
3351 it is called frequently in otherwise tend to fragment.
3354 else {
3355 idx = largebin_index(nb);
3356 if (have_fastchunks(av))
3357 malloc_consolidate(av);
3361 Process recently freed or remaindered chunks, taking one only if
3362 it is exact fit, or, if this a small request, the chunk is remainder from
3363 the most recent non-exact fit. Place other traversed chunks in
3364 bins. Note that this step is the only place in any routine where
3365 chunks are placed in bins.
3367 The outer loop here is needed because we might not realize until
3368 near the end of malloc that we should have consolidated, so must
3369 do so and retry. This happens at most once, and only when we would
3370 otherwise need to expand memory to service a "small" request.
3373 for(;;) {
3375 int iters = 0;
3376 while ( (victim = unsorted_chunks(av)->bk) != unsorted_chunks(av)) {
3377 bck = victim->bk;
3378 if (__builtin_expect (victim->size <= 2 * SIZE_SZ, 0)
3379 || __builtin_expect (victim->size > av->system_mem, 0))
3380 malloc_printerr (check_action, "malloc(): memory corruption",
3381 chunk2mem (victim));
3382 size = chunksize(victim);
3385 If a small request, try to use last remainder if it is the
3386 only chunk in unsorted bin. This helps promote locality for
3387 runs of consecutive small requests. This is the only
3388 exception to best-fit, and applies only when there is
3389 no exact fit for a small chunk.
3392 if (in_smallbin_range(nb) &&
3393 bck == unsorted_chunks(av) &&
3394 victim == av->last_remainder &&
3395 (unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
3397 /* split and reattach remainder */
3398 remainder_size = size - nb;
3399 remainder = chunk_at_offset(victim, nb);
3400 unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
3401 av->last_remainder = remainder;
3402 remainder->bk = remainder->fd = unsorted_chunks(av);
3403 if (!in_smallbin_range(remainder_size))
3405 remainder->fd_nextsize = NULL;
3406 remainder->bk_nextsize = NULL;
3409 set_head(victim, nb | PREV_INUSE |
3410 (av != &main_arena ? NON_MAIN_ARENA : 0));
3411 set_head(remainder, remainder_size | PREV_INUSE);
3412 set_foot(remainder, remainder_size);
3414 check_malloced_chunk(av, victim, nb);
3415 void *p = chunk2mem(victim);
3416 alloc_perturb (p, bytes);
3417 return p;
3420 /* remove from unsorted list */
3421 unsorted_chunks(av)->bk = bck;
3422 bck->fd = unsorted_chunks(av);
3424 /* Take now instead of binning if exact fit */
3426 if (size == nb) {
3427 set_inuse_bit_at_offset(victim, size);
3428 if (av != &main_arena)
3429 victim->size |= NON_MAIN_ARENA;
3430 check_malloced_chunk(av, victim, nb);
3431 void *p = chunk2mem(victim);
3432 alloc_perturb (p, bytes);
3433 return p;
3436 /* place chunk in bin */
3438 if (in_smallbin_range(size)) {
3439 victim_index = smallbin_index(size);
3440 bck = bin_at(av, victim_index);
3441 fwd = bck->fd;
3443 else {
3444 victim_index = largebin_index(size);
3445 bck = bin_at(av, victim_index);
3446 fwd = bck->fd;
3448 /* maintain large bins in sorted order */
3449 if (fwd != bck) {
3450 /* Or with inuse bit to speed comparisons */
3451 size |= PREV_INUSE;
3452 /* if smaller than smallest, bypass loop below */
3453 assert((bck->bk->size & NON_MAIN_ARENA) == 0);
3454 if ((unsigned long)(size) < (unsigned long)(bck->bk->size)) {
3455 fwd = bck;
3456 bck = bck->bk;
3458 victim->fd_nextsize = fwd->fd;
3459 victim->bk_nextsize = fwd->fd->bk_nextsize;
3460 fwd->fd->bk_nextsize = victim->bk_nextsize->fd_nextsize = victim;
3462 else {
3463 assert((fwd->size & NON_MAIN_ARENA) == 0);
3464 while ((unsigned long) size < fwd->size)
3466 fwd = fwd->fd_nextsize;
3467 assert((fwd->size & NON_MAIN_ARENA) == 0);
3470 if ((unsigned long) size == (unsigned long) fwd->size)
3471 /* Always insert in the second position. */
3472 fwd = fwd->fd;
3473 else
3475 victim->fd_nextsize = fwd;
3476 victim->bk_nextsize = fwd->bk_nextsize;
3477 fwd->bk_nextsize = victim;
3478 victim->bk_nextsize->fd_nextsize = victim;
3480 bck = fwd->bk;
3482 } else
3483 victim->fd_nextsize = victim->bk_nextsize = victim;
3486 mark_bin(av, victim_index);
3487 victim->bk = bck;
3488 victim->fd = fwd;
3489 fwd->bk = victim;
3490 bck->fd = victim;
3492 #define MAX_ITERS 10000
3493 if (++iters >= MAX_ITERS)
3494 break;
3498 If a large request, scan through the chunks of current bin in
3499 sorted order to find smallest that fits. Use the skip list for this.
3502 if (!in_smallbin_range(nb)) {
3503 bin = bin_at(av, idx);
3505 /* skip scan if empty or largest chunk is too small */
3506 if ((victim = first(bin)) != bin &&
3507 (unsigned long)(victim->size) >= (unsigned long)(nb)) {
3509 victim = victim->bk_nextsize;
3510 while (((unsigned long)(size = chunksize(victim)) <
3511 (unsigned long)(nb)))
3512 victim = victim->bk_nextsize;
3514 /* Avoid removing the first entry for a size so that the skip
3515 list does not have to be rerouted. */
3516 if (victim != last(bin) && victim->size == victim->fd->size)
3517 victim = victim->fd;
3519 remainder_size = size - nb;
3520 unlink(victim, bck, fwd);
3522 /* Exhaust */
3523 if (remainder_size < MINSIZE) {
3524 set_inuse_bit_at_offset(victim, size);
3525 if (av != &main_arena)
3526 victim->size |= NON_MAIN_ARENA;
3528 /* Split */
3529 else {
3530 remainder = chunk_at_offset(victim, nb);
3531 /* We cannot assume the unsorted list is empty and therefore
3532 have to perform a complete insert here. */
3533 bck = unsorted_chunks(av);
3534 fwd = bck->fd;
3535 if (__builtin_expect (fwd->bk != bck, 0))
3537 errstr = "malloc(): corrupted unsorted chunks";
3538 goto errout;
3540 remainder->bk = bck;
3541 remainder->fd = fwd;
3542 bck->fd = remainder;
3543 fwd->bk = remainder;
3544 if (!in_smallbin_range(remainder_size))
3546 remainder->fd_nextsize = NULL;
3547 remainder->bk_nextsize = NULL;
3549 set_head(victim, nb | PREV_INUSE |
3550 (av != &main_arena ? NON_MAIN_ARENA : 0));
3551 set_head(remainder, remainder_size | PREV_INUSE);
3552 set_foot(remainder, remainder_size);
3554 check_malloced_chunk(av, victim, nb);
3555 void *p = chunk2mem(victim);
3556 alloc_perturb (p, bytes);
3557 return p;
3562 Search for a chunk by scanning bins, starting with next largest
3563 bin. This search is strictly by best-fit; i.e., the smallest
3564 (with ties going to approximately the least recently used) chunk
3565 that fits is selected.
3567 The bitmap avoids needing to check that most blocks are nonempty.
3568 The particular case of skipping all bins during warm-up phases
3569 when no chunks have been returned yet is faster than it might look.
3572 ++idx;
3573 bin = bin_at(av,idx);
3574 block = idx2block(idx);
3575 map = av->binmap[block];
3576 bit = idx2bit(idx);
3578 for (;;) {
3580 /* Skip rest of block if there are no more set bits in this block. */
3581 if (bit > map || bit == 0) {
3582 do {
3583 if (++block >= BINMAPSIZE) /* out of bins */
3584 goto use_top;
3585 } while ( (map = av->binmap[block]) == 0);
3587 bin = bin_at(av, (block << BINMAPSHIFT));
3588 bit = 1;
3591 /* Advance to bin with set bit. There must be one. */
3592 while ((bit & map) == 0) {
3593 bin = next_bin(bin);
3594 bit <<= 1;
3595 assert(bit != 0);
3598 /* Inspect the bin. It is likely to be non-empty */
3599 victim = last(bin);
3601 /* If a false alarm (empty bin), clear the bit. */
3602 if (victim == bin) {
3603 av->binmap[block] = map &= ~bit; /* Write through */
3604 bin = next_bin(bin);
3605 bit <<= 1;
3608 else {
3609 size = chunksize(victim);
3611 /* We know the first chunk in this bin is big enough to use. */
3612 assert((unsigned long)(size) >= (unsigned long)(nb));
3614 remainder_size = size - nb;
3616 /* unlink */
3617 unlink(victim, bck, fwd);
3619 /* Exhaust */
3620 if (remainder_size < MINSIZE) {
3621 set_inuse_bit_at_offset(victim, size);
3622 if (av != &main_arena)
3623 victim->size |= NON_MAIN_ARENA;
3626 /* Split */
3627 else {
3628 remainder = chunk_at_offset(victim, nb);
3630 /* We cannot assume the unsorted list is empty and therefore
3631 have to perform a complete insert here. */
3632 bck = unsorted_chunks(av);
3633 fwd = bck->fd;
3634 if (__builtin_expect (fwd->bk != bck, 0))
3636 errstr = "malloc(): corrupted unsorted chunks 2";
3637 goto errout;
3639 remainder->bk = bck;
3640 remainder->fd = fwd;
3641 bck->fd = remainder;
3642 fwd->bk = remainder;
3644 /* advertise as last remainder */
3645 if (in_smallbin_range(nb))
3646 av->last_remainder = remainder;
3647 if (!in_smallbin_range(remainder_size))
3649 remainder->fd_nextsize = NULL;
3650 remainder->bk_nextsize = NULL;
3652 set_head(victim, nb | PREV_INUSE |
3653 (av != &main_arena ? NON_MAIN_ARENA : 0));
3654 set_head(remainder, remainder_size | PREV_INUSE);
3655 set_foot(remainder, remainder_size);
3657 check_malloced_chunk(av, victim, nb);
3658 void *p = chunk2mem(victim);
3659 alloc_perturb (p, bytes);
3660 return p;
3664 use_top:
3666 If large enough, split off the chunk bordering the end of memory
3667 (held in av->top). Note that this is in accord with the best-fit
3668 search rule. In effect, av->top is treated as larger (and thus
3669 less well fitting) than any other available chunk since it can
3670 be extended to be as large as necessary (up to system
3671 limitations).
3673 We require that av->top always exists (i.e., has size >=
3674 MINSIZE) after initialization, so if it would otherwise be
3675 exhausted by current request, it is replenished. (The main
3676 reason for ensuring it exists is that we may need MINSIZE space
3677 to put in fenceposts in sysmalloc.)
3680 victim = av->top;
3681 size = chunksize(victim);
3683 if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
3684 remainder_size = size - nb;
3685 remainder = chunk_at_offset(victim, nb);
3686 av->top = remainder;
3687 set_head(victim, nb | PREV_INUSE |
3688 (av != &main_arena ? NON_MAIN_ARENA : 0));
3689 set_head(remainder, remainder_size | PREV_INUSE);
3691 check_malloced_chunk(av, victim, nb);
3692 void *p = chunk2mem(victim);
3693 alloc_perturb (p, bytes);
3694 return p;
3697 /* When we are using atomic ops to free fast chunks we can get
3698 here for all block sizes. */
3699 else if (have_fastchunks(av)) {
3700 malloc_consolidate(av);
3701 /* restore original bin index */
3702 if (in_smallbin_range(nb))
3703 idx = smallbin_index(nb);
3704 else
3705 idx = largebin_index(nb);
3709 Otherwise, relay to handle system-dependent cases
3711 else {
3712 void *p = sysmalloc(nb, av);
3713 if (p != NULL)
3714 alloc_perturb (p, bytes);
3715 return p;
3721 ------------------------------ free ------------------------------
3724 static void
3725 _int_free(mstate av, mchunkptr p, int have_lock)
3727 INTERNAL_SIZE_T size; /* its size */
3728 mfastbinptr* fb; /* associated fastbin */
3729 mchunkptr nextchunk; /* next contiguous chunk */
3730 INTERNAL_SIZE_T nextsize; /* its size */
3731 int nextinuse; /* true if nextchunk is used */
3732 INTERNAL_SIZE_T prevsize; /* size of previous contiguous chunk */
3733 mchunkptr bck; /* misc temp for linking */
3734 mchunkptr fwd; /* misc temp for linking */
3736 const char *errstr = NULL;
3737 int locked = 0;
3739 size = chunksize(p);
3741 /* Little security check which won't hurt performance: the
3742 allocator never wrapps around at the end of the address space.
3743 Therefore we can exclude some size values which might appear
3744 here by accident or by "design" from some intruder. */
3745 if (__builtin_expect ((uintptr_t) p > (uintptr_t) -size, 0)
3746 || __builtin_expect (misaligned_chunk (p), 0))
3748 errstr = "free(): invalid pointer";
3749 errout:
3750 if (! have_lock && locked)
3751 (void)mutex_unlock(&av->mutex);
3752 malloc_printerr (check_action, errstr, chunk2mem(p));
3753 return;
3755 /* We know that each chunk is at least MINSIZE bytes in size or a
3756 multiple of MALLOC_ALIGNMENT. */
3757 if (__builtin_expect (size < MINSIZE || !aligned_OK (size), 0))
3759 errstr = "free(): invalid size";
3760 goto errout;
3763 check_inuse_chunk(av, p);
3766 If eligible, place chunk on a fastbin so it can be found
3767 and used quickly in malloc.
3770 if ((unsigned long)(size) <= (unsigned long)(get_max_fast ())
3772 #if TRIM_FASTBINS
3774 If TRIM_FASTBINS set, don't place chunks
3775 bordering top into fastbins
3777 && (chunk_at_offset(p, size) != av->top)
3778 #endif
3781 if (__builtin_expect (chunk_at_offset (p, size)->size <= 2 * SIZE_SZ, 0)
3782 || __builtin_expect (chunksize (chunk_at_offset (p, size))
3783 >= av->system_mem, 0))
3785 /* We might not have a lock at this point and concurrent modifications
3786 of system_mem might have let to a false positive. Redo the test
3787 after getting the lock. */
3788 if (have_lock
3789 || ({ assert (locked == 0);
3790 mutex_lock(&av->mutex);
3791 locked = 1;
3792 chunk_at_offset (p, size)->size <= 2 * SIZE_SZ
3793 || chunksize (chunk_at_offset (p, size)) >= av->system_mem;
3796 errstr = "free(): invalid next size (fast)";
3797 goto errout;
3799 if (! have_lock)
3801 (void)mutex_unlock(&av->mutex);
3802 locked = 0;
3806 free_perturb (chunk2mem(p), size - 2 * SIZE_SZ);
3808 set_fastchunks(av);
3809 unsigned int idx = fastbin_index(size);
3810 fb = &fastbin (av, idx);
3812 mchunkptr fd;
3813 mchunkptr old = *fb;
3814 unsigned int old_idx = ~0u;
3817 /* Another simple check: make sure the top of the bin is not the
3818 record we are going to add (i.e., double free). */
3819 if (__builtin_expect (old == p, 0))
3821 errstr = "double free or corruption (fasttop)";
3822 goto errout;
3824 if (old != NULL)
3825 old_idx = fastbin_index(chunksize(old));
3826 p->fd = fd = old;
3828 while ((old = catomic_compare_and_exchange_val_rel (fb, p, fd)) != fd);
3830 if (fd != NULL && __builtin_expect (old_idx != idx, 0))
3832 errstr = "invalid fastbin entry (free)";
3833 goto errout;
3838 Consolidate other non-mmapped chunks as they arrive.
3841 else if (!chunk_is_mmapped(p)) {
3842 if (! have_lock) {
3843 #if THREAD_STATS
3844 if(!mutex_trylock(&av->mutex))
3845 ++(av->stat_lock_direct);
3846 else {
3847 (void)mutex_lock(&av->mutex);
3848 ++(av->stat_lock_wait);
3850 #else
3851 (void)mutex_lock(&av->mutex);
3852 #endif
3853 locked = 1;
3856 nextchunk = chunk_at_offset(p, size);
3858 /* Lightweight tests: check whether the block is already the
3859 top block. */
3860 if (__builtin_expect (p == av->top, 0))
3862 errstr = "double free or corruption (top)";
3863 goto errout;
3865 /* Or whether the next chunk is beyond the boundaries of the arena. */
3866 if (__builtin_expect (contiguous (av)
3867 && (char *) nextchunk
3868 >= ((char *) av->top + chunksize(av->top)), 0))
3870 errstr = "double free or corruption (out)";
3871 goto errout;
3873 /* Or whether the block is actually not marked used. */
3874 if (__builtin_expect (!prev_inuse(nextchunk), 0))
3876 errstr = "double free or corruption (!prev)";
3877 goto errout;
3880 nextsize = chunksize(nextchunk);
3881 if (__builtin_expect (nextchunk->size <= 2 * SIZE_SZ, 0)
3882 || __builtin_expect (nextsize >= av->system_mem, 0))
3884 errstr = "free(): invalid next size (normal)";
3885 goto errout;
3888 free_perturb (chunk2mem(p), size - 2 * SIZE_SZ);
3890 /* consolidate backward */
3891 if (!prev_inuse(p)) {
3892 prevsize = p->prev_size;
3893 size += prevsize;
3894 p = chunk_at_offset(p, -((long) prevsize));
3895 unlink(p, bck, fwd);
3898 if (nextchunk != av->top) {
3899 /* get and clear inuse bit */
3900 nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
3902 /* consolidate forward */
3903 if (!nextinuse) {
3904 unlink(nextchunk, bck, fwd);
3905 size += nextsize;
3906 } else
3907 clear_inuse_bit_at_offset(nextchunk, 0);
3910 Place the chunk in unsorted chunk list. Chunks are
3911 not placed into regular bins until after they have
3912 been given one chance to be used in malloc.
3915 bck = unsorted_chunks(av);
3916 fwd = bck->fd;
3917 if (__builtin_expect (fwd->bk != bck, 0))
3919 errstr = "free(): corrupted unsorted chunks";
3920 goto errout;
3922 p->fd = fwd;
3923 p->bk = bck;
3924 if (!in_smallbin_range(size))
3926 p->fd_nextsize = NULL;
3927 p->bk_nextsize = NULL;
3929 bck->fd = p;
3930 fwd->bk = p;
3932 set_head(p, size | PREV_INUSE);
3933 set_foot(p, size);
3935 check_free_chunk(av, p);
3939 If the chunk borders the current high end of memory,
3940 consolidate into top
3943 else {
3944 size += nextsize;
3945 set_head(p, size | PREV_INUSE);
3946 av->top = p;
3947 check_chunk(av, p);
3951 If freeing a large space, consolidate possibly-surrounding
3952 chunks. Then, if the total unused topmost memory exceeds trim
3953 threshold, ask malloc_trim to reduce top.
3955 Unless max_fast is 0, we don't know if there are fastbins
3956 bordering top, so we cannot tell for sure whether threshold
3957 has been reached unless fastbins are consolidated. But we
3958 don't want to consolidate on each free. As a compromise,
3959 consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
3960 is reached.
3963 if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) {
3964 if (have_fastchunks(av))
3965 malloc_consolidate(av);
3967 if (av == &main_arena) {
3968 #ifndef MORECORE_CANNOT_TRIM
3969 if ((unsigned long)(chunksize(av->top)) >=
3970 (unsigned long)(mp_.trim_threshold))
3971 systrim(mp_.top_pad, av);
3972 #endif
3973 } else {
3974 /* Always try heap_trim(), even if the top chunk is not
3975 large, because the corresponding heap might go away. */
3976 heap_info *heap = heap_for_ptr(top(av));
3978 assert(heap->ar_ptr == av);
3979 heap_trim(heap, mp_.top_pad);
3983 if (! have_lock) {
3984 assert (locked);
3985 (void)mutex_unlock(&av->mutex);
3989 If the chunk was allocated via mmap, release via munmap().
3992 else {
3993 munmap_chunk (p);
3998 ------------------------- malloc_consolidate -------------------------
4000 malloc_consolidate is a specialized version of free() that tears
4001 down chunks held in fastbins. Free itself cannot be used for this
4002 purpose since, among other things, it might place chunks back onto
4003 fastbins. So, instead, we need to use a minor variant of the same
4004 code.
4006 Also, because this routine needs to be called the first time through
4007 malloc anyway, it turns out to be the perfect place to trigger
4008 initialization code.
4011 static void malloc_consolidate(mstate av)
4013 mfastbinptr* fb; /* current fastbin being consolidated */
4014 mfastbinptr* maxfb; /* last fastbin (for loop control) */
4015 mchunkptr p; /* current chunk being consolidated */
4016 mchunkptr nextp; /* next chunk to consolidate */
4017 mchunkptr unsorted_bin; /* bin header */
4018 mchunkptr first_unsorted; /* chunk to link to */
4020 /* These have same use as in free() */
4021 mchunkptr nextchunk;
4022 INTERNAL_SIZE_T size;
4023 INTERNAL_SIZE_T nextsize;
4024 INTERNAL_SIZE_T prevsize;
4025 int nextinuse;
4026 mchunkptr bck;
4027 mchunkptr fwd;
4030 If max_fast is 0, we know that av hasn't
4031 yet been initialized, in which case do so below
4034 if (get_max_fast () != 0) {
4035 clear_fastchunks(av);
4037 unsorted_bin = unsorted_chunks(av);
4040 Remove each chunk from fast bin and consolidate it, placing it
4041 then in unsorted bin. Among other reasons for doing this,
4042 placing in unsorted bin avoids needing to calculate actual bins
4043 until malloc is sure that chunks aren't immediately going to be
4044 reused anyway.
4047 maxfb = &fastbin (av, NFASTBINS - 1);
4048 fb = &fastbin (av, 0);
4049 do {
4050 p = atomic_exchange_acq (fb, 0);
4051 if (p != 0) {
4052 do {
4053 check_inuse_chunk(av, p);
4054 nextp = p->fd;
4056 /* Slightly streamlined version of consolidation code in free() */
4057 size = p->size & ~(PREV_INUSE|NON_MAIN_ARENA);
4058 nextchunk = chunk_at_offset(p, size);
4059 nextsize = chunksize(nextchunk);
4061 if (!prev_inuse(p)) {
4062 prevsize = p->prev_size;
4063 size += prevsize;
4064 p = chunk_at_offset(p, -((long) prevsize));
4065 unlink(p, bck, fwd);
4068 if (nextchunk != av->top) {
4069 nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
4071 if (!nextinuse) {
4072 size += nextsize;
4073 unlink(nextchunk, bck, fwd);
4074 } else
4075 clear_inuse_bit_at_offset(nextchunk, 0);
4077 first_unsorted = unsorted_bin->fd;
4078 unsorted_bin->fd = p;
4079 first_unsorted->bk = p;
4081 if (!in_smallbin_range (size)) {
4082 p->fd_nextsize = NULL;
4083 p->bk_nextsize = NULL;
4086 set_head(p, size | PREV_INUSE);
4087 p->bk = unsorted_bin;
4088 p->fd = first_unsorted;
4089 set_foot(p, size);
4092 else {
4093 size += nextsize;
4094 set_head(p, size | PREV_INUSE);
4095 av->top = p;
4098 } while ( (p = nextp) != 0);
4101 } while (fb++ != maxfb);
4103 else {
4104 malloc_init_state(av);
4105 check_malloc_state(av);
4110 ------------------------------ realloc ------------------------------
4113 void*
4114 _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
4115 INTERNAL_SIZE_T nb)
4117 mchunkptr newp; /* chunk to return */
4118 INTERNAL_SIZE_T newsize; /* its size */
4119 void* newmem; /* corresponding user mem */
4121 mchunkptr next; /* next contiguous chunk after oldp */
4123 mchunkptr remainder; /* extra space at end of newp */
4124 unsigned long remainder_size; /* its size */
4126 mchunkptr bck; /* misc temp for linking */
4127 mchunkptr fwd; /* misc temp for linking */
4129 unsigned long copysize; /* bytes to copy */
4130 unsigned int ncopies; /* INTERNAL_SIZE_T words to copy */
4131 INTERNAL_SIZE_T* s; /* copy source */
4132 INTERNAL_SIZE_T* d; /* copy destination */
4134 const char *errstr = NULL;
4136 /* oldmem size */
4137 if (__builtin_expect (oldp->size <= 2 * SIZE_SZ, 0)
4138 || __builtin_expect (oldsize >= av->system_mem, 0))
4140 errstr = "realloc(): invalid old size";
4141 errout:
4142 malloc_printerr (check_action, errstr, chunk2mem(oldp));
4143 return NULL;
4146 check_inuse_chunk(av, oldp);
4148 /* All callers already filter out mmap'ed chunks. */
4149 assert (!chunk_is_mmapped(oldp));
4151 next = chunk_at_offset(oldp, oldsize);
4152 INTERNAL_SIZE_T nextsize = chunksize(next);
4153 if (__builtin_expect (next->size <= 2 * SIZE_SZ, 0)
4154 || __builtin_expect (nextsize >= av->system_mem, 0))
4156 errstr = "realloc(): invalid next size";
4157 goto errout;
4160 if ((unsigned long)(oldsize) >= (unsigned long)(nb)) {
4161 /* already big enough; split below */
4162 newp = oldp;
4163 newsize = oldsize;
4166 else {
4167 /* Try to expand forward into top */
4168 if (next == av->top &&
4169 (unsigned long)(newsize = oldsize + nextsize) >=
4170 (unsigned long)(nb + MINSIZE)) {
4171 set_head_size(oldp, nb | (av != &main_arena ? NON_MAIN_ARENA : 0));
4172 av->top = chunk_at_offset(oldp, nb);
4173 set_head(av->top, (newsize - nb) | PREV_INUSE);
4174 check_inuse_chunk(av, oldp);
4175 return chunk2mem(oldp);
4178 /* Try to expand forward into next chunk; split off remainder below */
4179 else if (next != av->top &&
4180 !inuse(next) &&
4181 (unsigned long)(newsize = oldsize + nextsize) >=
4182 (unsigned long)(nb)) {
4183 newp = oldp;
4184 unlink(next, bck, fwd);
4187 /* allocate, copy, free */
4188 else {
4189 newmem = _int_malloc(av, nb - MALLOC_ALIGN_MASK);
4190 if (newmem == 0)
4191 return 0; /* propagate failure */
4193 newp = mem2chunk(newmem);
4194 newsize = chunksize(newp);
4197 Avoid copy if newp is next chunk after oldp.
4199 if (newp == next) {
4200 newsize += oldsize;
4201 newp = oldp;
4203 else {
4205 Unroll copy of <= 36 bytes (72 if 8byte sizes)
4206 We know that contents have an odd number of
4207 INTERNAL_SIZE_T-sized words; minimally 3.
4210 copysize = oldsize - SIZE_SZ;
4211 s = (INTERNAL_SIZE_T*)(chunk2mem(oldp));
4212 d = (INTERNAL_SIZE_T*)(newmem);
4213 ncopies = copysize / sizeof(INTERNAL_SIZE_T);
4214 assert(ncopies >= 3);
4216 if (ncopies > 9)
4217 MALLOC_COPY(d, s, copysize);
4219 else {
4220 *(d+0) = *(s+0);
4221 *(d+1) = *(s+1);
4222 *(d+2) = *(s+2);
4223 if (ncopies > 4) {
4224 *(d+3) = *(s+3);
4225 *(d+4) = *(s+4);
4226 if (ncopies > 6) {
4227 *(d+5) = *(s+5);
4228 *(d+6) = *(s+6);
4229 if (ncopies > 8) {
4230 *(d+7) = *(s+7);
4231 *(d+8) = *(s+8);
4237 _int_free(av, oldp, 1);
4238 check_inuse_chunk(av, newp);
4239 return chunk2mem(newp);
4244 /* If possible, free extra space in old or extended chunk */
4246 assert((unsigned long)(newsize) >= (unsigned long)(nb));
4248 remainder_size = newsize - nb;
4250 if (remainder_size < MINSIZE) { /* not enough extra to split off */
4251 set_head_size(newp, newsize | (av != &main_arena ? NON_MAIN_ARENA : 0));
4252 set_inuse_bit_at_offset(newp, newsize);
4254 else { /* split remainder */
4255 remainder = chunk_at_offset(newp, nb);
4256 set_head_size(newp, nb | (av != &main_arena ? NON_MAIN_ARENA : 0));
4257 set_head(remainder, remainder_size | PREV_INUSE |
4258 (av != &main_arena ? NON_MAIN_ARENA : 0));
4259 /* Mark remainder as inuse so free() won't complain */
4260 set_inuse_bit_at_offset(remainder, remainder_size);
4261 _int_free(av, remainder, 1);
4264 check_inuse_chunk(av, newp);
4265 return chunk2mem(newp);
4269 ------------------------------ memalign ------------------------------
4272 static void*
4273 _int_memalign(mstate av, size_t alignment, size_t bytes)
4275 INTERNAL_SIZE_T nb; /* padded request size */
4276 char* m; /* memory returned by malloc call */
4277 mchunkptr p; /* corresponding chunk */
4278 char* brk; /* alignment point within p */
4279 mchunkptr newp; /* chunk to return */
4280 INTERNAL_SIZE_T newsize; /* its size */
4281 INTERNAL_SIZE_T leadsize; /* leading space before alignment point */
4282 mchunkptr remainder; /* spare room at end to split off */
4283 unsigned long remainder_size; /* its size */
4284 INTERNAL_SIZE_T size;
4288 checked_request2size(bytes, nb);
4291 Strategy: find a spot within that chunk that meets the alignment
4292 request, and then possibly free the leading and trailing space.
4296 /* Call malloc with worst case padding to hit alignment. */
4298 m = (char*)(_int_malloc(av, nb + alignment + MINSIZE));
4300 if (m == 0) return 0; /* propagate failure */
4302 p = mem2chunk(m);
4304 if ((((unsigned long)(m)) % alignment) != 0) { /* misaligned */
4307 Find an aligned spot inside chunk. Since we need to give back
4308 leading space in a chunk of at least MINSIZE, if the first
4309 calculation places us at a spot with less than MINSIZE leader,
4310 we can move to the next aligned spot -- we've allocated enough
4311 total room so that this is always possible.
4314 brk = (char*)mem2chunk(((unsigned long)(m + alignment - 1)) &
4315 -((signed long) alignment));
4316 if ((unsigned long)(brk - (char*)(p)) < MINSIZE)
4317 brk += alignment;
4319 newp = (mchunkptr)brk;
4320 leadsize = brk - (char*)(p);
4321 newsize = chunksize(p) - leadsize;
4323 /* For mmapped chunks, just adjust offset */
4324 if (chunk_is_mmapped(p)) {
4325 newp->prev_size = p->prev_size + leadsize;
4326 set_head(newp, newsize|IS_MMAPPED);
4327 return chunk2mem(newp);
4330 /* Otherwise, give back leader, use the rest */
4331 set_head(newp, newsize | PREV_INUSE |
4332 (av != &main_arena ? NON_MAIN_ARENA : 0));
4333 set_inuse_bit_at_offset(newp, newsize);
4334 set_head_size(p, leadsize | (av != &main_arena ? NON_MAIN_ARENA : 0));
4335 _int_free(av, p, 1);
4336 p = newp;
4338 assert (newsize >= nb &&
4339 (((unsigned long)(chunk2mem(p))) % alignment) == 0);
4342 /* Also give back spare room at the end */
4343 if (!chunk_is_mmapped(p)) {
4344 size = chunksize(p);
4345 if ((unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
4346 remainder_size = size - nb;
4347 remainder = chunk_at_offset(p, nb);
4348 set_head(remainder, remainder_size | PREV_INUSE |
4349 (av != &main_arena ? NON_MAIN_ARENA : 0));
4350 set_head_size(p, nb);
4351 _int_free(av, remainder, 1);
4355 check_inuse_chunk(av, p);
4356 return chunk2mem(p);
4361 ------------------------------ malloc_trim ------------------------------
4364 static int mtrim(mstate av, size_t pad)
4366 /* Ensure initialization/consolidation */
4367 malloc_consolidate (av);
4369 const size_t ps = GLRO(dl_pagesize);
4370 int psindex = bin_index (ps);
4371 const size_t psm1 = ps - 1;
4373 int result = 0;
4374 for (int i = 1; i < NBINS; ++i)
4375 if (i == 1 || i >= psindex)
4377 mbinptr bin = bin_at (av, i);
4379 for (mchunkptr p = last (bin); p != bin; p = p->bk)
4381 INTERNAL_SIZE_T size = chunksize (p);
4383 if (size > psm1 + sizeof (struct malloc_chunk))
4385 /* See whether the chunk contains at least one unused page. */
4386 char *paligned_mem = (char *) (((uintptr_t) p
4387 + sizeof (struct malloc_chunk)
4388 + psm1) & ~psm1);
4390 assert ((char *) chunk2mem (p) + 4 * SIZE_SZ <= paligned_mem);
4391 assert ((char *) p + size > paligned_mem);
4393 /* This is the size we could potentially free. */
4394 size -= paligned_mem - (char *) p;
4396 if (size > psm1)
4398 #ifdef MALLOC_DEBUG
4399 /* When debugging we simulate destroying the memory
4400 content. */
4401 memset (paligned_mem, 0x89, size & ~psm1);
4402 #endif
4403 __madvise (paligned_mem, size & ~psm1, MADV_DONTNEED);
4405 result = 1;
4411 #ifndef MORECORE_CANNOT_TRIM
4412 return result | (av == &main_arena ? systrim (pad, av) : 0);
4413 #else
4414 return result;
4415 #endif
4420 __malloc_trim(size_t s)
4422 int result = 0;
4424 if(__malloc_initialized < 0)
4425 ptmalloc_init ();
4427 mstate ar_ptr = &main_arena;
4430 (void) mutex_lock (&ar_ptr->mutex);
4431 result |= mtrim (ar_ptr, s);
4432 (void) mutex_unlock (&ar_ptr->mutex);
4434 ar_ptr = ar_ptr->next;
4436 while (ar_ptr != &main_arena);
4438 return result;
4443 ------------------------- malloc_usable_size -------------------------
4446 static size_t
4447 musable(void* mem)
4449 mchunkptr p;
4450 if (mem != 0) {
4451 p = mem2chunk(mem);
4453 if (__builtin_expect(using_malloc_checking == 1, 0))
4454 return malloc_check_get_size(p);
4455 if (chunk_is_mmapped(p))
4456 return chunksize(p) - 2*SIZE_SZ;
4457 else if (inuse(p))
4458 return chunksize(p) - SIZE_SZ;
4460 return 0;
4464 size_t
4465 __malloc_usable_size(void* m)
4467 size_t result;
4469 result = musable(m);
4470 return result;
4474 ------------------------------ mallinfo ------------------------------
4475 Accumulate malloc statistics for arena AV into M.
4478 static void
4479 int_mallinfo(mstate av, struct mallinfo *m)
4481 size_t i;
4482 mbinptr b;
4483 mchunkptr p;
4484 INTERNAL_SIZE_T avail;
4485 INTERNAL_SIZE_T fastavail;
4486 int nblocks;
4487 int nfastblocks;
4489 /* Ensure initialization */
4490 if (av->top == 0) malloc_consolidate(av);
4492 check_malloc_state(av);
4494 /* Account for top */
4495 avail = chunksize(av->top);
4496 nblocks = 1; /* top always exists */
4498 /* traverse fastbins */
4499 nfastblocks = 0;
4500 fastavail = 0;
4502 for (i = 0; i < NFASTBINS; ++i) {
4503 for (p = fastbin (av, i); p != 0; p = p->fd) {
4504 ++nfastblocks;
4505 fastavail += chunksize(p);
4509 avail += fastavail;
4511 /* traverse regular bins */
4512 for (i = 1; i < NBINS; ++i) {
4513 b = bin_at(av, i);
4514 for (p = last(b); p != b; p = p->bk) {
4515 ++nblocks;
4516 avail += chunksize(p);
4520 m->smblks += nfastblocks;
4521 m->ordblks += nblocks;
4522 m->fordblks += avail;
4523 m->uordblks += av->system_mem - avail;
4524 m->arena += av->system_mem;
4525 m->fsmblks += fastavail;
4526 if (av == &main_arena)
4528 m->hblks = mp_.n_mmaps;
4529 m->hblkhd = mp_.mmapped_mem;
4530 m->usmblks = mp_.max_total_mem;
4531 m->keepcost = chunksize(av->top);
4536 struct mallinfo __libc_mallinfo()
4538 struct mallinfo m;
4539 mstate ar_ptr;
4541 if(__malloc_initialized < 0)
4542 ptmalloc_init ();
4544 memset(&m, 0, sizeof (m));
4545 ar_ptr = &main_arena;
4546 do {
4547 (void)mutex_lock(&ar_ptr->mutex);
4548 int_mallinfo(ar_ptr, &m);
4549 (void)mutex_unlock(&ar_ptr->mutex);
4551 ar_ptr = ar_ptr->next;
4552 } while (ar_ptr != &main_arena);
4554 return m;
4558 ------------------------------ malloc_stats ------------------------------
4561 void
4562 __malloc_stats (void)
4564 int i;
4565 mstate ar_ptr;
4566 unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b;
4567 #if THREAD_STATS
4568 long stat_lock_direct = 0, stat_lock_loop = 0, stat_lock_wait = 0;
4569 #endif
4571 if(__malloc_initialized < 0)
4572 ptmalloc_init ();
4573 _IO_flockfile (stderr);
4574 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
4575 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
4576 for (i=0, ar_ptr = &main_arena;; i++) {
4577 struct mallinfo mi;
4579 memset(&mi, 0, sizeof(mi));
4580 (void)mutex_lock(&ar_ptr->mutex);
4581 int_mallinfo(ar_ptr, &mi);
4582 fprintf(stderr, "Arena %d:\n", i);
4583 fprintf(stderr, "system bytes = %10u\n", (unsigned int)mi.arena);
4584 fprintf(stderr, "in use bytes = %10u\n", (unsigned int)mi.uordblks);
4585 #if MALLOC_DEBUG > 1
4586 if (i > 0)
4587 dump_heap(heap_for_ptr(top(ar_ptr)));
4588 #endif
4589 system_b += mi.arena;
4590 in_use_b += mi.uordblks;
4591 #if THREAD_STATS
4592 stat_lock_direct += ar_ptr->stat_lock_direct;
4593 stat_lock_loop += ar_ptr->stat_lock_loop;
4594 stat_lock_wait += ar_ptr->stat_lock_wait;
4595 #endif
4596 (void)mutex_unlock(&ar_ptr->mutex);
4597 ar_ptr = ar_ptr->next;
4598 if(ar_ptr == &main_arena) break;
4600 fprintf(stderr, "Total (incl. mmap):\n");
4601 fprintf(stderr, "system bytes = %10u\n", system_b);
4602 fprintf(stderr, "in use bytes = %10u\n", in_use_b);
4603 fprintf(stderr, "max mmap regions = %10u\n", (unsigned int)mp_.max_n_mmaps);
4604 fprintf(stderr, "max mmap bytes = %10lu\n",
4605 (unsigned long)mp_.max_mmapped_mem);
4606 #if THREAD_STATS
4607 fprintf(stderr, "heaps created = %10d\n", stat_n_heaps);
4608 fprintf(stderr, "locked directly = %10ld\n", stat_lock_direct);
4609 fprintf(stderr, "locked in loop = %10ld\n", stat_lock_loop);
4610 fprintf(stderr, "locked waiting = %10ld\n", stat_lock_wait);
4611 fprintf(stderr, "locked total = %10ld\n",
4612 stat_lock_direct + stat_lock_loop + stat_lock_wait);
4613 #endif
4614 ((_IO_FILE *) stderr)->_flags2 |= old_flags2;
4615 _IO_funlockfile (stderr);
4620 ------------------------------ mallopt ------------------------------
4623 int __libc_mallopt(int param_number, int value)
4625 mstate av = &main_arena;
4626 int res = 1;
4628 if(__malloc_initialized < 0)
4629 ptmalloc_init ();
4630 (void)mutex_lock(&av->mutex);
4631 /* Ensure initialization/consolidation */
4632 malloc_consolidate(av);
4634 LIBC_PROBE (memory_mallopt, 2, param_number, value);
4636 switch(param_number) {
4637 case M_MXFAST:
4638 if (value >= 0 && value <= MAX_FAST_SIZE)
4640 LIBC_PROBE (memory_mallopt_mxfast, 2, value, get_max_fast ());
4641 set_max_fast(value);
4643 else
4644 res = 0;
4645 break;
4647 case M_TRIM_THRESHOLD:
4648 LIBC_PROBE (memory_mallopt_trim_threshold, 3, value,
4649 mp_.trim_threshold, mp_.no_dyn_threshold);
4650 mp_.trim_threshold = value;
4651 mp_.no_dyn_threshold = 1;
4652 break;
4654 case M_TOP_PAD:
4655 LIBC_PROBE (memory_mallopt_top_pad, 3, value,
4656 mp_.top_pad, mp_.no_dyn_threshold);
4657 mp_.top_pad = value;
4658 mp_.no_dyn_threshold = 1;
4659 break;
4661 case M_MMAP_THRESHOLD:
4662 /* Forbid setting the threshold too high. */
4663 if((unsigned long)value > HEAP_MAX_SIZE/2)
4664 res = 0;
4665 else
4667 LIBC_PROBE (memory_mallopt_mmap_threshold, 3, value,
4668 mp_.mmap_threshold, mp_.no_dyn_threshold);
4669 mp_.mmap_threshold = value;
4670 mp_.no_dyn_threshold = 1;
4672 break;
4674 case M_MMAP_MAX:
4675 LIBC_PROBE (memory_mallopt_mmap_max, 3, value,
4676 mp_.n_mmaps_max, mp_.no_dyn_threshold);
4677 mp_.n_mmaps_max = value;
4678 mp_.no_dyn_threshold = 1;
4679 break;
4681 case M_CHECK_ACTION:
4682 LIBC_PROBE (memory_mallopt_check_action, 2, value, check_action);
4683 check_action = value;
4684 break;
4686 case M_PERTURB:
4687 LIBC_PROBE (memory_mallopt_perturb, 2, value, perturb_byte);
4688 perturb_byte = value;
4689 break;
4691 #ifdef PER_THREAD
4692 case M_ARENA_TEST:
4693 if (value > 0)
4695 LIBC_PROBE (memory_mallopt_arena_test, 2, value, mp_.arena_test);
4696 mp_.arena_test = value;
4698 break;
4700 case M_ARENA_MAX:
4701 if (value > 0)
4703 LIBC_PROBE (memory_mallopt_arena_max, 2, value, mp_.arena_max);
4704 mp_.arena_max = value;
4706 break;
4707 #endif
4709 (void)mutex_unlock(&av->mutex);
4710 return res;
4712 libc_hidden_def (__libc_mallopt)
4716 -------------------- Alternative MORECORE functions --------------------
4721 General Requirements for MORECORE.
4723 The MORECORE function must have the following properties:
4725 If MORECORE_CONTIGUOUS is false:
4727 * MORECORE must allocate in multiples of pagesize. It will
4728 only be called with arguments that are multiples of pagesize.
4730 * MORECORE(0) must return an address that is at least
4731 MALLOC_ALIGNMENT aligned. (Page-aligning always suffices.)
4733 else (i.e. If MORECORE_CONTIGUOUS is true):
4735 * Consecutive calls to MORECORE with positive arguments
4736 return increasing addresses, indicating that space has been
4737 contiguously extended.
4739 * MORECORE need not allocate in multiples of pagesize.
4740 Calls to MORECORE need not have args of multiples of pagesize.
4742 * MORECORE need not page-align.
4744 In either case:
4746 * MORECORE may allocate more memory than requested. (Or even less,
4747 but this will generally result in a malloc failure.)
4749 * MORECORE must not allocate memory when given argument zero, but
4750 instead return one past the end address of memory from previous
4751 nonzero call. This malloc does NOT call MORECORE(0)
4752 until at least one call with positive arguments is made, so
4753 the initial value returned is not important.
4755 * Even though consecutive calls to MORECORE need not return contiguous
4756 addresses, it must be OK for malloc'ed chunks to span multiple
4757 regions in those cases where they do happen to be contiguous.
4759 * MORECORE need not handle negative arguments -- it may instead
4760 just return MORECORE_FAILURE when given negative arguments.
4761 Negative arguments are always multiples of pagesize. MORECORE
4762 must not misinterpret negative args as large positive unsigned
4763 args. You can suppress all such calls from even occurring by defining
4764 MORECORE_CANNOT_TRIM,
4766 There is some variation across systems about the type of the
4767 argument to sbrk/MORECORE. If size_t is unsigned, then it cannot
4768 actually be size_t, because sbrk supports negative args, so it is
4769 normally the signed type of the same width as size_t (sometimes
4770 declared as "intptr_t", and sometimes "ptrdiff_t"). It doesn't much
4771 matter though. Internally, we use "long" as arguments, which should
4772 work across all reasonable possibilities.
4774 Additionally, if MORECORE ever returns failure for a positive
4775 request, then mmap is used as a noncontiguous system allocator. This
4776 is a useful backup strategy for systems with holes in address spaces
4777 -- in this case sbrk cannot contiguously expand the heap, but mmap
4778 may be able to map noncontiguous space.
4780 If you'd like mmap to ALWAYS be used, you can define MORECORE to be
4781 a function that always returns MORECORE_FAILURE.
4783 If you are using this malloc with something other than sbrk (or its
4784 emulation) to supply memory regions, you probably want to set
4785 MORECORE_CONTIGUOUS as false. As an example, here is a custom
4786 allocator kindly contributed for pre-OSX macOS. It uses virtually
4787 but not necessarily physically contiguous non-paged memory (locked
4788 in, present and won't get swapped out). You can use it by
4789 uncommenting this section, adding some #includes, and setting up the
4790 appropriate defines above:
4792 #define MORECORE osMoreCore
4793 #define MORECORE_CONTIGUOUS 0
4795 There is also a shutdown routine that should somehow be called for
4796 cleanup upon program exit.
4798 #define MAX_POOL_ENTRIES 100
4799 #define MINIMUM_MORECORE_SIZE (64 * 1024)
4800 static int next_os_pool;
4801 void *our_os_pools[MAX_POOL_ENTRIES];
4803 void *osMoreCore(int size)
4805 void *ptr = 0;
4806 static void *sbrk_top = 0;
4808 if (size > 0)
4810 if (size < MINIMUM_MORECORE_SIZE)
4811 size = MINIMUM_MORECORE_SIZE;
4812 if (CurrentExecutionLevel() == kTaskLevel)
4813 ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0);
4814 if (ptr == 0)
4816 return (void *) MORECORE_FAILURE;
4818 // save ptrs so they can be freed during cleanup
4819 our_os_pools[next_os_pool] = ptr;
4820 next_os_pool++;
4821 ptr = (void *) ((((unsigned long) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK);
4822 sbrk_top = (char *) ptr + size;
4823 return ptr;
4825 else if (size < 0)
4827 // we don't currently support shrink behavior
4828 return (void *) MORECORE_FAILURE;
4830 else
4832 return sbrk_top;
4836 // cleanup any allocated memory pools
4837 // called as last thing before shutting down driver
4839 void osCleanupMem(void)
4841 void **ptr;
4843 for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++)
4844 if (*ptr)
4846 PoolDeallocate(*ptr);
4847 *ptr = 0;
4854 /* Helper code. */
4856 extern char **__libc_argv attribute_hidden;
4858 static void
4859 malloc_printerr(int action, const char *str, void *ptr)
4861 if ((action & 5) == 5)
4862 __libc_message (action & 2, "%s\n", str);
4863 else if (action & 1)
4865 char buf[2 * sizeof (uintptr_t) + 1];
4867 buf[sizeof (buf) - 1] = '\0';
4868 char *cp = _itoa_word ((uintptr_t) ptr, &buf[sizeof (buf) - 1], 16, 0);
4869 while (cp > buf)
4870 *--cp = '0';
4872 __libc_message (action & 2, "*** Error in `%s': %s: 0x%s ***\n",
4873 __libc_argv[0] ?: "<unknown>", str, cp);
4875 else if (action & 2)
4876 abort ();
4879 /* We need a wrapper function for one of the additions of POSIX. */
4881 __posix_memalign (void **memptr, size_t alignment, size_t size)
4883 void *mem;
4885 /* Test whether the SIZE argument is valid. It must be a power of
4886 two multiple of sizeof (void *). */
4887 if (alignment % sizeof (void *) != 0
4888 || !powerof2 (alignment / sizeof (void *)) != 0
4889 || alignment == 0)
4890 return EINVAL;
4893 void *address = RETURN_ADDRESS (0);
4894 mem = _mid_memalign (alignment, size, address);
4896 if (mem != NULL) {
4897 *memptr = mem;
4898 return 0;
4901 return ENOMEM;
4903 weak_alias (__posix_memalign, posix_memalign)
4907 malloc_info (int options, FILE *fp)
4909 /* For now, at least. */
4910 if (options != 0)
4911 return EINVAL;
4913 int n = 0;
4914 size_t total_nblocks = 0;
4915 size_t total_nfastblocks = 0;
4916 size_t total_avail = 0;
4917 size_t total_fastavail = 0;
4918 size_t total_system = 0;
4919 size_t total_max_system = 0;
4920 size_t total_aspace = 0;
4921 size_t total_aspace_mprotect = 0;
4923 void mi_arena (mstate ar_ptr)
4925 fprintf (fp, "<heap nr=\"%d\">\n<sizes>\n", n++);
4927 size_t nblocks = 0;
4928 size_t nfastblocks = 0;
4929 size_t avail = 0;
4930 size_t fastavail = 0;
4931 struct
4933 size_t from;
4934 size_t to;
4935 size_t total;
4936 size_t count;
4937 } sizes[NFASTBINS + NBINS - 1];
4938 #define nsizes (sizeof (sizes) / sizeof (sizes[0]))
4940 mutex_lock (&ar_ptr->mutex);
4942 for (size_t i = 0; i < NFASTBINS; ++i)
4944 mchunkptr p = fastbin (ar_ptr, i);
4945 if (p != NULL)
4947 size_t nthissize = 0;
4948 size_t thissize = chunksize (p);
4950 while (p != NULL)
4952 ++nthissize;
4953 p = p->fd;
4956 fastavail += nthissize * thissize;
4957 nfastblocks += nthissize;
4958 sizes[i].from = thissize - (MALLOC_ALIGNMENT - 1);
4959 sizes[i].to = thissize;
4960 sizes[i].count = nthissize;
4962 else
4963 sizes[i].from = sizes[i].to = sizes[i].count = 0;
4965 sizes[i].total = sizes[i].count * sizes[i].to;
4969 mbinptr bin;
4970 struct malloc_chunk *r;
4972 for (size_t i = 1; i < NBINS; ++i)
4974 bin = bin_at (ar_ptr, i);
4975 r = bin->fd;
4976 sizes[NFASTBINS - 1 + i].from = ~((size_t) 0);
4977 sizes[NFASTBINS - 1 + i].to = sizes[NFASTBINS - 1 + i].total
4978 = sizes[NFASTBINS - 1 + i].count = 0;
4980 if (r != NULL)
4981 while (r != bin)
4983 ++sizes[NFASTBINS - 1 + i].count;
4984 sizes[NFASTBINS - 1 + i].total += r->size;
4985 sizes[NFASTBINS - 1 + i].from
4986 = MIN (sizes[NFASTBINS - 1 + i].from, r->size);
4987 sizes[NFASTBINS - 1 + i].to = MAX (sizes[NFASTBINS - 1 + i].to,
4988 r->size);
4990 r = r->fd;
4993 if (sizes[NFASTBINS - 1 + i].count == 0)
4994 sizes[NFASTBINS - 1 + i].from = 0;
4995 nblocks += sizes[NFASTBINS - 1 + i].count;
4996 avail += sizes[NFASTBINS - 1 + i].total;
4999 mutex_unlock (&ar_ptr->mutex);
5001 total_nfastblocks += nfastblocks;
5002 total_fastavail += fastavail;
5004 total_nblocks += nblocks;
5005 total_avail += avail;
5007 for (size_t i = 0; i < nsizes; ++i)
5008 if (sizes[i].count != 0 && i != NFASTBINS)
5009 fprintf (fp, "\
5010 <size from=\"%zu\" to=\"%zu\" total=\"%zu\" count=\"%zu\"/>\n",
5011 sizes[i].from, sizes[i].to, sizes[i].total, sizes[i].count);
5013 if (sizes[NFASTBINS].count != 0)
5014 fprintf (fp, "\
5015 <unsorted from=\"%zu\" to=\"%zu\" total=\"%zu\" count=\"%zu\"/>\n",
5016 sizes[NFASTBINS].from, sizes[NFASTBINS].to,
5017 sizes[NFASTBINS].total, sizes[NFASTBINS].count);
5019 total_system += ar_ptr->system_mem;
5020 total_max_system += ar_ptr->max_system_mem;
5022 fprintf (fp,
5023 "</sizes>\n<total type=\"fast\" count=\"%zu\" size=\"%zu\"/>\n"
5024 "<total type=\"rest\" count=\"%zu\" size=\"%zu\"/>\n"
5025 "<system type=\"current\" size=\"%zu\"/>\n"
5026 "<system type=\"max\" size=\"%zu\"/>\n",
5027 nfastblocks, fastavail, nblocks, avail,
5028 ar_ptr->system_mem, ar_ptr->max_system_mem);
5030 if (ar_ptr != &main_arena)
5032 heap_info *heap = heap_for_ptr(top(ar_ptr));
5033 fprintf (fp,
5034 "<aspace type=\"total\" size=\"%zu\"/>\n"
5035 "<aspace type=\"mprotect\" size=\"%zu\"/>\n",
5036 heap->size, heap->mprotect_size);
5037 total_aspace += heap->size;
5038 total_aspace_mprotect += heap->mprotect_size;
5040 else
5042 fprintf (fp,
5043 "<aspace type=\"total\" size=\"%zu\"/>\n"
5044 "<aspace type=\"mprotect\" size=\"%zu\"/>\n",
5045 ar_ptr->system_mem, ar_ptr->system_mem);
5046 total_aspace += ar_ptr->system_mem;
5047 total_aspace_mprotect += ar_ptr->system_mem;
5050 fputs ("</heap>\n", fp);
5053 if(__malloc_initialized < 0)
5054 ptmalloc_init ();
5056 fputs ("<malloc version=\"1\">\n", fp);
5058 /* Iterate over all arenas currently in use. */
5059 mstate ar_ptr = &main_arena;
5062 mi_arena (ar_ptr);
5063 ar_ptr = ar_ptr->next;
5065 while (ar_ptr != &main_arena);
5067 fprintf (fp,
5068 "<total type=\"fast\" count=\"%zu\" size=\"%zu\"/>\n"
5069 "<total type=\"rest\" count=\"%zu\" size=\"%zu\"/>\n"
5070 "<system type=\"current\" size=\"%zu\"/>\n"
5071 "<system type=\"max\" size=\"%zu\"/>\n"
5072 "<aspace type=\"total\" size=\"%zu\"/>\n"
5073 "<aspace type=\"mprotect\" size=\"%zu\"/>\n"
5074 "</malloc>\n",
5075 total_nfastblocks, total_fastavail, total_nblocks, total_avail,
5076 total_system, total_max_system,
5077 total_aspace, total_aspace_mprotect);
5079 return 0;
5083 strong_alias (__libc_calloc, __calloc) weak_alias (__libc_calloc, calloc)
5084 strong_alias (__libc_free, __cfree) weak_alias (__libc_free, cfree)
5085 strong_alias (__libc_free, __free) strong_alias (__libc_free, free)
5086 strong_alias (__libc_malloc, __malloc) strong_alias (__libc_malloc, malloc)
5087 strong_alias (__libc_memalign, __memalign)
5088 weak_alias (__libc_memalign, memalign)
5089 strong_alias (__libc_realloc, __realloc) strong_alias (__libc_realloc, realloc)
5090 strong_alias (__libc_valloc, __valloc) weak_alias (__libc_valloc, valloc)
5091 strong_alias (__libc_pvalloc, __pvalloc) weak_alias (__libc_pvalloc, pvalloc)
5092 strong_alias (__libc_mallinfo, __mallinfo)
5093 weak_alias (__libc_mallinfo, mallinfo)
5094 strong_alias (__libc_mallopt, __mallopt) weak_alias (__libc_mallopt, mallopt)
5096 weak_alias (__malloc_stats, malloc_stats)
5097 weak_alias (__malloc_usable_size, malloc_usable_size)
5098 weak_alias (__malloc_trim, malloc_trim)
5099 weak_alias (__malloc_get_state, malloc_get_state)
5100 weak_alias (__malloc_set_state, malloc_set_state)
5103 /* ------------------------------------------------------------
5104 History:
5106 [see ftp://g.oswego.edu/pub/misc/malloc.c for the history of dlmalloc]
5110 * Local variables:
5111 * c-basic-offset: 2
5112 * End: