Skip to content

Commit 40c6dfc

Browse files
committed
add post-initialize function to pools and providers
Split between initialize and post-initialize function is necessary for properly handling CTL defaults.
1 parent 856058d commit 40c6dfc

18 files changed

+254
-55
lines changed

.github/workflows/reusable_compatibility.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ jobs:
9898
run: >
9999
UMF_LOG="level:warning;flush:debug;output:stderr;pid:no"
100100
LD_LIBRARY_PATH=${{github.workspace}}/latest_version/build/lib/
101-
ctest --verbose
101+
ctest --verbose -E test_disjoint_pool
102102
103103
# Browse all folders in the examples directory, build them using the
104104
# latest UMF version, and run them, excluding those in the exclude list.
@@ -223,7 +223,7 @@ jobs:
223223
run: |
224224
$env:UMF_LOG="level:warning;flush:debug;output:stderr;pid:no"
225225
cp ${{github.workspace}}/latest_version/build/bin/Debug/umf.dll ${{github.workspace}}/tag_version/build/bin/Debug/umf.dll
226-
ctest -C Debug --verbose
226+
ctest -C Debug --verbose -E test_disjoint_pool
227227
228228
# Browse all folders in the examples directory, build them using the
229229
# latest UMF version, and run them, excluding those in the exclude list.
@@ -364,7 +364,7 @@ jobs:
364364
run: >
365365
UMF_LOG="level:warning;flush:debug;output:stderr;pid:no"
366366
LD_LIBRARY_PATH=${{github.workspace}}/latest_version/build/lib/
367-
ctest --verbose
367+
ctest --verbose -E test_disjoint_pool
368368
369369
# Browse all folders in the examples directory, build them using the
370370
# latest UMF version, and run them, excluding those in the exclude list.

include/umf/memory_pool_ops.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern "C" {
2222
/// @brief Version of the Memory Pool ops structure.
2323
/// NOTE: This is equal to the latest UMF version, in which the ops structure
2424
/// has been modified.
25-
#define UMF_POOL_OPS_VERSION_CURRENT UMF_MAKE_VERSION(1, 0)
25+
#define UMF_POOL_OPS_VERSION_CURRENT UMF_MAKE_VERSION(1, 1)
2626

2727
///
2828
/// @brief This structure comprises function pointers used by corresponding umfPool*
@@ -166,6 +166,18 @@ typedef struct umf_memory_pool_ops_t {
166166
const char *name, void *arg, size_t size,
167167
umf_ctl_query_type_t queryType, va_list args);
168168

169+
///
170+
/// @brief Post-initializes memory pool.
171+
/// @param provider memory provider that will be used for coarse-grain allocations.
172+
/// Should contain at least one memory provider.
173+
/// @param numProvider number of elements in the providers array
174+
/// @param params pool-specific params, or NULL for defaults
175+
/// @param pool [out] returns pointer to the pool
176+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
177+
///
178+
umf_result_t (*ext_post_initialize)(umf_memory_provider_handle_t provider,
179+
const void *params, void *pool);
180+
169181
} umf_memory_pool_ops_t;
170182

171183
#ifdef __cplusplus

include/umf/memory_provider_ops.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
/// @brief Version of the Memory Provider ops structure.
2222
/// NOTE: This is equal to the latest UMF version, in which the ops structure
2323
/// has been modified.
24-
#define UMF_PROVIDER_OPS_VERSION_CURRENT UMF_MAKE_VERSION(1, 0)
24+
#define UMF_PROVIDER_OPS_VERSION_CURRENT UMF_MAKE_VERSION(1, 1)
2525

2626
///
2727
/// @brief This structure comprises function pointers used by corresponding
@@ -278,6 +278,14 @@ typedef struct umf_memory_provider_ops_t {
278278
const char *name, void *arg, size_t size,
279279
umf_ctl_query_type_t queryType, va_list args);
280280

281+
///
282+
/// @brief Post-initializes memory provider.
283+
/// @param params provider-specific params, or NULL for defaults
284+
/// @param provider pointer to the provider
285+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
286+
///
287+
umf_result_t (*ext_post_initialize)(const void *params, void *provider);
288+
281289
} umf_memory_provider_ops_t;
282290

283291
#ifdef __cplusplus

src/memory_pool.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,26 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
200200
}
201201

