Update from main archive 961219
[glibc.git] / malloc / malloc.c
blob840655f4dbb535fb7986e447a81185e42ba23198
1 /* Malloc implementation for multiple threads without lock contention.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Wolfram Gloger <wmglo@dent.med.uni-muenchen.de>
5 and Doug Lea <dl@cs.oswego.edu>, 1996.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 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 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* V2.6.4-pt2 Sat Dec 14 1996
24 This work is mainly derived from malloc-2.6.4 by Doug Lea
25 <dl@cs.oswego.edu>, which is available from:
27 ftp://g.oswego.edu/pub/misc/malloc.c
29 Most of the original comments are reproduced in the code below.
31 * Why use this malloc?
33 This is not the fastest, most space-conserving, most portable, or
34 most tunable malloc ever written. However it is among the fastest
35 while also being among the most space-conserving, portable and tunable.
36 Consistent balance across these factors results in a good general-purpose
37 allocator. For a high-level description, see
38 http://g.oswego.edu/dl/html/malloc.html
40 On many systems, the standard malloc implementation is by itself not
41 thread-safe, and therefore wrapped with a single global lock around
42 all malloc-related functions. In some applications, especially with
43 multiple available processors, this can lead to contention problems
44 and bad performance. This malloc version was designed with the goal
45 to avoid waiting for locks as much as possible. Statistics indicate
46 that this goal is achieved in many cases.
48 * Synopsis of public routines
50 (Much fuller descriptions are contained in the program documentation below.)
52 ptmalloc_init();
53 Initialize global configuration. When compiled for multiple threads,
54 this function must be called once before any other function in the
55 package. It is not required otherwise. It is called automatically
56 in the Linux/GNU C libray or when compiling with MALLOC_HOOKS.
57 malloc(size_t n);
58 Return a pointer to a newly allocated chunk of at least n bytes, or null
59 if no space is available.
60 free(Void_t* p);
61 Release the chunk of memory pointed to by p, or no effect if p is null.
62 realloc(Void_t* p, size_t n);
63 Return a pointer to a chunk of size n that contains the same data
64 as does chunk p up to the minimum of (n, p's size) bytes, or null
65 if no space is available. The returned pointer may or may not be
66 the same as p. If p is null, equivalent to malloc. Unless the
67 #define REALLOC_ZERO_BYTES_FREES below is set, realloc with a
68 size argument of zero (re)allocates a minimum-sized chunk.
69 memalign(size_t alignment, size_t n);
70 Return a pointer to a newly allocated chunk of n bytes, aligned
71 in accord with the alignment argument, which must be a power of
72 two.
73 valloc(size_t n);
74 Equivalent to memalign(pagesize, n), where pagesize is the page
75 size of the system (or as near to this as can be figured out from
76 all the includes/defines below.)
77 pvalloc(size_t n);
78 Equivalent to valloc(minimum-page-that-holds(n)), that is,
79 round up n to nearest pagesize.
80 calloc(size_t unit, size_t quantity);
81 Returns a pointer to quantity * unit bytes, with all locations
82 set to zero.
83 cfree(Void_t* p);
84 Equivalent to free(p).
85 malloc_trim(size_t pad);
86 Release all but pad bytes of freed top-most memory back
87 to the system. Return 1 if successful, else 0.
88 malloc_usable_size(Void_t* p);
89 Report the number usable allocated bytes associated with allocated
90 chunk p. This may or may not report more bytes than were requested,
91 due to alignment and minimum size constraints.
92 malloc_stats();
93 Prints brief summary statistics on stderr.
94 mallinfo()
95 Returns (by copy) a struct containing various summary statistics.
96 mallopt(int parameter_number, int parameter_value)
97 Changes one of the tunable parameters described below. Returns
98 1 if successful in changing the parameter, else 0.
100 * Vital statistics:
102 Alignment: 8-byte
103 8 byte alignment is currently hardwired into the design. This
104 seems to suffice for all current machines and C compilers.
106 Assumed pointer representation: 4 or 8 bytes
107 Code for 8-byte pointers is untested by me but has worked
108 reliably by Wolfram Gloger, who contributed most of the
109 changes supporting this.
111 Assumed size_t representation: 4 or 8 bytes
112 Note that size_t is allowed to be 4 bytes even if pointers are 8.
114 Minimum overhead per allocated chunk: 4 or 8 bytes
115 Each malloced chunk has a hidden overhead of 4 bytes holding size
116 and status information.
118 Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead)
119 8-byte ptrs: 24/32 bytes (including, 4/8 overhead)
121 When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
122 ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
123 needed; 4 (8) for a trailing size field
124 and 8 (16) bytes for free list pointers. Thus, the minimum
125 allocatable size is 16/24/32 bytes.
127 Even a request for zero bytes (i.e., malloc(0)) returns a
128 pointer to something of the minimum allocatable size.
130 Maximum allocated size: 4-byte size_t: 2^31 - 8 bytes
131 8-byte size_t: 2^63 - 16 bytes
133 It is assumed that (possibly signed) size_t bit values suffice to
134 represent chunk sizes. `Possibly signed' is due to the fact
135 that `size_t' may be defined on a system as either a signed or
136 an unsigned type. To be conservative, values that would appear
137 as negative numbers are avoided.
138 Requests for sizes with a negative sign bit will return a
139 minimum-sized chunk.
141 Maximum overhead wastage per allocated chunk: normally 15 bytes
143 Alignment demands, plus the minimum allocatable size restriction
144 make the normal worst-case wastage 15 bytes (i.e., up to 15
145 more bytes will be allocated than were requested in malloc), with
146 two exceptions:
147 1. Because requests for zero bytes allocate non-zero space,
148 the worst case wastage for a request of zero bytes is 24 bytes.
149 2. For requests >= mmap_threshold that are serviced via
150 mmap(), the worst case wastage is 8 bytes plus the remainder
151 from a system page (the minimal mmap unit); typically 4096 bytes.
153 * Limitations
155 Here are some features that are NOT currently supported
157 * No automated mechanism for fully checking that all accesses
158 to malloced memory stay within their bounds.
159 * No support for compaction.
161 * Synopsis of compile-time options:
163 People have reported using previous versions of this malloc on all
164 versions of Unix, sometimes by tweaking some of the defines
165 below. It has been tested most extensively on Solaris and
166 Linux. People have also reported adapting this malloc for use in
167 stand-alone embedded systems.
169 The implementation is in straight, hand-tuned ANSI C. Among other
170 consequences, it uses a lot of macros. Because of this, to be at
171 all usable, this code should be compiled using an optimizing compiler
172 (for example gcc -O2) that can simplify expressions and control
173 paths.
175 __STD_C (default: derived from C compiler defines)
176 Nonzero if using ANSI-standard C compiler, a C++ compiler, or
177 a C compiler sufficiently close to ANSI to get away with it.
178 MALLOC_DEBUG (default: NOT defined)
179 Define to enable debugging. Adds fairly extensive assertion-based
180 checking to help track down memory errors, but noticeably slows down
181 execution.
182 MALLOC_HOOKS (default: NOT defined)
183 Define to enable support run-time replacement of the allocation
184 functions through user-defined `hooks'.
185 REALLOC_ZERO_BYTES_FREES (default: NOT defined)
186 Define this if you think that realloc(p, 0) should be equivalent
187 to free(p). Otherwise, since malloc returns a unique pointer for
188 malloc(0), so does realloc(p, 0).
189 HAVE_MEMCPY (default: defined)
190 Define if you are not otherwise using ANSI STD C, but still
191 have memcpy and memset in your C library and want to use them.
192 Otherwise, simple internal versions are supplied.
193 USE_MEMCPY (default: 1 if HAVE_MEMCPY is defined, 0 otherwise)
194 Define as 1 if you want the C library versions of memset and
195 memcpy called in realloc and calloc (otherwise macro versions are used).
196 At least on some platforms, the simple macro versions usually
197 outperform libc versions.
198 HAVE_MMAP (default: defined as 1)
199 Define to non-zero to optionally make malloc() use mmap() to
200 allocate very large blocks.
201 HAVE_MREMAP (default: defined as 0 unless Linux libc set)
202 Define to non-zero to optionally make realloc() use mremap() to
203 reallocate very large blocks.
204 malloc_getpagesize (default: derived from system #includes)
205 Either a constant or routine call returning the system page size.
206 HAVE_USR_INCLUDE_MALLOC_H (default: NOT defined)
207 Optionally define if you are on a system with a /usr/include/malloc.h
208 that declares struct mallinfo. It is not at all necessary to
209 define this even if you do, but will ensure consistency.
210 INTERNAL_SIZE_T (default: size_t)
211 Define to a 32-bit type (probably `unsigned int') if you are on a
212 64-bit machine, yet do not want or need to allow malloc requests of
213 greater than 2^31 to be handled. This saves space, especially for
214 very small chunks.
215 _LIBC (default: NOT defined)
216 Defined only when compiled as part of the Linux libc/glibc.
217 Also note that there is some odd internal name-mangling via defines
218 (for example, internally, `malloc' is named `mALLOc') needed
219 when compiling in this case. These look funny but don't otherwise
220 affect anything.
221 LACKS_UNISTD_H (default: undefined)
222 Define this if your system does not have a <unistd.h>.
223 MORECORE (default: sbrk)
224 The name of the routine to call to obtain more memory from the system.
225 MORECORE_FAILURE (default: -1)
226 The value returned upon failure of MORECORE.
227 MORECORE_CLEARS (default 1)
228 True (1) if the routine mapped to MORECORE zeroes out memory (which
229 holds for sbrk).
230 DEFAULT_TRIM_THRESHOLD
231 DEFAULT_TOP_PAD
232 DEFAULT_MMAP_THRESHOLD
233 DEFAULT_MMAP_MAX
234 Default values of tunable parameters (described in detail below)
235 controlling interaction with host system routines (sbrk, mmap, etc).
236 These values may also be changed dynamically via mallopt(). The
237 preset defaults are those that give best performance for typical
238 programs/systems.
239 DEFAULT_CHECK_ACTION
240 When the standard debugging hooks are in place, and a pointer is
241 detected as corrupt, do nothing (0), print an error message (1),
242 or call abort() (2).
249 * Compile-time options for multiple threads:
251 USE_PTHREADS, USE_THR, USE_SPROC
252 Define one of these as 1 to select the thread interface:
253 POSIX threads, Solaris threads or SGI sproc's, respectively.
254 If none of these is defined as non-zero, you get a `normal'
255 malloc implementation which is not thread-safe. Support for
256 multiple threads requires HAVE_MMAP=1. As an exception, when
257 compiling for GNU libc, i.e. when _LIBC is defined, then none of
258 the USE_... symbols have to be defined.
260 HEAP_MIN_SIZE
261 HEAP_MAX_SIZE
262 When thread support is enabled, additional `heap's are created
263 with mmap calls. These are limited in size; HEAP_MIN_SIZE should
264 be a multiple of the page size, while HEAP_MAX_SIZE must be a power
265 of two for alignment reasons. HEAP_MAX_SIZE should be at least
266 twice as large as the mmap threshold.
267 THREAD_STATS
268 When this is defined as non-zero, some statistics on mutex locking
269 are computed.
276 /* Preliminaries */
278 #ifndef __STD_C
279 #if defined (__STDC__)
280 #define __STD_C 1
281 #else
282 #if __cplusplus
283 #define __STD_C 1
284 #else
285 #define __STD_C 0
286 #endif /*__cplusplus*/
287 #endif /*__STDC__*/
288 #endif /*__STD_C*/
290 #ifndef Void_t
291 #if __STD_C
292 #define Void_t void
293 #else
294 #define Void_t char
295 #endif
296 #endif /*Void_t*/
298 #if __STD_C
299 # include <stddef.h> /* for size_t */
300 # if defined(_LIBC) || defined(MALLOC_HOOKS)
301 # include <stdlib.h> /* for getenv() */
302 # endif
303 #else
304 # include <sys/types.h>
305 #endif
307 /* Macros for handling mutexes and thread-specific data. This is
308 included early, because some thread-related header files (such as
309 pthread.h) should be included before any others. */
310 #include "thread-m.h"
312 #ifdef __cplusplus
313 extern "C" {
314 #endif
316 #include <stdio.h> /* needed for malloc_stats */
318 /* We must not pollute the name space in the GNU libc. */
319 #ifdef _LIBC
320 #define malloc_stats __malloc_stats
321 #define malloc_usable_size __malloc_usable_size
322 #define malloc_trim __malloc_trim
323 #endif
327 Compile-time options
332 Debugging:
334 Because freed chunks may be overwritten with link fields, this
335 malloc will often die when freed memory is overwritten by user
336 programs. This can be very effective (albeit in an annoying way)
337 in helping track down dangling pointers.
339 If you compile with -DMALLOC_DEBUG, a number of assertion checks are
340 enabled that will catch more memory errors. You probably won't be
341 able to make much sense of the actual assertion errors, but they
342 should help you locate incorrectly overwritten memory. The
343 checking is fairly extensive, and will slow down execution
344 noticeably. Calling malloc_stats or mallinfo with MALLOC_DEBUG set will
345 attempt to check every non-mmapped allocated and free chunk in the
346 course of computing the summaries. (By nature, mmapped regions
347 cannot be checked very much automatically.)
349 Setting MALLOC_DEBUG may also be helpful if you are trying to modify
350 this code. The assertions in the check routines spell out in more
351 detail the assumptions and invariants underlying the algorithms.
355 #if MALLOC_DEBUG
356 #include <assert.h>
357 #else
358 #define assert(x) ((void)0)
359 #endif
363 INTERNAL_SIZE_T is the word-size used for internal bookkeeping
364 of chunk sizes. On a 64-bit machine, you can reduce malloc
365 overhead by defining INTERNAL_SIZE_T to be a 32 bit `unsigned int'
366 at the expense of not being able to handle requests greater than
367 2^31. This limitation is hardly ever a concern; you are encouraged
368 to set this. However, the default version is the same as size_t.
371 #ifndef INTERNAL_SIZE_T
372 #define INTERNAL_SIZE_T size_t
373 #endif
376 REALLOC_ZERO_BYTES_FREES should be set if a call to
377 realloc with zero bytes should be the same as a call to free.
378 Some people think it should. Otherwise, since this malloc
379 returns a unique pointer for malloc(0), so does realloc(p, 0).
383 /* #define REALLOC_ZERO_BYTES_FREES */
387 HAVE_MEMCPY should be defined if you are not otherwise using
388 ANSI STD C, but still have memcpy and memset in your C library
389 and want to use them in calloc and realloc. Otherwise simple
390 macro versions are defined here.
392 USE_MEMCPY should be defined as 1 if you actually want to
393 have memset and memcpy called. People report that the macro
394 versions are often enough faster than libc versions on many
395 systems that it is better to use them.
399 #define HAVE_MEMCPY 1
401 #ifndef USE_MEMCPY
402 #ifdef HAVE_MEMCPY
403 #define USE_MEMCPY 1
404 #else
405 #define USE_MEMCPY 0
406 #endif
407 #endif
409 #if (__STD_C || defined(HAVE_MEMCPY))
411 #if __STD_C
412 void* memset(void*, int, size_t);
413 void* memcpy(void*, const void*, size_t);
414 #else
415 Void_t* memset();
416 Void_t* memcpy();
417 #endif
418 #endif
420 #if USE_MEMCPY
422 /* The following macros are only invoked with (2n+1)-multiples of
423 INTERNAL_SIZE_T units, with a positive integer n. This is exploited
424 for fast inline execution when n is small. */
426 #define MALLOC_ZERO(charp, nbytes) \
427 do { \
428 INTERNAL_SIZE_T mzsz = (nbytes); \
429 if(mzsz <= 9*sizeof(mzsz)) { \
430 INTERNAL_SIZE_T* mz = (INTERNAL_SIZE_T*) (charp); \
431 if(mzsz >= 5*sizeof(mzsz)) { *mz++ = 0; \
432 *mz++ = 0; \
433 if(mzsz >= 7*sizeof(mzsz)) { *mz++ = 0; \
434 *mz++ = 0; \
435 if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0; \
436 *mz++ = 0; }}} \
437 *mz++ = 0; \
438 *mz++ = 0; \
439 *mz = 0; \
440 } else memset((charp), 0, mzsz); \
441 } while(0)
443 #define MALLOC_COPY(dest,src,nbytes) \
444 do { \
445 INTERNAL_SIZE_T mcsz = (nbytes); \
446 if(mcsz <= 9*sizeof(mcsz)) { \
447 INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) (src); \
448 INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) (dest); \
449 if(mcsz >= 5*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
450 *mcdst++ = *mcsrc++; \
451 if(mcsz >= 7*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
452 *mcdst++ = *mcsrc++; \
453 if(mcsz >= 9*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
454 *mcdst++ = *mcsrc++; }}} \
455 *mcdst++ = *mcsrc++; \
456 *mcdst++ = *mcsrc++; \
457 *mcdst = *mcsrc ; \
458 } else memcpy(dest, src, mcsz); \
459 } while(0)
461 #else /* !USE_MEMCPY */
463 /* Use Duff's device for good zeroing/copying performance. */
465 #define MALLOC_ZERO(charp, nbytes) \
466 do { \
467 INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp); \
468 long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
469 if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
470 switch (mctmp) { \
471 case 0: for(;;) { *mzp++ = 0; \
472 case 7: *mzp++ = 0; \
473 case 6: *mzp++ = 0; \
474 case 5: *mzp++ = 0; \
475 case 4: *mzp++ = 0; \
476 case 3: *mzp++ = 0; \
477 case 2: *mzp++ = 0; \
478 case 1: *mzp++ = 0; if(mcn <= 0) break; mcn--; } \
480 } while(0)
482 #define MALLOC_COPY(dest,src,nbytes) \
483 do { \
484 INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src; \
485 INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest; \
486 long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
487 if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
488 switch (mctmp) { \
489 case 0: for(;;) { *mcdst++ = *mcsrc++; \
490 case 7: *mcdst++ = *mcsrc++; \
491 case 6: *mcdst++ = *mcsrc++; \
492 case 5: *mcdst++ = *mcsrc++; \
493 case 4: *mcdst++ = *mcsrc++; \
494 case 3: *mcdst++ = *mcsrc++; \
495 case 2: *mcdst++ = *mcsrc++; \
496 case 1: *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; } \
498 } while(0)
500 #endif
504 Define HAVE_MMAP to optionally make malloc() use mmap() to
505 allocate very large blocks. These will be returned to the
506 operating system immediately after a free().
509 #ifndef HAVE_MMAP
510 #define HAVE_MMAP 1
511 #endif
514 Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
515 large blocks. This is currently only possible on Linux with
516 kernel versions newer than 1.3.77.
519 #ifndef HAVE_MREMAP
520 #define HAVE_MREMAP defined(__linux__)
521 #endif
523 #if HAVE_MMAP
525 #include <unistd.h>
526 #include <fcntl.h>
527 #include <sys/mman.h>
529 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
530 #define MAP_ANONYMOUS MAP_ANON
531 #endif
533 #endif /* HAVE_MMAP */
536 Access to system page size. To the extent possible, this malloc
537 manages memory from the system in page-size units.
539 The following mechanics for getpagesize were adapted from
540 bsd/gnu getpagesize.h
543 #ifndef LACKS_UNISTD_H
544 # include <unistd.h>
545 #endif
547 #ifndef malloc_getpagesize
548 # ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */
549 # ifndef _SC_PAGE_SIZE
550 # define _SC_PAGE_SIZE _SC_PAGESIZE
551 # endif
552 # endif
553 # ifdef _SC_PAGE_SIZE
554 # define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
555 # else
556 # if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
557 extern size_t getpagesize();
558 # define malloc_getpagesize getpagesize()
559 # else
560 # include <sys/param.h>
561 # ifdef EXEC_PAGESIZE
562 # define malloc_getpagesize EXEC_PAGESIZE
563 # else
564 # ifdef NBPG
565 # ifndef CLSIZE
566 # define malloc_getpagesize NBPG
567 # else
568 # define malloc_getpagesize (NBPG * CLSIZE)
569 # endif
570 # else
571 # ifdef NBPC
572 # define malloc_getpagesize NBPC
573 # else
574 # ifdef PAGESIZE
575 # define malloc_getpagesize PAGESIZE
576 # else
577 # define malloc_getpagesize (4096) /* just guess */
578 # endif
579 # endif
580 # endif
581 # endif
582 # endif
583 # endif
584 #endif
590 This version of malloc supports the standard SVID/XPG mallinfo
591 routine that returns a struct containing the same kind of
592 information you can get from malloc_stats. It should work on
593 any SVID/XPG compliant system that has a /usr/include/malloc.h
594 defining struct mallinfo. (If you'd like to install such a thing
595 yourself, cut out the preliminary declarations as described above
596 and below and save them in a malloc.h file. But there's no
597 compelling reason to bother to do this.)
599 The main declaration needed is the mallinfo struct that is returned
600 (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a
601 bunch of fields, most of which are not even meaningful in this
602 version of malloc. Some of these fields are are instead filled by
603 mallinfo() with other numbers that might possibly be of interest.
605 HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
606 /usr/include/malloc.h file that includes a declaration of struct
607 mallinfo. If so, it is included; else an SVID2/XPG2 compliant
608 version is declared below. These must be precisely the same for
609 mallinfo() to work.
613 /* #define HAVE_USR_INCLUDE_MALLOC_H */
615 #if HAVE_USR_INCLUDE_MALLOC_H
616 # include "/usr/include/malloc.h"
617 #else
618 # ifdef _LIBC
619 # include "malloc.h"
620 # else
621 # include "ptmalloc.h"
622 # endif
623 #endif
627 #ifndef DEFAULT_TRIM_THRESHOLD
628 #define DEFAULT_TRIM_THRESHOLD (128 * 1024)
629 #endif
632 M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
633 to keep before releasing via malloc_trim in free().
635 Automatic trimming is mainly useful in long-lived programs.
636 Because trimming via sbrk can be slow on some systems, and can
637 sometimes be wasteful (in cases where programs immediately
638 afterward allocate more large chunks) the value should be high
639 enough so that your overall system performance would improve by
640 releasing.
642 The trim threshold and the mmap control parameters (see below)
643 can be traded off with one another. Trimming and mmapping are
644 two different ways of releasing unused memory back to the
645 system. Between these two, it is often possible to keep
646 system-level demands of a long-lived program down to a bare
647 minimum. For example, in one test suite of sessions measuring
648 the XF86 X server on Linux, using a trim threshold of 128K and a
649 mmap threshold of 192K led to near-minimal long term resource
650 consumption.
652 If you are using this malloc in a long-lived program, it should
653 pay to experiment with these values. As a rough guide, you
654 might set to a value close to the average size of a process
655 (program) running on your system. Releasing this much memory
656 would allow such a process to run in memory. Generally, it's
657 worth it to tune for trimming rather tham memory mapping when a
658 program undergoes phases where several large chunks are
659 allocated and released in ways that can reuse each other's
660 storage, perhaps mixed with phases where there are no such
661 chunks at all. And in well-behaved long-lived programs,
662 controlling release of large blocks via trimming versus mapping
663 is usually faster.
665 However, in most programs, these parameters serve mainly as
666 protection against the system-level effects of carrying around
667 massive amounts of unneeded memory. Since frequent calls to
668 sbrk, mmap, and munmap otherwise degrade performance, the default
669 parameters are set to relatively high values that serve only as
670 safeguards.
672 The default trim value is high enough to cause trimming only in
673 fairly extreme (by current memory consumption standards) cases.
674 It must be greater than page size to have any useful effect. To
675 disable trimming completely, you can set to (unsigned long)(-1);
681 #ifndef DEFAULT_TOP_PAD
682 #define DEFAULT_TOP_PAD (0)
683 #endif
686 M_TOP_PAD is the amount of extra `padding' space to allocate or
687 retain whenever sbrk is called. It is used in two ways internally:
689 * When sbrk is called to extend the top of the arena to satisfy
690 a new malloc request, this much padding is added to the sbrk
691 request.
693 * When malloc_trim is called automatically from free(),
694 it is used as the `pad' argument.
696 In both cases, the actual amount of padding is rounded
697 so that the end of the arena is always a system page boundary.
699 The main reason for using padding is to avoid calling sbrk so
700 often. Having even a small pad greatly reduces the likelihood
701 that nearly every malloc request during program start-up (or
702 after trimming) will invoke sbrk, which needlessly wastes
703 time.
705 Automatic rounding-up to page-size units is normally sufficient
706 to avoid measurable overhead, so the default is 0. However, in
707 systems where sbrk is relatively slow, it can pay to increase
708 this value, at the expense of carrying around more memory than
709 the program needs.
714 #ifndef DEFAULT_MMAP_THRESHOLD
715 #define DEFAULT_MMAP_THRESHOLD (128 * 1024)
716 #endif
720 M_MMAP_THRESHOLD is the request size threshold for using mmap()
721 to service a request. Requests of at least this size that cannot
722 be allocated using already-existing space will be serviced via mmap.
723 (If enough normal freed space already exists it is used instead.)
725 Using mmap segregates relatively large chunks of memory so that
726 they can be individually obtained and released from the host
727 system. A request serviced through mmap is never reused by any
728 other request (at least not directly; the system may just so
729 happen to remap successive requests to the same locations).
731 Segregating space in this way has the benefit that mmapped space
732 can ALWAYS be individually released back to the system, which
733 helps keep the system level memory demands of a long-lived
734 program low. Mapped memory can never become `locked' between
735 other chunks, as can happen with normally allocated chunks, which
736 menas that even trimming via malloc_trim would not release them.
738 However, it has the disadvantages that:
740 1. The space cannot be reclaimed, consolidated, and then
741 used to service later requests, as happens with normal chunks.
742 2. It can lead to more wastage because of mmap page alignment
743 requirements
744 3. It causes malloc performance to be more dependent on host
745 system memory management support routines which may vary in
746 implementation quality and may impose arbitrary
747 limitations. Generally, servicing a request via normal
748 malloc steps is faster than going through a system's mmap.
750 All together, these considerations should lead you to use mmap
751 only for relatively large requests.
758 #ifndef DEFAULT_MMAP_MAX
759 #if HAVE_MMAP
760 #define DEFAULT_MMAP_MAX (1024)
761 #else
762 #define DEFAULT_MMAP_MAX (0)
763 #endif
764 #endif
767 M_MMAP_MAX is the maximum number of requests to simultaneously
768 service using mmap. This parameter exists because:
770 1. Some systems have a limited number of internal tables for
771 use by mmap.
772 2. In most systems, overreliance on mmap can degrade overall
773 performance.
774 3. If a program allocates many large regions, it is probably
775 better off using normal sbrk-based allocation routines that
776 can reclaim and reallocate normal heap memory. Using a
777 small value allows transition into this mode after the
778 first few allocations.
780 Setting to 0 disables all use of mmap. If HAVE_MMAP is not set,
781 the default value is 0, and attempts to set it to non-zero values
782 in mallopt will fail.
787 #ifndef DEFAULT_CHECK_ACTION
788 #define DEFAULT_CHECK_ACTION 1
789 #endif
791 /* What to do if the standard debugging hooks are in place and a
792 corrupt pointer is detected: do nothing (0), print an error message
793 (1), or call abort() (2). */
797 #define HEAP_MIN_SIZE (32*1024)
798 #define HEAP_MAX_SIZE (1024*1024) /* must be a power of two */
800 /* HEAP_MIN_SIZE and HEAP_MAX_SIZE limit the size of mmap()ed heaps
801 that are dynamically created for multi-threaded programs. The
802 maximum size must be a power of two, for fast determination of
803 which heap belongs to a chunk. It should be much larger than
804 the mmap threshold, so that requests with a size just below that
805 threshold can be fulfilled without creating too many heaps.
810 #ifndef THREAD_STATS
811 #define THREAD_STATS 0
812 #endif
814 /* If THREAD_STATS is non-zero, some statistics on mutex locking are
815 computed. */
820 Special defines for the Linux/GNU C library.
825 #ifdef _LIBC
827 #if __STD_C
829 Void_t * __default_morecore (ptrdiff_t);
830 static Void_t *(*__morecore)(ptrdiff_t) = __default_morecore;
832 #else
834 Void_t * __default_morecore ();
835 static Void_t *(*__morecore)() = __default_morecore;
837 #endif
839 #define MORECORE (*__morecore)
840 #define MORECORE_FAILURE 0
841 #define MORECORE_CLEARS 1
842 #define mmap __mmap
843 #define munmap __munmap
844 #define mremap __mremap
845 #undef malloc_getpagesize
846 #define malloc_getpagesize __getpagesize()
848 #else /* _LIBC */
850 #if __STD_C
851 extern Void_t* sbrk(ptrdiff_t);
852 #else
853 extern Void_t* sbrk();
854 #endif
856 #ifndef MORECORE
857 #define MORECORE sbrk
858 #endif
860 #ifndef MORECORE_FAILURE
861 #define MORECORE_FAILURE -1
862 #endif
864 #ifndef MORECORE_CLEARS
865 #define MORECORE_CLEARS 1
866 #endif
868 #endif /* _LIBC */
870 #ifdef _LIBC
872 #define cALLOc __libc_calloc
873 #define fREe __libc_free
874 #define mALLOc __libc_malloc
875 #define mEMALIGn __libc_memalign
876 #define rEALLOc __libc_realloc
877 #define vALLOc __libc_valloc
878 #define pvALLOc __libc_pvalloc
879 #define mALLINFo __libc_mallinfo
880 #define mALLOPt __libc_mallopt
882 #else
884 #define cALLOc calloc
885 #define fREe free
886 #define mALLOc malloc
887 #define mEMALIGn memalign
888 #define rEALLOc realloc
889 #define vALLOc valloc
890 #define pvALLOc pvalloc
891 #define mALLINFo mallinfo
892 #define mALLOPt mallopt
894 #endif
896 /* Public routines */
898 #if __STD_C
900 #ifndef _LIBC
901 void ptmalloc_init(void);
902 #endif
903 Void_t* mALLOc(size_t);
904 void fREe(Void_t*);
905 Void_t* rEALLOc(Void_t*, size_t);
906 Void_t* mEMALIGn(size_t, size_t);
907 Void_t* vALLOc(size_t);
908 Void_t* pvALLOc(size_t);
909 Void_t* cALLOc(size_t, size_t);
910 void cfree(Void_t*);
911 int __malloc_trim(size_t);
912 int malloc_trim(size_t);
913 size_t __malloc_usable_size(Void_t*);
914 size_t malloc_usable_size(Void_t*);
915 void __malloc_stats(void);
916 void malloc_stats(void);
917 int mALLOPt(int, int);
918 struct mallinfo mALLINFo(void);
919 #else
920 #ifndef _LIBC
921 void ptmalloc_init();
922 #endif
923 Void_t* mALLOc();
924 void fREe();
925 Void_t* rEALLOc();
926 Void_t* mEMALIGn();
927 Void_t* vALLOc();
928 Void_t* pvALLOc();
929 Void_t* cALLOc();
930 void cfree();
931 int __malloc_trim();
932 int malloc_trim();
933 size_t _malloc_usable_size();
934 size_t malloc_usable_size();
935 void __malloc_stats();
936 void malloc_stats();
937 int mALLOPt();
938 struct mallinfo mALLINFo();
939 #endif
942 #ifdef __cplusplus
943 }; /* end of extern "C" */
944 #endif
946 #if !defined(NO_THREADS) && !HAVE_MMAP
947 "Can't have threads support without mmap"
948 #endif
952 Type declarations
956 struct malloc_chunk
958 INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
959 INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
960 struct malloc_chunk* fd; /* double links -- used only if free. */
961 struct malloc_chunk* bk;
964 typedef struct malloc_chunk* mchunkptr;
968 malloc_chunk details:
970 (The following includes lightly edited explanations by Colin Plumb.)
972 Chunks of memory are maintained using a `boundary tag' method as
973 described in e.g., Knuth or Standish. (See the paper by Paul
974 Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
975 survey of such techniques.) Sizes of free chunks are stored both
976 in the front of each chunk and at the end. This makes
977 consolidating fragmented chunks into bigger chunks very fast. The
978 size fields also hold bits representing whether chunks are free or
979 in use.
981 An allocated chunk looks like this:
984 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
985 | Size of previous chunk, if allocated | |
986 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
987 | Size of chunk, in bytes |P|
988 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
989 | User data starts here... .
991 . (malloc_usable_space() bytes) .
993 nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
994 | Size of chunk |
995 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
998 Where "chunk" is the front of the chunk for the purpose of most of
999 the malloc code, but "mem" is the pointer that is returned to the
1000 user. "Nextchunk" is the beginning of the next contiguous chunk.
1002 Chunks always begin on even word boundaries, so the mem portion
1003 (which is returned to the user) is also on an even word boundary, and
1004 thus double-word aligned.
1006 Free chunks are stored in circular doubly-linked lists, and look like this:
1008 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1009 | Size of previous chunk |
1010 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1011 `head:' | Size of chunk, in bytes |P|
1012 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1013 | Forward pointer to next chunk in list |
1014 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1015 | Back pointer to previous chunk in list |
1016 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1017 | Unused space (may be 0 bytes long) .
1020 nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1021 `foot:' | Size of chunk, in bytes |
1022 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1024 The P (PREV_INUSE) bit, stored in the unused low-order bit of the
1025 chunk size (which is always a multiple of two words), is an in-use
1026 bit for the *previous* chunk. If that bit is *clear*, then the
1027 word before the current chunk size contains the previous chunk
1028 size, and can be used to find the front of the previous chunk.
1029 (The very first chunk allocated always has this bit set,
1030 preventing access to non-existent (or non-owned) memory.)
1032 Note that the `foot' of the current chunk is actually represented
1033 as the prev_size of the NEXT chunk. (This makes it easier to
1034 deal with alignments etc).
1036 The two exceptions to all this are
1038 1. The special chunk `top', which doesn't bother using the
1039 trailing size field since there is no
1040 next contiguous chunk that would have to index off it. (After
1041 initialization, `top' is forced to always exist. If it would
1042 become less than MINSIZE bytes long, it is replenished via
1043 malloc_extend_top.)
1045 2. Chunks allocated via mmap, which have the second-lowest-order
1046 bit (IS_MMAPPED) set in their size fields. Because they are
1047 never merged or traversed from any other chunk, they have no
1048 foot size or inuse information.
1050 Available chunks are kept in any of several places (all declared below):
1052 * `av': An array of chunks serving as bin headers for consolidated
1053 chunks. Each bin is doubly linked. The bins are approximately
1054 proportionally (log) spaced. There are a lot of these bins
1055 (128). This may look excessive, but works very well in
1056 practice. All procedures maintain the invariant that no
1057 consolidated chunk physically borders another one. Chunks in
1058 bins are kept in size order, with ties going to the
1059 approximately least recently used chunk.
1061 The chunks in each bin are maintained in decreasing sorted order by
1062 size. This is irrelevant for the small bins, which all contain
1063 the same-sized chunks, but facilitates best-fit allocation for
1064 larger chunks. (These lists are just sequential. Keeping them in
1065 order almost never requires enough traversal to warrant using
1066 fancier ordered data structures.) Chunks of the same size are
1067 linked with the most recently freed at the front, and allocations
1068 are taken from the back. This results in LRU or FIFO allocation
1069 order, which tends to give each chunk an equal opportunity to be
1070 consolidated with adjacent freed chunks, resulting in larger free
1071 chunks and less fragmentation.
1073 * `top': The top-most available chunk (i.e., the one bordering the
1074 end of available memory) is treated specially. It is never
1075 included in any bin, is used only if no other chunk is
1076 available, and is released back to the system if it is very
1077 large (see M_TRIM_THRESHOLD).
1079 * `last_remainder': A bin holding only the remainder of the
1080 most recently split (non-top) chunk. This bin is checked
1081 before other non-fitting chunks, so as to provide better
1082 locality for runs of sequentially allocated chunks.
1084 * Implicitly, through the host system's memory mapping tables.
1085 If supported, requests greater than a threshold are usually
1086 serviced via calls to mmap, and then later released via munmap.
1091 Bins
1093 The bins are an array of pairs of pointers serving as the
1094 heads of (initially empty) doubly-linked lists of chunks, laid out
1095 in a way so that each pair can be treated as if it were in a
1096 malloc_chunk. (This way, the fd/bk offsets for linking bin heads
1097 and chunks are the same).
1099 Bins for sizes < 512 bytes contain chunks of all the same size, spaced
1100 8 bytes apart. Larger bins are approximately logarithmically
1101 spaced. (See the table below.)
1103 Bin layout:
1105 64 bins of size 8
1106 32 bins of size 64
1107 16 bins of size 512
1108 8 bins of size 4096
1109 4 bins of size 32768
1110 2 bins of size 262144
1111 1 bin of size what's left
1113 There is actually a little bit of slop in the numbers in bin_index
1114 for the sake of speed. This makes no difference elsewhere.
1116 The special chunks `top' and `last_remainder' get their own bins,
1117 (this is implemented via yet more trickery with the av array),
1118 although `top' is never properly linked to its bin since it is
1119 always handled specially.
1123 #define NAV 128 /* number of bins */
1125 typedef struct malloc_chunk* mbinptr;
1127 /* An arena is a configuration of malloc_chunks together with an array
1128 of bins. With multiple threads, it must be locked via a mutex
1129 before changing its data structures. One or more `heaps' are
1130 associated with each arena, except for the main_arena, which is
1131 associated only with the `main heap', i.e. the conventional free
1132 store obtained with calls to MORECORE() (usually sbrk). The `av'
1133 array is never mentioned directly in the code, but instead used via
1134 bin access macros. */
1136 typedef struct _arena {
1137 mbinptr av[2*NAV + 2];
1138 struct _arena *next;
1139 size_t size;
1140 #if THREAD_STATS
1141 long stat_lock_direct, stat_lock_loop, stat_lock_wait;
1142 #endif
1143 mutex_t mutex;
1144 } arena;
1147 /* A heap is a single contiguous memory region holding (coalesceable)
1148 malloc_chunks. It is allocated with mmap() and always starts at an
1149 address aligned to HEAP_MAX_SIZE. Not used unless compiling for
1150 multiple threads. */
1152 typedef struct _heap_info {
1153 arena *ar_ptr; /* Arena for this heap. */
1154 struct _heap_info *prev; /* Previous heap. */
1155 size_t size; /* Current size in bytes. */
1156 size_t pad; /* Make sure the following data is properly aligned. */
1157 } heap_info;
1161 Static functions (forward declarations)
1164 #if __STD_C
1166 static void chunk_free(arena *ar_ptr, mchunkptr p);
1167 static mchunkptr chunk_alloc(arena *ar_ptr, INTERNAL_SIZE_T size);
1168 static mchunkptr chunk_realloc(arena *ar_ptr, mchunkptr oldp,
1169 INTERNAL_SIZE_T oldsize, INTERNAL_SIZE_T nb);
1170 static mchunkptr chunk_align(arena *ar_ptr, INTERNAL_SIZE_T nb,
1171 size_t alignment);
1172 static int main_trim(size_t pad);
1173 #ifndef NO_THREADS
1174 static int heap_trim(heap_info *heap, size_t pad);
1175 #endif
1176 #if defined(_LIBC) || defined(MALLOC_HOOKS)
1177 static Void_t* malloc_check(size_t sz);
1178 static void free_check(Void_t* mem);
1179 static Void_t* realloc_check(Void_t* oldmem, size_t bytes);
1180 static Void_t* memalign_check(size_t alignment, size_t bytes);
1181 #endif
1183 #else
1185 static void chunk_free();
1186 static mchunkptr chunk_alloc();
1187 static mchunkptr chunk_realloc();
1188 static mchunkptr chunk_align();
1189 static int main_trim();
1190 #ifndef NO_THREADS
1191 static int heap_trim();
1192 #endif
1193 #if defined(_LIBC) || defined(MALLOC_HOOKS)
1194 static Void_t* malloc_check();
1195 static void free_check();
1196 static Void_t* realloc_check();
1197 static Void_t* memalign_check();
1198 #endif
1200 #endif
1204 /* sizes, alignments */
1206 #define SIZE_SZ (sizeof(INTERNAL_SIZE_T))
1207 #define MALLOC_ALIGNMENT (SIZE_SZ + SIZE_SZ)
1208 #define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
1209 #define MINSIZE (sizeof(struct malloc_chunk))
1211 /* conversion from malloc headers to user pointers, and back */
1213 #define chunk2mem(p) ((Void_t*)((char*)(p) + 2*SIZE_SZ))
1214 #define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
1216 /* pad request bytes into a usable size */
1218 #define request2size(req) \
1219 (((long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \
1220 (long)(MINSIZE + MALLOC_ALIGN_MASK)) ? MINSIZE : \
1221 (((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK)))
1223 /* Check if m has acceptable alignment */
1225 #define aligned_OK(m) (((unsigned long)((m)) & (MALLOC_ALIGN_MASK)) == 0)
1231 Physical chunk operations
1235 /* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
1237 #define PREV_INUSE 0x1
1239 /* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
1241 #define IS_MMAPPED 0x2
1243 /* Bits to mask off when extracting size */
1245 #define SIZE_BITS (PREV_INUSE|IS_MMAPPED)
1248 /* Ptr to next physical malloc_chunk. */
1250 #define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))
1252 /* Ptr to previous physical malloc_chunk */
1254 #define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
1257 /* Treat space at ptr + offset as a chunk */
1259 #define chunk_at_offset(p, s) ((mchunkptr)(((char*)(p)) + (s)))
1265 Dealing with use bits
1268 /* extract p's inuse bit */
1270 #define inuse(p) \
1271 ((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)
1273 /* extract inuse bit of previous chunk */
1275 #define prev_inuse(p) ((p)->size & PREV_INUSE)
1277 /* check for mmap()'ed chunk */
1279 #define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
1281 /* set/clear chunk as in use without otherwise disturbing */
1283 #define set_inuse(p) \
1284 ((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE
1286 #define clear_inuse(p) \
1287 ((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)
1289 /* check/set/clear inuse bits in known places */
1291 #define inuse_bit_at_offset(p, s)\
1292 (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
1294 #define set_inuse_bit_at_offset(p, s)\
1295 (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
1297 #define clear_inuse_bit_at_offset(p, s)\
1298 (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
1304 Dealing with size fields
1307 /* Get size, ignoring use bits */
1309 #define chunksize(p) ((p)->size & ~(SIZE_BITS))
1311 /* Set size at head, without disturbing its use bit */
1313 #define set_head_size(p, s) ((p)->size = (((p)->size & PREV_INUSE) | (s)))
1315 /* Set size/use ignoring previous bits in header */
1317 #define set_head(p, s) ((p)->size = (s))
1319 /* Set size at footer (only when chunk is not in use) */
1321 #define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
1327 /* access macros */
1329 #define bin_at(a, i) ((mbinptr)((char*)&(((a)->av)[2*(i) + 2]) - 2*SIZE_SZ))
1330 #define init_bin(a, i) ((a)->av[2*i+2] = (a)->av[2*i+3] = bin_at((a), i))
1331 #define next_bin(b) ((mbinptr)((char*)(b) + 2 * sizeof(mbinptr)))
1332 #define prev_bin(b) ((mbinptr)((char*)(b) - 2 * sizeof(mbinptr)))
1335 The first 2 bins are never indexed. The corresponding av cells are instead
1336 used for bookkeeping. This is not to save space, but to simplify
1337 indexing, maintain locality, and avoid some initialization tests.
1340 #define binblocks(a) (bin_at(a,0)->size)/* bitvector of nonempty blocks */
1341 #define top(a) (bin_at(a,0)->fd) /* The topmost chunk */
1342 #define last_remainder(a) (bin_at(a,1)) /* remainder from last split */
1345 Because top initially points to its own bin with initial
1346 zero size, thus forcing extension on the first malloc request,
1347 we avoid having any special code in malloc to check whether
1348 it even exists yet. But we still need to in malloc_extend_top.
1351 #define initial_top(a) ((mchunkptr)bin_at(a, 0))
1355 /* field-extraction macros */
1357 #define first(b) ((b)->fd)
1358 #define last(b) ((b)->bk)
1361 Indexing into bins
1364 #define bin_index(sz) \
1365 (((((unsigned long)(sz)) >> 9) == 0) ? (((unsigned long)(sz)) >> 3): \
1366 ((((unsigned long)(sz)) >> 9) <= 4) ? 56 + (((unsigned long)(sz)) >> 6): \
1367 ((((unsigned long)(sz)) >> 9) <= 20) ? 91 + (((unsigned long)(sz)) >> 9): \
1368 ((((unsigned long)(sz)) >> 9) <= 84) ? 110 + (((unsigned long)(sz)) >> 12): \
1369 ((((unsigned long)(sz)) >> 9) <= 340) ? 119 + (((unsigned long)(sz)) >> 15): \
1370 ((((unsigned long)(sz)) >> 9) <= 1364) ? 124 + (((unsigned long)(sz)) >> 18): \
1371 126)
1373 bins for chunks < 512 are all spaced 8 bytes apart, and hold
1374 identically sized chunks. This is exploited in malloc.
1377 #define MAX_SMALLBIN 63
1378 #define MAX_SMALLBIN_SIZE 512
1379 #define SMALLBIN_WIDTH 8
1381 #define smallbin_index(sz) (((unsigned long)(sz)) >> 3)
1384 Requests are `small' if both the corresponding and the next bin are small
1387 #define is_small_request(nb) ((nb) < MAX_SMALLBIN_SIZE - SMALLBIN_WIDTH)
1392 To help compensate for the large number of bins, a one-level index
1393 structure is used for bin-by-bin searching. `binblocks' is a
1394 one-word bitvector recording whether groups of BINBLOCKWIDTH bins
1395 have any (possibly) non-empty bins, so they can be skipped over
1396 all at once during during traversals. The bits are NOT always
1397 cleared as soon as all bins in a block are empty, but instead only
1398 when all are noticed to be empty during traversal in malloc.
1401 #define BINBLOCKWIDTH 4 /* bins per block */
1403 /* bin<->block macros */
1405 #define idx2binblock(ix) ((unsigned)1 << ((ix) / BINBLOCKWIDTH))
1406 #define mark_binblock(a, ii) (binblocks(a) |= idx2binblock(ii))
1407 #define clear_binblock(a, ii) (binblocks(a) &= ~(idx2binblock(ii)))
1412 /* Static bookkeeping data */
1414 /* Helper macro to initialize bins */
1415 #define IAV(i) bin_at(&main_arena, i), bin_at(&main_arena, i)
1417 static arena main_arena = {
1419 0, 0,
1420 IAV(0), IAV(1), IAV(2), IAV(3), IAV(4), IAV(5), IAV(6), IAV(7),
1421 IAV(8), IAV(9), IAV(10), IAV(11), IAV(12), IAV(13), IAV(14), IAV(15),
1422 IAV(16), IAV(17), IAV(18), IAV(19), IAV(20), IAV(21), IAV(22), IAV(23),
1423 IAV(24), IAV(25), IAV(26), IAV(27), IAV(28), IAV(29), IAV(30), IAV(31),
1424 IAV(32), IAV(33), IAV(34), IAV(35), IAV(36), IAV(37), IAV(38), IAV(39),
1425 IAV(40), IAV(41), IAV(42), IAV(43), IAV(44), IAV(45), IAV(46), IAV(47),
1426 IAV(48), IAV(49), IAV(50), IAV(51), IAV(52), IAV(53), IAV(54), IAV(55),
1427 IAV(56), IAV(57), IAV(58), IAV(59), IAV(60), IAV(61), IAV(62), IAV(63),
1428 IAV(64), IAV(65), IAV(66), IAV(67), IAV(68), IAV(69), IAV(70), IAV(71),
1429 IAV(72), IAV(73), IAV(74), IAV(75), IAV(76), IAV(77), IAV(78), IAV(79),
1430 IAV(80), IAV(81), IAV(82), IAV(83), IAV(84), IAV(85), IAV(86), IAV(87),
1431 IAV(88), IAV(89), IAV(90), IAV(91), IAV(92), IAV(93), IAV(94), IAV(95),
1432 IAV(96), IAV(97), IAV(98), IAV(99), IAV(100), IAV(101), IAV(102), IAV(103),
1433 IAV(104), IAV(105), IAV(106), IAV(107), IAV(108), IAV(109), IAV(110), IAV(111),
1434 IAV(112), IAV(113), IAV(114), IAV(115), IAV(116), IAV(117), IAV(118), IAV(119),
1435 IAV(120), IAV(121), IAV(122), IAV(123), IAV(124), IAV(125), IAV(126), IAV(127)
1437 NULL, /* next */
1438 0, /* size */
1439 #if THREAD_STATS
1440 0, 0, 0, /* stat_lock_direct, stat_lock_loop, stat_lock_wait */
1441 #endif
1442 MUTEX_INITIALIZER /* mutex */
1445 #undef IAV
1447 /* Thread specific data */
1449 #ifndef NO_THREADS
1450 static tsd_key_t arena_key;
1451 static mutex_t list_lock = MUTEX_INITIALIZER;
1452 #endif
1454 #if THREAD_STATS
1455 static int stat_n_heaps = 0;
1456 #define THREAD_STAT(x) x
1457 #else
1458 #define THREAD_STAT(x) do ; while(0)
1459 #endif
1461 /* variables holding tunable values */
1463 static unsigned long trim_threshold = DEFAULT_TRIM_THRESHOLD;
1464 static unsigned long top_pad = DEFAULT_TOP_PAD;
1465 static unsigned int n_mmaps_max = DEFAULT_MMAP_MAX;
1466 static unsigned long mmap_threshold = DEFAULT_MMAP_THRESHOLD;
1467 static int check_action = DEFAULT_CHECK_ACTION;
1469 /* The first value returned from sbrk */
1470 static char* sbrk_base = (char*)(-1);
1472 /* The maximum memory obtained from system via sbrk */
1473 static unsigned long max_sbrked_mem = 0;
1475 /* The maximum via either sbrk or mmap (too difficult to track with threads) */
1476 #ifdef NO_THREADS
1477 static unsigned long max_total_mem = 0;
1478 #endif
1480 /* The total memory obtained from system via sbrk */
1481 #define sbrked_mem (main_arena.size)
1483 /* Tracking mmaps */
1485 static unsigned int n_mmaps = 0;
1486 static unsigned int max_n_mmaps = 0;
1487 static unsigned long mmapped_mem = 0;
1488 static unsigned long max_mmapped_mem = 0;
1492 /* Already initialized? */
1493 int __malloc_initialized;
1496 /* Initialization routine. */
1497 #if defined(_LIBC)
1498 #if 0
1499 static void ptmalloc_init __MALLOC_P ((void)) __attribute__ ((constructor));
1500 #endif
1502 static void
1503 ptmalloc_init __MALLOC_P((void))
1504 #else
1505 void
1506 ptmalloc_init __MALLOC_P((void))
1507 #endif
1509 #if defined(_LIBC) || defined(MALLOC_HOOKS)
1510 const char* s;
1511 #endif
1513 if(__malloc_initialized) return;
1514 __malloc_initialized = 1;
1515 #if defined(_LIBC)
1516 /* Initialize the pthreads interface. */
1517 if (__pthread_initialize != NULL)
1518 __pthread_initialize();
1519 #endif
1520 #ifndef NO_THREADS
1521 mutex_init(&main_arena.mutex);
1522 mutex_init(&list_lock);
1523 tsd_key_create(&arena_key, NULL);
1524 tsd_setspecific(arena_key, (Void_t *)&main_arena);
1525 #endif
1526 #if defined(_LIBC) || defined(MALLOC_HOOKS)
1527 s = getenv("MALLOC_CHECK_");
1528 if(s) {
1529 if(s[0]) mallopt(M_CHECK_ACTION, (int)(s[0] - '0'));
1530 malloc_check_init();
1532 if(__malloc_initialize_hook != NULL)
1533 (*__malloc_initialize_hook)();
1534 #endif
1537 #if defined(_LIBC) || defined(MALLOC_HOOKS)
1539 /* Hooks for debugging versions. The initial hooks just call the
1540 initialization routine, then do the normal work. */
1542 static Void_t*
1543 #if __STD_C
1544 malloc_hook_ini(size_t sz)
1545 #else
1546 malloc_hook_ini(sz) size_t sz;
1547 #endif
1549 __malloc_hook = NULL;
1550 __realloc_hook = NULL;
1551 __memalign_hook = NULL;
1552 ptmalloc_init();
1553 return mALLOc(sz);
1556 static Void_t*
1557 #if __STD_C
1558 realloc_hook_ini(Void_t* ptr, size_t sz)
1559 #else
1560 realloc_hook_ini(ptr, sz) Void_t* ptr; size_t sz;
1561 #endif
1563 __malloc_hook = NULL;
1564 __realloc_hook = NULL;
1565 __memalign_hook = NULL;
1566 ptmalloc_init();
1567 return rEALLOc(ptr, sz);
1570 static Void_t*
1571 #if __STD_C
1572 memalign_hook_ini(size_t sz, size_t alignment)
1573 #else
1574 memalign_hook_ini(sz, alignment) size_t sz; size_t alignment;
1575 #endif
1577 __malloc_hook = NULL;
1578 __realloc_hook = NULL;
1579 __memalign_hook = NULL;
1580 ptmalloc_init();
1581 return mEMALIGn(sz, alignment);
1584 void (*__malloc_initialize_hook) __MALLOC_P ((void)) = NULL;
1585 void (*__free_hook) __MALLOC_P ((__malloc_ptr_t __ptr)) = NULL;
1586 __malloc_ptr_t (*__malloc_hook)
1587 __MALLOC_P ((size_t __size)) = malloc_hook_ini;
1588 __malloc_ptr_t (*__realloc_hook)
1589 __MALLOC_P ((__malloc_ptr_t __ptr, size_t __size)) = realloc_hook_ini;
1590 __malloc_ptr_t (*__memalign_hook)
1591 __MALLOC_P ((size_t __size, size_t __alignment)) = memalign_hook_ini;
1593 /* Activate a standard set of debugging hooks. */
1594 void
1595 malloc_check_init()
1597 __malloc_hook = malloc_check;
1598 __free_hook = free_check;
1599 __realloc_hook = realloc_check;
1600 __memalign_hook = memalign_check;
1601 fprintf(stderr, "Using debugging hooks\n");
1604 #endif
1610 /* Routines dealing with mmap(). */
1612 #if HAVE_MMAP
1614 #ifndef MAP_ANONYMOUS
1616 static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */
1618 #define MMAP(size, prot) ((dev_zero_fd < 0) ? \
1619 (dev_zero_fd = open("/dev/zero", O_RDWR), \
1620 mmap(0, (size), (prot), MAP_PRIVATE, dev_zero_fd, 0)) : \
1621 mmap(0, (size), (prot), MAP_PRIVATE, dev_zero_fd, 0))
1623 #else
1625 #define MMAP(size, prot) \
1626 (mmap(0, (size), (prot), MAP_PRIVATE|MAP_ANONYMOUS, -1, 0))
1628 #endif
1630 #if __STD_C
1631 static mchunkptr mmap_chunk(size_t size)
1632 #else
1633 static mchunkptr mmap_chunk(size) size_t size;
1634 #endif
1636 size_t page_mask = malloc_getpagesize - 1;
1637 mchunkptr p;
1639 if(n_mmaps >= n_mmaps_max) return 0; /* too many regions */
1641 /* For mmapped chunks, the overhead is one SIZE_SZ unit larger, because
1642 * there is no following chunk whose prev_size field could be used.
1644 size = (size + SIZE_SZ + page_mask) & ~page_mask;
1646 p = (mchunkptr)MMAP(size, PROT_READ|PROT_WRITE);
1647 if(p == (mchunkptr)-1) return 0;
1649 n_mmaps++;
1650 if (n_mmaps > max_n_mmaps) max_n_mmaps = n_mmaps;
1652 /* We demand that eight bytes into a page must be 8-byte aligned. */
1653 assert(aligned_OK(chunk2mem(p)));
1655 /* The offset to the start of the mmapped region is stored
1656 * in the prev_size field of the chunk; normally it is zero,
1657 * but that can be changed in memalign().
1659 p->prev_size = 0;
1660 set_head(p, size|IS_MMAPPED);
1662 mmapped_mem += size;
1663 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
1664 max_mmapped_mem = mmapped_mem;
1665 #ifdef NO_THREADS
1666 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
1667 max_total_mem = mmapped_mem + sbrked_mem;
1668 #endif
1669 return p;
1672 #if __STD_C
1673 static void munmap_chunk(mchunkptr p)
1674 #else
1675 static void munmap_chunk(p) mchunkptr p;
1676 #endif
1678 INTERNAL_SIZE_T size = chunksize(p);
1679 int ret;
1681 assert (chunk_is_mmapped(p));
1682 assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem));
1683 assert((n_mmaps > 0));
1684 assert(((p->prev_size + size) & (malloc_getpagesize-1)) == 0);
1686 n_mmaps--;
1687 mmapped_mem -= (size + p->prev_size);
1689 ret = munmap((char *)p - p->prev_size, size + p->prev_size);
1691 /* munmap returns non-zero on failure */
1692 assert(ret == 0);
1695 #if HAVE_MREMAP
1697 #if __STD_C
1698 static mchunkptr mremap_chunk(mchunkptr p, size_t new_size)
1699 #else
1700 static mchunkptr mremap_chunk(p, new_size) mchunkptr p; size_t new_size;
1701 #endif
1703 size_t page_mask = malloc_getpagesize - 1;
1704 INTERNAL_SIZE_T offset = p->prev_size;
1705 INTERNAL_SIZE_T size = chunksize(p);
1706 char *cp;
1708 assert (chunk_is_mmapped(p));
1709 assert(! ((char*)p >= sbrk_base && (char*)p < sbrk_base + sbrked_mem));
1710 assert((n_mmaps > 0));
1711 assert(((size + offset) & (malloc_getpagesize-1)) == 0);
1713 /* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
1714 new_size = (new_size + offset + SIZE_SZ + page_mask) & ~page_mask;
1716 cp = (char *)mremap((char *)p - offset, size + offset, new_size,
1717 MREMAP_MAYMOVE);
1719 if (cp == (char *)-1) return 0;
1721 p = (mchunkptr)(cp + offset);
1723 assert(aligned_OK(chunk2mem(p)));
1725 assert((p->prev_size == offset));
1726 set_head(p, (new_size - offset)|IS_MMAPPED);
1728 mmapped_mem -= size + offset;
1729 mmapped_mem += new_size;
1730 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem)
1731 max_mmapped_mem = mmapped_mem;
1732 #ifdef NO_THREADS
1733 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem)
1734 max_total_mem = mmapped_mem + sbrked_mem;
1735 #endif
1736 return p;
1739 #endif /* HAVE_MREMAP */
1741 #endif /* HAVE_MMAP */
1745 /* Managing heaps and arenas (for concurrent threads) */
1747 #ifndef NO_THREADS
1749 /* Create a new heap. size is automatically rounded up to a multiple
1750 of the page size. */
1752 static heap_info *
1753 #if __STD_C
1754 new_heap(size_t size)
1755 #else
1756 new_heap(size) size_t size;
1757 #endif
1759 size_t page_mask = malloc_getpagesize - 1;
1760 char *p1, *p2;
1761 unsigned long ul;
1762 heap_info *h;
1764 if(size < HEAP_MIN_SIZE)
1765 size = HEAP_MIN_SIZE;
1766 size = (size + page_mask) & ~page_mask;
1767 if(size > HEAP_MAX_SIZE)
1768 return 0;
1769 p1 = (char *)MMAP(HEAP_MAX_SIZE<<1, PROT_NONE);
1770 if(p1 == (char *)-1)
1771 return 0;
1772 p2 = (char *)(((unsigned long)p1 + HEAP_MAX_SIZE) & ~(HEAP_MAX_SIZE-1));
1773 ul = p2 - p1;
1774 munmap(p1, ul);
1775 munmap(p2 + HEAP_MAX_SIZE, HEAP_MAX_SIZE - ul);
1776 if(mprotect(p2, size, PROT_READ|PROT_WRITE) != 0) {
1777 munmap(p2, HEAP_MAX_SIZE);
1778 return 0;
1780 h = (heap_info *)p2;
1781 h->size = size;
1782 THREAD_STAT(stat_n_heaps++);
1783 return h;
1786 /* Grow or shrink a heap. size is automatically rounded up to a
1787 multiple of the page size if it is positive. */
1789 static int
1790 #if __STD_C
1791 grow_heap(heap_info *h, long diff)
1792 #else
1793 grow_heap(h, diff) heap_info *h; long diff;
1794 #endif
1796 size_t page_mask = malloc_getpagesize - 1;
1797 long new_size;
1799 if(diff >= 0) {
1800 diff = (diff + page_mask) & ~page_mask;
1801 new_size = (long)h->size + diff;
1802 if(new_size > HEAP_MAX_SIZE)
1803 return -1;
1804 if(mprotect((char *)h + h->size, diff, PROT_READ|PROT_WRITE) != 0)
1805 return -2;
1806 } else {
1807 new_size = (long)h->size + diff;
1808 if(new_size < (long)sizeof(*h))
1809 return -1;
1810 if(mprotect((char *)h + new_size, -diff, PROT_NONE) != 0)
1811 return -2;
1813 h->size = new_size;
1814 return 0;
1817 /* Delete a heap. */
1819 #define delete_heap(heap) munmap((char*)(heap), HEAP_MAX_SIZE)
1821 /* arena_get() acquires an arena and locks the corresponding mutex.
1822 First, try the one last locked successfully by this thread. (This
1823 is the common case and handled with a macro for speed.) Then, loop
1824 over the singly linked list of arenas. If no arena is readily
1825 available, create a new one. */
1827 #define arena_get(ptr, size) do { \
1828 Void_t *vptr = NULL; \
1829 ptr = (arena *)tsd_getspecific(arena_key, vptr); \
1830 if(ptr && !mutex_trylock(&ptr->mutex)) { \
1831 THREAD_STAT(++(ptr->stat_lock_direct)); \
1832 } else { \
1833 ptr = arena_get2(ptr, (size)); \
1835 } while(0)
1837 static arena *
1838 #if __STD_C
1839 arena_get2(arena *a_tsd, size_t size)
1840 #else
1841 arena_get2(a_tsd, size) arena *a_tsd; size_t size;
1842 #endif
1844 arena *a;
1845 heap_info *h;
1846 char *ptr;
1847 int i;
1848 unsigned long misalign;
1850 /* Check the singly-linked list for unlocked arenas. */
1851 if(a_tsd) {
1852 for(a = a_tsd->next; a; a = a->next) {
1853 if(!mutex_trylock(&a->mutex))
1854 goto done;
1857 for(a = &main_arena; a != a_tsd; a = a->next) {
1858 if(!mutex_trylock(&a->mutex))
1859 goto done;
1862 /* Nothing immediately available, so generate a new arena. */
1863 h = new_heap(size + (sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT));
1864 if(!h)
1865 return 0;
1866 a = h->ar_ptr = (arena *)(h+1);
1867 for(i=0; i<NAV; i++)
1868 init_bin(a, i);
1869 a->size = h->size;
1870 mutex_init(&a->mutex);
1871 i = mutex_lock(&a->mutex); /* remember result */
1873 /* Set up the top chunk, with proper alignment. */
1874 ptr = (char *)(a + 1);
1875 misalign = (unsigned long)chunk2mem(ptr) & MALLOC_ALIGN_MASK;
1876 if (misalign > 0)
1877 ptr += MALLOC_ALIGNMENT - misalign;
1878 top(a) = (mchunkptr)ptr;
1879 set_head(top(a), (((char*)h + h->size) - ptr) | PREV_INUSE);
1881 /* Add the new arena to the list. */
1882 (void)mutex_lock(&list_lock);
1883 a->next = main_arena.next;
1884 main_arena.next = a;
1885 (void)mutex_unlock(&list_lock);
1887 if(i) /* locking failed; keep arena for further attempts later */
1888 return 0;
1890 done:
1891 THREAD_STAT(++(a->stat_lock_loop));
1892 tsd_setspecific(arena_key, (Void_t *)a);
1893 return a;
1896 /* find the heap and corresponding arena for a given ptr */
1898 #define heap_for_ptr(ptr) \
1899 ((heap_info *)((unsigned long)(ptr) & ~(HEAP_MAX_SIZE-1)))
1900 #define arena_for_ptr(ptr) \
1901 (((mchunkptr)(ptr) < top(&main_arena) && (char *)(ptr) >= sbrk_base) ? \
1902 &main_arena : heap_for_ptr(ptr)->ar_ptr)
1904 #else /* defined(NO_THREADS) */
1906 /* Without concurrent threads, there is only one arena. */
1908 #define arena_get(ptr, sz) (ptr = &main_arena)
1909 #define arena_for_ptr(ptr) (&main_arena)
1911 #endif /* !defined(NO_THREADS) */
1916 Debugging support
1919 #if MALLOC_DEBUG
1923 These routines make a number of assertions about the states
1924 of data structures that should be true at all times. If any
1925 are not true, it's very likely that a user program has somehow
1926 trashed memory. (It's also possible that there is a coding error
1927 in malloc. In which case, please report it!)
1930 #if __STD_C
1931 static void do_check_chunk(arena *ar_ptr, mchunkptr p)
1932 #else
1933 static void do_check_chunk(ar_ptr, p) arena *ar_ptr; mchunkptr p;
1934 #endif
1936 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
1938 /* No checkable chunk is mmapped */
1939 assert(!chunk_is_mmapped(p));
1941 #ifndef NO_THREADS
1942 if(ar_ptr != &main_arena) {
1943 heap_info *heap = heap_for_ptr(p);
1944 assert(heap->ar_ptr == ar_ptr);
1945 assert((char *)p + sz <= (char *)heap + heap->size);
1946 return;
1948 #endif
1950 /* Check for legal address ... */
1951 assert((char*)p >= sbrk_base);
1952 if (p != top(ar_ptr))
1953 assert((char*)p + sz <= (char*)top(ar_ptr));
1954 else
1955 assert((char*)p + sz <= sbrk_base + sbrked_mem);
1960 #if __STD_C
1961 static void do_check_free_chunk(arena *ar_ptr, mchunkptr p)
1962 #else
1963 static void do_check_free_chunk(ar_ptr, p) arena *ar_ptr; mchunkptr p;
1964 #endif
1966 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
1967 mchunkptr next = chunk_at_offset(p, sz);
1969 do_check_chunk(ar_ptr, p);
1971 /* Check whether it claims to be free ... */
1972 assert(!inuse(p));
1974 /* Must have OK size and fields */
1975 assert((long)sz >= (long)MINSIZE);
1976 assert((sz & MALLOC_ALIGN_MASK) == 0);
1977 assert(aligned_OK(chunk2mem(p)));
1978 /* ... matching footer field */
1979 assert(next->prev_size == sz);
1980 /* ... and is fully consolidated */
1981 assert(prev_inuse(p));
1982 assert (next == top(ar_ptr) || inuse(next));
1984 /* ... and has minimally sane links */
1985 assert(p->fd->bk == p);
1986 assert(p->bk->fd == p);
1989 #if __STD_C
1990 static void do_check_inuse_chunk(arena *ar_ptr, mchunkptr p)
1991 #else
1992 static void do_check_inuse_chunk(ar_ptr, p) arena *ar_ptr; mchunkptr p;
1993 #endif
1995 mchunkptr next = next_chunk(p);
1996 do_check_chunk(ar_ptr, p);
1998 /* Check whether it claims to be in use ... */
1999 assert(inuse(p));
2001 /* ... whether its size is OK (it might be a fencepost) ... */
2002 assert(chunksize(p) >= MINSIZE || next->size == (0|PREV_INUSE));
2004 /* ... and is surrounded by OK chunks.
2005 Since more things can be checked with free chunks than inuse ones,
2006 if an inuse chunk borders them and debug is on, it's worth doing them.
2008 if (!prev_inuse(p))
2010 mchunkptr prv = prev_chunk(p);
2011 assert(next_chunk(prv) == p);
2012 do_check_free_chunk(ar_ptr, prv);
2014 if (next == top(ar_ptr))
2016 assert(prev_inuse(next));
2017 assert(chunksize(next) >= MINSIZE);
2019 else if (!inuse(next))
2020 do_check_free_chunk(ar_ptr, next);
2024 #if __STD_C
2025 static void do_check_malloced_chunk(arena *ar_ptr,
2026 mchunkptr p, INTERNAL_SIZE_T s)
2027 #else
2028 static void do_check_malloced_chunk(ar_ptr, p, s)
2029 arena *ar_ptr; mchunkptr p; INTERNAL_SIZE_T s;
2030 #endif
2032 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE;
2033 long room = sz - s;
2035 do_check_inuse_chunk(ar_ptr, p);
2037 /* Legal size ... */
2038 assert((long)sz >= (long)MINSIZE);
2039 assert((sz & MALLOC_ALIGN_MASK) == 0);
2040 assert(room >= 0);
2041 assert(room < (long)MINSIZE);
2043 /* ... and alignment */
2044 assert(aligned_OK(chunk2mem(p)));
2047 /* ... and was allocated at front of an available chunk */
2048 assert(prev_inuse(p));
2053 #define check_free_chunk(A,P) do_check_free_chunk(A,P)
2054 #define check_inuse_chunk(A,P) do_check_inuse_chunk(A,P)
2055 #define check_chunk(A,P) do_check_chunk(A,P)
2056 #define check_malloced_chunk(A,P,N) do_check_malloced_chunk(A,P,N)
2057 #else
2058 #define check_free_chunk(A,P)
2059 #define check_inuse_chunk(A,P)
2060 #define check_chunk(A,P)
2061 #define check_malloced_chunk(A,P,N)
2062 #endif
2067 Macro-based internal utilities
2072 Linking chunks in bin lists.
2073 Call these only with variables, not arbitrary expressions, as arguments.
2077 Place chunk p of size s in its bin, in size order,
2078 putting it ahead of others of same size.
2082 #define frontlink(A, P, S, IDX, BK, FD) \
2084 if (S < MAX_SMALLBIN_SIZE) \
2086 IDX = smallbin_index(S); \
2087 mark_binblock(A, IDX); \
2088 BK = bin_at(A, IDX); \
2089 FD = BK->fd; \
2090 P->bk = BK; \
2091 P->fd = FD; \
2092 FD->bk = BK->fd = P; \
2094 else \
2096 IDX = bin_index(S); \
2097 BK = bin_at(A, IDX); \
2098 FD = BK->fd; \
2099 if (FD == BK) mark_binblock(A, IDX); \
2100 else \
2102 while (FD != BK && S < chunksize(FD)) FD = FD->fd; \
2103 BK = FD->bk; \
2105 P->bk = BK; \
2106 P->fd = FD; \
2107 FD->bk = BK->fd = P; \
2112 /* take a chunk off a list */
2114 #define unlink(P, BK, FD) \
2116 BK = P->bk; \
2117 FD = P->fd; \
2118 FD->bk = BK; \
2119 BK->fd = FD; \
2122 /* Place p as the last remainder */
2124 #define link_last_remainder(A, P) \
2126 last_remainder(A)->fd = last_remainder(A)->bk = P; \
2127 P->fd = P->bk = last_remainder(A); \
2130 /* Clear the last_remainder bin */
2132 #define clear_last_remainder(A) \
2133 (last_remainder(A)->fd = last_remainder(A)->bk = last_remainder(A))
2140 Extend the top-most chunk by obtaining memory from system.
2141 Main interface to sbrk (but see also malloc_trim).
2144 #if __STD_C
2145 static void malloc_extend_top(arena *ar_ptr, INTERNAL_SIZE_T nb)
2146 #else
2147 static void malloc_extend_top(ar_ptr, nb) arena *ar_ptr; INTERNAL_SIZE_T nb;
2148 #endif
2150 unsigned long pagesz = malloc_getpagesize;
2151 mchunkptr old_top = top(ar_ptr); /* Record state of old top */
2152 INTERNAL_SIZE_T old_top_size = chunksize(old_top);
2153 INTERNAL_SIZE_T top_size; /* new size of top chunk */
2155 #ifndef NO_THREADS
2156 if(ar_ptr == &main_arena) {
2157 #endif
2159 char* brk; /* return value from sbrk */
2160 INTERNAL_SIZE_T front_misalign; /* unusable bytes at front of sbrked space */
2161 INTERNAL_SIZE_T correction; /* bytes for 2nd sbrk call */
2162 char* new_brk; /* return of 2nd sbrk call */
2163 char* old_end = (char*)(chunk_at_offset(old_top, old_top_size));
2165 /* Pad request with top_pad plus minimal overhead */
2166 INTERNAL_SIZE_T sbrk_size = nb + top_pad + MINSIZE;
2168 /* If not the first time through, round to preserve page boundary */
2169 /* Otherwise, we need to correct to a page size below anyway. */
2170 /* (We also correct below if an intervening foreign sbrk call.) */
2172 if (sbrk_base != (char*)(-1))
2173 sbrk_size = (sbrk_size + (pagesz - 1)) & ~(pagesz - 1);
2175 brk = (char*)(MORECORE (sbrk_size));
2177 /* Fail if sbrk failed or if a foreign sbrk call killed our space */
2178 if (brk == (char*)(MORECORE_FAILURE) ||
2179 (brk < old_end && old_top != initial_top(&main_arena)))
2180 return;
2182 sbrked_mem += sbrk_size;
2184 if (brk == old_end) { /* can just add bytes to current top */
2185 top_size = sbrk_size + old_top_size;
2186 set_head(old_top, top_size | PREV_INUSE);
2187 old_top = 0; /* don't free below */
2188 } else {
2189 if (sbrk_base == (char*)(-1)) /* First time through. Record base */
2190 sbrk_base = brk;
2191 else
2192 /* Someone else called sbrk(). Count those bytes as sbrked_mem. */
2193 sbrked_mem += brk - (char*)old_end;
2195 /* Guarantee alignment of first new chunk made from this space */
2196 front_misalign = (unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK;
2197 if (front_misalign > 0) {
2198 correction = (MALLOC_ALIGNMENT) - front_misalign;
2199 brk += correction;
2200 } else
2201 correction = 0;
2203 /* Guarantee the next brk will be at a page boundary */
2204 correction += pagesz - ((unsigned long)(brk + sbrk_size) & (pagesz - 1));
2206 /* Allocate correction */
2207 new_brk = (char*)(MORECORE (correction));
2208 if (new_brk == (char*)(MORECORE_FAILURE)) return;
2210 sbrked_mem += correction;
2212 top(&main_arena) = (mchunkptr)brk;
2213 top_size = new_brk - brk + correction;
2214 set_head(top(&main_arena), top_size | PREV_INUSE);
2216 if (old_top == initial_top(&main_arena))
2217 old_top = 0; /* don't free below */
2220 if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem)
2221 max_sbrked_mem = sbrked_mem;
2222 #ifdef NO_THREADS
2223 if ((unsigned long)(mmapped_mem + sbrked_mem) >
2224 (unsigned long)max_total_mem)
2225 max_total_mem = mmapped_mem + sbrked_mem;
2226 #endif
2228 #ifndef NO_THREADS
2229 } else { /* ar_ptr != &main_arena */
2230 heap_info *old_heap, *heap;
2231 size_t old_heap_size;
2233 if(old_top_size < MINSIZE) /* this should never happen */
2234 return;
2236 /* First try to extend the current heap. */
2237 if(MINSIZE + nb <= old_top_size)
2238 return;
2239 old_heap = heap_for_ptr(old_top);
2240 old_heap_size = old_heap->size;
2241 if(grow_heap(old_heap, MINSIZE + nb - old_top_size) == 0) {
2242 ar_ptr->size += old_heap->size - old_heap_size;
2243 top_size = ((char *)old_heap + old_heap->size) - (char *)old_top;
2244 set_head(old_top, top_size | PREV_INUSE);
2245 return;
2248 /* A new heap must be created. */
2249 heap = new_heap(nb + top_pad + (MINSIZE + sizeof(*heap)));
2250 if(!heap)
2251 return;
2252 heap->ar_ptr = ar_ptr;
2253 heap->prev = old_heap;
2254 ar_ptr->size += heap->size;
2256 /* Set up the new top, so we can safely use chunk_free() below. */
2257 top(ar_ptr) = chunk_at_offset(heap, sizeof(*heap));
2258 top_size = heap->size - sizeof(*heap);
2259 set_head(top(ar_ptr), top_size | PREV_INUSE);
2261 #endif /* !defined(NO_THREADS) */
2263 /* We always land on a page boundary */
2264 assert(((unsigned long)((char*)top(ar_ptr) + top_size) & (pagesz-1)) == 0);
2266 /* Setup fencepost and free the old top chunk. */
2267 if(old_top) {
2268 /* The fencepost takes at least MINSIZE bytes, because it might
2269 become the top chunk again later. Note that a footer is set
2270 up, too, although the chunk is marked in use. */
2271 old_top_size -= MINSIZE;
2272 set_head(chunk_at_offset(old_top, old_top_size + 2*SIZE_SZ), 0|PREV_INUSE);
2273 if(old_top_size >= MINSIZE) {
2274 set_head(chunk_at_offset(old_top, old_top_size), (2*SIZE_SZ)|PREV_INUSE);
2275 set_foot(chunk_at_offset(old_top, old_top_size), (2*SIZE_SZ));
2276 set_head_size(old_top, old_top_size);
2277 chunk_free(ar_ptr, old_top);
2278 } else {
2279 set_head(old_top, (old_top_size + 2*SIZE_SZ)|PREV_INUSE);
2280 set_foot(old_top, (old_top_size + 2*SIZE_SZ));
2288 /* Main public routines */
2292 Malloc Algorithm:
2294 The requested size is first converted into a usable form, `nb'.
2295 This currently means to add 4 bytes overhead plus possibly more to
2296 obtain 8-byte alignment and/or to obtain a size of at least
2297 MINSIZE (currently 16, 24, or 32 bytes), the smallest allocatable
2298 size. (All fits are considered `exact' if they are within MINSIZE
2299 bytes.)
2301 From there, the first successful of the following steps is taken:
2303 1. The bin corresponding to the request size is scanned, and if
2304 a chunk of exactly the right size is found, it is taken.
2306 2. The most recently remaindered chunk is used if it is big
2307 enough. This is a form of (roving) first fit, used only in
2308 the absence of exact fits. Runs of consecutive requests use
2309 the remainder of the chunk used for the previous such request
2310 whenever possible. This limited use of a first-fit style
2311 allocation strategy tends to give contiguous chunks
2312 coextensive lifetimes, which improves locality and can reduce
2313 fragmentation in the long run.
2315 3. Other bins are scanned in increasing size order, using a
2316 chunk big enough to fulfill the request, and splitting off
2317 any remainder. This search is strictly by best-fit; i.e.,
2318 the smallest (with ties going to approximately the least
2319 recently used) chunk that fits is selected.
2321 4. If large enough, the chunk bordering the end of memory
2322 (`top') is split off. (This use of `top' is in accord with
2323 the best-fit search rule. In effect, `top' is treated as
2324 larger (and thus less well fitting) than any other available
2325 chunk since it can be extended to be as large as necessary
2326 (up to system limitations).
2328 5. If the request size meets the mmap threshold and the
2329 system supports mmap, and there are few enough currently
2330 allocated mmapped regions, and a call to mmap succeeds,
2331 the request is allocated via direct memory mapping.
2333 6. Otherwise, the top of memory is extended by
2334 obtaining more space from the system (normally using sbrk,
2335 but definable to anything else via the MORECORE macro).
2336 Memory is gathered from the system (in system page-sized
2337 units) in a way that allows chunks obtained across different
2338 sbrk calls to be consolidated, but does not require
2339 contiguous memory. Thus, it should be safe to intersperse
2340 mallocs with other sbrk calls.
2343 All allocations are made from the the `lowest' part of any found
2344 chunk. (The implementation invariant is that prev_inuse is
2345 always true of any allocated chunk; i.e., that each allocated
2346 chunk borders either a previously allocated and still in-use chunk,
2347 or the base of its memory arena.)
2351 #if __STD_C
2352 Void_t* mALLOc(size_t bytes)
2353 #else
2354 Void_t* mALLOc(bytes) size_t bytes;
2355 #endif
2357 arena *ar_ptr;
2358 INTERNAL_SIZE_T nb; /* padded request size */
2359 mchunkptr victim;
2361 #if defined(_LIBC) || defined(MALLOC_HOOKS)
2362 if (__malloc_hook != NULL) {
2363 Void_t* result;
2365 result = (*__malloc_hook)(bytes);
2366 return result;
2368 #endif
2370 nb = request2size(bytes);
2371 arena_get(ar_ptr, nb + top_pad);
2372 if(!ar_ptr)
2373 return 0;
2374 victim = chunk_alloc(ar_ptr, nb);
2375 (void)mutex_unlock(&ar_ptr->mutex);
2376 return victim ? chunk2mem(victim) : 0;
2379 static mchunkptr
2380 #if __STD_C
2381 chunk_alloc(arena *ar_ptr, INTERNAL_SIZE_T nb)
2382 #else
2383 chunk_alloc(ar_ptr, nb) arena *ar_ptr; INTERNAL_SIZE_T nb;
2384 #endif
2386 mchunkptr victim; /* inspected/selected chunk */
2387 INTERNAL_SIZE_T victim_size; /* its size */
2388 int idx; /* index for bin traversal */
2389 mbinptr bin; /* associated bin */
2390 mchunkptr remainder; /* remainder from a split */
2391 long remainder_size; /* its size */
2392 int remainder_index; /* its bin index */
2393 unsigned long block; /* block traverser bit */
2394 int startidx; /* first bin of a traversed block */
2395 mchunkptr fwd; /* misc temp for linking */
2396 mchunkptr bck; /* misc temp for linking */
2397 mbinptr q; /* misc temp */
2400 /* Check for exact match in a bin */
2402 if (is_small_request(nb)) /* Faster version for small requests */
2404 idx = smallbin_index(nb);
2406 /* No traversal or size check necessary for small bins. */
2408 q = bin_at(ar_ptr, idx);
2409 victim = last(q);
2411 /* Also scan the next one, since it would have a remainder < MINSIZE */
2412 if (victim == q)
2414 q = next_bin(q);
2415 victim = last(q);
2417 if (victim != q)
2419 victim_size = chunksize(victim);
2420 unlink(victim, bck, fwd);
2421 set_inuse_bit_at_offset(victim, victim_size);
2422 check_malloced_chunk(ar_ptr, victim, nb);
2423 return victim;
2426 idx += 2; /* Set for bin scan below. We've already scanned 2 bins. */
2429 else
2431 idx = bin_index(nb);
2432 bin = bin_at(ar_ptr, idx);
2434 for (victim = last(bin); victim != bin; victim = victim->bk)
2436 victim_size = chunksize(victim);
2437 remainder_size = victim_size - nb;
2439 if (remainder_size >= (long)MINSIZE) /* too big */
2441 --idx; /* adjust to rescan below after checking last remainder */
2442 break;
2445 else if (remainder_size >= 0) /* exact fit */
2447 unlink(victim, bck, fwd);
2448 set_inuse_bit_at_offset(victim, victim_size);
2449 check_malloced_chunk(ar_ptr, victim, nb);
2450 return victim;
2454 ++idx;
2458 /* Try to use the last split-off remainder */
2460 if ( (victim = last_remainder(ar_ptr)->fd) != last_remainder(ar_ptr))
2462 victim_size = chunksize(victim);
2463 remainder_size = victim_size - nb;
2465 if (remainder_size >= (long)MINSIZE) /* re-split */
2467 remainder = chunk_at_offset(victim, nb);
2468 set_head(victim, nb | PREV_INUSE);
2469 link_last_remainder(ar_ptr, remainder);
2470 set_head(remainder, remainder_size | PREV_INUSE);
2471 set_foot(remainder, remainder_size);
2472 check_malloced_chunk(ar_ptr, victim, nb);
2473 return victim;
2476 clear_last_remainder(ar_ptr);
2478 if (remainder_size >= 0) /* exhaust */
2480 set_inuse_bit_at_offset(victim, victim_size);
2481 check_malloced_chunk(ar_ptr, victim, nb);
2482 return victim;
2485 /* Else place in bin */
2487 frontlink(ar_ptr, victim, victim_size, remainder_index, bck, fwd);
2491 If there are any possibly nonempty big-enough blocks,
2492 search for best fitting chunk by scanning bins in blockwidth units.
2495 if ( (block = idx2binblock(idx)) <= binblocks(ar_ptr))
2498 /* Get to the first marked block */
2500 if ( (block & binblocks(ar_ptr)) == 0)
2502 /* force to an even block boundary */
2503 idx = (idx & ~(BINBLOCKWIDTH - 1)) + BINBLOCKWIDTH;
2504 block <<= 1;
2505 while ((block & binblocks(ar_ptr)) == 0)
2507 idx += BINBLOCKWIDTH;
2508 block <<= 1;
2512 /* For each possibly nonempty block ... */
2513 for (;;)
2515 startidx = idx; /* (track incomplete blocks) */
2516 q = bin = bin_at(ar_ptr, idx);
2518 /* For each bin in this block ... */
2521 /* Find and use first big enough chunk ... */
2523 for (victim = last(bin); victim != bin; victim = victim->bk)
2525 victim_size = chunksize(victim);
2526 remainder_size = victim_size - nb;
2528 if (remainder_size >= (long)MINSIZE) /* split */
2530 remainder = chunk_at_offset(victim, nb);
2531 set_head(victim, nb | PREV_INUSE);
2532 unlink(victim, bck, fwd);
2533 link_last_remainder(ar_ptr, remainder);
2534 set_head(remainder, remainder_size | PREV_INUSE);
2535 set_foot(remainder, remainder_size);
2536 check_malloced_chunk(ar_ptr, victim, nb);
2537 return victim;
2540 else if (remainder_size >= 0) /* take */
2542 set_inuse_bit_at_offset(victim, victim_size);
2543 unlink(victim, bck, fwd);
2544 check_malloced_chunk(ar_ptr, victim, nb);
2545 return victim;
2550 bin = next_bin(bin);
2552 } while ((++idx & (BINBLOCKWIDTH - 1)) != 0);
2554 /* Clear out the block bit. */
2556 do /* Possibly backtrack to try to clear a partial block */
2558 if ((startidx & (BINBLOCKWIDTH - 1)) == 0)
2560 binblocks(ar_ptr) &= ~block;
2561 break;
2563 --startidx;
2564 q = prev_bin(q);
2565 } while (first(q) == q);
2567 /* Get to the next possibly nonempty block */
2569 if ( (block <<= 1) <= binblocks(ar_ptr) && (block != 0) )
2571 while ((block & binblocks(ar_ptr)) == 0)
2573 idx += BINBLOCKWIDTH;
2574 block <<= 1;
2577 else
2578 break;
2583 /* Try to use top chunk */
2585 /* Require that there be a remainder, ensuring top always exists */
2586 if ( (remainder_size = chunksize(top(ar_ptr)) - nb) < (long)MINSIZE)
2589 #if HAVE_MMAP
2590 /* If big and would otherwise need to extend, try to use mmap instead */
2591 if ((unsigned long)nb >= (unsigned long)mmap_threshold &&
2592 (victim = mmap_chunk(nb)) != 0)
2593 return victim;
2594 #endif
2596 /* Try to extend */
2597 malloc_extend_top(ar_ptr, nb);
2598 if ((remainder_size = chunksize(top(ar_ptr)) - nb) < (long)MINSIZE)
2599 return 0; /* propagate failure */
2602 victim = top(ar_ptr);
2603 set_head(victim, nb | PREV_INUSE);
2604 top(ar_ptr) = chunk_at_offset(victim, nb);
2605 set_head(top(ar_ptr), remainder_size | PREV_INUSE);
2606 check_malloced_chunk(ar_ptr, victim, nb);
2607 return victim;
2616 free() algorithm :
2618 cases:
2620 1. free(0) has no effect.
2622 2. If the chunk was allocated via mmap, it is released via munmap().
2624 3. If a returned chunk borders the current high end of memory,
2625 it is consolidated into the top, and if the total unused
2626 topmost memory exceeds the trim threshold, malloc_trim is
2627 called.
2629 4. Other chunks are consolidated as they arrive, and
2630 placed in corresponding bins. (This includes the case of
2631 consolidating with the current `last_remainder').
2636 #if __STD_C
2637 void fREe(Void_t* mem)
2638 #else
2639 void fREe(mem) Void_t* mem;
2640 #endif
2642 arena *ar_ptr;
2643 mchunkptr p; /* chunk corresponding to mem */
2645 #if defined(_LIBC) || defined(MALLOC_HOOKS)
2646 if (__free_hook != NULL) {
2647 (*__free_hook)(mem);
2648 return;
2650 #endif
2652 if (mem == 0) /* free(0) has no effect */
2653 return;
2655 p = mem2chunk(mem);
2657 #if HAVE_MMAP
2658 if (chunk_is_mmapped(p)) /* release mmapped memory. */
2660 munmap_chunk(p);
2661 return;
2663 #endif
2665 ar_ptr = arena_for_ptr(p);
2666 #if THREAD_STATS
2667 if(!mutex_trylock(&ar_ptr->mutex))
2668 ++(ar_ptr->stat_lock_direct);
2669 else {
2670 (void)mutex_lock(&ar_ptr->mutex);
2671 ++(ar_ptr->stat_lock_wait);
2673 #else
2674 (void)mutex_lock(&ar_ptr->mutex);
2675 #endif
2676 chunk_free(ar_ptr, p);
2677 (void)mutex_unlock(&ar_ptr->mutex);
2680 static void
2681 #if __STD_C
2682 chunk_free(arena *ar_ptr, mchunkptr p)
2683 #else
2684 chunk_free(ar_ptr, p) arena *ar_ptr; mchunkptr p;
2685 #endif
2687 INTERNAL_SIZE_T hd = p->size; /* its head field */
2688 INTERNAL_SIZE_T sz; /* its size */
2689 int idx; /* its bin index */
2690 mchunkptr next; /* next contiguous chunk */
2691 INTERNAL_SIZE_T nextsz; /* its size */
2692 INTERNAL_SIZE_T prevsz; /* size of previous contiguous chunk */
2693 mchunkptr bck; /* misc temp for linking */
2694 mchunkptr fwd; /* misc temp for linking */
2695 int islr; /* track whether merging with last_remainder */
2697 check_inuse_chunk(ar_ptr, p);
2699 sz = hd & ~PREV_INUSE;
2700 next = chunk_at_offset(p, sz);
2701 nextsz = chunksize(next);
2703 if (next == top(ar_ptr)) /* merge with top */
2705 sz += nextsz;
2707 if (!(hd & PREV_INUSE)) /* consolidate backward */
2709 prevsz = p->prev_size;
2710 p = chunk_at_offset(p, -prevsz);
2711 sz += prevsz;
2712 unlink(p, bck, fwd);
2715 set_head(p, sz | PREV_INUSE);
2716 top(ar_ptr) = p;
2718 #ifndef NO_THREADS
2719 if(ar_ptr == &main_arena) {
2720 #endif
2721 if ((unsigned long)(sz) >= (unsigned long)trim_threshold)
2722 main_trim(top_pad);
2723 #ifndef NO_THREADS
2724 } else {
2725 heap_info *heap = heap_for_ptr(p);
2727 assert(heap->ar_ptr == ar_ptr);
2729 /* Try to get rid of completely empty heaps, if possible. */
2730 if((unsigned long)(sz) >= (unsigned long)trim_threshold ||
2731 p == chunk_at_offset(heap, sizeof(*heap)))
2732 heap_trim(heap, top_pad);
2734 #endif
2735 return;
2738 set_head(next, nextsz); /* clear inuse bit */
2740 islr = 0;
2742 if (!(hd & PREV_INUSE)) /* consolidate backward */
2744 prevsz = p->prev_size;
2745 p = chunk_at_offset(p, -prevsz);
2746 sz += prevsz;
2748 if (p->fd == last_remainder(ar_ptr)) /* keep as last_remainder */
2749 islr = 1;
2750 else
2751 unlink(p, bck, fwd);
2754 if (!(inuse_bit_at_offset(next, nextsz))) /* consolidate forward */
2756 sz += nextsz;
2758 if (!islr && next->fd == last_remainder(ar_ptr))
2759 /* re-insert last_remainder */
2761 islr = 1;
2762 link_last_remainder(ar_ptr, p);
2764 else
2765 unlink(next, bck, fwd);
2768 set_head(p, sz | PREV_INUSE);
2769 set_foot(p, sz);
2770 if (!islr)
2771 frontlink(ar_ptr, p, sz, idx, bck, fwd);
2780 Realloc algorithm:
2782 Chunks that were obtained via mmap cannot be extended or shrunk
2783 unless HAVE_MREMAP is defined, in which case mremap is used.
2784 Otherwise, if their reallocation is for additional space, they are
2785 copied. If for less, they are just left alone.
2787 Otherwise, if the reallocation is for additional space, and the
2788 chunk can be extended, it is, else a malloc-copy-free sequence is
2789 taken. There are several different ways that a chunk could be
2790 extended. All are tried:
2792 * Extending forward into following adjacent free chunk.
2793 * Shifting backwards, joining preceding adjacent space
2794 * Both shifting backwards and extending forward.
2795 * Extending into newly sbrked space
2797 Unless the #define REALLOC_ZERO_BYTES_FREES is set, realloc with a
2798 size argument of zero (re)allocates a minimum-sized chunk.
2800 If the reallocation is for less space, and the new request is for
2801 a `small' (<512 bytes) size, then the newly unused space is lopped
2802 off and freed.
2804 The old unix realloc convention of allowing the last-free'd chunk
2805 to be used as an argument to realloc is no longer supported.
2806 I don't know of any programs still relying on this feature,
2807 and allowing it would also allow too many other incorrect
2808 usages of realloc to be sensible.
2814 #if __STD_C
2815 Void_t* rEALLOc(Void_t* oldmem, size_t bytes)
2816 #else
2817 Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
2818 #endif
2820 arena *ar_ptr;
2821 INTERNAL_SIZE_T nb; /* padded request size */
2823 mchunkptr oldp; /* chunk corresponding to oldmem */
2824 INTERNAL_SIZE_T oldsize; /* its size */
2826 mchunkptr newp; /* chunk to return */
2828 #if defined(_LIBC) || defined(MALLOC_HOOKS)
2829 if (__realloc_hook != NULL) {
2830 Void_t* result;
2832 result = (*__realloc_hook)(oldmem, bytes);
2833 return result;
2835 #endif
2837 #ifdef REALLOC_ZERO_BYTES_FREES
2838 if (bytes == 0) { fREe(oldmem); return 0; }
2839 #endif
2841 /* realloc of null is supposed to be same as malloc */
2842 if (oldmem == 0) return mALLOc(bytes);
2844 oldp = mem2chunk(oldmem);
2845 oldsize = chunksize(oldp);
2847 nb = request2size(bytes);
2849 #if HAVE_MMAP
2850 if (chunk_is_mmapped(oldp))
2852 Void_t* newmem;
2854 #if HAVE_MREMAP
2855 newp = mremap_chunk(oldp, nb);
2856 if(newp) return chunk2mem(newp);
2857 #endif
2858 /* Note the extra SIZE_SZ overhead. */
2859 if(oldsize - SIZE_SZ >= nb) return oldmem; /* do nothing */
2860 /* Must alloc, copy, free. */
2861 newmem = mALLOc(bytes);
2862 if (newmem == 0) return 0; /* propagate failure */
2863 MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
2864 munmap_chunk(oldp);
2865 return newmem;
2867 #endif
2869 ar_ptr = arena_for_ptr(oldp);
2870 #if THREAD_STATS
2871 if(!mutex_trylock(&ar_ptr->mutex))
2872 ++(ar_ptr->stat_lock_direct);
2873 else {
2874 (void)mutex_lock(&ar_ptr->mutex);
2875 ++(ar_ptr->stat_lock_wait);
2877 #else
2878 (void)mutex_lock(&ar_ptr->mutex);
2879 #endif
2881 /* As in malloc(), remember this arena for the next allocation. */
2882 tsd_setspecific(arena_key, (Void_t *)ar_ptr);
2884 newp = chunk_realloc(ar_ptr, oldp, oldsize, nb);
2886 (void)mutex_unlock(&ar_ptr->mutex);
2887 return newp ? chunk2mem(newp) : NULL;
2890 static mchunkptr
2891 #if __STD_C
2892 chunk_realloc(arena* ar_ptr, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
2893 INTERNAL_SIZE_T nb)
2894 #else
2895 chunk_realloc(ar_ptr, oldp, oldsize, nb)
2896 arena* ar_ptr; mchunkptr oldp; INTERNAL_SIZE_T oldsize, nb;
2897 #endif
2899 mchunkptr newp = oldp; /* chunk to return */
2900 INTERNAL_SIZE_T newsize = oldsize; /* its size */
2902 mchunkptr next; /* next contiguous chunk after oldp */
2903 INTERNAL_SIZE_T nextsize; /* its size */
2905 mchunkptr prev; /* previous contiguous chunk before oldp */
2906 INTERNAL_SIZE_T prevsize; /* its size */
2908 mchunkptr remainder; /* holds split off extra space from newp */
2909 INTERNAL_SIZE_T remainder_size; /* its size */
2911 mchunkptr bck; /* misc temp for linking */
2912 mchunkptr fwd; /* misc temp for linking */
2914 check_inuse_chunk(ar_ptr, oldp);
2916 if ((long)(oldsize) < (long)(nb))
2919 /* Try expanding forward */
2921 next = chunk_at_offset(oldp, oldsize);
2922 if (next == top(ar_ptr) || !inuse(next))
2924 nextsize = chunksize(next);
2926 /* Forward into top only if a remainder */
2927 if (next == top(ar_ptr))
2929 if ((long)(nextsize + newsize) >= (long)(nb + MINSIZE))
2931 newsize += nextsize;
2932 top(ar_ptr) = chunk_at_offset(oldp, nb);
2933 set_head(top(ar_ptr), (newsize - nb) | PREV_INUSE);
2934 set_head_size(oldp, nb);
2935 return oldp;
2939 /* Forward into next chunk */
2940 else if (((long)(nextsize + newsize) >= (long)(nb)))
2942 unlink(next, bck, fwd);
2943 newsize += nextsize;
2944 goto split;
2947 else
2949 next = 0;
2950 nextsize = 0;
2953 /* Try shifting backwards. */
2955 if (!prev_inuse(oldp))
2957 prev = prev_chunk(oldp);
2958 prevsize = chunksize(prev);
2960 /* try forward + backward first to save a later consolidation */
2962 if (next != 0)
2964 /* into top */
2965 if (next == top(ar_ptr))
2967 if ((long)(nextsize + prevsize + newsize) >= (long)(nb + MINSIZE))
2969 unlink(prev, bck, fwd);
2970 newp = prev;
2971 newsize += prevsize + nextsize;
2972 MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);
2973 top(ar_ptr) = chunk_at_offset(newp, nb);
2974 set_head(top(ar_ptr), (newsize - nb) | PREV_INUSE);
2975 set_head_size(newp, nb);
2976 return newp;
2980 /* into next chunk */
2981 else if (((long)(nextsize + prevsize + newsize) >= (long)(nb)))
2983 unlink(next, bck, fwd);
2984 unlink(prev, bck, fwd);
2985 newp = prev;
2986 newsize += nextsize + prevsize;
2987 MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);
2988 goto split;
2992 /* backward only */
2993 if (prev != 0 && (long)(prevsize + newsize) >= (long)nb)
2995 unlink(prev, bck, fwd);
2996 newp = prev;
2997 newsize += prevsize;
2998 MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);
2999 goto split;
3003 /* Must allocate */
3005 newp = chunk_alloc (ar_ptr, nb);
3007 if (newp == 0) /* propagate failure */
3008 return 0;
3010 /* Avoid copy if newp is next chunk after oldp. */
3011 /* (This can only happen when new chunk is sbrk'ed.) */
3013 if ( newp == next_chunk(oldp))
3015 newsize += chunksize(newp);
3016 newp = oldp;
3017 goto split;
3020 /* Otherwise copy, free, and exit */
3021 MALLOC_COPY(chunk2mem(newp), chunk2mem(oldp), oldsize - SIZE_SZ);
3022 chunk_free(ar_ptr, oldp);
3023 return newp;
3027 split: /* split off extra room in old or expanded chunk */
3029 if (newsize - nb >= MINSIZE) /* split off remainder */
3031 remainder = chunk_at_offset(newp, nb);
3032 remainder_size = newsize - nb;
3033 set_head_size(newp, nb);
3034 set_head(remainder, remainder_size | PREV_INUSE);
3035 set_inuse_bit_at_offset(remainder, remainder_size);
3036 chunk_free(ar_ptr, remainder);
3038 else
3040 set_head_size(newp, newsize);
3041 set_inuse_bit_at_offset(newp, newsize);
3044 check_inuse_chunk(ar_ptr, newp);
3045 return newp;
3053 memalign algorithm:
3055 memalign requests more than enough space from malloc, finds a spot
3056 within that chunk that meets the alignment request, and then
3057 possibly frees the leading and trailing space.
3059 The alignment argument must be a power of two. This property is not
3060 checked by memalign, so misuse may result in random runtime errors.
3062 8-byte alignment is guaranteed by normal malloc calls, so don't
3063 bother calling memalign with an argument of 8 or less.
3065 Overreliance on memalign is a sure way to fragment space.
3070 #if __STD_C
3071 Void_t* mEMALIGn(size_t alignment, size_t bytes)
3072 #else
3073 Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
3074 #endif
3076 arena *ar_ptr;
3077 INTERNAL_SIZE_T nb; /* padded request size */
3078 mchunkptr p;
3080 #if defined(_LIBC) || defined(MALLOC_HOOKS)
3081 if (__memalign_hook != NULL) {
3082 Void_t* result;
3084 result = (*__memalign_hook)(alignment, bytes);
3085 return result;
3087 #endif
3089 /* If need less alignment than we give anyway, just relay to malloc */
3091 if (alignment <= MALLOC_ALIGNMENT) return mALLOc(bytes);
3093 /* Otherwise, ensure that it is at least a minimum chunk size */
3095 if (alignment < MINSIZE) alignment = MINSIZE;
3097 nb = request2size(bytes);
3098 arena_get(ar_ptr, nb + alignment + MINSIZE);
3099 if(!ar_ptr)
3100 return 0;
3101 p = chunk_align(ar_ptr, nb, alignment);
3102 (void)mutex_unlock(&ar_ptr->mutex);
3103 return p ? chunk2mem(p) : NULL;
3106 static mchunkptr
3107 #if __STD_C
3108 chunk_align(arena* ar_ptr, INTERNAL_SIZE_T nb, size_t alignment)
3109 #else
3110 chunk_align(ar_ptr, nb, alignment)
3111 arena* ar_ptr; INTERNAL_SIZE_T nb; size_t alignment;
3112 #endif
3114 char* m; /* memory returned by malloc call */
3115 mchunkptr p; /* corresponding chunk */
3116 char* brk; /* alignment point within p */
3117 mchunkptr newp; /* chunk to return */
3118 INTERNAL_SIZE_T newsize; /* its size */
3119 INTERNAL_SIZE_T leadsize; /* leading space befor alignment point */
3120 mchunkptr remainder; /* spare room at end to split off */
3121 long remainder_size; /* its size */
3123 /* Call chunk_alloc with worst case padding to hit alignment. */
3124 p = chunk_alloc(ar_ptr, nb + alignment + MINSIZE);
3125 if (p == 0)
3126 return 0; /* propagate failure */
3128 m = chunk2mem(p);
3130 if ((((unsigned long)(m)) % alignment) == 0) /* aligned */
3132 #if HAVE_MMAP
3133 if(chunk_is_mmapped(p)) {
3134 return p; /* nothing more to do */
3136 #endif
3138 else /* misaligned */
3141 Find an aligned spot inside chunk.
3142 Since we need to give back leading space in a chunk of at
3143 least MINSIZE, if the first calculation places us at
3144 a spot with less than MINSIZE leader, we can move to the
3145 next aligned spot -- we've allocated enough total room so that
3146 this is always possible.
3149 brk = (char*)mem2chunk(((unsigned long)(m + alignment - 1)) & -alignment);
3150 if ((long)(brk - (char*)(p)) < (long)MINSIZE) brk += alignment;
3152 newp = (mchunkptr)brk;
3153 leadsize = brk - (char*)(p);
3154 newsize = chunksize(p) - leadsize;
3156 #if HAVE_MMAP
3157 if(chunk_is_mmapped(p))
3159 newp->prev_size = p->prev_size + leadsize;
3160 set_head(newp, newsize|IS_MMAPPED);
3161 return newp;
3163 #endif
3165 /* give back leader, use the rest */
3167 set_head(newp, newsize | PREV_INUSE);
3168 set_inuse_bit_at_offset(newp, newsize);
3169 set_head_size(p, leadsize);
3170 chunk_free(ar_ptr, p);
3171 p = newp;
3173 assert (newsize>=nb && (((unsigned long)(chunk2mem(p))) % alignment) == 0);
3176 /* Also give back spare room at the end */
3178 remainder_size = chunksize(p) - nb;
3180 if (remainder_size >= (long)MINSIZE)
3182 remainder = chunk_at_offset(p, nb);
3183 set_head(remainder, remainder_size | PREV_INUSE);
3184 set_head_size(p, nb);
3185 chunk_free(ar_ptr, remainder);
3188 check_inuse_chunk(ar_ptr, p);
3189 return p;
3196 valloc just invokes memalign with alignment argument equal
3197 to the page size of the system (or as near to this as can
3198 be figured out from all the includes/defines above.)
3201 #if __STD_C
3202 Void_t* vALLOc(size_t bytes)
3203 #else
3204 Void_t* vALLOc(bytes) size_t bytes;
3205 #endif
3207 return mEMALIGn (malloc_getpagesize, bytes);
3211 pvalloc just invokes valloc for the nearest pagesize
3212 that will accommodate request
3216 #if __STD_C
3217 Void_t* pvALLOc(size_t bytes)
3218 #else
3219 Void_t* pvALLOc(bytes) size_t bytes;
3220 #endif
3222 size_t pagesize = malloc_getpagesize;
3223 return mEMALIGn (pagesize, (bytes + pagesize - 1) & ~(pagesize - 1));
3228 calloc calls chunk_alloc, then zeroes out the allocated chunk.
3232 #if __STD_C
3233 Void_t* cALLOc(size_t n, size_t elem_size)
3234 #else
3235 Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size;
3236 #endif
3238 arena *ar_ptr;
3239 mchunkptr p, oldtop;
3240 INTERNAL_SIZE_T sz, csz, oldtopsize;
3241 Void_t* mem;
3243 #if defined(_LIBC) || defined(MALLOC_HOOKS)
3244 if (__malloc_hook != NULL) {
3245 sz = n * elem_size;
3246 mem = (*__malloc_hook)(sz);
3247 #ifdef HAVE_MEMCPY
3248 memset(mem, 0, sz);
3249 #else
3250 while(sz > 0) mem[--sz] = 0; /* rather inefficient */
3251 #endif
3252 return mem;
3254 #endif
3256 sz = request2size(n * elem_size);
3257 arena_get(ar_ptr, sz);
3258 if(!ar_ptr)
3259 return 0;
3261 /* check if expand_top called, in which case don't need to clear */
3262 #if MORECORE_CLEARS
3263 oldtop = top(ar_ptr);
3264 oldtopsize = chunksize(top(ar_ptr));
3265 #endif
3266 p = chunk_alloc (ar_ptr, sz);
3268 /* Only clearing follows, so we can unlock early. */
3269 (void)mutex_unlock(&ar_ptr->mutex);
3271 if (p == 0)
3272 return 0;
3273 else
3275 mem = chunk2mem(p);
3277 /* Two optional cases in which clearing not necessary */
3279 #if HAVE_MMAP
3280 if (chunk_is_mmapped(p)) return mem;
3281 #endif
3283 csz = chunksize(p);
3285 #if MORECORE_CLEARS
3286 if (p == oldtop && csz > oldtopsize)
3288 /* clear only the bytes from non-freshly-sbrked memory */
3289 csz = oldtopsize;
3291 #endif
3293 MALLOC_ZERO(mem, csz - SIZE_SZ);
3294 return mem;
3300 cfree just calls free. It is needed/defined on some systems
3301 that pair it with calloc, presumably for odd historical reasons.
3305 #if !defined(_LIBC)
3306 #if __STD_C
3307 void cfree(Void_t *mem)
3308 #else
3309 void cfree(mem) Void_t *mem;
3310 #endif
3312 free(mem);
3314 #endif
3320 Malloc_trim gives memory back to the system (via negative
3321 arguments to sbrk) if there is unused memory at the `high' end of
3322 the malloc pool. You can call this after freeing large blocks of
3323 memory to potentially reduce the system-level memory requirements
3324 of a program. However, it cannot guarantee to reduce memory. Under
3325 some allocation patterns, some large free blocks of memory will be
3326 locked between two used chunks, so they cannot be given back to
3327 the system.
3329 The `pad' argument to malloc_trim represents the amount of free
3330 trailing space to leave untrimmed. If this argument is zero,
3331 only the minimum amount of memory to maintain internal data
3332 structures will be left (one page or less). Non-zero arguments
3333 can be supplied to maintain enough trailing space to service
3334 future expected allocations without having to re-obtain memory
3335 from the system.
3337 Malloc_trim returns 1 if it actually released any memory, else 0.
3341 #if __STD_C
3342 int malloc_trim(size_t pad)
3343 #else
3344 int malloc_trim(pad) size_t pad;
3345 #endif
3347 int res;
3349 (void)mutex_lock(&main_arena.mutex);
3350 res = main_trim(pad);
3351 (void)mutex_unlock(&main_arena.mutex);
3352 return res;
3355 /* Trim the main arena. */
3357 static int
3358 #if __STD_C
3359 main_trim(size_t pad)
3360 #else
3361 main_trim(pad) size_t pad;
3362 #endif
3364 mchunkptr top_chunk; /* The current top chunk */
3365 long top_size; /* Amount of top-most memory */
3366 long extra; /* Amount to release */
3367 char* current_brk; /* address returned by pre-check sbrk call */
3368 char* new_brk; /* address returned by negative sbrk call */
3370 unsigned long pagesz = malloc_getpagesize;
3372 top_chunk = top(&main_arena);
3373 top_size = chunksize(top_chunk);
3374 extra = ((top_size - pad - MINSIZE + (pagesz-1)) / pagesz - 1) * pagesz;
3376 if (extra < (long)pagesz) /* Not enough memory to release */
3377 return 0;
3379 /* Test to make sure no one else called sbrk */
3380 current_brk = (char*)(MORECORE (0));
3381 if (current_brk != (char*)(top_chunk) + top_size)
3382 return 0; /* Apparently we don't own memory; must fail */
3384 new_brk = (char*)(MORECORE (-extra));
3386 if (new_brk == (char*)(MORECORE_FAILURE)) { /* sbrk failed? */
3387 /* Try to figure out what we have */
3388 current_brk = (char*)(MORECORE (0));
3389 top_size = current_brk - (char*)top_chunk;
3390 if (top_size >= (long)MINSIZE) /* if not, we are very very dead! */
3392 sbrked_mem = current_brk - sbrk_base;
3393 set_head(top_chunk, top_size | PREV_INUSE);
3395 check_chunk(&main_arena, top_chunk);
3396 return 0;
3398 sbrked_mem -= extra;
3400 /* Success. Adjust top accordingly. */
3401 set_head(top_chunk, (top_size - extra) | PREV_INUSE);
3402 check_chunk(&main_arena, top_chunk);
3403 return 1;
3406 #ifndef NO_THREADS
3408 static int
3409 #if __STD_C
3410 heap_trim(heap_info *heap, size_t pad)
3411 #else
3412 heap_trim(heap, pad) heap_info *heap; size_t pad;
3413 #endif
3415 unsigned long pagesz = malloc_getpagesize;
3416 arena *ar_ptr = heap->ar_ptr;
3417 mchunkptr top_chunk = top(ar_ptr), p, bck, fwd;
3418 heap_info *prev_heap;
3419 long new_size, top_size, extra;
3421 /* Can this heap go away completely ? */
3422 while(top_chunk == chunk_at_offset(heap, sizeof(*heap))) {
3423 prev_heap = heap->prev;
3424 p = chunk_at_offset(prev_heap, prev_heap->size - (MINSIZE-2*SIZE_SZ));
3425 assert(p->size == (0|PREV_INUSE)); /* must be fencepost */
3426 p = prev_chunk(p);
3427 new_size = chunksize(p) + (MINSIZE-2*SIZE_SZ);
3428 assert(new_size>0 && new_size<(long)(2*MINSIZE));
3429 if(!prev_inuse(p))
3430 new_size += p->prev_size;
3431 assert(new_size>0 && new_size<HEAP_MAX_SIZE);
3432 if(new_size + (HEAP_MAX_SIZE - prev_heap->size) < pad + MINSIZE + pagesz)
3433 break;
3434 ar_ptr->size -= heap->size;
3435 delete_heap(heap);
3436 heap = prev_heap;
3437 if(!prev_inuse(p)) { /* consolidate backward */
3438 p = prev_chunk(p);
3439 unlink(p, bck, fwd);
3441 assert(((unsigned long)((char*)p + new_size) & (pagesz-1)) == 0);
3442 assert( ((char*)p + new_size) == ((char*)heap + heap->size) );
3443 top(ar_ptr) = top_chunk = p;
3444 set_head(top_chunk, new_size | PREV_INUSE);
3445 check_chunk(ar_ptr, top_chunk);
3447 top_size = chunksize(top_chunk);
3448 extra = ((top_size - pad - MINSIZE + (pagesz-1))/pagesz - 1) * pagesz;
3449 if(extra < (long)pagesz)
3450 return 0;
3451 /* Try to shrink. */
3452 if(grow_heap(heap, -extra) != 0)
3453 return 0;
3454 ar_ptr->size -= extra;
3456 /* Success. Adjust top accordingly. */
3457 set_head(top_chunk, (top_size - extra) | PREV_INUSE);
3458 check_chunk(ar_ptr, top_chunk);
3459 return 1;
3462 #endif
3467 malloc_usable_size:
3469 This routine tells you how many bytes you can actually use in an
3470 allocated chunk, which may be more than you requested (although
3471 often not). You can use this many bytes without worrying about
3472 overwriting other allocated objects. Not a particularly great
3473 programming practice, but still sometimes useful.
3477 #if __STD_C
3478 size_t malloc_usable_size(Void_t* mem)
3479 #else
3480 size_t malloc_usable_size(mem) Void_t* mem;
3481 #endif
3483 mchunkptr p;
3485 if (mem == 0)
3486 return 0;
3487 else
3489 p = mem2chunk(mem);
3490 if(!chunk_is_mmapped(p))
3492 if (!inuse(p)) return 0;
3493 check_inuse_chunk(arena_for_ptr(mem), p);
3494 return chunksize(p) - SIZE_SZ;
3496 return chunksize(p) - 2*SIZE_SZ;
3503 /* Utility to update mallinfo for malloc_stats() and mallinfo() */
3505 static void
3506 #if __STD_C
3507 malloc_update_mallinfo(arena *ar_ptr, struct mallinfo *mi)
3508 #else
3509 malloc_update_mallinfo(ar_ptr, mi) arena *ar_ptr; struct mallinfo *mi;
3510 #endif
3512 int i, navail;
3513 mbinptr b;
3514 mchunkptr p;
3515 #if MALLOC_DEBUG
3516 mchunkptr q;
3517 #endif
3518 INTERNAL_SIZE_T avail;
3520 (void)mutex_lock(&ar_ptr->mutex);
3521 avail = chunksize(top(ar_ptr));
3522 navail = ((long)(avail) >= (long)MINSIZE)? 1 : 0;
3524 for (i = 1; i < NAV; ++i)
3526 b = bin_at(ar_ptr, i);
3527 for (p = last(b); p != b; p = p->bk)
3529 #if MALLOC_DEBUG
3530 check_free_chunk(ar_ptr, p);
3531 for (q = next_chunk(p);
3532 q != top(ar_ptr) && inuse(q) && (long)chunksize(q) > 0;
3533 q = next_chunk(q))
3534 check_inuse_chunk(ar_ptr, q);
3535 #endif
3536 avail += chunksize(p);
3537 navail++;
3541 mi->arena = ar_ptr->size;
3542 mi->ordblks = navail;
3543 mi->uordblks = ar_ptr->size - avail;
3544 mi->fordblks = avail;
3545 mi->hblks = n_mmaps;
3546 mi->hblkhd = mmapped_mem;
3547 mi->keepcost = chunksize(top(ar_ptr));
3549 (void)mutex_unlock(&ar_ptr->mutex);
3552 #if !defined(NO_THREADS) && MALLOC_DEBUG > 1
3554 /* Print the complete contents of a single heap to stderr. */
3556 static void
3557 #if __STD_C
3558 dump_heap(heap_info *heap)
3559 #else
3560 dump_heap(heap) heap_info *heap;
3561 #endif
3563 char *ptr;
3564 mchunkptr p;
3566 fprintf(stderr, "Heap %p, size %10lx:\n", heap, (long)heap->size);
3567 ptr = (heap->ar_ptr != (arena*)(heap+1)) ?
3568 (char*)(heap + 1) : (char*)(heap + 1) + sizeof(arena);
3569 p = (mchunkptr)(((unsigned long)ptr + MALLOC_ALIGN_MASK) &
3570 ~MALLOC_ALIGN_MASK);
3571 for(;;) {
3572 fprintf(stderr, "chunk %p size %10lx", p, (long)p->size);
3573 if(p == top(heap->ar_ptr)) {
3574 fprintf(stderr, " (top)\n");
3575 break;
3576 } else if(p->size == (0|PREV_INUSE)) {
3577 fprintf(stderr, " (fence)\n");
3578 break;
3580 fprintf(stderr, "\n");
3581 p = next_chunk(p);
3585 #endif
3591 malloc_stats:
3593 For all arenas separately and in total, prints on stderr the
3594 amount of space obtained from the system, and the current number
3595 of bytes allocated via malloc (or realloc, etc) but not yet
3596 freed. (Note that this is the number of bytes allocated, not the
3597 number requested. It will be larger than the number requested
3598 because of alignment and bookkeeping overhead.) When not compiled
3599 for multiple threads, the maximum amount of allocated memory
3600 (which may be more than current if malloc_trim and/or munmap got
3601 called) is also reported. When using mmap(), prints the maximum
3602 number of simultaneous mmap regions used, too.
3606 void malloc_stats()
3608 int i;
3609 arena *ar_ptr;
3610 struct mallinfo mi;
3611 unsigned int in_use_b = mmapped_mem, system_b = in_use_b;
3612 #if THREAD_STATS
3613 long stat_lock_direct = 0, stat_lock_loop = 0, stat_lock_wait = 0;
3614 #endif
3616 for(i=0, ar_ptr = &main_arena; ar_ptr; ar_ptr = ar_ptr->next, i++) {
3617 malloc_update_mallinfo(ar_ptr, &mi);
3618 fprintf(stderr, "Arena %d:\n", i);
3619 fprintf(stderr, "system bytes = %10u\n", (unsigned int)mi.arena);
3620 fprintf(stderr, "in use bytes = %10u\n", (unsigned int)mi.uordblks);
3621 system_b += mi.arena;
3622 in_use_b += mi.uordblks;
3623 #if THREAD_STATS
3624 stat_lock_direct += ar_ptr->stat_lock_direct;
3625 stat_lock_loop += ar_ptr->stat_lock_loop;
3626 stat_lock_wait += ar_ptr->stat_lock_wait;
3627 #endif
3628 #if !defined(NO_THREADS) && MALLOC_DEBUG > 1
3629 if(ar_ptr != &main_arena) {
3630 heap_info *heap = heap_for_ptr(top(ar_ptr));
3631 while(heap) { dump_heap(heap); heap = heap->prev; }
3633 #endif
3635 fprintf(stderr, "Total (incl. mmap):\n");
3636 fprintf(stderr, "system bytes = %10u\n", system_b);
3637 fprintf(stderr, "in use bytes = %10u\n", in_use_b);
3638 #ifdef NO_THREADS
3639 fprintf(stderr, "max system bytes = %10u\n", (unsigned int)max_total_mem);
3640 #endif
3641 #if HAVE_MMAP
3642 fprintf(stderr, "max mmap regions = %10u\n", (unsigned int)max_n_mmaps);
3643 #endif
3644 #if THREAD_STATS
3645 fprintf(stderr, "heaps created = %10d\n", stat_n_heaps);
3646 fprintf(stderr, "locked directly = %10ld\n", stat_lock_direct);
3647 fprintf(stderr, "locked in loop = %10ld\n", stat_lock_loop);
3648 fprintf(stderr, "locked waiting = %10ld\n", stat_lock_wait);
3649 fprintf(stderr, "locked total = %10ld\n",
3650 stat_lock_direct + stat_lock_loop + stat_lock_wait);
3651 #endif
3655 mallinfo returns a copy of updated current mallinfo.
3656 The information reported is for the arena last used by the thread.
3659 struct mallinfo mALLINFo()
3661 struct mallinfo mi;
3662 Void_t *vptr = NULL;
3664 tsd_getspecific(arena_key, vptr);
3665 malloc_update_mallinfo((vptr ? (arena*)vptr : &main_arena), &mi);
3666 return mi;
3673 mallopt:
3675 mallopt is the general SVID/XPG interface to tunable parameters.
3676 The format is to provide a (parameter-number, parameter-value) pair.
3677 mallopt then sets the corresponding parameter to the argument
3678 value if it can (i.e., so long as the value is meaningful),
3679 and returns 1 if successful else 0.
3681 See descriptions of tunable parameters above.
3685 #if __STD_C
3686 int mALLOPt(int param_number, int value)
3687 #else
3688 int mALLOPt(param_number, value) int param_number; int value;
3689 #endif
3691 switch(param_number)
3693 case M_TRIM_THRESHOLD:
3694 trim_threshold = value; return 1;
3695 case M_TOP_PAD:
3696 top_pad = value; return 1;
3697 case M_MMAP_THRESHOLD:
3698 #ifndef NO_THREADS
3699 /* Forbid setting the threshold too high. */
3700 if((unsigned long)value > HEAP_MAX_SIZE/2) return 0;
3701 #endif
3702 mmap_threshold = value; return 1;
3703 case M_MMAP_MAX:
3704 #if HAVE_MMAP
3705 n_mmaps_max = value; return 1;
3706 #else
3707 if (value != 0) return 0; else n_mmaps_max = value; return 1;
3708 #endif
3709 case M_CHECK_ACTION:
3710 check_action = value; return 1;
3712 default:
3713 return 0;
3717 #ifdef _LIBC
3718 weak_alias (__libc_calloc, __calloc) weak_alias (__libc_calloc, calloc)
3719 weak_alias (__libc_free, __cfree) weak_alias (__libc_free, cfree)
3720 weak_alias (__libc_free, __free) weak_alias (__libc_free, free)
3721 weak_alias (__libc_malloc, __malloc) weak_alias (__libc_malloc, malloc)
3722 weak_alias (__libc_memalign, __memalign) weak_alias (__libc_memalign, memalign)
3723 weak_alias (__libc_realloc, __realloc) weak_alias (__libc_realloc, realloc)
3724 weak_alias (__libc_valloc, __valloc) weak_alias (__libc_valloc, valloc)
3725 weak_alias (__libc_pvalloc, __pvalloc) weak_alias (__libc_pvalloc, pvalloc)
3726 weak_alias (__libc_mallinfo, __mallinfo) weak_alias (__libc_mallinfo, mallinfo)
3727 weak_alias (__libc_mallopt, __mallopt) weak_alias (__libc_mallopt, mallopt)
3729 #undef malloc_stats
3730 weak_alias (__malloc_stats, malloc_stats)
3731 #undef malloc_usable_size
3732 weak_alias (__malloc_usable_size, malloc_usable_size)
3733 #undef malloc_trim
3734 weak_alias (__malloc_trim, malloc_trim)
3735 #endif
3738 #if defined(_LIBC) || defined(MALLOC_HOOKS)
3740 /* A simple, standard set of debugging hooks. Overhead is `only' one
3741 byte per chunk; still this will catch most cases of double frees or
3742 overruns. */
3744 #define MAGICBYTE ((char)0xd7)
3746 /* Convert a pointer to be free()d or realloc()ed to a valid chunk
3747 pointer. If the provided pointer is not valid, return NULL. The
3748 goal here is to avoid crashes, unlike in the MALLOC_DEBUG code. */
3750 static mchunkptr
3751 #if __STD_C
3752 mem2chunk_check(Void_t* mem)
3753 #else
3754 mem2chunk_check(mem) Void_t* mem;
3755 #endif
3757 mchunkptr p;
3758 INTERNAL_SIZE_T sz;
3760 p = mem2chunk(mem);
3761 if(!aligned_OK(p)) return NULL;
3762 if( (char*)p>=sbrk_base && (char*)p<(sbrk_base+sbrked_mem) ) {
3763 /* Must be a chunk in conventional memory. */
3764 if(chunk_is_mmapped(p) ||
3765 ( (sz = chunksize(p)), ((char*)p + sz)>=(sbrk_base+sbrked_mem) ) ||
3766 sz<MINSIZE || sz&MALLOC_ALIGN_MASK || !inuse(p) ) return NULL;
3767 if(*((char*)p + sz + (SIZE_SZ-1)) != MAGICBYTE) return NULL;
3768 *((char*)p + sz + (SIZE_SZ-1)) = 0;
3769 } else {
3770 unsigned long offset, page_mask = malloc_getpagesize-1;
3772 /* mmap()ed chunks have MALLOC_ALIGNMENT or higher power-of two
3773 alignment relative to the beginning of a page. Check this
3774 first. */
3775 offset = (unsigned long)mem & page_mask;
3776 if((offset!=MALLOC_ALIGNMENT && offset!=0 && offset!=0x10 &&
3777 offset!=0x20 && offset!=0x40 && offset!=0x80 && offset!=0x100 &&
3778 offset!=0x200 && offset!=0x400 && offset!=0x800 && offset!=0x1000 &&
3779 offset<0x2000) ||
3780 !chunk_is_mmapped(p) || (p->size & PREV_INUSE) ||
3781 ( (((unsigned long)p - p->prev_size) & page_mask) != 0 ) ||
3782 ( (sz = chunksize(p)), ((p->prev_size + sz) & page_mask) != 0 ) )
3783 return NULL;
3784 if(*((char*)p + sz - 1) != MAGICBYTE) return NULL;
3785 *((char*)p + sz - 1) = 0;
3787 return p;
3790 static Void_t*
3791 #if __STD_C
3792 malloc_check(size_t sz)
3793 #else
3794 malloc_check(sz) size_t sz;
3795 #endif
3797 mchunkptr victim;
3798 INTERNAL_SIZE_T nb = request2size(sz + 1);
3800 (void)mutex_lock(&main_arena.mutex);
3801 victim = chunk_alloc(&main_arena, nb);
3802 (void)mutex_unlock(&main_arena.mutex);
3803 if(!victim) return NULL;
3804 nb = chunksize(victim);
3805 if(chunk_is_mmapped(victim))
3806 --nb;
3807 else
3808 nb += SIZE_SZ - 1;
3809 *((char*)victim + nb) = MAGICBYTE;
3810 return chunk2mem(victim);
3813 static void
3814 #if __STD_C
3815 free_check(Void_t* mem)
3816 #else
3817 free_check(mem) Void_t* mem;
3818 #endif
3820 mchunkptr p;
3822 if(!mem) return;
3823 p = mem2chunk_check(mem);
3824 if(!p) {
3825 switch(check_action) {
3826 case 1:
3827 fprintf(stderr, "free(): invalid pointer %lx!\n", (long)(mem));
3828 break;
3829 case 2:
3830 abort();
3832 return;
3834 #if HAVE_MMAP
3835 if (chunk_is_mmapped(p)) {
3836 munmap_chunk(p);
3837 return;
3839 #endif
3840 (void)mutex_lock(&main_arena.mutex);
3841 chunk_free(&main_arena, p);
3842 (void)mutex_unlock(&main_arena.mutex);
3845 static Void_t*
3846 #if __STD_C
3847 realloc_check(Void_t* oldmem, size_t bytes)
3848 #else
3849 realloc_check(oldmem, bytes) Void_t* oldmem; size_t bytes;
3850 #endif
3852 mchunkptr oldp, newp;
3853 INTERNAL_SIZE_T nb, oldsize;
3855 if (oldmem == 0) return malloc_check(bytes);
3856 oldp = mem2chunk_check(oldmem);
3857 if(!oldp) {
3858 switch(check_action) {
3859 case 1:
3860 fprintf(stderr, "realloc(): invalid pointer %lx!\n", (long)(oldmem));
3861 break;
3862 case 2:
3863 abort();
3865 return malloc_check(bytes);
3867 oldsize = chunksize(oldp);
3869 nb = request2size(bytes+1);
3871 (void)mutex_lock(&main_arena.mutex);
3872 #if HAVE_MMAP
3873 if (chunk_is_mmapped(oldp)) {
3874 #if HAVE_MREMAP
3875 newp = mremap_chunk(oldp, nb);
3876 if(!newp) {
3877 #endif
3878 /* Note the extra SIZE_SZ overhead. */
3879 if(oldsize - SIZE_SZ >= nb) newp = oldp; /* do nothing */
3880 else {
3881 /* Must alloc, copy, free. */
3882 newp = chunk_alloc(&main_arena, nb);
3883 if (newp) {
3884 MALLOC_COPY(chunk2mem(newp), oldmem, oldsize - 2*SIZE_SZ);
3885 munmap_chunk(oldp);
3888 #if HAVE_MREMAP
3890 #endif
3891 } else
3892 #endif /* HAVE_MMAP */
3893 newp = chunk_realloc(&main_arena, oldp, oldsize, nb);
3894 (void)mutex_unlock(&main_arena.mutex);
3896 if(!newp) return NULL;
3897 nb = chunksize(newp);
3898 if(chunk_is_mmapped(newp))
3899 --nb;
3900 else
3901 nb += SIZE_SZ - 1;
3902 *((char*)newp + nb) = MAGICBYTE;
3903 return chunk2mem(newp);
3906 static Void_t*
3907 #if __STD_C
3908 memalign_check(size_t alignment, size_t bytes)
3909 #else
3910 memalign_check(alignment, bytes) size_t alignment; size_t bytes;
3911 #endif
3913 INTERNAL_SIZE_T nb;
3914 mchunkptr p;
3916 if (alignment <= MALLOC_ALIGNMENT) return malloc_check(bytes);
3917 if (alignment < MINSIZE) alignment = MINSIZE;
3919 nb = request2size(bytes+1);
3920 (void)mutex_lock(&main_arena.mutex);
3921 p = chunk_align(&main_arena, nb, alignment);
3922 (void)mutex_unlock(&main_arena.mutex);
3923 if(!p) return NULL;
3924 nb = chunksize(p);
3925 if(chunk_is_mmapped(p))
3926 --nb;
3927 else
3928 nb += SIZE_SZ - 1;
3929 *((char*)p + nb) = MAGICBYTE;
3930 return chunk2mem(p);
3933 #endif /* defined(_LIBC) || defined(MALLOC_HOOKS) */
3937 History:
3939 V2.6.4-pt2 Sat Dec 14 1996 Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
3940 * Added debugging hooks
3941 * Fixed possible deadlock in realloc() when out of memory
3942 * Don't pollute namespace in glibc: use __getpagesize, __mmap, etc.
3944 V2.6.4-pt Wed Dec 4 1996 Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
3945 * Very minor updates from the released 2.6.4 version.
3946 * Trimmed include file down to exported data structures.
3947 * Changes from H.J. Lu for glibc-2.0.
3949 V2.6.3i-pt Sep 16 1996 Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
3950 * Many changes for multiple threads
3951 * Introduced arenas and heaps
3953 V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee)
3954 * Added pvalloc, as recommended by H.J. Liu
3955 * Added 64bit pointer support mainly from Wolfram Gloger
3956 * Added anonymously donated WIN32 sbrk emulation
3957 * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen
3958 * malloc_extend_top: fix mask error that caused wastage after
3959 foreign sbrks
3960 * Add linux mremap support code from HJ Liu
3962 V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee)
3963 * Integrated most documentation with the code.
3964 * Add support for mmap, with help from
3965 Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
3966 * Use last_remainder in more cases.
3967 * Pack bins using idea from colin@nyx10.cs.du.edu
3968 * Use ordered bins instead of best-fit threshold
3969 * Eliminate block-local decls to simplify tracing and debugging.
3970 * Support another case of realloc via move into top
3971 * Fix error occurring when initial sbrk_base not word-aligned.
3972 * Rely on page size for units instead of SBRK_UNIT to
3973 avoid surprises about sbrk alignment conventions.
3974 * Add mallinfo, mallopt. Thanks to Raymond Nijssen
3975 (raymond@es.ele.tue.nl) for the suggestion.
3976 * Add `pad' argument to malloc_trim and top_pad mallopt parameter.
3977 * More precautions for cases where other routines call sbrk,
3978 courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
3979 * Added macros etc., allowing use in linux libc from
3980 H.J. Lu (hjl@gnu.ai.mit.edu)
3981 * Inverted this history list
3983 V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee)
3984 * Re-tuned and fixed to behave more nicely with V2.6.0 changes.
3985 * Removed all preallocation code since under current scheme
3986 the work required to undo bad preallocations exceeds
3987 the work saved in good cases for most test programs.
3988 * No longer use return list or unconsolidated bins since
3989 no scheme using them consistently outperforms those that don't
3990 given above changes.
3991 * Use best fit for very large chunks to prevent some worst-cases.
3992 * Added some support for debugging
3994 V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee)
3995 * Removed footers when chunks are in use. Thanks to
3996 Paul Wilson (wilson@cs.texas.edu) for the suggestion.
3998 V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee)
3999 * Added malloc_trim, with help from Wolfram Gloger
4000 (wmglo@Dent.MED.Uni-Muenchen.DE).
4002 V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g)
4004 V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g)
4005 * realloc: try to expand in both directions
4006 * malloc: swap order of clean-bin strategy;
4007 * realloc: only conditionally expand backwards
4008 * Try not to scavenge used bins
4009 * Use bin counts as a guide to preallocation
4010 * Occasionally bin return list chunks in first scan
4011 * Add a few optimizations from colin@nyx10.cs.du.edu
4013 V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g)
4014 * faster bin computation & slightly different binning
4015 * merged all consolidations to one part of malloc proper
4016 (eliminating old malloc_find_space & malloc_clean_bin)
4017 * Scan 2 returns chunks (not just 1)
4018 * Propagate failure in realloc if malloc returns 0
4019 * Add stuff to allow compilation on non-ANSI compilers
4020 from kpv@research.att.com
4022 V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu)
4023 * removed potential for odd address access in prev_chunk
4024 * removed dependency on getpagesize.h
4025 * misc cosmetics and a bit more internal documentation
4026 * anticosmetics: mangled names in macros to evade debugger strangeness
4027 * tested on sparc, hp-700, dec-mips, rs6000
4028 with gcc & native cc (hp, dec only) allowing
4029 Detlefs & Zorn comparison study (in SIGPLAN Notices.)
4031 Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu)
4032 * Based loosely on libg++-1.2X malloc. (It retains some of the overall
4033 structure of old version, but most details differ.)