129
Changing a volatile application to start taking advantage of persistent memory
should rely on modifying structures and classes with only slight modifications to
functions. We will begin by modifying the queue_node structure by changing its layout as
shown in Listing 8-7.
Listing 8-7. A persistent queue implementation – modifying the queue_node struct
38
#include
39
#include
40
#include
41
#include
42
#include
43
44
struct queue_node {
45
pmem::obj::p value;
46
pmem::obj::persistent_ptr next;
47
};
48
49
struct queue {
...
100
private:
101
pmem::obj::persistent_ptr head = nullptr;
102
pmem::obj::persistent_ptr tail = nullptr;
103
};
As you can see, all the modifications are limited to replace the volatile pointers with
pmem:obj::persistent_ptr and to start using the p<> property.
Next, we modify a push() method, shown in Listing 8-8.
Listing 8-8. A persistent queue implementation – a persistent push() method
50
void
51
push(pmem::obj::pool_base &pop, int value)
52
{
53
pmem::obj::transaction::run(pop, [&]{
54
auto node = pmem::obj::make_persistent();
55
node->value = value;
Chapter 8 libpmemobj-Cpp: the adaptable language - C++ and persistent memory
Changing a volatile application to start taking advantage of persistent memory
should rely on modifying structures and classes with only slight modifications to
functions. We will begin by modifying the queue_node structure by changing its layout as
shown in Listing 8-7.
Listing 8-7. A persistent queue implementation – modifying the queue_node struct
38
#include
39
#include
40
#include
41
#include
42
#include
43
44
struct queue_node {
45
pmem::obj::p
46
pmem::obj::persistent_ptr
47
};
48
49
struct queue {
...
100
private:
101
pmem::obj::persistent_ptr
102
pmem::obj::persistent_ptr
103
};
As you can see, all the modifications are limited to replace the volatile pointers with
pmem:obj::persistent_ptr and to start using the p<> property.
Next, we modify a push() method, shown in Listing 8-8.
Listing 8-8. A persistent queue implementation – a persistent push() method
50
void
51
push(pmem::obj::pool_base &pop, int value)
52
{
53
pmem::obj::transaction::run(pop, [&]{
54
auto node = pmem::obj::make_persistent
55
node->value = value;
Chapter 8 libpmemobj-Cpp: the adaptable language - C++ and persistent memory
