[hwasan] simplify the code, NFC
authorKostya Serebryany <kcc@google.com>
Wed, 5 Sep 2018 23:22:38 +0000 (23:22 +0000)
committerKostya Serebryany <kcc@google.com>
Wed, 5 Sep 2018 23:22:38 +0000 (23:22 +0000)
llvm-svn: 341501

compiler-rt/lib/hwasan/hwasan_allocator.cc
compiler-rt/lib/hwasan/hwasan_allocator.h
compiler-rt/lib/hwasan/hwasan_thread.cc
compiler-rt/lib/hwasan/hwasan_thread.h

index 3b0859a..c54117b 100644 (file)
@@ -55,13 +55,8 @@ void HwasanAllocatorInit() {
   allocator.Init(common_flags()->allocator_release_to_os_interval_ms);
 }
 
-AllocatorCache *GetAllocatorCache(HwasanThreadLocalMallocStorage *ms) {
-  CHECK(ms);
-  return &ms->allocator_cache;
-}
-
-void HwasanThreadLocalMallocStorage::CommitBack() {
-  allocator.SwallowCache(GetAllocatorCache(this));
+void AllocatorSwallowThreadLocalCache(AllocatorCache *cache) {
+  allocator.SwallowCache(cache);
 }
 
 static uptr TaggedSize(uptr size) {
@@ -85,8 +80,7 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
   Thread *t = GetCurrentThread();
   void *allocated;
   if (t) {
-    AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
-    allocated = allocator.Allocate(cache, size, alignment);
+    allocated = allocator.Allocate(t->allocator_cache(), size, alignment);
   } else {
     SpinMutexLock l(&fallback_mutex);
     AllocatorCache *cache = &fallback_allocator_cache;
@@ -154,8 +148,7 @@ void HwasanDeallocate(StackTrace *stack, void *tagged_ptr) {
     TagMemoryAligned((uptr)untagged_ptr, TaggedSize(orig_size),
                      t ? t->GenerateRandomTag() : kFallbackFreeTag);
   if (t) {
-    AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
-    allocator.Deallocate(cache, untagged_ptr);
+    allocator.Deallocate(t->allocator_cache(), untagged_ptr);
     if (auto *ha = t->heap_allocations())
       ha->push({reinterpret_cast<uptr>(tagged_ptr), alloc_context_id,
                 free_context_id, static_cast<u32>(orig_size)});
index 4235c4f..8e58085 100644 (file)
@@ -64,14 +64,8 @@ typedef LargeMmapAllocator<HwasanMapUnmapCallback> SecondaryAllocator;
 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
                           SecondaryAllocator> Allocator;
 
-struct HwasanThreadLocalMallocStorage {
-  AllocatorCache allocator_cache;
-  void CommitBack();
 
- private:
-  // These objects are allocated via mmap() and are zero-initialized.
-  HwasanThreadLocalMallocStorage() {}
-};
+void AllocatorSwallowThreadLocalCache(AllocatorCache *cache);
 
 class HwasanChunkView {
  public:
index ce19e16..0a93b43 100644 (file)
@@ -114,7 +114,7 @@ void Thread::ClearShadowForThreadStackAndTLS() {
 void Thread::Destroy() {
   if (flags()->verbose_threads)
     Print("Destroying: ");
-  malloc_storage().CommitBack();
+  AllocatorSwallowThreadLocalCache(allocator_cache());
   ClearShadowForThreadStackAndTLS();
   RemoveFromThreadList(this);
   uptr size = RoundUpTo(sizeof(Thread), GetPageSizeCached());
index f1d79c7..451baf3 100644 (file)
@@ -46,7 +46,7 @@ class Thread {
   void EnterInterceptorScope() { in_interceptor_scope_++; }
   void LeaveInterceptorScope() { in_interceptor_scope_--; }
 
-  HwasanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
+  AllocatorCache *allocator_cache() { return &allocator_cache_; }
   HeapAllocationsRingBuffer *heap_allocations() {
     return heap_allocations_;
   }
@@ -93,7 +93,7 @@ class Thread {
   u32 random_state_;
   u32 random_buffer_;
 
-  HwasanThreadLocalMallocStorage malloc_storage_;
+  AllocatorCache allocator_cache_;
   HeapAllocationsRingBuffer *heap_allocations_;
 
   static void InsertIntoThreadList(Thread *t);