Skip to content

Commit 51f1429

Browse files
authored
Merge pull request #1265 from lplewa/flat
make ops structure flat
2 parents 77ef317 + 8bc687d commit 51f1429

20 files changed

+373
-343
lines changed

include/umf/memory_pool_ops.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ typedef struct umf_memory_pool_ops_t {
126126
///
127127
umf_result_t (*get_last_allocation_error)(void *pool);
128128

129+
///
130+
/// Following functions, with ext prefix, are optional and memory pool implementation
131+
/// can keep them NULL.
132+
///
133+
129134
///
130135
/// @brief Control operation for the memory pool.
131136
/// The function is used to perform various control operations
@@ -135,16 +140,17 @@ typedef struct umf_memory_pool_ops_t {
135140
/// @param operationType type of the operation to be performed.
136141
/// @param name name associated with the operation.
137142
/// @param arg argument for the operation.
138-
/// @param size size of the argument [optional - check path requirements]
143+
/// @param size size of the argument [optional - check name requirements]
139144
/// @param queryType type of the query to be performed.
140145
///
141146
/// @return umf_result_t result of the control operation.
142147
///
143-
umf_result_t (*ctl)(void *pool, int operationType, const char *name,
144-
void *arg, size_t size, umf_ctl_query_type_t queryType);
148+
umf_result_t (*ext_ctl)(void *hPool, int operationType, const char *name,
149+
void *arg, size_t size,
150+
umf_ctl_query_type_t queryType);
145151

146152
///
147-
/// @brief Retrieves the name of the memory pool [optional]
153+
/// @brief Retrieves the name of the memory pool
148154
/// @param pool valid pointer to the memory pool or NULL value
149155
/// \details
150156
/// * Implementations *must* return a literal null-terminated string.
@@ -153,7 +159,7 @@ typedef struct umf_memory_pool_ops_t {
153159
/// otherwise the pool's name is returned.
154160
/// @return A constant character string representing the pool's name.
155161
///
156-
const char *(*get_name)(void *pool);
162+
const char *(*ext_get_name)(void *pool);
157163
} umf_memory_pool_ops_t;
158164

159165
#ifdef __cplusplus

include/umf/memory_provider_ops.h

Lines changed: 127 additions & 126 deletions
Large diffs are not rendered by default.

src/memory_pool.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ static int CTL_SUBTREE_HANDLER(by_handle_pool)(void *ctx,
4343
umf_ctl_query_type_t queryType) {
4444
(void)indexes, (void)source;
4545
umf_memory_pool_handle_t hPool = (umf_memory_pool_handle_t)ctx;
46-
hPool->ops.ctl(hPool->pool_priv, /*unused*/ 0, extra_name, arg, size,
47-
queryType);
46+
47+
hPool->ops.ext_ctl(hPool->pool_priv, /*unused*/ 0, extra_name, arg, size,
48+
queryType);
4849
return 0;
4950
}
5051

@@ -89,6 +90,7 @@ static int CTL_SUBTREE_HANDLER(default)(void *ctx,
8990
}
9091

9192
utils_mutex_unlock(&ctl_mtx);
93+
9294
return 0;
9395
}
9496

@@ -118,18 +120,19 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
118120
}
119121

120122
umf_result_t ret = UMF_RESULT_SUCCESS;
121-
umf_memory_pool_handle_t pool =
122-
umf_ba_global_alloc(sizeof(umf_memory_pool_t));
123-
if (!pool) {
124-
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
125-
}
126123

127124
if (ops->version != UMF_POOL_OPS_VERSION_CURRENT) {
128125
LOG_WARN("Memory Pool ops version \"%d\" is different than the current "
129126
"version \"%d\"",
130127
ops->version, UMF_POOL_OPS_VERSION_CURRENT);
131128
}
132129

