move __expand_heap into malloc.c
authorRich Felker <dalias@aerifal.cx>
Wed, 3 Jun 2020 23:17:19 +0000 (19:17 -0400)
committerRich Felker <dalias@aerifal.cx>
Wed, 3 Jun 2020 23:17:19 +0000 (19:17 -0400)
this function is no longer used elsewhere, and moving it reduces the
number of source files specific to the malloc implementation.

src/malloc/expand_heap.c [deleted file]
src/malloc/malloc.c
src/malloc/malloc_impl.h

diff --git a/src/malloc/expand_heap.c b/src/malloc/expand_heap.c
deleted file mode 100644 (file)
index e6a3d7a..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-#include <limits.h>
-#include <stdint.h>
-#include <errno.h>
-#include <sys/mman.h>
-#include "libc.h"
-#include "syscall.h"
-#include "malloc_impl.h"
-
-/* This function returns true if the interval [old,new]
- * intersects the 'len'-sized interval below &libc.auxv
- * (interpreted as the main-thread stack) or below &b
- * (the current stack). It is used to defend against
- * buggy brk implementations that can cross the stack. */
-
-static int traverses_stack_p(uintptr_t old, uintptr_t new)
-{
-       const uintptr_t len = 8<<20;
-       uintptr_t a, b;
-
-       b = (uintptr_t)libc.auxv;
-       a = b > len ? b-len : 0;
-       if (new>a && old<b) return 1;
-
-       b = (uintptr_t)&b;
-       a = b > len ? b-len : 0;
-       if (new>a && old<b) return 1;
-
-       return 0;
-}
-
-/* Expand the heap in-place if brk can be used, or otherwise via mmap,
- * using an exponential lower bound on growth by mmap to make
- * fragmentation asymptotically irrelevant. The size argument is both
- * an input and an output, since the caller needs to know the size
- * allocated, which will be larger than requested due to page alignment
- * and mmap minimum size rules. The caller is responsible for locking
- * to prevent concurrent calls. */
-
-void *__expand_heap(size_t *pn)
-{
-       static uintptr_t brk;
-       static unsigned mmap_step;
-       size_t n = *pn;
-
-       if (n > SIZE_MAX/2 - PAGE_SIZE) {
-               errno = ENOMEM;
-               return 0;
-       }
-       n += -n & PAGE_SIZE-1;
-
-       if (!brk) {
-               brk = __syscall(SYS_brk, 0);
-               brk += -brk & PAGE_SIZE-1;
-       }
-
-       if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
-           && __syscall(SYS_brk, brk+n)==brk+n) {
-               *pn = n;
-               brk += n;
-               return (void *)(brk-n);
-       }
-
-       size_t min = (size_t)PAGE_SIZE << mmap_step/2;
-       if (n < min) n = min;
-       void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
-               MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
-       if (area == MAP_FAILED) return 0;
-       *pn = n;
-       mmap_step++;
-       return area;
-}
index 20598ec3ab3d6a34d7f5ff14e9cc7e724af58770..df3ea1becd3d5eb81ab68cb8e493b3ba27e81b6c 100644 (file)
@@ -126,6 +126,70 @@ void __dump_heap(int x)
 }
 #endif
 
 }
 #endif
 
+/* This function returns true if the interval [old,new]
+ * intersects the 'len'-sized interval below &libc.auxv
+ * (interpreted as the main-thread stack) or below &b
+ * (the current stack). It is used to defend against
+ * buggy brk implementations that can cross the stack. */
+
+static int traverses_stack_p(uintptr_t old, uintptr_t new)
+{
+       const uintptr_t len = 8<<20;
+       uintptr_t a, b;
+
+       b = (uintptr_t)libc.auxv;
+       a = b > len ? b-len : 0;
+       if (new>a && old<b) return 1;
+
+       b = (uintptr_t)&b;
+       a = b > len ? b-len : 0;
+       if (new>a && old<b) return 1;
+
+       return 0;
+}
+
+/* Expand the heap in-place if brk can be used, or otherwise via mmap,
+ * using an exponential lower bound on growth by mmap to make
+ * fragmentation asymptotically irrelevant. The size argument is both
+ * an input and an output, since the caller needs to know the size
+ * allocated, which will be larger than requested due to page alignment
+ * and mmap minimum size rules. The caller is responsible for locking
+ * to prevent concurrent calls. */
+
+static void *__expand_heap(size_t *pn)
+{
+       static uintptr_t brk;
+       static unsigned mmap_step;
+       size_t n = *pn;
+
+       if (n > SIZE_MAX/2 - PAGE_SIZE) {
+               errno = ENOMEM;
+               return 0;
+       }
+       n += -n & PAGE_SIZE-1;
+
+       if (!brk) {
+               brk = __syscall(SYS_brk, 0);
+               brk += -brk & PAGE_SIZE-1;
+       }
+
+       if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
+           && __syscall(SYS_brk, brk+n)==brk+n) {
+               *pn = n;
+               brk += n;
+               return (void *)(brk-n);
+       }
+
+       size_t min = (size_t)PAGE_SIZE << mmap_step/2;
+       if (n < min) n = min;
+       void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
+               MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+       if (area == MAP_FAILED) return 0;
+       *pn = n;
+       mmap_step++;
+       return area;
+}
+
 static struct chunk *expand_heap(size_t n)
 {
        static void *end;
 static struct chunk *expand_heap(size_t n)
 {
        static void *end;
index 6a92c1c6b2d24d91ab17c13e69203ae026a77637..e1cf4774c1e40d44d338200e95f8028b8083ce53 100644 (file)
@@ -4,8 +4,6 @@
 #include <sys/mman.h>
 #include "dynlink.h"
 
 #include <sys/mman.h>
 #include "dynlink.h"
 
-hidden void *__expand_heap(size_t *);
-
 struct chunk {
        size_t psize, csize;
        struct chunk *next, *prev;
 struct chunk {
        size_t psize, csize;
        struct chunk *next, *prev;