glibc  2.34
About: The GNU C Library ("system calls" and other basic facilities such as open, malloc, printf, exit ...).
  Fossies Dox: glibc-2.34.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

malloc.c File Reference
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <atomic.h>
#include <_itoa.h>
#include <bits/wordsize.h>
#include <sys/sysinfo.h>
#include <ldsodefs.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <shlib-compat.h>
#include <stdint.h>
#include <stdarg.h>
#include <sys/param.h>
#include <libc-pointer-arith.h>
#include <libc-diag.h>
#include <libc-mtag.h>
#include <malloc/malloc-internal.h>
#include <sysdep-cancel.h>
#include <libc-internal.h>
#include <random-bits.h>
#include <sys/random.h>
#include "morecore.c"
#include <string.h>
#include <malloc.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stap-probe.h>
#include "arena.c"
Include dependency graph for malloc.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  malloc_chunk
 
struct  malloc_state
 
struct  malloc_par
 

Macros

#define void   void
  More...
 
#define MALLOC_DEBUG   0
 
#define PROTECT_PTR(pos, ptr)    ((__typeof (ptr)) ((((size_t) pos) >> 12) ^ ((size_t) ptr)))
 
#define REVEAL_PTR(ptr)   PROTECT_PTR (&ptr, ptr)
 
#define REALLOC_ZERO_BYTES_FREES   1
 
#define TRIM_FASTBINS   0
 
#define MORECORE   (*__glibc_morecore)
 
#define MORECORE_FAILURE   0
 
#define mtag_enabled   false
 
#define mtag_mmap_flags   0
 
#define MORECORE_CONTIGUOUS   1
 
#define MORECORE_CLEARS   1
 
#define MMAP_AS_MORECORE_SIZE   (1024 * 1024)
 
#define HAVE_MREMAP   0
 
#define M_MXFAST   1
 
#define DEFAULT_MXFAST   (64 * SIZE_SZ / 4)
 
#define M_TRIM_THRESHOLD   -1
 
#define DEFAULT_TRIM_THRESHOLD   (128 * 1024)
 
#define M_TOP_PAD   -2
 
#define DEFAULT_MMAP_THRESHOLD_MIN   (128 * 1024)
 
#define DEFAULT_MMAP_THRESHOLD_MAX   (512 * 1024)
 
#define M_MMAP_THRESHOLD   -3
 
#define DEFAULT_MMAP_THRESHOLD   DEFAULT_MMAP_THRESHOLD_MIN
 
#define M_MMAP_MAX   -4
 
#define DEFAULT_MMAP_MAX   (65536)
 
#define MAP_NORESERVE   0
 
#define MMAP(addr, size, prot, flags)    __mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0)
 
#define CHUNK_HDR_SZ   (2 * SIZE_SZ)
 
#define chunk2mem(p)   ((void*)((char*)(p) + CHUNK_HDR_SZ))
 
#define chunk2mem_tag(p)   ((void*)tag_at ((char*)(p) + CHUNK_HDR_SZ))
 
#define mem2chunk(mem)   ((mchunkptr)tag_at (((char*)(mem) - CHUNK_HDR_SZ)))
 
#define MIN_CHUNK_SIZE   (offsetof(struct malloc_chunk, fd_nextsize))
 
#define MINSIZE    (unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK))
 
#define aligned_OK(m)   (((unsigned long)(m) & MALLOC_ALIGN_MASK) == 0)
 
#define misaligned_chunk(p)
  More...
 
#define request2size(req)
  More...
 
#define PREV_INUSE   0x1
 
#define prev_inuse(p)   ((p)->mchunk_size & PREV_INUSE)
 
#define IS_MMAPPED   0x2
 
#define chunk_is_mmapped(p)   ((p)->mchunk_size & IS_MMAPPED)
 
#define NON_MAIN_ARENA   0x4
 
#define chunk_main_arena(p)   (((p)->mchunk_size & NON_MAIN_ARENA) == 0)
 
#define set_non_main_arena(p)   ((p)->mchunk_size |= NON_MAIN_ARENA)
 
#define SIZE_BITS   (PREV_INUSE | IS_MMAPPED | NON_MAIN_ARENA)
 
#define chunksize(p)   (chunksize_nomask (p) & ~(SIZE_BITS))
 
#define chunksize_nomask(p)   ((p)->mchunk_size)
 
#define next_chunk(p)   ((mchunkptr) (((char *) (p)) + chunksize (p)))
 
#define prev_size(p)   ((p)->mchunk_prev_size)
 
#define set_prev_size(p, sz)   ((p)->mchunk_prev_size = (sz))
 
#define prev_chunk(p)   ((mchunkptr) (((char *) (p)) - prev_size (p)))
 
