Skip to content

Commit 2c03c43

Browse files
committed
add free handle POC
1 parent 66b161a commit 2c03c43

11 files changed

+111
-29
lines changed

include/umf/proxy_lib_handlers.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#ifndef UMF_PROXY_LIB_HANDLERS_H
9+
#define UMF_PROXY_LIB_HANDLERS_H 1
10+
11+
#include <umf/memory_pool.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
// malloc API handlers
18+
typedef void (*umf_proxy_lib_handler_free_pre_t)(void *ptr,
19+
umf_memory_pool_handle_t pool);
20+
21+
void umfSetProxyLibHandlerFreePre(umf_proxy_lib_handler_free_pre_t handler);
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif
26+
27+
#endif /* UMF_PROXY_LIB_HANDLERS_H */

src/proxy_lib/proxy_lib.c

+22-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
#include <umf/memory_pool.h>
5151
#include <umf/memory_provider.h>
5252
#include <umf/providers/provider_os_memory.h>
53+
#include <umf/proxy_lib_handlers.h>
5354

5455
#include "base_alloc_linear.h"
55-
#include "proxy_lib.h"
5656
#include "utils_common.h"
5757
#include "utils_load_library.h"
5858
#include "utils_log.h"
@@ -135,6 +135,9 @@ static __TLS int was_called_from_umfPool = 0;
135135
// TODO remove this WA when the issue is fixed.
136136
static __TLS int was_called_from_malloc_usable_size = 0;
137137

138+
// malloc API handlers
139+
static umf_proxy_lib_handler_free_pre_t Handler_free_pre = NULL;
140+
138141
/*****************************************************************************/
139142
/*** The constructor and destructor of the proxy library *********************/
140143
/*****************************************************************************/
@@ -391,11 +394,19 @@ void free(void *ptr) {
391394
return;
392395
}
393396

397+
// NOTE: for system allocations made during UMF and Proxy Lib
398+
// initialisation, we never call free handlers, as they should handle
399+
// only user-made allocations
394400
if (ba_leak_free(ptr) == 0) {
395401
return;
396402
}
397403

398-
if (Proxy_pool && (umfPoolByPtr(ptr) == Proxy_pool)) {
404+
umf_memory_pool_handle_t pool = umfPoolByPtr(ptr);
405+
if (Proxy_pool && (pool == Proxy_pool)) {
406+
if (Handler_free_pre) {
407+
Handler_free_pre(ptr, pool);
408+
}
409+
399410
if (umfPoolFree(Proxy_pool, ptr) != UMF_RESULT_SUCCESS) {
400411
LOG_ERR("umfPoolFree() failed");
401412
}
@@ -404,6 +415,9 @@ void free(void *ptr) {
404415

405416
#ifndef _WIN32
406417
if (Size_threshold_value) {
418+
if (Handler_free_pre) {
419+
Handler_free_pre(ptr, NULL);
420+
}
407421
System_free(ptr);
408422
return;
409423
}
@@ -555,3 +569,9 @@ void *_aligned_offset_recalloc(void *ptr, size_t num, size_t size,
555569
}
556570

557571
#endif
572+
573+
// malloc API handlers
574+
575+
void umfSetProxyLibHandlerFreePre(umf_proxy_lib_handler_free_pre_t handler) {
576+
Handler_free_pre = handler;
577+
}

src/proxy_lib/proxy_lib.def

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ EXPORTS
2121
_aligned_offset_malloc
2222
_aligned_offset_realloc
2323
_aligned_offset_recalloc
24+
; handlers
25+
umfSetProxyLibHandlerFreePre

src/proxy_lib/proxy_lib.h

-22
This file was deleted.

src/proxy_lib/proxy_lib.map

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
# linker VERSION script
66

7-
{
7+
PROXY_LIB_0.10 {
88
global:
99
aligned_alloc;
1010
calloc;
1111
free;
1212
malloc;
1313
malloc_usable_size;
1414
realloc;
15+
# handlers
16+
umfSetProxyLibHandlerFreePre;
1517
local:
1618
*;
1719
};

src/proxy_lib/proxy_lib_linux.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
*
88
*/
99

10-
#include "proxy_lib.h"
10+
#include <umf/proxy_lib_handlers.h>
11+
12+
void proxy_lib_create_common(void);
13+
void proxy_lib_destroy_common(void);
1114

1215
// The priority 102 is used, because the constructor should be called as the second one
1316
// (just after the first constructor of the base allocator with priority 101)

src/proxy_lib/proxy_lib_windows.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
#include <Windows.h>
1111

12-
#include "proxy_lib.h"
12+
#include <umf/proxy_lib_handlers.h>
13+
14+
void proxy_lib_create_common(void);
15+
void proxy_lib_destroy_common(void);
1316

1417
static void proxy_lib_create(void) { proxy_lib_create_common(); }
1518

test/CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,19 @@ add_umf_test(
447447
if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
448448
add_umf_test(
449449
NAME proxy_lib_basic
450-
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib.cpp
450+
SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib.cpp
451+
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
452+
453+
add_umf_test(
454+
NAME proxy_lib_handlers
455+
SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib_handlers.cpp
451456
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
452457

453458
# TODO enable this test on Windows
454459
if(LINUX)
455460
add_umf_test(
456461
NAME test_proxy_lib_size_threshold
457-
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib_size_threshold.cpp
462+
SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib_size_threshold.cpp
458463
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
459464
set_property(TEST umf-test_proxy_lib_size_threshold
460465
PROPERTY ENVIRONMENT UMF_PROXY="size.threshold=64")
File renamed without changes.

test/proxy_lib/proxy_lib_handlers.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#if defined(__APPLE__)
9+
#include <malloc/malloc.h>
10+
#else
11+
#include <malloc.h>
12+
#endif
13+
14+
#include <umf/proxy_lib_handlers.h>
15+
16+
#include "base.hpp"
17+
#include "test_helpers.h"
18+
#include "utils_common.h"
19+
20+
using umf_test::test;
21+
22+
#define SIZE_64 64
23+
#define ALIGN_1024 1024
24+
25+
static int free_cnt = 0;
26+
void handler_free_pre(void *ptr, umf_memory_pool_handle_t pool) {
27+
(void)ptr;
28+
(void)pool;
29+
free_cnt += 1;
30+
}
31+
32+
TEST_F(test, proxyLib_handlers_free) {
33+
34+
void *ptr = ::malloc(SIZE_64);
35+
ASSERT_NE(ptr, nullptr);
36+
37+
umfSetProxyLibHandlerFreePre(handler_free_pre);
38+
ASSERT_EQ(free_cnt, 0);
39+
40+
::free(ptr);
41+
ASSERT_EQ(free_cnt, 1);
42+
}

0 commit comments

Comments
 (0)