Rizin
unix-like reverse engineering framework and cli tools
malloc.c File Reference
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h>
#include "libc.h"
#include "atomic.h"
#include "pthread_impl.h"

Go to the source code of this file.

Classes

struct  chunk
 
struct  bin
 

Macros

#define _GNU_SOURCE
 
#define SIZE_ALIGN   (4*sizeof(size_t))
 
#define SIZE_MASK   (-SIZE_ALIGN)
 
#define OVERHEAD   (2*sizeof(size_t))
 
#define MMAP_THRESHOLD   (0x1c00*SIZE_ALIGN)
 
#define DONTCARE   16
 
#define RECLAIM   163840
 
#define CHUNK_SIZE(c)   ((c)->csize & -2)
 
#define CHUNK_PSIZE(c)   ((c)->psize & -2)
 
#define PREV_CHUNK(c)   ((struct chunk *)((char *)(c) - CHUNK_PSIZE(c)))
 
#define NEXT_CHUNK(c)   ((struct chunk *)((char *)(c) + CHUNK_SIZE(c)))
 
#define MEM_TO_CHUNK(p)   (struct chunk *)((char *)(p) - OVERHEAD)
 
#define CHUNK_TO_MEM(c)   (void *)((char *)(c) + OVERHEAD)
 
#define BIN_TO_CHUNK(i)   (MEM_TO_CHUNK(&mal.bins[i].head))
 
#define C_INUSE   ((size_t)1)
 
#define IS_MMAPPED(c)   !((c)->csize & (C_INUSE))
 

Functions

void * __mmap (void *, size_t, int, int, int, off_t)
 
int __munmap (void *, size_t)
 
void * __mremap (void *, size_t, size_t, int,...)
 
int __madvise (void *, size_t, int)
 
static void lock (volatile int *lk)
 
static void unlock (volatile int *lk)
 
static void lock_bin (int i)
 
static void unlock_bin (int i)
 
static int first_set (uint64_t x)
 
static int bin_index (size_t x)
 
static int bin_index_up (size_t x)
 
void * __expand_heap (size_t *)
 
static struct chunkexpand_heap (size_t n)
 
static int adjust_size (size_t *n)
 
static void unbin (struct chunk *c, int i)
 
static int alloc_fwd (struct chunk *c)
 
static int alloc_rev (struct chunk *c)
 
static int pretrim (struct chunk *self, size_t n, int i, int j)
 
static void trim (struct chunk *self, size_t n)
 
void * malloc (size_t n)
 
void * __malloc0 (size_t n)
 
void * realloc (void *p, size_t n)
 
void free (void *p)
 

Variables

struct {
   volatile uint64_t   binmap
 
   struct bin   bins [64]
 
