@@ -79,6 +79,8 @@ namespace fs = std::filesystem;
79
79
#include " git.h"
80
80
#include " toml.hpp"
81
81
82
+ #include < mimalloc-override.h>
83
+
82
84
using fileExtent = std::pair<std::string, roofer::TBox<double >>;
83
85
84
86
struct InputPointcloud {
@@ -421,6 +423,7 @@ std::vector<roofer::TBox<double>> create_tiles(roofer::TBox<double>& roi,
421
423
return tiles;
422
424
}
423
425
426
+ #ifdef RF_ENABLE_HEAP_TRACING
424
427
// Overrides for heap allocation counting
425
428
// Ref.: https://www.youtube.com/watch?v=sLlGEUO_EGE
426
429
namespace {
@@ -433,15 +436,133 @@ namespace {
433
436
};
434
437
HeapAllocationCounter heap_allocation_counter;
435
438
} // namespace
439
+ #endif
440
+
441
+ /*
442
+ * Code snippet below is taken from
443
+ * https://github.com/microsoft/mimalloc/blob/dev/include/mimalloc-new-delete.h
444
+ * and modified to work with the roofer trace feature for heap memory usage.
445
+ */
446
+
447
+ #if defined(_MSC_VER) && defined(_Ret_notnull_) && \
448
+ defined (_Post_writable_byte_size_)
449
+ // stay consistent with VCRT definitions
450
+ #define mi_decl_new (n ) \
451
+ mi_decl_nodiscard mi_decl_restrict _Ret_notnull_ _Post_writable_byte_size_ (n)
452
+ #define mi_decl_new_nothrow (n ) \
453
+ mi_decl_nodiscard mi_decl_restrict _Ret_maybenull_ _Success_ (return != NULL ) \
454
+ _Post_writable_byte_size_(n)
455
+ #else
456
+ #define mi_decl_new (n ) mi_decl_nodiscard mi_decl_restrict
457
+ #define mi_decl_new_nothrow (n ) mi_decl_nodiscard mi_decl_restrict
458
+ #endif
459
+
460
+ void operator delete (void * p) noexcept { mi_free (p); };
461
+ void operator delete[] (void * p) noexcept { mi_free (p); };
462
+
463
+ void operator delete (void * p, const std::nothrow_t &) noexcept { mi_free (p); }
464
+ void operator delete[] (void * p, const std::nothrow_t &) noexcept { mi_free (p); }
465
+
466
+ mi_decl_new (n) void * operator new (std::size_t n) noexcept (false ) {
467
+ #ifdef RF_ENABLE_HEAP_TRACING
468
+ heap_allocation_counter.total_allocated += n;
469
+ #endif
470
+ return mi_new (n);
471
+ }
472
+ mi_decl_new (n) void * operator new [](std::size_t n) noexcept (false ) {
473
+ #ifdef RF_ENABLE_HEAP_TRACING
474
+ heap_allocation_counter.total_allocated += n;
475
+ #endif
476
+ return mi_new (n);
477
+ }
478
+
479
+ mi_decl_new_nothrow (n) void * operator new (std::size_t n,
480
+ const std::nothrow_t & tag) noexcept {
481
+ (void )(tag);
482
+ #ifdef RF_ENABLE_HEAP_TRACING
483
+ heap_allocation_counter.total_allocated += n;
484
+ #endif
485
+ return mi_new_nothrow (n);
486
+ }
487
+ mi_decl_new_nothrow (n) void * operator new [](
488
+ std::size_t n, const std::nothrow_t & tag) noexcept {
489
+ (void )(tag);
490
+ #ifdef RF_ENABLE_HEAP_TRACING
491
+ heap_allocation_counter.total_allocated += n;
492
+ #endif
493
+ return mi_new_nothrow (n);
494
+ }
495
+
496
+ #if (__cplusplus >= 201402L || _MSC_VER >= 1916)
497
+ void operator delete (void * p, std::size_t n) noexcept {
498
+ #ifdef RF_ENABLE_HEAP_TRACING
499
+ heap_allocation_counter.total_freed += n;
500
+ #endif
501
+ mi_free_size (p, n);
502
+ };
503
+ void operator delete[] (void * p, std::size_t n) noexcept {
504
+ #ifdef RF_ENABLE_HEAP_TRACING
505
+ heap_allocation_counter.total_freed += n;
506
+ #endif
507
+ mi_free_size (p, n);
508
+ };
509
+ #endif
436
510
437
- void * operator new (size_t size) {
438
- heap_allocation_counter.total_allocated += size;
439
- return malloc (size);
511
+ #if (__cplusplus > 201402L || defined(__cpp_aligned_new))
512
+ void operator delete (void * p, std::align_val_t al) noexcept {
513
+ mi_free_aligned (p, static_cast <size_t >(al));
514
+ }
515
+ void operator delete[] (void * p, std::align_val_t al) noexcept {
516
+ mi_free_aligned (p, static_cast <size_t >(al));
440
517
}
441
- void operator delete (void * memory, size_t size) noexcept {
442
- heap_allocation_counter.total_freed += size;
443
- free (memory);
518
+ void operator delete (void * p, std::size_t n, std::align_val_t al) noexcept {
519
+ #ifdef RF_ENABLE_HEAP_TRACING
520
+ heap_allocation_counter.total_freed += n;
521
+ #endif
522
+ mi_free_size_aligned (p, n, static_cast <size_t >(al));
444
523
};
524
+ void operator delete[] (void * p, std::size_t n, std::align_val_t al) noexcept {
525
+ #ifdef RF_ENABLE_HEAP_TRACING
526
+ heap_allocation_counter.total_freed += n;
527
+ #endif
528
+ mi_free_size_aligned (p, n, static_cast <size_t >(al));
529
+ };
530
+ void operator delete (void * p, std::align_val_t al,
531
+ const std::nothrow_t &) noexcept {
532
+ mi_free_aligned (p, static_cast <size_t >(al));
533
+ }
534
+ void operator delete[] (void * p, std::align_val_t al,
535
+ const std::nothrow_t &) noexcept {
536
+ mi_free_aligned (p, static_cast <size_t >(al));
537
+ }
538
+
539
+ void * operator new (std::size_t n, std::align_val_t al) noexcept (false ) {
540
+ #ifdef RF_ENABLE_HEAP_TRACING
541
+ heap_allocation_counter.total_allocated += n;
542
+ #endif
543
+ return mi_new_aligned (n, static_cast <size_t >(al));
544
+ }
545
+ void * operator new [](std::size_t n, std::align_val_t al) noexcept (false ) {
546
+ #ifdef RF_ENABLE_HEAP_TRACING
547
+ heap_allocation_counter.total_allocated += n;
548
+ #endif
549
+ return mi_new_aligned (n, static_cast <size_t >(al));
550
+ }
551
+ void * operator new (std::size_t n, std::align_val_t al,
552
+ const std::nothrow_t &) noexcept {
553
+ #ifdef RF_ENABLE_HEAP_TRACING
554
+ heap_allocation_counter.total_allocated += n;
555
+ #endif
556
+ return mi_new_aligned_nothrow (n, static_cast <size_t >(al));
557
+ }
558
+ void * operator new [](std::size_t n, std::align_val_t al,
559
+ const std::nothrow_t &) noexcept {
560
+ #ifdef RF_ENABLE_HEAP_TRACING
561
+ heap_allocation_counter.total_allocated += n;
562
+ #endif
563
+ return mi_new_aligned_nothrow (n, static_cast <size_t >(al));
564
+ }
565
+ #endif
445
566
446
567
/*
447
568
* Author: David Robert Nadeau
@@ -688,7 +809,9 @@ int main(int argc, const char* argv[]) {
688
809
tracer_thread.emplace ([&] {
689
810
while (crop_running.load () || reconstruction_running.load () ||
690
811
serialization_running.load ()) {
812
+ #ifdef RF_ENABLE_HEAP_TRACING
691
813
logger.trace (" heap" , heap_allocation_counter.current_usage ());
814
+ #endif
692
815
logger.trace (" rss" , GetCurrentRSS ());
693
816
logger.trace (" crop" , cropped_buildings_cnt);
694
817
logger.trace (" reconstruct" , reconstructed_buildings_cnt);
@@ -700,9 +823,11 @@ int main(int argc, const char* argv[]) {
700
823
reconstructor_pool.get_tasks_queued ());
701
824
std::this_thread::sleep_for (trace_interval);
702
825
}
703
- // We log once more after all threads have finished, to measure the finaly
704
- // memory use
826
+ // We log once more after all threads have finished, to measure the finaly
827
+ // memory use
828
+ #ifdef RF_ENABLE_HEAP_TRACING
705
829
logger.trace (" heap" , heap_allocation_counter.current_usage ());
830
+ #endif
706
831
logger.trace (" rss" , GetCurrentRSS ());
707
832
logger.trace (" crop" , cropped_buildings_cnt);
708
833
logger.trace (" reconstruct" , reconstructed_buildings_cnt);
0 commit comments