From 70a9caf4de9ddac52783a276a6cdd196eb5fef44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20K=C3=BCmmel?= Date: Wed, 10 Oct 2012 16:35:44 +0200 Subject: QVarLengthArray: add squeeze function Add function to move back data to the stack. Change-Id: Ic78a368459bce68629e29602e4eeae2e1afe398b Reviewed-by: Thiago Macieira --- src/corelib/tools/qvarlengtharray.h | 5 +++++ src/corelib/tools/qvarlengtharray.qdoc | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 5e57de5767..095e6e929e 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -103,6 +103,7 @@ public: inline bool isEmpty() const { return (s == 0); } inline void resize(int size); inline void clear() { resize(0); } + inline void squeeze(); inline int capacity() const { return a; } inline void reserve(int size); @@ -243,6 +244,10 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::append(const T *abuf, in } } +template +Q_INLINE_TEMPLATE void QVarLengthArray::squeeze() +{ realloc(s, s); } + template Q_OUTOFLINE_TEMPLATE void QVarLengthArray::realloc(int asize, int aalloc) { diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index 5e78946892..93aa5e993e 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -210,7 +210,7 @@ initialized. For other types, the elements are initialized with a \l{default-constructed value}. - \sa size() + \sa size(), squeeze() */ /*! \fn int QVarLengthArray::capacity() const @@ -223,7 +223,7 @@ need to call this function. If you want to know how many items are in the array, call size(). - \sa reserve() + \sa reserve(), squeeze() */ /*! \fn void QVarLengthArray::reserve(int size) @@ -240,7 +240,21 @@ rarely ever need to call this function. If you want to change the size of the array, call resize(). - \sa capacity() + \sa capacity(), squeeze() +*/ + +/*! \fn void QVarLengthArray::squeeze() + \since 5.1 + + Releases any memory not required to store the items. + If the container can fit its storage on the stack allocation, + it will free the heap allocation and copy the elements back to the stack. + + The sole purpose of this function is to provide a means of fine + tuning QVarLengthArray's memory usage. In general, you will rarely ever + need to call this function. + + \sa reserve(), capacity(), resize() */ /*! \fn T &QVarLengthArray::operator[](int i) -- cgit v1.2.3