165
Allocating Memory
The memkind library provides memkind_malloc(), memkind_calloc(), and memkind_
realloc() functions for allocating memory, defined as follows:
void *memkind_malloc(memkind_t kind, size_t size);
void *memkind_calloc(memkind_t kind, size_t num, size_t size);
void *memkind_realloc(memkind_t kind, void *ptr, size_t size);
memkind_malloc() allocates size bytes of uninitialized memory of the specified kind.
The allocated space is suitably aligned (after possible pointer coercion) for storage of any
object type. If size is 0, then memkind_malloc() returns NULL.
memkind_calloc() allocates space for num objects, each is size bytes in length. The
result is identical to calling memkind_malloc() with an argument of num * size. The
exception is that the allocated memory is explicitly initialized to zero bytes. If num or size
is 0, then memkind_calloc() returns NULL.
memkind_realloc() changes the size of the previously allocated memory
referenced by ptr to size bytes of the specified kind. The contents of the memory
remain unchanged, up to the lesser of the new and old sizes. If the new size is larger,
the contents of the newly allocated portion of the memory are undefined. If successful,
the memory referenced by ptr is freed, and a pointer to the newly allocated memory is
returned.
The code example in Listing 10-7 shows how to allocate memory from DRAM and
persistent memory (pmem_kind) using memkind_malloc(). Rather than using the
common C library malloc() for DRAM and memkind_malloc() for persistent memory,
we recommend using a single library to simplify the code.
Listing 10-7. An example of allocating memory from both DRAM and persistent
memory
/*
* Allocates 100 bytes using appropriate "kind"
* of volatile memory
*/
Chapter 10 Volatile Use of persistent MeMory
Précédent

- 189/457

Suivant