#include "shim.h" /* 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 unsigned mmap_step; size_t n = *pn; if (n > SIZE_MAX/2 - PAGE_SIZE) { errno = ENOMEM; return 0; } n += -n & PAGE_SIZE-1; 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; }