97
• Line 47: Defines a color that will be stored in the pool.
• Lines 49-54: The paintball_init() function is called when we
allocate memory (line 76). This function takes a pool and object
pointer, calculates a random hex value for the paintball color, and
persistently writes it to the pool. The program exits when the write
completes.
• Lines 59-70: Opens or creates a pool and acquires a pointer to the
root object within the pool.
• Line 72: Obtain a pointer to an offset within the pool.
• Lines 74-78: If the pointer in line 72 is not a valid object, we allocate
some space and call paintball_init().
• Lines 79-80: If the pointer in line 72 is a valid object, we read the color
value, print the string, and free the object.
Reserve/Publish API
The atomic allocation API will not help if
• There is more than one reference to the object that needs to be
updated
• There are multiple scalars that need to be updated
For example, if your program needs to subtract money from account A and add it
to account B, both operations must be done together. This can be done via the reserve/
publish API.
To use it, you specify any number of operations to be done. The operations may be
setting a scalar 64-bit value using pmemobj_set_value(), freeing an object with pmemobj_
defer_free(), or allocating it using pmemobj_reserve(). Of these, only the allocation
happens immediately, letting you do any initialization of the newly reserved object.
Modifications will not become persistent until pmemobj_publish() is called.
Functions provided by libpmemobj related to the reserve/publish feature are
PMEMoid pmemobj_reserve(PMEMobjpool *pop,
struct pobj_action *act, size_t size, uint64_t type_num);
void pmemobj_defer_free(PMEMobjpool *pop, PMEMoid oid,
Chapter 7 libpmemobj: a Native traNsaCtioNal objeCt store
• Line 47: Defines a color that will be stored in the pool.
• Lines 49-54: The paintball_init() function is called when we
allocate memory (line 76). This function takes a pool and object
pointer, calculates a random hex value for the paintball color, and
persistently writes it to the pool. The program exits when the write
completes.
• Lines 59-70: Opens or creates a pool and acquires a pointer to the
root object within the pool.
• Line 72: Obtain a pointer to an offset within the pool.
• Lines 74-78: If the pointer in line 72 is not a valid object, we allocate
some space and call paintball_init().
• Lines 79-80: If the pointer in line 72 is a valid object, we read the color
value, print the string, and free the object.
Reserve/Publish API
The atomic allocation API will not help if
• There is more than one reference to the object that needs to be
updated
• There are multiple scalars that need to be updated
For example, if your program needs to subtract money from account A and add it
to account B, both operations must be done together. This can be done via the reserve/
publish API.
To use it, you specify any number of operations to be done. The operations may be
setting a scalar 64-bit value using pmemobj_set_value(), freeing an object with pmemobj_
defer_free(), or allocating it using pmemobj_reserve(). Of these, only the allocation
happens immediately, letting you do any initialization of the newly reserved object.
Modifications will not become persistent until pmemobj_publish() is called.
Functions provided by libpmemobj related to the reserve/publish feature are
PMEMoid pmemobj_reserve(PMEMobjpool *pop,
struct pobj_action *act, size_t size, uint64_t type_num);
void pmemobj_defer_free(PMEMobjpool *pop, PMEMoid oid,
Chapter 7 libpmemobj: a Native traNsaCtioNal objeCt store