   volatile int   free_lock [2]
 
mal
 
static const unsigned char bin_tab [60]
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 1 of file malloc.c.

◆ BIN_TO_CHUNK

#define BIN_TO_CHUNK (   i)    (MEM_TO_CHUNK(&mal.bins[i].head))

Definition at line 52 of file malloc.c.

◆ C_INUSE

#define C_INUSE   ((size_t)1)

Definition at line 54 of file malloc.c.

◆ CHUNK_PSIZE

#define CHUNK_PSIZE (   c)    ((c)->psize & -2)

Definition at line 47 of file malloc.c.

◆ CHUNK_SIZE

#define CHUNK_SIZE (   c)    ((c)->csize & -2)

Definition at line 46 of file malloc.c.

◆ CHUNK_TO_MEM

#define CHUNK_TO_MEM (   c)    (void *)((char *)(c) + OVERHEAD)

Definition at line 51 of file malloc.c.

◆ DONTCARE

#define DONTCARE   16

Definition at line 43 of file malloc.c.

◆ IS_MMAPPED

#define IS_MMAPPED (   c)    !((c)->csize & (C_INUSE))

Definition at line 56 of file malloc.c.

◆ MEM_TO_CHUNK

#define MEM_TO_CHUNK (   p)    (struct chunk *)((char *)(p) - OVERHEAD)

Definition at line 50 of file malloc.c.

◆ MMAP_THRESHOLD

#define MMAP_THRESHOLD   (0x1c00*SIZE_ALIGN)

Definition at line 42 of file malloc.c.

◆ NEXT_CHUNK

#define NEXT_CHUNK (   c)    ((struct chunk *)((char *)(c) + CHUNK_SIZE(c)))

Definition at line 49 of file malloc.c.

◆ OVERHEAD

#define OVERHEAD   (2*sizeof(size_t))

Definition at line 41 of file malloc.c.

◆ PREV_CHUNK

#define PREV_CHUNK (   c)    ((struct chunk *)((char *)(c) - CHUNK_PSIZE(c)))

Definition at line 48 of file malloc.c.

◆ RECLAIM

#define RECLAIM   163840

Definition at line 44 of file malloc.c.

◆ SIZE_ALIGN

#define SIZE_ALIGN   (4*sizeof(size_t))

Definition at line 39 of file malloc.c.

◆ SIZE_MASK

#define SIZE_MASK   (-SIZE_ALIGN)

Definition at line 40 of file malloc.c.

Function Documentation

◆ __expand_heap()

void* __expand_heap ( size_t )

Referenced by expand_heap().

◆ __madvise()

int __madvise ( void *  ,
size_t  ,
int   
)

Referenced by free().

◆ __malloc0()

void* __malloc0 ( size_t  n)

Definition at line 369 of file malloc.c.

370 {
371  void *p = malloc(n);
372  if (p && !IS_MMAPPED(MEM_TO_CHUNK(p))) {
373  size_t *z;
374  n = (n + sizeof *z - 1)/sizeof *z;
375  for (z=p; n; n--, z++) if (*z) *z=0;
376  }
377  return p;
378 }
void * p
Definition: libc.cpp:67
void * malloc(size_t size)
Definition: malloc.c:123
int n
Definition: mipsasm.c:19
#define MEM_TO_CHUNK(p)
Definition: malloc.c:50
#define IS_MMAPPED(c)
Definition: malloc.c:56

References IS_MMAPPED, malloc(), MEM_TO_CHUNK, n, and p.

◆ __mmap()

void* __mmap ( void *  ,
size_t  ,
int  ,
int  ,
int  ,
off_t   
)

Referenced by free(), and malloc().

◆ __mremap()

void* __mremap ( void *  ,
size_t  ,
size_t  ,
int  ,
  ... 
)

Referenced by realloc().

◆ __munmap()

int __munmap ( void *  ,
size_t   
)

Referenced by free().

◆ adjust_size()

static int adjust_size ( size_t n)
static

Definition at line 208 of file malloc.c.

209 {
210  /* Result of pointer difference must fit in ptrdiff_t. */
211  if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
212  if (*n) {
213  errno = ENOMEM;
214  return -1;
215  } else {
216  *n = SIZE_ALIGN;
217  return 0;
218  }
219  }
220  *n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK;
221  return 0;
222 }
#define ENOMEM
Definition: sftypes.h:122
#define PTRDIFF_MAX
#define SIZE_MASK
Definition: malloc.c:40
#define OVERHEAD
Definition: malloc.c:41
#define SIZE_ALIGN
Definition: malloc.c:39

References ENOMEM, n, OVERHEAD, PTRDIFF_MAX, SIZE_ALIGN, and SIZE_MASK.

Referenced by malloc(), and realloc().

◆ alloc_fwd()

static int alloc_fwd ( struct chunk c)
static

Definition at line 234 of file malloc.c.

235 {
236  int i;
237  size_t k;
238  while (!((k=c->csize) & C_INUSE)) {
239  i = bin_index(k);
240  lock_bin(i);
241  if (c->csize == k) {
242  unbin(c, i);
243  unlock_bin(i);
244  return 1;
245  }
246  unlock_bin(i);
247  }
248  return 0;
249 }
lzma_index ** i
Definition: index.h:629
const char * k
Definition: dsignal.c:11
#define c(i)
Definition: sha256.c:43
static void unlock_bin(int i)
Definition: malloc.c:82
#define C_INUSE
Definition: malloc.c:54
static int bin_index(size_t x)
Definition: malloc.c:121
static void unbin(struct chunk *c, int i)
Definition: malloc.c:224
static void lock_bin(int i)
Definition: malloc.c:75

