QVarLengthArray: use memory on stack if possible
authorPeter Kümmel <syntheticpp@gmx.net>
Tue, 9 Oct 2012 14:52:37 +0000 (16:52 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 11 Oct 2012 00:47:52 +0000 (02:47 +0200)
After allocating memory on the heap it is ATM not possible to use the
memory on the stack again, QVarLengthArray then uses/resizes only
the memory on the heap. But the memory on stack could be used
if it is big enough.

Change-Id: I566003c25dd1093eb6ba8087a1e5378a11712934
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/tools/qvarlengtharray.h

index 90b10a3..0c5b61d 100644 (file)
@@ -252,12 +252,17 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int a
 
     const int copySize = qMin(asize, osize);
     if (aalloc != a) {
-        T* newPtr = reinterpret_cast<T *>(malloc(aalloc * sizeof(T)));
-        Q_CHECK_PTR(newPtr); // could throw
-        // by design: in case of QT_NO_EXCEPTIONS malloc must not fail or it crashes here
-        ptr = newPtr;
+        if (aalloc > Prealloc) {
+            T* newPtr = reinterpret_cast<T *>(malloc(aalloc * sizeof(T)));
+            Q_CHECK_PTR(newPtr); // could throw
+            // by design: in case of QT_NO_EXCEPTIONS malloc must not fail or it crashes here
+            ptr = newPtr;
+            a = aalloc;
+        } else {
+            ptr = reinterpret_cast<T *>(array);
+            a = Prealloc;
+        }
         s = 0;
-        a = aalloc;
         if (QTypeInfo<T>::isStatic) {
             QT_TRY {
                 // copy all the old elements