170
2. Challenge: How to ensure that an outermost vector will properly
construct nested pmem_string with a proper instance of
pmem::allocator.
Solution: From C++11 and later, the std::scoped_allocator_
adaptor class template can be used with multilevel containers.
The purpose of this adaptor is to correctly initialize stateful
allocators in nested containers, such as when all levels of a nested
container must be placed in the same memory segment.
C++ Examples
This section presents several full-code examples demonstrating the use of libmemkind
using C and C++.
Using the pmem::allocator
As mentioned earlier, you can use pmem::allocator with any STL-like data structure.
The code sample in Listing 10-10 includes a pmem_allocator.h header file to use
pmem::allocator.
Listing 10-10. pmem_allocator.cpp: using pmem::allocator with std:vector
37 #include
38 #include
39 #include
40
41 int main(int argc, char *argv[]) {
42
const size_t pmem_max_size = 64 * 1024 * 1024; //64 MB
43
const std::string pmem_dir("/daxfs");
44
45
// Create allocator object
46
libmemkind::pmem::allocator
47
alc(pmem_dir, pmem_max_size);
48
Chapter 10 Volatile Use of persistent MeMory
2. Challenge: How to ensure that an outermost vector will properly
construct nested pmem_string with a proper instance of
pmem::allocator.
Solution: From C++11 and later, the std::scoped_allocator_
adaptor class template can be used with multilevel containers.
The purpose of this adaptor is to correctly initialize stateful
allocators in nested containers, such as when all levels of a nested
container must be placed in the same memory segment.
C++ Examples
This section presents several full-code examples demonstrating the use of libmemkind
using C and C++.
Using the pmem::allocator
As mentioned earlier, you can use pmem::allocator with any STL-like data structure.
The code sample in Listing 10-10 includes a pmem_allocator.h header file to use
pmem::allocator.
Listing 10-10. pmem_allocator.cpp: using pmem::allocator with std:vector
37 #include
38 #include
39 #include
40
41 int main(int argc, char *argv[]) {
42
const size_t pmem_max_size = 64 * 1024 * 1024; //64 MB
43
const std::string pmem_dir("/daxfs");
44
45
// Create allocator object
46
libmemkind::pmem::allocator
47
alc(pmem_dir, pmem_max_size);
48
Chapter 10 Volatile Use of persistent MeMory
