Skip to content

Commit 9eacc88

Browse files
committed
make ops structure flat
fixes: #1078
1 parent 6b3a124 commit 9eacc88

18 files changed

+350
-318
lines changed

include/umf/memory_pool_ops.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ extern "C" {
2828
/// pointers.
2929
///
3030
typedef struct umf_memory_pool_ops_t {
31+
/// Size of this structure.
32+
/// Should be initialized with sizeof(umf_memory_pool_ops_t) in the current umf version
33+
size_t size;
3134
/// Version of the ops structure.
3235
/// Should be initialized using UMF_POOL_OPS_VERSION_CURRENT.
3336
uint32_t version;
@@ -126,6 +129,11 @@ typedef struct umf_memory_pool_ops_t {
126129
///
127130
umf_result_t (*get_last_allocation_error)(void *pool);
128131

132+
///
133+
/// Following functions, with ext prefix, are optional and memory pool implementation
134+
/// can keep them NULL.
135+
///
136+
129137
///
130138
/// @brief Control operation for the memory pool.
131139
/// The function is used to perform various control operations
@@ -139,8 +147,12 @@ typedef struct umf_memory_pool_ops_t {
139147
///
140148
/// @return umf_result_t result of the control operation.
141149
///
142-
umf_result_t (*ctl)(void *hPool, int operationType, const char *name,
143-
void *arg, umf_ctl_query_type_t queryType);
150+
umf_result_t (*ext_ctl)(void *hPool, int operationType, const char *name,
151+
void *arg, umf_ctl_query_type_t queryType);
152+
153+
/// Reserved for future use
154+
/// Note: When copying this structure, use its provided size field rather than using sizeof.
155+
char reserved[];
144156
} umf_memory_pool_ops_t;
145157

146158
#ifdef __cplusplus

include/umf/memory_provider_ops.h

+133-125
Large diffs are not rendered by default.

src/memory_pool.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static int CTL_SUBTREE_HANDLER(by_handle_pool)(void *ctx,
3030
umf_ctl_query_type_t queryType) {
3131
(void)indexes, (void)source;
3232
umf_memory_pool_handle_t hPool = (umf_memory_pool_handle_t)ctx;
33-
hPool->ops.ctl(hPool, /*unused*/ 0, extra_name, arg, queryType);
33+
hPool->ops.ext_ctl(hPool, /*unused*/ 0, extra_name, arg, queryType);
3434
return 0;
3535
}
3636

@@ -84,8 +84,8 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
8484
pool->ops = *ops;
8585
pool->tag = NULL;
8686

87-
if (NULL == pool->ops.ctl) {
88-
pool->ops.ctl = umfDefaultCtlPoolHandle;
87+
if (NULL == pool->ops.ext_ctl) {
88+
pool->ops.ext_ctl = umfDefaultCtlPoolHandle;
8989
}
9090

9191
if (NULL == utils_mutex_init(&pool->lock)) {

src/memory_provider.c

+65-54
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ static int CTL_SUBTREE_HANDLER(by_handle_provider)(
2727
umf_ctl_query_type_t queryType) {
2828
(void)indexes, (void)source;
2929
umf_memory_provider_handle_t hProvider = (umf_memory_provider_handle_t)ctx;
30-
hProvider->ops.ctl(hProvider->provider_priv, /*unused*/ 0, extra_name, arg,
31-
queryType);
30+
hProvider->ops.ext_ctl(hProvider->provider_priv, /*unused*/ 0, extra_name,
31+
arg, queryType);
3232
return 0;
3333
}
3434

@@ -120,67 +120,78 @@ static umf_result_t umfDefaultCtlHandle(void *provider, int operationType,
120120
}
121121

122122
void assignOpsExtDefaults(umf_memory_provider_ops_t *ops) {
123-
if (!ops->ext.purge_lazy) {
124-
ops->ext.purge_lazy = umfDefaultPurgeLazy;
123+
if (!ops->ext_purge_lazy) {
124+
ops->ext_purge_lazy = umfDefaultPurgeLazy;
125125
}
126-
if (!ops->ext.purge_force) {
127-
ops->ext.purge_force = umfDefaultPurgeForce;
126+
if (!ops->ext_purge_force) {
127+
ops->ext_purge_force = umfDefaultPurgeForce;
128128
}
129-
if (!ops->ext.allocation_split) {
130-
ops->ext.allocation_split = umfDefaultAllocationSplit;
129+
if (!ops->ext_allocation_split) {
130+
ops->ext_allocation_split = umfDefaultAllocationSplit;
131131
}
132-
if (!ops->ext.allocation_merge) {
133-
ops->ext.allocation_merge = umfDefaultAllocationMerge;
132+
if (!ops->ext_allocation_merge) {
133+
ops->ext_allocation_merge = umfDefaultAllocationMerge;
134134
}
135135
}
136136

137137
void assignOpsIpcDefaults(umf_memory_provider_ops_t *ops) {
138-
if (!ops->ipc.get_ipc_handle_size) {
139-
ops->ipc.get_ipc_handle_size = umfDefaultGetIPCHandleSize;
138+
if (!ops->ext_get_ipc_handle_size) {
139+
ops->ext_get_ipc_handle_size = umfDefaultGetIPCHandleSize;
140140
}
141-
if (!ops->ipc.get_ipc_handle) {
142-
ops->ipc.get_ipc_handle = umfDefaultGetIPCHandle;
141+
if (!ops->ext_get_ipc_handle) {
142+
ops->ext_get_ipc_handle = umfDefaultGetIPCHandle;
143143
}
144-
if (!ops->ipc.put_ipc_handle) {
145-
ops->ipc.put_ipc_handle = umfDefaultPutIPCHandle;
144+
if (!ops->ext_put_ipc_handle) {
145+
ops->ext_put_ipc_handle = umfDefaultPutIPCHandle;
146146
}
147-
if (!ops->ipc.open_ipc_handle) {
148-
ops->ipc.open_ipc_handle = umfDefaultOpenIPCHandle;
147+
if (!ops->ext_open_ipc_handle) {
148+
ops->ext_open_ipc_handle = umfDefaultOpenIPCHandle;
149149
}
150-
if (!ops->ipc.close_ipc_handle) {
151-
ops->ipc.close_ipc_handle = umfDefaultCloseIPCHandle;
150+
if (!ops->ext_close_ipc_handle) {
151+
ops->ext_close_ipc_handle = umfDefaultCloseIPCHandle;
152152
}
153-
if (!ops->ctl) {
154-
ops->ctl = umfDefaultCtlHandle;
153+
if (!ops->ext_ctl) {
154+
ops->ext_ctl = umfDefaultCtlHandle;
155155
}
156156
}
157157

158-
static bool validateOpsMandatory(const umf_memory_provider_ops_t *ops) {
159-
// Mandatory ops should be non-NULL
160-
return ops->alloc && ops->free && ops->get_recommended_page_size &&
161-
ops->get_min_page_size && ops->initialize && ops->finalize &&
162-
ops->get_last_native_error && ops->get_name;
163-
}
158+
#define CHECK_OP(ops, fn) \
159+
if (!(ops)->fn) { \
160+
LOG_ERR("Error: missing function pointer: %s\n", #fn); \
161+
return false; \
162+
}
164163

165-
static bool validateOpsExt(const umf_memory_provider_ext_ops_t *ext) {
166-
// split and merge functions should be both NULL or both non-NULL
167-
return (ext->allocation_split && ext->allocation_merge) ||
168-
(!ext->allocation_split && !ext->allocation_merge);
169-
}
164+
static bool validateOps(const umf_memory_provider_ops_t *ops) {
165+
// Validate mandatory operations one by one
166+
CHECK_OP(ops, alloc);
167+
CHECK_OP(ops, free);
168+
CHECK_OP(ops, get_recommended_page_size);
169+
CHECK_OP(ops, get_min_page_size);
170+
CHECK_OP(ops, initialize);
171+
CHECK_OP(ops, finalize);
172+
CHECK_OP(ops, get_last_native_error);
173+
CHECK_OP(ops, get_name);
174+
175+
if ((ops->ext_allocation_split == NULL) !=
176+
(ops->ext_allocation_merge == NULL)) {
177+
LOG_ERR("Error: ext_allocation_split and ext_allocation_merge must be "
178+
"both set or both NULL\n");
179+
return false;
180+
}
170181

171-
static bool validateOpsIpc(const umf_memory_provider_ipc_ops_t *ipc) {
172-
// valid if all ops->ipc.* are non-NULL or all are NULL
173-
return (ipc->get_ipc_handle_size && ipc->get_ipc_handle &&
174-
ipc->put_ipc_handle && ipc->open_ipc_handle &&
175-
ipc->close_ipc_handle) ||
176-
(!ipc->get_ipc_handle_size && !ipc->get_ipc_handle &&
177-
!ipc->put_ipc_handle && !ipc->open_ipc_handle &&
178-
!ipc->close_ipc_handle);
179-
}
182+
bool ipcAllSet = ops->ext_get_ipc_handle_size && ops->ext_get_ipc_handle &&
183+
ops->ext_put_ipc_handle && ops->ext_open_ipc_handle &&
184+
ops->ext_close_ipc_handle;
185+
bool ipcAllNull = !ops->ext_get_ipc_handle_size &&
186+
!ops->ext_get_ipc_handle && !ops->ext_put_ipc_handle &&
187+
!ops->ext_open_ipc_handle && !ops->ext_close_ipc_handle;
188+
if (!ipcAllSet && !ipcAllNull) {
189+
LOG_ERR("Error: IPC function pointers must be either all set or all "
190+
"NULL\n");
191+
return false;
192+
}
180193

181-
static bool validateOps(const umf_memory_provider_ops_t *ops) {
182-
return validateOpsMandatory(ops) && validateOpsExt(&(ops->ext)) &&
183-
validateOpsIpc(&(ops->ipc));
194+
return true;
184195
}
185196

186197
umf_result_t umfMemoryProviderCreate(const umf_memory_provider_ops_t *ops,
@@ -300,7 +311,7 @@ umf_result_t umfMemoryProviderPurgeLazy(umf_memory_provider_handle_t hProvider,
300311
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
301312
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
302313
umf_result_t res =
303-
hProvider->ops.ext.purge_lazy(hProvider->provider_priv, ptr, size);
314+
hProvider->ops.ext_purge_lazy(hProvider->provider_priv, ptr, size);
304315
checkErrorAndSetLastProvider(res, hProvider);
305316
return res;
306317
}
@@ -310,7 +321,7 @@ umf_result_t umfMemoryProviderPurgeForce(umf_memory_provider_handle_t hProvider,
310321
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
311322
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
312323
umf_result_t res =
313-
hProvider->ops.ext.purge_force(hProvider->provider_priv, ptr, size);
324+
hProvider->ops.ext_purge_force(hProvider->provider_priv, ptr, size);
314325
checkErrorAndSetLastProvider(res, hProvider);
315326
return res;
316327
}
@@ -329,7 +340,7 @@ umfMemoryProviderAllocationSplit(umf_memory_provider_handle_t hProvider,
329340
UMF_RESULT_ERROR_INVALID_ARGUMENT);
330341
UMF_CHECK((firstSize < totalSize), UMF_RESULT_ERROR_INVALID_ARGUMENT);
331342

332-
umf_result_t res = hProvider->ops.ext.allocation_split(
343+
umf_result_t res = hProvider->ops.ext_allocation_split(
333344
hProvider->provider_priv, ptr, totalSize, firstSize);
334345
checkErrorAndSetLastProvider(res, hProvider);
335346
return res;
@@ -347,7 +358,7 @@ umfMemoryProviderAllocationMerge(umf_memory_provider_handle_t hProvider,
347358
UMF_CHECK(((uintptr_t)highPtr - (uintptr_t)lowPtr < totalSize),
348359
UMF_RESULT_ERROR_INVALID_ARGUMENT);
349360

350-
umf_result_t res = hProvider->ops.ext.allocation_merge(
361+
umf_result_t res = hProvider->ops.ext_allocation_merge(
351362
hProvider->provider_priv, lowPtr, highPtr, totalSize);
352363
checkErrorAndSetLastProvider(res, hProvider);
353364
return res;
@@ -358,7 +369,7 @@ umfMemoryProviderGetIPCHandleSize(umf_memory_provider_handle_t hProvider,
358369
size_t *size) {
359370
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
360371
UMF_CHECK((size != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
361-
return hProvider->ops.ipc.get_ipc_handle_size(hProvider->provider_priv,
372+
return hProvider->ops.ext_get_ipc_handle_size(hProvider->provider_priv,
362373
size);
363374
}
364375

@@ -369,7 +380,7 @@ umfMemoryProviderGetIPCHandle(umf_memory_provider_handle_t hProvider,
369380
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
370381
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
371382
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
372-
return hProvider->ops.ipc.get_ipc_handle(hProvider->provider_priv, ptr,
383+
return hProvider->ops.ext_get_ipc_handle(hProvider->provider_priv, ptr,
373384
size, providerIpcData);
374385
}
375386

@@ -378,7 +389,7 @@ umfMemoryProviderPutIPCHandle(umf_memory_provider_handle_t hProvider,
378389
void *providerIpcData) {
379390
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
380391
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
381-
return hProvider->ops.ipc.put_ipc_handle(hProvider->provider_priv,
392+
return hProvider->ops.ext_put_ipc_handle(hProvider->provider_priv,
382393
providerIpcData);
383394
}
384395

@@ -388,7 +399,7 @@ umfMemoryProviderOpenIPCHandle(umf_memory_provider_handle_t hProvider,
388399
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
389400
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
390401
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
391-
return hProvider->ops.ipc.open_ipc_handle(hProvider->provider_priv,
402+
return hProvider->ops.ext_open_ipc_handle(hProvider->provider_priv,
392403
providerIpcData, ptr);
393404
}
394405

@@ -397,6 +408,6 @@ umfMemoryProviderCloseIPCHandle(umf_memory_provider_handle_t hProvider,
397408
void *ptr, size_t size) {
398409
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
399410
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
400-
return hProvider->ops.ipc.close_ipc_handle(hProvider->provider_priv, ptr,
411+
return hProvider->ops.ext_close_ipc_handle(hProvider->provider_priv, ptr,
401412
size);
402413
}

src/pool/pool_scalable.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ static umf_memory_pool_ops_t UMF_SCALABLE_POOL_OPS = {
454454
.malloc_usable_size = tbb_malloc_usable_size,
455455
.free = tbb_free,
456456
.get_last_allocation_error = tbb_get_last_allocation_error,
457-
.ctl = pool_ctl};
457+
.ext_ctl = pool_ctl};
458458

459459
const umf_memory_pool_ops_t *umfScalablePoolOps(void) {
460460
return &UMF_SCALABLE_POOL_OPS;

src/provider/provider_cuda.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -743,16 +743,16 @@ static umf_memory_provider_ops_t UMF_CUDA_MEMORY_PROVIDER_OPS = {
743743
.get_name = cu_memory_provider_get_name,
744744
// TODO
745745
/*
746-
.ext.purge_lazy = cu_memory_provider_purge_lazy,
747-
.ext.purge_force = cu_memory_provider_purge_force,
748-
.ext.allocation_merge = cu_memory_provider_allocation_merge,
749-
.ext.allocation_split = cu_memory_provider_allocation_split,
746+
.ext_purge_lazy = cu_memory_provider_purge_lazy,
747+
.ext_purge_force = cu_memory_provider_purge_force,
748+
.ext_allocation_merge = cu_memory_provider_allocation_merge,
749+
.ext_allocation_split = cu_memory_provider_allocation_split,
750750
*/
751-
.ipc.get_ipc_handle_size = cu_memory_provider_get_ipc_handle_size,
752-
.ipc.get_ipc_handle = cu_memory_provider_get_ipc_handle,
753-
.ipc.put_ipc_handle = cu_memory_provider_put_ipc_handle,
754-
.ipc.open_ipc_handle = cu_memory_provider_open_ipc_handle,
755-
.ipc.close_ipc_handle = cu_memory_provider_close_ipc_handle,
751+
.ext_get_ipc_handle_size = cu_memory_provider_get_ipc_handle_size,
752+
.ext_get_ipc_handle = cu_memory_provider_get_ipc_handle,
753+
.ext_put_ipc_handle = cu_memory_provider_put_ipc_handle,
754+
.ext_open_ipc_handle = cu_memory_provider_open_ipc_handle,
755+
.ext_close_ipc_handle = cu_memory_provider_close_ipc_handle,
756756
};
757757

758758
const umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void) {

src/provider/provider_devdax_memory.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -538,15 +538,15 @@ static umf_memory_provider_ops_t UMF_DEVDAX_MEMORY_PROVIDER_OPS = {
538538
.get_recommended_page_size = devdax_get_recommended_page_size,
539539
.get_min_page_size = devdax_get_min_page_size,
540540
.get_name = devdax_get_name,
541-
.ext.purge_lazy = devdax_purge_lazy,
542-
.ext.purge_force = devdax_purge_force,
543-
.ext.allocation_merge = devdax_allocation_merge,
544-
.ext.allocation_split = devdax_allocation_split,
545-
.ipc.get_ipc_handle_size = devdax_get_ipc_handle_size,
546-
.ipc.get_ipc_handle = devdax_get_ipc_handle,
547-
.ipc.put_ipc_handle = devdax_put_ipc_handle,
548-
.ipc.open_ipc_handle = devdax_open_ipc_handle,
549-
.ipc.close_ipc_handle = devdax_close_ipc_handle};
541+
.ext_purge_lazy = devdax_purge_lazy,
542+
.ext_purge_force = devdax_purge_force,
543+
.ext_allocation_merge = devdax_allocation_merge,
544+
.ext_allocation_split = devdax_allocation_split,
545+
.ext_get_ipc_handle_size = devdax_get_ipc_handle_size,
546+
.ext_get_ipc_handle = devdax_get_ipc_handle,
547+
.ext_put_ipc_handle = devdax_put_ipc_handle,
548+
.ext_open_ipc_handle = devdax_open_ipc_handle,
549+
.ext_close_ipc_handle = devdax_close_ipc_handle};
550550

551551
const umf_memory_provider_ops_t *umfDevDaxMemoryProviderOps(void) {
552552
return &UMF_DEVDAX_MEMORY_PROVIDER_OPS;

src/provider/provider_file_memory.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -863,15 +863,15 @@ static umf_memory_provider_ops_t UMF_FILE_MEMORY_PROVIDER_OPS = {
863863
.get_recommended_page_size = file_get_recommended_page_size,
864864
.get_min_page_size = file_get_min_page_size,
865865
.get_name = file_get_name,
866-
.ext.purge_lazy = file_purge_lazy,
867-
.ext.purge_force = file_purge_force,
868-
.ext.allocation_merge = file_allocation_merge,
869-
.ext.allocation_split = file_allocation_split,
870-
.ipc.get_ipc_handle_size = file_get_ipc_handle_size,
871-
.ipc.get_ipc_handle = file_get_ipc_handle,
872-
.ipc.put_ipc_handle = file_put_ipc_handle,
873-
.ipc.open_ipc_handle = file_open_ipc_handle,
874-
.ipc.close_ipc_handle = file_close_ipc_handle};
866+
.ext_purge_lazy = file_purge_lazy,
867+
.ext_purge_force = file_purge_force,
868+
.ext_allocation_merge = file_allocation_merge,
869+
.ext_allocation_split = file_allocation_split,
870+
.ext_get_ipc_handle_size = file_get_ipc_handle_size,
871+
.ext_get_ipc_handle = file_get_ipc_handle,
872+
.ext_put_ipc_handle = file_put_ipc_handle,
873+
.ext_open_ipc_handle = file_open_ipc_handle,
874+
.ext_close_ipc_handle = file_close_ipc_handle};
875875

876876
const umf_memory_provider_ops_t *umfFileMemoryProviderOps(void) {
877877
return &UMF_FILE_MEMORY_PROVIDER_OPS;

src/provider/provider_fixed_memory.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,16 @@ static umf_memory_provider_ops_t UMF_FIXED_MEMORY_PROVIDER_OPS = {
297297
.get_recommended_page_size = fixed_get_recommended_page_size,
298298
.get_min_page_size = fixed_get_min_page_size,
299299
.get_name = fixed_get_name,
300-
.ext.purge_lazy = fixed_purge_lazy,
301-
.ext.purge_force = fixed_purge_force,
302-
.ext.allocation_merge = fixed_allocation_merge,
303-
.ext.allocation_split = fixed_allocation_split,
304-
.ipc.get_ipc_handle_size = NULL,
305-
.ipc.get_ipc_handle = NULL,
306-
.ipc.put_ipc_handle = NULL,
307-
.ipc.open_ipc_handle = NULL,
308-
.ipc.close_ipc_handle = NULL,
309-
.ctl = fixed_ctl};
300+
.ext_purge_lazy = fixed_purge_lazy,
301+
.ext_purge_force = fixed_purge_force,
302+
.ext_allocation_merge = fixed_allocation_merge,
303+
.ext_allocation_split = fixed_allocation_split,
304+
.ext_get_ipc_handle_size = NULL,
305+
.ext_get_ipc_handle = NULL,
306+
.ext_put_ipc_handle = NULL,
307+
.ext_open_ipc_handle = NULL,
308+
.ext_close_ipc_handle = NULL,
309+
.ext_ctl = fixed_ctl};
310310

311311
const umf_memory_provider_ops_t *umfFixedMemoryProviderOps(void) {
312312
return &UMF_FIXED_MEMORY_PROVIDER_OPS;

0 commit comments

Comments
 (0)