[compiler-rt] r276383 - [sanitizer] refactor TransferBatch to hide the implementation. NFC

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 21 19:21:12 PDT 2016


Author: kcc
Date: Thu Jul 21 21:21:12 2016
New Revision: 276383

URL: http://llvm.org/viewvc/llvm-project?rev=276383&view=rev
Log:
[sanitizer] refactor TransferBatch to hide the implementation. NFC

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_local_cache.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_size_class_map.h

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_local_cache.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_local_cache.h?rev=276383&r1=276382&r2=276383&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_local_cache.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_local_cache.h Thu Jul 21 21:21:12 2016
@@ -109,10 +109,10 @@ struct SizeClassAllocatorLocalCache {
     InitCache();
     PerClass *c = &per_class_[class_id];
     Batch *b = allocator->AllocateBatch(&stats_, this, class_id);
-    CHECK_GT(b->count, 0);
-    for (uptr i = 0; i < b->count; i++)
-      c->batch[i] = b->batch[i];
-    c->count = b->count;
+    CHECK_GT(b->Count(), 0);
+    for (uptr i = 0; i < b->Count(); i++)
+      c->batch[i] = b->Get(i);
+    c->count = b->Count();
     DestroyBatch(class_id, allocator, b);
   }
 
@@ -121,13 +121,11 @@ struct SizeClassAllocatorLocalCache {
     PerClass *c = &per_class_[class_id];
     Batch *b = CreateBatch(class_id, allocator, (Batch*)c->batch[0]);
     uptr cnt = Min(c->max_count / 2, c->count);
-    for (uptr i = 0; i < cnt; i++) {
-      b->batch[i] = c->batch[i];
+    b->SetFromArray(c->batch, cnt);
+    for (uptr i = 0; i < cnt; i++)
       c->batch[i] = c->batch[i + c->max_count / 2];
-    }
-    b->count = cnt;
+
     c->count -= cnt;
-    CHECK_GT(b->count, 0);
     allocator->DeallocateBatch(&stats_, class_id, b);
   }
 };

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h?rev=276383&r1=276382&r2=276383&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary32.h Thu Jul 21 21:21:12 2016
@@ -96,7 +96,7 @@ class SizeClassAllocator32 {
     CHECK_LT(class_id, kNumClasses);
     SizeClassInfo *sci = GetSizeClassInfo(class_id);
     SpinMutexLock l(&sci->mutex);
-    CHECK_GT(b->count, 0);
+    CHECK_GT(b->Count(), 0);
     sci->free_list.push_front(b);
   }
 
@@ -229,20 +229,15 @@ class SizeClassAllocator32 {
     uptr n_chunks = kRegionSize / (size + kMetadataSize);
     uptr max_count = SizeClassMap::MaxCached(class_id);
     Batch *b = nullptr;
-    for (uptr i = reg; i < reg + n_chunks * size; i += size) {
-      if (!b) {
-        b = c->CreateBatch(class_id, this, (Batch*)i);
-        b->count = 0;
-      }
-      b->batch[b->count++] = (void*)i;
-      if (b->count == max_count) {
-        CHECK_GT(b->count, 0);
-        sci->free_list.push_back(b);
-        b = nullptr;
-      }
-    }
-    if (b) {
-      CHECK_GT(b->count, 0);
+
+    uptr beg = reg;
+    uptr remaining_chunks = n_chunks;
+    while (remaining_chunks) {
+      b = c->CreateBatch(class_id, this, (Batch*)beg);
+      uptr count = Min(remaining_chunks, max_count);
+      b->SetFromRange(0, beg, size, count);
+      remaining_chunks -= count;
+      beg += count * size;
       sci->free_list.push_back(b);
     }
   }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h?rev=276383&r1=276382&r2=276383&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_primary64.h Thu Jul 21 21:21:12 2016
@@ -76,15 +76,15 @@ class SizeClassAllocator64 {
     Batch *b = region->free_list.Pop();
     if (!b)
       b = PopulateFreeList(stat, c, class_id, region);
-    region->n_allocated += b->count;
+    region->n_allocated += b->Count();
     return b;
   }
 
   NOINLINE void DeallocateBatch(AllocatorStats *stat, uptr class_id, Batch *b) {
     RegionInfo *region = GetRegionInfo(class_id);
-    CHECK_GT(b->count, 0);
+    CHECK_GT(b->Count(), 0);
     region->free_list.Push(b);
-    region->n_freed += b->count;
+    region->n_freed += b->Count();
   }
 
   bool PointerIsMine(const void *p) {
@@ -310,15 +310,13 @@ class SizeClassAllocator64 {
     }
     for (;;) {
       b = c->CreateBatch(class_id, this, (Batch*)(region_beg + beg_idx));
-      b->count = count;
-      for (uptr i = 0; i < count; i++)
-        b->batch[i] = (void*)(region_beg + beg_idx + i * size);
+      b->SetFromRange(region_beg, beg_idx, size, count);
       region->allocated_user += count * size;
       CHECK_LE(region->allocated_user, region->mapped_user);
       beg_idx += count * size;
       if (beg_idx + count * size + size > region->mapped_user)
         break;
-      CHECK_GT(b->count, 0);
+      CHECK_GT(b->Count(), 0);
       region->free_list.Push(b);
     }
     return b;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_size_class_map.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_size_class_map.h?rev=276383&r1=276382&r2=276383&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_size_class_map.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator_size_class_map.h Thu Jul 21 21:21:12 2016
@@ -96,9 +96,25 @@ class SizeClassMap {
   // For large size classes we use one of the chunks to store the batch.
   // sizeof(TransferBatch) must be a power of 2 for more efficient allocation.
   struct TransferBatch {
+    void SetFromRange(uptr region_beg, uptr beg_offset, uptr step, uptr count) {
+      count_ = count;
+      for (uptr i = 0; i < count; i++)
+        batch_[i] = (void*)(region_beg + beg_offset + i * step);
+    }
+    void SetFromArray(void *batch[], uptr count) {
+      count_ = count;
+      for (uptr i = 0; i < count; i++)
+        batch_[i] = batch[i];
+    }
+    void *Get(uptr idx) {
+      CHECK_LT(idx, count_);
+      return batch_[idx];
+    }
+    uptr Count() const { return count_; }
     TransferBatch *next;
-    uptr count;
-    void *batch[kMaxNumCached];
+   private:
+    uptr count_;
+    void *batch_[kMaxNumCached];
   };
   static const uptr kBatchSize = sizeof(TransferBatch);
   COMPILER_CHECK((kBatchSize & (kBatchSize - 1)) == 0);
@@ -209,4 +225,3 @@ class SizeClassMap {
 typedef SizeClassMap<17, 126, 16> DefaultSizeClassMap;
 typedef SizeClassMap<17, 62,  14> CompactSizeClassMap;
 template<class SizeClassAllocator> struct SizeClassAllocatorLocalCache;
-




More information about the llvm-commits mailing list