|
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 |
|
|
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 |
struct memkind *pmem_kind_unlimited = 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 allocate memory with unlimited kind size."
|
|
Packit |
345191 |
"\nPMEM kind directory: %s\n", path);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
// Create PMEM partition with unlimited size
|
|
Packit |
345191 |
err = memkind_create_pmem(path, 0, &pmem_kind_unlimited);
|
|
Packit |
345191 |
if (err) {
|
|
Packit |
345191 |
print_err_message(err);
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
char *pmem_str10 = NULL;
|
|
Packit |
345191 |
char *pmem_str11 = NULL;
|
|
Packit |
345191 |
|
|
Packit |
345191 |
// Huge allocation
|
|
Packit |
345191 |
pmem_str10 = (char *)memkind_malloc(pmem_kind_unlimited, 32 * 1024 * 1024);
|
|
Packit |
345191 |
if (pmem_str10 == NULL) {
|
|
Packit |
345191 |
fprintf(stderr, "Unable to allocate pmem string (pmem_str10).\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
// Another huge allocation, kind size is only limited by OS resources
|
|
Packit |
345191 |
pmem_str11 = (char *)memkind_malloc(pmem_kind_unlimited, 32 * 1024 * 1024);
|
|
Packit |
345191 |
if (pmem_str11 == NULL) {
|
|
Packit |
345191 |
fprintf(stderr, "Unable to allocate pmem string (pmem_str11).\n");
|
|
Packit |
345191 |
return 1;
|
|
Packit |
345191 |
}
|
|
Packit |
345191 |
|
|
Packit |
345191 |
memkind_free(pmem_kind_unlimited, pmem_str10);
|
|
Packit |
345191 |
memkind_free(pmem_kind_unlimited, pmem_str11);
|
|
Packit |
345191 |
|
|
Packit |
345191 |
err = memkind_destroy_kind(pmem_kind_unlimited);
|
|
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 |
}
|