QByteArray: Don't realloc when resizing to a smaller size

The same change was done for QVector and QString for 5.6 while
QByteArray was not done/forgotten. For the sake of consistency
we should converge on one type of behavior.

The change mirrors the one for QString:
50ab7c16d4

[ChangeLog][QtCore][QByteArray] resize() will no longer shrink the
capacity. That means resize(0) now reliably preserves capacity().

Change-Id: I4fcfa92f75fa472af0eaab567e1cdd62e6d336b0
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mårten Nordheim 2019-08-28 12:46:39 +02:00 committed by Marc Mutz
parent e75c691d07
commit 6b884d2aa1

View File

@ -1775,12 +1775,7 @@ void QByteArray::resize(int size)
return; return;
} }
if (size == 0 && !d->capacityReserved) { if (d->size == 0 && d->ref.isStatic()) {
Data *x = Data::allocate(0);
if (!d->ref.deref())
Data::deallocate(d);
d = x;
} else if (d->size == 0 && d->ref.isStatic()) {
// //
// Optimize the idiom: // Optimize the idiom:
// QByteArray a; // QByteArray a;
@ -1795,9 +1790,7 @@ void QByteArray::resize(int size)
x->data()[size] = '\0'; x->data()[size] = '\0';
d = x; d = x;
} else { } else {
if (d->ref.isShared() || uint(size) + 1u > d->alloc if (d->ref.isShared() || uint(size) + 1u > d->alloc)
|| (!d->capacityReserved && size < d->size
&& uint(size) + 1u < uint(d->alloc >> 1)))
reallocData(uint(size) + 1u, d->detachFlags() | Data::Grow); reallocData(uint(size) + 1u, d->detachFlags() | Data::Grow);
if (d->alloc) { if (d->alloc) {
d->size = size; d->size = size;