50
50
#include <umf/memory_pool.h>
51
51
#include <umf/memory_provider.h>
52
52
#include <umf/providers/provider_os_memory.h>
53
+ #include <umf/proxy_lib_handlers.h>
53
54
54
55
#include "base_alloc_linear.h"
55
- #include "proxy_lib.h"
56
56
#include "utils_common.h"
57
57
#include "utils_load_library.h"
58
58
#include "utils_log.h"
@@ -128,6 +128,9 @@ static umf_memory_pool_handle_t Proxy_pool = NULL;
128
128
// it protects us from recursion in umfPool*()
129
129
static __TLS int was_called_from_umfPool = 0 ;
130
130
131
+ // malloc API handlers
132
+ static umf_proxy_lib_handler_free_pre_t Handler_free_pre = NULL ;
133
+
131
134
/*****************************************************************************/
132
135
/*** The constructor and destructor of the proxy library *********************/
133
136
/*****************************************************************************/
@@ -384,11 +387,19 @@ void free(void *ptr) {
384
387
return ;
385
388
}
386
389
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
387
393
if (ba_leak_free (ptr ) == 0 ) {
388
394
return ;
389
395
}
390
396
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
+
392
403
if (umfPoolFree (Proxy_pool , ptr ) != UMF_RESULT_SUCCESS ) {
393
404
LOG_ERR ("umfPoolFree() failed" );
394
405
}
@@ -397,6 +408,9 @@ void free(void *ptr) {
397
408
398
409
#ifndef _WIN32
399
410
if (Size_threshold_value ) {
411
+ if (Handler_free_pre ) {
412
+ Handler_free_pre (ptr , NULL );
413
+ }
400
414
System_free (ptr );
401
415
return ;
402
416
}
@@ -545,3 +559,9 @@ void *_aligned_offset_recalloc(void *ptr, size_t num, size_t size,
545
559
}
546
560
547
561
#endif
562
+
563
+ // malloc API handlers
564
+
565
+ void umfSetProxyLibHandlerFreePre (umf_proxy_lib_handler_free_pre_t handler ) {
566
+ Handler_free_pre = handler ;
567
+ }
0 commit comments