简体   繁体   中英

Mapping virtual memory to virtual memory

I am supplied with a struct that lies somewhere in the memory. Is it possible in WinAPI/POSIX to create a memory mapping that would alias the struct to a different part of the memory range so that identical bytes could be accessed from both locations? I cannot control the location of the original struct, but the new one can be stored anywhere.

I think, at least in WinAPI, I could create a temporary file and store the pages that contain the struct there, and then I could (hopefully) replace the original pages with new ones that point to the file, then map the file to a new section in the memory. However, I am worried about the performance of accessing the memory. Is there something better?

In Linux you can use memfd_create(2) to allocate memory which you can access through a file descriptor. You'd then write(2) the original data to that in-memory file and map it over the original location with mmap(2) using MAP_FIXED flag, and then to another location with a non-fixed mmap(2) . Be aware that mapping must be a multiple of the underlying page size (4k in regular address space, or hugepage size if the memfd was allocated from hugepages).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM