@@ -48,7 +48,13 @@ struct NRT_MemSys {
4848 /* Shutdown flag */
4949 int shutting;
5050 /* Stats */
51- std::atomic_size_t stats_alloc, stats_free, stats_mi_alloc, stats_mi_free;
51+ struct {
52+ bool enabled;
53+ std::atomic_size_t alloc;
54+ std::atomic_size_t free;
55+ std::atomic_size_t mi_alloc;
56+ std::atomic_size_t mi_free;
57+ } stats;
5258 /* System allocation functions */
5359 struct {
5460 NRT_malloc_func malloc;
@@ -64,10 +70,12 @@ static NRT_MemSys TheMSys;
6470
6571extern " C" void NRT_MemSys_init (void ) {
6672 TheMSys.shutting = 0 ;
67- TheMSys.stats_alloc = 0 ;
68- TheMSys.stats_free = 0 ;
69- TheMSys.stats_mi_alloc = 0 ;
70- TheMSys.stats_mi_free = 0 ;
73+ // Stats are off by default, call NRT_MemSys_enable_stats to enable
74+ TheMSys.stats .enabled = false ;
75+ TheMSys.stats .alloc = 0 ;
76+ TheMSys.stats .free = 0 ;
77+ TheMSys.stats .mi_alloc = 0 ;
78+ TheMSys.stats .mi_free = 0 ;
7179 /* Bind to libc allocator */
7280 TheMSys.allocator .malloc = malloc;
7381 TheMSys.allocator .realloc = realloc;
@@ -78,36 +86,68 @@ extern "C" void NRT_MemSys_shutdown(void) {
7886 TheMSys.shutting = 1 ;
7987}
8088
89+ extern " C" void NRT_MemSys_enable_stats (void ) {
90+ TheMSys.stats .enabled = true ;
91+ }
92+
8193extern " C" void NRT_MemSys_set_allocator (NRT_malloc_func malloc_func,
8294 NRT_realloc_func realloc_func,
8395 NRT_free_func free_func)
8496{
97+ bool stats_cond = false ;
98+ if (TheMSys.stats .enabled )
99+ {
100+ stats_cond = (TheMSys.stats .alloc != TheMSys.stats .free ||
101+ TheMSys.stats .mi_alloc != TheMSys.stats .mi_free );
102+ }
85103 if ((malloc_func != TheMSys.allocator .malloc ||
86104 realloc_func != TheMSys.allocator .realloc ||
87105 free_func != TheMSys.allocator .free ) &&
88- (TheMSys.stats_alloc != TheMSys.stats_free ||
89- TheMSys.stats_mi_alloc != TheMSys.stats_mi_free )) {
106+ stats_cond) {
90107 nrt_fatal_error (" cannot change allocator while blocks are allocated" );
91108 }
92109 TheMSys.allocator .malloc = malloc_func;
93110 TheMSys.allocator .realloc = realloc_func;
94111 TheMSys.allocator .free = free_func;
95112}
96113
114+ /* This value is used as a marker for "stats are disabled", it's ASCII "AAAA" */
115+ static size_t _DISABLED_STATS_VALUE = 0x41414141 ;
116+
97117extern " C" size_t NRT_MemSys_get_stats_alloc () {
98- return TheMSys.stats_alloc ;
118+ if (TheMSys.stats .enabled )
119+ {
120+ return TheMSys.stats .alloc .load ();
121+ } else {
122+ return _DISABLED_STATS_VALUE;
123+ }
99124}
100125
101126extern " C" size_t NRT_MemSys_get_stats_free () {
102- return TheMSys.stats_free ;
127+ if (TheMSys.stats .enabled )
128+ {
129+ return TheMSys.stats .free .load ();
130+ } else {
131+ return _DISABLED_STATS_VALUE;
132+ }
103133}
104134
105135extern " C" size_t NRT_MemSys_get_stats_mi_alloc () {
106- return TheMSys.stats_mi_alloc ;
136+ if (TheMSys.stats .enabled )
137+ {
138+ return TheMSys.stats .mi_alloc .load ();
139+ } else {
140+ return _DISABLED_STATS_VALUE;
141+ }
107142}
108143
109144extern " C" size_t NRT_MemSys_get_stats_mi_free () {
110- return TheMSys.stats_mi_free ;
145+ if (TheMSys.stats .enabled )
146+ {
147+ return TheMSys.stats .mi_free .load ();
148+ } else {
149+ return _DISABLED_STATS_VALUE;
150+ }
111151}
112152
113153/*
@@ -126,7 +166,10 @@ extern "C" void NRT_MemInfo_init(NRT_MemInfo *mi,void *data, size_t size,
126166 mi->external_allocator = external_allocator;
127167 NRT_Debug (nrt_debug_print (" NRT_MemInfo_init mi=%p external_allocator=%p\n " , mi, external_allocator));
128168 /* Update stats */
129- TheMSys.stats_mi_alloc ++;
169+ if (TheMSys.stats .enabled )
170+ {
171+ TheMSys.stats .mi_alloc ++;
172+ }
130173}
131174
132175NRT_MemInfo *NRT_MemInfo_new (void *data, size_t size,
@@ -319,15 +362,21 @@ extern "C" void NRT_dealloc(NRT_MemInfo *mi) {
319362 NRT_Debug (nrt_debug_print (" NRT_dealloc meminfo: %p external_allocator: %p\n " , mi, mi->external_allocator ));
320363 if (mi->external_allocator ) {
321364 mi->external_allocator ->free (mi, mi->external_allocator ->opaque_data );
322- TheMSys.stats_free ++;;
365+ if (TheMSys.stats .enabled )
366+ {
367+ TheMSys.stats .free ++;
368+ }
323369 } else {
324370 NRT_Free (mi);
325371 }
326372}
327373
328374extern " C" void NRT_MemInfo_destroy (NRT_MemInfo *mi) {
329375 NRT_dealloc (mi);
330- TheMSys.stats_mi_free ++;
376+ if (TheMSys.stats .enabled )
377+ {
378+ TheMSys.stats .mi_free ++;
379+ }
331380}
332381
333382extern " C" void NRT_MemInfo_acquire (NRT_MemInfo *mi) {
@@ -471,7 +520,10 @@ extern "C" void* NRT_Allocate_External(size_t size, NRT_ExternalAllocator *alloc
471520 ptr = TheMSys.allocator .malloc (size);
472521 NRT_Debug (nrt_debug_print (" NRT_Allocate_External bytes=%zu ptr=%p\n " , size, ptr));
473522 }
474- TheMSys.stats_alloc ++;
523+ if (TheMSys.stats .enabled )
524+ {
525+ TheMSys.stats .alloc ++;
526+ }
475527 return ptr;
476528}
477529
@@ -485,7 +537,10 @@ extern "C" void *NRT_Reallocate(void *ptr, size_t size) {
485537extern " C" void NRT_Free (void *ptr) {
486538 NRT_Debug (nrt_debug_print (" NRT_Free %p\n " , ptr));
487539 TheMSys.allocator .free (ptr);
488- TheMSys.stats_free ++;;
540+ if (TheMSys.stats .enabled )
541+ {
542+ TheMSys.stats .free ++;
543+ }
489544}
490545
491546/*
0 commit comments