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 /* Synchronization tools */
25 static inline void lock(volatile int *lk)
27 if (libc.threads_minus_1)
28 while(a_swap(lk, 1)) __wait(lk, lk+1, 1, 1);
31 static inline void unlock(volatile int *lk)
35 if (lk[1]) __wake(lk, 1, 1);
39 static inline void lock_bin(int i)
41 lock(mal.bins[i].lock);
42 if (!mal.bins[i].head)
43 mal.bins[i].head = mal.bins[i].tail = BIN_TO_CHUNK(i);
46 static inline void unlock_bin(int i)
48 unlock(mal.bins[i].lock);
51 static int first_set(uint64_t x)
56 static const char debruijn64[64] = {
57 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
58 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
59 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
60 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
62 static const char debruijn32[32] = {
63 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13,
64 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14
66 if (sizeof(long) < 8) {
70 return 32 + debruijn32[(y&-y)*0x076be629 >> 27];
72 return debruijn32[(y&-y)*0x076be629 >> 27];
74 return debruijn64[(x&-x)*0x022fdd63cc95386dull >> 58];
78 static const unsigned char bin_tab[60] = {
79 32,33,34,35,36,36,37,37,38,38,39,39,
80 40,40,40,40,41,41,41,41,42,42,42,42,43,43,43,43,
81 44,44,44,44,44,44,44,44,45,45,45,45,45,45,45,45,
82 46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,47,
85 static int bin_index(size_t x)
87 x = x / SIZE_ALIGN - 1;
88 if (x <= 32) return x;
89 if (x < 512) return bin_tab[x/8-4];
90 if (x > 0x1c00) return 63;
91 return bin_tab[x/128-4] + 16;
94 static int bin_index_up(size_t x)
96 x = x / SIZE_ALIGN - 1;
97 if (x <= 32) return x;
99 if (x < 512) return bin_tab[x/8-4] + 1;
100 return bin_tab[x/128-4] + 17;
104 void __dump_heap(int x)
108 for (c = (void *)mal.heap; CHUNK_SIZE(c); c = NEXT_CHUNK(c))
109 fprintf(stderr, "base %p size %zu (%d) flags %d/%d\n",
110 c, CHUNK_SIZE(c), bin_index(CHUNK_SIZE(c)),
112 NEXT_CHUNK(c)->psize & 15);
113 for (i=0; i<64; i++) {
114 if (mal.bins[i].head != BIN_TO_CHUNK(i) && mal.bins[i].head) {
115 fprintf(stderr, "bin %d: %p\n", i, mal.bins[i].head);
116 if (!(mal.binmap & 1ULL<<i))
117 fprintf(stderr, "missing from binmap!\n");
118 } else if (mal.binmap & 1ULL<<i)
119 fprintf(stderr, "binmap wrongly contains %d!\n", i);
124 void *__expand_heap(size_t *);
126 static struct chunk *expand_heap(size_t n)
128 static int heap_lock[2];
133 /* The argument n already accounts for the caller's chunk
134 * overhead needs, but if the heap can't be extended in-place,
135 * we need room for an extra zero-sized sentinel chunk. */
140 p = __expand_heap(&n);
146 /* If not just expanding existing space, we need to make a
147 * new sentinel chunk below the allocated space. */
149 /* Valid/safe because of the prologue increment. */
151 p = (char *)p + SIZE_ALIGN;
153 w->psize = 0 | C_INUSE;
156 /* Record new heap end and fill in footer. */
158 w = MEM_TO_CHUNK(end);
159 w->psize = n | C_INUSE;
160 w->csize = 0 | C_INUSE;
162 /* Fill in header, which may be new or may be replacing a
163 * zero-size sentinel header at the old end-of-heap. */
165 w->csize = n | C_INUSE;
172 static int adjust_size(size_t *n)
174 /* Result of pointer difference must fit in ptrdiff_t. */
175 if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
184 *n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK;
188 static void unbin(struct chunk *c, int i)
190 if (c->prev == c->next)
191 a_and_64(&mal.binmap, ~(1ULL<<i));
192 c->prev->next = c->next;
193 c->next->prev = c->prev;
195 NEXT_CHUNK(c)->psize |= C_INUSE;
198 static int alloc_fwd(struct chunk *c)
202 while (!((k=c->csize) & C_INUSE)) {
215 static int alloc_rev(struct chunk *c)
219 while (!((k=c->psize) & C_INUSE)) {
223 unbin(PREV_CHUNK(c), i);
233 /* pretrim - trims a chunk _prior_ to removing it from its bin.
234 * Must be called with i as the ideal bin for size n, j the bin
235 * for the _free_ chunk self, and bin j locked. */
236 static int pretrim(struct chunk *self, size_t n, int i, int j)
239 struct chunk *next, *split;
241 /* We cannot pretrim if it would require re-binning. */
242 if (j < 40) return 0;
244 if (j != 63) return 0;
245 n1 = CHUNK_SIZE(self);
246 if (n1-n <= MMAP_THRESHOLD) return 0;
248 n1 = CHUNK_SIZE(self);
250 if (bin_index(n1-n) != j) return 0;
252 next = NEXT_CHUNK(self);
253 split = (void *)((char *)self + n);
255 split->prev = self->prev;
256 split->next = self->next;
257 split->prev->next = split;
258 split->next->prev = split;
259 split->psize = n | C_INUSE;
262 self->csize = n | C_INUSE;
266 static void trim(struct chunk *self, size_t n)
268 size_t n1 = CHUNK_SIZE(self);
269 struct chunk *next, *split;
271 if (n >= n1 - DONTCARE) return;
273 next = NEXT_CHUNK(self);
274 split = (void *)((char *)self + n);
276 split->psize = n | C_INUSE;
277 split->csize = n1-n | C_INUSE;
278 next->psize = n1-n | C_INUSE;
279 self->csize = n | C_INUSE;
284 void *malloc(size_t n)
289 if (adjust_size(&n) < 0) return 0;
291 if (n > MMAP_THRESHOLD) {
292 size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE;
293 char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
294 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
295 if (base == (void *)-1) return 0;
296 c = (void *)(base + SIZE_ALIGN - OVERHEAD);
297 c->csize = len - (SIZE_ALIGN - OVERHEAD);
298 c->psize = SIZE_ALIGN - OVERHEAD;
299 return CHUNK_TO_MEM(c);
304 uint64_t mask = mal.binmap & -(1ULL<<i);
311 NEXT_CHUNK(x)->psize = c->csize =
312 x->csize + CHUNK_SIZE(c);
318 c = mal.bins[j].head;
319 if (c != BIN_TO_CHUNK(j)) {
320 if (!pretrim(c, n, i, j)) unbin(c, j);
327 /* Now patch up in case we over-allocated */
330 return CHUNK_TO_MEM(c);
333 static size_t mal0_clear(char *p, size_t pagesz, size_t n)
336 typedef uint64_t __attribute__((__may_alias__)) T;
338 typedef unsigned char T;
341 size_t i = (uintptr_t)pp & (pagesz - 1);
343 pp = memset(pp - i, 0, i);
344 if (pp - p < pagesz) return pp - p;
345 for (i = pagesz; i; i -= 2*sizeof(T), pp -= 2*sizeof(T))
346 if (((T *)pp)[-1] | ((T *)pp)[-2])
351 void *calloc(size_t m, size_t n)
353 if (n && m > (size_t)-1/n) {
359 if (!p || IS_MMAPPED(MEM_TO_CHUNK(p)))
362 n = mal0_clear(p, PAGE_SIZE, n);
363 return memset(p, 0, n);
366 void *realloc(void *p, size_t n)
368 struct chunk *self, *next;
372 if (!p) return malloc(n);
374 if (adjust_size(&n) < 0) return 0;
376 self = MEM_TO_CHUNK(p);
377 n1 = n0 = CHUNK_SIZE(self);
379 if (IS_MMAPPED(self)) {
380 size_t extra = self->psize;
381 char *base = (char *)self - extra;
382 size_t oldlen = n0 + extra;
383 size_t newlen = n + extra;
384 /* Crash on realloc of freed chunk */
385 if (extra & 1) a_crash();
386 if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) {
390 newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE;
391 if (oldlen == newlen) return p;
392 base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE);
393 if (base == (void *)-1)
395 self = (void *)(base + extra);
396 self->csize = newlen - extra;
397 return CHUNK_TO_MEM(self);
400 next = NEXT_CHUNK(self);
402 /* Crash on corrupted footer (likely from buffer overflow) */
403 if (next->psize != self->csize) a_crash();
405 /* Merge adjacent chunks if we need more space. This is not
406 * a waste of time even if we fail to get enough space, because our
407 * subsequent call to free would otherwise have to do the merge. */
408 if (n > n1 && alloc_fwd(next)) {
409 n1 += CHUNK_SIZE(next);
410 next = NEXT_CHUNK(next);
412 /* FIXME: find what's wrong here and reenable it..? */
413 if (0 && n > n1 && alloc_rev(self)) {
414 self = PREV_CHUNK(self);
415 n1 += CHUNK_SIZE(self);
417 self->csize = n1 | C_INUSE;
418 next->psize = n1 | C_INUSE;
420 /* If we got enough space, split off the excess and return */
422 //memmove(CHUNK_TO_MEM(self), p, n0-OVERHEAD);
424 return CHUNK_TO_MEM(self);
428 /* As a last resort, allocate a new chunk and copy to it. */
429 new = malloc(n-OVERHEAD);
432 memcpy(new, p, n0-OVERHEAD);
433 free(CHUNK_TO_MEM(self));
437 void __bin_chunk(struct chunk *self)
439 struct chunk *next = NEXT_CHUNK(self);
440 size_t final_size, new_size, size;
444 final_size = new_size = CHUNK_SIZE(self);
446 /* Crash on corrupted footer (likely from buffer overflow) */
447 if (next->psize != self->csize) a_crash();
450 if (self->psize & next->csize & C_INUSE) {
451 self->csize = final_size | C_INUSE;
452 next->psize = final_size | C_INUSE;
453 i = bin_index(final_size);
456 if (self->psize & next->csize & C_INUSE)
458 unlock(mal.free_lock);
462 if (alloc_rev(self)) {
463 self = PREV_CHUNK(self);
464 size = CHUNK_SIZE(self);
466 if (new_size+size > RECLAIM && (new_size+size^size) > size)
470 if (alloc_fwd(next)) {
471 size = CHUNK_SIZE(next);
473 if (new_size+size > RECLAIM && (new_size+size^size) > size)
475 next = NEXT_CHUNK(next);
479 if (!(mal.binmap & 1ULL<<i))
480 a_or_64(&mal.binmap, 1ULL<<i);
482 self->csize = final_size;
483 next->psize = final_size;
484 unlock(mal.free_lock);
486 self->next = BIN_TO_CHUNK(i);
487 self->prev = mal.bins[i].tail;
488 self->next->prev = self;
489 self->prev->next = self;
491 /* Replace middle of large chunks with fresh zero pages */
493 uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE;
494 uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE;
496 __madvise((void *)a, b-a, MADV_DONTNEED);
498 __mmap((void *)a, b-a, PROT_READ|PROT_WRITE,
499 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
506 static void unmap_chunk(struct chunk *self)
508 size_t extra = self->psize;
509 char *base = (char *)self - extra;
510 size_t len = CHUNK_SIZE(self) + extra;
511 /* Crash on double free */
512 if (extra & 1) a_crash();
520 struct chunk *self = MEM_TO_CHUNK(p);
522 if (IS_MMAPPED(self))
528 void __malloc_donate(char *start, char *end)
530 size_t align_start_up = (SIZE_ALIGN-1) & (-(uintptr_t)start - OVERHEAD);
531 size_t align_end_down = (SIZE_ALIGN-1) & (uintptr_t)end;
533 /* Getting past this condition ensures that the padding for alignment
534 * and header overhead will not overflow and will leave a nonzero
535 * multiple of SIZE_ALIGN bytes between start and end. */
536 if (end - start <= OVERHEAD + align_start_up + align_end_down)
538 start += align_start_up + OVERHEAD;
539 end -= align_end_down;
541 struct chunk *c = MEM_TO_CHUNK(start), *n = MEM_TO_CHUNK(end);
542 c->psize = n->csize = C_INUSE;
543 c->csize = n->psize = C_INUSE | (end-start);