References bin_index(), c, C_INUSE, i, k, lock_bin(), unbin(), and unlock_bin().

Referenced by free(), and realloc().

◆ alloc_rev()

static int alloc_rev ( struct chunk c)
static

Definition at line 251 of file malloc.c.

252 {
253  int i;
254  size_t k;
255  while (!((k=c->psize) & C_INUSE)) {
256  i = bin_index(k);
257  lock_bin(i);
258  if (c->psize == k) {
259  unbin(PREV_CHUNK(c), i);
260  unlock_bin(i);
261  return 1;
262  }
263  unlock_bin(i);
264  }
265  return 0;
266 }
#define PREV_CHUNK(c)
Definition: malloc.c:48

References bin_index(), c, C_INUSE, i, k, lock_bin(), PREV_CHUNK, unbin(), and unlock_bin().

Referenced by free(), malloc(), and realloc().

◆ bin_index()

static int bin_index ( size_t  x)
static

Definition at line 121 of file malloc.c.

122 {
123  x = x / SIZE_ALIGN - 1;
124  if (x <= 32) return x;
125  if (x < 512) return bin_tab[x/8-4];
126  if (x > 0x1c00) return 63;
127  return bin_tab[x/128-4] + 16;
128 }
int x
Definition: mipsasm.c:20
static const unsigned char bin_tab[60]
Definition: malloc.c:114

References bin_tab, SIZE_ALIGN, and x.

Referenced by alloc_fwd(), alloc_rev(), free(), and pretrim().

◆ bin_index_up()

static int bin_index_up ( size_t  x)
static

Definition at line 130 of file malloc.c.

131 {
132  x = x / SIZE_ALIGN - 1;
133  if (x <= 32) return x;
134  x--;
135  if (x < 512) return bin_tab[x/8-4] + 1;
136  return bin_tab[x/128-4] + 17;
137 }

References bin_tab, SIZE_ALIGN, and x.

Referenced by malloc().

◆ expand_heap()

static struct chunk* expand_heap ( size_t  n)
static

Definition at line 162 of file malloc.c.

163 {
164  static int heap_lock[2];
165  static void *end;
166  void *p;
167  struct chunk *w;
168 
169  /* The argument n already accounts for the caller's chunk
170  * overhead needs, but if the heap can't be extended in-place,
171  * we need room for an extra zero-sized sentinel chunk. */
172  n += SIZE_ALIGN;
173 
174  lock(heap_lock);
175 
176  p = __expand_heap(&n);
177  if (!p) {
178  unlock(heap_lock);
179  return 0;
180  }
181 
182  /* If not just expanding existing space, we need to make a
183  * new sentinel chunk below the allocated space. */
184  if (p != end) {
185  /* Valid/safe because of the prologue increment. */
186  n -= SIZE_ALIGN;
187  p = (char *)p + SIZE_ALIGN;
188  w = MEM_TO_CHUNK(p);
189  w->psize = 0 | C_INUSE;
190  }
191 
192  /* Record new heap end and fill in footer. */
193  end = (char *)p + n;
194  w = MEM_TO_CHUNK(end);
195  w->psize = n | C_INUSE;
196  w->csize = 0 | C_INUSE;
197 
198  /* Fill in header, which may be new or may be replacing a
199  * zero-size sentinel header at the old end-of-heap. */
200  w = MEM_TO_CHUNK(p);
201  w->csize = n | C_INUSE;
202 
203  unlock(heap_lock);
204 
205  return w;
206 }
#define w
Definition: crypto_rc6.c:13
Definition: malloc.c:21
static void unlock(volatile int *lk)
Definition: malloc.c:67
void * __expand_heap(size_t *)
static void lock(volatile int *lk)
Definition: malloc.c:61

References __expand_heap(), C_INUSE, test_evm::end, lock(), MEM_TO_CHUNK, n, p, SIZE_ALIGN, unlock(), and w.

Referenced by malloc().

◆ first_set()

static int first_set ( uint64_t  x)
static

