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:
parent
ba465f00ed
commit
7bb17d185d
@ -521,7 +521,9 @@ void QList<T>::reserve(qsizetype asize)
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
inline void QList<T>::squeeze()
|
inline void QList<T>::squeeze()
|
||||||
{
|
{
|
||||||
if (d->needsDetach() || size() != capacity()) {
|
if (!d.isMutable())
|
||||||
|
return;
|
||||||
|
if (d->needsDetach() || size() < capacity()) {
|
||||||
// must allocate memory
|
// must allocate memory
|
||||||
DataPointer detached(Data::allocate(size(), d->detachFlags() & ~Data::CapacityReserved));
|
DataPointer detached(Data::allocate(size(), d->detachFlags() & ~Data::CapacityReserved));
|
||||||
if (size()) {
|
if (size()) {
|
||||||
|
@ -1870,6 +1870,8 @@ void tst_QArrayData::literals()
|
|||||||
QCOMPARE(l.capacity(), 0);
|
QCOMPARE(l.capacity(), 0);
|
||||||
for (int i = 0; i < 3; ++i)
|
for (int i = 0; i < 3; ++i)
|
||||||
QCOMPARE(l.at(i).value, i);
|
QCOMPARE(l.at(i).value, i);
|
||||||
|
l.squeeze(); // shouldn't detach
|
||||||
|
QCOMPARE(l.capacity(), 0);
|
||||||
|
|
||||||
(void)l.begin(); // "detach"
|
(void)l.begin(); // "detach"
|
||||||
|
|
||||||
|
@ -3076,6 +3076,9 @@ void tst_QList::fromReadOnlyData() const
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
QVector<char> d = QVector<char>::fromReadOnlyData("ABCDEFGHIJ");
|
QVector<char> d = QVector<char>::fromReadOnlyData("ABCDEFGHIJ");
|
||||||
|
QCOMPARE(d.capacity(), 0);
|
||||||
|
d.squeeze();
|
||||||
|
QCOMPARE(d.capacity(), 0);
|
||||||
QCOMPARE(d.size(), 10u + 1u);
|
QCOMPARE(d.size(), 10u + 1u);
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
QCOMPARE(d.data()[i], char('A' + i));
|
QCOMPARE(d.data()[i], char('A' + i));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user