1 #ifndef __DLMALLOC_H 2 #define __DLMALLOC_H 3 4 #ifndef MALLOC_ALIGNMENT 5 #define MALLOC_ALIGNMENT 16 6 #endif 7 8 #ifndef FOOTERS 9 #define FOOTERS 0 10 #endif 11 12 #ifndef MMAP_CLEARS 13 #define MMAP_CLEARS 1 14 #endif 15 16 #ifndef HAVE_MMAP 17 #define HAVE_MMAP 0 18 #endif 19 20 #ifndef DEBUG 21 #define DEBUG 0 22 #endif 23 24 //#ifndef HAVE_USR_INCLUDE_MALLOC_H 25 //#define HAVE_USR_INCLUDE_MALLOC_H 1 26 //#endif 27 28 #if !(MALLOC_ALIGNMENT == 16 || MALLOC_ALIGNMENT == 0) 29 #error Modify lines below to make MALLOC_ALIGNMENT consistent 30 #endif 31 32 #undef MALLOC_ALIGNMENT 33 #define MALLOC_ALIGNMENT 16 34 35 /* The byte and bit size of a size_t */ 36 #define SIZE_T_SIZE (sizeof(size_t)) 37 #define SIZE_T_BITSIZE (sizeof(size_t) << 3) 38 39 /* Some constants coerced to size_t */ 40 /* Annoying but necessary to avoid errors on some plaftorms */ 41 #define SIZE_T_ZERO ((size_t)0) 42 #define SIZE_T_ONE ((size_t)1) 43 #define SIZE_T_TWO ((size_t)2) 44 #define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1) 45 #define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2) 46 #define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES) 47 #define HALF_MAX_SIZE_T (MAX_SIZE_T / 2U) 48 49 /* The bit mask value corresponding to MALLOC_ALIGNMENT */ 50 #define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE) 51 52 /* True if address a has acceptable alignment */ 53 #define is_aligned(A) (((size_t)((A)) & (CHUNK_ALIGN_MASK)) == 0) 54 55 /* the number of bytes to offset an address to align it */ 56 #define align_offset(A)\ 57 ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\ 58 ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK)) 59 60 struct malloc_chunk { 61 size_t prev_foot; /* Size of previous chunk (if free). */ 62 size_t head; /* Size and inuse bits. */ 63 struct malloc_chunk* fd; /* double links -- used only if free. */ 64 struct malloc_chunk* bk; 65 }; 66 67 typedef struct malloc_chunk mchunk; 68 typedef struct malloc_chunk* mchunkptr; 69 typedef struct malloc_chunk* sbinptr; /* The type of bins of chunks */ 70 typedef unsigned int bindex_t; /* Described below */ 71 typedef unsigned int binmap_t; /* Described below */ 72 typedef unsigned int flag_t; /* The type of various bit flag sets */ 73 74 /* ------------------- Chunks sizes and alignments ----------------------- */ 75 76 #define MCHUNK_SIZE (sizeof(mchunk)) 77 78 #if FOOTERS 79 #define CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) 80 #else /* FOOTERS */ 81 #define CHUNK_OVERHEAD (SIZE_T_SIZE) 82 #endif /* FOOTERS */ 83 84 /* MMapped chunks need a second word of overhead ... */ 85 #define MMAP_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) 86 /* ... and additional padding for fake next-chunk at foot */ 87 #define MMAP_FOOT_PAD (FOUR_SIZE_T_SIZES) 88 89 /* The smallest size we can malloc is an aligned minimal chunk */ 90 #define MIN_CHUNK_SIZE\ 91 ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) 92 93 /* conversion from malloc headers to user pointers, and back */ 94 #define chunk2mem(p) ((void*)((unsigned long)(p) + TWO_SIZE_T_SIZES)) 95 #define mem2chunk(mem) ((mchunkptr)((unsigned long)(mem) - TWO_SIZE_T_SIZES)) 96 /* chunk associated with aligned address A */ 97 #define align_as_chunk(A) (mchunkptr)(((unsigned long)A) + align_offset(chunk2mem(A))) 98 99 /* Bounds on request (not chunk) sizes. */ 100 #define MAX_REQUEST ((-MIN_CHUNK_SIZE) << 2) 101 #define MIN_REQUEST (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE) 102 103 /* pad request bytes into a usable size */ 104 #define pad_request(req) \ 105 (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) 106 107 /* pad request, checking for minimum (but not maximum) */ 108 #define request2size(req) \ 109 (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req)) 110 111 112 /* ------------------ Operations on head and foot fields ----------------- */ 113 114 /* 115 The head field of a chunk is or'ed with PINUSE_BIT when previous 116 adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in 117 use. If the chunk was obtained with mmap, the prev_foot field has 118 IS_MMAPPED_BIT set, otherwise holding the offset of the base of the 119 mmapped region to the base of the chunk. 120 */ 121 122 #define PINUSE_BIT (SIZE_T_ONE) 123 #define CINUSE_BIT (SIZE_T_TWO) 124 #define INUSE_BITS (PINUSE_BIT|CINUSE_BIT) 125 126 /* Head value for fenceposts */ 127 #define FENCEPOST_HEAD (INUSE_BITS|SIZE_T_SIZE) 128 129 /* extraction of fields from head words */ 130 #define cinuse(p) ((p)->head & CINUSE_BIT) 131 #define pinuse(p) ((p)->head & PINUSE_BIT) 132 #define chunksize(p) ((p)->head & ~(INUSE_BITS)) 133 134 #define clear_pinuse(p) ((p)->head &= ~PINUSE_BIT) 135 #define clear_cinuse(p) ((p)->head &= ~CINUSE_BIT) 136 137 /* Treat space at ptr +/- offset as a chunk */ 138 #define chunk_plus_offset(p, s) ((mchunkptr)(((unsigned long)(p)) + (s))) 139 #define chunk_minus_offset(p, s) ((mchunkptr)(((unsigned long)(p)) - (s))) 140 141 /* Ptr to next or previous physical malloc_chunk. */ 142 #define next_chunk(p) ((mchunkptr)( ((unsigned long)(p)) + ((p)->head & ~INUSE_BITS))) 143 #define prev_chunk(p) ((mchunkptr)( ((unsigned long)(p)) - ((p)->prev_foot) )) 144 145 /* extract next chunk's pinuse bit */ 146 #define next_pinuse(p) ((next_chunk(p)->head) & PINUSE_BIT) 147 148 /* Get/set size at footer */ 149 #define get_foot(p, s) (((mchunkptr)((unsigned long)(p) + (s)))->prev_foot) 150 #define set_foot(p, s) (((mchunkptr)((unsigned long)(p) + (s)))->prev_foot = (s)) 151 152 /* Set size, pinuse bit, and foot */ 153 #define set_size_and_pinuse_of_free_chunk(p, s)\ 154 ((p)->head = (s|PINUSE_BIT), set_foot(p, s)) 155 156 /* Set size, pinuse bit, foot, and clear next pinuse */ 157 #define set_free_with_pinuse(p, s, n)\ 158 (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s)) 159 160 #define is_mmapped(p)\ 161 (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_MMAPPED_BIT)) 162 163 /* Get the internal overhead associated with chunk p */ 164 #define overhead_for(p)\ 165 (is_mmapped(p)? MMAP_CHUNK_OVERHEAD : CHUNK_OVERHEAD) 166 167 /* Return true if malloced space is not necessarily cleared */ 168 #if MMAP_CLEARS 169 #define calloc_must_clear(p) (!is_mmapped(p)) 170 #else /* MMAP_CLEARS */ 171 #define calloc_must_clear(p) (1) 172 #endif /* MMAP_CLEARS */ 173 174 175 void* dlmalloc(size_t); 176 void* dlrealloc(void*, size_t); 177 void dlfree(void* mem); 178 struct mallinfo dlmallinfo(void); 179 struct mallinfo dlmallinfo_ex(size_t *plargest_free); 180 void *MoreCore (size_t nbytes); 181 182 #endif // __DLMALLOC_H 183 184