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