YAP 7.1.0
dlmalloc.h
1
2#if USE_DL_MALLOC
3
4#if HAVE_SYS_PARAM_H
5#include <sys/param.h>
6#endif
7
8/* YAP only stuff */
9
10void Yap_initdlmalloc(void);
11void Yap_RestoreDLMalloc(void);
12
13
14/* Synopsis of compile-time options:
15
16 People have reported using previous versions of this malloc on all
17 versions of Unix, sometimes by tweaking some of the defines
18 below. It has been tested most extensively on Solaris and
19 Linux. It is also reported to work on WIN32 platforms.
20 People also report using it in stand-alone embedded systems.
21
22 The implementation is in straight, hand-tuned ANSI C. It is not
23 at all modular. (Sorry!) It uses a lot of macros. To be at all
24 usable, this code should be compiled using an optimizing compiler
25 (for example gcc -O3) that can simplify expressions and control
26 paths. (FAQ: some macros import variables as arguments rather than
27 declare locals because people reported that some debuggers
28 otherwise get confused.)
29
30 OPTION DEFAULT VALUE
31
32 Compilation Environment options:
33
34 __STD_C derived from C compiler defines
35 WIN32 NOT defined
36 HAVE_MEMCPY defined
37 USE_MEMCPY 1 if HAVE_MEMCPY is defined
38 HAVE_MMAP defined as 1
39 MMAP_CLEARS 1
40 HAVE_MREMAP 0 unless linux defined
41 malloc_getpagesize derived from system #includes, or 4096 if not
42 HAVE_USR_INCLUDE_MALLOC_H NOT defined
43 LACKS_UNISTD_H NOT defined unless WIN32
44 LACKS_SYS_PARAM_H NOT defined unless WIN32
45 LACKS_SYS_MMAN_H NOT defined unless WIN32
46 LACKS_FCNTL_H NOT defined
47
48 Changing default word sizes:
49
50 INTERNAL_SIZE_T size_t
51 MALLOC_ALIGNMENT 2 * sizeof(INTERNAL_SIZE_T)
52 PTR_UINT unsigned long
53 CHUNK_SIZE_T unsigned long
54
55 Configuration and functionality options:
56
57 USE_DL_PREFIX NOT defined
58 USE_PUBLIC_MALLOC_WRAPPERS NOT defined
59 USE_MALLOC_LOCK NOT defined
60 DEBUG NOT defined
61 REALLOC_ZERO_BYTES_FREES NOT defined
62 MALLOC_FAILURE_ACTION errno = ENOMEM, if __STD_C defined, else no-op
63 TRIM_FASTBINS 0
64 FIRST_SORTED_BIN_SIZE 512
65
66 Options for customizing MORECORE:
67
68 MORECORE sbrk
69 MORECORE_CONTIGUOUS 1
70 MORECORE_CANNOT_TRIM NOT defined
71 MMAP_AS_MORECORE_SIZE (1024 * 1024)
72
73 Tuning options that are also dynamically changeable via mallopt:
74
75 DEFAULT_MXFAST 64
76 DEFAULT_TRIM_THRESHOLD 256 * 1024
77 DEFAULT_TOP_PAD 0
78 DEFAULT_MMAP_THRESHOLD 256 * 1024
79 DEFAULT_MMAP_MAX 65536
80
81 There are several other #defined constants and macros that you
82 probably don't want to touch unless you are extending or adapting malloc.
83*/
84
85#define MORECORE yapsbrk
86#define MORECORE_CONTIGUOUS 0
87#define USE_DL_PREFIX 1
88
89/*
90 WIN32 sets up defaults for MS environment and compilers.
91 Otherwise defaults are for unix.
92*/
93
94/* #define WIN32 */
95
96
97/*
98 __STD_C should be nonzero if using ANSI-standard C compiler, a C++
99 compiler, or a C compiler sufficiently close to ANSI to get away
100 with it.
101*/
102
103#ifndef __STD_C
104#if defined(__STDC__) || defined(_cplusplus)
105#define __STD_C 1
106#else
107#define __STD_C 0
108#endif
109#endif /*__STD_C*/
110
111
112/*
113 Void_t* is the pointer type that malloc should say it returns
114*/
115
116#ifndef Void_t
117#if (__STD_C || defined(WIN32))
118#define Void_t void
119#else
120#define Void_t char
121#endif
122#endif /*Void_t*/
123
124#if __STD_C
125#include <stddef.h> /* for size_t */
126#else
127#include <sys/types.h>
128#endif
129
130#ifdef __cplusplus
131extern "C" {
132#endif
133
134/* define LACKS_UNISTD_H if your system does not have a <unistd.h>. */
135
136/* #define LACKS_UNISTD_H */
137
138#ifndef LACKS_UNISTD_H
139#include <unistd.h>
140#endif
141
142/* define LACKS_SYS_PARAM_H if your system does not have a <sys/param.h>. */
143
144/* #define LACKS_SYS_PARAM_H */
145
146
147#include <stdio.h> /* needed for malloc_stats */
148#include <errno.h> /* needed for optional MALLOC_FAILURE_ACTION */
149
150
151/*
152 Debugging:
153
154 Because freed chunks may be overwritten with bookkeeping fields, this
155 malloc will often die when freed memory is overwritten by user
156 programs. This can be very effective (albeit in an annoying way)
157 in helping track down dangling pointers.
158
159 If you compile with -DDEBUG, a number of assertion checks are
160 enabled that will catch more memory errors. You probably won't be
161 able to make much sense of the actual assertion errors, but they
162 should help you locate incorrectly overwritten memory. The
163 checking is fairly extensive, and will slow down execution
164 noticeably. Calling malloc_stats or mallinfo with DEBUG set will
165 attempt to check every non-mmapped allocated and free chunk in the
166 course of computing the summmaries. (By nature, mmapped regions
167 cannot be checked very much automatically.)
168
169 Setting DEBUG may also be helpful if you are trying to modify
170 this code. The assertions in the check routines spell out in more
171 detail the assumptions and invariants underlying the algorithms.
172
173 Setting DEBUG does NOT provide an automated mechanism for checking
174 that all accesses to malloced memory stay within their
175 bounds. However, there are several add-ons and adaptations of this
176 or other mallocs available that do this.
177*/
178
179#define DEBUG_DLMALLOC 1
180#if DEBUG_DLMALLOC
181#include <assert.h>
182#else
183#ifndef assert
184#define assert(x) ((void)0)
185#endif
186#endif
187
188/*
189 The unsigned integer type used for comparing any two chunk sizes.
190 This should be at least as wide as size_t, but should not be signed.
191*/
192
193#ifndef CHUNK_SIZE_T
194#define CHUNK_SIZE_T unsigned long
195#endif
196
197/*
198 The unsigned integer type used to hold addresses when they are are
199 manipulated as integers. Except that it is not defined on all
200 systems, intptr_t would suffice.
201*/
202#ifndef PTR_UINT
203#define PTR_UINT unsigned long
204#endif
205
206
207/*
208 INTERNAL_SIZE_T is the word-size used for internal bookkeeping
209 of chunk sizes.
210
211 The default version is the same as size_t.
212
213 While not strictly necessary, it is best to define this as an
214 unsigned type, even if size_t is a signed type. This may avoid some
215 artificial size limitations on some systems.
216
217 On a 64-bit machine, you may be able to reduce malloc overhead by
218 defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
219 expense of not being able to handle more than 2^32 of malloced
220 space. If this limitation is acceptable, you are encouraged to set
221 this unless you are on a platform requiring 16byte alignments. In
222 this case the alignment requirements turn out to negate any
223 potential advantages of decreasing size_t word size.
224
225 Implementors: Beware of the possible combinations of:
226 - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
227 and might be the same width as int or as long
228 - size_t might have different width and signedness as INTERNAL_SIZE_T
229 - int and long might be 32 or 64 bits, and might be the same width
230 To deal with this, most comparisons and difference computations
231 among INTERNAL_SIZE_Ts should cast them to CHUNK_SIZE_T, being
232 aware of the fact that casting an unsigned int to a wider long does
233 not sign-extend. (This also makes checking for negative numbers
234 awkward.) Some of these casts result in harmless compiler warnings
235 on some systems.
236*/
237
238#ifndef INTERNAL_SIZE_T
239#define INTERNAL_SIZE_T size_t
240#endif
241
242/* The corresponding word size */
243#define SIZE_SZ (sizeof(INTERNAL_SIZE_T))
244
245
246
247/*
248 MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks.
249 It must be a power of two at least 2 * SIZE_SZ, even on machines
250 for which smaller alignments would suffice. It may be defined as
251 larger than this though. Note however that code and data structures
252 are optimized for the case of 8-byte alignment.
253*/
254
255
256#ifndef MALLOC_ALIGNMENT
257#define MALLOC_ALIGNMENT (2 * SIZE_SZ)
258#endif
259
260/* The corresponding bit mask value */
261#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
262
263
264
265/*
266 REALLOC_ZERO_BYTES_FREES should be set if a call to
267 realloc with zero bytes should be the same as a call to free.
268 Some people think it should. Otherwise, since this malloc
269 returns a unique pointer for malloc(0), so does realloc(p, 0).
270*/
271
272/* #define REALLOC_ZERO_BYTES_FREES */
273
274/*
275 TRIM_FASTBINS controls whether free() of a very small chunk can
276 immediately lead to trimming. Setting to true (1) can reduce memory
277 footprint, but will almost always slow down programs that use a lot
278 of small chunks.
279
280 Define this only if you are willing to give up some speed to more
281 aggressively reduce system-level memory footprint when releasing
282 memory in programs that use many small chunks. You can get
283 essentially the same effect by setting MXFAST to 0, but this can
284 lead to even greater slowdowns in programs using many small chunks.
285 TRIM_FASTBINS is an in-between compile-time option, that disables
286 only those chunks bordering topmost memory from being placed in
287 fastbins.
288*/
289
290#ifndef TRIM_FASTBINS
291#define TRIM_FASTBINS 0
292#endif
293
294
295/*
296 USE_DL_PREFIX will prefix all public routines with the string 'dl'.
297 This is necessary when you only want to use this malloc in one part
298 of a program, using your regular system malloc elsewhere.
299*/
300
301/* #define USE_DL_PREFIX */
302
303
304
305/*
306 Two-phase name translation.
307 All of the actual routines are given mangled names.
308 When wrappers are used, they become the public callable versions.
309 When DL_PREFIX is used, the callable names are prefixed.
310*/
311
312#define cALLOc Yap_dlcalloc
313#define fREe Yap_dlfree
314#define cFREe Yap_dlcfree
315#define mALLOc Yap_dlmalloc
316#define mEMALIGn Yap_dlmemalign
317#define rEALLOc Yap_dlrealloc
318#define vALLOc Yap_dlvalloc
319#define pVALLOc Yap_dlpvalloc
320#define mALLINFo Yap_dlmallinfo
321#define mALLOPt Yap_dlmallopt
322#define mTRIm Yap_dlmalloc_trim
323#define mSTATs Yap_dlmalloc_stats
324#define mUSABLe Yap_dlmalloc_usable_size
325#define iCALLOc Yap_dlindependent_calloc
326#define iCOMALLOc Yap_dlindependent_comalloc
327
328/*
329 MALLOC_FAILURE_ACTION is the action to take before "return 0" when
330 malloc fails to be able to return memory, either because memory is
331 exhausted or because of illegal arguments.
332
333 By default, sets errno if running on STD_C platform, else does nothing.
334*/
335
336#ifndef MALLOC_FAILURE_ACTION
337#if __STD_C
338#define MALLOC_FAILURE_ACTION \
339 errno = ENOMEM;
340
341#else
342#define MALLOC_FAILURE_ACTION
343#endif
344#endif
345
346/*
347 MORECORE-related declarations. By default, rely on sbrk
348*/
349
350
351#ifdef LACKS_UNISTD_H
352#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
353#if __STD_C
354extern Void_t* sbrk(ptrdiff_t);
355#else
356extern Void_t* sbrk();
357#endif
358#endif
359#endif
360
361/*
362 MORECORE is the name of the routine to call to obtain more memory
363 from the system. See below for general guidance on writing
364 alternative MORECORE functions, as well as a version for WIN32 and a
365 sample version for pre-OSX macos.
366*/
367
368#ifndef MORECORE
369#define MORECORE sbrk
370#endif
371
372/*
373 MORECORE_FAILURE is the value returned upon failure of MORECORE
374 as well as mmap. Since it cannot be an otherwise valid memory address,
375 and must reflect values of standard sys calls, you probably ought not
376 try to redefine it.
377*/
378
379#ifndef MORECORE_FAILURE
380#define MORECORE_FAILURE (-1)
381#endif
382
383/*
384 If MORECORE_CONTIGUOUS is true, take advantage of fact that
385 consecutive calls to MORECORE with positive arguments always return
386 contiguous increasing addresses. This is true of unix sbrk. Even
387 if not defined, when regions happen to be contiguous, malloc will
388 permit allocations spanning regions obtained from different
389 calls. But defining this when applicable enables some stronger
390 consistency checks and space efficiencies.
391*/
392
393#ifndef MORECORE_CONTIGUOUS
394#define MORECORE_CONTIGUOUS 1
395#endif
396
397/*
398 Define MORECORE_CANNOT_TRIM if your version of MORECORE
399 cannot release space back to the system when given negative
400 arguments. This is generally necessary only if you are using
401 a hand-crafted MORECORE function that cannot handle negative arguments.
402*/
403
404/* #define MORECORE_CANNOT_TRIM */
405
406
407/*
408 The system page size. To the extent possible, this malloc manages
409 memory from the system in page-size units. Note that this value is
410 cached during initialization into a field of malloc_state. So even
411 if malloc_getpagesize is a function, it is only called once.
412
413 The following mechanics for getpagesize were adapted from bsd/gnu
414 getpagesize.h. If none of the system-probes here apply, a value of
415 4096 is used, which should be OK: If they don't apply, then using
416 the actual value probably doesn't impact performance.
417*/
418
419#define malloc_getpagesize Yap_page_size
420
421#ifndef malloc_getpagesize
422
423#ifndef LACKS_UNISTD_H
424# include <unistd.h>
425#endif
426
427# ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */
428# ifndef _SC_PAGE_SIZE
429# define _SC_PAGE_SIZE _SC_PAGESIZE
430# endif
431# endif
432
433# ifdef _SC_PAGE_SIZE
434# define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
435# else
436# if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
437 extern size_t getpagesize();
438# define malloc_getpagesize getpagesize()
439# else
440# ifdef WIN32 /* use supplied emulation of getpagesize */
441# define malloc_getpagesize getpagesize()
442# else
443# ifndef LACKS_SYS_PARAM_H
444# include <sys/param.h>
445# endif
446# ifdef EXEC_PAGESIZE
447# define malloc_getpagesize EXEC_PAGESIZE
448# else
449# ifdef NBPG
450# ifndef CLSIZE
451# define malloc_getpagesize NBPG
452# else
453# define malloc_getpagesize (NBPG * CLSIZE)
454# endif
455# else
456# ifdef NBPC
457# define malloc_getpagesize NBPC
458# else
459# ifdef PAGESIZE
460# define malloc_getpagesize PAGESIZE
461# else /* just guess */
462# define malloc_getpagesize (4096)
463# endif
464# endif
465# endif
466# endif
467# endif
468# endif
469# endif
470#endif
471
472/*
473 This version of malloc supports the standard SVID/XPG mallinfo
474 routine that returns a struct containing usage properties and
475 statistics. It should work on any SVID/XPG compliant system that has
476 a /usr/include/malloc.h defining struct mallinfo. (If you'd like to
477 install such a thing yourself, cut out the preliminary declarations
478 as described above and below and save them in a malloc.h file. But
479 there's no compelling reason to bother to do this.)
480
481 The main declaration needed is the mallinfo struct that is returned
482 (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a
483 bunch of fields that are not even meaningful in this version of
484 malloc. These fields are are instead filled by mallinfo() with
485 other numbers that might be of interest.
486
487 HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
488 /usr/include/malloc.h file that includes a declaration of struct
489 mallinfo. If so, it is included; else an SVID2/XPG2 compliant
490 version is declared below. These must be precisely the same for
491 mallinfo() to work. The original SVID version of this struct,
492 defined on most systems with mallinfo, declares all fields as
493 ints. But some others define as unsigned long. If your system
494 defines the fields using a type of different width than listed here,
495 you must #include your system version and #define
496 HAVE_USR_INCLUDE_MALLOC_H.
497*/
498
499#if HAVE_MALLOC_H && !defined(_WIN32) && !defined(__NetBSD_Version__)
500#define HAVE_USR_INCLUDE_MALLOC_H 1
501#endif
502
503#ifdef HAVE_USR_INCLUDE_MALLOC_H
504#include <malloc.h>
505#else
506
507/* SVID2/XPG mallinfo structure */
508
509struct mallinfo {
510 int arena; /* non-mmapped space allocated from system */
511 int ordblks; /* number of free chunks */
512 int smblks; /* number of fastbin blocks */
513 int hblks; /* number of mmapped regions */
514 int hblkhd; /* space in mmapped regions */
515 int usmblks; /* maximum total allocated space */
516 int fsmblks; /* space available in freed fastbin blocks */
517 int uordblks; /* total allocated space */
518 int fordblks; /* total free space */
519 int keepcost; /* top-most, releasable (via malloc_trim) space */
520};
521
522/*
523 SVID/XPG defines four standard parameter numbers for mallopt,
524 normally defined in malloc.h. Only one of these (M_MXFAST) is used
525 in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
526 so setting them has no effect. But this malloc also supports other
527 options in mallopt described below.
528*/
529#endif
530
531
532/* ---------- description of public routines ------------ */
533
534/*
535 malloc(size_t n)
536 Returns a pointer to a newly allocated chunk of at least n bytes, or null
537 if no space is available. Additionally, on failure, errno is
538 set to ENOMEM on ANSI C systems.
539
540 If n is zero, malloc returns a minumum-sized chunk. (The minimum
541 size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
542 systems.) On most systems, size_t is an unsigned type, so calls
543 with negative arguments are interpreted as requests for huge amounts
544 of space, which will often fail. The maximum supported value of n
545 differs across systems, but is in all cases less than the maximum
546 representable value of a size_t.
547*/
548#if __STD_C
549Void_t* mALLOc(size_t);
550#else
551Void_t* mALLOc();
552#endif
553
554/*
555 free(Void_t* p)
556 Releases the chunk of memory pointed to by p, that had been previously
557 allocated using malloc or a related routine such as realloc.
558 It has no effect if p is null. It can have arbitrary (i.e., bad!)
559 effects if p has already been freed.
560
561 Unless disabled (using mallopt), freeing very large spaces will
562 when possible, automatically trigger operations that give
563 back unused memory to the system, thus reducing program footprint.
564*/
565#if __STD_C
566void fREe(Void_t*);
567#else
568void fREe();
569#endif
570
571/*
572 calloc(size_t n_elements, size_t element_size);
573 Returns a pointer to n_elements * element_size bytes, with all locations
574 set to zero.
575*/
576#if __STD_C
577Void_t* cALLOc(size_t, size_t);
578#else
579Void_t* cALLOc();
580#endif
581
582/*
583 realloc(Void_t* p, size_t n)
584 Returns a pointer to a chunk of size n that contains the same data
585 as does chunk p up to the minimum of (n, p's size) bytes, or null
586 if no space is available.
587
588 The returned pointer may or may not be the same as p. The algorithm
589 prefers extending p when possible, otherwise it employs the
590 equivalent of a malloc-copy-free sequence.
591
592 If p is null, realloc is equivalent to malloc.
593
594 If space is not available, realloc returns null, errno is set (if on
595 ANSI) and p is NOT freed.
596
597 if n is for fewer bytes than already held by p, the newly unused
598 space is lopped off and freed if possible. Unless the #define
599 REALLOC_ZERO_BYTES_FREES is set, realloc with a size argument of
600 zero (re)allocates a minimum-sized chunk.
601
602 Large chunks that were internally obtained via mmap will always
603 be reallocated using malloc-copy-free sequences unless
604 the system supports MREMAP (currently only linux).
605
606 The old unix realloc convention of allowing the last-free'd chunk
607 to be used as an argument to realloc is not supported.
608*/
609#if __STD_C
610Void_t* rEALLOc(Void_t*, size_t);
611#else
612Void_t* rEALLOc();
613#endif
614
615/*
616 memalign(size_t alignment, size_t n);
617 Returns a pointer to a newly allocated chunk of n bytes, aligned
618 in accord with the alignment argument.
619
620 The alignment argument should be a power of two. If the argument is
621 not a power of two, the nearest greater power is used.
622 8-byte alignment is guaranteed by normal malloc calls, so don't
623 bother calling memalign with an argument of 8 or less.
624
625 Overreliance on memalign is a sure way to fragment space.
626*/
627#if __STD_C
628Void_t* mEMALIGn(size_t, size_t);
629#else
630Void_t* mEMALIGn();
631#endif
632
633/*
634 valloc(size_t n);
635 Equivalent to memalign(pagesize, n), where pagesize is the page
636 size of the system. If the pagesize is unknown, 4096 is used.
637*/
638#if __STD_C
639Void_t* vALLOc(size_t);
640#else
641Void_t* vALLOc();
642#endif
643
644
645
646/*
647 mallopt(int parameter_number, int parameter_value)
648 Sets tunable parameters The format is to provide a
649 (parameter-number, parameter-value) pair. mallopt then sets the
650 corresponding parameter to the argument value if it can (i.e., so
651 long as the value is meaningful), and returns 1 if successful else
652 0. SVID/XPG/ANSI defines four standard param numbers for mallopt,
653 normally defined in malloc.h. Only one of these (M_MXFAST) is used
654 in this malloc. The others (M_NLBLKS, M_GRAIN, M_KEEP) don't apply,
655 so setting them has no effect. But this malloc also supports four
656 other options in mallopt. See below for details. Briefly, supported
657 parameters are as follows (listed defaults are for "typical"
658 configurations).
659
660 Symbol param # default allowed param values
661 M_MXFAST 1 64 0-80 (0 disables fastbins)
662 M_TRIM_THRESHOLD -1 256*1024 any (-1U disables trimming)
663 M_TOP_PAD -2 0 any
664 M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support)
665 M_MMAP_MAX -4 65536 any (0 disables use of mmap)
666*/
667#if __STD_C
668int mALLOPt(int, int);
669#else
670int mALLOPt();
671#endif
672
673
674/*
675 mallinfo()
676 Returns (by copy) a struct containing various summary statistics:
677
678 arena: current total non-mmapped bytes allocated from system
679 ordblks: the number of free chunks
680 smblks: the number of fastbin blocks (i.e., small chunks that
681 have been freed but not use resused or consolidated)
682 hblks: current number of mmapped regions
683 hblkhd: total bytes held in mmapped regions
684 usmblks: the maximum total allocated space. This will be greater
685 than current total if trimming has occurred.
686 fsmblks: total bytes held in fastbin blocks
687 uordblks: current total allocated space (normal or mmapped)
688 fordblks: total free space
689 keepcost: the maximum number of bytes that could ideally be released
690 back to system via malloc_trim. ("ideally" means that
691 it ignores page restrictions etc.)
692
693 Because these fields are ints, but internal bookkeeping may
694 be kept as longs, the reported values may wrap around zero and
695 thus be inaccurate.
696*/
697#if __STD_C
698struct mallinfo mALLINFo(void);
699#else
700struct mallinfo mALLINFo();
701#endif
702
703/*
704 independent_calloc(size_t n_elements, size_t element_size, Void_t* chunks[]);
705
706 independent_calloc is similar to calloc, but instead of returning a
707 single cleared space, it returns an array of pointers to n_elements
708 independent elements that can hold contents of size elem_size, each
709 of which starts out cleared, and can be independently freed,
710 realloc'ed etc. The elements are guaranteed to be adjacently
711 allocated (this is not guaranteed to occur with multiple callocs or
712 mallocs), which may also improve cache locality in some
713 applications.
714
715 The "chunks" argument is optional (i.e., may be null, which is
716 probably the most typical usage). If it is null, the returned array
717 is itself dynamically allocated and should also be freed when it is
718 no longer needed. Otherwise, the chunks array must be of at least
719 n_elements in length. It is filled in with the pointers to the
720 chunks.
721
722 In either case, independent_calloc returns this pointer array, or
723 null if the allocation failed. If n_elements is zero and "chunks"
724 is null, it returns a chunk representing an array with zero elements
725 (which should be freed if not wanted).
726
727 Each element must be individually freed when it is no longer
728 needed. If you'd like to instead be able to free all at once, you
729 should instead use regular calloc and assign pointers into this
730 space to represent elements. (In this case though, you cannot
731 independently free elements.)
732
733 independent_calloc simplifies and speeds up implementations of many
734 kinds of pools. It may also be useful when constructing large data
735 structures that initially have a fixed number of fixed-sized nodes,
736 but the number is not known at compile time, and some of the nodes
737 may later need to be freed. For example:
738
739 struct Node { int item; struct Node* next; };
740
741 struct Node* build_list() {
742 struct Node** pool;
743 int n = read_number_of_nodes_needed();
744 if (n <= 0) return 0;
745 pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
746 if (pool == 0) die();
747 // organize into a linked list...
748 struct Node* first = pool[0];
749 for (i = 0; i < n-1; ++i)
750 pool[i]->next = pool[i+1];
751 free(pool); // Can now free the array (or not, if it is needed later)
752 return first;
753 }
754*/
755#if __STD_C
756Void_t** iCALLOc(size_t, size_t, Void_t**);
757#else
758Void_t** iCALLOc();
759#endif
760
761/*
762 independent_comalloc(size_t n_elements, size_t sizes[], Void_t* chunks[]);
763
764 independent_comalloc allocates, all at once, a set of n_elements
765 chunks with sizes indicated in the "sizes" array. It returns
766 an array of pointers to these elements, each of which can be
767 independently freed, realloc'ed etc. The elements are guaranteed to
768 be adjacently allocated (this is not guaranteed to occur with
769 multiple callocs or mallocs), which may also improve cache locality
770 in some applications.
771
772 The "chunks" argument is optional (i.e., may be null). If it is null
773 the returned array is itself dynamically allocated and should also
774 be freed when it is no longer needed. Otherwise, the chunks array
775 must be of at least n_elements in length. It is filled in with the
776 pointers to the chunks.
777
778 In either case, independent_comalloc returns this pointer array, or
779 null if the allocation failed. If n_elements is zero and chunks is
780 null, it returns a chunk representing an array with zero elements
781 (which should be freed if not wanted).
782
783 Each element must be individually freed when it is no longer
784 needed. If you'd like to instead be able to free all at once, you
785 should instead use a single regular malloc, and assign pointers at
786 particular offsets in the aggregate space. (In this case though, you
787 cannot independently free elements.)
788
789 independent_comallac differs from independent_calloc in that each
790 element may have a different size, and also that it does not
791 automatically clear elements.
792
793 independent_comalloc can be used to speed up allocation in cases
794 where several structs or objects must always be allocated at the
795 same time. For example:
796
797 struct Head { ... }
798 struct Foot { ... }
799
800 void send_message(char* msg) {
801 int msglen = strlen(msg);
802 size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
803 void* chunks[3];
804 if (independent_comalloc(3, sizes, chunks) == 0)
805 die();
806 struct Head* head = (struct Head*)(chunks[0]);
807 char* body = (char*)(chunks[1]);
808 struct Foot* foot = (struct Foot*)(chunks[2]);
809 // ...
810 }
811
812 In general though, independent_comalloc is worth using only for
813 larger values of n_elements. For small values, you probably won't
814 detect enough difference from series of malloc calls to bother.
815
816 Overuse of independent_comalloc can increase overall memory usage,
817 since it cannot reuse existing noncontiguous small chunks that
818 might be available for some of the elements.
819*/
820#if __STD_C
821Void_t** iCOMALLOc(size_t, size_t*, Void_t**);
822#else
823Void_t** iCOMALLOc();
824#endif
825
826
827/*
828 pvalloc(size_t n);
829 Equivalent to valloc(minimum-page-that-holds(n)), that is,
830 round up n to nearest pagesize.
831 */
832#if __STD_C
833Void_t* pVALLOc(size_t);
834#else
835Void_t* pVALLOc();
836#endif
837
838/*
839 cfree(Void_t* p);
840 Equivalent to free(p).
841
842 cfree is needed/defined on some systems that pair it with calloc,
843 for odd historical reasons (such as: cfree is used in example
844 code in the first edition of K&R).
845*/
846#if __STD_C
847void cFREe(Void_t*);
848#else
849void cFREe();
850#endif
851
852/*
853 malloc_trim(size_t pad);
854
855 If possible, gives memory back to the system (via negative
856 arguments to sbrk) if there is unused memory at the `high' end of
857 the malloc pool. You can call this after freeing large blocks of
858 memory to potentially reduce the system-level memory requirements
859 of a program. However, it cannot guarantee to reduce memory. Under
860 some allocation patterns, some large free blocks of memory will be
861 locked between two used chunks, so they cannot be given back to
862 the system.
863
864 The `pad' argument to malloc_trim represents the amount of free
865 trailing space to leave untrimmed. If this argument is zero,
866 only the minimum amount of memory to maintain internal data
867 structures will be left (one page or less). Non-zero arguments
868 can be supplied to maintain enough trailing space to service
869 future expected allocations without having to re-obtain memory
870 from the system.
871
872 Malloc_trim returns 1 if it actually released any memory, else 0.
873 On systems that do not support "negative sbrks", it will always
874 rreturn 0.
875*/
876#if __STD_C
877int mTRIm(size_t);
878#else
879int mTRIm();
880#endif
881
882/*
883 malloc_usable_size(Void_t* p);
884
885 Returns the number of bytes you can actually use in
886 an allocated chunk, which may be more than you requested (although
887 often not) due to alignment and minimum size constraints.
888 You can use this many bytes without worrying about
889 overwriting other allocated objects. This is not a particularly great
890 programming practice. malloc_usable_size can be more useful in
891 debugging and assertions, for example:
892
893 p = malloc(n);
894 assert(malloc_usable_size(p) >= 256);
895
896*/
897#if __STD_C
898size_t mUSABLe(Void_t*);
899#else
900size_t mUSABLe();
901#endif
902
903/*
904 malloc_stats();
905 Prints on stderr the amount of space obtained from the system (both
906 via sbrk and mmap), the maximum amount (which may be more than
907 current if malloc_trim and/or munmap got called), and the current
908 number of bytes allocated via malloc (or realloc, etc) but not yet
909 freed. Note that this is the number of bytes allocated, not the
910 number requested. It will be larger than the number requested
911 because of alignment and bookkeeping overhead. Because it includes
912 alignment wastage as being in use, this figure may be greater than
913 zero even when no user-level chunks are allocated.
914
915 The reported current and maximum system memory can be inaccurate if
916 a program makes other calls to system memory allocation functions
917 (normally sbrk) outside of malloc.
918
919 malloc_stats prints only the most commonly interesting statistics.
920 More information can be obtained by calling mallinfo.
921
922*/
923#if __STD_C
924void mSTATs(void);
925#else
926void mSTATs();
927#endif
928
929
930/* mallopt tuning options */
931
932/*
933 M_MXFAST is the maximum request size used for "fastbins", special bins
934 that hold returned chunks without consolidating their spaces. This
935 enables future requests for chunks of the same size to be handled
936 very quickly, but can increase fragmentation, and thus increase the
937 overall memory footprint of a program.
938
939 This malloc manages fastbins very conservatively yet still
940 efficiently, so fragmentation is rarely a problem for values less
941 than or equal to the default. The maximum supported value of MXFAST
942 is 80. You wouldn't want it any higher than this anyway. Fastbins
943 are designed especially for use with many small structs, objects or
944 strings -- the default handles structs/objects/arrays with sizes up
945 to 16 4byte fields, or small strings representing words, tokens,
946 etc. Using fastbins for larger objects normally worsens
947 fragmentation without improving speed.
948
949 M_MXFAST is set in REQUEST size units. It is internally used in
950 chunksize units, which adds padding and alignment. You can reduce
951 M_MXFAST to 0 to disable all use of fastbins. This causes the malloc
952 algorithm to be a closer approximation of fifo-best-fit in all cases,
953 not just for larger requests, but will generally cause it to be
954 slower.
955*/
956
957
958/* M_MXFAST is a standard SVID/XPG tuning option, usually listed in malloc.h */
959#ifndef M_MXFAST
960#define M_MXFAST 1
961#endif
962
963#ifndef DEFAULT_MXFAST
964#define DEFAULT_MXFAST 64
965#endif
966
967
968/*
969 M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
970 to keep before releasing via malloc_trim in free().
971
972 Automatic trimming is mainly useful in long-lived programs.
973 Because trimming via sbrk can be slow on some systems, and can
974 sometimes be wasteful (in cases where programs immediately
975 afterward allocate more large chunks) the value should be high
976 enough so that your overall system performance would improve by
977 releasing this much memory.
978
979 The trim threshold and the mmap control parameters (see below)
980 can be traded off with one another. Trimming and mmapping are
981 two different ways of releasing unused memory back to the
982 system. Between these two, it is often possible to keep
983 system-level demands of a long-lived program down to a bare
984 minimum. For example, in one test suite of sessions measuring
985 the XF86 X server on Linux, using a trim threshold of 128K and a
986 mmap threshold of 192K led to near-minimal long term resource
987 consumption.
988
989 If you are using this malloc in a long-lived program, it should
990 pay to experiment with these values. As a rough guide, you
991 might set to a value close to the average size of a process
992 (program) running on your system. Releasing this much memory
993 would allow such a process to run in memory. Generally, it's
994 worth it to tune for trimming rather tham memory mapping when a
995 program undergoes phases where several large chunks are
996 allocated and released in ways that can reuse each other's
997 storage, perhaps mixed with phases where there are no such
998 chunks at all. And in well-behaved long-lived programs,
999 controlling release of large blocks via trimming versus mapping
1000 is usually faster.
1001
1002 However, in most programs, these parameters serve mainly as
1003 protection against the system-level effects of carrying around
1004 massive amounts of unneeded memory. Since frequent calls to
1005 sbrk, mmap, and munmap otherwise degrade performance, the default
1006 parameters are set to relatively high values that serve only as
1007 safeguards.
1008
1009 The trim value must be greater than page size to have any useful
1010 effect. To disable trimming completely, you can set to
1011 (unsigned long)(-1)
1012
1013 Trim settings interact with fastbin (MXFAST) settings: Unless
1014 TRIM_FASTBINS is defined, automatic trimming never takes place upon
1015 freeing a chunk with size less than or equal to MXFAST. Trimming is
1016 instead delayed until subsequent freeing of larger chunks. However,
1017 you can still force an attempted trim by calling malloc_trim.
1018
1019 Also, trimming is not generally possible in cases where
1020 the main arena is obtained via mmap.
1021
1022 Note that the trick some people use of mallocing a huge space and
1023 then freeing it at program startup, in an attempt to reserve system
1024 memory, doesn't have the intended effect under automatic trimming,
1025 since that memory will immediately be returned to the system.
1026*/
1027
1028#define M_TRIM_THRESHOLD -1
1029
1030#ifndef DEFAULT_TRIM_THRESHOLD
1031#define DEFAULT_TRIM_THRESHOLD (256 * 1024)
1032#endif
1033
1034/*
1035 M_TOP_PAD is the amount of extra `padding' space to allocate or
1036 retain whenever sbrk is called. It is used in two ways internally:
1037
1038 * When sbrk is called to extend the top of the arena to satisfy
1039 a new malloc request, this much padding is added to the sbrk
1040 request.
1041
1042 * When malloc_trim is called automatically from free(),
1043 it is used as the `pad' argument.
1044
1045 In both cases, the actual amount of padding is rounded
1046 so that the end of the arena is always a system page boundary.
1047
1048 The main reason for using padding is to avoid calling sbrk so
1049 often. Having even a small pad greatly reduces the likelihood
1050 that nearly every malloc request during program start-up (or
1051 after trimming) will invoke sbrk, which needlessly wastes
1052 time.
1053
1054 Automatic rounding-up to page-size units is normally sufficient
1055 to avoid measurable overhead, so the default is 0. However, in
1056 systems where sbrk is relatively slow, it can pay to increase
1057 this value, at the expense of carrying around more memory than
1058 the program needs.
1059*/
1060
1061#define M_TOP_PAD -2
1062
1063#ifndef DEFAULT_TOP_PAD
1064#define DEFAULT_TOP_PAD (0)
1065#endif
1066
1067#ifdef __cplusplus
1068}; /* end of extern "C" */
1069#endif
1070
1071/*
1072 ----------------------- Chunk representations -----------------------
1073*/
1074
1075
1076/*
1077 This struct declaration is misleading (but accurate and necessary).
1078 It declares a "view" into memory allowing access to necessary
1079 fields at known offsets from a given base. See explanation below.
1080*/
1081
1082struct malloc_chunk {
1083
1084 INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
1085 INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
1086
1087 struct malloc_chunk* fd; /* double links -- used only if free. */
1088 struct malloc_chunk* bk;
1089};
1090
1091
1092typedef struct malloc_chunk* mchunkptr;
1093
1094/*
1095 malloc_chunk details:
1096
1097 (The following includes lightly edited explanations by Colin Plumb.)
1098
1099 Chunks of memory are maintained using a `boundary tag' method as
1100 described in e.g., Knuth or Standish. (See the paper by Paul
1101 Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a
1102 survey of such techniques.) Sizes of free chunks are stored both
1103 in the front of each chunk and at the end. This makes
1104 consolidating fragmented chunks into bigger chunks very fast. The
1105 size fields also hold bits representing whether chunks are free or
1106 in use.
1107
1108 An allocated chunk looks like this:
1109
1110
1111 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1112 | Size of previous chunk, if allocated | |
1113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1114 | Size of chunk, in bytes |P|
1115 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1116 | User data starts here... .
1117 . .
1118 . (malloc_usable_space() bytes) .
1119 . |
1120nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1121 | Size of chunk |
1122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1123
1124
1125 Where "chunk" is the front of the chunk for the purpose of most of
1126 the malloc code, but "mem" is the pointer that is returned to the
1127 user. "Nextchunk" is the beginning of the next contiguous chunk.
1128
1129 Chunks always begin on even word boundries, so the mem portion
1130 (which is returned to the user) is also on an even word boundary, and
1131 thus at least double-word aligned.
1132
1133 Free chunks are stored in circular doubly-linked lists, and look like this:
1134
1135 chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1136 | Size of previous chunk |
1137 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1138 `head:' | Size of chunk, in bytes |P|
1139 mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1140 | Forward pointer to next chunk in list |
1141 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1142 | Back pointer to previous chunk in list |
1143 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1144 | Unused space (may be 0 bytes long) .
1145 . .
1146 . |
1147nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1148 `foot:' | Size of chunk, in bytes |
1149 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1150
1151 The P (PREV_INUSE) bit, stored in the unused low-order bit of the
1152 chunk size (which is always a multiple of two words), is an in-use
1153 bit for the *previous* chunk. If that bit is *clear*, then the
1154 word before the current chunk size contains the previous chunk
1155 size, and can be used to find the front of the previous chunk.
1156 The very first chunk allocated always has this bit set,
1157 preventing access to non-existent (or non-owned) memory. If
1158 prev_inuse is set for any given chunk, then you CANNOT determine
1159 the size of the previous chunk, and might even get a memory
1160 addressing fault when trying to do so.
1161
1162 Note that the `foot' of the current chunk is actually represented
1163 as the prev_size of the NEXT chunk. This makes it easier to
1164 deal with alignments etc but can be very confusing when trying
1165 to extend or adapt this code.
1166
1167 The two exceptions to all this are
1168
1169 1. The special chunk `top' doesn't bother using the
1170 trailing size field since there is no next contiguous chunk
1171 that would have to index off it. After initialization, `top'
1172 is forced to always exist. If it would become less than
1173 MINSIZE bytes long, it is replenished.
1174
1175 2. Chunks allocated via mmap, which have the second-lowest-order
1176 bit (IS_MMAPPED) set in their size fields. Because they are
1177 allocated one-by-one, each must contain its own trailing size field.
1178
1179*/
1180
1181/*
1182 ---------- Size and alignment checks and conversions ----------
1183*/
1184
1185/* conversion from malloc headers to user pointers, and back */
1186
1187#define chunk2mem(p) ((Void_t*)((char*)(p) + 2*SIZE_SZ))
1188#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))
1189
1190/* The smallest possible chunk */
1191#define MIN_CHUNK_SIZE (sizeof(struct malloc_chunk))
1192
1193/* The smallest size we can malloc is an aligned minimal chunk */
1194
1195#define MINSIZE \
1196 (CHUNK_SIZE_T)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
1197
1198/* Check if m has acceptable alignment */
1199
1200#define aligned_OK(m) (((PTR_UINT)((m)) & (MALLOC_ALIGN_MASK)) == 0)
1201
1202
1203/*
1204 Check if a request is so large that it would wrap around zero when
1205 padded and aligned. To simplify some other code, the bound is made
1206 low enough so that adding MINSIZE will also not wrap around sero.
1207*/
1208
1209#define REQUEST_OUT_OF_RANGE(req) \
1210 ((CHUNK_SIZE_T)(req) >= \
1211 (CHUNK_SIZE_T)(INTERNAL_SIZE_T)(-2 * MINSIZE))
1212
1213/* pad request bytes into a usable size -- internal version */
1214
1215#define request2size(req) \
1216 (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE) ? \
1217 MINSIZE : \
1218 ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
1219
1220/* Same, except also perform argument check */
1221
1222#define checked_request2size(req, sz) \
1223 if (REQUEST_OUT_OF_RANGE(req)) { \
1224 MALLOC_FAILURE_ACTION; \
1225 return 0; \
1226 } \
1227 (sz) = request2size(req);
1228
1229/*
1230 --------------- Physical chunk operations ---------------
1231*/
1232
1233
1234/* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */
1235#define PREV_INUSE 0x1
1236
1237/* extract inuse bit of previous chunk */
1238#define prev_inuse(p) ((p)->size & PREV_INUSE)
1239
1240
1241/* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */
1242#define IS_MMAPPED 0x2
1243
1244/* check for mmap()'ed chunk */
1245#define chunk_is_mmapped(p) ((p)->size & IS_MMAPPED)
1246
1247/*
1248 Bits to mask off when extracting size
1249
1250 Note: IS_MMAPPED is intentionally not masked off from size field in
1251 macros for which mmapped chunks should never be seen. This should
1252 cause helpful core dumps to occur if it is tried by accident by
1253 people extending or adapting this malloc.
1254*/
1255#define SIZE_BITS (PREV_INUSE|IS_MMAPPED)
1256
1257/* Get size, ignoring use bits */
1258#define chunksize(p) ((p)->size & ~(SIZE_BITS))
1259
1260
1261/* Ptr to next physical malloc_chunk. */
1262#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))
1263
1264/* Ptr to previous physical malloc_chunk */
1265#define prev_chunk(p) ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))
1266
1267/* Treat space at ptr + offset as a chunk */
1268#define chunk_at_offset(p, s) ((mchunkptr)(((char*)(p)) + (s)))
1269
1270/* extract p's inuse bit */
1271#define inuse(p)\
1272((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)
1273
1274/* set/clear chunk as being inuse without otherwise disturbing */
1275#define set_inuse(p)\
1276((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE
1277
1278#define clear_inuse(p)\
1279((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)
1280
1281
1282/* check/set/clear inuse bits in known places */
1283#define inuse_bit_at_offset(p, s)\
1284 (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)
1285
1286#define set_inuse_bit_at_offset(p, s)\
1287 (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)
1288
1289#define clear_inuse_bit_at_offset(p, s)\
1290 (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))
1291
1292
1293/* Set size at head, without disturbing its use bit */
1294#define set_head_size(p, s) ((p)->size = (((p)->size & PREV_INUSE) | (s)))
1295
1296/* Set size/use field */
1297#define set_head(p, s) ((p)->size = (s))
1298
1299/* Set size at footer (only when chunk is not in use) */
1300#define set_foot(p, s) (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))
1301
1302
1303/*
1304 -------------------- Internal data structures --------------------
1305
1306 All internal state is held in an instance of malloc_state defined
1307 below. There are no other static variables, except in two optional
1308 cases:
1309 * If USE_MALLOC_LOCK is defined, the mALLOC_MUTEx declared above.
1310 * If HAVE_MMAP is true, but mmap doesn't support
1311 MAP_ANONYMOUS, a dummy file descriptor for mmap.
1312
1313 Beware of lots of tricks that minimize the total bookkeeping space
1314 requirements. The result is a little over 1K bytes (for 4byte
1315 pointers and size_t.)
1316*/
1317
1318/*
1319 Bins
1320
1321 An array of bin headers for free chunks. Each bin is doubly
1322 linked. The bins are approximately proportionally (log) spaced.
1323 There are a lot of these bins (128). This may look excessive, but
1324 works very well in practice. Most bins hold sizes that are
1325 unusual as malloc request sizes, but are more usual for fragments
1326 and consolidated sets of chunks, which is what these bins hold, so
1327 they can be found quickly. All procedures maintain the invariant
1328 that no consolidated chunk physically borders another one, so each
1329 chunk in a list is known to be preceeded and followed by either
1330 inuse chunks or the ends of memory.
1331
1332 Chunks in bins are kept in size order, with ties going to the
1333 approximately least recently used chunk. Ordering isn't needed
1334 for the small bins, which all contain the same-sized chunks, but
1335 facilitates best-fit allocation for larger chunks. These lists
1336 are just sequential. Keeping them in order almost never requires
1337 enough traversal to warrant using fancier ordered data
1338 structures.
1339
1340 Chunks of the same size are linked with the most
1341 recently freed at the front, and allocations are taken from the
1342 back. This results in LRU (FIFO) allocation order, which tends
1343 to give each chunk an equal opportunity to be consolidated with
1344 adjacent freed chunks, resulting in larger free chunks and less
1345 fragmentation.
1346
1347 To simplify use in double-linked lists, each bin header acts
1348 as a malloc_chunk. This avoids special-casing for headers.
1349 But to conserve space and improve locality, we allocate
1350 only the fd/bk pointers of bins, and then use repositioning tricks
1351 to treat these as the fields of a malloc_chunk*.
1352*/
1353
1354typedef struct malloc_chunk* mbinptr;
1355
1356/* addressing -- note that bin_at(0) does not exist */
1357#define bin_at(m, i) ((mbinptr)((char*)&((m)->bins[(i)<<1]) - (SIZE_SZ<<1)))
1358
1359/* analog of ++bin */
1360#define next_bin(b) ((mbinptr)((char*)(b) + (sizeof(mchunkptr)<<1)))
1361
1362/* Reminders about list directionality within bins */
1363#define first(b) ((b)->fd)
1364#define last(b) ((b)->bk)
1365
1366/* Take a chunk off a bin list */
1367#define dl_unlink(P, BK, FD) { \
1368 FD = P->fd; \
1369 BK = P->bk; \
1370 FD->bk = BK; \
1371 BK->fd = FD; \
1372}
1373
1374/*
1375 Indexing
1376
1377 Bins for sizes < 512 bytes contain chunks of all the same size, spaced
1378 8 bytes apart. Larger bins are approximately logarithmically spaced:
1379
1380 64 bins of size 8
1381 32 bins of size 64
1382 16 bins of size 512
1383 8 bins of size 4096
1384 4 bins of size 32768
1385 2 bins of size 262144
1386 1 bin of size what's left
1387
1388 The bins top out around 1MB because we expect to service large
1389 requests via mmap.
1390*/
1391
1392#define NBINS 96
1393#define NSMALLBINS 32
1394#define SMALLBIN_WIDTH 8
1395#define MIN_LARGE_SIZE 256
1396
1397#define in_smallbin_range(sz) \
1398 ((CHUNK_SIZE_T)(sz) < (CHUNK_SIZE_T)MIN_LARGE_SIZE)
1399
1400#define smallbin_index(sz) (((unsigned)(sz)) >> 3)
1401
1402/*
1403 ----------- Internal state representation and initialization -----------
1404*/
1405
1406/*
1407 Binmap
1408
1409 To help compensate for the large number of bins, a one-level index
1410 structure is used for bin-by-bin searching. `binmap' is a
1411 bitvector recording whether bins are definitely empty so they can
1412 be skipped over during during traversals. The bits are NOT always
1413 cleared as soon as bins are empty, but instead only
1414 when they are noticed to be empty during traversal in malloc.
1415*/
1416
1417/* Conservatively use 32 bits per map word, even if on 64bit system */
1418#define BINMAPSHIFT 5
1419#define BITSPERMAP (1U << BINMAPSHIFT)
1420#define BINMAPSIZE (NBINS / BITSPERMAP)
1421
1422/*
1423 Fastbins
1424
1425 An array of lists holding recently freed small chunks. Fastbins
1426 are not doubly linked. It is faster to single-link them, and
1427 since chunks are never removed from the middles of these lists,
1428 double linking is not necessary. Also, unlike regular bins, they
1429 are not even processed in FIFO order (they use faster LIFO) since
1430 ordering doesn't much matter in the transient contexts in which
1431 fastbins are normally used.
1432
1433 Chunks in fastbins keep their inuse bit set, so they cannot
1434 be consolidated with other free chunks. malloc_consolidate
1435 releases all chunks in fastbins and consolidates them with
1436 other free chunks.
1437*/
1438
1439typedef struct malloc_chunk* mfastbinptr;
1440
1441/* The maximum fastbin request size we support */
1442#define MAX_FAST_SIZE 80
1443
1444/* offset 2 to use otherwise unindexable first 2 bins */
1445#define fastbin_index(sz) ((((unsigned int)(sz)) >> 3) - 2)
1446
1447#define NFASTBINS (fastbin_index(request2size(MAX_FAST_SIZE))+1)
1448
1449struct malloc_state {
1450
1451 /* The maximum chunk size to be eligible for fastbin */
1452 INTERNAL_SIZE_T max_fast; /* low 2 bits used as flags */
1453
1454 /* Fastbins */
1455 mfastbinptr fastbins[NFASTBINS];
1456
1457 /* Base of the topmost chunk -- not otherwise kept in a bin */
1458 mchunkptr top;
1459
1460 /* The remainder from the most recent split of a small request */
1461 mchunkptr last_remainder;
1462
1463 /* Normal bins packed as described above */
1464 mchunkptr bins[NBINS * 2];
1465
1466 /* Bitmap of bins. Trailing zero map handles cases of largest binned size */
1467 unsigned int binmap[BINMAPSIZE+1];
1468
1469 /* Tunable parameters */
1470 CHUNK_SIZE_T trim_threshold;
1471 INTERNAL_SIZE_T top_pad;
1472 INTERNAL_SIZE_T mmap_threshold;
1473
1474 /* Cache malloc_getpagesize */
1475 unsigned int pagesize;
1476
1477 /* Track properties of MORECORE */
1478 unsigned int morecore_properties;
1479
1480 /* Statistics */
1481 INTERNAL_SIZE_T mmapped_mem;
1482 INTERNAL_SIZE_T sbrked_mem;
1483 INTERNAL_SIZE_T max_sbrked_mem;
1484 INTERNAL_SIZE_T max_mmapped_mem;
1485 INTERNAL_SIZE_T max_total_mem;
1486};
1487
1488typedef struct malloc_state *mstate;
1489
1490#endif /* USE_DL_MALLOC */