|
| 1 | +// Copyright (C) 2025 The Qt Company Ltd. |
| 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 headerSize, 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 headerSize, size_t *capacity, |
| 81 | + size_t elementSize, size_t unusedCapacity) noexcept |
| 82 | +{ |
| 83 | + size_t allocSize = fittedAllocSize(headerSize, capacity, |
| 84 | + elementSize, unusedCapacity, alignof(std::max_align_t)); |
| 85 | + if (Q_LIKELY(allocSize != 0)) |
| 86 | + return malloc(allocSize); |
| 87 | + else |
| 88 | + return nullptr; |
| 89 | +} |
| 90 | +inline void *fittedMalloc(size_t headerSize, qsizetype *capacity, |
| 91 | + size_t elementSize, size_t unusedCapacity = 0) noexcept |
| 92 | +{ |
| 93 | + size_t uCapacity = size_t(*capacity); |
| 94 | + void *ptr = fittedMalloc(headerSize, &uCapacity, elementSize, unusedCapacity); |
| 95 | + *capacity = qsizetype(uCapacity); |
| 96 | + return ptr; |
| 97 | +} |
| 98 | + |
| 99 | +inline void *fittedRealloc(void *ptr, size_t headerSize, size_t *capacity, |
| 100 | + size_t elementSize, size_t unusedCapacity) noexcept |
| 101 | +{ |
| 102 | + size_t allocSize = fittedAllocSize(headerSize, capacity, |
| 103 | + elementSize, unusedCapacity, alignof(std::max_align_t)); |
| 104 | + if (Q_LIKELY(allocSize != 0)) |
| 105 | + return realloc(ptr, allocSize); |
| 106 | + else |
| 107 | + return nullptr; |
| 108 | +} |
| 109 | +inline void *fittedRealloc(void *ptr, size_t headerSize, qsizetype *capacity, |
| 110 | + size_t elementSize, size_t unusedCapacity = 0) noexcept |
| 111 | +{ |
| 112 | + size_t uCapacity = size_t(*capacity); |
| 113 | + ptr = fittedRealloc(ptr, headerSize, &uCapacity, elementSize, unusedCapacity); |
| 114 | + *capacity = qsizetype(uCapacity); |
| 115 | + return ptr; |
| 116 | +} |
| 117 | + |
| 118 | +Q_CORE_EXPORT void sizedFree(void *ptr, size_t allocSize) noexcept; |
| 119 | +inline void sizedFree(void *ptr, size_t capacity, size_t elementSize) noexcept |
| 120 | +{ |
| 121 | + sizedFree(ptr, capacity * elementSize); |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace QtPrivate |
| 125 | + |
| 126 | +QT_END_NAMESPACE |
| 127 | + |
| 128 | +#endif // QALLOC_H |
0 commit comments