From 89a13f837f8bf0941e092ca757f65f65230110aa Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Wed, 3 Aug 2016 00:14:10 +0000 Subject: [sanitizer] refactor TransferBatch to hide the implementation. NFC expected. Second attempt after failed r276383 which was reverted. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@277554 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../sanitizer_allocator_local_cache.h | 16 ++++++------ .../sanitizer_allocator_primary32.h | 12 ++++----- .../sanitizer_allocator_primary64.h | 12 ++++----- .../sanitizer_allocator_size_class_map.h | 29 +++++++++++++++++++--- 4 files changed, 44 insertions(+), 25 deletions(-) (limited to 'lib/sanitizer_common') diff --git a/lib/sanitizer_common/sanitizer_allocator_local_cache.h b/lib/sanitizer_common/sanitizer_allocator_local_cache.h index 5c42707c6..780c45c0c 100644 --- a/lib/sanitizer_common/sanitizer_allocator_local_cache.h +++ b/lib/sanitizer_common/sanitizer_allocator_local_cache.h @@ -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); } }; diff --git a/lib/sanitizer_common/sanitizer_allocator_primary32.h b/lib/sanitizer_common/sanitizer_allocator_primary32.h index 4b4574c1e..7470565e3 100644 --- a/lib/sanitizer_common/sanitizer_allocator_primary32.h +++ b/lib/sanitizer_common/sanitizer_allocator_primary32.h @@ -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); } @@ -232,17 +232,17 @@ class SizeClassAllocator32 { 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->Clear(); } - b->batch[b->count++] = (void*)i; - if (b->count == max_count) { - CHECK_GT(b->count, 0); + b->Add((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); + CHECK_GT(b->Count(), 0); sci->free_list.push_back(b); } } diff --git a/lib/sanitizer_common/sanitizer_allocator_primary64.h b/lib/sanitizer_common/sanitizer_allocator_primary64.h index cc374109e..754962837 100644 --- a/lib/sanitizer_common/sanitizer_allocator_primary64.h +++ b/lib/sanitizer_common/sanitizer_allocator_primary64.h @@ -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; diff --git a/lib/sanitizer_common/sanitizer_allocator_size_class_map.h b/lib/sanitizer_common/sanitizer_allocator_size_class_map.h index 212614b2d..6997a07b5 100644 --- a/lib/sanitizer_common/sanitizer_allocator_size_class_map.h +++ b/lib/sanitizer_common/sanitizer_allocator_size_class_map.h @@ -96,9 +96,33 @@ 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; + CHECK_LE(count_, kMaxNumCached); + for (uptr i = 0; i < count; i++) + batch_[i] = (void*)(region_beg + beg_offset + i * step); + } + void SetFromArray(void *batch[], uptr count) { + count_ = count; + CHECK_LE(count_, kMaxNumCached); + 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_; } + void Clear() { count_ = 0; } + void Add(void *ptr) { + batch_[count_++] = ptr; + CHECK_LE(count_, kMaxNumCached); + } 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 +233,3 @@ class SizeClassMap { typedef SizeClassMap<17, 126, 16> DefaultSizeClassMap; typedef SizeClassMap<17, 62, 14> CompactSizeClassMap; template struct SizeClassAllocatorLocalCache; - -- cgit v1.2.3