diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2009-11-02 17:43:10 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2009-11-02 17:43:10 (GMT) |
commit | 5fc1013753babd397efbe5b7bae0dac387fbc2fd (patch) | |
tree | 3a5ccd5b15397879ae4ec26a8708b5ee2d86dc15 /src/corelib/global | |
parent | 20cfe1e790295254370cf472df39813f864de7ea (diff) | |
parent | e4bec39a139363d1ee4cf3fb15a3fe4499215e77 (diff) | |
download | Qt-5fc1013753babd397efbe5b7bae0dac387fbc2fd.zip Qt-5fc1013753babd397efbe5b7bae0dac387fbc2fd.tar.gz Qt-5fc1013753babd397efbe5b7bae0dac387fbc2fd.tar.bz2 |
Merge commit 'e4bec39a139363d1ee4cf3fb15a3fe4499215e77' into 4.6
Diffstat (limited to 'src/corelib/global')
-rw-r--r-- | src/corelib/global/qglobal.h | 3 | ||||
-rw-r--r-- | src/corelib/global/qmalloc.cpp | 58 |
2 files changed, 61 insertions, 0 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 5ee1815..cbb8fda 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -2065,6 +2065,9 @@ Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE); Q_CORE_EXPORT void *qMalloc(size_t size); Q_CORE_EXPORT void qFree(void *ptr); Q_CORE_EXPORT void *qRealloc(void *ptr, size_t size); +Q_CORE_EXPORT void *qMallocAligned(size_t size, size_t alignment); +Q_CORE_EXPORT void *qReallocAligned(void *ptr, size_t size, size_t oldsize, size_t alignment); +Q_CORE_EXPORT void qFreeAligned(void *ptr); Q_CORE_EXPORT void *qMemCopy(void *dest, const void *src, size_t n); Q_CORE_EXPORT void *qMemSet(void *dest, int c, size_t n); diff --git a/src/corelib/global/qmalloc.cpp b/src/corelib/global/qmalloc.cpp index a9f103c..4aab1bd 100644 --- a/src/corelib/global/qmalloc.cpp +++ b/src/corelib/global/qmalloc.cpp @@ -65,4 +65,62 @@ void *qRealloc(void *ptr, size_t size) return ::realloc(ptr, size); } +void *qMallocAligned(size_t size, size_t alignment) +{ + return qReallocAligned(0, size, 0, alignment); +} + +void *qReallocAligned(void *oldptr, size_t newsize, size_t oldsize, size_t alignment) +{ + // fake an aligned allocation + Q_UNUSED(oldsize); + + void *actualptr = oldptr ? static_cast<void **>(oldptr)[-1] : 0; + if (alignment <= sizeof(void*)) { + // special, fast case + void **newptr = static_cast<void **>(qRealloc(actualptr, newsize + sizeof(void*))); + if (!newptr) + return 0; + if (newptr == actualptr) { + // realloc succeeded without reallocating + return oldptr; + } + + *newptr = newptr; + return newptr + 1; + } + + union { void *ptr; void **pptr; quintptr n; } real, faked; + + // qMalloc returns pointers aligned at least at sizeof(size_t) boundaries + // but usually more (8- or 16-byte boundaries). + // So we overallocate by alignment-sizeof(size_t) bytes, so we're guaranteed to find a + // somewhere within the first alignment-sizeof(size_t) that is properly aligned. + + // However, we need to store the actual pointer, so we need to allocate actually size + + // alignment anyway. + + real.ptr = qRealloc(actualptr, newsize + alignment); + if (!real.ptr) + return 0; + + faked.n = real.n + alignment; + faked.n &= ~(alignment - 1); + + // now save the value of the real pointer at faked-sizeof(void*) + // by construction, alignment > sizeof(void*) and is a power of 2, so + // faked-sizeof(void*) is properly aligned for a pointer + faked.pptr[-1] = real.ptr; + + return faked.ptr; +} + +void qFreeAligned(void *ptr) +{ + if (!ptr) + return; + void **ptr2 = static_cast<void **>(ptr); + free(ptr2[-1]); +} + QT_END_NAMESPACE |