1 #ifndef MALLOC_IMPL_H 2 #define MALLOC_IMPL_H 3 4 #include <sys/mman.h> 5 #include "malloc_config.h" 6 7 hidden void *__expand_heap(size_t *); 8 9 hidden void __malloc_donate(char *, char *); 10 11 hidden void *__memalign(size_t, size_t); 12 13 #ifdef MUSL_ITERATE_AND_STATS_API 14 typedef struct occupied_bin_s { 15 struct chunk *head, *tail; 16 volatile int lock[2]; 17 } occupied_bin_t; 18 #endif 19 20 struct chunk { 21 size_t psize, csize; 22 #ifdef MALLOC_RED_ZONE 23 size_t usize; 24 size_t state; 25 #endif 26 #ifdef MUSL_ITERATE_AND_STATS_API 27 size_t thread_id; 28 struct chunk *next_occupied, *prev_occupied; 29 #endif 30 struct chunk *next, *prev; 31 }; 32 33 struct bin { 34 volatile int lock[2]; 35 struct chunk *head; 36 struct chunk *tail; 37 #ifdef MALLOC_FREELIST_HARDENED 38 void *key; 39 #endif 40 }; 41 42 #ifdef MUSL_ITERATE_AND_STATS_API 43 typedef void (*malloc_iterate_callback)(void* base, size_t size, void* arg); 44 45 hidden occupied_bin_t *__get_occupied_bin_by_idx(size_t bin_index); 46 hidden occupied_bin_t *__get_occupied_bin(struct chunk *c); 47 hidden occupied_bin_t *__get_current_occupied_bin(); 48 hidden void __push_chunk(struct chunk *c); 49 hidden void __pop_chunk(struct chunk *c); 50 hidden size_t __get_total_heap_space(void); 51 #endif 52 53 #define BINS_COUNT 64 54 55 #define SIZE_MASK (-SIZE_ALIGN) 56 57 #ifdef MUSL_ITERATE_AND_STATS_API 58 #define OCCUPIED_BIN_COUNT 64 59 #define OCCUPIED_LIST_OVERHEAD (2*sizeof(void*)) 60 #define ITERATE_AND_STATS_OVERHEAD (sizeof(size_t) + OCCUPIED_LIST_OVERHEAD) 61 #else 62 #define ITERATE_AND_STATS_OVERHEAD (0) 63 #endif 64 65 #ifdef MUSL_ITERATE_AND_STATS_API 66 #ifndef MALLOC_RED_ZONE 67 #define SIZE_ALIGN (8*sizeof(size_t)) 68 #define OVERHEAD (2*sizeof(size_t) + ITERATE_AND_STATS_OVERHEAD) 69 #else 70 #define SIZE_ALIGN (16*sizeof(size_t)) 71 #define OVERHEAD (4*sizeof(size_t) + ITERATE_AND_STATS_OVERHEAD) 72 #endif 73 #else 74 #ifndef MALLOC_RED_ZONE 75 #define SIZE_ALIGN (4*sizeof(size_t)) 76 #define OVERHEAD (2*sizeof(size_t)) 77 #else 78 #define SIZE_ALIGN (8*sizeof(size_t)) 79 #define OVERHEAD (4*sizeof(size_t)) 80 #endif 81 #endif 82 83 #define MMAP_THRESHOLD (0x1c00*SIZE_ALIGN) 84 #ifndef MALLOC_RED_ZONE 85 #ifdef MUSL_ITERATE_AND_STATS_API 86 #define DONTCARE OVERHEAD 87 #else 88 #define DONTCARE 16 89 #endif 90 #else 91 #define DONTCARE OVERHEAD 92 #define POINTER_USAGE (2*sizeof(void *)) 93 #endif 94 #define RECLAIM 163840 95 96 #ifdef MALLOC_FREELIST_QUARANTINE 97 #define QUARANTINE_MEM_SIZE 16384 98 #define QUARANTINE_THRESHOLD (QUARANTINE_MEM_SIZE / QUARANTINE_NUM) 99 #define QUARANTINE_N_THRESHOLD 32 100 #define QUARANTINE_NUM 8 101 #endif 102 103 #define CHUNK_SIZE(c) ((c)->csize & -2) 104 #define CHUNK_PSIZE(c) ((c)->psize & -2) 105 #define PREV_CHUNK(c) ((struct chunk *)((char *)(c) - CHUNK_PSIZE(c))) 106 #define NEXT_CHUNK(c) ((struct chunk *)((char *)(c) + CHUNK_SIZE(c))) 107 #define MEM_TO_CHUNK(p) (struct chunk *)((char *)(p) - OVERHEAD) 108 #define CHUNK_TO_MEM(c) (void *)((char *)(c) + OVERHEAD) 109 #define BIN_TO_CHUNK(i) (MEM_TO_CHUNK(&mal.bins[i].head)) 110 111 #ifdef MALLOC_FREELIST_QUARANTINE 112 #define QUARANTINE_TO_CHUNK(i) (MEM_TO_CHUNK(&mal.quarantine[i].head)) 113 #endif 114 115 #ifdef MALLOC_RED_ZONE 116 #define M_STATE_FREE 0x00 117 #define M_STATE_USED 0x01U 118 #define M_STATE_BRK 0x02U 119 #define M_STATE_MMAP 0x04U 120 121 #define M_RZ_NONE 0x00 122 #define M_RZ_POISON 0x10U 123 124 #define M_STATE_MASK 0xffU 125 #define M_CHECKSUM_SHIFT 8 126 127 #define POISON_PERIOD 31 128 #endif 129 130 #define C_INUSE ((size_t)1) 131 132 #define IS_MMAPPED(c) !((c)->csize & (C_INUSE)) 133 134 hidden void __bin_chunk(struct chunk *); 135 136 hidden extern int __malloc_replaced; 137 138 hidden void *internal_malloc(size_t n); 139 140 hidden void internal_free(void *p); 141 142 hidden void *internal_calloc(size_t m, size_t n); 143 144 hidden void *internal_realloc(void *p, size_t n); 145 146 hidden size_t internal_malloc_usable_size(void *p); 147 148 #ifdef MALLOC_RED_ZONE 149 hidden void chunk_checksum_set(struct chunk *c); 150 151 hidden int chunk_checksum_check(struct chunk *c); 152 #endif 153 154 #endif 155