#define chunk_at_offset(p, s)   ((mchunkptr) (((char *) (p)) + (s)))
 
#define inuse(p)    ((((mchunkptr) (((char *) (p)) + chunksize (p)))->mchunk_size) & PREV_INUSE)
 
#define set_inuse(p)    ((mchunkptr) (((char *) (p)) + chunksize (p)))->mchunk_size |= PREV_INUSE
 
#define clear_inuse(p)    ((mchunkptr) (((char *) (p)) + chunksize (p)))->mchunk_size &= ~(PREV_INUSE)
 
#define inuse_bit_at_offset(p, s)    (((mchunkptr) (((char *) (p)) + (s)))->mchunk_size & PREV_INUSE)
 
#define set_inuse_bit_at_offset(p, s)    (((mchunkptr) (((char *) (p)) + (s)))->mchunk_size |= PREV_INUSE)
 
#define clear_inuse_bit_at_offset(p, s)    (((mchunkptr) (((char *) (p)) + (s)))->mchunk_size &= ~(PREV_INUSE))
 
#define set_head_size(p, s)   ((p)->mchunk_size = (((p)->mchunk_size & SIZE_BITS) | (s)))
 
#define set_head(p, s)   ((p)->mchunk_size = (s))
 
#define set_foot(p, s)   (((mchunkptr) ((char *) (p) + (s)))->mchunk_prev_size = (s))
 
#define memsize(p)
  More...
 
#define bin_at(m, i)
  More...
 
#define next_bin(b)   ((mbinptr) ((char *) (b) + (sizeof (mchunkptr) << 1)))
 
#define first(b)   ((b)->fd)
 
#define last(b)   ((b)->bk)
 
#define NBINS   128
 
#define NSMALLBINS   64
 
#define SMALLBIN_WIDTH   MALLOC_ALIGNMENT
 
#define SMALLBIN_CORRECTION   (MALLOC_ALIGNMENT > CHUNK_HDR_SZ)
 
#define MIN_LARGE_SIZE   ((NSMALLBINS - SMALLBIN_CORRECTION) * SMALLBIN_WIDTH)
 
#define in_smallbin_range(sz)    ((unsigned long) (sz) < (unsigned long) MIN_LARGE_SIZE)
 
#define smallbin_index(sz)
  More...
 
#define largebin_index_32(sz)
  More...
 
#define largebin_index_32_big(sz)
  More...
 
#define largebin_index_64(sz)
  More...
 
#define largebin_index(sz)
  More...
 
#define bin_index(sz)    ((in_smallbin_range (sz)) ? smallbin_index (sz) : largebin_index (sz))
 
#define unsorted_chunks(M)   (bin_at (M, 1))
 
#define initial_top(M)   (unsorted_chunks (M))
 
#define BINMAPSHIFT   5
 
#define BITSPERMAP   (1U << BINMAPSHIFT)
 
#define BINMAPSIZE   (NBINS / BITSPERMAP)
 
#define idx2block(i)   ((i) >> BINMAPSHIFT)
 
#define idx2bit(i)   ((1U << ((i) & ((1U << BINMAPSHIFT) - 1))))
 
#define mark_bin(m, i)   ((m)->binmap[idx2block (i)] |= idx2bit (i))
 
#define unmark_bin(m, i)   ((m)->binmap[idx2block (i)] &= ~(idx2bit (i)))
 
#define get_binmap(m, i)   ((m)->binmap[idx2block (i)] & idx2bit (i))
 
#define fastbin(ar_ptr, idx)   ((ar_ptr)->fastbinsY[idx])
 
#define fastbin_index(sz)    ((((unsigned int) (sz)) >> (SIZE_SZ == 8 ? 4 : 3)) - 2)
 
#define MAX_FAST_SIZE   (80 * SIZE_SZ / 4)
 
#define NFASTBINS   (fastbin_index (request2size (MAX_FAST_SIZE)) + 1)
 
#define FASTBIN_CONSOLIDATION_THRESHOLD   (65536UL)
 
#define NONCONTIGUOUS_BIT   (2U)
 
#define contiguous(M)   (((M)->flags & NONCONTIGUOUS_BIT) == 0)
 
#define noncontiguous(M)   (((M)->flags & NONCONTIGUOUS_BIT) != 0)
 
#define set_noncontiguous(M)   ((M)->flags |= NONCONTIGUOUS_BIT)
 
#define set_contiguous(M)   ((M)->flags &= ~NONCONTIGUOUS_BIT)
 
#define set_max_fast(s)
  More...
 
#define NARENAS_FROM_NCORES(n)   ((n) * (sizeof (long) == 4 ? 2 : 8))
 
#define check_chunk(A, P)
 
#define check_free_chunk(A, P)
 
#define check_inuse_chunk(A, P)
 
#define check_remalloced_chunk(A, P, N)
 
