aarch64: add HWCAP_DCPOP from linux v4.14
[musl] / src / malloc / malloc.c
index 290fda1..9e05e1d 100644 (file)
@@ -111,19 +111,29 @@ static int first_set(uint64_t x)
 #endif
 }
 
+static const unsigned char bin_tab[60] = {
+                   32,33,34,35,36,36,37,37,38,38,39,39,
+       40,40,40,40,41,41,41,41,42,42,42,42,43,43,43,43,
+       44,44,44,44,44,44,44,44,45,45,45,45,45,45,45,45,
+       46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,47,
+};
+
 static int bin_index(size_t x)
 {
        x = x / SIZE_ALIGN - 1;
        if (x <= 32) return x;
+       if (x < 512) return bin_tab[x/8-4];
        if (x > 0x1c00) return 63;
-       return ((union { float v; uint32_t r; }){(int)x}.r>>21) - 496;
+       return bin_tab[x/128-4] + 16;
 }
 
 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; }){(int)x}.r+0x1fffff>>21) - 496;
+       x--;
+       if (x < 512) return bin_tab[x/8-4] + 1;
+       return bin_tab[x/128-4] + 17;
 }
 
 #if 0
@@ -356,6 +366,17 @@ void *malloc(size_t n)
        return CHUNK_TO_MEM(c);
 }
 
+void *__malloc0(size_t n)
+{
+       void *p = malloc(n);
+       if (p && !IS_MMAPPED(MEM_TO_CHUNK(p))) {
+               size_t *z;
+               n = (n + sizeof *z - 1)/sizeof *z;
+               for (z=p; n; n--, z++) if (*z) *z=0;
+       }
+       return p;
+}
+
 void *realloc(void *p, size_t n)
 {
        struct chunk *self, *next;
@@ -385,7 +406,7 @@ void *realloc(void *p, size_t n)
                if (oldlen == newlen) return p;
                base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE);
                if (base == (void *)-1)
-                       return newlen < oldlen ? p : 0;
+                       goto copy_realloc;
                self = (void *)(base + extra);
                self->csize = newlen - extra;
                return CHUNK_TO_MEM(self);
@@ -418,6 +439,7 @@ void *realloc(void *p, size_t n)
                return CHUNK_TO_MEM(self);
        }
 
+copy_realloc:
        /* As a last resort, allocate a new chunk and copy to it. */
        new = malloc(n-OVERHEAD);
        if (!new) return 0;
@@ -428,14 +450,15 @@ void *realloc(void *p, size_t n)
 
 void free(void *p)
 {
-       struct chunk *self = MEM_TO_CHUNK(p);
-       struct chunk *next;
+       struct chunk *self, *next;
        size_t final_size, new_size, size;
        int reclaim=0;
        int i;
 
        if (!p) return;
 
+       self = MEM_TO_CHUNK(p);
+
        if (IS_MMAPPED(self)) {
                size_t extra = self->psize;
                char *base = (char *)self - extra;
@@ -453,18 +476,6 @@ void free(void *p)
        if (next->psize != self->csize) a_crash();
 
        for (;;) {
-               /* Replace middle of large chunks with fresh zero pages */
-               if (reclaim && (self->psize & next->csize & C_INUSE)) {
-                       uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE;
-                       uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE;
-#if 1
-                       __madvise((void *)a, b-a, MADV_DONTNEED);
-#else
-                       __mmap((void *)a, b-a, PROT_READ|PROT_WRITE,
-                               MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
-#endif
-               }
-
                if (self->psize & next->csize & C_INUSE) {
                        self->csize = final_size | C_INUSE;
                        next->psize = final_size | C_INUSE;
@@ -494,6 +505,9 @@ void free(void *p)
                }
        }
 
+       if (!(mal.binmap & 1ULL<<i))
+               a_or_64(&mal.binmap, 1ULL<<i);
+
        self->csize = final_size;
        next->psize = final_size;
        unlock(mal.free_lock);
@@ -503,8 +517,17 @@ void free(void *p)
        self->next->prev = self;
        self->prev->next = self;
 
-       if (!(mal.binmap & 1ULL<<i))
-               a_or_64(&mal.binmap, 1ULL<<i);
+       /* Replace middle of large chunks with fresh zero pages */
+       if (reclaim) {
+               uintptr_t a = (uintptr_t)self + SIZE_ALIGN+PAGE_SIZE-1 & -PAGE_SIZE;
+               uintptr_t b = (uintptr_t)next - SIZE_ALIGN & -PAGE_SIZE;
+#if 1
+               __madvise((void *)a, b-a, MADV_DONTNEED);
+#else
+               __mmap((void *)a, b-a, PROT_READ|PROT_WRITE,
+                       MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
+#endif
+       }
 
        unlock_bin(i);
 }