Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In addition to growing pages using VirtualAlloc, it's also possible to have the same memory pages mapped at different locations by using memory mapped 'files' (even when the backing file is just the paging file). This means that if your new block isn't contiguous, you can remap the existing memory without a copy.

However, system calls also have overhead (thanks Meltdown/Spectre mitigations), and you might not come ahead by avoiding memory copies.



Is there another option besides memfd_create? I'm currently using those to implement a circular buffer that avoids having to reconstruct objects if they happen to cross the size boundary by mapping the same memory twice consecutively into the process space.


On Unix-like platforms? sysv shared memory (shmget, etc). https://github.com/gnzlbg/slice_deque/blob/045fb28701d3b674b... does this. (Though that code looks racy to me; unmapping the second half's virtual address space before mapping again is wrong. I guess it does that because shmat's SHM_REMAP flag is Linux-specific. At least it will fail rather than silently overwrite some other mapping. edit: oh, and it retries too, so not bad.)

Seems like you could use shm_open + mmap also.

On Linux, you could probably also do some juggling with mremap + MREMAP_DONTUNMAP, but I don't know why you'd prefer this over memfd_create.


Any virtual memory manipulation will have massive overhead.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: