Skip to content

Commit 84e7b71

Browse files
committed
Add Coarse provider
Add Coarse provider - a memory provider that can manage a memory (i.e. handle the alloc() and free() ops) of a given pre-allocated buffer or of an additional upstream provider (e.g. OS Memory Provider or providers that do not support the free() operation, like the file memory provider and the DevDax memory provider). Co-developed-by: Rafał Rudnicki <[email protected]> Co-developed-by: Lukasz Dorau <[email protected]> Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 3674f6f commit 84e7b71

File tree

12 files changed

+2495
-1
lines changed

12 files changed

+2495
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ More detailed documentation is available here: https://oneapi-src.github.io/unif
135135

136136
### Memory providers
137137

138+
#### Coarse Provider
139+
140+
A memory provider that can provide memory from:
141+
1) a given pre-allocated buffer (the fixed-size memory provider option) or
142+
2) from an additional upstream provider (e.g. provider that does not support the free() operation
143+
like the File memory provider or the DevDax memory provider - see below).
144+
138145
#### OS memory provider
139146

140147
A memory provider that provides memory from an operating system.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (C) 2023-2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#ifndef UMF_COARSE_PROVIDER_H
9+
#define UMF_COARSE_PROVIDER_H
10+
11+
#include <stdbool.h>
12+
#include <umf/memory_provider.h>
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
/// @brief Coarse Memory Provider allocation strategy
19+
typedef enum coarse_memory_provider_strategy_t {
20+
/// Always allocate a free block of the (size + alignment) size
21+
/// and cut out the properly aligned part leaving two remaining parts.
22+
/// It is the fastest strategy but causes memory fragmentation
23+
/// when alignment is greater than 0.
24+
/// It is the best strategy when alignment always equals 0.
25+
UMF_COARSE_MEMORY_STRATEGY_FASTEST = 0,
26+
27+
/// Check if the first free block of the 'size' size has the correct alignment.
28+
/// If not, use the `UMF_COARSE_MEMORY_STRATEGY_FASTEST` strategy.
29+
UMF_COARSE_MEMORY_STRATEGY_FASTEST_BUT_ONE,
30+
31+
/// Look through all free blocks of the 'size' size
32+
/// and choose the first one with the correct alignment.
33+
/// If none of them had the correct alignment,
34+
/// use the `UMF_COARSE_MEMORY_STRATEGY_FASTEST` strategy.
35+
UMF_COARSE_MEMORY_STRATEGY_CHECK_ALL_SIZE,
36+
37+
/// The maximum value (it has to be the last one).
38+
UMF_COARSE_MEMORY_STRATEGY_MAX
39+
} coarse_memory_provider_strategy_t;
40+
41+
/// @brief Coarse Memory Provider settings struct.
42+
typedef struct coarse_memory_provider_params_t {
43+
/// Handle to the upstream memory provider.
44+
/// It has to be NULL if init_buffer is set
45+
/// (exactly one of them has to be non-NULL).
46+
umf_memory_provider_handle_t upstream_memory_provider;
47+
48+
/// Memory allocation strategy.
49+
/// See coarse_memory_provider_strategy_t for details.
50+
coarse_memory_provider_strategy_t allocation_strategy;
51+
52+
/// A pre-allocated buffer that will be the only memory that
53+
/// the coarse provider can provide (the fixed-size memory provider option).
54+
/// If it is non-NULL, `init_buffer_size ` has to contain its size.
55+
/// It has to be NULL if upstream_memory_provider is set
56+
/// (exactly one of them has to be non-NULL).
57+
void *init_buffer;
58+
59+
/// Size of the initial buffer:
60+
/// 1) `init_buffer` if it is non-NULL xor
61+
/// 2) that will be allocated from the upstream_memory_provider
62+
/// (if it is non-NULL) in the `.initialize` operation.
63+
size_t init_buffer_size;
64+
65+
/// When it is true and the upstream_memory_provider is given,
66+
/// the init buffer (of `init_buffer_size` bytes) would be pre-allocated
67+
/// during creation time using the `upstream_memory_provider`.
68+
/// If upstream_memory_provider is not given,
69+
/// the init_buffer is always used instead
70+
/// (regardless of the value of this parameter).
71+
bool immediate_init_from_upstream;
72+
} coarse_memory_provider_params_t;
73+
74+
/// @brief Coarse Memory Provider stats (TODO move to CTL)
75+
typedef struct coarse_memory_provider_stats_t {
76+
/// Total allocation size.
77+
size_t alloc_size;
78+
79+
/// Size of used memory.
80+
size_t used_size;
81+
82+
/// Number of memory blocks allocated from the upstream provider.
83+
size_t num_upstream_blocks;
84+
85+
/// Total number of allocated memory blocks.
86+
size_t num_all_blocks;
87+
88+
/// Number of free memory blocks.
89+
size_t num_free_blocks;
90+
} coarse_memory_provider_stats_t;
91+
92+
umf_memory_provider_ops_t *umfCoarseMemoryProviderOps(void);
93+
94+
// TODO use CTL
95+
coarse_memory_provider_stats_t
96+
umfCoarseMemoryProviderGetStats(umf_memory_provider_handle_t provider);
97+
98+
#ifdef __cplusplus
99+
}
100+
#endif
101+
102+
#endif // UMF_COARSE_PROVIDER_H

scripts/docs_config/api.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ and operate on the provider.
8080
.. doxygenfile:: memory_provider.h
8181
:sections: define enum typedef func var
8282

83+
Coarse Provider
84+
------------------------------------------
85+
86+
A memory provider that can provide memory from:
87+
1) a given pre-allocated buffer (the fixed-size memory provider option) or
88+
2) from an additional upstream provider (e.g. provider that does not support the free() operation
89+
like the File memory provider or the DevDax memory provider - see below).
90+
91+
.. doxygenfile:: provider_coarse.h
92+
:sections: define enum typedef func var
93+
8394
OS Memory Provider
8495
------------------------------------------
8596

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ set(UMF_SOURCES
110110
memtarget.c
111111
mempolicy.c
112112
memspace.c
113+
provider/provider_coarse.c
113114
provider/provider_tracking.c
114115
critnib/critnib.c
115116
ravl/ravl.c
@@ -266,6 +267,7 @@ target_include_directories(
266267
umf
267268
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
268269
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
270+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ravl>
269271
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/critnib>
270272
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/provider>
271273
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/memspaces>

src/libumf.def.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ EXPORTS
1414
umfTearDown
1515
umfGetCurrentVersion
1616
umfCloseIPCHandle
17+
umfCoarseMemoryProviderGetStats
18+
umfCoarseMemoryProviderOps
1719
umfFree
1820
umfGetIPCHandle
1921
umfGetLastFailedMemoryProvider

src/libumf.map.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ UMF_1.0 {
88
umfTearDown;
99
umfGetCurrentVersion;
1010
umfCloseIPCHandle;
11+
umfCoarseMemoryProviderGetStats;
12+
umfCoarseMemoryProviderOps;
1113
umfFree;
1214
umfGetIPCHandle;
1315
umfGetLastFailedMemoryProvider;

0 commit comments

Comments
 (0)