#define check_malloced_chunk(A, P, N)
 
#define check_malloc_state(A)
 
#define MAYBE_INIT_TCACHE()
 
#define REMOVE_FB(fb, victim, pp)
  More...
 
#define MAX_ITERS   10000
 
#define nsizes   (sizeof (sizes) / sizeof (sizes[0]))
 

Typedefs

typedef struct malloc_chunkmchunkptr
 
typedef struct malloc_chunkmbinptr
 
typedef struct malloc_chunkmfastbinptr
 

Functions

static voidtag_region (void *ptr, size_t size)
  More...
 
static voidtag_new_zero_region (void *ptr, size_t size)
  More...
 
static voidtag_new_usable (void *ptr)
  More...
 
static voidtag_at (void *ptr)
  More...
 
int __libc_mallopt (int, int)
  More...
 
static void_int_malloc (mstate, size_t)
  More...
 
static void _int_free (mstate, mchunkptr, int)
  More...
 
static void_int_realloc (mstate, mchunkptr, size_t, size_t)
  More...
 
static void_int_memalign (mstate, size_t, size_t)
  More...
 
static void malloc_printerr (const char *str)
  More...
 
static void munmap_chunk (mchunkptr p)
  More...
 
static bool checked_request2size (size_t req, size_t *sz)
  More...
 
int(* __Static_assert_function (void))[!!sizeof(struct { int __error_if_negative:(__MTAG_GRANULE_SIZE<=(2 *(sizeof(size_t)))) ? 2 :-1;})]
 
static void unlink_chunk (mstate av, mchunkptr p)
  More...
 
static size_t get_max_fast (void)
  More...
 
static void malloc_init_state (mstate av)
  More...
 
static voidsysmalloc (size_t, mstate)
  More...
 
static int systrim (size_t, mstate)
  More...
 
static void malloc_consolidate (mstate)
  More...
 
static void tcache_thread_shutdown (void)
  More...
 
static void alloc_perturb (char *p, size_t n)
  More...
 
static void free_perturb (char *p, size_t n)
  More...
 
static int mtrim (mstate av, size_t pad)
  More...
 
int __malloc_trim (size_t s)
  More...
 
static size_t musable (void *mem)
  More...
 
static void int_mallinfo (mstate av, struct mallinfo2 *m)
  More...
 
struct mallinfo2 __libc_mallinfo2 (void)
 
struct mallinfo __libc_mallinfo (void)
  More...
 
void __malloc_stats (void)
  More...
 
static int do_set_trim_threshold (size_t value)
  More...
 
static int do_set_top_pad (size_t value)
  More...
 
static int do_set_mmap_threshold (size_t value)
  More...
 
static int do_set_mmaps_max (int32_t value)
  More...
 
static int do_set_mallopt_check (int32_t value)
  More...
 
static int do_set_perturb_byte (int32_t value)
  More...
 
static int do_set_arena_test (size_t value)
  More...
 
static int do_set_arena_max (size_t value)
  More...
 
static int do_set_mxfast (size_t value)
  More...
 
int __malloc_info (int options, FILE *fp)
  More...
 

Variables

static size_t global_max_fast
  More...
 
static struct malloc_state main_arena
  More...
 
static struct malloc_par mp_
  More...
 
static int perturb_byte
  More...
 
char ** __libc_argv
 

Macro Definition Documentation

◆ bin_at

#define bin_at (   m,
  i 
)
Value:
(mbinptr) (((char *) &((m)->bins[((i) - 1) * 2])) \
- offsetof (struct malloc_chunk, fd))
static pthread_mutex_t m
struct malloc_chunk * mbinptr
Definition: malloc.c:1539
static int fd
Definition: pcprofile.c:30
int i
Definition: tst-tls14.c:0

Definition at line 1542 of file malloc.c.

◆ largebin_index