Definition at line 87 of file malloc.c.

88 {
89 #if 1
90  return a_ctz_64(x);
91 #else
92  static const char debruijn64[64] = {
93  0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
94  62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
95  63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
96  51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
97  };
98  static const char debruijn32[32] = {
99  0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13,
100  31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14
101  };
102  if (sizeof(long) < 8) {
103  uint32_t y = x;
104  if (!y) {
105  y = x>>32;
106  return 32 + debruijn32[(y&-y)*0x076be629 >> 27];
107  }
108  return debruijn32[(y&-y)*0x076be629 >> 27];
109  }
110  return debruijn64[(x&-x)*0x022fdd63cc95386dull >> 58];
111 #endif
112 }
unsigned int uint32_t
Definition: sftypes.h:29

References x.

Referenced by malloc().

◆ free()

void free ( void *  p)

Definition at line 451 of file malloc.c.

452 {
453  struct chunk *self = MEM_TO_CHUNK(p);
454  struct chunk *next;
455  size_t final_size, new_size, size;
456  int reclaim=0;
457  int i;
458 
459  if (!p) return;
460 
461  if (IS_MMAPPED(self)) {
462  size_t extra = self->psize;
463  char *base = (char *)self - extra;
464  size_t len = CHUNK_SIZE(self) + extra;
465  /* Crash on double free */
466  if (extra & 1) a_crash();
467  __munmap(base, len);
468  return;
469  }
470 
471  final_size = new_size = CHUNK_SIZE(self);
472  next = NEXT_CHUNK(self);
473 
474  /* Crash on corrupted footer (likely from buffer overflow) */
475  if (next->psize != self->csize) a_crash();
476 
477  for (;;) {
478  if (self->psize & next->csize & C_INUSE) {
479  self->csize = final_size | C_INUSE;
480  next->psize = final_size | C_INUSE;
481  i = bin_index(final_size);
482  lock_bin(i);
483  lock(mal.free_lock);
484  if (self->psize & next->csize & C_INUSE)
485  break;
486  unlock(mal.free_lock);
487  unlock_bin(i);
488  }
489 
490  if (alloc_rev(self)) {
491  self = PREV_CHUNK(self);
492  size = CHUNK_SIZE(self);
493  final_size += size;
494  if (new_size+size > RECLAIM && (new_size+size^size) > size)
495  reclaim = 1;
496  }
497 
498  if (alloc_fwd(next)) {
499  size = CHUNK_SIZE(next);
500  final_size += size;
501  if (new_size+size > RECLAIM && (new_size+size^size) > size)
502  reclaim = 1;
503  next = NEXT_CHUNK(next);
504  }
505  }
506 
507  if (!(mal.binmap & 1ULL<<i))
508  a_or_64(&mal.binmap, 1ULL<<i);
509 
510  self->csize = final_size;
511  next->psize = final_size;
512  unlock(mal.free_lock);
513 
514  self->next = BIN_TO_CHUNK(i);
515  self->prev = mal.bins[i].tail;
516  self->next->prev = self;
517  self->prev->next = self;
518 
519  /* Replace middle of large chunks with fresh zero pages */
520  if (reclaim) {
521  uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE;
522  uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE;
523 #if 1
524  __madvise((void *)a, b-a, MADV_DONTNEED);
525 #else
526  __mmap((void *)a, b-a, PROT_READ|PROT_WRITE,
528 #endif
529  }
530 
531  unlock_bin(i);
532 }
size_t len
Definition: 6502dis.c:15
voidpf void uLong size
Definition: ioapi.h:138
#define PROT_READ
Definition: sftypes.h:95
#define PROT_WRITE
Definition: sftypes.h:96
#define MAP_FIXED
Definition: sftypes.h:104
#define MAP_PRIVATE
Definition: sftypes.h:102
#define MAP_ANONYMOUS
Definition: sftypes.h:106
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41
_W64 unsigned int uintptr_t
size_t csize
Definition: malloc.c:22
size_t psize
Definition: malloc.c:22
struct chunk * next
Definition: malloc.c:23
static struct @626 mal
int __munmap(void *, size_t)
#define CHUNK_SIZE(c)
Definition: malloc.c:46
#define NEXT_CHUNK(c)
Definition: malloc.c:49
static int alloc_rev(struct chunk *c)
Definition: malloc.c:251
static int alloc_fwd(struct chunk *c)
Definition: malloc.c:234
#define RECLAIM
Definition: malloc.c:44
int __madvise(void *, size_t, int)
void * __mmap(void *, size_t, int, int, int, off_t)
#define BIN_TO_CHUNK(i)
Definition: malloc.c:52

References __madvise(), __mmap(), __munmap(), a, alloc_fwd(), alloc_rev(), b, bin_index(), BIN_TO_CHUNK, C_INUSE, CHUNK_SIZE, chunk::csize, i, IS_MMAPPED, len, lock(), lock_bin(), mal, MAP_ANONYMOUS, MAP_FIXED, MAP_PRIVATE, MEM_TO_CHUNK, chunk::next, NEXT_CHUNK, p, PREV_CHUNK, PROT_READ, PROT_WRITE, chunk::psize, RECLAIM, SIZE_ALIGN, unlock(), and unlock_bin().

Referenced by realloc(), and trim().

◆ lock()

static void lock ( volatile int lk)
inlinestatic

Definition at line 61 of file malloc.c.

62 {
63  if (libc.threads_minus_1)
64  while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1);
65 }

