+#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <limits.h>
return 0;
}
-static int init_malloc()
+static int init_malloc(size_t n)
{
static int init, waiters;
int state;
mal.brk = __brk(0) + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
- c = expand_heap(1);
+ c = expand_heap(n);
if (!c) {
a_store(&init, 0);
a_store(&init, 2);
if (waiters) __wake(&init, -1, 1);
- return 0;
+ return 1;
}
static int adjust_size(size_t *n)
if (adjust_size(&n) < 0) return 0;
if (n > MMAP_THRESHOLD) {
- size_t len = n + PAGE_SIZE - 1 & -PAGE_SIZE;
+ size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE;
char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (base == (void *)-1) return 0;
for (;;) {
uint64_t mask = mal.binmap & -(1ULL<<i);
if (!mask) {
- init_malloc();
+ if (init_malloc(n) > 0) continue;
c = expand_heap(n);
if (!c) return 0;
if (alloc_rev(c)) {
char *base = (char *)self - extra;
size_t oldlen = n0 + extra;
size_t newlen = n + extra;
+ /* Crash on realloc of freed chunk */
+ if ((uintptr_t)base < mal.brk) *(volatile char *)0=0;
if (newlen < PAGE_SIZE && (new = malloc(n))) {
memcpy(new, p, n-OVERHEAD);
free(p);
size_t extra = self->data[-1];
char *base = (char *)self - extra;
size_t len = CHUNK_SIZE(self) + extra;
+ /* Crash on double free */
+ if ((uintptr_t)base < mal.brk) *(volatile char *)0=0;
__munmap(base, len);
return;
}