diff options
| author | Andrei Golubev <andrei.golubev@qt.io> | 2020-11-03 11:26:20 +0100 |
|---|---|---|
| committer | Andrei Golubev <andrei.golubev@qt.io> | 2020-11-06 10:16:31 +0100 |
| commit | 504972f838761f79a170c22225add496e7e5af6a (patch) | |
| tree | 0c6dc4f0bc915a1d8f154cb0c06d64f5dd429e00 /src/corelib/tools/qarraydata.cpp | |
| parent | 42f8afc2dc4c271175f28eb512dac62c765157c4 (diff) | |
Refine {QString, QBA}::reallocData() logic
Fixed misleading naming of "slowReallocatePath". It's no longer "slow",
it's downright dangerous now to reallocate under certain conditions
While at it, added extra assert to QArrayData::reallocateUnaligned()
and cleaned up that function a bit
Change-Id: I05921fb5058eb563997e66107566c87fb4ea5599
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qarraydata.cpp')
| -rw-r--r-- | src/corelib/tools/qarraydata.cpp | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index 2bf3e9bacc2..e73dc358106 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -233,10 +233,13 @@ QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer, { Q_ASSERT(!data || !data->isShared()); - qsizetype headerSize = sizeof(QArrayData); + const qsizetype headerSize = sizeof(QArrayData); qsizetype allocSize = calculateBlockSize(capacity, objectSize, headerSize, option); - qptrdiff offset = dataPointer ? reinterpret_cast<char *>(dataPointer) - reinterpret_cast<char *>(data) : headerSize; + const qptrdiff offset = dataPointer + ? reinterpret_cast<char *>(dataPointer) - reinterpret_cast<char *>(data) + : headerSize; Q_ASSERT(offset > 0); + Q_ASSERT(offset <= allocSize); // == when all free space is at the beginning allocSize = reserveExtraBytes(allocSize); if (Q_UNLIKELY(allocSize < 0)) // handle overflow. cannot reallocate reliably @@ -244,7 +247,7 @@ QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer, QArrayData *header = static_cast<QArrayData *>(::realloc(data, size_t(allocSize))); if (header) { - header->alloc = uint(capacity); + header->alloc = capacity; dataPointer = reinterpret_cast<char *>(header) + offset; } else { dataPointer = nullptr; |
