From ee3d396f737362f9113860717c5cac108484a410 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Sun, 29 May 2016 14:45:42 +0000 Subject: [PATCH] inline qMallocAligned(), qReallocAligned() and qFreeAligned() to non-alighgned functions Signed-off-by: Ivailo Monev --- src/core/global/qglobal.cpp | 58 ----------------------------------------- src/core/global/qglobal.h.cmake | 23 ++++++++++------ 2 files changed, 15 insertions(+), 66 deletions(-) diff --git a/src/core/global/qglobal.cpp b/src/core/global/qglobal.cpp index 6a790ce0e..e4ad76d49 100644 --- a/src/core/global/qglobal.cpp +++ b/src/core/global/qglobal.cpp @@ -3090,64 +3090,6 @@ bool QInternal::callFunction(InternalFunction func, void **args) return false; } -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(oldptr)[-1] : 0; - if (alignment <= sizeof(void*)) { - // special, fast case - void **newptr = static_cast(realloc(actualptr, newsize + sizeof(void*))); - if (!newptr) - return 0; - if (newptr == actualptr) { - // realloc succeeded without reallocating - return oldptr; - } - - *newptr = newptr; - return newptr + 1; - } - - // 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. - - void *real = realloc(actualptr, newsize + alignment); - if (!real) - return 0; - - quintptr faked = reinterpret_cast(real) + alignment; - faked &= ~(alignment - 1); - - void **faked_ptr = reinterpret_cast(faked); - - // 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_ptr[-1] = real; - - return faked_ptr; -} - -void qFreeAligned(void *ptr) -{ - if (!ptr) - return; - void **ptr2 = static_cast(ptr); - free(ptr2[-1]); -} - /*! \macro Q_BYTE_ORDER \relates diff --git a/src/core/global/qglobal.h.cmake b/src/core/global/qglobal.h.cmake index 7567617cc..91f740d9e 100644 --- a/src/core/global/qglobal.h.cmake +++ b/src/core/global/qglobal.h.cmake @@ -2250,18 +2250,25 @@ Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE); #endif /* - These functions make it possible to use standard C++ functions with - a similar name from Qt header files (especially template classes). + These functions are for compatibility only */ Q_CORE_EXPORT_INLINE void *qMalloc(size_t size) { return ::malloc(size); } Q_CORE_EXPORT_INLINE void qFree(void *ptr) { ::free(ptr); } Q_CORE_EXPORT_INLINE void *qRealloc(void *ptr, size_t size) { return ::realloc(ptr, size); } -Q_CORE_EXPORT_INLINE void *qMemCopy(void *dest, const void *src, size_t n) { return memcpy(dest, src, n); } -Q_CORE_EXPORT_INLINE void *qMemSet(void *dest, int c, size_t n) { return memset(dest, c, n); } -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_INLINE void *qMemCopy(void *dest, const void *src, size_t n) { return ::memcpy(dest, src, n); } +Q_CORE_EXPORT_INLINE void *qMemSet(void *dest, int c, size_t n) { return ::memset(dest, c, n); } +Q_CORE_EXPORT_INLINE void *qMallocAligned(size_t size, size_t alignment) { Q_UNUSED(alignment); return qMalloc(size); } +Q_CORE_EXPORT_INLINE void qFreeAligned(void *ptr) { qFree(ptr); } +Q_CORE_EXPORT_INLINE void *qReallocAligned(void *ptr, size_t size, size_t oldsize, size_t alignment) +{ + Q_UNUSED(oldsize); + Q_UNUSED(alignment); + return qRealloc(ptr, size); +} +/* + These functions make it possible to use standard C++ functions with + a similar name from Qt header files (especially template classes). +*/ /* Avoid some particularly useless warnings from some stupid compilers. -- 2.11.0