path: root/src/corelib/tools/qarraydata.cpp
diff options
authorJoão Abecasis <>2012-02-16 23:25:33 +0100
committerQt by Nokia <>2012-02-29 14:31:15 +0100
commit7919c0529ed5bdafd9a7dab82780ad1b1bf33178 (patch)
tree3e7b80a204e982b095d239a26312ad486d58517f /src/corelib/tools/qarraydata.cpp
parentf94709366229b479a17457021cff0d664cfe8de7 (diff)
Add AllocationOption::Grow
This is meant to reduce the number of allocations on growing containers. It serves the same purpose as the existing qAllocMore which is currently used by container classes. While only a container knows when it is growing, it doesn't need to care how that information is used. qAllocMore is currently treated as a black-box and its result is (basically) forwarded blindly to an allocate function. In that respect, container code using qAllocMore acts as an intermediary. By merging that functionality in the allocate function itself we offer the same benefits without the intermediaries, allowing for simpler code and centralized decisions on memory allocation. Once all users of qAllocMore get ported to QArrayData and QArrayData::allocate, qAllocMore can be moved or more closely integrated into qarraydata.cpp and qtools_p.h can be dropped. Change-Id: I4c09bf7df274b45c399082fc7113a18e4641c5f0 Reviewed-by: Bradley T. Hughes <>
Diffstat (limited to 'src/corelib/tools/qarraydata.cpp')
1 files changed, 9 insertions, 2 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp
index 5ed3ce015f..8498d0e4d5 100644
--- a/src/corelib/tools/qarraydata.cpp
+++ b/src/corelib/tools/qarraydata.cpp
@@ -40,6 +40,7 @@
#include <QtCore/qarraydata.h>
+#include <QtCore/private/qtools_p.h>
#include <stdlib.h>
@@ -63,14 +64,20 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment,
? const_cast<QArrayData *>(&qt_array_empty)
: const_cast<QArrayData *>(&qt_array_unsharable_empty);
- size_t allocSize = sizeof(QArrayData) + objectSize * capacity;
+ size_t headerSize = sizeof(QArrayData);
// Allocate extra (alignment - Q_ALIGNOF(QArrayData)) padding bytes so we
// can properly align the data array. This assumes malloc is able to
// provide appropriate alignment for the header -- as it should!
// Padding is skipped when allocating a header for RawData.
if (!(options & RawData))
- allocSize += (alignment - Q_ALIGNOF(QArrayData));
+ headerSize += (alignment - Q_ALIGNOF(QArrayData));
+ // Allocate additional space if array is growing
+ if (options & Grow)
+ capacity = qAllocMore(objectSize * capacity, headerSize) / objectSize;
+ size_t allocSize = headerSize + objectSize * capacity;
QArrayData *header = static_cast<QArrayData *>(::malloc(allocSize));
if (header) {