Despite continued interest there is still no way to create a “cow-copy” of a memory region on Linux. With the inception of the memfd_create(2)
syscall, the situation has slightely improved, as one does not have to create an explicit file any more for shared memory.
I am wondering, why isn't there a thing like the following?
void *ptr = mmap((void *)0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, (size_t)0);
void *ptr2= mremap(ptr, 0, size, MREMAP_MAYMOVE | MREMAP_COW);
The intended semantics are that ptr2
and ptr
share the underlying memory, but a write from either will trigger a copy-on-write with page-granularity.
Is this just a case of “nobody bothered to implement this yet” or am I missing something technical?