124
Listing 8-5. Example of class with a volatile pointer as a class member
39
struct root {
40
int* vptr1;
41
int* vptr2;
42
};
43
44
int main(int argc, char *argv[]) {
45
auto pop = pmem::obj::pool::open("/daxfs/file", "tx");
46
47
auto r = pop.root();
48
49
int a1 = 1;
50
51
pmem::obj::transaction::run(pop, [&](){
52
auto ptr = pmem::obj::make_persistent(0);
53
r->vptr1 = ptr.get();
54
r->vptr2 = &a1;
55
});
56
57
return 0;
58
}
• Lines 39-42: We create a root structure with two volatile pointers as
members.
• Lines 51-52: Our application is assigning, transactionally, two virtual
addresses. One to an integer residing on the stack and the second to
an integer residing on persistent memory. What will happen if the
application crashes or exits after execution of the transaction and we
execute the application again? Since the variable a1 was residing on
the stack, the old value vanished. But what is the value assigned to
vptr1? Even if it resides on persistent memory, the volatile pointer
is no longer valid. With ASLR we are not guaranteed to get the same
virtual address again if we call mmap(). The pointer could point to
something, nothing, or garbage.
Chapter 8 libpmemobj-Cpp: the adaptable language - C++ and persistent memory
Listing 8-5. Example of class with a volatile pointer as a class member
39
struct root {
40
int* vptr1;
41
int* vptr2;
42
};
43
44
int main(int argc, char *argv[]) {
45
auto pop = pmem::obj::pool
46
47
auto r = pop.root();
48
49
int a1 = 1;
50
51
pmem::obj::transaction::run(pop, [&](){
52
auto ptr = pmem::obj::make_persistent
53
r->vptr1 = ptr.get();
54
r->vptr2 = &a1;
55
});
56
57
return 0;
58
}
• Lines 39-42: We create a root structure with two volatile pointers as
members.
• Lines 51-52: Our application is assigning, transactionally, two virtual
addresses. One to an integer residing on the stack and the second to
an integer residing on persistent memory. What will happen if the
application crashes or exits after execution of the transaction and we
execute the application again? Since the variable a1 was residing on
the stack, the old value vanished. But what is the value assigned to
vptr1? Even if it resides on persistent memory, the volatile pointer
is no longer valid. With ASLR we are not guaranteed to get the same
virtual address again if we call mmap(). The pointer could point to
something, nothing, or garbage.
Chapter 8 libpmemobj-Cpp: the adaptable language - C++ and persistent memory