#define largebin_index (   sz)
Value:
(SIZE_SZ == 8 ? largebin_index_64 (sz) \
#define MALLOC_ALIGNMENT
#define SIZE_SZ
Definition: malloc-size.h:57
#define largebin_index_64(sz)
Definition: malloc.c:1609
#define largebin_index_32(sz)
Definition: malloc.c:1590
#define largebin_index_32_big(sz)
Definition: malloc.c:1598

Definition at line 1617 of file malloc.c.

◆ largebin_index_32

#define largebin_index_32 (   sz)
Value:
(((((unsigned long) (sz)) >> 6) <= 38) ? 56 + (((unsigned long) (sz)) >> 6) :\
((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\
((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\
((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\
((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\
126)

Definition at line 1590 of file malloc.c.

◆ largebin_index_32_big

#define largebin_index_32_big (   sz)
Value:
(((((unsigned long) (sz)) >> 6) <= 45) ? 49 + (((unsigned long) (sz)) >> 6) :\
((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\
((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\
((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\
((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\
126)

Definition at line 1598 of file malloc.c.

◆ largebin_index_64

#define largebin_index_64 (   sz)
Value:
(((((unsigned long) (sz)) >> 6) <= 48) ? 48 + (((unsigned long) (sz)) >> 6) :\
((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\
((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\
((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\
((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\
126)

Definition at line 1609 of file malloc.c.

◆ memsize

#define memsize (   p)
Value:
chunksize (p) - CHUNK_HDR_SZ : \
chunksize (p) - CHUNK_HDR_SZ + (chunk_is_mmapped (p) ? 0 : SIZE_SZ))
#define __MTAG_GRANULE_SIZE
Definition: libc-mtag.h:26
#define mtag_enabled
Definition: malloc.c:455
#define chunk_is_mmapped(p)
Definition: malloc.c:1383
#define CHUNK_HDR_SZ
Definition: malloc.c:1301
#define __glibc_unlikely(cond)
Definition: cdefs.h:453
volatile char * p
Definition: tst-chk1.c:89

Definition at line 1466 of file malloc.c.

◆ misaligned_chunk

#define misaligned_chunk (   p)
Value:
#define MALLOC_ALIGN_MASK
Definition: malloc-size.h:62
#define chunk2mem(p)
Definition: malloc.c:1305
unsigned long int uintptr_t
Definition: stdint.h:90

Definition at line 1325 of file malloc.c.

◆ REMOVE_FB

#define REMOVE_FB (   fb,
  victim,
  pp 
)
Value:
do \
{ \
victim = pp; \
if (victim == NULL) \
break; \
pp = REVEAL_PTR (victim->fd); \
malloc_printerr ("malloc(): unaligned fastbin chunk detected"); \
} \
while ((pp = catomic_compare_and_exchange_val_acq (fb, pp, victim)) \
!= victim); \
#define catomic_compare_and_exchange_val_acq(mem, newval, oldval)
Definition: atomic.h:103
#define NULL
Definition: gmp-impl.h:61
#define misaligned_chunk(p)
Definition: malloc.c:1325
#define REVEAL_PTR(ptr)
Definition: malloc.c:352
static const double pp[]
Definition: s_erf.c:135

◆ request2size

#define request2size (   req)
Value:
MINSIZE : \
((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~~MALLOC_ALIGN_MASK)
#define MINSIZE
Definition: malloc.c:1318

Definition at line 1332 of file malloc.c.

◆ set_max_fast

#define set_max_fast (   s)
Value:
? MIN_CHUNK_SIZE / 2 : ((s + SIZE_SZ) & ~~MALLOC_ALIGN_MASK))
#define s(name)
Definition: e_atan2.c:31
static size_t global_max_fast
Definition: malloc.c:1787
#define MIN_CHUNK_SIZE
Definition: malloc.c:1314
#define size_t
Definition: mallocbug.c:6

Definition at line 1798 of file malloc.c.

◆ smallbin_index

#define smallbin_index (   sz)
Value:
((SMALLBIN_WIDTH == 16 ? (((unsigned) (sz)) >> 4) : (((unsigned) (sz)) >> 3))\
#define SMALLBIN_WIDTH
Definition: malloc.c:1579
#define SMALLBIN_CORRECTION
Definition: malloc.c:1580

Definition at line 1586 of file malloc.c.

◆ void

__fortify_function void   void

Definition at line 209 of file malloc.c.

Referenced by _int_realloc().

Variable Documentation

◆ global_max_fast

size_t global_max_fast
static

Definition at line 1787 of file malloc.c.

Referenced by get_max_fast().

◆ main_arena

◆ mp_

struct malloc_par mp_
static
Initial value:
=
{
.top_pad = 131072 ,
.n_mmaps_max = (65536) ,
.mmap_threshold = (128 * 1024) ,
.trim_threshold = (128 * 1024) ,
.arena_test = (( 1 ) * (sizeof (long) == 4 ? 2 : 8))
}
#define NARENAS_FROM_NCORES(n)
unsigned long mmap_threshold
size_t n
Definition: tst_swprintf.c:10

Definition at line 1803 of file malloc.c.

Referenced by __libc_mallinfo(), __malloc_info(), __malloc_stats(), _int_free(), _int_malloc(), do_set_arena_max(), do_set_arena_test(), do_set_mmap_threshold(), do_set_mmaps_max(), do_set_top_pad(), do_set_trim_threshold(), heap_trim(), mem2chunk_check(), munmap_chunk(), sysmalloc(), and top_check().

◆ perturb_byte

int perturb_byte
static

Definition at line 1992 of file malloc.c.

Referenced by alloc_perturb(), do_set_perturb_byte(), and free_perturb().