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 <lars.knoll@nokia.com>
bb10
Robin Burchell 2012-03-22 21:44:13 +01:00 committed by Qt by Nokia
parent ad921347f7
commit 47728445a5
13 changed files with 50 additions and 43 deletions

View File

@ -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);

View File

@ -45,6 +45,7 @@
#include <QtCore/qrefcount.h>
#include <QtCore/qnamespace.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
@ -402,7 +403,7 @@ public:
};
inline QByteArray::QByteArray(): d(const_cast<Data *>(&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; }

View File

@ -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)
{

View File

@ -55,6 +55,7 @@
#include <initializer_list>
#endif
#include <stdlib.h>
#include <new>
#include <limits.h>
#include <string.h>
@ -667,7 +668,7 @@ Q_OUTOFLINE_TEMPLATE typename QList<T>::Node *QList<T>::detach_helper_grow(int i
node_copy(reinterpret_cast<Node *>(p.begin()),
reinterpret_cast<Node *>(p.begin() + i), n);
} QT_CATCH(...) {
qFree(d);
free(d);
d = x;
QT_RETHROW;
}
@ -677,7 +678,7 @@ Q_OUTOFLINE_TEMPLATE typename QList<T>::Node *QList<T>::detach_helper_grow(int i
} QT_CATCH(...) {
node_destruct(reinterpret_cast<Node *>(p.begin()),
reinterpret_cast<Node *>(p.begin() + i));
qFree(d);
free(d);
d = x;
QT_RETHROW;
}
@ -696,7 +697,7 @@ Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper(int alloc)
QT_TRY {
node_copy(reinterpret_cast<Node *>(p.begin()), reinterpret_cast<Node *>(p.end()), n);
} QT_CATCH(...) {
qFree(d);
free(d);
d = x;
QT_RETHROW;
}
@ -721,7 +722,7 @@ Q_OUTOFLINE_TEMPLATE QList<T>::QList(const QList<T> &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<T>::dealloc(QListData::Data *data)
{
node_destruct(reinterpret_cast<Node *>(data->array + data->begin),
reinterpret_cast<Node *>(data->array + data->end));
qFree(data);
free(data);
}

View File

@ -44,6 +44,8 @@
#include <QtCore/qglobal.h>
#include <stdlib.h>
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 <typename T, typename Cleanup = QScopedPointerDeleter<T> >

View File

@ -48,6 +48,7 @@
#include <new>
#include <string.h>
#include <stdlib.h>
QT_BEGIN_HEADER
@ -77,7 +78,7 @@ public:
i->~T();
}
if (ptr != reinterpret_cast<T *>(array))
qFree(ptr);
free(ptr);
}
inline QVarLengthArray<T, Prealloc> &operator=(const QVarLengthArray<T, Prealloc> &other)
{
@ -190,7 +191,7 @@ template <class T, int Prealloc>
Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(int asize)
: s(asize) {
if (s > Prealloc) {
ptr = reinterpret_cast<T *>(qMalloc(s * sizeof(T)));
ptr = reinterpret_cast<T *>(malloc(s * sizeof(T)));
Q_CHECK_PTR(ptr);
a = s;
} else {
@ -243,7 +244,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int a
const int copySize = qMin(asize, osize);
if (aalloc != a) {
ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
ptr = reinterpret_cast<T *>(malloc(aalloc * sizeof(T)));
Q_CHECK_PTR(ptr);
if (ptr) {
s = 0;
@ -263,7 +264,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int a
while (sClean < osize)
(oldPtr+(sClean++))->~T();
if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
qFree(oldPtr);
free(oldPtr);
QT_RETHROW;
}
} else {
@ -283,7 +284,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int a
}
if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
qFree(oldPtr);
free(oldPtr);
if (QTypeInfo<T>::isComplex) {
// call default constructor for new objects (which can throw)

View File

@ -152,12 +152,12 @@ template <typename T> 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<T *>(qRealloc(data, cap * sizeof(T)));
data = reinterpret_cast<T *>(realloc(data, cap * sizeof(T)));
Q_CHECK_PTR(data);
}
}

View File

@ -646,12 +646,12 @@ template <typename T> 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<T *>(qRealloc(data, cap * sizeof(T)));
data = reinterpret_cast<T *>(realloc(data, cap * sizeof(T)));
Q_CHECK_PTR(data);
}
}

View File

@ -55,6 +55,8 @@
#include "QtCore/qbytearray.h"
#include <stdlib.h>
QT_BEGIN_NAMESPACE
template <typename Type> 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;
}
}

View File

@ -55,6 +55,7 @@
#include <qglobal.h>
#include <stdio.h>
#include <stdlib.h>
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<char *>(qMalloc(newSize));
newBuf = reinterpret_cast<char *>(malloc(newSize));
} else {
// if we already malloc'ed, just realloc
newBuf = reinterpret_cast<char *>(qRealloc(buf, newSize));
newBuf = reinterpret_cast<char *>(realloc(buf, newSize));
}
// if the allocation went wrong (newBuf == 0), we leave the object as is

View File

@ -60,6 +60,7 @@
#include <QtCore/qdebug.h>
#include <cstring>
#include <stdlib.h>
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;

View File

@ -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()

View File

@ -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<tst_BadXmlSub *>(this)->mo = builder.toMetaObject();