1 #ifndef MALLOC_IMPL_H 2 #define MALLOC_IMPL_H 3 4 #include <sys/mman.h> 5 #include <stdbool.h> 6 7 #define BACKTRACE_DEPTH_MAX 5 /* The max depth of backtrace */ 8 #define BACKTRACE_START_OFFSET 0 /* The start offset of backtrace */ 9 #define SECONDARY_CALL_OFFSET 2 /* The backtrace offset for secondary call backtrace() */ 10 #define BACKTRACE_OFFSET (BACKTRACE_START_OFFSET + SECONDARY_CALL_OFFSET) 11 #define PREFIX_PLACE_HOLDER 10 /* Reserve positions for file name prefix "pid()_" */ 12 #define PTHREAD_NUM_MAX 128 /* Same as number of task of kernel */ 13 #define NODE_MAGIC 0xFCFCFCFC /* Magic number for node of chunk */ 14 #define FREE_MAGIC 0xFE /* Magic number for filling freed heap memory not recycled to heap pool */ 15 #define RECYCLE_MAX 128 /* The queue size for free() to recycle */ 16 #define RECYCLE_SIZE_MAX 0x300000 /* The max sum size of freed chunk for recycle list */ 17 #define ITEM_BUFFER_SIZE 256 /* The buffer max size for one item of memory debug info */ 18 #define CHECK_POINT_TRACE_MAX 2 /* The trace max for check point */ 19 20 hidden void *__expand_heap(size_t *); 21 22 hidden void __malloc_donate(char *, char *); 23 24 hidden void *__memalign(size_t, size_t); 25 26 struct chunk { 27 unsigned int checksum; 28 size_t psize, csize; 29 struct chunk *next, *prev; 30 }; 31 32 struct bin { 33 unsigned int checksum; 34 volatile int lock[2]; 35 struct chunk *head; 36 struct chunk *tail; 37 }; 38 39 struct heap_block { 40 struct heap_block *next; 41 struct heap_block *prev; 42 }; 43 44 struct list { 45 struct list *prev; 46 struct list *next; 47 }; 48 49 struct node { 50 short tid, pid; 51 void *ptr; 52 size_t size; 53 void *lr[BACKTRACE_DEPTH_MAX]; 54 struct list list; 55 }; 56 57 struct stat_bin { 58 volatile int lock[2]; 59 struct list head; 60 size_t t_total_size; 61 }; 62 63 #define ROUNDUP(a, b) (((a) + ((b) - 1)) & ~((b) - 1)) 64 #define SIZE_ALIGN ROUNDUP(sizeof(struct chunk), 0x10) 65 #define SIZE_MASK (-SIZE_ALIGN) 66 #define OVERHEAD (sizeof(struct chunk)) 67 #define BLOCK_HEAD (sizeof(struct heap_block) + OVERHEAD) 68 #define CHUNK_BLOCK_OFFSET (sizeof(struct heap_block)) 69 #define CHUNK_TO_BLOCK(c) (struct heap_block *)((char *)(c) - CHUNK_BLOCK_OFFSET) 70 #define BLOCK_TO_CHUNK(p) (struct chunk *)((char *)(p) + CHUNK_BLOCK_OFFSET) 71 #define MMAP_THRESHOLD (0x1c00*(4*sizeof(size_t))) 72 #define DONTCARE SIZE_ALIGN 73 #define RECLAIM 163840 74 75 #define CHUNK_SIZE(c) ((c)->csize & -2) 76 #define CHUNK_PSIZE(c) ((c)->psize & -2) 77 #define PREV_CHUNK(c) ((struct chunk *)((char *)(c) - CHUNK_PSIZE(c))) 78 #define NEXT_CHUNK(c) ((struct chunk *)((char *)(c) + CHUNK_SIZE(c))) 79 #define MEM_TO_CHUNK(p) (struct chunk *)((char *)(p) - OVERHEAD) 80 #define CHUNK_TO_MEM(c) (void *)((char *)(c) + OVERHEAD) 81 #define BIN_TO_CHUNK(i) (&mal.bins[i].checksum) 82 83 #define C_INUSE ((size_t)1) 84 85 #define IS_MMAPPED(c) !((c)->csize & (C_INUSE)) 86 87 hidden void __bin_chunk(struct chunk *); 88 89 hidden extern int __malloc_replaced; 90 91 hidden extern bool g_enable_check; 92 hidden extern int g_recycle_num; 93 hidden extern size_t g_recycle_size; 94 hidden extern int g_mem_lock[]; 95 hidden extern void insert_node(void *ptr, size_t size); 96 hidden extern int delete_node(void *ptr); 97 hidden extern void insert_block_list(struct chunk *ptr); 98 hidden extern void insert_free_tail(struct chunk *self); 99 hidden extern struct chunk *get_free_head(void); 100 hidden extern void get_free_trace(void *ptr); 101 hidden extern void clean_recycle_list(bool clean_all); 102 hidden extern void check_chunk_integrity(struct chunk *cur); 103 hidden extern void calculate_checksum(struct chunk *cur, struct chunk *next); 104 105 #endif 106