Referenced by backtrace_windows(), expand_heap(), free(), iob_read(), iob_write(), jemalloc_print_narenas(), lock_bin(), rz_basefind(), rz_bin_file_strings(), rz_core_autocomplete_add(), rz_th_cond_wait(), sdb_new(), sdb_ns_lock(), uv__fs_write(), and uv_pipe_zero_readfile_thread_proc().

◆ lock_bin()

static void lock_bin ( int  i)
inlinestatic

Definition at line 75 of file malloc.c.

76 {
77  lock(mal.bins[i].lock);
78  if (!mal.bins[i].head)
79  mal.bins[i].head = mal.bins[i].tail = BIN_TO_CHUNK(i);
80 }

References BIN_TO_CHUNK, i, lock(), and mal.

Referenced by alloc_fwd(), alloc_rev(), free(), and malloc().

◆ malloc()

void* malloc ( size_t  n)

Definition at line 320 of file malloc.c.

321 {
322  struct chunk *c;
323  int i, j;
324 
325  if (adjust_size(&n) < 0) return 0;
326 
327  if (n > MMAP_THRESHOLD) {
328  size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE;
329  char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
330  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
331  if (base == (void *)-1) return 0;
332  c = (void *)(base + SIZE_ALIGN - OVERHEAD);
333  c->csize = len - (SIZE_ALIGN - OVERHEAD);
334  c->psize = SIZE_ALIGN - OVERHEAD;
335  return CHUNK_TO_MEM(c);
336  }
337 
338  i = bin_index_up(n);
339  for (;;) {
340  uint64_t mask = mal.binmap & -(1ULL<<i);
341  if (!mask) {
342  c = expand_heap(n);
343  if (!c) return 0;
344  if (alloc_rev(c)) {
345  struct chunk *x = c;
346  c = PREV_CHUNK(c);
347  NEXT_CHUNK(x)->psize = c->csize =
348  x->csize + CHUNK_SIZE(c);
349  }
350  break;
351  }
352  j = first_set(mask);
353  lock_bin(j);
354  c = mal.bins[j].head;
355  if (c != BIN_TO_CHUNK(j)) {
356  if (!pretrim(c, n, i, j)) unbin(c, j);
357  unlock_bin(j);
358  break;
359  }
360  unlock_bin(j);
361  }
362 
363  /* Now patch up in case we over-allocated */
364  trim(c, n);
365 
366  return CHUNK_TO_MEM(c);
367 }
#define mask()
unsigned long uint64_t
Definition: sftypes.h:28
static struct chunk * expand_heap(size_t n)
Definition: malloc.c:162
#define MMAP_THRESHOLD
Definition: malloc.c:42
static int pretrim(struct chunk *self, size_t n, int i, int j)
Definition: malloc.c:272
static int bin_index_up(size_t x)
Definition: malloc.c:130
#define CHUNK_TO_MEM(c)
Definition: malloc.c:51
static int first_set(uint64_t x)
Definition: malloc.c:87
static void trim(struct chunk *self, size_t n)
Definition: malloc.c:302
static int adjust_size(size_t *n)
Definition: malloc.c:208

