From c788f6d202f63bbead62606f75104aabac6643ac Mon Sep 17 00:00:00 2001 From: Rafal Rudnicki Date: Tue, 8 Jul 2025 16:44:25 +0000 Subject: [PATCH] improve pool ops tests --- test/common/pool.hpp | 18 ++++++++++++++++++ test/pools/pool_base_alloc.cpp | 6 ++++++ test/utils/cpp_helpers.hpp | 17 +++++++++-------- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/test/common/pool.hpp b/test/common/pool.hpp index 6f1077a15..5cae85411 100644 --- a/test/common/pool.hpp +++ b/test/common/pool.hpp @@ -122,16 +122,25 @@ typedef struct pool_base_t { umf_result_t get_last_allocation_error() noexcept { return UMF_RESULT_SUCCESS; } + umf_result_t get_name(const char **name) noexcept { + if (name) { + *name = "pool_base"; + } + return UMF_RESULT_SUCCESS; + } } pool_base_t; struct malloc_pool : public pool_base_t { void *malloc(size_t size) noexcept { return ::malloc(size); } + void *calloc(size_t num, size_t size) noexcept { return ::calloc(num, size); } + void *realloc(void *ptr, size_t size) noexcept { return ::realloc(ptr, size); } + void *aligned_malloc(size_t size, size_t alignment) noexcept { #ifdef _WIN32 (void)size; // unused @@ -143,6 +152,7 @@ struct malloc_pool : public pool_base_t { return ::aligned_alloc(alignment, size); #endif } + umf_result_t malloc_usable_size(const void *ptr, size_t *size) noexcept { if (size) { #ifdef _WIN32 @@ -155,10 +165,18 @@ struct malloc_pool : public pool_base_t { } return UMF_RESULT_SUCCESS; } + umf_result_t free(void *ptr) noexcept { ::free(ptr); return UMF_RESULT_SUCCESS; } + + umf_result_t get_name(const char **name) noexcept { + if (name) { + *name = "malloc_pool"; + } + return UMF_RESULT_SUCCESS; + } }; umf_memory_pool_ops_t MALLOC_POOL_OPS = diff --git a/test/pools/pool_base_alloc.cpp b/test/pools/pool_base_alloc.cpp index 49fb7bc28..4be438936 100644 --- a/test/pools/pool_base_alloc.cpp +++ b/test/pools/pool_base_alloc.cpp @@ -44,6 +44,12 @@ struct base_alloc_pool : public umf_test::pool_base_t { umf_result_t get_last_allocation_error() { return umf_test::getPoolLastStatusRef(); } + umf_result_t get_name(const char **name) noexcept { + if (name) { + *name = "base_alloc_pool"; + } + return UMF_RESULT_SUCCESS; + } }; umf_memory_pool_ops_t BA_POOL_OPS = diff --git a/test/utils/cpp_helpers.hpp b/test/utils/cpp_helpers.hpp index 0baf2ba87..ca1940e16 100644 --- a/test/utils/cpp_helpers.hpp +++ b/test/utils/cpp_helpers.hpp @@ -34,6 +34,9 @@ using provider_unique_handle_t = #define UMF_ASSIGN_OP(ops, type, func, default_return) \ ops.func = [](void *obj, auto... args) { \ try { \ + if (!obj) { \ + return type().func(args...); \ + } \ return reinterpret_cast(obj)->func(args...); \ } catch (...) { \ return default_return; \ @@ -43,6 +46,9 @@ using provider_unique_handle_t = #define UMF_ASSIGN_OP_NORETURN(ops, type, func) \ ops.func = [](void *obj, auto... args) { \ try { \ + if (!obj) { \ + return type().func(args...); \ + } \ return reinterpret_cast(obj)->func(args...); \ } catch (...) { \ } \ @@ -76,15 +82,10 @@ template umf_memory_pool_ops_t poolOpsBase() { UMF_ASSIGN_OP(ops, T, calloc, ((void *)nullptr)); UMF_ASSIGN_OP(ops, T, aligned_malloc, ((void *)nullptr)); UMF_ASSIGN_OP(ops, T, realloc, ((void *)nullptr)); - UMF_ASSIGN_OP(ops, T, malloc_usable_size, UMF_RESULT_SUCCESS); - UMF_ASSIGN_OP(ops, T, free, UMF_RESULT_SUCCESS); + UMF_ASSIGN_OP(ops, T, get_name, UMF_RESULT_ERROR_UNKNOWN); + UMF_ASSIGN_OP(ops, T, malloc_usable_size, UMF_RESULT_ERROR_UNKNOWN); + UMF_ASSIGN_OP(ops, T, free, UMF_RESULT_ERROR_UNKNOWN); UMF_ASSIGN_OP(ops, T, get_last_allocation_error, UMF_RESULT_ERROR_UNKNOWN); - ops.get_name = [](void *, const char **name) { - if (name) { - *name = "test_pool"; - } - return UMF_RESULT_SUCCESS; - }; return ops; }