|
Packit |
345191 |
/*
|
|
Packit |
345191 |
* Copyright (c) 2019 Intel Corporation
|
|
Packit |
345191 |
*
|
|
Packit |
345191 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
345191 |
* modification, are permitted provided that the following conditions
|
|
Packit |
345191 |
* are met:
|
|
Packit |
345191 |
*
|
|
Packit |
345191 |
* * Redistributions of source code must retain the above copyright
|
|
Packit |
345191 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
345191 |
*
|
|
Packit |
345191 |
* * Redistributions in binary form must reproduce the above copyright
|
|
Packit |
345191 |
* notice, this list of conditions and the following disclaimer in
|
|
Packit |
345191 |
* the documentation and/or other materials provided with the
|
|
Packit |
345191 |
* distribution.
|
|
Packit |
345191 |
*
|
|
Packit |
345191 |
* * Neither the name of Intel Corporation nor the names of its
|
|
Packit |
345191 |
* contributors may be used to endorse or promote products derived
|
|
Packit |
345191 |
* from this software without specific prior written permission.
|
|
Packit |
345191 |
*
|
|
Packit |
345191 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
345191 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
345191 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
345191 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
345191 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
345191 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
345191 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
345191 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
345191 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
345191 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE
|
|
Packit |
345191 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
345191 |
*/
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#include <memkind.h>
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#include <limits.h>
|
|
Packit |
345191 |
#include <stdio.h>
|
|
Packit |
345191 |
#include <stdlib.h>
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static char path[PATH_MAX]="/tmp/";
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#define MALLOC_SIZE 512U
|
|
Packit |
345191 |
#define REALLOC_SIZE 2048U
|
|
Packit |
345191 |
#define ALLOC_LIMIT 1000U
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static void *alloc_buffer[ALLOC_LIMIT];
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static void print_err_message(int err)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
char error_message[MEMKIND_ERROR_MESSAGE_SIZE];
|
|
Packit |
345191 |
memkind_error_message(err, error_message, MEMKIND_ERROR_MESSAGE_SIZE);
|
|
Packit |
345191 |
fprintf(stderr, "%s\n", error_message);
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static int allocate_pmem_and_default_kind(struct memkind *pmem_kind)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
unsigned i;
|
|
Packit |
345191 |
for(i = 0; i < ALLOC_LIMIT; i++) {
|
|
Packit |
345191 |
if (i%2)
|
|
Packit |
345191 |
alloc_buffer[i] = memkind_malloc(pmem_kind, MALLOC_SIZE);
|
|
Packit |
345191 |
else
|
|
Packit |
345191 |
alloc_buffer[i] = memkind_malloc(MEMKIND_DEFAULT, MALLOC_SIZE);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (!alloc_buffer[i]) {
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
return 0;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static int realloc_using_get_kind_only_on_pmem()
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
unsigned i;
|
|
Packit |
345191 |
for(i = 0; i < ALLOC_LIMIT; i++) {
|
|
Packit |
345191 |
if (memkind_detect_kind(alloc_buffer[i]) != MEMKIND_DEFAULT) {
|
|
Packit |
345191 |
void *temp = memkind_realloc(NULL, alloc_buffer[i], REALLOC_SIZE);
|
|
Packit |
345191 |
if (!temp) {
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
alloc_buffer[i] = temp;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
return 0;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static int verify_allocation_size(struct memkind *pmem_kind, size_t pmem_size)
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
unsigned i;
|
|
Packit |
345191 |
for(i = 0; i < ALLOC_LIMIT; i++) {
|
|
Packit |
345191 |
void *val = alloc_buffer[i];
|
|
Packit |
345191 |
if (i%2) {
|
|
Packit |
345191 |
if (memkind_malloc_usable_size(pmem_kind, val) != pmem_size ) {
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
} else {
|
|
Packit |
345191 |
if (memkind_malloc_usable_size(MEMKIND_DEFAULT, val) != MALLOC_SIZE) {
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
return 0;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
int main(int argc, char *argv[])
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
|
|
Packit |
345191 |
struct memkind *pmem_kind = NULL;
|
|
Packit |
345191 |
int err = 0;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (argc > 2) {
|
|
Packit |
345191 |
fprintf(stderr, "Usage: %s [pmem_kind_dir_path]\n", argv[0]);
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
} else if (argc == 2 && (realpath(argv[1], path) == NULL)) {
|
|
Packit |
345191 |
fprintf(stderr, "Incorrect pmem_kind_dir_path %s\n", argv[1]);
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
fprintf(stdout,
|
|
Packit |
345191 |
"This example shows how to distinguish allocation from different kinds using detect kind function"
|
|
Packit |
345191 |
"\nPMEM kind directory: %s\n", path);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
err = memkind_create_pmem(path, 0, &pmem_kind);
|
|
Packit |
345191 |
if (err) {
|
|
Packit |
345191 |
print_err_message(err);
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
fprintf(stdout, "Allocate to PMEM and DEFAULT kind.\n");
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (allocate_pmem_and_default_kind(pmem_kind)) {
|
|
Packit |
345191 |
fprintf(stderr, "allocate_pmem_and_default_kind().\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (verify_allocation_size(pmem_kind, MALLOC_SIZE)) {
|
|
Packit |
345191 |
fprintf(stderr, "verify_allocation_size() before resize.\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
fprintf(stdout,
|
|
Packit |
345191 |
"Reallocate memory only on PMEM kind using memkind_detect_kind().\n");
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (realloc_using_get_kind_only_on_pmem()) {
|
|
Packit |
345191 |
fprintf(stderr, "realloc_using_get_kind_only_on_pmem().\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
if (verify_allocation_size(pmem_kind, REALLOC_SIZE)) {
|
|
Packit |
345191 |
fprintf(stderr, "verify_allocation_size() after resize.\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
err = memkind_destroy_kind(pmem_kind);
|
|
Packit |
345191 |
if (err) {
|
|
Packit |
345191 |
print_err_message(err);
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
fprintf(stdout, "Memory from PMEM kind was successfully reallocated.\n");
|
|
Packit |
345191 |
|
|
Packit |
345191 |
return 0;
|
|
Packit |
345191 |
}
|