From 6ea5b2f642ceba48e914bf2d151a85331fa982e4 Mon Sep 17 00:00:00 2001 From: Rafal Rudnicki Date: Mon, 9 Dec 2024 09:20:27 +0100 Subject: [PATCH] add free handle POC --- include/umf/proxy_lib_handlers.h | 27 ++++++++++++ src/proxy_lib/proxy_lib.c | 24 ++++++++++- src/proxy_lib/proxy_lib.def | 2 + src/proxy_lib/proxy_lib.h | 22 ---------- src/proxy_lib/proxy_lib.map | 4 +- src/proxy_lib/proxy_lib_linux.c | 5 ++- src/proxy_lib/proxy_lib_windows.c | 5 ++- test/CMakeLists.txt | 9 +++- .../proxy_lib.cpp} | 0 test/proxy_lib/proxy_lib_handlers.cpp | 42 +++++++++++++++++++ .../proxy_lib_size_threshold.cpp} | 0 11 files changed, 111 insertions(+), 29 deletions(-) create mode 100644 include/umf/proxy_lib_handlers.h delete mode 100644 src/proxy_lib/proxy_lib.h rename test/{test_proxy_lib.cpp => proxy_lib/proxy_lib.cpp} (100%) create mode 100644 test/proxy_lib/proxy_lib_handlers.cpp rename test/{test_proxy_lib_size_threshold.cpp => proxy_lib/proxy_lib_size_threshold.cpp} (100%) diff --git a/include/umf/proxy_lib_handlers.h b/include/umf/proxy_lib_handlers.h new file mode 100644 index 000000000..9e6ce3108 --- /dev/null +++ b/include/umf/proxy_lib_handlers.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2024 Intel Corporation + * + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +*/ + +#ifndef UMF_PROXY_LIB_HANDLERS_H +#define UMF_PROXY_LIB_HANDLERS_H 1 + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// malloc API handlers +typedef void (*umf_proxy_lib_handler_free_pre_t)( + void *ptr, umf_memory_pool_handle_t pool); + +void umfSetProxyLibHandlerFreePre(umf_proxy_lib_handler_free_pre_t handler); + +#ifdef __cplusplus +} +#endif + +#endif /* UMF_PROXY_LIB_HANDLERS_H */ diff --git a/src/proxy_lib/proxy_lib.c b/src/proxy_lib/proxy_lib.c index 15ddfca1b..e3e4c6ebc 100644 --- a/src/proxy_lib/proxy_lib.c +++ b/src/proxy_lib/proxy_lib.c @@ -50,9 +50,9 @@ #include #include #include +#include #include "base_alloc_linear.h" -#include "proxy_lib.h" #include "utils_common.h" #include "utils_load_library.h" #include "utils_log.h" @@ -128,6 +128,9 @@ static umf_memory_pool_handle_t Proxy_pool = NULL; // it protects us from recursion in umfPool*() static __TLS int was_called_from_umfPool = 0; +// malloc API handlers +static umf_proxy_lib_handler_free_pre_t Handler_free_pre = NULL; + /*****************************************************************************/ /*** The constructor and destructor of the proxy library *********************/ /*****************************************************************************/ @@ -384,11 +387,19 @@ void free(void *ptr) { return; } + // NOTE: for system allocations made during UMF and Proxy Lib + // initialisation, we never call free handlers, as they should handle + // only user-made allocations if (ba_leak_free(ptr) == 0) { return; } - if (Proxy_pool && (umfPoolByPtr(ptr) == Proxy_pool)) { + umf_memory_pool_handle_t pool = umfPoolByPtr(ptr); + if (Proxy_pool && (pool == Proxy_pool)) { + if (Handler_free_pre) { + Handler_free_pre(ptr, pool); + } + if (umfPoolFree(Proxy_pool, ptr) != UMF_RESULT_SUCCESS) { LOG_ERR("umfPoolFree() failed"); } @@ -397,6 +408,9 @@ void free(void *ptr) { #ifndef _WIN32 if (Size_threshold_value) { + if (Handler_free_pre) { + Handler_free_pre(ptr, NULL); + } System_free(ptr); return; } @@ -545,3 +559,9 @@ void *_aligned_offset_recalloc(void *ptr, size_t num, size_t size, } #endif + +// malloc API handlers + +void umfSetProxyLibHandlerFreePre(umf_proxy_lib_handler_free_pre_t handler) { + Handler_free_pre = handler; +} diff --git a/src/proxy_lib/proxy_lib.def b/src/proxy_lib/proxy_lib.def index f30b40556..5335388a5 100644 --- a/src/proxy_lib/proxy_lib.def +++ b/src/proxy_lib/proxy_lib.def @@ -21,3 +21,5 @@ EXPORTS _aligned_offset_malloc _aligned_offset_realloc _aligned_offset_recalloc +; handlers + umfSetProxyLibHandlerFreePre diff --git a/src/proxy_lib/proxy_lib.h b/src/proxy_lib/proxy_lib.h deleted file mode 100644 index aca5e8b58..000000000 --- a/src/proxy_lib/proxy_lib.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2024 Intel Corporation - * - * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ - -#ifndef UMF_PROXY_LIB_H -#define UMF_PROXY_LIB_H 1 - -#ifdef __cplusplus -extern "C" { -#endif - -void proxy_lib_create_common(void); -void proxy_lib_destroy_common(void); - -#ifdef __cplusplus -} -#endif - -#endif /* UMF_PROXY_LIB_H */ diff --git a/src/proxy_lib/proxy_lib.map b/src/proxy_lib/proxy_lib.map index 5d93d03ba..23ca9569a 100644 --- a/src/proxy_lib/proxy_lib.map +++ b/src/proxy_lib/proxy_lib.map @@ -4,7 +4,7 @@ # linker VERSION script -{ +PROXY_LIB_0.10 { global: aligned_alloc; calloc; @@ -12,6 +12,8 @@ malloc; malloc_usable_size; realloc; + # handlers + umfSetProxyLibHandlerFreePre; local: *; }; diff --git a/src/proxy_lib/proxy_lib_linux.c b/src/proxy_lib/proxy_lib_linux.c index 50332b49f..d1960d607 100644 --- a/src/proxy_lib/proxy_lib_linux.c +++ b/src/proxy_lib/proxy_lib_linux.c @@ -7,7 +7,10 @@ * */ -#include "proxy_lib.h" +#include + +void proxy_lib_create_common(void); +void proxy_lib_destroy_common(void); // The priority 102 is used, because the constructor should be called as the second one // (just after the first constructor of the base allocator with priority 101) diff --git a/src/proxy_lib/proxy_lib_windows.c b/src/proxy_lib/proxy_lib_windows.c index 8bbff609c..c396ed484 100644 --- a/src/proxy_lib/proxy_lib_windows.c +++ b/src/proxy_lib/proxy_lib_windows.c @@ -9,7 +9,10 @@ #include -#include "proxy_lib.h" +#include + +void proxy_lib_create_common(void); +void proxy_lib_destroy_common(void); static void proxy_lib_create(void) { proxy_lib_create_common(); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d5a07bfbb..4924742b4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -445,14 +445,19 @@ add_umf_test( if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY) add_umf_test( NAME proxy_lib_basic - SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib.cpp + SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib.cpp + LIBS ${UMF_UTILS_FOR_TEST} umf_proxy) + + add_umf_test( + NAME proxy_lib_handlers + SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib_handlers.cpp LIBS ${UMF_UTILS_FOR_TEST} umf_proxy) # TODO enable this test on Windows if(LINUX) add_umf_test( NAME test_proxy_lib_size_threshold - SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib_size_threshold.cpp + SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib_size_threshold.cpp LIBS ${UMF_UTILS_FOR_TEST} umf_proxy) set_property(TEST umf-test_proxy_lib_size_threshold PROPERTY ENVIRONMENT UMF_PROXY="size.threshold=64") diff --git a/test/test_proxy_lib.cpp b/test/proxy_lib/proxy_lib.cpp similarity index 100% rename from test/test_proxy_lib.cpp rename to test/proxy_lib/proxy_lib.cpp diff --git a/test/proxy_lib/proxy_lib_handlers.cpp b/test/proxy_lib/proxy_lib_handlers.cpp new file mode 100644 index 000000000..ad367935f --- /dev/null +++ b/test/proxy_lib/proxy_lib_handlers.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2024 Intel Corporation + * + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +*/ + +#if defined(__APPLE__) +#include +#else +#include +#endif + +#include + +#include "base.hpp" +#include "test_helpers.h" +#include "utils_common.h" + +using umf_test::test; + +#define SIZE_64 64 +#define ALIGN_1024 1024 + +static int free_cnt = 0; +void handler_free_pre(void *ptr, umf_memory_pool_handle_t pool) { + (void)ptr; + (void)pool; + free_cnt += 1; +} + +TEST_F(test, proxyLib_handlers_free) { + + void *ptr = ::malloc(SIZE_64); + ASSERT_NE(ptr, nullptr); + + umfSetProxyLibHandlerFreePre(handler_free_pre); + ASSERT_EQ(free_cnt, 0); + + ::free(ptr); + ASSERT_EQ(free_cnt, 1); +} diff --git a/test/test_proxy_lib_size_threshold.cpp b/test/proxy_lib/proxy_lib_size_threshold.cpp similarity index 100% rename from test/test_proxy_lib_size_threshold.cpp rename to test/proxy_lib/proxy_lib_size_threshold.cpp