Skip to content

Commit

Permalink
QArrayDataOps: refactor appendInitialize
Browse files Browse the repository at this point in the history
Instead of a manual loop, we can use std::uninitialized_value_construct.
QGenericArrayOps was using default construction, which is wrong:
appendInitalize is called from resize() and similar, so it must do value
initialization. Since the implementation between Pod and Generic ops is
now identical, centralize it in QArrayDataPointer.

Change-Id: I46c24886e1130daab79563d7f2f286ce2ba5984a
Reviewed-by: Thiago Macieira <[email protected]>
Reviewed-by: Fabian Kosmale <[email protected]>
  • Loading branch information
dangelog committed Jul 26, 2024
1 parent 0026b17 commit 33155f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
27 changes: 0 additions & 27 deletions src/corelib/tools/qarraydataops.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ struct QPodArrayOps

using QArrayDataPointer<T>::QArrayDataPointer;

void appendInitialize(qsizetype newSize) noexcept
{
Q_ASSERT(this->isMutable());
Q_ASSERT(!this->isShared());
Q_ASSERT(newSize > this->size);
Q_ASSERT(newSize - this->size <= this->freeSpaceAtEnd());

T *where = this->end();
this->size = newSize;
const T *e = this->end();
while (where != e)
*where++ = T();
}

void copyAppend(const T *b, const T *e) noexcept
{
Q_ASSERT(this->isMutable() || b == e);
Expand Down Expand Up @@ -293,19 +279,6 @@ struct QGenericArrayOps
public:
typedef typename QArrayDataPointer<T>::parameter_type parameter_type;

void appendInitialize(qsizetype newSize)
{
Q_ASSERT(this->isMutable());
Q_ASSERT(!this->isShared());
Q_ASSERT(newSize > this->size);
Q_ASSERT(newSize - this->size <= this->freeSpaceAtEnd());

T *const b = this->begin();
do {
new (b + this->size) T;
} while (++this->size != newSize);
}

void copyAppend(const T *b, const T *e)
{
Q_ASSERT(this->isMutable() || b == e);
Expand Down
13 changes: 13 additions & 0 deletions src/corelib/tools/qarraydatapointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,19 @@ struct QArrayDataPointer
return std::move(*this);
}

void appendInitialize(qsizetype newSize)
{
Q_ASSERT(this->isMutable());
Q_ASSERT(!this->isShared());
Q_ASSERT(newSize > this->size);
Q_ASSERT(newSize - this->size <= this->freeSpaceAtEnd());

T *const b = this->begin() + this->size;
T *const e = this->begin() + newSize;
std::uninitialized_value_construct(b, e);
this->size = newSize;
}

// forwards from QArrayData
qsizetype allocatedCapacity() noexcept { return d ? d->allocatedCapacity() : 0; }
qsizetype constAllocatedCapacity() const noexcept { return d ? d->constAllocatedCapacity() : 0; }
Expand Down

0 comments on commit 33155f0

Please sign in to comment.