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"
@@ -135,6 +135,9 @@ static __TLS int was_called_from_umfPool = 0;
135
135
// TODO remove this WA when the issue is fixed.
136
136
static __TLS int was_called_from_malloc_usable_size = 0 ;
137
137
138
+ // malloc API handlers
139
+ static umf_proxy_lib_handler_free_pre_t Handler_free_pre = NULL ;
140
+
138
141
/*****************************************************************************/
139
142
/*** The constructor and destructor of the proxy library *********************/
140
143
/*****************************************************************************/
@@ -391,11 +394,19 @@ void free(void *ptr) {
391
394
return ;
392
395
}
393
396
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
394
400
if (ba_leak_free (ptr ) == 0 ) {
395
401
return ;
396
402
}
397
403
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
+
399
410
if (umfPoolFree (Proxy_pool , ptr ) != UMF_RESULT_SUCCESS ) {
400
411
LOG_ERR ("umfPoolFree() failed" );
401
412
}
@@ -404,6 +415,9 @@ void free(void *ptr) {
404
415
405
416
#ifndef _WIN32
406
417
if (Size_threshold_value ) {
418
+ if (Handler_free_pre ) {
419
+ Handler_free_pre (ptr , NULL );
420
+ }
407
421
System_free (ptr );
408
422
return ;
409
423
}
@@ -555,3 +569,9 @@ void *_aligned_offset_recalloc(void *ptr, size_t num, size_t size,
555
569
}
556
570
557
571
#endif
572
+
573
+ // malloc API handlers
574
+
575
+ void umfSetProxyLibHandlerFreePre (umf_proxy_lib_handler_free_pre_t handler ) {
576
+ Handler_free_pre = handler ;
577
+ }
0 commit comments