/* * rpmem_memcpy_msync -- memcpy and msync */ static void * rpmem_memcpy_msync(void *pmemdest, const void *src, size_t len) { void *ret = pmem_memcpy(pmemdest, src, len, PMEM_F_MEM_NOFLUSH); pmem_msync(pmemdest, len); return ret; }
int main(int argc, char *argv[]) { int srcfd; int dstfd; char buf[BUF_LEN]; char *pmemaddr; int is_pmem; int cc; if (argc != 3) { fprintf(stderr, "usage: %s src-file dst-file\n", argv[0]); exit(1); } /* open src-file */ if ((srcfd = open(argv[1], O_RDONLY)) < 0) { perror(argv[1]); exit(1); } /* create a pmem file */ if ((dstfd = open(argv[2], O_CREAT|O_EXCL|O_RDWR, 0666)) < 0) { perror(argv[2]); exit(1); } /* allocate the pmem */ if ((errno = posix_fallocate(dstfd, 0, BUF_LEN)) != 0) { perror("posix_fallocate"); exit(1); } /* memory map it */ if ((pmemaddr = pmem_map(dstfd)) == NULL) { perror("pmem_map"); exit(1); } close(dstfd); /* determine if range is true pmem */ is_pmem = pmem_is_pmem(pmemaddr, BUF_LEN); /* read up to BUF_LEN from srcfd */ if ((cc = read(srcfd, buf, BUF_LEN)) < 0) { perror("read"); exit(1); } /* write it to the pmem */ if (is_pmem) { pmem_memcpy(pmemaddr, buf, cc); } else { memcpy(pmemaddr, buf, cc); pmem_msync(pmemaddr, cc); } close(srcfd); exit(0); }