| 1 | // Copyright (C) 2025 Aurélien Brooke <aurelien@bahiasoft.fr> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QALLOC_H |
| 5 | #define QALLOC_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtCore/qtconfigmacros.h> |
| 19 | #include <QtCore/qtcoreexports.h> |
| 20 | #include <QtCore/qnumeric.h> |
| 21 | #include <QtCore/qtypeinfo.h> |
| 22 | |
| 23 | #include <cstddef> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | namespace QtPrivate { |
| 28 | |
| 29 | /** |
| 30 | * \internal |
| 31 | * \return the size that would be allocated for the given request. |
| 32 | * |
| 33 | * Computes the actual allocation size for \a allocSize and \a alignment, |
| 34 | * as determined by the active allocator, without performing the allocation. |
| 35 | * |
| 36 | * In practice, it only returns nonzero when using jemalloc. |
| 37 | */ |
| 38 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION |
| 39 | size_t expectedAllocSize(size_t allocSize, size_t alignment) noexcept; |
| 40 | |
| 41 | /** |
| 42 | * \internal |
| 43 | * \brief Computes the best allocation size for the requested minimum capacity, and updates capacity. |
| 44 | * |
| 45 | * Computes the allocation size starting from \a headerSize and a requested minimum capacity in \a capacity, |
| 46 | * multiplied by the \a elementSize and adjusted by the \a unusedCapacity. |
| 47 | * The final capacity is written back into \a capacity. |
| 48 | * The \a headerSize and \a unusedCapacity values are not included in the final reported capacity. |
| 49 | */ |
| 50 | inline size_t fittedAllocSize(size_t , size_t *capacity, |
| 51 | size_t elementSize, size_t unusedCapacity, size_t alignment) noexcept |
| 52 | { |
| 53 | size_t totalCapacity = 0; // = capacity + unusedCapacity |
| 54 | if (Q_UNLIKELY(qAddOverflow(*capacity, unusedCapacity, &totalCapacity))) |
| 55 | return 0; // or handle error |
| 56 | |
| 57 | size_t payloadSize = 0; // = totalCapacity * elementSize |
| 58 | if (Q_UNLIKELY(qMulOverflow(totalCapacity, elementSize, &payloadSize))) |
| 59 | return 0; |
| 60 | |
| 61 | size_t allocSize = 0; // = headerSize + payloadSize |
| 62 | if (Q_UNLIKELY(qAddOverflow(headerSize, payloadSize, &allocSize))) |
| 63 | return 0; |
| 64 | |
| 65 | if (size_t fittedSize = expectedAllocSize(allocSize, alignment); fittedSize != 0) { |
| 66 | // no need to overflow/underflow check from fittedSize, |
| 67 | // since allocSize <= fittedSize <= SIZE_T_MAX |
| 68 | *capacity = (fittedSize - headerSize) / elementSize - unusedCapacity; |
| 69 | size_t newTotalCapacity = *capacity + unusedCapacity; |
| 70 | size_t newPayloadSize = newTotalCapacity * elementSize; |
| 71 | return headerSize + newPayloadSize; |
| 72 | } |
| 73 | |
| 74 | return allocSize; |
| 75 | } |
| 76 | |
| 77 | #ifdef Q_CC_GNU |
| 78 | __attribute__((malloc)) |
| 79 | #endif |
| 80 | inline void *fittedMalloc(size_t , size_t *capacity, |
| 81 | size_t elementSize, size_t unusedCapacity) noexcept |
| 82 | { |
| 83 | size_t allocSize = fittedAllocSize(headerSize, capacity, |
| 84 | elementSize, unusedCapacity, alignment: alignof(std::max_align_t)); |
| 85 | if (Q_LIKELY(allocSize != 0)) |
| 86 | return malloc(size: allocSize); |
| 87 | else |
| 88 | return nullptr; |
| 89 | } |
| 90 | inline void *fittedMalloc(size_t , qsizetype *capacity, |
| 91 | size_t elementSize, size_t unusedCapacity = 0) noexcept |
| 92 | { |
| 93 | size_t uCapacity = size_t(*capacity); |
| 94 | void *ptr = fittedMalloc(headerSize, capacity: &uCapacity, elementSize, unusedCapacity); |
| 95 | *capacity = qsizetype(uCapacity); |
| 96 | return ptr; |
| 97 | } |
| 98 | |
| 99 | inline void *fittedRealloc(void *ptr, size_t , size_t *capacity, |
| 100 | size_t elementSize, size_t unusedCapacity) noexcept |
| 101 | { |
| 102 | size_t newCapacity = *capacity; |
| 103 | size_t allocSize = fittedAllocSize(headerSize, capacity: &newCapacity, |
| 104 | elementSize, unusedCapacity, alignment: alignof(std::max_align_t)); |
| 105 | if (Q_LIKELY(allocSize != 0)) { |
| 106 | void *newPtr = realloc(ptr: ptr, size: allocSize); |
| 107 | if (newPtr) |
| 108 | *capacity = newCapacity; |
| 109 | return newPtr; |
| 110 | } else { |
| 111 | return nullptr; |
| 112 | } |
| 113 | } |
| 114 | inline void *fittedRealloc(void *ptr, size_t , qsizetype *capacity, |
| 115 | size_t elementSize, size_t unusedCapacity = 0) noexcept |
| 116 | { |
| 117 | size_t uCapacity = size_t(*capacity); |
| 118 | ptr = fittedRealloc(ptr, headerSize, capacity: &uCapacity, elementSize, unusedCapacity); |
| 119 | *capacity = qsizetype(uCapacity); |
| 120 | return ptr; |
| 121 | } |
| 122 | |
| 123 | Q_CORE_EXPORT void sizedFree(void *ptr, size_t allocSize) noexcept; |
| 124 | inline void sizedFree(void *ptr, size_t capacity, size_t elementSize) noexcept |
| 125 | { |
| 126 | sizedFree(ptr, allocSize: capacity * elementSize); |
| 127 | } |
| 128 | |
| 129 | } // namespace QtPrivate |
| 130 | |
| 131 | QT_END_NAMESPACE |
| 132 | |
| 133 | #endif // QALLOC_H |
| 134 | |