166
// Create a PMEM memory pool with a specific size
err = memkind_create_pmem(path, PMEM_MAX_SIZE, &pmem_kind);
if (err) {
memkind_fatal(err);
}
char *pstring = memkind_malloc(pmem_kind, 100);
char *dstring = memkind_malloc(MEMKIND_DEFAULT, 100);
Freeing Allocated Memory
To avoid memory leaks, allocated memory can be freed using the memkind_free()
function, defined as:
void memkind_free(memkind_t kind, void *ptr);
memkind_free() causes the allocated memory referenced by ptr to be made
available for future allocations. This pointer must be returned by a previous call to
memkind_malloc(), memkind_calloc(), memkind_realloc(), or memkind_posix_
memalign(). Otherwise, if memkind_free(kind, ptr) was previously called, undefined
behavior occurs. If ptr is NULL, no operation is performed. In cases where the kind is
unknown in the context of the call to memkind_free(), NULL can be given as the kind
specified to memkind_free(), but this will require an internal lookup for the correct kind.
Always specify the correct kind because the lookup for kind could result in a serious
performance penalty.
Listing 10-8 shows four examples of memkind_free() being used. The first two specify
the kind, and the second two use NULL to detect the kind automatically.
Listing 10-8. Examples of memkind_free() usage
/* Free the memory by specifying the kind */
memkind_free(MEMKIND_DEFAULT, dstring);
memkind_free(PMEM_KIND, pstring);
/* Free the memory using automatic kind detection */
memkind_free(NULL, dstring);
memkind_free(NULL, pstring);
Chapter 10 Volatile Use of persistent MeMory
Précédent

- 190/457

Suivant