The following image shows multiple and overlapped views to a memory-mapped file: The following illustration shows how multiple processes can have multiple and overlapping views to the same memory-mapped file at the same time. You do not have to handle the memory management yourself. Memory-mapped files are accessed through the operating system's memory manager, so the file is automatically partitioned into a number of pages and accessed as needed. Random access views are preferred for working with persisted files. Use stream access views for sequential access to a file this is recommended for non-persisted files and IPC. There are two types of views: stream access view and random access view. Multiple views may also be necessary if the file is greater than the size of the application's logical memory space available for memory mapping (2 GB on a 32-bit computer). For two views to remain concurrent, they have to be created from the same memory-mapped file. You can also create multiple views to the same part of the memory-mapped file, thereby creating concurrent memory. To work with a memory-mapped file, you must create a view of the entire memory-mapped file or a part of it. Processes can map to the same memory-mapped file by using a common name that is assigned by the process that created the file. Memory-mapped files can be shared across multiple processes. These files are suitable for creating shared memory for inter-process communications (IPC). When the last process has finished working with the file, the data is lost and the file is reclaimed by garbage collection. Non-persisted files are memory-mapped files that are not associated with a file on a disk. These memory-mapped files are suitable for working with extremely large source files. When the last process has finished working with the file, the data is saved to the source file on the disk. Persisted files are memory-mapped files that are associated with a source file on a disk. There are two types of memory-mapped files: You can use managed code to access memory-mapped files in the same way that native Windows functions access memory-mapped files, as described in Managing Memory-Mapped Files. This mapping between a file and memory space enables an application, including multiple processes, to modify the file by reading and writing directly to the memory. ![]() It just makes it harder to reason about safety.A memory-mapped file contains the contents of a file in virtual memory. But this in and of itself doesn't make them inaccessible to you. The main problem in Rust land with memory maps is that they don't fit into Rust's safety story in an obvious way. ![]() ![]() You'd have the same problems if you read a file into heap memory. And when you use `unsafe`, you want to be sure that it's justified. With that said, this technique is not common in Rust because it requires `unsafe` to do it. The most obvious thing that is problematic for mmap structures like this that is otherwise easy to do is pointer indirection. ![]() That is, you need to build your data structures to be mmap friendly. But when you memory map stuff like this, you need to take at least all the same precautions as you would in C. You can tag your types with `repr(C)` to get a consistent memory layout equivalent to whatever C does. You should be able to do in Rust whatever you would do in C. > My argument was more about structured data (created using the type system), which is a level higher than raw bytes. The docs of the crate give links to papers if you want to drill down. So your code operates directly on a block of raw bytes? I can see how that can work with mmap without much problems.Ĭorrect.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |