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>
This commit is contained in:
Thiago Macieira 2023-05-03 11:11:36 -07:00
parent c6540cb6e4
commit 1acbcc318a

View File

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