Qt 4.8
Functions
qmalloc.cpp File Reference
#include "qplatformdefs.h"
#include <stdlib.h>

Go to the source code of this file.

Functions

void qFree (void *ptr)
 
void qFreeAligned (void *ptr)
 
void * qMalloc (size_t size)
 
void * qMallocAligned (size_t size, size_t alignment)
 
void * qRealloc (void *ptr, size_t size)
 
void * qReallocAligned (void *oldptr, size_t newsize, size_t oldsize, size_t alignment)
 

Function Documentation

◆ qFree()

void qFree ( void *  ptr)

Definition at line 58 of file qmalloc.cpp.

Referenced by QSGGeometry::allocate(), QScopedPointerPodDeleter::cleanup(), QGL2PaintEngineExPrivate::cleanupVectorPath(), QByteArray::clear(), QMapData::continueFreeData(), QDeclarativeOpenMetaObjectType::createProperty(), QList< QPostEvent >::detach_helper(), QList< QPostEvent >::detach_helper_grow(), QGL2PaintEngineExPrivate::fill(), QVectorData::free(), QList< QPostEvent >::free(), QString::free(), QHashData::freeNode(), QScriptEnginePrivate::freeScriptValuePrivate(), QPixmap::fromWinHBITMAP(), QString::insert(), interfaceListing(), interfaceListingWin2k(), interfaceListingWinXP(), QMetaMethod::invoke(), ipv4Netmasks(), QHostInfo::localDomainName(), QMapData::node_delete(), QScriptValuePrivate::operator delete(), QByteArray::operator=(), QAudioDeviceInfoInternal::QAudioDeviceInfoInternal(), readSymLink(), QVarLengthArray< QVariant, 9 >::realloc(), QByteArray::realloc(), QString::replace_helper(), QByteArray::resize(), QDataBuffer< GLfloat >::shrink(), QPixmap::toWinHICON(), QTiffHandler::write(), QTextCodec::ConverterState::~ConverterState(), QDeclarativeJS::MemoryPool::~MemoryPool(), QDeclarativeJS::Parser::~Parser(), QtMultimediaInternal::QAudioBufferList::~QAudioBufferList(), QByteArray::~QByteArray(), QDataBuffer< GLfloat >::~QDataBuffer(), QDeclarativeBoundSignalParameters::~QDeclarativeBoundSignalParameters(), QDeclarativeOpenMetaObjectType::~QDeclarativeOpenMetaObjectType(), QMetaCallEvent::~QMetaCallEvent(), QScriptEnginePrivate::~QScriptEnginePrivate(), QScriptParser::~QScriptParser(), QSGGeometry::~QSGGeometry(), QTestCharBuffer::~QTestCharBuffer(), QVarLengthArray< QVariant, 9 >::~QVarLengthArray(), QXmlStreamReaderPrivate::~QXmlStreamReaderPrivate(), QXmlStreamSimpleStack< QXmlStreamReaderPrivate::Entity *>::~QXmlStreamSimpleStack(), and QScript::SyntaxChecker::~SyntaxChecker().

59 {
60  ::free(ptr);
61 }
const T * ptr(const T &t)

◆ qFreeAligned()

void qFreeAligned ( void *  ptr)

Definition at line 118 of file qmalloc.cpp.

Referenced by QMapData::continueFreeData(), QContiguousCacheData::free(), QVectorData::free(), QHashData::freeNode(), and QMapData::node_delete().

119 {
120  if (!ptr)
121  return;
122  void **ptr2 = static_cast<void **>(ptr);
123  free(ptr2[-1]);
124 }
const T * ptr(const T &t)

◆ qMalloc()

void* qMalloc ( size_t  size)

◆ qMallocAligned()

void* qMallocAligned ( size_t  size,
size_t  alignment 
)

Definition at line 68 of file qmalloc.cpp.

Referenced by QContiguousCacheData::allocate(), QVectorData::allocate(), QHashData::allocateNode(), and QMapData::node_create().

69 {
70  return qReallocAligned(0, size, 0, alignment);
71 }
void * qReallocAligned(void *oldptr, size_t newsize, size_t oldsize, size_t alignment)
Definition: qmalloc.cpp:73

◆ qRealloc()

void* qRealloc ( void *  ptr,
size_t  size 
)

◆ qReallocAligned()

void* qReallocAligned ( void *  oldptr,
size_t  newsize,
size_t  oldsize,
size_t  alignment 
)

Definition at line 73 of file qmalloc.cpp.

Referenced by qMallocAligned(), and QVectorData::reallocate().

74 {
75  // fake an aligned allocation
76  Q_UNUSED(oldsize);
77 
78  void *actualptr = oldptr ? static_cast<void **>(oldptr)[-1] : 0;
79  if (alignment <= sizeof(void*)) {
80  // special, fast case
81  void **newptr = static_cast<void **>(qRealloc(actualptr, newsize + sizeof(void*)));
82  if (!newptr)
83  return 0;
84  if (newptr == actualptr) {
85  // realloc succeeded without reallocating
86  return oldptr;
87  }
88 
89  *newptr = newptr;
90  return newptr + 1;
91  }
92 
93  // qMalloc returns pointers aligned at least at sizeof(size_t) boundaries
94  // but usually more (8- or 16-byte boundaries).
95  // So we overallocate by alignment-sizeof(size_t) bytes, so we're guaranteed to find a
96  // somewhere within the first alignment-sizeof(size_t) that is properly aligned.
97 
98  // However, we need to store the actual pointer, so we need to allocate actually size +
99  // alignment anyway.
100 
101  void *real = qRealloc(actualptr, newsize + alignment);
102  if (!real)
103  return 0;
104 
105  quintptr faked = reinterpret_cast<quintptr>(real) + alignment;
106  faked &= ~(alignment - 1);
107 
108  void **faked_ptr = reinterpret_cast<void **>(faked);
109 
110  // now save the value of the real pointer at faked-sizeof(void*)
111  // by construction, alignment > sizeof(void*) and is a power of 2, so
112  // faked-sizeof(void*) is properly aligned for a pointer
113  faked_ptr[-1] = real;
114 
115  return faked_ptr;
116 }
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
void * qRealloc(void *ptr, size_t size)
Definition: qmalloc.cpp:63
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729