202202
umf_result_t ret = UMF_RESULT_SUCCESS;
203-
203+
umf_memory_pool_ops_t compatible_ops;
204204
if (ops->version != UMF_POOL_OPS_VERSION_CURRENT) {
205205
LOG_WARN("Memory Pool ops version \"%d\" is different than the current "
206206
"version \"%d\"",
207207
ops->version, UMF_POOL_OPS_VERSION_CURRENT);
208+
209+
// Create a new ops compatible structure with the current version
210+
memset(&compatible_ops, 0, sizeof(compatible_ops));
211+
if (UMF_MINOR_VERSION(ops->version) == 0) {
212+
LOG_INFO("Detected 1.0 version of Memory Pool ops, "
213+
"upgrading to current version");
214+
memcpy(&compatible_ops, ops,
215+
offsetof(umf_memory_pool_ops_t, ext_post_initialize));
216+
} else {
217+
LOG_ERR("Memory Pool ops unknown version, which \"%d\" is not "
218+
"supported",
219+
ops->version);
220+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
221+
}
222+
ops = &compatible_ops;
208223
}
209224

210225
umf_memory_pool_handle_t pool =
@@ -261,7 +276,16 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
261276
}
262277
}
263278

279+
if (ops->ext_post_initialize != NULL) {
280+
ret = ops->ext_post_initialize(pool->provider, params, pool->pool_priv);
281+
if (ret != UMF_RESULT_SUCCESS) {
282+
LOG_ERR("Failed to post-initialize pool");
283+
goto err_pool_init;
284+
}
285+
}
286+
264287
*hPool = pool;
288+
265289
LOG_INFO("Memory pool created: %p", (void *)pool);
266290
return UMF_RESULT_SUCCESS;
267291

src/memory_provider.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdbool.h>
1212
#include <stdio.h>
1313
#include <stdlib.h>
14+
#include <string.h>
1415

1516
#include <umf/base.h>
1617
#include <umf/memory_provider.h>
@@ -119,6 +120,13 @@ static umf_result_t umfDefaultCloseIPCHandle(void *provider, void *ptr,
119120
return UMF_RESULT_ERROR_NOT_SUPPORTED;
120121
}
121122

