Realloc inplace?

28, Sep, 2018

Sometimes it would be nice to have realloc which does not move (eg. graph with pointers backed in array). That’s easy. The trick is to reserve first the big chunk of address space and then allocate or deallocate memory in that particular address space.

On Unixes:
- reserve address space

mmap(NULL, max_length, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); /* reserve */

- (re)alloc in grow direction

mprotect(addr, current_length, PROT_READ | PROT_WRITE);

- shrink/dealloc

madvise(addr + new_length, current_length - new_length, MADV_DONTNEED);

On Windows:
- reserve address space

VirtualAlloc(NULL, max_length, MEM_RESERVE, PAGE_NOACCESS);

- (re)alloc in grow direction

VirtualAlloc(addr, current_length, MEM_COMMIT, PAGE_READWRITE);

- shrink/dealloc

VirtualAlloc(addr + new_length, current_length - new_length, MEM_RESET, PAGE_NOACCESS);

Limitation: you have “only” 128TB of address space on amd64.