Heap related structures

发布于 2019-10-27  55 次阅读


chunk

Overview

  • malloced chunk
  • free chunk
    • free chunk in tcache
    • free chunk in fast bins
    • free chunk in unsorted bin & small bins
    • free chunk in large bins
  • top chunk
  • last remainder chunk
struct malloc_chunk {
  INTERNAL_SIZE_T      prev_size;  /* Size of previous chunk (if free).  */
  INTERNAL_SIZE_T      size;       /* Size in bytes, including overhead. */
  struct malloc_chunk* fd;         /* double links -- used only if free. */
  struct malloc_chunk* bk;
  /* Only used for large blocks: pointer to next larger size.  */
  struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */
  struct malloc_chunk* bk_nextsize;
};

malloced chunk

free chunk

free chunk in fast bins

free chunk in unsorted bin & small bins

free chunk in large bins

top chunk

last remainder chunk

bins and tcache

arena

malloc_state : record anrena's information

struct malloc_state {
mutex_t mutex; // mutex lock
int flags; // records some characteristics of current arena
mfastbinptr fastbins[NFASTBINS]; // fast bins
mchunkptr top; // points to top chunk
mchunkptr last_remainder; // points to last remainder chunk
mchunkptr bins[BINS * 2]; // bins : unsorted bin + small bins + large bins. 32bits: &main_arena + 48 = bins ; 64bits: &main_arena + 88 = bins
unsigned int binmap[BINMAPSIZE]; // whether one of bins is empty
struct malloc_state *next; // points to next malloc_state
INTERNAL_SIZE_T system_mem; // tracks the sum of memory allocated by system
INTERNAL_SIZE_T max_system_mem;
}