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 free_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)
131 static int heap_lock[2];
136 /* The argument n already accounts for the caller's chunk
137 * overhead needs, but if the heap can't be extended in-place,
138 * we need room for an extra zero-sized sentinel chunk. */
143 p = __expand_heap(&n);
149 /* If not just expanding existing space, we need to make a
150 * new sentinel chunk below the allocated space. */
152 /* Valid/safe because of the prologue increment. */
154 p = (char *)p + SIZE_ALIGN;
156 w->psize = 0 | C_INUSE;
159 /* Record new heap end and fill in footer. */
161 w = MEM_TO_CHUNK(end);
162 w->psize = n | C_INUSE;
163 w->csize = 0 | C_INUSE;
165 /* Fill in header, which may be new or may be replacing a
166 * zero-size sentinel header at the old end-of-heap. */
168 w->csize = n | C_INUSE;
175 static int adjust_size(size_t *n)
177 /* Result of pointer difference must fit in ptrdiff_t. */
178 if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
187 *n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK;
191 static void unbin(struct chunk *c, int i)
193 if (c->prev == c->next)
194 a_and_64(&mal.binmap, ~(1ULL<<i));
195 c->prev->next = c->next;
196 c->next->prev = c->prev;
198 NEXT_CHUNK(c)->psize |= C_INUSE;
201 static int alloc_fwd(struct chunk *c)
205 while (!((k=c->csize) & C_INUSE)) {
218 static int alloc_rev(struct chunk *c)
222 while (!((k=c->psize) & C_INUSE)) {
226 unbin(PREV_CHUNK(c), i);
236 /* pretrim - trims a chunk _prior_ to removing it from its bin.
237 * Must be called with i as the ideal bin for size n, j the bin
238 * for the _free_ chunk self, and bin j locked. */
239 static int pretrim(struct chunk *self, size_t n, int i, int j)
242 struct chunk *next, *split;
244 /* We cannot pretrim if it would require re-binning. */
245 if (j < 40) return 0;
247 if (j != 63) return 0;
248 n1 = CHUNK_SIZE(self);
249 if (n1-n <= MMAP_THRESHOLD) return 0;
251 n1 = CHUNK_SIZE(self);
253 if (bin_index(n1-n) != j) return 0;
255 next = NEXT_CHUNK(self);
256 split = (void *)((char *)self + n);
258 split->prev = self->prev;
259 split->next = self->next;
260 split->prev->next = split;
261 split->next->prev = split;
262 split->psize = n | C_INUSE;
265 self->csize = n | C_INUSE;
269 static void trim(struct chunk *self, size_t n)
271 size_t n1 = CHUNK_SIZE(self);
272 struct chunk *next, *split;
274 if (n >= n1 - DONTCARE) return;
276 next = NEXT_CHUNK(self);
277 split = (void *)((char *)self + n);
279 split->psize = n | C_INUSE;
280 split->csize = n1-n | C_INUSE;
281 next->psize = n1-n | C_INUSE;
282 self->csize = n | C_INUSE;
287 void *malloc(size_t n)
292 if (adjust_size(&n) < 0) return 0;
294 if (n > MMAP_THRESHOLD) {
295 size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE;
296 char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
297 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
298 if (base == (void *)-1) return 0;
299 c = (void *)(base + SIZE_ALIGN - OVERHEAD);
300 c->csize = len - (SIZE_ALIGN - OVERHEAD);
301 c->psize = SIZE_ALIGN - OVERHEAD;
302 return CHUNK_TO_MEM(c);
307 uint64_t mask = mal.binmap & -(1ULL<<i);
314 NEXT_CHUNK(x)->psize = c->csize =
315 x->csize + CHUNK_SIZE(c);
321 c = mal.bins[j].head;
322 if (c != BIN_TO_CHUNK(j)) {
323 if (!pretrim(c, n, i, j)) unbin(c, j);
330 /* Now patch up in case we over-allocated */
333 return CHUNK_TO_MEM(c);
336 static size_t mal0_clear(char *p, size_t pagesz, size_t n)
339 typedef uint64_t __attribute__((__may_alias__)) T;
341 typedef unsigned char T;
344 size_t i = (uintptr_t)pp & (pagesz - 1);
346 pp = memset(pp - i, 0, i);
347 if (pp - p < pagesz) return pp - p;
348 for (i = pagesz; i; i -= 2*sizeof(T), pp -= 2*sizeof(T))
349 if (((T *)pp)[-1] | ((T *)pp)[-2])
354 void *calloc(size_t m, size_t n)
356 if (n && m > (size_t)-1/n) {
363 if (!__malloc_replaced) {
364 if (IS_MMAPPED(MEM_TO_CHUNK(p)))
367 n = mal0_clear(p, PAGE_SIZE, n);
369 return memset(p, 0, n);
372 void *realloc(void *p, size_t n)
374 struct chunk *self, *next;
378 if (!p) return malloc(n);
380 if (adjust_size(&n) < 0) return 0;
382 self = MEM_TO_CHUNK(p);
383 n1 = n0 = CHUNK_SIZE(self);
385 if (IS_MMAPPED(self)) {
386 size_t extra = self->psize;
387 char *base = (char *)self - extra;
388 size_t oldlen = n0 + extra;
389 size_t newlen = n + extra;
390 /* Crash on realloc of freed chunk */
391 if (extra & 1) a_crash();
392 if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) {
396 newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE;
397 if (oldlen == newlen) return p;
398 base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE);
399 if (base == (void *)-1)
401 self = (void *)(base + extra);
402 self->csize = newlen - extra;
403 return CHUNK_TO_MEM(self);
406 next = NEXT_CHUNK(self);
408 /* Crash on corrupted footer (likely from buffer overflow) */
409 if (next->psize != self->csize) a_crash();
411 /* Merge adjacent chunks if we need more space. This is not
412 * a waste of time even if we fail to get enough space, because our
413 * subsequent call to free would otherwise have to do the merge. */
414 if (n > n1 && alloc_fwd(next)) {
415 n1 += CHUNK_SIZE(next);
416 next = NEXT_CHUNK(next);
418 /* FIXME: find what's wrong here and reenable it..? */
419 if (0 && n > n1 && alloc_rev(self)) {
420 self = PREV_CHUNK(self);
421 n1 += CHUNK_SIZE(self);
423 self->csize = n1 | C_INUSE;
424 next->psize = n1 | C_INUSE;
426 /* If we got enough space, split off the excess and return */
428 //memmove(CHUNK_TO_MEM(self), p, n0-OVERHEAD);
430 return CHUNK_TO_MEM(self);
434 /* As a last resort, allocate a new chunk and copy to it. */
435 new = malloc(n-OVERHEAD);
438 memcpy(new, p, n0-OVERHEAD);
439 free(CHUNK_TO_MEM(self));
443 void __bin_chunk(struct chunk *self)
445 struct chunk *next = NEXT_CHUNK(self);
446 size_t final_size, new_size, size;
450 final_size = new_size = CHUNK_SIZE(self);
452 /* Crash on corrupted footer (likely from buffer overflow) */
453 if (next->psize != self->csize) a_crash();
456 if (self->psize & next->csize & C_INUSE) {
457 self->csize = final_size | C_INUSE;
458 next->psize = final_size | C_INUSE;
459 i = bin_index(final_size);
462 if (self->psize & next->csize & C_INUSE)
464 unlock(mal.free_lock);
468 if (alloc_rev(self)) {
469 self = PREV_CHUNK(self);
470 size = CHUNK_SIZE(self);
472 if (new_size+size > RECLAIM && (new_size+size^size) > size)
476 if (alloc_fwd(next)) {
477 size = CHUNK_SIZE(next);
479 if (new_size+size > RECLAIM && (new_size+size^size) > size)
481 next = NEXT_CHUNK(next);
485 if (!(mal.binmap & 1ULL<<i))
486 a_or_64(&mal.binmap, 1ULL<<i);
488 self->csize = final_size;
489 next->psize = final_size;
490 unlock(mal.free_lock);
492 self->next = BIN_TO_CHUNK(i);
493 self->prev = mal.bins[i].tail;
494 self->next->prev = self;
495 self->prev->next = self;
497 /* Replace middle of large chunks with fresh zero pages */
499 uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE;
500 uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE;
502 __madvise((void *)a, b-a, MADV_DONTNEED);
504 __mmap((void *)a, b-a, PROT_READ|PROT_WRITE,
505 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
512 static void unmap_chunk(struct chunk *self)
514 size_t extra = self->psize;
515 char *base = (char *)self - extra;
516 size_t len = CHUNK_SIZE(self) + extra;
517 /* Crash on double free */
518 if (extra & 1) a_crash();
526 struct chunk *self = MEM_TO_CHUNK(p);
528 if (IS_MMAPPED(self))
534 void __malloc_donate(char *start, char *end)
536 size_t align_start_up = (SIZE_ALIGN-1) & (-(uintptr_t)start - OVERHEAD);
537 size_t align_end_down = (SIZE_ALIGN-1) & (uintptr_t)end;
539 /* Getting past this condition ensures that the padding for alignment
540 * and header overhead will not overflow and will leave a nonzero
541 * multiple of SIZE_ALIGN bytes between start and end. */
542 if (end - start <= OVERHEAD + align_start_up + align_end_down)
544 start += align_start_up + OVERHEAD;
545 end -= align_end_down;
547 struct chunk *c = MEM_TO_CHUNK(start), *n = MEM_TO_CHUNK(end);
548 c->psize = n->csize = C_INUSE;
549 c->csize = n->psize = C_INUSE | (end-start);