Skip to content

Commit 5b35410

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 047432a commit 5b35410

17 files changed

+276
-50
lines changed

.github/workflows/reusable_compatibility.yml

Lines changed: 22 additions & 4 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.
@@ -221,9 +221,18 @@ jobs:
221221
- name: Run "tag" UMF tests with latest UMF libs (warnings enabled)
222222
working-directory: ${{github.workspace}}/tag_version/build
223223
run: |
224-
$env:UMF_LOG="level:warning;flush:debug;output:stderr;pid:no"
224+
$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+
# test_disjoint_pool tests also internals and we want to skip them during compatibility check
227+
ctest -C Debug --verbose -E test_disjoint_pool
228+
229+
- name: Run disabled tests individually with latest UMF libs (warnings enabled)
230+
working-directory: ${{github.workspace}}/tag_version/build
231+
run: >
232+
$env:UMF_LOG="level:warning;flush:debug;output:stderr;pid:no"
233+
cp ${{github.workspace}}/latest_version/build/bin/Debug/umf.dll ${{github.workspace}}/tag_version/build/bin/Debug/umf.dll
234+
# Run the test_disjoint_pool with the latest UMF libs, excluding disjoint pool internals
235+
./test/test_disjoint_pool --gtest_filter="-test.internals"
227236
228237
# Browse all folders in the examples directory, build them using the
229238
# latest UMF version, and run them, excluding those in the exclude list.
@@ -364,7 +373,16 @@ jobs:
364373
run: >
365374
UMF_LOG="level:warning;flush:debug;output:stderr;pid:no"
366375
LD_LIBRARY_PATH=${{github.workspace}}/latest_version/build/lib/
367-
ctest --verbose
376+
# test_disjoint_pool tests also internals and we want to skip them during compatibility check
377+
ctest --verbose -E test_disjoint_pool
378+
379+
- name: Run disabled tests individually with latest UMF libs (warnings enabled)
380+
working-directory: ${{github.workspace}}/tag_version/build
381+
run: >
382+
UMF_LOG="level:warning;flush:debug;output:stderr;pid:no"
383+
LD_LIBRARY_PATH=${{github.workspace}}/tag_version/build/lib/
384+
# Run the test_disjoint_pool with the latest UMF libs, excluding disjoint pool internals
385+
./test/test_disjoint_pool --gtest_filter="-test.internals"
368386
369387
# Browse all folders in the examples directory, build them using the
370388
# latest UMF version, and run them, excluding those in the exclude list.

include/umf/memory_pool_ops.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ typedef struct umf_memory_pool_ops_t {
3636

3737
///
3838
/// @brief Initializes memory pool.
39+
/// /details
40+
/// * The memory pool implementation *must* allocate the memory pool structure
41+
/// and return it by the \p pool parameter.
3942
/// @param provider memory provider that will be used for coarse-grain allocations.
4043
/// @param params pool-specific params, or NULL for defaults
4144
/// @param pool [out] returns pointer to the pool
@@ -185,7 +188,23 @@ typedef struct umf_memory_pool_ops_t {
185188
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on
186189
/// failure.
187190
///
191+
188192
umf_result_t (*ext_trim_memory)(void *pool, size_t minBytesToKeep);
193+
///
194+
/// @brief Post-initializes and set up memory pool.
195+
///
196+
/// \details
197+
/// * This function must be implemented if the pool/provider supports CTL that overrides defaults
198+
/// * This function *must* be called after the memory pool has been allocated in initialize function
199+
/// and is used to perform any additional setup required by the memory pool.
200+
/// * This function *may* be used to set up any additional resources required by the memory pool.
201+
/// * This function *may* be used to set up default values for the memory pool parameters set up by CTL.
202+
///
203+
/// @param pool pointer to the pool
204+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
205+
///
206+
umf_result_t (*ext_post_initialize)(void *pool);
207+
189208
} umf_memory_pool_ops_t;
190209

191210
#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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ static umf_result_t umfDefaultTrimMemory(void *provider,
219219
return UMF_RESULT_ERROR_NOT_SUPPORTED;
220220
}
221221

