X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=blobdiff_plain;f=src%2Fmalloc%2Fmalloc.c;h=207b6ef2c2c2e481fbf4a56dd6b1143a5d6824f6;hp=a4eefda94323be47967cc370b58204311c2e0f0a;hb=2afebbbcd16e8bfc5e008a40b2faf3bd8cf14e88;hpb=095820016689dfdc9141f477a86de22054c86078 diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c index a4eefda9..207b6ef2 100644 --- a/src/malloc/malloc.c +++ b/src/malloc/malloc.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include #include #include @@ -119,14 +120,14 @@ static int bin_index(size_t x) x = x / SIZE_ALIGN - 1; if (x <= 32) return x; if (x > 0x1c00) return 63; - return ((union { float v; uint32_t r; }){ x }.r>>21) - 496; + return ((union { float v; uint32_t r; }){(int)x}.r>>21) - 496; } static int bin_index_up(size_t x) { x = x / SIZE_ALIGN - 1; if (x <= 32) return x; - return ((union { float v; uint32_t r; }){ x }.r+0x1fffff>>21) - 496; + return ((union { float v; uint32_t r; }){(int)x}.r+0x1fffff>>21) - 496; } #if 0 @@ -179,7 +180,7 @@ fail: return 0; } -static int init_malloc() +static int init_malloc(size_t n) { static int init, waiters; int state; @@ -196,7 +197,7 @@ static int init_malloc() mal.brk = __brk(0) + 2*SIZE_ALIGN-1 & -SIZE_ALIGN; - c = expand_heap(1); + c = expand_heap(n); if (!c) { a_store(&init, 0); @@ -210,7 +211,7 @@ static int init_malloc() a_store(&init, 2); if (waiters) __wake(&init, -1, 1); - return 0; + return 1; } static int adjust_size(size_t *n) @@ -333,7 +334,7 @@ void *malloc(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; @@ -347,7 +348,7 @@ void *malloc(size_t n) for (;;) { uint64_t mask = mal.binmap & -(1ULL< 0) continue; c = expand_heap(n); if (!c) return 0; if (alloc_rev(c)) { @@ -394,7 +395,7 @@ void *realloc(void *p, size_t n) size_t oldlen = n0 + extra; size_t newlen = n + extra; /* Crash on realloc of freed chunk */ - if ((uintptr_t)base < mal.brk) *(char *)0=0; + if ((uintptr_t)base < mal.brk) *(volatile char *)0=0; if (newlen < PAGE_SIZE && (new = malloc(n))) { memcpy(new, p, n-OVERHEAD); free(p); @@ -457,7 +458,7 @@ void free(void *p) char *base = (char *)self - extra; size_t len = CHUNK_SIZE(self) + extra; /* Crash on double free */ - if ((uintptr_t)base < mal.brk) *(char *)0=0; + if ((uintptr_t)base < mal.brk) *(volatile char *)0=0; __munmap(base, len); return; }