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.
136136static __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+ }
0 commit comments