3 ms·
OSes do already provide this ability: mmap.
by pencilguin 4y ago
OSes do already provide this ability: mmap.
- ndriscoll 4y agoMaybe I'm just not seeing it, but I don't see how mmap let's you remap an existing address to a new one? Looks like there is a mremap on Linux in any case.
- pencilguin 4y agoOr, you can map new space onto the end of an existing mapping, at a slightly greater cost of keeping track. Mremap is usually better.
- comex 4y agoIf you want to stick to mmap, you can do it by creating a file which doesn’t have any actual disk backing (one way to do this is with POSIX shm_open), and handling all memory allocations by mmapping parts of that file rather than mapping true anonymous memory. Then, ‘remapping’ an address to a new one is a matter of calling mmap again with the same file offset. Unlike mremap, this allows mapping the same memory at different addresses simultaneously, which can be useful for some purposes.
- pencilguin 4y ago/dev/hugepages/ is a good place to put this if you have enough control over the host.
- kabdib 4y agoThat sounds like a lot of work unless you absolutely have to have a contiguous allocation of every element. If you don't need that, you can get "O(1.1)" access and nice heap behavior with an indirection or two, and the implementation is likely to be lots more portable and standards-friendly than if you also dragged in a bunch of OS mmap() semantics (which tend to have exciting, platform-dependent misfeatures). Also, I would have to reach for the mmap() man page. I have malloc() right in front of me. :-)