QList: don't detach on squeeze when holding raw data

To match QString and QByteArray behavior

Change-Id: Ifce4a5dee6fc9077e855a24499f11f911e359cf5
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mårten Nordheim 2020-09-25 15:39:01 +02:00
parent ba465f00ed
commit 7bb17d185d
3 changed files with 8 additions and 1 deletions

View File

@ -521,7 +521,9 @@ void QList<T>::reserve(qsizetype asize)
template <typename T>
inline void QList<T>::squeeze()
{
if (d->needsDetach() || size() != capacity()) {
if (!d.isMutable())
return;
if (d->needsDetach() || size() < capacity()) {
// must allocate memory
DataPointer detached(Data::allocate(size(), d->detachFlags() & ~Data::CapacityReserved));
if (size()) {

View File

@ -1870,6 +1870,8 @@ void tst_QArrayData::literals()
QCOMPARE(l.capacity(), 0);
for (int i = 0; i < 3; ++i)
QCOMPARE(l.at(i).value, i);
l.squeeze(); // shouldn't detach
QCOMPARE(l.capacity(), 0);
(void)l.begin(); // "detach"

View File

@ -3076,6 +3076,9 @@ void tst_QList::fromReadOnlyData() const
{
{
QVector<char> d = QVector<char>::fromReadOnlyData("ABCDEFGHIJ");
QCOMPARE(d.capacity(), 0);
d.squeeze();
QCOMPARE(d.capacity(), 0);
QCOMPARE(d.size(), 10u + 1u);
for (int i = 0; i < 10; ++i)
QCOMPARE(d.data()[i], char('A' + i));