Skip to content

Commit a2a0919

Browse files
authored
Merge pull request #1387 from lplewa/umf_result
return umf_result in (almost) all umf functions
2 parents 7fddd2c + d80aa22 commit a2a0919

Some content is hidden

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

53 files changed

+525
-263
lines changed

.github/workflows/pr_push.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ jobs:
9494
contents: read
9595
security-events: write
9696
uses: ./.github/workflows/reusable_trivy.yml
97-
Compatibility:
98-
needs: [Build]
99-
uses: ./.github/workflows/reusable_compatibility.yml
100-
strategy:
101-
matrix:
102-
tag: ["v0.12.0-dev3"]
103-
with:
104-
tag: ${{matrix.tag}}
97+
# Compatibility:
98+
# needs: [Build]
99+
# uses: ./.github/workflows/reusable_compatibility.yml
100+
# strategy:
101+
# matrix:
102+
# tag: ["v0.12.0-dev3"]
103+
# with:
104+
# tag: ${{matrix.tag}}

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: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,25 @@ 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+
if (!name) {
207+
fprintf(stderr, "Name is NULL\n");
208+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
209+
}
210+
*name = "File Provider";
211+
return UMF_RESULT_SUCCESS;
207212
}
208213

209214
// Function to get the last native error of the file provider
210215
// 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) {
216+
static umf_result_t file_get_last_native_error(void *provider,
217+
const char **ppMessage,
218+
int32_t *pError) {
213219
(void)provider; // Unused parameter
214220
*ppMessage = "";
215221
*pError = 0;
222+
return UMF_RESULT_SUCCESS;
216223
}
217224

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

321328
// Retrieve a memory pool from a pointer, available with memory tracking
322-
umf_memory_pool_handle_t check_pool = umfPoolByPtr(ptr);
329+
umf_memory_pool_handle_t check_pool;
330+
res = umfPoolByPtr(ptr, &check_pool);
331+
if (res != UMF_RESULT_SUCCESS) {
332+
fprintf(stderr, "Failed to retrieve a memory pool for the pointer!\n");
333+
goto memory_pool_destroy;
334+
}
323335
printf("Memory at %p has been allocated from the pool at %p\n", (void *)ptr,
324336
(void *)check_pool);
325337

include/umf.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef UMF_UNIFIED_MEMORY_FRAMEWORK_H
1111
#define UMF_UNIFIED_MEMORY_FRAMEWORK_H 1
1212

