|
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_post_t Handler_free_post = NULL; |
| 133 | + |
131 | 134 | /*****************************************************************************/ |
132 | 135 | /*** The constructor and destructor of the proxy library *********************/ |
133 | 136 | /*****************************************************************************/ |
@@ -380,30 +383,38 @@ void *calloc(size_t nmemb, size_t size) { |
380 | 383 | } |
381 | 384 |
|
382 | 385 | void free(void *ptr) { |
| 386 | + umf_memory_pool_handle_t pool = NULL; |
| 387 | + |
383 | 388 | if (ptr == NULL) { |
384 | | - return; |
| 389 | + goto call_handler_post; |
385 | 390 | } |
386 | 391 |
|
387 | 392 | if (ba_leak_free(ptr) == 0) { |
388 | | - return; |
| 393 | + goto call_handler_post; |
389 | 394 | } |
390 | 395 |
|
391 | | - if (Proxy_pool && (umfPoolByPtr(ptr) == Proxy_pool)) { |
| 396 | + pool = umfPoolByPtr(ptr); |
| 397 | + if (Proxy_pool && (pool == Proxy_pool)) { |
392 | 398 | if (umfPoolFree(Proxy_pool, ptr) != UMF_RESULT_SUCCESS) { |
393 | 399 | LOG_ERR("umfPoolFree() failed"); |
394 | 400 | } |
395 | | - return; |
| 401 | + goto call_handler_post; |
396 | 402 | } |
397 | 403 |
|
398 | 404 | #ifndef _WIN32 |
399 | 405 | if (Size_threshold_value) { |
400 | 406 | System_free(ptr); |
401 | | - return; |
| 407 | + goto call_handler_post; |
402 | 408 | } |
403 | 409 | #endif /* _WIN32 */ |
404 | 410 |
|
405 | 411 | LOG_ERR("free() failed: %p", ptr); |
406 | 412 |
|
| 413 | +call_handler_post: |
| 414 | + if (Handler_free_post) { |
| 415 | + Handler_free_post(ptr, pool); |
| 416 | + } |
| 417 | + |
407 | 418 | return; |
408 | 419 | } |
409 | 420 |
|
@@ -545,3 +556,9 @@ void *_aligned_offset_recalloc(void *ptr, size_t num, size_t size, |
545 | 556 | } |
546 | 557 |
|
547 | 558 | #endif |
| 559 | + |
| 560 | +// malloc API handlers |
| 561 | + |
| 562 | +void umfSetProxyLibHandlerFreePost(umf_proxy_lib_handler_free_post_t handler) { |
| 563 | + Handler_free_post = handler; |
| 564 | +} |
0 commit comments