123+
static umf_result_t umfDefaultPostInitialize(const void *params,
124+
void *provider) {
125+
(void)params;
126+
(void)provider;
127+
return UMF_RESULT_SUCCESS;
128+
}
129+
122130
static umf_result_t
123131
umfDefaultCtlHandle(void *provider, umf_ctl_query_source_t operationType,
124132
const char *name, void *arg, size_t size,
@@ -153,6 +161,10 @@ void assignOpsExtDefaults(umf_memory_provider_ops_t *ops) {
153161
if (!ops->ext_ctl) {
154162
ops->ext_ctl = umfDefaultCtlHandle;
155163
}
164+
165+
if (!ops->ext_post_initialize) {
166+
ops->ext_post_initialize = umfDefaultPostInitialize;
167+
}
156168
}
157169

158170
void assignOpsIpcDefaults(umf_memory_provider_ops_t *ops) {
@@ -224,10 +236,24 @@ umf_result_t umfMemoryProviderCreate(const umf_memory_provider_ops_t *ops,
224236
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
225237
}
226238

239+
umf_memory_provider_ops_t compatible_ops;
227240
if (ops->version != UMF_PROVIDER_OPS_VERSION_CURRENT) {
228241
LOG_WARN("Memory Provider ops version \"%d\" is different than the "
229242
"current version \"%d\"",
230243
ops->version, UMF_PROVIDER_OPS_VERSION_CURRENT);
244+
memset(&compatible_ops, 0, sizeof(compatible_ops));
245+
if (UMF_MINOR_VERSION(ops->version) == 0) {
246+
LOG_INFO("Detected 1.0 version of Memory Pool ops, "
247+
"upgrading to current version");
248+
memcpy(&compatible_ops, ops,
249+
offsetof(umf_memory_provider_ops_t, ext_post_initialize));
250+
} else {
251+
LOG_ERR("Memory Provider ops unknown version, which \"%d\" is not "
252+
"supported",
253+
ops->version);
254+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
255+
}
256+
ops = &compatible_ops;
231257
}
232258

233259
umf_memory_provider_handle_t provider =
@@ -250,6 +276,16 @@ umf_result_t umfMemoryProviderCreate(const umf_memory_provider_ops_t *ops,
250276

251277
provider->provider_priv = provider_priv;
252278

279+
if (provider->ops.ext_post_initialize != NULL) {
280+
ret = provider->ops.ext_post_initialize(params, provider_priv);
281+
if (ret != UMF_RESULT_SUCCESS) {
282+
LOG_ERR("Failed to post-initialize provider");
283+
provider->ops.finalize(provider_priv);
284+
umf_ba_global_free(provider);
285+
return ret;
286+
}
287+
}
288+
253289
*hProvider = provider;
254290

255291
return UMF_RESULT_SUCCESS;

src/pool/pool_disjoint.c

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,39 @@ static UTIL_ONCE_FLAG ctl_initialized = UTIL_ONCE_FLAG_INIT;
3535

3636
// Disable name ctl for 1.0 release
3737
#if 0
38-
static umf_result_t CTL_READ_HANDLER(name)(void *ctx,
39-
umf_ctl_query_source_t source,
40-
void *arg, size_t size,
41-
umf_ctl_index_utlist_t *indexes) {
38+
static umf_result_t CTL_READ_HANDLER(name)(void* ctx,
39+
umf_ctl_query_source_t source,
40+
void* arg, size_t size,
41+
umf_ctl_index_utlist_t* indexes) {
4242
(void)source, (void)indexes;
4343

44-
disjoint_pool_t *pool = (disjoint_pool_t *)ctx;
44+
disjoint_pool_t* pool = (disjoint_pool_t*)ctx;
4545

4646
if (arg == NULL) {
4747
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
4848
}
4949

5050
if (size > 0) {
51-
strncpy((char *)arg, pool->params.name, size - 1);
52-
((char *)arg)[size - 1] = '\0';
51+
strncpy((char*)arg, pool->params.name, size - 1);
52+
((char*)arg)[size - 1] = '\0';
5353
}
5454

5555
return UMF_RESULT_SUCCESS;
5656
}
5757

5858
static const struct ctl_argument CTL_ARG(name) = CTL_ARG_STRING(255);
5959

60-
static umf_result_t CTL_WRITE_HANDLER(name)(void *ctx,
61-
umf_ctl_query_source_t source,
62-
void *arg, size_t size,
63-
umf_ctl_index_utlist_t *indexes) {
60+
static umf_result_t CTL_WRITE_HANDLER(name)(void* ctx,
61+
umf_ctl_query_source_t source,
62+
void* arg, size_t size,
63+
umf_ctl_index_utlist_t* indexes) {
6464
(void)source, (void)indexes, (void)size;
65-
disjoint_pool_t *pool = (disjoint_pool_t *)ctx;
65+
disjoint_pool_t* pool = (disjoint_pool_t*)ctx;
6666
if (arg == NULL) {
6767
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
6868
}
6969

70-
strncpy(pool->params.name, (char *)arg, sizeof(pool->params.name) - 1);
70+
strncpy(pool->params.name, (char*)arg, sizeof(pool->params.name) - 1);
7171
pool->params.name[sizeof(pool->params.name) - 1] = '\0';
7272

7373
return UMF_RESULT_SUCCESS;
@@ -758,9 +758,21 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
758758
disjoint_pool->provider = provider;
759759
disjoint_pool->params = *dp_params;
760760

761+
*ppPool = (void *)disjoint_pool;
762+
763+
return UMF_RESULT_SUCCESS;
764+
}
765+
766+
umf_result_t
767+
disjoint_pool_post_initialize(umf_memory_provider_handle_t provider,
768+
const void *params, void *ppPool) {
769+
(void)params;
770+
disjoint_pool_t *disjoint_pool = (disjoint_pool_t *)ppPool;
771+
761772
disjoint_pool->known_slabs = critnib_new(free_slab, NULL);
762773
if (disjoint_pool->known_slabs == NULL) {
763-
goto err_free_disjoint_pool;
774+
umf_ba_global_free(disjoint_pool);
775+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
764776
}
765777

766778
// Generate buckets sized such as: 64, 96, 128, 192, ..., CutOff.
@@ -821,8 +833,6 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
821833
disjoint_pool->provider_min_page_size = 0;
822834
}
823835

824-
*ppPool = (void *)disjoint_pool;
825-
826836
return UMF_RESULT_SUCCESS;
827837

828838
err_free_buckets:
@@ -838,10 +848,6 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
838848

839849
err_free_known_slabs:
840850
critnib_delete(disjoint_pool->known_slabs);
841-
842-
err_free_disjoint_pool:
843-
umf_ba_global_free(disjoint_pool);
844-
845851
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
846852
}
847853

@@ -1146,7 +1152,7 @@ static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS = {
11461152
.get_last_allocation_error = disjoint_pool_get_last_allocation_error,
11471153
.get_name = disjoint_pool_get_name,
11481154
.ext_ctl = disjoint_pool_ctl,
1149-
};
1155+
.ext_post_initialize = disjoint_pool_post_initialize};
11501156

11511157
const umf_memory_pool_ops_t *umfDisjointPoolOps(void) {
11521158
return &UMF_DISJOINT_POOL_OPS;

0 commit comments

Comments
 (0)