3 ms·
Speaking of memory mapped files: how easy is it to allocate and store various data structures into mmapped files in languages such as C, C++, Rust, etc.? If it
by amelius 12y ago
Speaking of memory mapped files: how easy is it to allocate and store various data structures into mmapped files in languages such as C, C++, Rust, etc.?
If it is difficult, then why is this the case? Are languages lacking in this respect?
- wmf 12y agoIt mostly involves casting a pointer.
- amelius 12y agoIt also involves offsetting the pointer with the beginning of the mmapped area. And this gets really cumbersome if you have to do it for every pointer dereference. Basically all standard libraries are immediately unusable if the language does not provide support for this operation. For example, try storing a C++ std::map inside an mmapped file. Practically not possible AFAIK.
- xxxyy 12y agoYou just have to override the new operator (C++) or use a different malloc (C). All you need is to replace brk() with an appropriate mmap(). Don't know how it goes for Rust. C++ is especially appealing, because you can embed file-backed mmap allocations in chosen classes by overriding their new operator. So you could for example create a float array class that automagically allocates itself in a file-backed mmap region by a simple new Array() call. Edit: Python's numpy has a file-backed array: http://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html http://docs.scipy.org/doc/numpy/reference/generated/numpy.me...
- amelius 12y agowell, the problem is that the mmapped file is based at a different address every time you mmap it. (in realistic situations, and the general case) thus, every pointer dereference needs a base address. how is the support for this?
- xxxyy 12y agoThis is not an easy way to serialize objects, it is merely a way to help virtual memory manager recognize portions of memory that can be saved to disk in the first place. .so files are loaded through mmap, and the pointers problem is solved there through the mechanism of relocations. But please don't write raw memory objects to disk, use Google Protobuf or ASN.1