C++ bindings for libpmemobj (part 1) - pmem resident variables
One of the biggest hurdles and error prone things about our C API is that the user has to manually keep track of modifications to the persistent memory resident variables while in a transaction. A special semi-transparent template property class has been implemented to automatically add variable modifications to the transaction undo log.
Let’s start with the vector example from the previous tutorial series. It looked like this:
As you can see, the programmer has to remember to call
function before any modifications to the memory. In a simple case like this one
it might not be such a big deal, but once the code gets complex it may lead to
some difficult to find consistency issues.
By using the C++ API we can simplify this code like so:
The template class
pmem::obj::p does not add storage overhead. The size of
the vector structure is exactly the same as in the C version.
This mechanism works overriding
operator= and adding the memory to the undo log
before modification. It’s pretty straightforward.