malloc_impl.h 2.4 KB
Newer Older
F
Far 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef MALLOC_IMPL_H
#define MALLOC_IMPL_H

#include <sys/mman.h>
#include "malloc_config.h"

hidden void *__expand_heap(size_t *);

hidden void __malloc_donate(char *, char *);

hidden void *__memalign(size_t, size_t);

struct chunk {
	size_t psize, csize;
15 16 17 18
#ifdef MALLOC_RED_ZONE
	size_t usize;
	size_t state;
#endif
F
Far 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
	struct chunk *next, *prev;
};

struct bin {
	volatile int lock[2];
	struct chunk *head;
	struct chunk *tail;
#ifdef MALLOC_FREELIST_HARDENED
	void *key;
#endif
};

#define SIZE_MASK (-SIZE_ALIGN)
32
#ifndef MALLOC_RED_ZONE
33 34
#define SIZE_ALIGN (4*sizeof(size_t))
#define OVERHEAD (2*sizeof(size_t))
35
#else
36 37
#define SIZE_ALIGN (8*sizeof(size_t))
#define OVERHEAD (4*sizeof(size_t))
38
#endif
F
Far 已提交
39
#define MMAP_THRESHOLD (0x1c00*SIZE_ALIGN)
40
#ifndef MALLOC_RED_ZONE
41
#define DONTCARE 16
42 43 44 45
#else
#define DONTCARE OVERHEAD
#define POINTER_USAGE (2*sizeof(void *))
#endif
F
Far 已提交
46 47
#define RECLAIM 163840

48 49 50 51 52 53 54
#ifdef MALLOC_FREELIST_QUARANTINE
#define QUARANTINE_MEM_SIZE 16384
#define QUARANTINE_THRESHOLD (QUARANTINE_MEM_SIZE / QUARANTINE_NUM)
#define QUARANTINE_N_THRESHOLD 32
#define QUARANTINE_NUM 8
#endif

F
Far 已提交
55 56 57 58 59 60 61 62
#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))

63 64 65 66
#ifdef MALLOC_FREELIST_QUARANTINE
#define QUARANTINE_TO_CHUNK(i) (MEM_TO_CHUNK(&mal.quarantine[i].head))
#endif

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#ifdef MALLOC_RED_ZONE
#define M_STATE_FREE   0x00
#define M_STATE_USED   0x01U
#define M_STATE_BRK    0x02U
#define M_STATE_MMAP   0x04U

#define M_RZ_NONE   0x00
#define M_RZ_POISON 0x10U

#define M_STATE_MASK   0xffU
#define M_CHECKSUM_SHIFT 8

#define POISON_PERIOD 31
#endif

F
Far 已提交
82 83 84 85 86 87 88 89
#define C_INUSE  ((size_t)1)

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

hidden void __bin_chunk(struct chunk *);

hidden extern int __malloc_replaced;

90 91 92 93 94 95 96 97
hidden void *internal_malloc(size_t n);

hidden void internal_free(void *p);

hidden void *internal_calloc(size_t m, size_t n);

hidden void *internal_realloc(void *p, size_t n);

98 99
hidden size_t internal_malloc_usable_size(void *p);

100 101 102 103 104 105
#ifdef MALLOC_RED_ZONE
hidden void chunk_checksum_set(struct chunk *c);

hidden int chunk_checksum_check(struct chunk *c);
#endif

106
#endif