95
POBJ_REALLOC(PMEMobjpool *pop, TOID *oidp, TYPE, size_t size)
POBJ_ZREALLOC(PMEMobjpool *pop, TOID *oidp, TYPE, size_t size)
POBJ_FREE(TOID *oidp)
These functions reserve the object in a temporary state, call the constructor you
provided, and then in one atomic action, mark the allocation as persistent. They will
insert the pointer to the newly initialized object into a variable of your choice.
If the new object needs to be merely zeroed, pmemobj_zalloc() does so without
requiring a constructor.
Because copying NULL-terminated strings is a common operation, libpmemobj
provides pmemobj_strdup() and its wide-char variant pmemobj_wcsdup() to handle
this. pmemobj_strdup() provides the same semantics as strdup(3) but operates on the
persistent memory heap associated with the memory pool.
Once you are done with the object, pmemobj_free() will deallocate the object while
zeroing the variable that stored the pointer to it. The pmemobj_free() function frees the
memory space represented by oidp, which must have been allocated by a previous call
to pmemobj_alloc(), pmemobj_xalloc(), pmemobj_zalloc(), pmemobj_realloc(),
or pmemobj_zrealloc(). The pmemobj_free() function provides the same semantics as
free(3), but instead of operating on the process heap supplied by the system, it operates
on the persistent memory heap.
Listing 7-5 shows a small example of allocating and freeing memory using the
libpmemobj API.
Listing 7-5. Using pmemobj_alloc() to allocate memory and using pmemobj_
free() to free it
33 /*
34 * pmemobj_alloc.c - An example to show how to use
35 *
pmemobj_alloc()
36 */
..
47 typedef uint32_t color;
48
49 static int paintball_init(PMEMobjpool *pop,
50
void *ptr, void *arg)
51 {
52
*(color *)ptr = time(0) & 0xffffff;
Chapter 7 libpmemobj: a Native traNsaCtioNal objeCt store
POBJ_REALLOC(PMEMobjpool *pop, TOID *oidp, TYPE, size_t size)
POBJ_ZREALLOC(PMEMobjpool *pop, TOID *oidp, TYPE, size_t size)
POBJ_FREE(TOID *oidp)
These functions reserve the object in a temporary state, call the constructor you
provided, and then in one atomic action, mark the allocation as persistent. They will
insert the pointer to the newly initialized object into a variable of your choice.
If the new object needs to be merely zeroed, pmemobj_zalloc() does so without
requiring a constructor.
Because copying NULL-terminated strings is a common operation, libpmemobj
provides pmemobj_strdup() and its wide-char variant pmemobj_wcsdup() to handle
this. pmemobj_strdup() provides the same semantics as strdup(3) but operates on the
persistent memory heap associated with the memory pool.
Once you are done with the object, pmemobj_free() will deallocate the object while
zeroing the variable that stored the pointer to it. The pmemobj_free() function frees the
memory space represented by oidp, which must have been allocated by a previous call
to pmemobj_alloc(), pmemobj_xalloc(), pmemobj_zalloc(), pmemobj_realloc(),
or pmemobj_zrealloc(). The pmemobj_free() function provides the same semantics as
free(3), but instead of operating on the process heap supplied by the system, it operates
on the persistent memory heap.
Listing 7-5 shows a small example of allocating and freeing memory using the
libpmemobj API.
Listing 7-5. Using pmemobj_alloc() to allocate memory and using pmemobj_
free() to free it
33 /*
34 * pmemobj_alloc.c - An example to show how to use
35 *
pmemobj_alloc()
36 */
..
47 typedef uint32_t color;
48
49 static int paintball_init(PMEMobjpool *pop,
50
void *ptr, void *arg)
51 {
52
*(color *)ptr = time(0) & 0xffffff;
Chapter 7 libpmemobj: a Native traNsaCtioNal objeCt store
