Skip to content

Commit 2dc6630

Browse files
committed
return umf_result in (almost) all umf functions
1 parent b894c77 commit 2dc6630

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+504
-242
lines changed

examples/basic/basic.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ int main(void) {
9696
printf("%s %p\n", ptr, (void *)ptr);
9797

9898
// Retrieve a memory pool from a pointer, available with memory tracking
99-
umf_memory_pool_handle_t check_pool = umfPoolByPtr(ptr);
99+
umf_memory_pool_handle_t check_pool;
100+
res = umfPoolByPtr(ptr, &check_pool);
101+
if (res != UMF_RESULT_SUCCESS) {
102+
printf("Failed to retrieve a memory pool for the pointer!\n");
103+
goto memory_pool_destroy;
104+
}
100105
printf("Memory at %p has been allocated from the pool at %p\n", (void *)ptr,
101106
(void *)check_pool);
102107

examples/custom_file_provider/custom_file_provider.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,21 @@ static umf_result_t file_free(void *provider, void *ptr, size_t size) {
201201
}
202202

203203
// Function to get the name of the file provider
204-
static const char *file_get_name(void *provider) {
204+
static umf_result_t file_get_name(void *provider, const char **name) {
205205
(void)provider; // Unused parameter
206-
return "File Provider";
206+
*name = "File Provider";
207+
return UMF_RESULT_SUCCESS;
207208
}
208209

209210
// Function to get the last native error of the file provider
210211
// This function is needed only if the provider returns UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC
211-
static void file_get_last_native_error(void *provider, const char **ppMessage,
212-
int32_t *pError) {
212+
static umf_result_t file_get_last_native_error(void *provider,
213+
const char **ppMessage,
214+
int32_t *pError) {
213215
(void)provider; // Unused parameter
214216
*ppMessage = "";
215217
*pError = 0;
218+
return UMF_RESULT_SUCCESS;
216219
}
217220

218221
// Function to get the recommended page size of the file provider
@@ -319,7 +322,12 @@ int main(void) {
319322
printf("%s %p\n", ptr, (void *)ptr);
320323

321324
// Retrieve a memory pool from a pointer, available with memory tracking
322-
umf_memory_pool_handle_t check_pool = umfPoolByPtr(ptr);
325+
umf_memory_pool_handle_t check_pool;
326+
res = umfPoolByPtr(ptr, &check_pool);
327+
if (res != UMF_RESULT_SUCCESS) {
328+
fprintf(stderr, "Failed to retrieve a memory pool for the pointer!\n");
329+
goto memory_pool_destroy;
330+
}
323331
printf("Memory at %p has been allocated from the pool at %p\n", (void *)ptr,
324332
(void *)check_pool);
325333

include/umf.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
*/
99

10+
#include "umf/base.h"
1011
#ifndef UMF_UNIFIED_MEMORY_FRAMEWORK_H
1112
#define UMF_UNIFIED_MEMORY_FRAMEWORK_H 1
1213

@@ -24,17 +25,17 @@ extern "C" {
2425
/// if the usage reference counter was equal to 0.
2526
/// It must be called just after dlopen() and it is not required in other scenarios.
2627
/// @return 0 on success or -1 on failure.
27-
int umfInit(void);
28+
umf_result_t umfInit(void);
2829

2930
///
3031
/// @brief Decrement the usage reference counter and destroy the global state of libumf
3132
/// if the usage reference counter is equal to 0.
3233
/// It must be called just before dlclose() and it is not required in other scenarios.
33-
void umfTearDown(void);
34+
umf_result_t umfTearDown(void);
3435

3536
///
3637
/// @brief Get the current version of the UMF headers defined by UMF_VERSION_CURRENT.
37-
int umfGetCurrentVersion(void);
38+
umf_result_t umfGetCurrentVersion(void);
3839

3940
#ifdef __cplusplus
4041
}

include/umf/ipc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2023-2024 Intel Corporation
3+
* Copyright (C) 2023-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -66,6 +66,7 @@ umf_result_t umfCloseIPCHandle(void *ptr);
6666
umf_result_t umfPoolGetIPCHandler(umf_memory_pool_handle_t hPool,
6767
umf_ipc_handler_handle_t *hIPCHandler);
6868

69+
/// todo: IPC handler creation and destruction (IPC provider??)
6970
#ifdef __cplusplus
7071
}
7172
#endif

include/umf/memory_pool.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
*/
99

10+
#include <stddef.h>
1011
#ifndef UMF_MEMORY_POOL_H
1112
#define UMF_MEMORY_POOL_H 1
1213

@@ -105,9 +106,11 @@ void *umfPoolRealloc(umf_memory_pool_handle_t hPool, void *ptr, size_t size);
105106
/// @brief Obtains size of block of memory allocated from the \p hPool for a given \p ptr
106107
/// @param hPool specified memory hPool
107108
/// @param ptr pointer to the allocated memory
108-
/// @return size of the memory block allocated from the \p hPool
109+
/// @param size [out] pointer to a variable that will receive the size of the memory block
110+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
109111
///
110-
size_t umfPoolMallocUsableSize(umf_memory_pool_handle_t hPool, const void *ptr);
112+
umf_result_t umfPoolMallocUsableSize(umf_memory_pool_handle_t hPool,
113+
const void *ptr, size_t *size);
111114

112115
///
113116
/// @brief Frees the memory space of the specified \p hPool pointed by \p ptr
@@ -152,9 +155,11 @@ umf_result_t umfPoolGetLastAllocationError(umf_memory_pool_handle_t hPool);
152155
/// @brief Retrieve memory pool associated with a given ptr. Only memory allocated
153156
/// with the usage of a memory provider is being tracked.
154157
/// @param ptr pointer to memory belonging to a memory pool
155-
/// @return Handle to a memory pool that contains ptr or NULL if pointer does not belong to any UMF pool.
158+
/// @param pool [out] handle to the memory pool that contains ptr.
159+
/// @return UMF_RESULT_SUCCESS on success
160+
/// UMF_RESULT_ERROR_INVALID_ARGUMENT if pool is NULL, or ptr do not belongs to any pool.
156161
///
157-
umf_memory_pool_handle_t umfPoolByPtr(const void *ptr);
162+
umf_result_t umfPoolByPtr(const void *ptr, umf_memory_pool_handle_t *pool);
158163

159164
///
160165
/// @brief Retrieve memory provider associated with a given pool.
@@ -169,10 +174,10 @@ umf_result_t umfPoolGetMemoryProvider(umf_memory_pool_handle_t hPool,
169174
///
170175
/// @brief Retrieve name of a given memory \p pool.
171176
/// @param pool handle to the memory pool
172-
/// @return pointer to a string containing the name of the \p pool
173-
/// or NULL if the pool doesn't support retrieving its name.
177+
/// @param name [out] pointer to a string containing the name of the \p pool.
178+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
174179
///
175-
const char *umfPoolGetName(umf_memory_pool_handle_t pool);
180+
umf_result_t umfPoolGetName(umf_memory_pool_handle_t pool, const char **name);
176181

177182
///
178183
/// @brief Set a custom tag on the memory pool that can be later retrieved using umfPoolGetTag.

include/umf/memory_pool_ops.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ typedef struct umf_memory_pool_ops_t {
9595
/// @param ptr pointer to the allocated memory
9696
/// @return size of the memory block allocated from the \p pool
9797
///
98-
size_t (*malloc_usable_size)(void *pool, const void *ptr);
98+
umf_result_t (*malloc_usable_size)(void *pool, const void *ptr,
99+
size_t *size);
99100

100101
///
101102
/// @brief Frees the memory space of the specified \p pool pointed by \p ptr
@@ -105,7 +106,7 @@ typedef struct umf_memory_pool_ops_t {
105106
/// Whether any status other than UMF_RESULT_SUCCESS can be returned
106107
/// depends on the memory provider used by the \p pool.
107108
///
108-
umf_result_t (*free)(void *pool, void *);
109+
umf_result_t (*free)(void *pool, void *ptr);
109110

110111
///
111112
/// @brief Retrieve \p umf_result_t representing the error of the last failed allocation
@@ -160,7 +161,7 @@ typedef struct umf_memory_pool_ops_t {
160161
/// otherwise the pool's name is returned.
161162
/// @return A constant character string representing the pool's name.
162163
///
163-
const char *(*ext_get_name)(void *pool);
164+
umf_result_t (*ext_get_name)(void *pool, const char **name);
164165
} umf_memory_pool_ops_t;
165166

166167
#ifdef __cplusplus

include/umf/memory_provider.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ typedef enum umf_memory_visibility_t {
2424
} umf_memory_visibility_t;
2525

2626
/// @brief Protection of the memory allocations
27-
typedef enum umf_mem_protection_flags_t {
27+
typedef enum umf_mem_protection_flag_t {
2828
UMF_PROTECTION_NONE = (1 << 0), ///< Memory allocations can not be accessed
2929
UMF_PROTECTION_READ = (1 << 1), ///< Memory allocations can be read.
3030
UMF_PROTECTION_WRITE = (1 << 2), ///< Memory allocations can be written.
3131
UMF_PROTECTION_EXEC = (1 << 3), ///< Memory allocations can be executed.
3232
/// @cond
3333
UMF_PROTECTION_MAX // must be the last one
3434
/// @endcond
35-
} umf_mem_protection_flags_t;
35+
} umf_mem_protection_flag_t;
3636

3737
/// @brief A struct containing memory provider specific set of functions
3838
typedef struct umf_memory_provider_t *umf_memory_provider_handle_t;
@@ -101,9 +101,9 @@ umf_result_t umfMemoryProviderFree(umf_memory_provider_handle_t hProvider,
101101
/// result in string representation
102102
/// @param pError [out] pointer to an integer where the adapter specific error code will be stored
103103
///
104-
void umfMemoryProviderGetLastNativeError(umf_memory_provider_handle_t hProvider,
105-
const char **ppMessage,
106-
int32_t *pError);
104+
umf_result_t
105+
umfMemoryProviderGetLastNativeError(umf_memory_provider_handle_t hProvider,
106+
const char **ppMessage, int32_t *pError);
107107

108108
///
109109
/// @brief Retrieve recommended page size for a given allocation size.
@@ -217,9 +217,10 @@ umfMemoryProviderCloseIPCHandle(umf_memory_provider_handle_t hProvider,
217217
///
218218
/// @brief Retrieve name of a given memory \p hProvider.
219219
/// @param hProvider handle to the memory provider
220-
/// @return pointer to a string containing the name of the \p hProvider
221-
///
222-
const char *umfMemoryProviderGetName(umf_memory_provider_handle_t hProvider);
220+
/// @param name [out] pointer to the provider name string
221+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
222+
umf_result_t umfMemoryProviderGetName(umf_memory_provider_handle_t hProvider,
223+
const char **name);
223224

224225
///
225226
/// @brief Retrieve handle to the last memory provider that returned status other
@@ -231,7 +232,8 @@ const char *umfMemoryProviderGetName(umf_memory_provider_handle_t hProvider);
231232
///
232233
/// @return Handle to the memory provider
233234
///
234-
umf_memory_provider_handle_t umfGetLastFailedMemoryProvider(void);
235+
umf_result_t
236+
umfGetLastFailedMemoryProvider(umf_memory_provider_handle_t *provider);
235237

236238
///
237239
/// @brief Splits a coarse grain allocation into 2 adjacent allocations that

include/umf/memory_provider_ops.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ typedef struct umf_memory_provider_ops_t {
9191
/// result in string representation
9292
/// @param pError [out] pointer to an integer where the adapter specific error code will be stored
9393
///
94-
void (*get_last_native_error)(void *provider, const char **ppMessage,
95-
int32_t *pError);
94+
umf_result_t (*get_last_native_error)(void *provider,
95+
const char **ppMessage,
96+
int32_t *pError);
9697

9798
///
9899
/// @brief Retrieve recommended page size for a given allocation size.
@@ -120,7 +121,7 @@ typedef struct umf_memory_provider_ops_t {
120121
/// @param provider pointer to the memory provider
121122
/// @return pointer to a string containing the name of the \p provider
122123
///
123-
const char *(*get_name)(void *provider);
124+
umf_result_t (*get_name)(void *provider, const char **name);
124125

125126
///
126127
/// Following functions, with ext prefix, are optional and memory provider implementation

include/umf/pools/pool_disjoint.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ typedef struct umf_disjoint_pool_params_t *umf_disjoint_pool_params_handle_t;
3030
/// @brief Create a pool limits struct.
3131
/// @param MaxSize specifies hard limit for memory allocated from a provider.
3232
/// @return handle to the created shared limits struct.
33-
umf_disjoint_pool_shared_limits_handle_t
34-
umfDisjointPoolSharedLimitsCreate(size_t MaxSize);
33+
umf_result_t umfDisjointPoolSharedLimitsCreate(
34+
size_t MaxSize, umf_disjoint_pool_shared_limits_handle_t *hSharedLimits);
3535

3636
/// @brief Destroy previously created pool limits struct.
3737
/// @param hSharedLimits handle to the shared limits struct.

src/libumf.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ static umf_ctl_node_t CTL_NODE(umf)[] = {CTL_CHILD(provider), CTL_CHILD(pool),
3333

3434
void initialize_global_ctl(void) { CTL_REGISTER_MODULE(NULL, umf); }
3535

36-
int umfInit(void) {
36+
umf_result_t umfInit(void) {
3737
if (utils_fetch_and_add_u64(&umfRefCount, 1) == 0) {
3838
utils_log_init();
39-
TRACKER = umfMemoryTrackerCreate();
40-
if (!TRACKER) {
39+
umf_result_t umf_result = umfMemoryTrackerCreate(&TRACKER);
40+
if (umf_result != UMF_RESULT_SUCCESS) {
4141
LOG_ERR("Failed to create memory tracker");
42-
return -1;
42+
return umf_result;
4343
}
4444

4545
LOG_DEBUG("UMF tracker created");
4646

47-
umf_result_t umf_result = umfIpcCacheGlobalInit();
47+
umf_result = umfIpcCacheGlobalInit();
4848
if (umf_result != UMF_RESULT_SUCCESS) {
4949
LOG_ERR("Failed to initialize IPC cache");
50-
return -1;
50+
return umf_result;
5151
}
5252

5353
LOG_DEBUG("UMF IPC cache initialized");
@@ -58,10 +58,10 @@ int umfInit(void) {
5858
LOG_DEBUG("UMF library initialized");
5959
}
6060

61-
return 0;
61+
return UMF_RESULT_SUCCESS;
6262
}
6363

64-
void umfTearDown(void) {
64+
umf_result_t umfTearDown(void) {
6565
if (utils_fetch_and_sub_u64(&umfRefCount, 1) == 1) {
6666
#if !defined(_WIN32) && !defined(UMF_NO_HWLOC)
6767
umfMemspaceHostAllDestroy();
@@ -95,9 +95,10 @@ void umfTearDown(void) {
9595
fini_tbb_global_state();
9696
LOG_DEBUG("UMF library finalized");
9797
}
98+
return UMF_RESULT_SUCCESS;
9899
}
99100

100-
int umfGetCurrentVersion(void) { return UMF_VERSION_CURRENT; }
101+
umf_result_t umfGetCurrentVersion(void) { return UMF_VERSION_CURRENT; }
101102

102103
umf_result_t umfCtlGet(const char *name, void *ctx, void *arg, size_t size) {
103104
// ctx can be NULL when getting defaults

0 commit comments

Comments
 (0)