Skip to content

Commit 81cc968

Browse files
committed
convert buckets vector to C array
1 parent cb7f676 commit 81cc968

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/pool/pool_disjoint.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class DisjointPool::AllocImpl {
117117
umf_memory_provider_handle_t MemHandle;
118118

119119
// Store as unique_ptrs since Bucket is not Movable(because of std::mutex)
120-
std::vector<bucket_t *> Buckets;
120+
bucket_t **buckets;
121+
size_t buckets_num;
121122

122123
// Configuration for this instance
123124
umf_disjoint_pool_params_t params;
@@ -141,22 +142,36 @@ class DisjointPool::AllocImpl {
141142

142143
// Generate buckets sized such as: 64, 96, 128, 192, ..., CutOff.
143144
// Powers of 2 and the value halfway between the powers of 2.
144-
auto Size1 = this->params.MinBucketSize;
145+
size_t Size1 = this->params.MinBucketSize;
146+
145147
// MinBucketSize cannot be larger than CutOff.
146148
Size1 = std::min(Size1, CutOff);
149+
147150
// Buckets sized smaller than the bucket default size- 8 aren't needed.
148151
Size1 = std::max(Size1, UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE);
152+
149153
// Calculate the exponent for MinBucketSize used for finding buckets.
150154
MinBucketSizeExp = (size_t)log2Utils(Size1);
151155
DefaultSharedLimits = shared_limits_create(SIZE_MAX);
152156

157+
// count number of buckets, start from 1
158+
buckets_num = 1;
153159
auto Size2 = Size1 + Size1 / 2;
160+
size_t ts2 = Size2, ts1 = Size1;
154161
for (; Size2 < CutOff; Size1 *= 2, Size2 *= 2) {
155-
// TODO copy allocimpl
156-
Buckets.push_back(create_bucket(Size1, this, this->getLimits()));
157-
Buckets.push_back(create_bucket(Size2, this, this->getLimits()));
162+
buckets_num += 2;
163+
}
164+
buckets =
165+
(bucket_t **)umf_ba_global_alloc(sizeof(bucket_t *) * buckets_num);
166+
167+
int i = 0;
168+
Size1 = ts1;
169+
Size2 = ts2;
170+
for (; Size2 < CutOff; Size1 *= 2, Size2 *= 2, i += 2) {
171+
buckets[i] = create_bucket(Size1, this, this->getLimits());
172+
buckets[i + 1] = create_bucket(Size2, this, this->getLimits());
158173
}
159-
Buckets.push_back(create_bucket(CutOff, this, this->getLimits()));
174+
buckets[i] = create_bucket(CutOff, this, this->getLimits());
160175

161176
auto ret = umfMemoryProviderGetMinPageSize(hProvider, nullptr,
162177
&ProviderMinPageSize);
@@ -169,8 +184,8 @@ class DisjointPool::AllocImpl {
169184
// TODO
170185
// destroy DefaultSharedLimits
171186

172-
for (auto it = Buckets.begin(); it != Buckets.end(); it++) {
173-
destroy_bucket(*it);
187+
for (size_t i = 0; i < buckets_num; i++) {
188+
destroy_bucket(buckets[i]);
174189
}
175190

176191
VALGRIND_DO_DESTROY_MEMPOOL(this);
@@ -399,17 +414,17 @@ std::size_t DisjointPool::AllocImpl::sizeToIdx(size_t Size) {
399414

400415
bucket_t *DisjointPool::AllocImpl::findBucket(size_t Size) {
401416
auto calculatedIdx = sizeToIdx(Size);
402-
bucket_t *bucket = Buckets[calculatedIdx];
417+
bucket_t *bucket = buckets[calculatedIdx];
403418
assert(bucket_get_size(bucket) >= Size);
404419
(void)bucket;
405420

406421
if (calculatedIdx > 0) {
407-
bucket_t *bucket_prev = Buckets[calculatedIdx - 1];
422+
bucket_t *bucket_prev = buckets[calculatedIdx - 1];
408423
assert(bucket_get_size(bucket_prev) < Size);
409424
(void)bucket_prev;
410425
}
411426

412-
return Buckets[calculatedIdx];
427+
return buckets[calculatedIdx];
413428
}
414429

415430
umf_result_t DisjointPool::AllocImpl::deallocate(void *Ptr, bool &ToPool) {
@@ -473,15 +488,15 @@ void DisjointPool::AllocImpl::printStats(bool &TitlePrinted,
473488

474489
HighBucketSize = 0;
475490
HighPeakSlabsInUse = 0;
476-
for (auto &B : Buckets) {
491+
for (size_t i = 0; i < buckets_num; i++) {
477492
// TODO
478493
//(*B).printStats(TitlePrinted, MTName);
479-
bucket_t *bucket = B;
494+
bucket_t *bucket = buckets[i];
480495
HighPeakSlabsInUse =
481-
std::max(bucket->maxSlabsInUse, HighPeakSlabsInUse);
482-
if ((*B).allocCount) {
496+
utils_max(bucket->maxSlabsInUse, HighPeakSlabsInUse);
497+
if (bucket->allocCount) {
483498
HighBucketSize =
484-
std::max(bucket_slab_alloc_size(bucket), HighBucketSize);
499+
utils_max(bucket_slab_alloc_size(bucket), HighBucketSize);
485500
}
486501
}
487502
}

0 commit comments

Comments
 (0)