Is this linearity always supported or, for example, can the mmap call allocate memory that spans a data segment?
The observed behavior is that the brk region is always linear. Implementation Details: If expanding the brk region is not possible, for example, due to a blocking display, glibc will switch to mmap-only. Small allocations (<128 Kbytes) are apparently obtained with glibc via brk, if possible, therefore blocking with:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> int main(void) { int i; for (i = 0; i < 1024; ++i) { malloc(2048); if (i == 512) { void *r, *end = sbrk(0); r = mmap(end, 4096, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); } } }
when he suffers, gives really
[...] brk(0x1e7d000) = 0x1e7d000 mmap(0x1e7d000, 4096, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0) = 0x1e7d000 brk(0x1e9e000) = 0x1e7d000 <-- (!) mmap(NULL, 1048576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fbfd9bc9000
source share