Using the memorymap class
The memorymap class provides methods for mapping files or portions of files into memory.
Memory-mapped files allow a region of a file to be accessed as if it were a block of memory. This can be more efficient than traditional read/write calls for some access patterns, and allows multiple processes to share data by mapping the same file.
Changes made to the mapped region can be synced back to the file on disk. The static isSupported() method can be used to determine if memory mapping is supported on the current platform.
The following example creates a file, maps it into memory, reads and modifies data through the mapping, syncs the changes to disk, and detaches the mapping.
#include <rudiments/memorymap.h>
#include <rudiments/file.h>
#include <rudiments/permissions.h>
#include <rudiments/bytestring.h>
#include <rudiments/stdio.h>
int main(int argc, const char **argv) {
// check if memory mapping is supported
if (!memorymap::isSupported()) {
stdoutput.write("memory mapping is not supported\n");
return 1;
}
// create a file to map
file f;
f.open("testfile",O_RDWR|O_CREAT,
permissions::parsePermString("rw-rw-rw-"));
// write some data to it
const char *data="hello world, this is memory mapped data!";
f.write(data);
// map the file into memory
memorymap mm;
if (!mm.attach(f.getFileDescriptor(),0,f.getSize(),
PROT_READ|PROT_WRITE,
MAP_SHARED)) {
stdoutput.write("failed to map file\n");
return 1;
}
// read from the mapped region
char *mapped=(char *)mm.getData();
stdoutput.printf("mapped data: %.*s\n",(int)mm.getSize(),mapped);
// modify the data through the mapping
bytestring::copy(mapped,"HELLO WORLD",11);
stdoutput.printf("modified: %.*s\n",(int)mm.getSize(),mapped);
// sync the changes to disk
mm.sync(true,false);
stdoutput.write("synced to disk\n");
// detach the mapping
mm.detach();
f.close();
// clean up
file::remove("testfile");
}