@@ -9,7 +9,9 @@ terms of the MIT license. A copy of the license can be found in the file
99#include "mimalloc/atomic.h"
1010#include "mimalloc/prim.h"
1111
12- #define os_stats (&_mi_stats_main)
12+ #define mi_os_stat_increase (stat ,amount ) _mi_stat_increase(&_mi_stats_main.stat, amount)
13+ #define mi_os_stat_decrease (stat ,amount ) _mi_stat_decrease(&_mi_stats_main.stat, amount)
14+ #define mi_os_stat_counter_increase (stat ,inc ) _mi_stat_counter_increase(&_mi_stats_main.stat, inc)
1315
1416/* -----------------------------------------------------------
1517 Initialization.
@@ -171,9 +173,9 @@ static void mi_os_prim_free(void* addr, size_t size, size_t commit_size) {
171173 _mi_warning_message ("unable to free OS memory (error: %d (0x%x), size: 0x%zx bytes, address: %p)\n" , err , err , size , addr );
172174 }
173175 if (commit_size > 0 ) {
174- _mi_stat_decrease ( & os_stats -> committed , commit_size );
176+ mi_os_stat_decrease ( committed , commit_size );
175177 }
176- _mi_stat_decrease ( & os_stats -> reserved , size );
178+ mi_os_stat_decrease ( reserved , size );
177179}
178180
179181void _mi_os_free_ex (void * addr , size_t size , bool still_committed , mi_memid_t memid ) {
@@ -236,11 +238,11 @@ static void* mi_os_prim_alloc_at(void* hint_addr, size_t size, size_t try_alignm
236238
237239
238240
239- mi_stat_counter_increase ( os_stats -> mmap_calls , 1 );
241+ mi_os_stat_counter_increase ( mmap_calls , 1 );
240242 if (p != NULL ) {
241- _mi_stat_increase ( & os_stats -> reserved , size );
243+ mi_os_stat_increase ( reserved , size );
242244 if (commit ) {
243- _mi_stat_increase ( & os_stats -> committed , size );
245+ mi_os_stat_increase ( committed , size );
244246 // seems needed for asan (or `mimalloc-test-api` fails)
245247 #ifdef MI_TRACK_ASAN
246248 if (* is_zero ) { mi_track_mem_defined (p ,size ); }
@@ -357,7 +359,8 @@ void* _mi_os_alloc_aligned(size_t size, size_t alignment, bool commit, bool allo
357359 if (p != NULL ) {
358360 * memid = _mi_memid_create_os (commit , os_is_zero , os_is_large );
359361 memid -> mem .os .base = os_base ;
360- memid -> mem .os .alignment = alignment ;
362+ // memid->mem.os.alignment = alignment;
363+ memid -> mem .os .size += ((uint8_t * )p - (uint8_t * )os_base ); // todo: return from prim_alloc_aligned
361364 }
362365 return p ;
363366}
@@ -425,10 +428,10 @@ static void* mi_os_page_align_area_conservative(void* addr, size_t size, size_t*
425428 return mi_os_page_align_areax (true, addr , size , newsize );
426429}
427430
428- bool _mi_os_commit (void * addr , size_t size , bool * is_zero ) {
431+ bool _mi_os_commit_ex (void * addr , size_t size , bool * is_zero , size_t stat_size ) {
429432 if (is_zero != NULL ) { * is_zero = false; }
430- _mi_stat_increase ( & os_stats -> committed , size ); // use size for precise commit vs. decommit
431- _mi_stat_counter_increase ( & os_stats -> commit_calls , 1 );
433+ mi_os_stat_increase ( committed , stat_size ); // use size for precise commit vs. decommit
434+ mi_os_stat_counter_increase ( commit_calls , 1 );
432435
433436 // page align range
434437 size_t csize ;
@@ -492,8 +495,8 @@ bool _mi_os_reset(void* addr, size_t size) {
492495 size_t csize ;
493496 void * start = mi_os_page_align_area_conservative (addr , size , & csize );
494497 if (csize == 0 ) return true; // || _mi_os_is_huge_reserved(addr)
495- _mi_stat_increase ( & os_stats -> reset , csize );
496- _mi_stat_counter_increase ( & os_stats -> reset_calls , 1 );
498+ mi_os_stat_increase ( reset , csize );
499+ mi_os_stat_counter_increase ( reset_calls , 1 );
497500
498501 #if (MI_DEBUG > 1 ) && !MI_SECURE && !MI_TRACK_ENABLED // && !MI_TSAN
499502 memset (start , 0 , csize ); // pretend it is eagerly reset
@@ -509,17 +512,17 @@ bool _mi_os_reset(void* addr, size_t size) {
509512
510513// either resets or decommits memory, returns true if the memory needs
511514// to be recommitted if it is to be re-used later on.
512- bool _mi_os_purge_ex (void * p , size_t size , bool allow_reset )
515+ bool _mi_os_purge_ex (void * p , size_t size , bool allow_reset , size_t stat_size )
513516{
514517 if (mi_option_get (mi_option_purge_delay ) < 0 ) return false; // is purging allowed?
515- _mi_stat_counter_increase ( & os_stats -> purge_calls , 1 );
516- _mi_stat_increase ( & os_stats -> purged , size );
518+ mi_os_stat_counter_increase ( purge_calls , 1 );
519+ mi_os_stat_increase ( purged , size );
517520
518521 if (mi_option_is_enabled (mi_option_purge_decommits ) && // should decommit?
519522 !_mi_preloading ()) // don't decommit during preloading (unsafe)
520523 {
521524 bool needs_recommit = true;
522- mi_os_decommit_ex (p , size , & needs_recommit );
525+ mi_os_decommit_ex (p , size , & needs_recommit , stat_size );
523526 return needs_recommit ;
524527 }
525528 else {
@@ -533,7 +536,7 @@ bool _mi_os_purge_ex(void* p, size_t size, bool allow_reset)
533536// either resets or decommits memory, returns true if the memory needs
534537// to be recommitted if it is to be re-used later on.
535538bool _mi_os_purge (void * p , size_t size ) {
536- return _mi_os_purge_ex (p , size , true);
539+ return _mi_os_purge_ex (p , size , true, size );
537540}
538541
539542
@@ -648,8 +651,8 @@ void* _mi_os_alloc_huge_os_pages(size_t pages, int numa_node, mi_msecs_t max_mse
648651
649652 // success, record it
650653 page ++ ; // increase before timeout check (see issue #711)
651- _mi_stat_increase ( & os_stats -> committed , MI_HUGE_OS_PAGE_SIZE );
652- _mi_stat_increase ( & os_stats -> reserved , MI_HUGE_OS_PAGE_SIZE );
654+ mi_os_stat_increase ( committed , MI_HUGE_OS_PAGE_SIZE );
655+ mi_os_stat_increase ( reserved , MI_HUGE_OS_PAGE_SIZE );
653656
654657 // check for timeout
655658 if (max_msecs > 0 ) {
0 commit comments