From 47728445a5e7317ed2123a8824c54a012eeee142 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Thu, 22 Mar 2012 21:44:13 +0100 Subject: Remove all calls to, and deprecate qMalloc, qRealloc and qFree. Callers should just call the standard allocation functions directly. Adding an extra function call onto all basic memory management for the sake of making it instrumentable in rare cases isn't really fair to everyone else. What's more, this wasn't completely reliable, as not everything was using them in a number of places. Memory management can still be overridden using tricks like LD_PRELOAD if needed. Their aligned equivilents cannot be deprecated, as no standard equivilents exist, although investigation into posix_memalign(3) is a possibility for the future. Change-Id: Ic5f74b14be33f8bc188fe7236c55e15c36a23fc7 Reviewed-by: Lars Knoll --- src/corelib/global/qglobal.h | 12 +++++------- src/corelib/tools/qbytearray.h | 3 ++- src/corelib/tools/qbytedata_p.h | 8 ++++---- src/corelib/tools/qlist.h | 11 ++++++----- src/corelib/tools/qscopedpointer.h | 4 +++- src/corelib/tools/qvarlengtharray.h | 11 ++++++----- src/corelib/xml/qxmlstream.g | 4 ++-- src/corelib/xml/qxmlstream_p.h | 4 ++-- src/gui/painting/qdatabuffer_p.h | 12 +++++++----- src/testlib/qabstracttestlogger_p.h | 7 ++++--- src/tools/qdoc/qmlparser/qqmljsmemorypool_p.h | 9 +++++---- .../kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp | 4 ++-- tests/auto/testlib/selftests/badxml/tst_badxml.cpp | 4 ++-- 13 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 24e05fc72c..22f65a6aba 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1193,13 +1193,11 @@ inline void qSwap(T &value1, T &value2) #endif } -/* - These functions make it possible to use standard C++ functions with - a similar name from Qt header files (especially template classes). -*/ -Q_CORE_EXPORT void *qMalloc(size_t size) Q_ALLOC_SIZE(1); -Q_CORE_EXPORT void qFree(void *ptr); -Q_CORE_EXPORT void *qRealloc(void *ptr, size_t size) Q_ALLOC_SIZE(2); +#if QT_DEPRECATED_SINCE(5, 0) +Q_CORE_EXPORT QT_DEPRECATED void *qMalloc(size_t size) Q_ALLOC_SIZE(1); +Q_CORE_EXPORT QT_DEPRECATED void qFree(void *ptr); +Q_CORE_EXPORT QT_DEPRECATED void *qRealloc(void *ptr, size_t size) Q_ALLOC_SIZE(2); +#endif Q_CORE_EXPORT void *qMallocAligned(size_t size, size_t alignment) Q_ALLOC_SIZE(1); Q_CORE_EXPORT void *qReallocAligned(void *ptr, size_t size, size_t oldsize, size_t alignment) Q_ALLOC_SIZE(2); Q_CORE_EXPORT void qFreeAligned(void *ptr); diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 1e70e26be3..31fa462ca4 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -45,6 +45,7 @@ #include #include +#include #include #include @@ -402,7 +403,7 @@ public: }; inline QByteArray::QByteArray(): d(const_cast(&shared_null.ba)) { } -inline QByteArray::~QByteArray() { if (!d->ref.deref()) qFree(d); } +inline QByteArray::~QByteArray() { if (!d->ref.deref()) free(d); } inline int QByteArray::size() const { return d->size; } diff --git a/src/corelib/tools/qbytedata_p.h b/src/corelib/tools/qbytedata_p.h index 763b71c8f3..2deff997d2 100644 --- a/src/corelib/tools/qbytedata_p.h +++ b/src/corelib/tools/qbytedata_p.h @@ -102,7 +102,7 @@ public: bufferCompleteSize += bd.size(); } - // return the first QByteData. User of this function has to qFree() its .data! + // return the first QByteData. User of this function has to free() its .data! // preferably use this function to read data. inline QByteArray read() { @@ -110,14 +110,14 @@ public: return buffers.takeFirst(); } - // return everything. User of this function has to qFree() its .data! + // return everything. User of this function has to free() its .data! // avoid to use this, it might malloc and memcpy. inline QByteArray readAll() { return read(byteAmount()); } - // return amount. User of this function has to qFree() its .data! + // return amount. User of this function has to free() its .data! // avoid to use this, it might malloc and memcpy. inline QByteArray read(qint64 amount) { @@ -128,7 +128,7 @@ public: return byteData; } - // return amount bytes. User of this function has to qFree() its .data! + // return amount bytes. User of this function has to free() its .data! // avoid to use this, it will memcpy. qint64 read(char* dst, qint64 amount) { diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index a15b0c9124..9d70e55b1e 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -55,6 +55,7 @@ #include #endif +#include #include #include #include @@ -667,7 +668,7 @@ Q_OUTOFLINE_TEMPLATE typename QList::Node *QList::detach_helper_grow(int i node_copy(reinterpret_cast(p.begin()), reinterpret_cast(p.begin() + i), n); } QT_CATCH(...) { - qFree(d); + free(d); d = x; QT_RETHROW; } @@ -677,7 +678,7 @@ Q_OUTOFLINE_TEMPLATE typename QList::Node *QList::detach_helper_grow(int i } QT_CATCH(...) { node_destruct(reinterpret_cast(p.begin()), reinterpret_cast(p.begin() + i)); - qFree(d); + free(d); d = x; QT_RETHROW; } @@ -696,7 +697,7 @@ Q_OUTOFLINE_TEMPLATE void QList::detach_helper(int alloc) QT_TRY { node_copy(reinterpret_cast(p.begin()), reinterpret_cast(p.end()), n); } QT_CATCH(...) { - qFree(d); + free(d); d = x; QT_RETHROW; } @@ -721,7 +722,7 @@ Q_OUTOFLINE_TEMPLATE QList::QList(const QList &l) struct Cleanup { Cleanup(QListData::Data *d) : d_(d) {} - ~Cleanup() { if (d_) qFree(d_); } + ~Cleanup() { if (d_) free(d_); } QListData::Data *d_; } tryCatch(d); @@ -763,7 +764,7 @@ Q_OUTOFLINE_TEMPLATE void QList::dealloc(QListData::Data *data) { node_destruct(reinterpret_cast(data->array + data->begin), reinterpret_cast(data->array + data->end)); - qFree(data); + free(data); } diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h index 4e1027e86a..c01a623414 100644 --- a/src/corelib/tools/qscopedpointer.h +++ b/src/corelib/tools/qscopedpointer.h @@ -44,6 +44,8 @@ #include +#include + QT_BEGIN_HEADER QT_BEGIN_NAMESPACE @@ -79,7 +81,7 @@ struct QScopedPointerArrayDeleter struct QScopedPointerPodDeleter { - static inline void cleanup(void *pointer) { if (pointer) qFree(pointer); } + static inline void cleanup(void *pointer) { if (pointer) free(pointer); } }; template > diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 4e042f765e..6ce6573843 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -48,6 +48,7 @@ #include #include +#include QT_BEGIN_HEADER @@ -77,7 +78,7 @@ public: i->~T(); } if (ptr != reinterpret_cast(array)) - qFree(ptr); + free(ptr); } inline QVarLengthArray &operator=(const QVarLengthArray &other) { @@ -190,7 +191,7 @@ template Q_INLINE_TEMPLATE QVarLengthArray::QVarLengthArray(int asize) : s(asize) { if (s > Prealloc) { - ptr = reinterpret_cast(qMalloc(s * sizeof(T))); + ptr = reinterpret_cast(malloc(s * sizeof(T))); Q_CHECK_PTR(ptr); a = s; } else { @@ -243,7 +244,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::realloc(int asize, int a const int copySize = qMin(asize, osize); if (aalloc != a) { - ptr = reinterpret_cast(qMalloc(aalloc * sizeof(T))); + ptr = reinterpret_cast(malloc(aalloc * sizeof(T))); Q_CHECK_PTR(ptr); if (ptr) { s = 0; @@ -263,7 +264,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::realloc(int asize, int a while (sClean < osize) (oldPtr+(sClean++))->~T(); if (oldPtr != reinterpret_cast(array) && oldPtr != ptr) - qFree(oldPtr); + free(oldPtr); QT_RETHROW; } } else { @@ -283,7 +284,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::realloc(int asize, int a } if (oldPtr != reinterpret_cast(array) && oldPtr != ptr) - qFree(oldPtr); + free(oldPtr); if (QTypeInfo::isComplex) { // call default constructor for new objects (which can throw) diff --git a/src/corelib/xml/qxmlstream.g b/src/corelib/xml/qxmlstream.g index 87a10003d3..a1d1e84b51 100644 --- a/src/corelib/xml/qxmlstream.g +++ b/src/corelib/xml/qxmlstream.g @@ -152,12 +152,12 @@ template class QXmlStreamSimpleStack { int tos, cap; public: inline QXmlStreamSimpleStack():data(0), tos(-1), cap(0){} - inline ~QXmlStreamSimpleStack(){ if (data) qFree(data); } + inline ~QXmlStreamSimpleStack(){ if (data) free(data); } inline void reserve(int extraCapacity) { if (tos + extraCapacity + 1 > cap) { cap = qMax(tos + extraCapacity + 1, cap << 1 ); - data = reinterpret_cast(qRealloc(data, cap * sizeof(T))); + data = reinterpret_cast(realloc(data, cap * sizeof(T))); Q_CHECK_PTR(data); } } diff --git a/src/corelib/xml/qxmlstream_p.h b/src/corelib/xml/qxmlstream_p.h index 067216d3a1..b8a87fbc3f 100644 --- a/src/corelib/xml/qxmlstream_p.h +++ b/src/corelib/xml/qxmlstream_p.h @@ -646,12 +646,12 @@ template class QXmlStreamSimpleStack { int tos, cap; public: inline QXmlStreamSimpleStack():data(0), tos(-1), cap(0){} - inline ~QXmlStreamSimpleStack(){ if (data) qFree(data); } + inline ~QXmlStreamSimpleStack(){ if (data) free(data); } inline void reserve(int extraCapacity) { if (tos + extraCapacity + 1 > cap) { cap = qMax(tos + extraCapacity + 1, cap << 1 ); - data = reinterpret_cast(qRealloc(data, cap * sizeof(T))); + data = reinterpret_cast(realloc(data, cap * sizeof(T))); Q_CHECK_PTR(data); } } diff --git a/src/gui/painting/qdatabuffer_p.h b/src/gui/painting/qdatabuffer_p.h index e2855c80d0..0eea4c641f 100644 --- a/src/gui/painting/qdatabuffer_p.h +++ b/src/gui/painting/qdatabuffer_p.h @@ -55,6 +55,8 @@ #include "QtCore/qbytearray.h" +#include + QT_BEGIN_NAMESPACE template class QDataBuffer @@ -64,7 +66,7 @@ public: { capacity = res; if (res) - buffer = (Type*) qMalloc(capacity * sizeof(Type)); + buffer = (Type*) malloc(capacity * sizeof(Type)); else buffer = 0; siz = 0; @@ -73,7 +75,7 @@ public: ~QDataBuffer() { if (buffer) - qFree(buffer); + free(buffer); } inline void reset() { siz = 0; } @@ -112,16 +114,16 @@ public: capacity = 1; while (capacity < size) capacity *= 2; - buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type)); + buffer = (Type*) realloc(buffer, capacity * sizeof(Type)); } } inline void shrink(int size) { capacity = size; if (size) - buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type)); + buffer = (Type*) realloc(buffer, capacity * sizeof(Type)); else { - qFree(buffer); + free(buffer); buffer = 0; } } diff --git a/src/testlib/qabstracttestlogger_p.h b/src/testlib/qabstracttestlogger_p.h index fcd2feeeb4..585ef0bc7f 100644 --- a/src/testlib/qabstracttestlogger_p.h +++ b/src/testlib/qabstracttestlogger_p.h @@ -55,6 +55,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -116,7 +117,7 @@ struct QTestCharBuffer inline ~QTestCharBuffer() { if (buf != staticBuf) - qFree(buf); + free(buf); } inline char *data() @@ -144,10 +145,10 @@ struct QTestCharBuffer char *newBuf = 0; if (buf == staticBuf) { // if we point to our internal buffer, we need to malloc first - newBuf = reinterpret_cast(qMalloc(newSize)); + newBuf = reinterpret_cast(malloc(newSize)); } else { // if we already malloc'ed, just realloc - newBuf = reinterpret_cast(qRealloc(buf, newSize)); + newBuf = reinterpret_cast(realloc(buf, newSize)); } // if the allocation went wrong (newBuf == 0), we leave the object as is diff --git a/src/tools/qdoc/qmlparser/qqmljsmemorypool_p.h b/src/tools/qdoc/qmlparser/qqmljsmemorypool_p.h index fd52fd25e4..113166644a 100644 --- a/src/tools/qdoc/qmlparser/qqmljsmemorypool_p.h +++ b/src/tools/qdoc/qmlparser/qqmljsmemorypool_p.h @@ -60,6 +60,7 @@ #include #include +#include QT_QML_BEGIN_NAMESPACE @@ -84,10 +85,10 @@ public: if (_blocks) { for (int i = 0; i < _allocatedBlocks; ++i) { if (char *b = _blocks[i]) - qFree(b); + free(b); } - qFree(_blocks); + free(_blocks); } } @@ -119,7 +120,7 @@ private: else _allocatedBlocks *= 2; - _blocks = (char **) qRealloc(_blocks, sizeof(char *) * _allocatedBlocks); + _blocks = (char **) realloc(_blocks, sizeof(char *) * _allocatedBlocks); for (int index = _blockCount; index < _allocatedBlocks; ++index) _blocks[index] = 0; @@ -128,7 +129,7 @@ private: char *&block = _blocks[_blockCount]; if (! block) - block = (char *) qMalloc(BLOCK_SIZE); + block = (char *) malloc(BLOCK_SIZE); _ptr = block; _end = _ptr + BLOCK_SIZE; diff --git a/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp b/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp index 0649d1e1d8..4b0a64ab54 100644 --- a/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp +++ b/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp @@ -774,7 +774,7 @@ void tst_QMetaObjectBuilder::variantProperty() QCOMPARE(QMetaType::Type(prop.userType()), QMetaType::QVariant); QCOMPARE(QByteArray(prop.typeName()), QByteArray("QVariant")); - qFree(meta); + free(meta); } void tst_QMetaObjectBuilder::notifySignal() @@ -1414,7 +1414,7 @@ TestObject::TestObject(QObject *parent) TestObject::~TestObject() { - qFree(m_metaObject); + free(m_metaObject); } QMetaObject *TestObject::buildMetaObject() diff --git a/tests/auto/testlib/selftests/badxml/tst_badxml.cpp b/tests/auto/testlib/selftests/badxml/tst_badxml.cpp index 2fb9e5ea90..8c75602842 100644 --- a/tests/auto/testlib/selftests/badxml/tst_badxml.cpp +++ b/tests/auto/testlib/selftests/badxml/tst_badxml.cpp @@ -76,7 +76,7 @@ class tst_BadXmlSub : public tst_BadXml public: tst_BadXmlSub() : className("tst_BadXml"), mo(0) {} - ~tst_BadXmlSub() { qFree(mo); } + ~tst_BadXmlSub() { free(mo); } const QMetaObject* metaObject() const; @@ -88,7 +88,7 @@ private: const QMetaObject* tst_BadXmlSub::metaObject() const { if (!mo || (mo->className() != className)) { - qFree(mo); + free(mo); QMetaObjectBuilder builder(&EmptyClass::staticMetaObject); builder.setClassName(className); const_cast(this)->mo = builder.toMetaObject(); -- cgit v1.2.3