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*()
129129static __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+ }
0 commit comments