130+
umf_memory_pool_handle_t pool =
131+
umf_ba_global_alloc(sizeof(umf_memory_pool_t));
132+
if (!pool) {
133+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
134+
}
135+
133136
if (!(flags & UMF_POOL_CREATE_FLAG_DISABLE_TRACKING)) {
134137
// Wrap provider with memory tracking provider.
135138
ret = umfTrackingMemoryProviderCreate(provider, pool, &pool->provider);
@@ -146,8 +149,8 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
146149
pool->ops = *ops;
147150
pool->tag = NULL;
148151

149-
if (NULL == pool->ops.ctl) {
150-
pool->ops.ctl = umfDefaultCtlPoolHandle;
152+
if (NULL == pool->ops.ext_ctl) {
153+
pool->ops.ext_ctl = umfDefaultCtlPoolHandle;
151154
}
152155

153156
if (NULL == utils_mutex_init(&pool->lock)) {
@@ -164,10 +167,10 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
164167
// Set default property "name" to pool if exists
165168
for (int i = 0; i < UMF_DEFAULT_SIZE; i++) {
166169
if (CTL_DEFAULT_ENTRIES[i][0] != '\0' &&
167-
strstr(CTL_DEFAULT_ENTRIES[i], ops->get_name(NULL))) {
168-
ops->ctl(pool->pool_priv, CTL_QUERY_PROGRAMMATIC,
169-
CTL_DEFAULT_ENTRIES[i], CTL_DEFAULT_VALUES[i],
170-
UMF_DEFAULT_LEN, CTL_QUERY_WRITE);
170+
strstr(CTL_DEFAULT_ENTRIES[i], ops->ext_get_name(NULL))) {
171+
ops->ext_ctl(pool->pool_priv, CTL_QUERY_PROGRAMMATIC,
172+
CTL_DEFAULT_ENTRIES[i], CTL_DEFAULT_VALUES[i],
173+
UMF_DEFAULT_LEN, CTL_QUERY_WRITE);
171174
}
172175
}
173176

@@ -246,10 +249,10 @@ umf_result_t umfPoolGetMemoryProvider(umf_memory_pool_handle_t hPool,
246249

247250
const char *umfPoolGetName(umf_memory_pool_handle_t pool) {
248251
UMF_CHECK((pool != NULL), NULL);
249-
if (pool->ops.get_name == NULL) {
252+
if (pool->ops.ext_get_name == NULL) {
250253
return NULL;
251254
}
252-
return pool->ops.get_name(pool->pool_priv);
255+
return pool->ops.ext_get_name(pool->pool_priv);
253256
}
254257

255258
umf_result_t umfPoolCreate(const umf_memory_pool_ops_t *ops,

src/memory_provider.c

Lines changed: 65 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-
size, queryType);
30+
hProvider->ops.ext_ctl(hProvider->provider_priv, /*unused*/ 0, extra_name,
31+
arg, size, queryType);
3232
return 0;
3333
}
3434

@@ -122,67 +122,78 @@ static umf_result_t umfDefaultCtlHandle(void *provider, int operationType,
122122
}
123123

124124
void assignOpsExtDefaults(umf_memory_provider_ops_t *ops) {
125-
if (!ops->ext.purge_lazy) {
126-
ops->ext.purge_lazy = umfDefaultPurgeLazy;
125+
if (!ops->ext_purge_lazy) {
126+
ops->ext_purge_lazy = umfDefaultPurgeLazy;
127127
}
128-
if (!ops->ext.purge_force) {
129-
ops->ext.purge_force = umfDefaultPurgeForce;
128+
if (!ops->ext_purge_force) {
129+
ops->ext_purge_force = umfDefaultPurgeForce;
130130
}
131-
if (!ops->ext.allocation_split) {
132-
ops->ext.allocation_split = umfDefaultAllocationSplit;
131+
if (!ops->ext_allocation_split) {
132+
ops->ext_allocation_split = umfDefaultAllocationSplit;
133133
}
134-
if (!ops->ext.allocation_merge) {
135-
ops->ext.allocation_merge = umfDefaultAllocationMerge;
134+
if (!ops->ext_allocation_merge) {
135+
ops->ext_allocation_merge = umfDefaultAllocationMerge;
136136
}
137137
}
138138

139139
void assignOpsIpcDefaults(umf_memory_provider_ops_t *ops) {
140-
if (!ops->ipc.get_ipc_handle_size) {
141-
ops->ipc.get_ipc_handle_size = umfDefaultGetIPCHandleSize;
140+
if (!ops->ext_get_ipc_handle_size) {
141+
ops->ext_get_ipc_handle_size = umfDefaultGetIPCHandleSize;
142142
}
143-
if (!ops->ipc.get_ipc_handle) {
144-
ops->ipc.get_ipc_handle = umfDefaultGetIPCHandle;
143+
if (!ops->ext_get_ipc_handle) {
144+
ops->ext_get_ipc_handle = umfDefaultGetIPCHandle;
145145
}
146-
if (!ops->ipc.put_ipc_handle) {
147-
ops->ipc.put_ipc_handle = umfDefaultPutIPCHandle;
146+
if (!ops->ext_put_ipc_handle) {
147+
ops->ext_put_ipc_handle = umfDefaultPutIPCHandle;
148148
}
149-
if (!ops->ipc.open_ipc_handle) {
150-
ops->ipc.open_ipc_handle = umfDefaultOpenIPCHandle;
149+
if (!ops->ext_open_ipc_handle) {
150+
ops->ext_open_ipc_handle = umfDefaultOpenIPCHandle;
151151
}
152-
if (!ops->ipc.close_ipc_handle) {
153-
ops->ipc.close_ipc_handle = umfDefaultCloseIPCHandle;
152+
if (!ops->ext_close_ipc_handle) {
153+
ops->ext_close_ipc_handle = umfDefaultCloseIPCHandle;
154154
}
155-
if (!ops->ctl) {
156-
ops->ctl = umfDefaultCtlHandle;
155+
if (!ops->ext_ctl) {
156+
ops->ext_ctl = umfDefaultCtlHandle;
157157
}
158158
}
159159

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

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

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

183-
static bool validateOps(const umf_memory_provider_ops_t *ops) {
184-
return validateOpsMandatory(ops) && validateOpsExt(&(ops->ext)) &&
185-
validateOpsIpc(&(ops->ipc));
196+
return true;
186197
}
187198

188199
umf_result_t umfMemoryProviderCreate(const umf_memory_provider_ops_t *ops,
@@ -302,7 +313,7 @@ umf_result_t umfMemoryProviderPurgeLazy(umf_memory_provider_handle_t hProvider,
302313
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
303314
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
304315
umf_result_t res =
305-
hProvider->ops.ext.purge_lazy(hProvider->provider_priv, ptr, size);
316+
hProvider->ops.ext_purge_lazy(hProvider->provider_priv, ptr, size);
306317
checkErrorAndSetLastProvider(res, hProvider);
307318
return res;
308319
}
@@ -312,7 +323,7 @@ umf_result_t umfMemoryProviderPurgeForce(umf_memory_provider_handle_t hProvider,
312323
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
313324
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
314325
umf_result_t res =
315-
hProvider->ops.ext.purge_force(hProvider->provider_priv, ptr, size);
326+
hProvider->ops.ext_purge_force(hProvider->provider_priv, ptr, size);
316327
checkErrorAndSetLastProvider(res, hProvider);
317328
return res;
318329
}
@@ -331,7 +342,7 @@ umfMemoryProviderAllocationSplit(umf_memory_provider_handle_t hProvider,
331342
UMF_RESULT_ERROR_INVALID_ARGUMENT);
332343
UMF_CHECK((firstSize < totalSize), UMF_RESULT_ERROR_INVALID_ARGUMENT);
333344

334-
umf_result_t res = hProvider->ops.ext.allocation_split(
345+
umf_result_t res = hProvider->ops.ext_allocation_split(
335346
hProvider->provider_priv, ptr, totalSize, firstSize);
336347
checkErrorAndSetLastProvider(res, hProvider);
337348
return res;
@@ -349,7 +360,7 @@ umfMemoryProviderAllocationMerge(umf_memory_provider_handle_t hProvider,
349360
UMF_CHECK(((uintptr_t)highPtr - (uintptr_t)lowPtr < totalSize),
350361
UMF_RESULT_ERROR_INVALID_ARGUMENT);
351362

352-
umf_result_t res = hProvider->ops.ext.allocation_merge(
363+
umf_result_t res = hProvider->ops.ext_allocation_merge(
353364
hProvider->provider_priv, lowPtr, highPtr, totalSize);
354365
checkErrorAndSetLastProvider(res, hProvider);
355366
return res;
@@ -360,7 +371,7 @@ umfMemoryProviderGetIPCHandleSize(umf_memory_provider_handle_t hProvider,
360371
size_t *size) {
361372
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
362373
UMF_CHECK((size != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
363-
return hProvider->ops.ipc.get_ipc_handle_size(hProvider->provider_priv,
374+
return hProvider->ops.ext_get_ipc_handle_size(hProvider->provider_priv,
364375
size);
365376
}
366377

@@ -371,7 +382,7 @@ umfMemoryProviderGetIPCHandle(umf_memory_provider_handle_t hProvider,
371382
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
372383
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
373384
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
374-
return hProvider->ops.ipc.get_ipc_handle(hProvider->provider_priv, ptr,
385+
return hProvider->ops.ext_get_ipc_handle(hProvider->provider_priv, ptr,
375386
size, providerIpcData);
376387
}
377388

@@ -380,7 +391,7 @@ umfMemoryProviderPutIPCHandle(umf_memory_provider_handle_t hProvider,
380391
void *providerIpcData) {
381392
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
382393
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
383-
return hProvider->ops.ipc.put_ipc_handle(hProvider->provider_priv,
394+
return hProvider->ops.ext_put_ipc_handle(hProvider->provider_priv,
384395
providerIpcData);
385396
}
386397

@@ -390,7 +401,7 @@ umfMemoryProviderOpenIPCHandle(umf_memory_provider_handle_t hProvider,
390401
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
391402
UMF_CHECK((providerIpcData != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
392403
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
393-
return hProvider->ops.ipc.open_ipc_handle(hProvider->provider_priv,
404+
return hProvider->ops.ext_open_ipc_handle(hProvider->provider_priv,
394405
providerIpcData, ptr);
395406
}
396407

@@ -399,6 +410,6 @@ umfMemoryProviderCloseIPCHandle(umf_memory_provider_handle_t hProvider,
399410
void *ptr, size_t size) {
400411
UMF_CHECK((hProvider != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
401412
UMF_CHECK((ptr != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
402-
return hProvider->ops.ipc.close_ipc_handle(hProvider->provider_priv, ptr,
413+
return hProvider->ops.ext_close_ipc_handle(hProvider->provider_priv, ptr,
403414
size);
404415
}

src/pool/pool_disjoint.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,8 @@ static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS = {
10331033
.malloc_usable_size = disjoint_pool_malloc_usable_size,
10341034
.free = disjoint_pool_free,
10351035
.get_last_allocation_error = disjoint_pool_get_last_allocation_error,
1036-
.get_name = disjoint_pool_get_name,
1037-
.ctl = disjoint_pool_ctl,
1036+
.ext_get_name = disjoint_pool_get_name,
1037+
.ext_ctl = disjoint_pool_ctl,
10381038
};
10391039

10401040
const umf_memory_pool_ops_t *umfDisjointPoolOps(void) {

src/pool/pool_scalable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ static umf_memory_pool_ops_t UMF_SCALABLE_POOL_OPS = {
437437
.malloc_usable_size = tbb_malloc_usable_size,
438438
.free = tbb_free,
439439
.get_last_allocation_error = tbb_get_last_allocation_error,
440-
.ctl = pool_ctl,
441-
.get_name = scalable_get_name,
440+
.ext_ctl = pool_ctl,
441+
.ext_get_name = scalable_get_name,
442442
};
443443

444444
const umf_memory_pool_ops_t *umfScalablePoolOps(void) {

src/provider/provider_cuda.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -688,16 +688,16 @@ static umf_memory_provider_ops_t UMF_CUDA_MEMORY_PROVIDER_OPS = {
688688
.get_name = cu_memory_provider_get_name,
689689
// TODO
690690
/*
691-
.ext.purge_lazy = cu_memory_provider_purge_lazy,
692-
.ext.purge_force = cu_memory_provider_purge_force,
693-
.ext.allocation_merge = cu_memory_provider_allocation_merge,
694-
.ext.allocation_split = cu_memory_provider_allocation_split,
691+
.ext_purge_lazy = cu_memory_provider_purge_lazy,
692+
.ext_purge_force = cu_memory_provider_purge_force,
693+
.ext_allocation_merge = cu_memory_provider_allocation_merge,
694+
.ext_allocation_split = cu_memory_provider_allocation_split,
695695
*/
696-
.ipc.get_ipc_handle_size = cu_memory_provider_get_ipc_handle_size,
697-
.ipc.get_ipc_handle = cu_memory_provider_get_ipc_handle,
698-
.ipc.put_ipc_handle = cu_memory_provider_put_ipc_handle,
699-
.ipc.open_ipc_handle = cu_memory_provider_open_ipc_handle,
700-
.ipc.close_ipc_handle = cu_memory_provider_close_ipc_handle,
696+
.ext_get_ipc_handle_size = cu_memory_provider_get_ipc_handle_size,
697+
.ext_get_ipc_handle = cu_memory_provider_get_ipc_handle,
698+
.ext_put_ipc_handle = cu_memory_provider_put_ipc_handle,
699+
.ext_open_ipc_handle = cu_memory_provider_open_ipc_handle,
700+
.ext_close_ipc_handle = cu_memory_provider_close_ipc_handle,
701701
};
702702

703703
const umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void) {

0 commit comments

Comments
 (0)