Skip to content

Commit 6ea5b2f

Browse files
committed
add free handle POC
1 parent 109f4b2 commit 6ea5b2f

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)(
19+
void *ptr, 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"
@@ -128,6 +128,9 @@ static umf_memory_pool_handle_t Proxy_pool = NULL;
128128
// it protects us from recursion in umfPool*()
129129
static __TLS int was_called_from_umfPool = 0;
130130

131+
// malloc API handlers
132+
static umf_proxy_lib_handler_free_pre_t Handler_free_pre = NULL;
133+
131134
/*****************************************************************************/
132135
/*** The constructor and destructor of the proxy library *********************/
133136
/*****************************************************************************/
@@ -384,11 +387,19 @@ void free(void *ptr) {
384387
return;
385388
}
386389

390+
// NOTE: for system allocations made during UMF and Proxy Lib
391+
// initialisation, we never call free handlers, as they should handle
392+
// only user-made allocations
387393
if (ba_leak_free(ptr) == 0) {
388394
return;
389395
}
390396

391-
if (Proxy_pool && (umfPoolByPtr(ptr) == Proxy_pool)) {
397+
umf_memory_pool_handle_t pool = umfPoolByPtr(ptr);
398+
if (Proxy_pool && (pool == Proxy_pool)) {
399+
if (Handler_free_pre) {
400+
Handler_free_pre(ptr, pool);
401+
}
402+
392403
if (umfPoolFree(Proxy_pool, ptr) != UMF_RESULT_SUCCESS) {
393404
LOG_ERR("umfPoolFree() failed");
394405
}
@@ -397,6 +408,9 @@ void free(void *ptr) {
397408

398409
#ifndef _WIN32
399410
if (Size_threshold_value) {
411+
if (Handler_free_pre) {
412+
Handler_free_pre(ptr, NULL);
413+
}
400414
System_free(ptr);
401415
return;
402416
}
@@ -545,3 +559,9 @@ void *_aligned_offset_recalloc(void *ptr, size_t num, size_t size,
545559
}
546560

547561
#endif
562+
563+
// malloc API handlers
564+
565+
void umfSetProxyLibHandlerFreePre(umf_proxy_lib_handler_free_pre_t handler) {
566+
Handler_free_pre = handler;
567+
}

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
@@ -445,14 +445,19 @@ add_umf_test(
445445
if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
446446
add_umf_test(
447447
NAME proxy_lib_basic
448-
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib.cpp
448+
SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib.cpp
449+
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
450+
451+
add_umf_test(
452+
NAME proxy_lib_handlers
453+
SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib_handlers.cpp
449454
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
450455

451456
# TODO enable this test on Windows
452457
if(LINUX)
453458
add_umf_test(
454459
NAME test_proxy_lib_size_threshold
455-
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib_size_threshold.cpp
460+
SRCS ${BA_SOURCES_FOR_TEST} proxy_lib/proxy_lib_size_threshold.cpp
456461
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
457462
set_property(TEST umf-test_proxy_lib_size_threshold
458463
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)