From 47b99599e8676f42234a39a81b07ee06462a2e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20K=C3=BCmmel?= Date: Tue, 9 Oct 2012 16:52:37 +0200 Subject: QVarLengthArray: use memory on stack if possible 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 --- src/corelib/tools/qvarlengtharray.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/corelib/tools/qvarlengtharray.h') diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 90b10a3ed1..0c5b61d68a 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -252,12 +252,17 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::realloc(int asize, int a const int copySize = qMin(asize, osize); if (aalloc != a) { - T* newPtr = reinterpret_cast(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(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(array); + a = Prealloc; + } s = 0; - a = aalloc; if (QTypeInfo::isStatic) { QT_TRY { // copy all the old elements -- cgit v1.2.3