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