Skip to content

Commit da79756

Browse files
committed
Add umfFixedMemoryProviderParamsSetFlags()
Add umfFixedMemoryProviderParamsSetFlags() to set flags of Fixed-size Memory Provider. Add the UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR flag that makes it possible to create Fixed-size Memory Provider from a pointer belonging to a UMF memory pool. This pointer is already tracked in the UMF tracker, so we will have to remove this memory range <base, base+size) from the tracker. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent fc1cbed commit da79756

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

include/umf/providers/provider_fixed_memory.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -18,6 +18,14 @@ extern "C" {
1818
#define UMF_FIXED_RESULTS_START_FROM 4000
1919
/// @endcond
2020

21+
/// @brief Fixed Memory Provider flags
22+
typedef enum umf_fixed_memory_provider_flag {
23+
UMF_FIXED_FLAG_DEFAULT = 0, ///< the default - no flags set
24+
UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR =
25+
(1
26+
<< 0), ///< The Fixed Memory Provider is created from the UMF pool's pointer
27+
} umf_fixed_memory_provider_flag_t;
28+
2129
struct umf_fixed_memory_provider_params_t;
2230

2331
typedef struct umf_fixed_memory_provider_params_t
@@ -41,6 +49,13 @@ umf_result_t umfFixedMemoryProviderParamsCreate(
4149
umf_result_t umfFixedMemoryProviderParamsSetMemory(
4250
umf_fixed_memory_provider_params_handle_t hParams, void *ptr, size_t size);
4351

52+
/// @brief Set the memory region flags in params struct.
53+
/// @param hParams [in] handle to the parameters of the Fixed Memory Provider.
54+
/// @param flags [in] flags for the memory region.
55+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
56+
umf_result_t umfFixedMemoryProviderParamsSetFlags(
57+
umf_fixed_memory_provider_params_handle_t hParams, unsigned int flags);
58+
4459
/// @brief Destroy parameters struct.
4560
/// @param hParams [in] handle to the parameters of the Fixed Memory Provider.
4661
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.

src/libumf.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,6 @@ EXPORTS
134134
umfFixedMemoryProviderOps
135135
umfFixedMemoryProviderParamsCreate
136136
umfFixedMemoryProviderParamsDestroy
137+
umfFixedMemoryProviderParamsSetFlags
137138
umfLevelZeroMemoryProviderParamsSetFreePolicy
138139
umfLevelZeroMemoryProviderParamsSetDeviceOrdinal

src/libumf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ UMF_0.11 {
132132
umfFixedMemoryProviderOps;
133133
umfFixedMemoryProviderParamsCreate;
134134
umfFixedMemoryProviderParamsDestroy;
135+
umfFixedMemoryProviderParamsSetFlags;
135136
umfLevelZeroMemoryProviderParamsSetFreePolicy;
136137
umfLevelZeroMemoryProviderParamsSetDeviceOrdinal;
137138
} UMF_0.10;

src/provider/provider_fixed_memory.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@
2727
#define TLS_MSG_BUF_LEN 1024
2828

2929
typedef struct fixed_memory_provider_t {
30-
void *base; // base address of memory
31-
size_t size; // size of the memory region
32-
coarse_t *coarse; // coarse library handle
30+
void *base; // base address of memory
31+
size_t size; // size of the memory region
32+
unsigned int flags; // flags of the memory region
33+
coarse_t *coarse; // coarse library handle
34+
35+
// used only when the UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR flag is set
36+
size_t ptr_orig_size; // original size of the memory region in the tracker
3337
} fixed_memory_provider_t;
3438

3539
// Fixed Memory provider settings struct
3640
typedef struct umf_fixed_memory_provider_params_t {
3741
void *ptr;
3842
size_t size;
43+
unsigned int flags;
3944
} umf_fixed_memory_provider_params_t;
4045

4146
typedef struct fixed_last_native_error_t {
@@ -83,6 +88,7 @@ static umf_result_t fixed_allocation_merge_cb(void *provider, void *lowPtr,
8388

8489
static umf_result_t fixed_initialize(void *params, void **provider) {
8590
umf_result_t ret;
91+
size_t ptr_orig_size = 0;
8692

8793
if (params == NULL) {
8894
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
@@ -91,6 +97,15 @@ static umf_result_t fixed_initialize(void *params, void **provider) {
9197
umf_fixed_memory_provider_params_t *in_params =
9298
(umf_fixed_memory_provider_params_t *)params;
9399

100+
if (in_params->flags & UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR) {
101+
umf_memory_pool_handle_t pool = umfPoolByPtr(in_params->ptr);
102+
if (pool == NULL) {
103+
LOG_ERR("The UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR flag is set, but "
104+
"the given pointer does not belong to any UMF pool");
105+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
106+
}
107+
}
108+
94109
fixed_memory_provider_t *fixed_provider =
95110
umf_ba_global_alloc(sizeof(*fixed_provider));
96111
if (!fixed_provider) {
@@ -122,6 +137,8 @@ static umf_result_t fixed_initialize(void *params, void **provider) {
122137

123138
fixed_provider->base = in_params->ptr;
124139
fixed_provider->size = in_params->size;
140+
fixed_provider->flags = in_params->flags;
141+
fixed_provider->ptr_orig_size = ptr_orig_size;
125142

126143
// add the entire memory as a single block
127144
ret = coarse_add_memory_fixed(coarse, fixed_provider->base,
@@ -333,5 +350,26 @@ umf_result_t umfFixedMemoryProviderParamsSetMemory(
333350

334351
hParams->ptr = ptr;
335352
hParams->size = size;
353+
hParams->flags = 0;
354+
355+
return UMF_RESULT_SUCCESS;
356+
}
357+
358+
umf_result_t umfFixedMemoryProviderParamsSetFlags(
359+
umf_fixed_memory_provider_params_handle_t hParams, unsigned int flags) {
360+
if (hParams == NULL) {
361+
LOG_ERR("Memory Provider params handle is NULL");
362+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
363+
}
364+
365+
if ((flags & UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR) &&
366+
(umfPoolByPtr(hParams->ptr) == NULL)) {
367+
LOG_ERR("Cannot set the UMF_FIXED_FLAG_CREATE_FROM_POOL_PTR, because "
368+
"the given pointer does not belong to any UMF pool");
369+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
370+
}
371+
372+
hParams->flags = flags;
373+
336374
return UMF_RESULT_SUCCESS;
337375
}

0 commit comments

Comments
 (0)