Skip to content

Commit e2ef603

Browse files
committed
make ops structure flat
fixes: oneapi-src#1078
1 parent 6b3a124 commit e2ef603

21 files changed

+391
-323
lines changed

include/umf/memory_pool_ops.h

Lines changed: 14 additions & 2 deletions
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

Lines changed: 133 additions & 125 deletions
Large diffs are not rendered by default.

src/memory_pool.c

Lines changed: 21 additions & 8 deletions
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

@@ -58,18 +58,31 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
5858
}
5959

6060
umf_result_t ret = UMF_RESULT_SUCCESS;
61-
umf_memory_pool_handle_t pool =
62-
umf_ba_global_alloc(sizeof(umf_memory_pool_t));
63-
if (!pool) {
64-
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
65-
}
6661

6762
if (ops->version != UMF_POOL_OPS_VERSION_CURRENT) {
6863
LOG_WARN("Memory Pool ops version \"%d\" is different than the current "
6964
"version \"%d\"",
7065
ops->version, UMF_POOL_OPS_VERSION_CURRENT);
7166
}
7267

68+
if (ops->size == 0) {
69+
LOG_ERR("Memory Pool ops size is not set");
70+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
71+
}
72+
73+
if (ops->size > sizeof(umf_memory_pool_ops_t)) {
74+
LOG_ERR("Memory Pool ops size \"%zu\" is greater than the current "
75+
"size \"%zu\"",
76+
ops->size, sizeof(umf_memory_pool_ops_t));
77+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
78+
}
79+
80+
umf_memory_pool_handle_t pool =
81+
umf_ba_global_alloc(sizeof(umf_memory_pool_t));
82+
if (!pool) {
83+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
84+
}
85+
7386
if (!(flags & UMF_POOL_CREATE_FLAG_DISABLE_TRACKING)) {
7487
// Wrap provider with memory tracking provider.
7588
ret = umfTrackingMemoryProviderCreate(provider, pool, &pool->provider);
@@ -84,8 +97,8 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
8497
pool->ops = *ops;
8598
pool->tag = NULL;
8699

87-
if (NULL == pool->ops.ctl) {
88-
pool->ops.ctl = umfDefaultCtlPoolHandle;
100+
if (NULL == pool->ops.ext_ctl) {
101+
pool->ops.ext_ctl = umfDefaultCtlPoolHandle;
89102
}
90103

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

src/memory_provider.c

Lines changed: 77 additions & 54 deletions
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,
@@ -197,6 +208,18 @@ umf_result_t umfMemoryProviderCreate(const umf_memory_provider_ops_t *ops,
197208
ops->version, UMF_PROVIDER_OPS_VERSION_CURRENT);
198209
}
199210

211+
if (ops->size == 0) {
212+
LOG_ERR("Memory Provider ops size is zero");
213+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
214+
}
215+
216+
if (ops->size > sizeof(umf_memory_provider_ops_t)) {
217+
LOG_ERR("Memory Provider ops size \"%zu\" is greater than the "
218+
"current size \"%zu\"",
219+
ops->size, sizeof(umf_memory_provider_ops_t));
220+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
221+
}
222+
200223
umf_memory_provider_handle_t provider =
201224
umf_ba_global_alloc(sizeof(umf_memory_provider_t));
202225
if (!provider) {
@@ -300,7 +323,7 @@ umf_result_t umfMemoryProviderPurgeLazy(umf_memory_provider_handle_t hProvider,
300323
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
301324
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
302325
umf_result_t res =
303-
hProvider->ops.ext.purge_lazy(hProvider->provider_priv, ptr, size);
326+
hProvider->ops.ext_purge_lazy(hProvider->provider_priv, ptr, size);
304327
checkErrorAndSetLastProvider(res, hProvider);
305328
return res;
306329
}
@@ -310,7 +333,7 @@ umf_result_t umfMemoryProviderPurgeForce(umf_memory_provider_handle_t hProvider,
310333
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
311334
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
312335
umf_result_t res =
313-
hProvider->ops.ext.purge_force(hProvider->provider_priv, ptr, size);
336+
hProvider->ops.ext_purge_force(hProvider->provider_priv, ptr, size);
314337
checkErrorAndSetLastProvider(res, hProvider);
315338
return res;
316339
}
@@ -329,7 +352,7 @@ umfMemoryProviderAllocationSplit(umf_memory_provider_handle_t hProvider,
329352
UMF_RESULT_ERROR_INVALID_ARGUMENT);
330353
UMF_CHECK((firstSize < totalSize), UMF_RESULT_ERROR_INVALID_ARGUMENT);
331354

332-
umf_result_t res = hProvider->ops.ext.allocation_split(
355+
umf_result_t res = hProvider->ops.ext_allocation_split(
333356
hProvider->provider_priv, ptr, totalSize, firstSize);
334357
checkErrorAndSetLastProvider(res, hProvider);
335358
return res;
@@ -347,7 +370,7 @@ umfMemoryProviderAllocationMerge(umf_memory_provider_handle_t hProvider,
347370
UMF_CHECK(((uintptr_t)highPtr - (uintptr_t)lowPtr < totalSize),
348371
UMF_RESULT_ERROR_INVALID_ARGUMENT);
349372

350-
umf_result_t res = hProvider->ops.ext.allocation_merge(
373+
umf_result_t res = hProvider->ops.ext_allocation_merge(
351374
hProvider->provider_priv, lowPtr, highPtr, totalSize);
352375
checkErrorAndSetLastProvider(res, hProvider);
353376
return res;
@@ -358,7 +381,7 @@ umfMemoryProviderGetIPCHandleSize(umf_memory_provider_handle_t hProvider,
358381
size_t *size) {
359382
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
360383
UMF_CHECK((size != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
361-
return hProvider->ops.ipc.get_ipc_handle_size(hProvider->provider_priv,
384+
return hProvider->ops.ext_get_ipc_handle_size(hProvider->provider_priv,
362385
size);
363386
}
364387

@@ -369,7 +392,7 @@ umfMemoryProviderGetIPCHandle(umf_memory_provider_handle_t hProvider,
369392
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
370393
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
371394
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
372-
return hProvider->ops.ipc.get_ipc_handle(hProvider->provider_priv, ptr,
395+
return hProvider->ops.ext_get_ipc_handle(hProvider->provider_priv, ptr,
373396
size, providerIpcData);
374397
}
375398

@@ -378,7 +401,7 @@ umfMemoryProviderPutIPCHandle(umf_memory_provider_handle_t hProvider,
378401
void *providerIpcData) {
379402
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
380403
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
381-
return hProvider->ops.ipc.put_ipc_handle(hProvider->provider_priv,
404+
return hProvider->ops.ext_put_ipc_handle(hProvider->provider_priv,
382405
providerIpcData);
383406
}
384407

@@ -388,7 +411,7 @@ umfMemoryProviderOpenIPCHandle(umf_memory_provider_handle_t hProvider,
388411
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
389412
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
390413
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
391-
return hProvider->ops.ipc.open_ipc_handle(hProvider->provider_priv,
414+
return hProvider->ops.ext_open_ipc_handle(hProvider->provider_priv,
392415
providerIpcData, ptr);
393416
}
394417

@@ -397,6 +420,6 @@ umfMemoryProviderCloseIPCHandle(umf_memory_provider_handle_t hProvider,
397420
void *ptr, size_t size) {
398421
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
399422
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
400-
return hProvider->ops.ipc.close_ipc_handle(hProvider->provider_priv, ptr,
423+
return hProvider->ops.ext_close_ipc_handle(hProvider->provider_priv, ptr,
401424
size);
402425
}

src/pool/pool_disjoint.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ void disjoint_pool_finalize(void *pool) {
929929
}
930930

931931
static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS = {
932+
.size = sizeof(umf_memory_provider_ops_t),
932933
.version = UMF_VERSION_CURRENT,
933934
.initialize = disjoint_pool_initialize,
934935
.finalize = disjoint_pool_finalize,

src/pool/pool_jemalloc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ static umf_result_t op_get_last_allocation_error(void *pool) {
527527
}
528528

529529
static umf_memory_pool_ops_t UMF_JEMALLOC_POOL_OPS = {
530+
.size = sizeof(umf_memory_provider_ops_t),
530531
.version = UMF_POOL_OPS_VERSION_CURRENT,
531532
.initialize = op_initialize,
532533
.finalize = op_finalize,

src/pool/pool_proxy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ static umf_result_t proxy_get_last_allocation_error(void *pool) {
123123
}
124124

125125
static umf_memory_pool_ops_t UMF_PROXY_POOL_OPS = {
126+
.size = sizeof(umf_memory_provider_ops_t),
126127
.version = UMF_POOL_OPS_VERSION_CURRENT,
127128
.initialize = proxy_pool_initialize,
128129
.finalize = proxy_pool_finalize,

src/pool/pool_scalable.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ static umf_result_t pool_ctl(void *hPool, int operationType, const char *name,
444444
}
445445

446446
static umf_memory_pool_ops_t UMF_SCALABLE_POOL_OPS = {
447+
.size = sizeof(umf_memory_provider_ops_t),
447448
.version = UMF_POOL_OPS_VERSION_CURRENT,
448449
.initialize = tbb_pool_initialize,
449450
.finalize = tbb_pool_finalize,
@@ -454,7 +455,7 @@ static umf_memory_pool_ops_t UMF_SCALABLE_POOL_OPS = {
454455
.malloc_usable_size = tbb_malloc_usable_size,
455456
.free = tbb_free,
456457
.get_last_allocation_error = tbb_get_last_allocation_error,
457-
.ctl = pool_ctl};
458+
.ext_ctl = pool_ctl};
458459

459460
const umf_memory_pool_ops_t *umfScalablePoolOps(void) {
460461
return &UMF_SCALABLE_POOL_OPS;

0 commit comments

Comments
 (0)