References __mmap(), adjust_size(), alloc_rev(), bin_index_up(), BIN_TO_CHUNK, c, CHUNK_SIZE, CHUNK_TO_MEM, expand_heap(), first_set(), i, len, lock_bin(), mal, MAP_ANONYMOUS, MAP_PRIVATE, mask, MMAP_THRESHOLD, n, NEXT_CHUNK, OVERHEAD, pretrim(), PREV_CHUNK, PROT_READ, PROT_WRITE, SIZE_ALIGN, trim(), unbin(), unlock_bin(), and x.

◆ pretrim()

static int pretrim ( struct chunk self,
size_t  n,
int  i,
int  j 
)
static

Definition at line 272 of file malloc.c.

273 {
274  size_t n1;
275  struct chunk *next, *split;
276 
277  /* We cannot pretrim if it would require re-binning. */
278  if (j < 40) return 0;
279  if (j < i+3) {
280  if (j != 63) return 0;
281  n1 = CHUNK_SIZE(self);
282  if (n1-n <= MMAP_THRESHOLD) return 0;
283  } else {
284  n1 = CHUNK_SIZE(self);
285  }
286  if (bin_index(n1-n) != j) return 0;
287 
288  next = NEXT_CHUNK(self);
289  split = (void *)((char *)self + n);
290 
291  split->prev = self->prev;
292  split->next = self->next;
293  split->prev->next = split;
294  split->next->prev = split;
295  split->psize = n | C_INUSE;
296  split->csize = n1-n;
297  next->psize = n1-n;
298  self->csize = n | C_INUSE;
299  return 1;
300 }
struct chunk * prev
Definition: malloc.c:23

References bin_index(), C_INUSE, CHUNK_SIZE, chunk::csize, i, MMAP_THRESHOLD, n, chunk::next, NEXT_CHUNK, chunk::prev, and chunk::psize.

Referenced by malloc().

◆ realloc()

void* realloc ( void *  p,
size_t  n 
)

Definition at line 380 of file malloc.c.

381 {
382  struct chunk *self, *next;
383  size_t n0, n1;
384  void *new;
385 
386  if (!p) return malloc(n);
387 
388  if (adjust_size(&n) < 0) return 0;
389 
390  self = MEM_TO_CHUNK(p);
391  n1 = n0 = CHUNK_SIZE(self);
392 
393  if (IS_MMAPPED(self)) {
394  size_t extra = self->psize;
395  char *base = (char *)self - extra;
396  size_t oldlen = n0 + extra;
397  size_t newlen = n + extra;
398  /* Crash on realloc of freed chunk */
399  if (extra & 1) a_crash();
400  if (newlen < PAGE_SIZE && (new = malloc(n))) {
401  memcpy(new, p, n-OVERHEAD);
402  free(p);
403  return new;
404  }
405  newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE;
406  if (oldlen == newlen) return p;
407  base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE);
408  if (base == (void *)-1)
409  goto copy_realloc;
410  self = (void *)(base + extra);
411  self->csize = newlen - extra;
412  return CHUNK_TO_MEM(self);
413  }
414 
415  next = NEXT_CHUNK(self);
416 
417  /* Crash on corrupted footer (likely from buffer overflow) */
418  if (next->psize != self->csize) a_crash();
419 
420  /* Merge adjacent chunks if we need more space. This is not
421  * a waste of time even if we fail to get enough space, because our
422  * subsequent call to free would otherwise have to do the merge. */
423  if (n > n1 && alloc_fwd(next)) {
424  n1 += CHUNK_SIZE(next);
425  next = NEXT_CHUNK(next);
426  }
427  /* FIXME: find what's wrong here and reenable it..? */
428  if (0 && n > n1 && alloc_rev(self)) {
429  self = PREV_CHUNK(self);
430  n1 += CHUNK_SIZE(self);
431  }
432  self->csize = n1 | C_INUSE;
433  next->psize = n1 | C_INUSE;
434 
435  /* If we got enough space, split off the excess and return */
436  if (n <= n1) {
437  //memmove(CHUNK_TO_MEM(self), p, n0-OVERHEAD);
438  trim(self, n);
439  return CHUNK_TO_MEM(self);
440  }
441 
442 copy_realloc:
443  /* As a last resort, allocate a new chunk and copy to it. */
444  new = malloc(n-OVERHEAD);
445  if (!new) return 0;
446  memcpy(new, p, n0-OVERHEAD);
447  free(CHUNK_TO_MEM(self));
448  return new;
449 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * __mremap(void *, size_t, size_t, int,...)
void free(void *p)
Definition: malloc.c:451

