76
Listing 6-3. manpage.c: Using the is_pmem flag
74
/ * Flush above strcpy to persistence * /
75
if (is_pmem)
76
pmem_persist(pmemaddr, mapped_len);
77
else
78
pmem_msync(pmemaddr, mapped_len);
Listing 6-3 shows the convenience function pmem_msync(), which is just a small
wrapper around msync() or the Windows equivalent. You do not need to build in
different logic for Linux and Windows because libpmem handles this.
Copying to Persistent Memory
There are several interfaces in libpmem for optimally copying or zeroing ranges of
persistent memory. The simplest interface shown in Listing 6-4 is used to copy the block
of data from the source file to the persistent memory in the destination file and flush it to
persistence.
Listing 6-4. simple_copy.c: Copying to persistent memory
88
/ * read up to BUF_LEN from srcfd * /
89
if ((cc = read(srcfd, buf, BUF_LEN)) < 0) {
90
pmem_unmap(pmemaddr, mapped_len);
91
perror("read");
92
exit(1);
93
}
94
95
/ * write it to the pmem * /
96
if (is_pmem) {
97
pmem_memcpy_persist(pmemaddr, buf, cc);
98
} else {
99
memcpy(pmemaddr, buf, cc);
100
pmem_msync(pmemaddr, cc);
101
}
Chapter 6 libpmem: low-level persistent memory support
Précédent

- 104/457

Suivant