2727 * - _aligned_offset_recalloc()
2828 */
2929
30+ #ifndef _WIN32
31+ #define _GNU_SOURCE
32+ #include <dlfcn.h>
33+ #undef _GNU_SOURCE
34+ #endif /* _WIN32 */
35+
3036#if (defined PROXY_LIB_USES_JEMALLOC_POOL )
3137#include <umf/pools/pool_jemalloc.h>
3238#define umfPoolManagerOps umfJemallocPoolOps
@@ -103,12 +109,28 @@ static umf_memory_pool_handle_t Proxy_pool = NULL;
103109// it protects us from recursion in umfPool*()
104110static __TLS int was_called_from_umfPool = 0 ;
105111
112+ typedef void * (* system_aligned_alloc_t )(size_t alignment , size_t size );
113+ typedef void * (* system_calloc_t )(size_t nmemb , size_t size );
114+ typedef void (* system_free_t )(void * ptr );
115+ typedef void * (* system_malloc_t )(size_t size );
116+ typedef size_t (* system_malloc_usable_size_t )(void * ptr );
117+ typedef void * (* system_realloc_t )(void * ptr , size_t size );
118+
119+ static system_aligned_alloc_t system_aligned_alloc ;
120+ static system_calloc_t system_calloc ;
121+ static system_free_t system_free ;
122+ static system_malloc_t system_malloc ;
123+ static system_malloc_usable_size_t system_malloc_usable_size ;
124+ static system_realloc_t system_realloc ;
125+ static size_t threshold_value = 0 ;
126+
106127/*****************************************************************************/
107128/*** The constructor and destructor of the proxy library *********************/
108129/*****************************************************************************/
109130
110131void proxy_lib_create_common (void ) {
111132 utils_log_init ();
133+
112134 umf_os_memory_provider_params_t os_params =
113135 umfOsMemoryProviderParamsDefault ();
114136 umf_result_t umf_result ;
@@ -117,8 +139,8 @@ void proxy_lib_create_common(void) {
117139 char shm_name [NAME_MAX ];
118140
119141 if (utils_env_var_has_str ("UMF_PROXY" , "page.disposition=shared-fd" )) {
120- LOG_DEBUG ("proxy_lib: using the MAP_SHARED visibility mode with the "
121- "file descriptor duplication" );
142+ LOG_DEBUG ("using the MAP_SHARED visibility mode with the file "
143+ "descriptor duplication" );
122144 os_params .visibility = UMF_MEM_MAP_SHARED ;
123145 os_params .shm_name = NULL ;
124146
@@ -130,8 +152,8 @@ void proxy_lib_create_common(void) {
130152 sprintf (shm_name , "umf_proxy_lib_shm_pid_%i" , utils_getpid ());
131153 os_params .shm_name = shm_name ;
132154
133- LOG_DEBUG ("proxy_lib: using the MAP_SHARED visibility mode with the "
134- "named shared memory: %s" ,
155+ LOG_DEBUG ("using the MAP_SHARED visibility mode with the named shared "
156+ "memory: %s" ,
135157 os_params .shm_name );
136158 }
137159#endif
@@ -149,8 +171,32 @@ void proxy_lib_create_common(void) {
149171 LOG_ERR ("creating UMF pool manager failed" );
150172 exit (-1 );
151173 }
174+
175+ LOG_DEBUG ("proxy pool created" );
176+
177+ #ifndef _WIN32
178+ * ((void * * )(& system_aligned_alloc )) = dlsym (RTLD_NEXT , "aligned_alloc" );
179+ * ((void * * )(& system_calloc )) = dlsym (RTLD_NEXT , "calloc" );
180+ * ((void * * )(& system_free )) = dlsym (RTLD_NEXT , "free" );
181+ * ((void * * )(& system_malloc )) = dlsym (RTLD_NEXT , "malloc" );
182+ * ((void * * )(& system_malloc_usable_size )) =
183+ dlsym (RTLD_NEXT , "malloc_usable_size" );
184+ * ((void * * )(& system_realloc )) = dlsym (RTLD_NEXT , "realloc" );
185+
186+ if (system_aligned_alloc && system_calloc && system_free && system_malloc &&
187+ system_malloc_usable_size && system_realloc ) {
188+ // TODO threshold_value will be read from the environment variable
189+ threshold_value = 128 ;
190+ LOG_DEBUG ("all system hooks initialized, threshold_value = %zu" ,
191+ threshold_value );
192+ } else {
193+ LOG_WARN ("NOT all system hooks initialized" );
194+ }
195+ #endif
196+
152197 // The UMF pool has just been created (Proxy_pool != NULL). Stop using
153198 // the linear allocator and start using the UMF pool allocator from now on.
199+ LOG_DEBUG ("proxy library initialized" );
154200}
155201
156202void proxy_lib_destroy_common (void ) {
@@ -246,6 +292,10 @@ static inline size_t ba_leak_pool_contains_pointer(void *ptr) {
246292/*****************************************************************************/
247293
248294void * malloc (size_t size ) {
295+ if (size < threshold_value ) {
296+ return system_malloc (size );
297+ }
298+
249299 if (!was_called_from_umfPool && Proxy_pool ) {
250300 was_called_from_umfPool = 1 ;
251301 void * ptr = umfPoolMalloc (Proxy_pool , size );
@@ -257,6 +307,10 @@ void *malloc(size_t size) {
257307}
258308
259309void * calloc (size_t nmemb , size_t size ) {
310+ if ((nmemb * size ) < threshold_value ) {
311+ return system_calloc (nmemb , size );
312+ }
313+
260314 if (!was_called_from_umfPool && Proxy_pool ) {
261315 was_called_from_umfPool = 1 ;
262316 void * ptr = umfPoolCalloc (Proxy_pool , nmemb , size );
@@ -276,15 +330,17 @@ void free(void *ptr) {
276330 return ;
277331 }
278332
279- if (Proxy_pool ) {
333+ if (Proxy_pool && ( umfPoolByPtr ( ptr ) == Proxy_pool ) ) {
280334 if (umfPoolFree (Proxy_pool , ptr ) != UMF_RESULT_SUCCESS ) {
281335 LOG_ERR ("umfPoolFree() failed" );
282- assert (0 );
283336 }
284337 return ;
285338 }
286339
287- assert (0 );
340+ if (threshold_value ) {
341+ system_free (ptr );
342+ }
343+
288344 return ;
289345}
290346
@@ -303,18 +359,26 @@ void *realloc(void *ptr, size_t size) {
303359 return ba_leak_realloc (ptr , size , leak_pool_contains_pointer );
304360 }
305361
306- if (Proxy_pool ) {
362+ if (!was_called_from_umfPool && Proxy_pool &&
363+ (umfPoolByPtr (ptr ) == Proxy_pool )) {
307364 was_called_from_umfPool = 1 ;
308365 void * new_ptr = umfPoolRealloc (Proxy_pool , ptr , size );
309366 was_called_from_umfPool = 0 ;
310367 return new_ptr ;
311368 }
312369
313- assert (0 );
370+ if (threshold_value ) {
371+ return system_realloc (ptr , size );
372+ }
373+
314374 return NULL ;
315375}
316376
317377void * aligned_alloc (size_t alignment , size_t size ) {
378+ if (size < threshold_value ) {
379+ return system_aligned_alloc (alignment , size );
380+ }
381+
318382 if (!was_called_from_umfPool && Proxy_pool ) {
319383 was_called_from_umfPool = 1 ;
320384 void * ptr = umfPoolAlignedMalloc (Proxy_pool , size , alignment );
@@ -330,19 +394,30 @@ size_t _msize(void *ptr) {
330394#else
331395size_t malloc_usable_size (void * ptr ) {
332396#endif
333-
397+ LOG_DEBUG ( "malloc_usable_size: %p BEGIN" , ptr );
334398 // a check to verify we are running the proxy library
335399 if (ptr == (void * )0x01 ) {
336400 return 0xDEADBEEF ;
337401 }
338402
339- if (!was_called_from_umfPool && Proxy_pool ) {
403+ LOG_DEBUG ("malloc_usable_size: %p ba_leak_pool_contains_pointer" , ptr );
404+ if (ba_leak_pool_contains_pointer (ptr )) {
405+ return 0 ; // unsupported in this case
406+ }
407+
408+ LOG_DEBUG ("malloc_usable_size: %p umfPoolByPtr" , ptr );
409+ if (!was_called_from_umfPool && Proxy_pool && (umfPoolByPtr (ptr ) == Proxy_pool )) {
340410 was_called_from_umfPool = 1 ;
341411 size_t size = umfPoolMallocUsableSize (Proxy_pool , ptr );
342412 was_called_from_umfPool = 0 ;
343413 return size ;
344414 }
345415
416+ if (threshold_value ) {
417+ LOG_DEBUG ("malloc_usable_size: %p SYSTEM" , ptr );
418+ return system_malloc_usable_size (ptr );
419+ }
420+
346421 return 0 ; // unsupported in this case
347422}
348423
0 commit comments