|
Packit Service |
7f3b24 |
// SPDX-License-Identifier: BSD-3-Clause
|
|
Packit Service |
7f3b24 |
/* Copyright (C) 2018 - 2020 Intel Corporation. */
|
|
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 |
#include <errno.h>
|
|
Packit |
345191 |
#include <sys/resource.h>
|
|
Packit |
345191 |
|
|
Packit |
345191 |
#define MB (1024 * 1024)
|
|
Packit |
345191 |
#define HEAP_LIMIT_SIMULATE (1024 * MB)
|
|
Packit |
345191 |
|
|
Packit |
345191 |
static char path[PATH_MAX]="/tmp/";
|
|
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 |
int main(int argc, char *argv[])
|
|
Packit |
345191 |
{
|
|
Packit |
345191 |
const size_t size = 512;
|
|
Packit |
345191 |
struct memkind *pmem_kind = NULL;
|
|
Packit |
345191 |
int err = 0;
|
|
Packit |
345191 |
errno = 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) {
|
|
Packit |
345191 |
if (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 |
|
|
Packit |
345191 |
// Operation below limit the current size of heap
|
|
Packit |
345191 |
// to show different place of allocation
|
|
Packit |
345191 |
const struct rlimit heap_limit = { HEAP_LIMIT_SIMULATE, HEAP_LIMIT_SIMULATE };
|
|
Packit |
345191 |
err = setrlimit(RLIMIT_DATA, &heap_limit);
|
|
Packit |
345191 |
if (err) {
|
|
Packit |
345191 |
fprintf(stderr, "Unable to set heap limit.\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
char *ptr_default = NULL;
|
|
Packit |
345191 |
char *ptr_default_not_possible = NULL;
|
|
Packit |
345191 |
char *ptr_pmem = NULL;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
fprintf(stdout,
|
|
Packit |
345191 |
"This example shows how to allocate memory using standard memory (MEMKIND_DEFAULT) "
|
|
Packit |
345191 |
"and file-backed kind of memory (PMEM).\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 |
ptr_default = (char *)memkind_malloc(MEMKIND_DEFAULT, size);
|
|
Packit |
345191 |
if (!ptr_default) {
|
|
Packit |
345191 |
fprintf(stderr, "Unable allocate 512 bytes in standard memory.\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
errno = 0;
|
|
Packit |
345191 |
ptr_default_not_possible = (char *)memkind_malloc(MEMKIND_DEFAULT,
|
|
Packit |
345191 |
HEAP_LIMIT_SIMULATE);
|
|
Packit |
345191 |
if (ptr_default_not_possible) {
|
|
Packit |
345191 |
fprintf(stderr,
|
|
Packit |
345191 |
"Failure, this allocation should not be possible "
|
|
Packit |
345191 |
"(expected result was NULL), because of setlimit function.\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
if (errno != ENOMEM) {
|
|
Packit |
345191 |
fprintf(stderr,
|
|
Packit |
345191 |
"Failure, this allocation should set errno to ENOMEM value, because of setlimit function.\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
errno = 0;
|
|
Packit |
345191 |
ptr_pmem = (char *)memkind_malloc(pmem_kind, HEAP_LIMIT_SIMULATE);
|
|
Packit |
345191 |
if (!ptr_pmem) {
|
|
Packit |
345191 |
fprintf(stderr, "Unable allocate HEAP_LIMIT_SIMULATE in file-backed memory.\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
if (errno != 0) {
|
|
Packit |
345191 |
fprintf(stderr, "Failure, this allocation should not set errno value.\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
sprintf(ptr_default, "Hello world from standard memory - ptr_default.\n");
|
|
Packit |
345191 |
sprintf(ptr_pmem, "Hello world from file-backed memory - ptr_pmem.\n");
|
|
Packit |
345191 |
|
|
Packit |
345191 |
fprintf(stdout, "%s", ptr_default);
|
|
Packit |
345191 |
fprintf(stdout, "%s", ptr_pmem);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
memkind_free(MEMKIND_DEFAULT, ptr_default);
|
|
Packit |
345191 |
memkind_free(pmem_kind, ptr_pmem);
|
|
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 was successfully allocated and released.\n");
|
|
Packit |
345191 |
|
|
Packit |
345191 |
return 0;
|
|
Packit |
345191 |
}
|