diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-05-03 11:11:36 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-05-15 18:18:00 -0700 |
commit | 1acbcc318ad536ab25ffde3f7c786a3c0706311f (patch) | |
tree | deba80257476562d5b3bd2cad484dc6b28230b97 | |
parent | c6540cb6e41aea6b4152109cade9aa145884a8f6 (diff) | |
download | qtbase-1acbcc318ad536ab25ffde3f7c786a3c0706311f.tar.gz |
QHash: centralize the span allocation
Deduplicates code and will allow me to insert some magic.
Pick-to: 6.5
Task-number: QTBUG-113335
Change-Id: Ieab617d69f3b4b54ab30fffd175bb4a2af610ff8
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
-rw-r--r-- | src/corelib/tools/qhash.h | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 040feb080b..b7d3a85e56 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -531,11 +531,21 @@ struct Data } }; + static auto allocateSpans(size_t numBuckets) + { + struct R { + Span *spans; + size_t nSpans; + }; + + size_t nSpans = numBuckets >> SpanConstants::SpanShift; + return R{ new Span[nSpans], nSpans }; + } + Data(size_t reserve = 0) { numBuckets = GrowthPolicy::bucketsForCapacity(reserve); - size_t nSpans = numBuckets >> SpanConstants::SpanShift; - spans = new Span[nSpans]; + spans = allocateSpans(numBuckets).spans; seed = QHashSeed::globalSeed(); } @@ -557,15 +567,14 @@ struct Data Data(const Data &other) : size(other.size), numBuckets(other.numBuckets), seed(other.seed) { - size_t nSpans = numBuckets >> SpanConstants::SpanShift; - spans = new Span[nSpans]; - reallocationHelper(other, nSpans, false); + auto r = allocateSpans(numBuckets); + spans = r.spans; + reallocationHelper(other, r.nSpans, false); } Data(const Data &other, size_t reserved) : size(other.size), seed(other.seed) { numBuckets = GrowthPolicy::bucketsForCapacity(qMax(size, reserved)); - size_t nSpans = numBuckets >> SpanConstants::SpanShift; - spans = new Span[nSpans]; + spans = allocateSpans(numBuckets).spans; size_t otherNSpans = other.numBuckets >> SpanConstants::SpanShift; reallocationHelper(other, otherNSpans, true); } @@ -623,8 +632,7 @@ struct Data Span *oldSpans = spans; size_t oldBucketCount = numBuckets; - size_t nSpans = newBucketCount >> SpanConstants::SpanShift; - spans = new Span[nSpans]; + spans = allocateSpans(newBucketCount).spans; numBuckets = newBucketCount; size_t oldNSpans = oldBucketCount >> SpanConstants::SpanShift; |