10 #include "pthread_impl.h"
11 #include "malloc_impl.h"
13 #if defined(__GNUC__) && defined(__PIC__)
14 #define inline inline __attribute__((always_inline))
18 volatile uint64_t binmap;
20 volatile int split_merge_lock[2];
23 int __malloc_replaced;
25 /* Synchronization tools */
27 static inline void lock(volatile int *lk)
29 int need_locks = libc.need_locks;
31 while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1);
32 if (need_locks < 0) libc.need_locks = 0;
36 static inline void unlock(volatile int *lk)
40 if (lk[1]) __wake(lk, 1, 1);
44 static inline void lock_bin(int i)
46 lock(mal.bins[i].lock);
47 if (!mal.bins[i].head)
48 mal.bins[i].head = mal.bins[i].tail = BIN_TO_CHUNK(i);
51 static inline void unlock_bin(int i)
53 unlock(mal.bins[i].lock);
56 static int first_set(uint64_t x)
61 static const char debruijn64[64] = {
62 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
63 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
64 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
65 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
67 static const char debruijn32[32] = {
68 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13,
69 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14
71 if (sizeof(long) < 8) {
75 return 32 + debruijn32[(y&-y)*0x076be629 >> 27];
77 return debruijn32[(y&-y)*0x076be629 >> 27];
79 return debruijn64[(x&-x)*0x022fdd63cc95386dull >> 58];
83 static const unsigned char bin_tab[60] = {
84 32,33,34,35,36,36,37,37,38,38,39,39,
85 40,40,40,40,41,41,41,41,42,42,42,42,43,43,43,43,
86 44,44,44,44,44,44,44,44,45,45,45,45,45,45,45,45,
87 46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,47,
90 static int bin_index(size_t x)
92 x = x / SIZE_ALIGN - 1;
93 if (x <= 32) return x;
94 if (x < 512) return bin_tab[x/8-4];
95 if (x > 0x1c00) return 63;
96 return bin_tab[x/128-4] + 16;
99 static int bin_index_up(size_t x)
101 x = x / SIZE_ALIGN - 1;
102 if (x <= 32) return x;
104 if (x < 512) return bin_tab[x/8-4] + 1;
105 return bin_tab[x/128-4] + 17;
109 void __dump_heap(int x)
113 for (c = (void *)mal.heap; CHUNK_SIZE(c); c = NEXT_CHUNK(c))
114 fprintf(stderr, "base %p size %zu (%d) flags %d/%d\n",
115 c, CHUNK_SIZE(c), bin_index(CHUNK_SIZE(c)),
117 NEXT_CHUNK(c)->psize & 15);
118 for (i=0; i<64; i++) {
119 if (mal.bins[i].head != BIN_TO_CHUNK(i) && mal.bins[i].head) {
120 fprintf(stderr, "bin %d: %p\n", i, mal.bins[i].head);
121 if (!(mal.binmap & 1ULL<<i))
122 fprintf(stderr, "missing from binmap!\n");
123 } else if (mal.binmap & 1ULL<<i)
124 fprintf(stderr, "binmap wrongly contains %d!\n", i);
129 static struct chunk *expand_heap(size_t n)
135 /* The argument n already accounts for the caller's chunk
136 * overhead needs, but if the heap can't be extended in-place,
137 * we need room for an extra zero-sized sentinel chunk. */
140 p = __expand_heap(&n);
143 /* If not just expanding existing space, we need to make a
144 * new sentinel chunk below the allocated space. */
146 /* Valid/safe because of the prologue increment. */
148 p = (char *)p + SIZE_ALIGN;
150 w->psize = 0 | C_INUSE;
153 /* Record new heap end and fill in footer. */
155 w = MEM_TO_CHUNK(end);
156 w->psize = n | C_INUSE;
157 w->csize = 0 | C_INUSE;
159 /* Fill in header, which may be new or may be replacing a
160 * zero-size sentinel header at the old end-of-heap. */
162 w->csize = n | C_INUSE;
167 static int adjust_size(size_t *n)
169 /* Result of pointer difference must fit in ptrdiff_t. */
170 if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
179 *n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK;
183 static void unbin(struct chunk *c, int i)
185 if (c->prev == c->next)
186 a_and_64(&mal.binmap, ~(1ULL<<i));
187 c->prev->next = c->next;
188 c->next->prev = c->prev;
190 NEXT_CHUNK(c)->psize |= C_INUSE;
193 static void bin_chunk(struct chunk *self, int i)
195 self->next = BIN_TO_CHUNK(i);
196 self->prev = mal.bins[i].tail;
197 self->next->prev = self;
198 self->prev->next = self;
199 if (self->prev == BIN_TO_CHUNK(i))
200 a_or_64(&mal.binmap, 1ULL<<i);
203 static void trim(struct chunk *self, size_t n)
205 size_t n1 = CHUNK_SIZE(self);
206 struct chunk *next, *split;
208 if (n >= n1 - DONTCARE) return;
210 next = NEXT_CHUNK(self);
211 split = (void *)((char *)self + n);
213 split->psize = n | C_INUSE;
216 self->csize = n | C_INUSE;
218 int i = bin_index(n1-n);
226 void *malloc(size_t n)
232 if (adjust_size(&n) < 0) return 0;
234 if (n > MMAP_THRESHOLD) {
235 size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE;
236 char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
237 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
238 if (base == (void *)-1) return 0;
239 c = (void *)(base + SIZE_ALIGN - OVERHEAD);
240 c->csize = len - (SIZE_ALIGN - OVERHEAD);
241 c->psize = SIZE_ALIGN - OVERHEAD;
242 return CHUNK_TO_MEM(c);
246 if (i<63 && (mal.binmap & (1ULL<<i))) {
248 c = mal.bins[i].head;
249 if (c != BIN_TO_CHUNK(i) && CHUNK_SIZE(c)-n <= DONTCARE) {
252 return CHUNK_TO_MEM(c);
256 lock(mal.split_merge_lock);
257 for (mask = mal.binmap & -(1ULL<<i); mask; mask -= (mask&-mask)) {
260 c = mal.bins[j].head;
261 if (c != BIN_TO_CHUNK(j)) {
271 unlock(mal.split_merge_lock);
276 unlock(mal.split_merge_lock);
277 return CHUNK_TO_MEM(c);
280 static size_t mal0_clear(char *p, size_t pagesz, size_t n)
283 typedef uint64_t __attribute__((__may_alias__)) T;
285 typedef unsigned char T;
288 size_t i = (uintptr_t)pp & (pagesz - 1);
290 pp = memset(pp - i, 0, i);
291 if (pp - p < pagesz) return pp - p;
292 for (i = pagesz; i; i -= 2*sizeof(T), pp -= 2*sizeof(T))
293 if (((T *)pp)[-1] | ((T *)pp)[-2])
298 void *calloc(size_t m, size_t n)
300 if (n && m > (size_t)-1/n) {
307 if (!__malloc_replaced) {
308 if (IS_MMAPPED(MEM_TO_CHUNK(p)))
311 n = mal0_clear(p, PAGE_SIZE, n);
313 return memset(p, 0, n);
316 void *realloc(void *p, size_t n)
318 struct chunk *self, *next;
322 if (!p) return malloc(n);
324 if (adjust_size(&n) < 0) return 0;
326 self = MEM_TO_CHUNK(p);
327 n1 = n0 = CHUNK_SIZE(self);
329 if (n<=n0 && n0-n<=DONTCARE) return p;
331 if (IS_MMAPPED(self)) {
332 size_t extra = self->psize;
333 char *base = (char *)self - extra;
334 size_t oldlen = n0 + extra;
335 size_t newlen = n + extra;
336 /* Crash on realloc of freed chunk */
337 if (extra & 1) a_crash();
338 if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) {
342 newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE;
343 if (oldlen == newlen) return p;
344 base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE);
345 if (base == (void *)-1)
347 self = (void *)(base + extra);
348 self->csize = newlen - extra;
349 return CHUNK_TO_MEM(self);
352 next = NEXT_CHUNK(self);
354 /* Crash on corrupted footer (likely from buffer overflow) */
355 if (next->psize != self->csize) a_crash();
357 lock(mal.split_merge_lock);
359 size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next);
361 int i = bin_index(nsize);
363 if (!(next->csize & C_INUSE)) {
366 next = NEXT_CHUNK(next);
367 self->csize = next->psize = n0+nsize | C_INUSE;
369 unlock(mal.split_merge_lock);
370 return CHUNK_TO_MEM(self);
374 unlock(mal.split_merge_lock);
377 /* As a last resort, allocate a new chunk and copy to it. */
378 new = malloc(n-OVERHEAD);
381 memcpy(new, p, n0-OVERHEAD);
382 free(CHUNK_TO_MEM(self));
386 void __bin_chunk(struct chunk *self)
388 struct chunk *next = NEXT_CHUNK(self);
390 /* Crash on corrupted footer (likely from buffer overflow) */
391 if (next->psize != self->csize) a_crash();
393 lock(mal.split_merge_lock);
395 size_t osize = CHUNK_SIZE(self), size = osize;
397 /* Since we hold split_merge_lock, only transition from free to
398 * in-use can race; in-use to free is impossible */
399 size_t psize = self->psize & C_INUSE ? 0 : CHUNK_PSIZE(self);
400 size_t nsize = next->csize & C_INUSE ? 0 : CHUNK_SIZE(next);
403 int i = bin_index(psize);
405 if (!(self->psize & C_INUSE)) {
406 struct chunk *prev = PREV_CHUNK(self);
414 int i = bin_index(nsize);
416 if (!(next->csize & C_INUSE)) {
418 next = NEXT_CHUNK(next);
424 int i = bin_index(size);
430 unlock(mal.split_merge_lock);
432 /* Replace middle of large chunks with fresh zero pages */
433 if (size > RECLAIM && (size^(size-osize)) > size-osize) {
434 uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE;
435 uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE;
437 __madvise((void *)a, b-a, MADV_DONTNEED);
439 __mmap((void *)a, b-a, PROT_READ|PROT_WRITE,
440 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
447 static void unmap_chunk(struct chunk *self)
449 size_t extra = self->psize;
450 char *base = (char *)self - extra;
451 size_t len = CHUNK_SIZE(self) + extra;
452 /* Crash on double free */
453 if (extra & 1) a_crash();
461 struct chunk *self = MEM_TO_CHUNK(p);
463 if (IS_MMAPPED(self))
469 void __malloc_donate(char *start, char *end)
471 size_t align_start_up = (SIZE_ALIGN-1) & (-(uintptr_t)start - OVERHEAD);
472 size_t align_end_down = (SIZE_ALIGN-1) & (uintptr_t)end;
474 /* Getting past this condition ensures that the padding for alignment
475 * and header overhead will not overflow and will leave a nonzero
476 * multiple of SIZE_ALIGN bytes between start and end. */
477 if (end - start <= OVERHEAD + align_start_up + align_end_down)
479 start += align_start_up + OVERHEAD;
480 end -= align_end_down;
482 struct chunk *c = MEM_TO_CHUNK(start), *n = MEM_TO_CHUNK(end);
483 c->psize = n->csize = C_INUSE;
484 c->csize = n->psize = C_INUSE | (end-start);