Commit 3f0b969772cf3056ed54349bfe6837d9de2995ea
- Diff rendering mode:
- inline
- side by side
src/corelib/global/qglobal.h
(3 / 0)
|   | |||
| 2065 | 2065 | Q_CORE_EXPORT void *qMalloc(size_t size); | |
| 2066 | 2066 | Q_CORE_EXPORT void qFree(void *ptr); | |
| 2067 | 2067 | Q_CORE_EXPORT void *qRealloc(void *ptr, size_t size); | |
| 2068 | Q_CORE_EXPORT void *qMallocAligned(size_t size, size_t alignment); | ||
| 2069 | Q_CORE_EXPORT void *qReallocAligned(void *ptr, size_t size, size_t oldsize, size_t alignment); | ||
| 2070 | Q_CORE_EXPORT void qFreeAligned(void *ptr); | ||
| 2068 | 2071 | Q_CORE_EXPORT void *qMemCopy(void *dest, const void *src, size_t n); | |
| 2069 | 2072 | Q_CORE_EXPORT void *qMemSet(void *dest, int c, size_t n); | |
| 2070 | 2073 |
src/corelib/global/qmalloc.cpp
(100 / 0)
|   | |||
| 42 | 42 | #include "qplatformdefs.h" | |
| 43 | 43 | ||
| 44 | 44 | #include <stdlib.h> | |
| 45 | #include <malloc.h> | ||
| 45 | 46 | ||
| 46 | 47 | /* | |
| 47 | 48 | Define the container allocation functions in a separate file, so that our | |
| … | … | ||
| 64 | 64 | void *qRealloc(void *ptr, size_t size) | |
| 65 | 65 | { | |
| 66 | 66 | return ::realloc(ptr, size); | |
| 67 | } | ||
| 68 | |||
| 69 | #if ((defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)) | ||
| 70 | # define HAVE_POSIX_MEMALIGN | ||
| 71 | #endif | ||
| 72 | |||
| 73 | void *qMallocAligned(size_t size, size_t alignment) | ||
| 74 | { | ||
| 75 | #if defined(Q_OS_WIN) | ||
| 76 | return _aligned_malloc(size, alignment); | ||
| 77 | #elif defined(HAVE_POSIX_MEMALIGN) | ||
| 78 | if (alignment <= sizeof(void*)) | ||
| 79 | return qMalloc(size); | ||
| 80 | |||
| 81 | // we have posix_memalign | ||
| 82 | void *ptr = 0; | ||
| 83 | if (posix_memalign(&ptr, alignment, size) == 0) | ||
| 84 | return ptr; | ||
| 85 | return 0; | ||
| 86 | #else | ||
| 87 | return qReallocAligned(0, size, 0, alignment); | ||
| 88 | #endif | ||
| 89 | } | ||
| 90 | |||
| 91 | void *qReallocAligned(void *oldptr, size_t newsize, size_t oldsize, size_t alignment) | ||
| 92 | { | ||
| 93 | #if defined(Q_OS_WIN) | ||
| 94 | Q_UNUSED(oldsize); | ||
| 95 | return _aligned_realloc(oldptr, newsize, alignment); | ||
| 96 | #elif defined(HAVE_POSIX_MEMALIGN) | ||
| 97 | if (alignment <= sizeof(void*)) | ||
| 98 | return qRealloc(oldptr, newsize); | ||
| 99 | |||
| 100 | void *newptr = qMallocAligned(newsize, alignment); | ||
| 101 | if (!newptr) | ||
| 102 | return 0; | ||
| 103 | qMemCopy(newptr, oldptr, qMin(oldsize, newsize)); | ||
| 104 | qFree(oldptr); | ||
| 105 | return newptr; | ||
| 106 | #else | ||
| 107 | // fake an aligned allocation | ||
| 108 | Q_UNUSED(oldsize); | ||
| 109 | |||
| 110 | void *actualptr = oldptr ? static_cast<void **>(oldptr)[-1] : 0; | ||
| 111 | if (alignment <= sizeof(void*)) { | ||
| 112 | // special, fast case | ||
| 113 | void **newptr = static_cast<void **>(qRealloc(actualptr, newsize + sizeof(void*))); | ||
| 114 | if (!newptr) | ||
| 115 | return 0; | ||
| 116 | if (newptr == actualptr) { | ||
| 117 | // realloc succeeded without reallocating | ||
| 118 | return oldptr; | ||
| 119 | } | ||
| 120 | |||
| 121 | *newptr = newptr; | ||
| 122 | return newptr + 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | union { void *ptr; void **pptr; quintptr n; } real, faked; | ||
| 126 | |||
| 127 | // qMalloc returns pointers aligned at least at sizeof(size_t) boundaries | ||
| 128 | // but usually more (8- or 16-byte boundaries). | ||
| 129 | // So we overallocate by alignment-sizeof(size_t) bytes, so we're guaranteed to find a | ||
| 130 | // somewhere within the first alignment-sizeof(size_t) that is properly aligned. | ||
| 131 | |||
| 132 | // However, we need to store the actual pointer, so we need to allocate actually size + | ||
| 133 | // alignment anyway. | ||
| 134 | |||
| 135 | real.ptr = qRealloc(actualptr, newsize + alignment); | ||
| 136 | if (!real.ptr) | ||
| 137 | return 0; | ||
| 138 | |||
| 139 | faked.n = real.n + alignment; | ||
| 140 | faked.n &= ~(alignment - 1); | ||
| 141 | |||
| 142 | // now save the value of the real pointer at faked-sizeof(void*) | ||
| 143 | // by construction, alignment > sizeof(void*) and is a power of 2, so | ||
| 144 | // faked-sizeof(void*) is properly aligned for a pointer | ||
| 145 | faked.pptr[-1] = real.ptr; | ||
| 146 | |||
| 147 | // and save the actual size just before the pointer | ||
| 148 | //reinterpret_cast<size_t *>(faked.pptr - 1)[-1] = size; | ||
| 149 | |||
| 150 | return faked.ptr; | ||
| 151 | #endif | ||
| 152 | } | ||
| 153 | |||
| 154 | void qFreeAligned(void *ptr) | ||
| 155 | { | ||
| 156 | #if defined(Q_OS_WIN) | ||
| 157 | _aligned_free(ptr); | ||
| 158 | #elif defined(HAVE_POSIX_MEMALIGN) | ||
| 159 | ::free(ptr); | ||
| 160 | #else | ||
| 161 | if (!ptr) | ||
| 162 | return; | ||
| 163 | void **ptr2 = static_cast<void **>(ptr); | ||
| 164 | free(ptr2[-1]); | ||
| 165 | #endif | ||
| 67 | 166 | } | |
| 68 | 167 | ||
| 69 | 168 | QT_END_NAMESPACE |

