2 ** Bundled memory allocator.
4 ** Beware: this is a HEAVILY CUSTOMIZED version of dlmalloc.
5 ** The original bears the following remark:
7 ** This is a version (aka dlmalloc) of malloc/free/realloc written by
8 ** Doug Lea and released to the public domain, as explained at
9 ** http://creativecommons.org/licenses/publicdomain.
11 ** * Version pre-2.8.4 Wed Mar 29 19:46:29 2006 (dl at gee)
13 ** No additional copyright is claimed over the customizations.
14 ** Please do NOT bother the original author about this version here!
16 ** If you want to use dlmalloc in another project, you should get
17 ** the original from: ftp://gee.cs.oswego.edu/pub/misc/
18 ** For thread-safe derivatives, take a look at:
19 ** - ptmalloc: http://www.malloc.de/
20 ** - nedmalloc: http://www.nedprod.com/programs/portable/nedmalloc/
26 /* To get the mremap prototype. Must be defined before any system includes. */
27 #if defined(__linux__) && !defined(_GNU_SOURCE)
35 #ifndef LUAJIT_USE_SYSMALLOC
37 #define MAX_SIZE_T (~(size_t)0)
38 #define MALLOC_ALIGNMENT ((size_t)8U)
40 #define DEFAULT_GRANULARITY ((size_t)128U * (size_t)1024U)
41 #define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U)
42 #define DEFAULT_MMAP_THRESHOLD ((size_t)128U * (size_t)1024U)
43 #define MAX_RELEASE_CHECK_RATE 255
45 /* ------------------- size_t and alignment properties -------------------- */
47 /* The byte and bit size of a size_t */
48 #define SIZE_T_SIZE (sizeof(size_t))
49 #define SIZE_T_BITSIZE (sizeof(size_t) << 3)
51 /* Some constants coerced to size_t */
52 /* Annoying but necessary to avoid errors on some platforms */
53 #define SIZE_T_ZERO ((size_t)0)
54 #define SIZE_T_ONE ((size_t)1)
55 #define SIZE_T_TWO ((size_t)2)
56 #define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1)
57 #define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2)
58 #define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES)
60 /* The bit mask value corresponding to MALLOC_ALIGNMENT */
61 #define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE)
63 /* the number of bytes to offset an address to align it */
64 #define align_offset(A)\
65 ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\
66 ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK))
68 /* -------------------------- MMAP support ------------------------------- */
70 #define MFAIL ((void *)(MAX_SIZE_T))
71 #define CMFAIL ((char *)(MFAIL)) /* defined for convenience */
73 #define IS_DIRECT_BIT (SIZE_T_ONE)
77 #define WIN32_LEAN_AND_MEAN
82 /* Undocumented, but hey, that's what we all love so much about Windows. */
83 typedef long (*PNTAVM
)(HANDLE handle
, void **addr
, ULONG zbits
,
84 size_t *size
, ULONG alloctype
, ULONG prot
);
87 /* Number of top bits of the lower 32 bits of an address that must be zero.
88 ** Apparently 0 gives us full 64 bit addresses and 1 gives us the lower 2GB.
90 #define NTAVM_ZEROBITS 1
92 static void INIT_MMAP(void)
94 ntavm
= (PNTAVM
)GetProcAddress(GetModuleHandleA("ntdll.dll"),
95 "NtAllocateVirtualMemory");
98 /* Win64 32 bit MMAP via NtAllocateVirtualMemory. */
99 static LJ_AINLINE
void *CALL_MMAP(size_t size
)
101 DWORD olderr
= GetLastError();
103 long st
= ntavm(INVALID_HANDLE_VALUE
, &ptr
, NTAVM_ZEROBITS
, &size
,
104 MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
105 SetLastError(olderr
);
106 return st
== 0 ? ptr
: MFAIL
;
109 /* For direct MMAP, use MEM_TOP_DOWN to minimize interference */
110 static LJ_AINLINE
void *DIRECT_MMAP(size_t size
)
112 DWORD olderr
= GetLastError();
114 long st
= ntavm(INVALID_HANDLE_VALUE
, &ptr
, NTAVM_ZEROBITS
, &size
,
115 MEM_RESERVE
|MEM_COMMIT
|MEM_TOP_DOWN
, PAGE_READWRITE
);
116 SetLastError(olderr
);
117 return st
== 0 ? ptr
: MFAIL
;
122 #define INIT_MMAP() ((void)0)
124 /* Win32 MMAP via VirtualAlloc */
125 static LJ_AINLINE
void *CALL_MMAP(size_t size
)
127 DWORD olderr
= GetLastError();
128 void *ptr
= VirtualAlloc(0, size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
);
129 SetLastError(olderr
);
130 return ptr
? ptr
: MFAIL
;
133 /* For direct MMAP, use MEM_TOP_DOWN to minimize interference */
134 static LJ_AINLINE
void *DIRECT_MMAP(size_t size
)
136 DWORD olderr
= GetLastError();
137 void *ptr
= VirtualAlloc(0, size
, MEM_RESERVE
|MEM_COMMIT
|MEM_TOP_DOWN
,
139 SetLastError(olderr
);
140 return ptr
? ptr
: MFAIL
;
145 /* This function supports releasing coalesed segments */
146 static LJ_AINLINE
int CALL_MUNMAP(void *ptr
, size_t size
)
148 DWORD olderr
= GetLastError();
149 MEMORY_BASIC_INFORMATION minfo
;
150 char *cptr
= (char *)ptr
;
152 if (VirtualQuery(cptr
, &minfo
, sizeof(minfo
)) == 0)
154 if (minfo
.BaseAddress
!= cptr
|| minfo
.AllocationBase
!= cptr
||
155 minfo
.State
!= MEM_COMMIT
|| minfo
.RegionSize
> size
)
157 if (VirtualFree(cptr
, 0, MEM_RELEASE
) == 0)
159 cptr
+= minfo
.RegionSize
;
160 size
-= minfo
.RegionSize
;
162 SetLastError(olderr
);
169 #include <sys/mman.h>
171 #define MMAP_PROT (PROT_READ|PROT_WRITE)
172 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
173 #define MAP_ANONYMOUS MAP_ANON
175 #define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS)
178 /* 64 bit mode needs special support for allocating memory in the lower 2GB. */
182 /* Actually this only gives us max. 1GB in current Linux kernels. */
183 static LJ_AINLINE
void *CALL_MMAP(size_t size
)
186 void *ptr
= mmap(NULL
, size
, MMAP_PROT
, MAP_32BIT
|MMAP_FLAGS
, -1, 0);
191 #elif LJ_TARGET_OSX || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
193 /* OSX and FreeBSD mmap() use a naive first-fit linear search.
194 ** That's perfect for us. Except that -pagezero_size must be set for OSX,
195 ** otherwise the lower 4GB are blocked. And the 32GB RLIMIT_DATA needs
196 ** to be reduced to 250MB on FreeBSD.
198 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
199 #include <sys/resource.h>
200 #define MMAP_REGION_START ((uintptr_t)0x10000000)
202 #define MMAP_REGION_START ((uintptr_t)0x10000)
204 #define MMAP_REGION_END ((uintptr_t)0x80000000)
206 static LJ_AINLINE
void *CALL_MMAP(size_t size
)
209 /* Hint for next allocation. Doesn't need to be thread-safe. */
210 static uintptr_t alloc_hint
= MMAP_REGION_START
;
212 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
213 static int rlimit_modified
= 0;
214 if (LJ_UNLIKELY(rlimit_modified
== 0)) {
216 rlim
.rlim_cur
= rlim
.rlim_max
= MMAP_REGION_START
;
217 setrlimit(RLIMIT_DATA
, &rlim
); /* Ignore result. May fail below. */
222 void *p
= mmap((void *)alloc_hint
, size
, MMAP_PROT
, MMAP_FLAGS
, -1, 0);
223 if ((uintptr_t)p
>= MMAP_REGION_START
&&
224 (uintptr_t)p
+ size
< MMAP_REGION_END
) {
225 alloc_hint
= (uintptr_t)p
+ size
;
229 if (p
!= CMFAIL
) munmap(p
, size
);
232 alloc_hint
= MMAP_REGION_START
;
240 #error "NYI: need an equivalent of MAP_32BIT for this 64 bit OS"
246 /* 32 bit mode is easy. */
247 static LJ_AINLINE
void *CALL_MMAP(size_t size
)
250 void *ptr
= mmap(NULL
, size
, MMAP_PROT
, MMAP_FLAGS
, -1, 0);
257 #define INIT_MMAP() ((void)0)
258 #define DIRECT_MMAP(s) CALL_MMAP(s)
260 static LJ_AINLINE
int CALL_MUNMAP(void *ptr
, size_t size
)
263 int ret
= munmap(ptr
, size
);
269 /* Need to define _GNU_SOURCE to get the mremap prototype. */
270 static LJ_AINLINE
void *CALL_MREMAP_(void *ptr
, size_t osz
, size_t nsz
,
274 ptr
= mremap(ptr
, osz
, nsz
, flags
);
279 #define CALL_MREMAP(addr, osz, nsz, mv) CALL_MREMAP_((addr), (osz), (nsz), (mv))
280 #define CALL_MREMAP_NOMOVE 0
281 #define CALL_MREMAP_MAYMOVE 1
283 #define CALL_MREMAP_MV CALL_MREMAP_NOMOVE
285 #define CALL_MREMAP_MV CALL_MREMAP_MAYMOVE
292 #define CALL_MREMAP(addr, osz, nsz, mv) ((void)osz, MFAIL)
295 /* ----------------------- Chunk representations ------------------------ */
297 struct malloc_chunk
{
298 size_t prev_foot
; /* Size of previous chunk (if free). */
299 size_t head
; /* Size and inuse bits. */
300 struct malloc_chunk
*fd
; /* double links -- used only if free. */
301 struct malloc_chunk
*bk
;
304 typedef struct malloc_chunk mchunk
;
305 typedef struct malloc_chunk
*mchunkptr
;
306 typedef struct malloc_chunk
*sbinptr
; /* The type of bins of chunks */
307 typedef size_t bindex_t
; /* Described below */
308 typedef unsigned int binmap_t
; /* Described below */
309 typedef unsigned int flag_t
; /* The type of various bit flag sets */
311 /* ------------------- Chunks sizes and alignments ----------------------- */
313 #define MCHUNK_SIZE (sizeof(mchunk))
315 #define CHUNK_OVERHEAD (SIZE_T_SIZE)
317 /* Direct chunks need a second word of overhead ... */
318 #define DIRECT_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES)
319 /* ... and additional padding for fake next-chunk at foot */
320 #define DIRECT_FOOT_PAD (FOUR_SIZE_T_SIZES)
322 /* The smallest size we can malloc is an aligned minimal chunk */
323 #define MIN_CHUNK_SIZE\
324 ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)
326 /* conversion from malloc headers to user pointers, and back */
327 #define chunk2mem(p) ((void *)((char *)(p) + TWO_SIZE_T_SIZES))
328 #define mem2chunk(mem) ((mchunkptr)((char *)(mem) - TWO_SIZE_T_SIZES))
329 /* chunk associated with aligned address A */
330 #define align_as_chunk(A) (mchunkptr)((A) + align_offset(chunk2mem(A)))
332 /* Bounds on request (not chunk) sizes. */
333 #define MAX_REQUEST ((~MIN_CHUNK_SIZE+1) << 2)
334 #define MIN_REQUEST (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE)
336 /* pad request bytes into a usable size */
337 #define pad_request(req) \
338 (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK)
340 /* pad request, checking for minimum (but not maximum) */
341 #define request2size(req) \
342 (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req))
344 /* ------------------ Operations on head and foot fields ----------------- */
346 #define PINUSE_BIT (SIZE_T_ONE)
347 #define CINUSE_BIT (SIZE_T_TWO)
348 #define INUSE_BITS (PINUSE_BIT|CINUSE_BIT)
350 /* Head value for fenceposts */
351 #define FENCEPOST_HEAD (INUSE_BITS|SIZE_T_SIZE)
353 /* extraction of fields from head words */
354 #define cinuse(p) ((p)->head & CINUSE_BIT)
355 #define pinuse(p) ((p)->head & PINUSE_BIT)
356 #define chunksize(p) ((p)->head & ~(INUSE_BITS))
358 #define clear_pinuse(p) ((p)->head &= ~PINUSE_BIT)
359 #define clear_cinuse(p) ((p)->head &= ~CINUSE_BIT)
361 /* Treat space at ptr +/- offset as a chunk */
362 #define chunk_plus_offset(p, s) ((mchunkptr)(((char *)(p)) + (s)))
363 #define chunk_minus_offset(p, s) ((mchunkptr)(((char *)(p)) - (s)))
365 /* Ptr to next or previous physical malloc_chunk. */
366 #define next_chunk(p) ((mchunkptr)(((char *)(p)) + ((p)->head & ~INUSE_BITS)))
367 #define prev_chunk(p) ((mchunkptr)(((char *)(p)) - ((p)->prev_foot) ))
369 /* extract next chunk's pinuse bit */
370 #define next_pinuse(p) ((next_chunk(p)->head) & PINUSE_BIT)
372 /* Get/set size at footer */
373 #define get_foot(p, s) (((mchunkptr)((char *)(p) + (s)))->prev_foot)
374 #define set_foot(p, s) (((mchunkptr)((char *)(p) + (s)))->prev_foot = (s))
376 /* Set size, pinuse bit, and foot */
377 #define set_size_and_pinuse_of_free_chunk(p, s)\
378 ((p)->head = (s|PINUSE_BIT), set_foot(p, s))
380 /* Set size, pinuse bit, foot, and clear next pinuse */
381 #define set_free_with_pinuse(p, s, n)\
382 (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s))
384 #define is_direct(p)\
385 (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_DIRECT_BIT))
387 /* Get the internal overhead associated with chunk p */
388 #define overhead_for(p)\
389 (is_direct(p)? DIRECT_CHUNK_OVERHEAD : CHUNK_OVERHEAD)
391 /* ---------------------- Overlaid data structures ----------------------- */
393 struct malloc_tree_chunk
{
394 /* The first four fields must be compatible with malloc_chunk */
397 struct malloc_tree_chunk
*fd
;
398 struct malloc_tree_chunk
*bk
;
400 struct malloc_tree_chunk
*child
[2];
401 struct malloc_tree_chunk
*parent
;
405 typedef struct malloc_tree_chunk tchunk
;
406 typedef struct malloc_tree_chunk
*tchunkptr
;
407 typedef struct malloc_tree_chunk
*tbinptr
; /* The type of bins of trees */
409 /* A little helper macro for trees */
410 #define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1])
412 /* ----------------------------- Segments -------------------------------- */
414 struct malloc_segment
{
415 char *base
; /* base address */
416 size_t size
; /* allocated size */
417 struct malloc_segment
*next
; /* ptr to next segment */
420 typedef struct malloc_segment msegment
;
421 typedef struct malloc_segment
*msegmentptr
;
423 /* ---------------------------- malloc_state ----------------------------- */
425 /* Bin types, widths and sizes */
426 #define NSMALLBINS (32U)
427 #define NTREEBINS (32U)
428 #define SMALLBIN_SHIFT (3U)
429 #define SMALLBIN_WIDTH (SIZE_T_ONE << SMALLBIN_SHIFT)
430 #define TREEBIN_SHIFT (8U)
431 #define MIN_LARGE_SIZE (SIZE_T_ONE << TREEBIN_SHIFT)
432 #define MAX_SMALL_SIZE (MIN_LARGE_SIZE - SIZE_T_ONE)
433 #define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD)
435 struct malloc_state
{
443 size_t release_checks
;
444 mchunkptr smallbins
[(NSMALLBINS
+1)*2];
445 tbinptr treebins
[NTREEBINS
];
449 typedef struct malloc_state
*mstate
;
451 #define is_initialized(M) ((M)->top != 0)
453 /* -------------------------- system alloc setup ------------------------- */
455 /* page-align a size */
456 #define page_align(S)\
457 (((S) + (LJ_PAGESIZE - SIZE_T_ONE)) & ~(LJ_PAGESIZE - SIZE_T_ONE))
459 /* granularity-align a size */
460 #define granularity_align(S)\
461 (((S) + (DEFAULT_GRANULARITY - SIZE_T_ONE))\
462 & ~(DEFAULT_GRANULARITY - SIZE_T_ONE))
464 #if LJ_TARGET_WINDOWS
465 #define mmap_align(S) granularity_align(S)
467 #define mmap_align(S) page_align(S)
470 /* True if segment S holds address A */
471 #define segment_holds(S, A)\
472 ((char *)(A) >= S->base && (char *)(A) < S->base + S->size)
474 /* Return segment holding given address */
475 static msegmentptr
segment_holding(mstate m
, char *addr
)
477 msegmentptr sp
= &m
->seg
;
479 if (addr
>= sp
->base
&& addr
< sp
->base
+ sp
->size
)
481 if ((sp
= sp
->next
) == 0)
486 /* Return true if segment contains a segment link */
487 static int has_segment_link(mstate m
, msegmentptr ss
)
489 msegmentptr sp
= &m
->seg
;
491 if ((char *)sp
>= ss
->base
&& (char *)sp
< ss
->base
+ ss
->size
)
493 if ((sp
= sp
->next
) == 0)
499 TOP_FOOT_SIZE is padding at the end of a segment, including space
500 that may be needed to place segment records and fenceposts when new
501 noncontiguous segments are added.
503 #define TOP_FOOT_SIZE\
504 (align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE)
506 /* ---------------------------- Indexing Bins ---------------------------- */
508 #define is_small(s) (((s) >> SMALLBIN_SHIFT) < NSMALLBINS)
509 #define small_index(s) ((s) >> SMALLBIN_SHIFT)
510 #define small_index2size(i) ((i) << SMALLBIN_SHIFT)
511 #define MIN_SMALL_INDEX (small_index(MIN_CHUNK_SIZE))
513 /* addressing by index. See above about smallbin repositioning */
514 #define smallbin_at(M, i) ((sbinptr)((char *)&((M)->smallbins[(i)<<1])))
515 #define treebin_at(M,i) (&((M)->treebins[i]))
517 /* assign tree index for size S to variable I */
518 #define compute_tree_index(S, I)\
520 unsigned int X = (unsigned int)(S >> TREEBIN_SHIFT);\
523 } else if (X > 0xFFFF) {\
526 unsigned int K = lj_fls(X);\
527 I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\
531 /* Bit representing maximum resolved size in a treebin at i */
532 #define bit_for_tree_index(i) \
533 (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2)
535 /* Shift placing maximum resolved bit in a treebin at i as sign bit */
536 #define leftshift_for_tree_index(i) \
537 ((i == NTREEBINS-1)? 0 : \
538 ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2)))
540 /* The size of the smallest chunk held in bin with index i */
541 #define minsize_for_tree_index(i) \
542 ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) | \
543 (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1)))
545 /* ------------------------ Operations on bin maps ----------------------- */
547 /* bit corresponding to given index */
548 #define idx2bit(i) ((binmap_t)(1) << (i))
550 /* Mark/Clear bits with given index */
551 #define mark_smallmap(M,i) ((M)->smallmap |= idx2bit(i))
552 #define clear_smallmap(M,i) ((M)->smallmap &= ~idx2bit(i))
553 #define smallmap_is_marked(M,i) ((M)->smallmap & idx2bit(i))
555 #define mark_treemap(M,i) ((M)->treemap |= idx2bit(i))
556 #define clear_treemap(M,i) ((M)->treemap &= ~idx2bit(i))
557 #define treemap_is_marked(M,i) ((M)->treemap & idx2bit(i))
559 /* mask with all bits to left of least bit of x on */
560 #define left_bits(x) ((x<<1) | (~(x<<1)+1))
562 /* Set cinuse bit and pinuse bit of next chunk */
563 #define set_inuse(M,p,s)\
564 ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\
565 ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT)
567 /* Set cinuse and pinuse of this chunk and pinuse of next chunk */
568 #define set_inuse_and_pinuse(M,p,s)\
569 ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\
570 ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT)
572 /* Set size, cinuse and pinuse bit of this chunk */
573 #define set_size_and_pinuse_of_inuse_chunk(M, p, s)\
574 ((p)->head = (s|PINUSE_BIT|CINUSE_BIT))
576 /* ----------------------- Operations on smallbins ----------------------- */
578 /* Link a free chunk into a smallbin */
579 #define insert_small_chunk(M, P, S) {\
580 bindex_t I = small_index(S);\
581 mchunkptr B = smallbin_at(M, I);\
583 if (!smallmap_is_marked(M, I))\
584 mark_smallmap(M, I);\
593 /* Unlink a chunk from a smallbin */
594 #define unlink_small_chunk(M, P, S) {\
595 mchunkptr F = P->fd;\
596 mchunkptr B = P->bk;\
597 bindex_t I = small_index(S);\
599 clear_smallmap(M, I);\
606 /* Unlink the first chunk from a smallbin */
607 #define unlink_first_small_chunk(M, B, P, I) {\
608 mchunkptr F = P->fd;\
610 clear_smallmap(M, I);\
617 /* Replace dv node, binning the old one */
618 /* Used only when dvsize known to be small */
619 #define replace_dv(M, P, S) {\
620 size_t DVS = M->dvsize;\
622 mchunkptr DV = M->dv;\
623 insert_small_chunk(M, DV, DVS);\
629 /* ------------------------- Operations on trees ------------------------- */
631 /* Insert chunk into tree */
632 #define insert_large_chunk(M, X, S) {\
635 compute_tree_index(S, I);\
636 H = treebin_at(M, I);\
638 X->child[0] = X->child[1] = 0;\
639 if (!treemap_is_marked(M, I)) {\
642 X->parent = (tchunkptr)H;\
646 size_t K = S << leftshift_for_tree_index(I);\
648 if (chunksize(T) != S) {\
649 tchunkptr *C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\
660 tchunkptr F = T->fd;\
671 #define unlink_large_chunk(M, X) {\
672 tchunkptr XP = X->parent;\
675 tchunkptr F = X->fd;\
681 if (((R = *(RP = &(X->child[1]))) != 0) ||\
682 ((R = *(RP = &(X->child[0]))) != 0)) {\
684 while ((*(CP = &(R->child[1])) != 0) ||\
685 (*(CP = &(R->child[0])) != 0)) {\
692 tbinptr *H = treebin_at(M, X->index);\
695 clear_treemap(M, X->index);\
697 if (XP->child[0] == X) \
705 if ((C0 = X->child[0]) != 0) {\
709 if ((C1 = X->child[1]) != 0) {\
717 /* Relays to large vs small bin operations */
719 #define insert_chunk(M, P, S)\
720 if (is_small(S)) { insert_small_chunk(M, P, S)\
721 } else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); }
723 #define unlink_chunk(M, P, S)\
724 if (is_small(S)) { unlink_small_chunk(M, P, S)\
725 } else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); }
727 /* ----------------------- Direct-mmapping chunks ----------------------- */
729 static void *direct_alloc(size_t nb
)
731 size_t mmsize
= mmap_align(nb
+ SIX_SIZE_T_SIZES
+ CHUNK_ALIGN_MASK
);
732 if (LJ_LIKELY(mmsize
> nb
)) { /* Check for wrap around 0 */
733 char *mm
= (char *)(DIRECT_MMAP(mmsize
));
735 size_t offset
= align_offset(chunk2mem(mm
));
736 size_t psize
= mmsize
- offset
- DIRECT_FOOT_PAD
;
737 mchunkptr p
= (mchunkptr
)(mm
+ offset
);
738 p
->prev_foot
= offset
| IS_DIRECT_BIT
;
739 p
->head
= psize
|CINUSE_BIT
;
740 chunk_plus_offset(p
, psize
)->head
= FENCEPOST_HEAD
;
741 chunk_plus_offset(p
, psize
+SIZE_T_SIZE
)->head
= 0;
748 static mchunkptr
direct_resize(mchunkptr oldp
, size_t nb
)
750 size_t oldsize
= chunksize(oldp
);
751 if (is_small(nb
)) /* Can't shrink direct regions below small size */
753 /* Keep old chunk if big enough but not too big */
754 if (oldsize
>= nb
+ SIZE_T_SIZE
&&
755 (oldsize
- nb
) <= (DEFAULT_GRANULARITY
<< 1)) {
758 size_t offset
= oldp
->prev_foot
& ~IS_DIRECT_BIT
;
759 size_t oldmmsize
= oldsize
+ offset
+ DIRECT_FOOT_PAD
;
760 size_t newmmsize
= mmap_align(nb
+ SIX_SIZE_T_SIZES
+ CHUNK_ALIGN_MASK
);
761 char *cp
= (char *)CALL_MREMAP((char *)oldp
- offset
,
762 oldmmsize
, newmmsize
, CALL_MREMAP_MV
);
764 mchunkptr newp
= (mchunkptr
)(cp
+ offset
);
765 size_t psize
= newmmsize
- offset
- DIRECT_FOOT_PAD
;
766 newp
->head
= psize
|CINUSE_BIT
;
767 chunk_plus_offset(newp
, psize
)->head
= FENCEPOST_HEAD
;
768 chunk_plus_offset(newp
, psize
+SIZE_T_SIZE
)->head
= 0;
775 /* -------------------------- mspace management -------------------------- */
777 /* Initialize top chunk and its size */
778 static void init_top(mstate m
, mchunkptr p
, size_t psize
)
780 /* Ensure alignment */
781 size_t offset
= align_offset(chunk2mem(p
));
782 p
= (mchunkptr
)((char *)p
+ offset
);
787 p
->head
= psize
| PINUSE_BIT
;
788 /* set size of fake trailing chunk holding overhead space only once */
789 chunk_plus_offset(p
, psize
)->head
= TOP_FOOT_SIZE
;
790 m
->trim_check
= DEFAULT_TRIM_THRESHOLD
; /* reset on each update */
793 /* Initialize bins for a new mstate that is otherwise zeroed out */
794 static void init_bins(mstate m
)
796 /* Establish circular links for smallbins */
798 for (i
= 0; i
< NSMALLBINS
; i
++) {
799 sbinptr bin
= smallbin_at(m
,i
);
800 bin
->fd
= bin
->bk
= bin
;
804 /* Allocate chunk and prepend remainder with chunk in successor base. */
805 static void *prepend_alloc(mstate m
, char *newbase
, char *oldbase
, size_t nb
)
807 mchunkptr p
= align_as_chunk(newbase
);
808 mchunkptr oldfirst
= align_as_chunk(oldbase
);
809 size_t psize
= (size_t)((char *)oldfirst
- (char *)p
);
810 mchunkptr q
= chunk_plus_offset(p
, nb
);
811 size_t qsize
= psize
- nb
;
812 set_size_and_pinuse_of_inuse_chunk(m
, p
, nb
);
814 /* consolidate remainder with first chunk of old base */
815 if (oldfirst
== m
->top
) {
816 size_t tsize
= m
->topsize
+= qsize
;
818 q
->head
= tsize
| PINUSE_BIT
;
819 } else if (oldfirst
== m
->dv
) {
820 size_t dsize
= m
->dvsize
+= qsize
;
822 set_size_and_pinuse_of_free_chunk(q
, dsize
);
824 if (!cinuse(oldfirst
)) {
825 size_t nsize
= chunksize(oldfirst
);
826 unlink_chunk(m
, oldfirst
, nsize
);
827 oldfirst
= chunk_plus_offset(oldfirst
, nsize
);
830 set_free_with_pinuse(q
, qsize
, oldfirst
);
831 insert_chunk(m
, q
, qsize
);
837 /* Add a segment to hold a new noncontiguous region */
838 static void add_segment(mstate m
, char *tbase
, size_t tsize
)
840 /* Determine locations and sizes of segment, fenceposts, old top */
841 char *old_top
= (char *)m
->top
;
842 msegmentptr oldsp
= segment_holding(m
, old_top
);
843 char *old_end
= oldsp
->base
+ oldsp
->size
;
844 size_t ssize
= pad_request(sizeof(struct malloc_segment
));
845 char *rawsp
= old_end
- (ssize
+ FOUR_SIZE_T_SIZES
+ CHUNK_ALIGN_MASK
);
846 size_t offset
= align_offset(chunk2mem(rawsp
));
847 char *asp
= rawsp
+ offset
;
848 char *csp
= (asp
< (old_top
+ MIN_CHUNK_SIZE
))? old_top
: asp
;
849 mchunkptr sp
= (mchunkptr
)csp
;
850 msegmentptr ss
= (msegmentptr
)(chunk2mem(sp
));
851 mchunkptr tnext
= chunk_plus_offset(sp
, ssize
);
854 /* reset top to new space */
855 init_top(m
, (mchunkptr
)tbase
, tsize
- TOP_FOOT_SIZE
);
857 /* Set up segment record */
858 set_size_and_pinuse_of_inuse_chunk(m
, sp
, ssize
);
859 *ss
= m
->seg
; /* Push current record */
864 /* Insert trailing fenceposts */
866 mchunkptr nextp
= chunk_plus_offset(p
, SIZE_T_SIZE
);
867 p
->head
= FENCEPOST_HEAD
;
868 if ((char *)(&(nextp
->head
)) < old_end
)
874 /* Insert the rest of old top into a bin as an ordinary free chunk */
875 if (csp
!= old_top
) {
876 mchunkptr q
= (mchunkptr
)old_top
;
877 size_t psize
= (size_t)(csp
- old_top
);
878 mchunkptr tn
= chunk_plus_offset(q
, psize
);
879 set_free_with_pinuse(q
, psize
, tn
);
880 insert_chunk(m
, q
, psize
);
884 /* -------------------------- System allocation -------------------------- */
886 static void *alloc_sys(mstate m
, size_t nb
)
888 char *tbase
= CMFAIL
;
891 /* Directly map large chunks */
892 if (LJ_UNLIKELY(nb
>= DEFAULT_MMAP_THRESHOLD
)) {
893 void *mem
= direct_alloc(nb
);
899 size_t req
= nb
+ TOP_FOOT_SIZE
+ SIZE_T_ONE
;
900 size_t rsize
= granularity_align(req
);
901 if (LJ_LIKELY(rsize
> nb
)) { /* Fail if wraps around zero */
902 char *mp
= (char *)(CALL_MMAP(rsize
));
910 if (tbase
!= CMFAIL
) {
911 msegmentptr sp
= &m
->seg
;
912 /* Try to merge with an existing segment */
913 while (sp
!= 0 && tbase
!= sp
->base
+ sp
->size
)
915 if (sp
!= 0 && segment_holds(sp
, m
->top
)) { /* append */
917 init_top(m
, m
->top
, m
->topsize
+ tsize
);
920 while (sp
!= 0 && sp
->base
!= tbase
+ tsize
)
923 char *oldbase
= sp
->base
;
926 return prepend_alloc(m
, tbase
, oldbase
, nb
);
928 add_segment(m
, tbase
, tsize
);
932 if (nb
< m
->topsize
) { /* Allocate from new or extended top space */
933 size_t rsize
= m
->topsize
-= nb
;
934 mchunkptr p
= m
->top
;
935 mchunkptr r
= m
->top
= chunk_plus_offset(p
, nb
);
936 r
->head
= rsize
| PINUSE_BIT
;
937 set_size_and_pinuse_of_inuse_chunk(m
, p
, nb
);
945 /* ----------------------- system deallocation -------------------------- */
947 /* Unmap and unlink any mmapped segments that don't contain used chunks */
948 static size_t release_unused_segments(mstate m
)
952 msegmentptr pred
= &m
->seg
;
953 msegmentptr sp
= pred
->next
;
955 char *base
= sp
->base
;
956 size_t size
= sp
->size
;
957 msegmentptr next
= sp
->next
;
960 mchunkptr p
= align_as_chunk(base
);
961 size_t psize
= chunksize(p
);
962 /* Can unmap if first chunk holds entire segment and not pinned */
963 if (!cinuse(p
) && (char *)p
+ psize
>= base
+ size
- TOP_FOOT_SIZE
) {
964 tchunkptr tp
= (tchunkptr
)p
;
969 unlink_large_chunk(m
, tp
);
971 if (CALL_MUNMAP(base
, size
) == 0) {
973 /* unlink obsoleted record */
976 } else { /* back out if cannot unmap */
977 insert_large_chunk(m
, tp
, psize
);
984 /* Reset check counter */
985 m
->release_checks
= nsegs
> MAX_RELEASE_CHECK_RATE
?
986 nsegs
: MAX_RELEASE_CHECK_RATE
;
990 static int alloc_trim(mstate m
, size_t pad
)
993 if (pad
< MAX_REQUEST
&& is_initialized(m
)) {
994 pad
+= TOP_FOOT_SIZE
; /* ensure enough room for segment overhead */
996 if (m
->topsize
> pad
) {
997 /* Shrink top space in granularity-size units, keeping at least one */
998 size_t unit
= DEFAULT_GRANULARITY
;
999 size_t extra
= ((m
->topsize
- pad
+ (unit
- SIZE_T_ONE
)) / unit
-
1001 msegmentptr sp
= segment_holding(m
, (char *)m
->top
);
1003 if (sp
->size
>= extra
&&
1004 !has_segment_link(m
, sp
)) { /* can't shrink if pinned */
1005 size_t newsize
= sp
->size
- extra
;
1006 /* Prefer mremap, fall back to munmap */
1007 if ((CALL_MREMAP(sp
->base
, sp
->size
, newsize
, CALL_MREMAP_NOMOVE
) != MFAIL
) ||
1008 (CALL_MUNMAP(sp
->base
+ newsize
, extra
) == 0)) {
1013 if (released
!= 0) {
1014 sp
->size
-= released
;
1015 init_top(m
, m
->top
, m
->topsize
- released
);
1019 /* Unmap any unused mmapped segments */
1020 released
+= release_unused_segments(m
);
1022 /* On failure, disable autotrim to avoid repeated failed future calls */
1023 if (released
== 0 && m
->topsize
> m
->trim_check
)
1024 m
->trim_check
= MAX_SIZE_T
;
1027 return (released
!= 0)? 1 : 0;
1030 /* ---------------------------- malloc support --------------------------- */
1032 /* allocate a large request from the best fitting chunk in a treebin */
1033 static void *tmalloc_large(mstate m
, size_t nb
)
1036 size_t rsize
= ~nb
+1; /* Unsigned negation */
1039 compute_tree_index(nb
, idx
);
1041 if ((t
= *treebin_at(m
, idx
)) != 0) {
1042 /* Traverse tree for this bin looking for node with size == nb */
1043 size_t sizebits
= nb
<< leftshift_for_tree_index(idx
);
1044 tchunkptr rst
= 0; /* The deepest untaken right subtree */
1047 size_t trem
= chunksize(t
) - nb
;
1050 if ((rsize
= trem
) == 0)
1054 t
= t
->child
[(sizebits
>> (SIZE_T_BITSIZE
-SIZE_T_ONE
)) & 1];
1055 if (rt
!= 0 && rt
!= t
)
1058 t
= rst
; /* set t to least subtree holding sizes > nb */
1065 if (t
== 0 && v
== 0) { /* set t to root of next non-empty treebin */
1066 binmap_t leftbits
= left_bits(idx2bit(idx
)) & m
->treemap
;
1068 t
= *treebin_at(m
, lj_ffs(leftbits
));
1071 while (t
!= 0) { /* find smallest of tree or subtree */
1072 size_t trem
= chunksize(t
) - nb
;
1077 t
= leftmost_child(t
);
1080 /* If dv is a better fit, return NULL so malloc will use it */
1081 if (v
!= 0 && rsize
< (size_t)(m
->dvsize
- nb
)) {
1082 mchunkptr r
= chunk_plus_offset(v
, nb
);
1083 unlink_large_chunk(m
, v
);
1084 if (rsize
< MIN_CHUNK_SIZE
) {
1085 set_inuse_and_pinuse(m
, v
, (rsize
+ nb
));
1087 set_size_and_pinuse_of_inuse_chunk(m
, v
, nb
);
1088 set_size_and_pinuse_of_free_chunk(r
, rsize
);
1089 insert_chunk(m
, r
, rsize
);
1091 return chunk2mem(v
);
1096 /* allocate a small request from the best fitting chunk in a treebin */
1097 static void *tmalloc_small(mstate m
, size_t nb
)
1102 bindex_t i
= lj_ffs(m
->treemap
);
1104 v
= t
= *treebin_at(m
, i
);
1105 rsize
= chunksize(t
) - nb
;
1107 while ((t
= leftmost_child(t
)) != 0) {
1108 size_t trem
= chunksize(t
) - nb
;
1115 r
= chunk_plus_offset(v
, nb
);
1116 unlink_large_chunk(m
, v
);
1117 if (rsize
< MIN_CHUNK_SIZE
) {
1118 set_inuse_and_pinuse(m
, v
, (rsize
+ nb
));
1120 set_size_and_pinuse_of_inuse_chunk(m
, v
, nb
);
1121 set_size_and_pinuse_of_free_chunk(r
, rsize
);
1122 replace_dv(m
, r
, rsize
);
1124 return chunk2mem(v
);
1127 /* ----------------------------------------------------------------------- */
1129 void *lj_alloc_create(void)
1131 size_t tsize
= DEFAULT_GRANULARITY
;
1134 tbase
= (char *)(CALL_MMAP(tsize
));
1135 if (tbase
!= CMFAIL
) {
1136 size_t msize
= pad_request(sizeof(struct malloc_state
));
1138 mchunkptr msp
= align_as_chunk(tbase
);
1139 mstate m
= (mstate
)(chunk2mem(msp
));
1140 memset(m
, 0, msize
);
1141 msp
->head
= (msize
|PINUSE_BIT
|CINUSE_BIT
);
1142 m
->seg
.base
= tbase
;
1143 m
->seg
.size
= tsize
;
1144 m
->release_checks
= MAX_RELEASE_CHECK_RATE
;
1146 mn
= next_chunk(mem2chunk(m
));
1147 init_top(m
, mn
, (size_t)((tbase
+ tsize
) - (char *)mn
) - TOP_FOOT_SIZE
);
1153 void lj_alloc_destroy(void *msp
)
1155 mstate ms
= (mstate
)msp
;
1156 msegmentptr sp
= &ms
->seg
;
1158 char *base
= sp
->base
;
1159 size_t size
= sp
->size
;
1161 CALL_MUNMAP(base
, size
);
1165 static LJ_NOINLINE
void *lj_alloc_malloc(void *msp
, size_t nsize
)
1167 mstate ms
= (mstate
)msp
;
1170 if (nsize
<= MAX_SMALL_REQUEST
) {
1173 nb
= (nsize
< MIN_REQUEST
)? MIN_CHUNK_SIZE
: pad_request(nsize
);
1174 idx
= small_index(nb
);
1175 smallbits
= ms
->smallmap
>> idx
;
1177 if ((smallbits
& 0x3U
) != 0) { /* Remainderless fit to a smallbin. */
1179 idx
+= ~smallbits
& 1; /* Uses next bin if idx empty */
1180 b
= smallbin_at(ms
, idx
);
1182 unlink_first_small_chunk(ms
, b
, p
, idx
);
1183 set_inuse_and_pinuse(ms
, p
, small_index2size(idx
));
1186 } else if (nb
> ms
->dvsize
) {
1187 if (smallbits
!= 0) { /* Use chunk in next nonempty smallbin */
1190 binmap_t leftbits
= (smallbits
<< idx
) & left_bits(idx2bit(idx
));
1191 bindex_t i
= lj_ffs(leftbits
);
1192 b
= smallbin_at(ms
, i
);
1194 unlink_first_small_chunk(ms
, b
, p
, i
);
1195 rsize
= small_index2size(i
) - nb
;
1196 /* Fit here cannot be remainderless if 4byte sizes */
1197 if (SIZE_T_SIZE
!= 4 && rsize
< MIN_CHUNK_SIZE
) {
1198 set_inuse_and_pinuse(ms
, p
, small_index2size(i
));
1200 set_size_and_pinuse_of_inuse_chunk(ms
, p
, nb
);
1201 r
= chunk_plus_offset(p
, nb
);
1202 set_size_and_pinuse_of_free_chunk(r
, rsize
);
1203 replace_dv(ms
, r
, rsize
);
1207 } else if (ms
->treemap
!= 0 && (mem
= tmalloc_small(ms
, nb
)) != 0) {
1211 } else if (nsize
>= MAX_REQUEST
) {
1212 nb
= MAX_SIZE_T
; /* Too big to allocate. Force failure (in sys alloc) */
1214 nb
= pad_request(nsize
);
1215 if (ms
->treemap
!= 0 && (mem
= tmalloc_large(ms
, nb
)) != 0) {
1220 if (nb
<= ms
->dvsize
) {
1221 size_t rsize
= ms
->dvsize
- nb
;
1222 mchunkptr p
= ms
->dv
;
1223 if (rsize
>= MIN_CHUNK_SIZE
) { /* split dv */
1224 mchunkptr r
= ms
->dv
= chunk_plus_offset(p
, nb
);
1226 set_size_and_pinuse_of_free_chunk(r
, rsize
);
1227 set_size_and_pinuse_of_inuse_chunk(ms
, p
, nb
);
1228 } else { /* exhaust dv */
1229 size_t dvs
= ms
->dvsize
;
1232 set_inuse_and_pinuse(ms
, p
, dvs
);
1236 } else if (nb
< ms
->topsize
) { /* Split top */
1237 size_t rsize
= ms
->topsize
-= nb
;
1238 mchunkptr p
= ms
->top
;
1239 mchunkptr r
= ms
->top
= chunk_plus_offset(p
, nb
);
1240 r
->head
= rsize
| PINUSE_BIT
;
1241 set_size_and_pinuse_of_inuse_chunk(ms
, p
, nb
);
1245 return alloc_sys(ms
, nb
);
1248 static LJ_NOINLINE
void *lj_alloc_free(void *msp
, void *ptr
)
1251 mchunkptr p
= mem2chunk(ptr
);
1252 mstate fm
= (mstate
)msp
;
1253 size_t psize
= chunksize(p
);
1254 mchunkptr next
= chunk_plus_offset(p
, psize
);
1256 size_t prevsize
= p
->prev_foot
;
1257 if ((prevsize
& IS_DIRECT_BIT
) != 0) {
1258 prevsize
&= ~IS_DIRECT_BIT
;
1259 psize
+= prevsize
+ DIRECT_FOOT_PAD
;
1260 CALL_MUNMAP((char *)p
- prevsize
, psize
);
1263 mchunkptr prev
= chunk_minus_offset(p
, prevsize
);
1266 /* consolidate backward */
1268 unlink_chunk(fm
, p
, prevsize
);
1269 } else if ((next
->head
& INUSE_BITS
) == INUSE_BITS
) {
1271 set_free_with_pinuse(p
, psize
, next
);
1276 if (!cinuse(next
)) { /* consolidate forward */
1277 if (next
== fm
->top
) {
1278 size_t tsize
= fm
->topsize
+= psize
;
1280 p
->head
= tsize
| PINUSE_BIT
;
1285 if (tsize
> fm
->trim_check
)
1288 } else if (next
== fm
->dv
) {
1289 size_t dsize
= fm
->dvsize
+= psize
;
1291 set_size_and_pinuse_of_free_chunk(p
, dsize
);
1294 size_t nsize
= chunksize(next
);
1296 unlink_chunk(fm
, next
, nsize
);
1297 set_size_and_pinuse_of_free_chunk(p
, psize
);
1304 set_free_with_pinuse(p
, psize
, next
);
1307 if (is_small(psize
)) {
1308 insert_small_chunk(fm
, p
, psize
);
1310 tchunkptr tp
= (tchunkptr
)p
;
1311 insert_large_chunk(fm
, tp
, psize
);
1312 if (--fm
->release_checks
== 0)
1313 release_unused_segments(fm
);
1319 static LJ_NOINLINE
void *lj_alloc_realloc(void *msp
, void *ptr
, size_t nsize
)
1321 if (nsize
>= MAX_REQUEST
) {
1324 mstate m
= (mstate
)msp
;
1325 mchunkptr oldp
= mem2chunk(ptr
);
1326 size_t oldsize
= chunksize(oldp
);
1327 mchunkptr next
= chunk_plus_offset(oldp
, oldsize
);
1329 size_t nb
= request2size(nsize
);
1331 /* Try to either shrink or extend into top. Else malloc-copy-free */
1332 if (is_direct(oldp
)) {
1333 newp
= direct_resize(oldp
, nb
); /* this may return NULL. */
1334 } else if (oldsize
>= nb
) { /* already big enough */
1335 size_t rsize
= oldsize
- nb
;
1337 if (rsize
>= MIN_CHUNK_SIZE
) {
1338 mchunkptr rem
= chunk_plus_offset(newp
, nb
);
1339 set_inuse(m
, newp
, nb
);
1340 set_inuse(m
, rem
, rsize
);
1341 lj_alloc_free(m
, chunk2mem(rem
));
1343 } else if (next
== m
->top
&& oldsize
+ m
->topsize
> nb
) {
1344 /* Expand into top */
1345 size_t newsize
= oldsize
+ m
->topsize
;
1346 size_t newtopsize
= newsize
- nb
;
1347 mchunkptr newtop
= chunk_plus_offset(oldp
, nb
);
1348 set_inuse(m
, oldp
, nb
);
1349 newtop
->head
= newtopsize
|PINUSE_BIT
;
1351 m
->topsize
= newtopsize
;
1356 return chunk2mem(newp
);
1358 void *newmem
= lj_alloc_malloc(m
, nsize
);
1360 size_t oc
= oldsize
- overhead_for(oldp
);
1361 memcpy(newmem
, ptr
, oc
< nsize
? oc
: nsize
);
1362 lj_alloc_free(m
, ptr
);
1369 void *lj_alloc_f(void *msp
, void *ptr
, size_t osize
, size_t nsize
)
1373 return lj_alloc_free(msp
, ptr
);
1374 } else if (ptr
== NULL
) {
1375 return lj_alloc_malloc(msp
, nsize
);
1377 return lj_alloc_realloc(msp
, ptr
, nsize
);