75
Notice the include on line 53. To use libpmem, use this include line, and link the C
program with libpmem using the -lpmem option when building under Linux.
Mapping a File
The libpmem library contains some convenience functions for memory mapping files.
Of course, your application can call mmap() on Linux or MapViewOfFile() on Windows
directly, but using libpmem has some advantages:
• libpmem knows the correct arguments to the operating system
mapping calls. For example, on Linux, it is not safe to flush changes
to persistent memory using the CPU instructions directly unless the
mapping is created with the MAP_SYNC flag to mmap().
• libpmem detects if the mapping is actually persistent memory and if
using the CPU instructions directly for flushing is safe.
Listing 6-2 shows how to memory map a file on a persistent memory-aware file
system into the application.
Listing 6-2. Mapping a persistent memory file
80
/ * create a pmem file and memory map it * /
81
if ((pmemaddr = pmem_map_file(argv[2], BUF_LEN,
82
PMEM_FILE_CREATE|PMEM_FILE_EXCL,
83
0666, &mapped_len, &is_pmem)) == NULL) {
84
perror("pmem_map_file");
85
exit(1);
86
}
As part of the persistent memory detection mentioned earlier, the flag is_pmem is
returned by pmem_map_file. It is the caller’s responsibility to use this flag to determine
how to flush changes to persistence. When making a range of memory persistent, the
caller can use the optimal flush provided by libpmem, pmem_persist, only if the is_pmem
flag is set. This is illustrated in the man page example excerpt in Listing 6-3.
Chapter 6 libpmem: low-level persistent memory support
Notice the include on line 53. To use libpmem, use this include line, and link the C
program with libpmem using the -lpmem option when building under Linux.
Mapping a File
The libpmem library contains some convenience functions for memory mapping files.
Of course, your application can call mmap() on Linux or MapViewOfFile() on Windows
directly, but using libpmem has some advantages:
• libpmem knows the correct arguments to the operating system
mapping calls. For example, on Linux, it is not safe to flush changes
to persistent memory using the CPU instructions directly unless the
mapping is created with the MAP_SYNC flag to mmap().
• libpmem detects if the mapping is actually persistent memory and if
using the CPU instructions directly for flushing is safe.
Listing 6-2 shows how to memory map a file on a persistent memory-aware file
system into the application.
Listing 6-2. Mapping a persistent memory file
80
/ * create a pmem file and memory map it * /
81
if ((pmemaddr = pmem_map_file(argv[2], BUF_LEN,
82
PMEM_FILE_CREATE|PMEM_FILE_EXCL,
83
0666, &mapped_len, &is_pmem)) == NULL) {
84
perror("pmem_map_file");
85
exit(1);
86
}
As part of the persistent memory detection mentioned earlier, the flag is_pmem is
returned by pmem_map_file. It is the caller’s responsibility to use this flag to determine
how to flush changes to persistence. When making a range of memory persistent, the
caller can use the optimal flush provided by libpmem, pmem_persist, only if the is_pmem
flag is set. This is illustrated in the man page example excerpt in Listing 6-3.
Chapter 6 libpmem: low-level persistent memory support