References __mremap(), adjust_size(), alloc_fwd(), alloc_rev(), C_INUSE, CHUNK_SIZE, CHUNK_TO_MEM, free(), IS_MMAPPED, malloc(), MEM_TO_CHUNK, memcpy(), n, chunk::next, NEXT_CHUNK, OVERHEAD, p, PREV_CHUNK, chunk::psize, and trim().

◆ trim()

static void trim ( struct chunk self,
size_t  n 
)
static

Definition at line 302 of file malloc.c.

303 {
304  size_t n1 = CHUNK_SIZE(self);
305  struct chunk *next, *split;
306 
307  if (n >= n1 - DONTCARE) return;
308 
309  next = NEXT_CHUNK(self);
310  split = (void *)((char *)self + n);
311 
312  split->psize = n | C_INUSE;
313  split->csize = n1-n | C_INUSE;
314  next->psize = n1-n | C_INUSE;
315  self->csize = n | C_INUSE;
316 
317  free(CHUNK_TO_MEM(split));
318 }
#define DONTCARE
Definition: malloc.c:43

References C_INUSE, CHUNK_SIZE, CHUNK_TO_MEM, chunk::csize, DONTCARE, free(), n, chunk::next, NEXT_CHUNK, and chunk::psize.

Referenced by malloc(), and realloc().

◆ unbin()

static void unbin ( struct chunk c,
int  i 
)
static

Definition at line 224 of file malloc.c.

225 {
226  if (c->prev == c->next)
227  a_and_64(&mal.binmap, ~(1ULL<<i));
228  c->prev->next = c->next;
229  c->next->prev = c->prev;
230  c->csize |= C_INUSE;
231  NEXT_CHUNK(c)->psize |= C_INUSE;
232 }

References c, C_INUSE, i, mal, and NEXT_CHUNK.

Referenced by alloc_fwd(), alloc_rev(), and malloc().

◆ unlock()

static void unlock ( volatile int lk)
inlinestatic

Definition at line 67 of file malloc.c.

68 {
69  if (lk[0]) {
70  a_store(lk, 0);
71  if (lk[1]) __wake(lk, 1, 1);
72  }
73 }

Referenced by expand_heap(), free(), and unlock_bin().

◆ unlock_bin()

static void unlock_bin ( int  i)
inlinestatic

Definition at line 82 of file malloc.c.

83 {
84  unlock(mal.bins[i].lock);
85 }

References i, mal, and unlock().

Referenced by alloc_fwd(), alloc_rev(), free(), and malloc().

Variable Documentation

◆ bin_tab

const unsigned char bin_tab[60]
static
Initial value:
= {
32,33,34,35,36,36,37,37,38,38,39,39,
40,40,40,40,41,41,41,41,42,42,42,42,43,43,43,43,
44,44,44,44,44,44,44,44,45,45,45,45,45,45,45,45,
46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,47,
}

Definition at line 114 of file malloc.c.

Referenced by bin_index(), and bin_index_up().

◆ binmap

Definition at line 33 of file malloc.c.

◆ bins

struct bin bins[64]

Definition at line 33 of file malloc.c.

Referenced by create_cache_bins(), jemalloc_print_narenas(), and print_tcache_content().

◆ free_lock

volatile int free_lock[2]

Definition at line 35 of file malloc.c.

◆