| 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 | #include "qalloc.h" |
| 5 | |
| 6 | #include <QtCore/qalgorithms.h> |
| 7 | #include <QtCore/qtpreprocessorsupport.h> |
| 8 | |
| 9 | #include <cstdlib> |
| 10 | |
| 11 | #if QT_CONFIG(jemalloc) |
| 12 | #include <jemalloc/jemalloc.h> |
| 13 | #endif |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | size_t QtPrivate::expectedAllocSize(size_t allocSize, size_t alignment) noexcept |
| 18 | { |
| 19 | Q_ASSERT(qPopulationCount(alignment) == 1); |
| 20 | #if QT_CONFIG(jemalloc) |
| 21 | return ::nallocx(allocSize, MALLOCX_ALIGN(alignment)); |
| 22 | #endif |
| 23 | Q_UNUSED(allocSize); |
| 24 | Q_UNUSED(alignment); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | void QtPrivate::sizedFree(void *ptr, size_t allocSize) noexcept |
| 29 | { |
| 30 | #if QT_CONFIG(jemalloc) |
| 31 | // jemalloc is okay with free(nullptr), as required by the standard, |
| 32 | // but will asssert (in debug) or invoke UB (in release) on sdallocx(nullptr, ...), |
| 33 | // so don't allow Qt to do that. |
| 34 | if (Q_LIKELY(ptr)) { |
| 35 | ::sdallocx(ptr, allocSize, 0); |
| 36 | return; |
| 37 | } |
| 38 | #endif |
| 39 | Q_UNUSED(allocSize); |
| 40 | ::free(ptr: ptr); |
| 41 | } |
| 42 | |
| 43 | QT_END_NAMESPACE |
| 44 | |