11#include " ../common/TracyAlloc.hpp"
22
3+ #if defined( TRACY_DEFAULT_MEMORY_PROFLER )/* && defined( TRACY_ON_DEMAND )*/
4+ # if __has_include( <dlfcn.h>)
5+ # include " ../client/TracyProfiler.hpp"
6+ # include < dlfcn.h>
7+ # define TRACY_ENABLE_DEFAULT_MEMORY_PROFLER
8+ # endif
9+ #endif
10+
11+ #ifndef TRACY_ENABLE_DEFAULT_MEMORY_PROFLER
12+ namespace
13+ {
14+ auto _malloc = &malloc;
15+ auto _free = &free;
16+ auto _calloc = &calloc;
17+ auto _realloc = &realloc;
18+ } // namespace {
19+ #else
20+ namespace
21+ {
22+
23+ inline void * _malloc ( size_t size )
24+ {
25+ static auto malloc_ = reinterpret_cast <void * (*)( size_t )>( dlsym ( RTLD_NEXT , " malloc" ) );
26+ return malloc_ ( size );
27+ }
28+ inline void _free ( void * ptr )
29+ {
30+ static auto free_ = reinterpret_cast <void ( * )( void * )>( dlsym ( RTLD_NEXT , " free" ) );
31+ free_ ( ptr );
32+ }
33+ inline void * _calloc ( size_t nmemb, size_t size )
34+ {
35+ static auto calloc_ = reinterpret_cast <void * (*)( size_t , size_t )>( dlsym ( RTLD_NEXT , " calloc" ) );
36+ return calloc_ ( nmemb, size );
37+ }
38+
39+ inline void * _realloc ( void * ptr, size_t size )
40+ {
41+ static auto realloc_ = reinterpret_cast <void * (*)( void *, size_t )>( dlsym ( RTLD_NEXT , " realloc" ) );
42+ return realloc_ ( ptr, size );
43+ }
44+
45+ } // namespace {
46+
47+ extern " C"
48+ {
49+ void * malloc ( size_t size )
50+ {
51+ if ( tracy::DirectAlloc::s_direct )
52+ return _malloc ( size );
53+ tracy::DirectAlloc locker;
54+ auto _ptr = _malloc ( size );
55+ tracy::Profiler::MemAllocCallstack ( _ptr, size, 50 , false );
56+ return _ptr;
57+ }
58+
59+ void free ( void * ptr )
60+ {
61+ if ( tracy::DirectAlloc::s_direct )
62+ return _free ( ptr );
63+ tracy::DirectAlloc locker;
64+ if ( ptr )
65+ tracy::Profiler::MemFreeCallstack ( ptr, 50 , false );
66+ _free ( ptr );
67+ }
68+
69+ void * calloc ( size_t nmemb, size_t size )
70+ {
71+ if ( tracy::DirectAlloc::s_direct )
72+ return _calloc ( nmemb, size );
73+ tracy::DirectAlloc locker;
74+ auto _ptr = _calloc ( nmemb, size );
75+ tracy::Profiler::MemAllocCallstack ( _ptr, nmemb * size, 50 , false );
76+ return _ptr;
77+ }
78+
79+ void * realloc ( void * ptr, size_t size )
80+ {
81+ if ( tracy::DirectAlloc::s_direct )
82+ return _realloc ( ptr, size );
83+ tracy::DirectAlloc locker;
84+ if ( ptr )
85+ tracy::Profiler::MemFreeCallstack ( ptr, 50 , false );
86+ auto _ptr = _realloc ( ptr, size );
87+ tracy::Profiler::MemAllocCallstack ( _ptr, size, 50 , false );
88+ return _ptr;
89+ }
90+ } // extern "C" {
91+ #endif
92+
393#ifdef TRACY_USE_RPMALLOC
494
5- #include < atomic>
95+ # include < atomic>
696
7- #include " ../common/TracyForceInline.hpp"
8- #include " ../common/TracyYield.hpp"
97+ # include " ../common/TracyForceInline.hpp"
98+ # include " ../common/TracyYield.hpp"
99+ #endif
9100
10101namespace tracy
11102{
12103
104+ thread_local int DirectAlloc::s_direct = 0 ;
105+
106+ #ifdef TRACY_USE_RPMALLOC
13107extern thread_local bool RpThreadInitDone;
14108extern std::atomic<int > RpInitDone;
15109extern std::atomic<int > RpInitLock;
@@ -20,7 +114,11 @@ tracy_no_inline static void InitRpmallocPlumbing()
20114 if ( !done )
21115 {
22116 int expected = 0 ;
23- while ( !RpInitLock.compare_exchange_weak ( expected, 1 , std::memory_order_release, std::memory_order_relaxed ) ) { expected = 0 ; YieldThread (); }
117+ while ( !RpInitLock.compare_exchange_weak ( expected, 1 , std::memory_order_release, std::memory_order_relaxed ) )
118+ {
119+ expected = 0 ;
120+ YieldThread ();
121+ }
24122 const auto done = RpInitDone.load ( std::memory_order_acquire );
25123 if ( !done )
26124 {
@@ -37,7 +135,54 @@ TRACY_API void InitRpmalloc()
37135{
38136 if ( !RpThreadInitDone ) InitRpmallocPlumbing ();
39137}
138+ #endif
40139
140+ TRACY_API void * tracy_malloc ( size_t size )
141+ {
142+ # ifdef TRACY_USE_RPMALLOC
143+ InitRpmalloc ();
144+ return rpmalloc ( size );
145+ # else
146+ return _malloc ( size );
147+ # endif
41148}
42149
43- #endif
150+ TRACY_API void * tracy_malloc_fast ( size_t size )
151+ {
152+ # ifdef TRACY_USE_RPMALLOC
153+ return rpmalloc ( size );
154+ # else
155+ return _malloc ( size );
156+ # endif
157+ }
158+
159+ TRACY_API void tracy_free ( void * ptr )
160+ {
161+ # ifdef TRACY_USE_RPMALLOC
162+ InitRpmalloc ();
163+ rpfree ( ptr );
164+ # else
165+ _free ( ptr );
166+ # endif
167+ }
168+
169+ TRACY_API void tracy_free_fast ( void * ptr )
170+ {
171+ # ifdef TRACY_USE_RPMALLOC
172+ rpfree ( ptr );
173+ # else
174+ _free ( ptr );
175+ # endif
176+ }
177+
178+ TRACY_API void * tracy_realloc ( void * ptr, size_t size )
179+ {
180+ # ifdef TRACY_USE_RPMALLOC
181+ InitRpmalloc ();
182+ return rprealloc ( ptr, size );
183+ # else
184+ return _realloc ( ptr, size );
185+ # endif
186+ }
187+
188+ } // namespace tracy
0 commit comments