13+
#include <umf/base.h>
1314
#include <umf/memory_pool.h>
1415
#include <umf/memory_provider.h>
1516
#include <umf/mempolicy.h>
@@ -23,17 +24,19 @@ extern "C" {
2324
/// @brief Increment the usage reference counter and initialize the global state of libumf
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.
26-
/// @return 0 on success or -1 on failure.
27-
int umfInit(void);
27+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
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+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
35+
umf_result_t umfTearDown(void);
3436

3537
///
3638
/// @brief Get the current version of the UMF headers defined by UMF_VERSION_CURRENT.
39+
/// @return The current version of the UMF headers.
3740
int umfGetCurrentVersion(void);
3841

3942
#ifdef __cplusplus

include/umf/ipc.h

Lines changed: 1 addition & 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

include/umf/memory_pool.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndef UMF_MEMORY_POOL_H
1111
#define UMF_MEMORY_POOL_H 1
1212

13+
#include <stddef.h>
14+
1315
#include <umf/base.h>
1416
#include <umf/memory_pool_ops.h>
1517
#include <umf/memory_provider.h>
@@ -105,9 +107,11 @@ void *umfPoolRealloc(umf_memory_pool_handle_t hPool, void *ptr, size_t size);
105107
/// @brief Obtains size of block of memory allocated from the \p hPool for a given \p ptr
106108
/// @param hPool specified memory hPool
107109
/// @param ptr pointer to the allocated memory
108-
/// @return size of the memory block allocated from the \p hPool
110+
/// @param size [out] pointer to a variable that will receive the size of the memory block
111+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
109112
///
110-
size_t umfPoolMallocUsableSize(umf_memory_pool_handle_t hPool, const void *ptr);
113+
umf_result_t umfPoolMallocUsableSize(umf_memory_pool_handle_t hPool,
114+
const void *ptr, size_t *size);
111115

112116
///
113117
/// @brief Frees the memory space of the specified \p hPool pointed by \p ptr
@@ -152,14 +156,16 @@ umf_result_t umfPoolGetLastAllocationError(umf_memory_pool_handle_t hPool);
152156
/// @brief Retrieve memory pool associated with a given ptr. Only memory allocated
153157
/// with the usage of a memory provider is being tracked.
154158
/// @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.
159+
/// @param pool [out] handle to the memory pool that contains ptr
160+
/// @return UMF_RESULT_SUCCESS on success
161+
/// UMF_RESULT_ERROR_INVALID_ARGUMENT if pool is NULL, or ptr do not belongs to any pool.
156162
///
157-
umf_memory_pool_handle_t umfPoolByPtr(const void *ptr);
163+
umf_result_t umfPoolByPtr(const void *ptr, umf_memory_pool_handle_t *pool);
158164

159165
///
160166
/// @brief Retrieve memory provider associated with a given pool.
161167
/// @param hPool specified memory pool
162-
/// @param hProvider [out] memory providers handle.
168+
/// @param hProvider [out] memory providers handle
163169
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
164170
/// UMF_RESULT_ERROR_INVALID_ARGUMENT if hProvider is NULL
165171
///
@@ -169,10 +175,10 @@ umf_result_t umfPoolGetMemoryProvider(umf_memory_pool_handle_t hPool,
169175
///
170176
/// @brief Retrieve name of a given memory \p pool.
171177
/// @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.
178+
/// @param name [out] pointer to a string containing the name of the \p pool
179+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
174180
///
175-
const char *umfPoolGetName(umf_memory_pool_handle_t pool);
181+
umf_result_t umfPoolGetName(umf_memory_pool_handle_t pool, const char **name);
176182

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

include/umf/memory_pool_ops.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ typedef struct umf_memory_pool_ops_t {
9393
/// @brief Obtains size of block of memory allocated from the \p pool for a given \p ptr
9494
/// @param pool pointer to the memory pool
9595
/// @param ptr pointer to the allocated memory
96-
/// @return size of the memory block allocated from the \p pool
96+
/// @param size [out] size of the memory block allocated from the \p pool
97+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
9798
///
98-
size_t (*malloc_usable_size)(void *pool, const void *ptr);
99+
umf_result_t (*malloc_usable_size)(void *pool, const void *ptr,
100+
size_t *size);
99101

100102
///
101103
/// @brief Frees the memory space of the specified \p pool pointed by \p ptr
@@ -105,7 +107,7 @@ typedef struct umf_memory_pool_ops_t {
105107
/// Whether any status other than UMF_RESULT_SUCCESS can be returned
106108
/// depends on the memory provider used by the \p pool.
107109
///
108-
umf_result_t (*free)(void *pool, void *);
110+
umf_result_t (*free)(void *pool, void *ptr);
109111

110112
///
111113
/// @brief Retrieve \p umf_result_t representing the error of the last failed allocation
@@ -153,14 +155,15 @@ typedef struct umf_memory_pool_ops_t {
153155
///
154156
/// @brief Retrieves the name of the memory pool
155157
/// @param pool valid pointer to the memory pool or NULL value
158+
/// @param name [out] pointer to a constant character string that will be set to the pool's name
156159
/// \details
157160
/// * Implementations *must* return a literal null-terminated string.
158161
///
159162
/// * Implementations *must* return default pool name when NULL is provided,
160163
/// otherwise the pool's name is returned.
161-
/// @return A constant character string representing the pool's name.
164+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
162165
///
163-
const char *(*ext_get_name)(void *pool);
166+
umf_result_t (*ext_get_name)(void *pool, const char **name);
164167
} umf_memory_pool_ops_t;
165168

166169
#ifdef __cplusplus

include/umf/memory_provider.h

Lines changed: 14 additions & 10 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;
@@ -100,10 +100,11 @@ umf_result_t umfMemoryProviderFree(umf_memory_provider_handle_t hProvider,
100100
/// @param ppMessage [out] pointer to a string containing provider specific
101101
/// result in string representation
102102
/// @param pError [out] pointer to an integer where the adapter specific error code will be stored
103+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
103104
///
104-
void umfMemoryProviderGetLastNativeError(umf_memory_provider_handle_t hProvider,
105-
const char **ppMessage,
106-
int32_t *pError);
105+
umf_result_t
106+
umfMemoryProviderGetLastNativeError(umf_memory_provider_handle_t hProvider,
107+
const char **ppMessage, int32_t *pError);
107108

108109
///
109110
/// @brief Retrieve recommended page size for a given allocation size.
@@ -217,21 +218,24 @@ umfMemoryProviderCloseIPCHandle(umf_memory_provider_handle_t hProvider,
217218
///
218219
/// @brief Retrieve name of a given memory \p hProvider.
219220
/// @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);
221+
/// @param name [out] pointer to the provider name string
222+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure
223+
umf_result_t umfMemoryProviderGetName(umf_memory_provider_handle_t hProvider,
224+
const char **name);
223225

224226
///
225227
/// @brief Retrieve handle to the last memory provider that returned status other
226228
/// than UMF_RESULT_SUCCESS on the calling thread.
229+
/// @param provider [out] pointer to the handle to the last failed memory provider
227230
///
228231
/// \details Handle to the memory provider is stored in the thread local
229232
/// storage. The handle is updated every time a memory provider
230233
/// returns status other than UMF_RESULT_SUCCESS.
231234
///
232-
/// @return Handle to the memory provider
235+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
233236
///
234-
umf_memory_provider_handle_t umfGetLastFailedMemoryProvider(void);
237+
umf_result_t
238+
umfGetLastFailedMemoryProvider(umf_memory_provider_handle_t *provider);
235239

236240
///
237241
/// @brief Splits a coarse grain allocation into 2 adjacent allocations that

include/umf/memory_provider_ops.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ typedef struct umf_memory_provider_ops_t {
9090
/// @param ppMessage [out] pointer to a string containing provider specific
9191
/// result in string representation
9292
/// @param pError [out] pointer to an integer where the adapter specific error code will be stored
93+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
9394
///
94-
void (*get_last_native_error)(void *provider, const char **ppMessage,
95-
int32_t *pError);
95+
umf_result_t (*get_last_native_error)(void *provider,
96+
const char **ppMessage,
97+
int32_t *pError);
9698

9799
///
98100
/// @brief Retrieve recommended page size for a given allocation size.
@@ -118,9 +120,10 @@ typedef struct umf_memory_provider_ops_t {
118120
///
119121
/// @brief Retrieve name of a given memory \p provider.
120122
/// @param provider pointer to the memory provider
121-
/// @return pointer to a string containing the name of the \p provider
123+
/// @param name [out] pointer to a string containing the name of the \p provider
124+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
122125
///
123-
const char *(*get_name)(void *provider);
126+
umf_result_t (*get_name)(void *provider, const char **name);
124127

125128
///
126129
/// Following functions, with ext prefix, are optional and memory provider implementation

include/umf/pools/pool_disjoint.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ typedef struct umf_disjoint_pool_params_t *umf_disjoint_pool_params_handle_t;
2929

3030
/// @brief Create a pool limits struct.
3131
/// @param MaxSize specifies hard limit for memory allocated from a provider.
32-
/// @return handle to the created shared limits struct.
33-
umf_disjoint_pool_shared_limits_handle_t
34-
umfDisjointPoolSharedLimitsCreate(size_t MaxSize);
32+
/// @param hSharedLimits [out] handle to the newly created shared limits struct.
33+
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
34+
umf_result_t umfDisjointPoolSharedLimitsCreate(
35+
size_t MaxSize, umf_disjoint_pool_shared_limits_handle_t *hSharedLimits);
3536

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

0 commit comments

Comments
 (0)