8 /* This function returns true if the interval [old,new]
9 * intersects the 'len'-sized interval below &libc.auxv
10 * (interpreted as the main-thread stack) or below &b
11 * (the current stack). It is used to defend against
12 * buggy brk implementations that can cross the stack. */
14 static int traverses_stack_p(uintptr_t old, uintptr_t new)
16 const uintptr_t len = 8<<20;
19 b = (uintptr_t)libc.auxv;
20 a = b > len ? b-len : 0;
21 if (new>a && old<b) return 1;
24 a = b > len ? b-len : 0;
25 if (new>a && old<b) return 1;
30 void *__mmap(void *, size_t, int, int, int, off_t);
32 /* Expand the heap in-place if brk can be used, or otherwise via mmap,
33 * using an exponential lower bound on growth by mmap to make
34 * fragmentation asymptotically irrelevant. The size argument is both
35 * an input and an output, since the caller needs to know the size
36 * allocated, which will be larger than requested due to page alignment
37 * and mmap minimum size rules. The caller is responsible for locking
38 * to prevent concurrent calls. */
40 void *__expand_heap(size_t *pn)
43 static unsigned mmap_step;
46 if (n > SIZE_MAX/2 - PAGE_SIZE) {
50 n += -n & PAGE_SIZE-1;
53 brk = __syscall(SYS_brk, 0);
54 brk += -brk & PAGE_SIZE-1;
57 if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
58 && __syscall(SYS_brk, brk+n)==brk+n) {
61 return (void *)(brk-n);
64 size_t min = (size_t)PAGE_SIZE << mmap_step/2;
66 void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
67 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
68 if (area == MAP_FAILED) return 0;