[compiler-rt] 096348b - [sanitizer] Lazy initialize AllocatorGlobalStats

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed May 24 16:10:58 PDT 2023


Author: Vitaly Buka
Date: 2023-05-24T16:10:45-07:00
New Revision: 096348b179872b75a9fd4129783777897617d6ec

URL: https://github.com/llvm/llvm-project/commit/096348b179872b75a9fd4129783777897617d6ec
DIFF: https://github.com/llvm/llvm-project/commit/096348b179872b75a9fd4129783777897617d6ec.diff

LOG: [sanitizer] Lazy initialize AllocatorGlobalStats

This allow to have no InitLinkerInitialized and let AllocatorGlobalStats
accept registration before allocator initialization.

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h
    compiler-rt/lib/sanitizer_common/sanitizer_allocator_stats.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h
index b76d36dcf5a4e..49940d9b5d505 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h
@@ -29,9 +29,9 @@ class CombinedAllocator {
                          LargeMmapAllocatorPtrArray,
                          typename PrimaryAllocator::AddressSpaceView>;
 
-  void InitLinkerInitialized(s32 release_to_os_interval_ms) {
-    stats_.InitLinkerInitialized();
-    primary_.Init(release_to_os_interval_ms);
+  void InitLinkerInitialized(s32 release_to_os_interval_ms,
+                             uptr heap_start = 0) {
+    primary_.Init(release_to_os_interval_ms, heap_start);
     secondary_.InitLinkerInitialized();
   }
 

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_stats.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_stats.h
index 6f14e3863c31c..9fb447763becc 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_stats.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_stats.h
@@ -58,17 +58,13 @@ class AllocatorStats {
 // Global stats, used for aggregation and querying.
 class AllocatorGlobalStats : public AllocatorStats {
  public:
-  void InitLinkerInitialized() {
-    next_ = this;
-    prev_ = this;
-  }
   void Init() {
     internal_memset(this, 0, sizeof(*this));
-    InitLinkerInitialized();
   }
 
   void Register(AllocatorStats *s) {
     SpinMutexLock l(&mu_);
+    LazyInit();
     s->next_ = next_;
     s->prev_ = this;
     next_->prev_ = s;
@@ -87,7 +83,7 @@ class AllocatorGlobalStats : public AllocatorStats {
     internal_memset(s, 0, AllocatorStatCount * sizeof(uptr));
     SpinMutexLock l(&mu_);
     const AllocatorStats *stats = this;
-    for (;;) {
+    for (; stats;) {
       for (int i = 0; i < AllocatorStatCount; i++)
         s[i] += stats->Get(AllocatorStat(i));
       stats = stats->next_;
@@ -100,6 +96,13 @@ class AllocatorGlobalStats : public AllocatorStats {
   }
 
  private:
+  void LazyInit() {
+    if (!next_) {
+      next_ = this;
+      prev_ = this;
+    }
+  }
+
   mutable StaticSpinMutex mu_;
 };
 


        


More information about the llvm-commits mailing list