-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbasic.c
124 lines (104 loc) · 4.02 KB
/
basic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
*
* Copyright (C) 2024 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/
#include "umf/pools/pool_scalable.h"
#include "umf/providers/provider_os_memory.h"
#include <stdio.h>
#include <string.h>
int main(void) {
// A result object for storing UMF API result status
umf_result_t res;
// Create an OS memory provider. It is used for allocating memory from
// NUMA nodes visible to the operating system.
// Allocations are made with mmap. The default values of params result
// in an mmap call like this:
// mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)
umf_memory_provider_ops_t *provider_ops = umfOsMemoryProviderOps();
umf_os_memory_provider_params_handle_t params = NULL;
umf_memory_provider_handle_t provider;
res = umfOsMemoryProviderParamsCreate(¶ms);
if (res != UMF_RESULT_SUCCESS) {
printf("Failed to create OS memory provider params!\n");
return -1;
}
res = umfMemoryProviderCreate(provider_ops, params, &provider);
umfOsMemoryProviderParamsDestroy(params);
if (res != UMF_RESULT_SUCCESS) {
printf("Failed to create a memory provider!\n");
return -1;
}
printf("OS memory provider created at %p\n", (void *)provider);
// Allocate memory from memory provider
size_t alloc_size = 5000;
size_t alignment = 0;
void *ptr_provider = NULL;
res =
umfMemoryProviderAlloc(provider, alloc_size, alignment, &ptr_provider);
if (res != UMF_RESULT_SUCCESS) {
printf("Failed to allocate memory from the memory provider!\n");
goto memory_provider_destroy;
}
const char *strSource = "Allocated memory at";
// Write to the allocated memory
memset(ptr_provider, '\0', alloc_size);
strncpy(ptr_provider, strSource, alloc_size);
printf("%s %p with the memory provider at %p\n", (char *)ptr_provider,
(void *)ptr_provider, (void *)provider);
// Free allocated memory
res = umfMemoryProviderFree(provider, ptr_provider, alloc_size);
if (res != UMF_RESULT_SUCCESS) {
printf("Failed to free memory to the provider!\n");
goto memory_provider_destroy;
}
printf("Freed memory at %p\n", ptr_provider);
// Create a memory pool
umf_memory_pool_ops_t *pool_ops = umfScalablePoolOps();
void *pool_params = NULL;
umf_pool_create_flags_t flags = 0;
umf_memory_pool_handle_t pool;
res = umfPoolCreate(pool_ops, provider, pool_params, flags, &pool);
if (res != UMF_RESULT_SUCCESS) {
printf("Failed to create a pool!\n");
goto memory_provider_destroy;
}
printf("Scalable memory pool created at %p\n", (void *)pool);
// Allocate some memory in the pool
size_t num = 1;
alloc_size = 128;
char *ptr = umfPoolCalloc(pool, num, alloc_size);
if (!ptr) {
printf("Failed to allocate memory in the pool!\n");
goto memory_pool_destroy;
}
// Write a string to allocated memory
strncpy(ptr, strSource, alloc_size);
printf("%s %p\n", ptr, (void *)ptr);
// Retrieve a memory pool from a pointer, available with memory tracking
umf_memory_pool_handle_t check_pool = umfPoolByPtr(ptr);
printf("Memory at %p has been allocated from the pool at %p\n", (void *)ptr,
(void *)check_pool);
// Retrieve a memory provider from a pool
umf_memory_provider_handle_t check_provider;
res = umfPoolGetMemoryProvider(pool, &check_provider);
if (res != UMF_RESULT_SUCCESS) {
printf("Failed to retrieve a memory provider for the pool!\n");
goto memory_pool_destroy;
}
printf("Pool at %p has been allocated from the provider at %p\n",
(void *)pool, (void *)check_provider);
// Clean up.
umfFree(ptr);
umfPoolDestroy(pool);
umfMemoryProviderDestroy(provider);
return 0;
memory_pool_destroy:
umfPoolDestroy(pool);
memory_provider_destroy:
umfMemoryProviderDestroy(provider);
return -1;
}