[sanitizer] refactor TransferBatch to hide the implementation. NFC

llvm-svn: 276383
This commit is contained in:
Kostya Serebryany 2016-07-22 02:21:12 +00:00
parent cadadaa1b3
commit 5bc01c108d
4 changed files with 40 additions and 34 deletions

View File

@ -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);
}
};

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;