forked from oneapi-src/unified-memory-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider_fixed_memory.c
More file actions
410 lines (337 loc) · 13.2 KB
/
provider_fixed_memory.c
File metadata and controls
410 lines (337 loc) · 13.2 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright (C) 2024-2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <umf.h>
#include <umf/memory_provider_ops.h>
#include <umf/providers/provider_fixed_memory.h>
#include "base_alloc_global.h"
#include "coarse.h"
#include "libumf.h"
#include "memory_pool_internal.h"
#include "provider_tracking.h"
#include "utils_common.h"
#include "utils_concurrency.h"
#include "utils_log.h"
#define TLS_MSG_BUF_LEN 1024
typedef struct fixed_memory_provider_t {
void *base; // base address of memory
size_t size; // size of the memory region
unsigned int flags; // flags of the memory region
coarse_t *coarse; // coarse library handle
// used only when the UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR flag is set
size_t ptr_orig_size; // original size of the memory region in the tracker
umf_memory_provider_handle_t
trackingProvider; // tracking provider of the original memory pool
} fixed_memory_provider_t;
// Fixed Memory provider settings struct
typedef struct umf_fixed_memory_provider_params_t {
void *ptr;
size_t size;
unsigned int flags;
} umf_fixed_memory_provider_params_t;
typedef struct fixed_last_native_error_t {
int32_t native_error;
int errno_value;
char msg_buff[TLS_MSG_BUF_LEN];
} fixed_last_native_error_t;
static __TLS fixed_last_native_error_t TLS_last_native_error;
// helper values used only in the Native_error_str array
#define _UMF_FIXED_RESULT_SUCCESS \
(UMF_FIXED_RESULT_SUCCESS - UMF_FIXED_RESULT_SUCCESS)
#define _UMF_FIXED_RESULT_ERROR_PURGE_FORCE_FAILED \
(UMF_FIXED_RESULT_ERROR_PURGE_FORCE_FAILED - UMF_FIXED_RESULT_SUCCESS)
static const char *Native_error_str[] = {
[_UMF_FIXED_RESULT_SUCCESS] = "success",
[_UMF_FIXED_RESULT_ERROR_PURGE_FORCE_FAILED] = "force purging failed"};
static void fixed_store_last_native_error(int32_t native_error,
int errno_value) {
TLS_last_native_error.native_error = native_error;
TLS_last_native_error.errno_value = errno_value;
}
static umf_result_t fixed_allocation_split_cb(void *provider, void *ptr,
size_t totalSize,
size_t firstSize) {
(void)provider;
(void)ptr;
(void)totalSize;
(void)firstSize;
return UMF_RESULT_SUCCESS;
}
static umf_result_t fixed_allocation_merge_cb(void *provider, void *lowPtr,
void *highPtr, size_t totalSize) {
(void)provider;
(void)lowPtr;
(void)highPtr;
(void)totalSize;
return UMF_RESULT_SUCCESS;
}
static umf_result_t fixed_initialize(void *params, void **provider) {
umf_result_t ret;
size_t ptr_orig_size = 0;
umf_memory_provider_handle_t trackingProvider = NULL;
if (params == NULL) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
umf_fixed_memory_provider_params_t *in_params =
(umf_fixed_memory_provider_params_t *)params;
if (in_params->flags & UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR) {
umf_memory_pool_handle_t pool = umfPoolByPtr(in_params->ptr);
if (pool == NULL) {
LOG_ERR("The UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR flag is set, but "
"the given pointer does not belong to any UMF pool");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
ret = umfPoolGetTrackingProvider(pool, &trackingProvider);
if (ret != UMF_RESULT_SUCCESS) {
LOG_ERR("cannot get the tracking provider of the pool %p", pool);
return ret;
}
ret = trackerShrinkEntry(trackingProvider, in_params->ptr,
in_params->size, &ptr_orig_size);
if (ret != UMF_RESULT_SUCCESS) {
LOG_ERR(
"cannot shrink the allocation %p in the tracker to size %zu",
in_params->ptr, in_params->size);
return ret;
}
}
fixed_memory_provider_t *fixed_provider =
umf_ba_global_alloc(sizeof(*fixed_provider));
if (!fixed_provider) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
memset(fixed_provider, 0, sizeof(*fixed_provider));
coarse_params_t coarse_params = {0};
coarse_params.provider = fixed_provider;
coarse_params.page_size = utils_get_page_size();
// The alloc callback is not available in case of the fixed provider
// because it is a fixed-size memory provider
// and the entire memory is added as a single block
// to the coarse library.
coarse_params.cb.alloc = NULL;
coarse_params.cb.free = NULL; // not available for the fixed provider
coarse_params.cb.split = fixed_allocation_split_cb;
coarse_params.cb.merge = fixed_allocation_merge_cb;
coarse_t *coarse = NULL;
ret = coarse_new(&coarse_params, &coarse);
if (ret != UMF_RESULT_SUCCESS) {
LOG_ERR("coarse_new() failed");
goto err_free_fixed_provider;
}
fixed_provider->coarse = coarse;
fixed_provider->base = in_params->ptr;
fixed_provider->size = in_params->size;
fixed_provider->flags = in_params->flags;
fixed_provider->ptr_orig_size = ptr_orig_size;
fixed_provider->trackingProvider = trackingProvider;
// add the entire memory as a single block
ret = coarse_add_memory_fixed(coarse, fixed_provider->base,
fixed_provider->size);
if (ret != UMF_RESULT_SUCCESS) {
LOG_ERR("adding memory block failed");
goto err_coarse_delete;
}
*provider = fixed_provider;
return UMF_RESULT_SUCCESS;
err_coarse_delete:
coarse_delete(fixed_provider->coarse);
err_free_fixed_provider:
umf_ba_global_free(fixed_provider);
return ret;
}
static void fixed_finalize(void *provider) {
fixed_memory_provider_t *fixed_provider = provider;
coarse_delete(fixed_provider->coarse);
if (fixed_provider->trackingProvider &&
(fixed_provider->ptr_orig_size >= fixed_provider->size)) {
umf_result_t ret = trackerGrowEntry(
fixed_provider->trackingProvider, fixed_provider->base,
fixed_provider->size, fixed_provider->ptr_orig_size);
if (ret != UMF_RESULT_SUCCESS) {
LOG_ERR("cannot grow the allocation %p in the tracker (from size "
"%zu to size %zu)",
fixed_provider->base, fixed_provider->size,
fixed_provider->ptr_orig_size);
}
}
umf_ba_global_free(fixed_provider);
}
static umf_result_t fixed_alloc(void *provider, size_t size, size_t alignment,
void **resultPtr) {
fixed_memory_provider_t *fixed_provider =
(fixed_memory_provider_t *)provider;
return coarse_alloc(fixed_provider->coarse, size, alignment, resultPtr);
}
static void fixed_get_last_native_error(void *provider, const char **ppMessage,
int32_t *pError) {
(void)provider; // unused
if (ppMessage == NULL || pError == NULL) {
assert(0);
return;
}
*pError = TLS_last_native_error.native_error;
if (TLS_last_native_error.errno_value == 0) {
*ppMessage = Native_error_str[*pError - UMF_FIXED_RESULT_SUCCESS];
return;
}
const char *msg;
size_t len;
size_t pos = 0;
msg = Native_error_str[*pError - UMF_FIXED_RESULT_SUCCESS];
len = strlen(msg);
memcpy(TLS_last_native_error.msg_buff + pos, msg, len + 1);
pos += len;
msg = ": ";
len = strlen(msg);
memcpy(TLS_last_native_error.msg_buff + pos, msg, len + 1);
pos += len;
utils_strerror(TLS_last_native_error.errno_value,
TLS_last_native_error.msg_buff + pos, TLS_MSG_BUF_LEN - pos);
*ppMessage = TLS_last_native_error.msg_buff;
}
static umf_result_t fixed_get_recommended_page_size(void *provider, size_t size,
size_t *page_size) {
(void)provider; // unused
(void)size; // unused
*page_size = utils_get_page_size();
return UMF_RESULT_SUCCESS;
}
static umf_result_t fixed_get_min_page_size(void *provider, void *ptr,
size_t *page_size) {
(void)ptr; // unused
return fixed_get_recommended_page_size(provider, 0, page_size);
}
static umf_result_t fixed_purge_lazy(void *provider, void *ptr, size_t size) {
(void)provider; // unused
(void)ptr; // unused
(void)size; // unused
// purge_lazy is unsupported in case of the fixed memory provider
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}
static umf_result_t fixed_purge_force(void *provider, void *ptr, size_t size) {
(void)provider; // unused
errno = 0;
if (utils_purge(ptr, size, UMF_PURGE_FORCE)) {
fixed_store_last_native_error(UMF_FIXED_RESULT_ERROR_PURGE_FORCE_FAILED,
errno);
LOG_PERR("force purging failed");
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
}
return UMF_RESULT_SUCCESS;
}
static const char *fixed_get_name(void *provider) {
(void)provider; // unused
return "FIXED";
}
static umf_result_t fixed_allocation_split(void *provider, void *ptr,
size_t totalSize, size_t firstSize) {
fixed_memory_provider_t *fixed_provider =
(fixed_memory_provider_t *)provider;
return coarse_split(fixed_provider->coarse, ptr, totalSize, firstSize);
}
static umf_result_t fixed_allocation_merge(void *provider, void *lowPtr,
void *highPtr, size_t totalSize) {
fixed_memory_provider_t *fixed_provider =
(fixed_memory_provider_t *)provider;
return coarse_merge(fixed_provider->coarse, lowPtr, highPtr, totalSize);
}
static umf_result_t fixed_free(void *provider, void *ptr, size_t size) {
fixed_memory_provider_t *fixed_provider =
(fixed_memory_provider_t *)provider;
return coarse_free(fixed_provider->coarse, ptr, size);
}
static umf_memory_provider_ops_t UMF_FIXED_MEMORY_PROVIDER_OPS = {
.version = UMF_PROVIDER_OPS_VERSION_CURRENT,
.initialize = fixed_initialize,
.finalize = fixed_finalize,
.alloc = fixed_alloc,
.free = fixed_free,
.get_last_native_error = fixed_get_last_native_error,
.get_recommended_page_size = fixed_get_recommended_page_size,
.get_min_page_size = fixed_get_min_page_size,
.get_name = fixed_get_name,
.ext.purge_lazy = fixed_purge_lazy,
.ext.purge_force = fixed_purge_force,
.ext.allocation_merge = fixed_allocation_merge,
.ext.allocation_split = fixed_allocation_split,
.ipc.get_ipc_handle_size = NULL,
.ipc.get_ipc_handle = NULL,
.ipc.put_ipc_handle = NULL,
.ipc.open_ipc_handle = NULL,
.ipc.close_ipc_handle = NULL};
umf_memory_provider_ops_t *umfFixedMemoryProviderOps(void) {
return &UMF_FIXED_MEMORY_PROVIDER_OPS;
}
umf_result_t umfFixedMemoryProviderParamsCreate(
umf_fixed_memory_provider_params_handle_t *hParams, void *ptr,
size_t size) {
libumfInit();
if (hParams == NULL) {
LOG_ERR("Memory Provider params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
umf_fixed_memory_provider_params_handle_t params =
umf_ba_global_alloc(sizeof(*params));
if (params == NULL) {
LOG_ERR("Allocating memory for the Memory Provider params failed");
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
umf_result_t ret = umfFixedMemoryProviderParamsSetMemory(params, ptr, size);
if (ret != UMF_RESULT_SUCCESS) {
umf_ba_global_free(params);
return ret;
}
*hParams = params;
return UMF_RESULT_SUCCESS;
}
umf_result_t umfFixedMemoryProviderParamsDestroy(
umf_fixed_memory_provider_params_handle_t hParams) {
if (hParams != NULL) {
umf_ba_global_free(hParams);
}
return UMF_RESULT_SUCCESS;
}
umf_result_t umfFixedMemoryProviderParamsSetMemory(
umf_fixed_memory_provider_params_handle_t hParams, void *ptr, size_t size) {
if (hParams == NULL) {
LOG_ERR("Memory Provider params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
if (ptr == NULL) {
LOG_ERR("Memory pointer is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
if (size == 0) {
LOG_ERR("Size must be greater than 0");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
hParams->ptr = ptr;
hParams->size = size;
hParams->flags = 0;
return UMF_RESULT_SUCCESS;
}
umf_result_t umfFixedMemoryProviderParamsSetFlags(
umf_fixed_memory_provider_params_handle_t hParams, unsigned int flags) {
if (hParams == NULL) {
LOG_ERR("Memory Provider params handle is NULL");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
if ((flags & UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR) &&
(umfPoolByPtr(hParams->ptr) == NULL)) {
LOG_ERR("Cannot set the UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR, because "
"the given pointer does not belong to any UMF pool");
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}
hParams->flags = flags;
return UMF_RESULT_SUCCESS;
}