222+
static umf_result_t
223+
umfDefaultExtPostInitialize(void *pool) {
224+
(void)pool;
225+
return UMF_RESULT_SUCCESS;
226+
}
227+
222228
// logical sum (OR) of all umf_pool_create_flags_t flags
223229
static const umf_pool_create_flags_t UMF_POOL_CREATE_FLAG_ALL =
224230
UMF_POOL_CREATE_FLAG_OWN_PROVIDER | UMF_POOL_CREATE_FLAG_DISABLE_TRACKING;
@@ -251,7 +257,6 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
251257
}
252258

253259
umf_result_t ret = UMF_RESULT_SUCCESS;
254-
255260
umf_memory_pool_ops_t compatible_ops;
256261
if (ops->version != UMF_POOL_OPS_VERSION_CURRENT) {
257262
LOG_WARN("Memory Pool ops version \"%d\" is different than the current "
@@ -303,13 +308,17 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
303308
pool->ops.ext_trim_memory = umfDefaultTrimMemory;
304309
}
305310

311+
if (NULL == pool->ops.ext_post_initialize) {
312+
pool->ops.ext_post_initialize = umfDefaultExtPostInitialize;
313+
}
314+
306315
if (NULL == utils_mutex_init(&pool->lock)) {
307316
LOG_ERR("Failed to initialize mutex for pool");
308317
ret = UMF_RESULT_ERROR_UNKNOWN;
309318
goto err_lock_init;
310319
}
311320

312-
ret = ops->initialize(pool->provider, params, &pool->pool_priv);
321+
ret = pool->ops.initialize(pool->provider, params, &pool->pool_priv);
313322
if (ret != UMF_RESULT_SUCCESS) {
314323
goto err_pool_init;
315324
}
@@ -335,7 +344,15 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
335344
}
336345
}
337346

347+
ret =
348+
pool->ops.ext_post_initialize(pool->pool_priv);
349+
if (ret != UMF_RESULT_SUCCESS) {
350+
LOG_ERR("Failed to post-initialize pool");
351+
goto err_pool_init;
352+
}
353+
338354
*hPool = pool;
355+
339356
LOG_INFO("Memory pool created: %p", (void *)pool);
340357
return UMF_RESULT_SUCCESS;
341358

src/memory_provider.c

Lines changed: 34 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,14 @@ umf_result_t umfMemoryProviderCreate(const umf_memory_provider_ops_t *ops,
250276

251277
provider->provider_priv = provider_priv;
252278

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

255289
return UMF_RESULT_SUCCESS;

src/pool/pool_disjoint.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,15 @@ 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(void *ppPool) {
768+
disjoint_pool_t *disjoint_pool = (disjoint_pool_t *)ppPool;
769+
761770
disjoint_pool->known_slabs = critnib_new(free_slab, NULL);
762771
if (disjoint_pool->known_slabs == NULL) {
763772
goto err_free_disjoint_pool;
@@ -816,13 +825,11 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
816825
}
817826

818827
umf_result_t ret = umfMemoryProviderGetMinPageSize(
819-
provider, NULL, &disjoint_pool->provider_min_page_size);
828+
disjoint_pool->provider, NULL, &disjoint_pool->provider_min_page_size);
820829
if (ret != UMF_RESULT_SUCCESS) {
821830
disjoint_pool->provider_min_page_size = 0;
822831
}
823832

824-
*ppPool = (void *)disjoint_pool;
825-
826833
return UMF_RESULT_SUCCESS;
827834

828835
err_free_buckets:
@@ -841,7 +848,6 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
841848

842849
err_free_disjoint_pool:
843850
umf_ba_global_free(disjoint_pool);
844-
845851
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
846852
}
847853

@@ -1188,7 +1194,7 @@ static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS = {
11881194
.get_name = disjoint_pool_get_name,
11891195
.ext_ctl = disjoint_pool_ctl,
11901196
.ext_trim_memory = disjoint_pool_trim_memory,
1191-
};
1197+
.ext_post_initialize = disjoint_pool_post_initialize};
11921198

11931199
const umf_memory_pool_ops_t *umfDisjointPoolOps(void) {
11941200
return &UMF_DISJOINT_POOL_OPS;

0 commit comments

Comments
 (0)