Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qalloc.h
Go to the documentation of this file.
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#include <cstdlib>
25
26QT_BEGIN_NAMESPACE
27
28namespace QtPrivate {
29
30/**
31 * \internal
32 * \return the size that would be allocated for the given request.
33 *
34 * Computes the actual allocation size for \a allocSize and \a alignment,
35 * as determined by the active allocator, without performing the allocation.
36 *
37 * In practice, it only returns nonzero when using jemalloc.
38 */
39Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
41
42/**
43 * \internal
44 * \brief Computes the best allocation size for the requested minimum capacity, and updates capacity.
45 *
46 * Computes the allocation size starting from \a headerSize and a requested minimum capacity in \a capacity,
47 * multiplied by the \a elementSize and adjusted by the \a unusedCapacity.
48 * The final capacity is written back into \a capacity.
49 * The \a headerSize and \a unusedCapacity values are not included in the final reported capacity.
50 */
51inline size_t fittedAllocSize(size_t headerSize, size_t *capacity,
52 size_t elementSize, size_t unusedCapacity, size_t alignment) noexcept
53{
54 size_t totalCapacity = 0; // = capacity + unusedCapacity
55 if (Q_UNLIKELY(qAddOverflow(*capacity, unusedCapacity, &totalCapacity)))
56 return 0; // or handle error
57
58 size_t payloadSize = 0; // = totalCapacity * elementSize
59 if (Q_UNLIKELY(qMulOverflow(totalCapacity, elementSize, &payloadSize)))
60 return 0;
61
62 size_t allocSize = 0; // = headerSize + payloadSize
63 if (Q_UNLIKELY(qAddOverflow(headerSize, payloadSize, &allocSize)))
64 return 0;
65
66 if (size_t fittedSize = expectedAllocSize(allocSize, alignment); fittedSize != 0) {
67 // no need to overflow/underflow check from fittedSize,
68 // since allocSize <= fittedSize <= SIZE_T_MAX
69 *capacity = (fittedSize - headerSize) / elementSize - unusedCapacity;
70 size_t newTotalCapacity = *capacity + unusedCapacity;
71 size_t newPayloadSize = newTotalCapacity * elementSize;
72 return headerSize + newPayloadSize;
73 }
74
75 return allocSize;
76}
77
78#ifdef Q_CC_GNU
79__attribute__((malloc))
80#endif
81inline void *fittedMalloc(size_t headerSize, size_t *capacity,
82 size_t elementSize, size_t unusedCapacity) noexcept
83{
84 size_t allocSize = fittedAllocSize(headerSize, capacity,
85 elementSize, unusedCapacity, alignof(std::max_align_t));
86 if (Q_LIKELY(allocSize != 0))
87 return malloc(allocSize);
88 else
89 return nullptr;
90}
91inline void *fittedMalloc(size_t headerSize, qsizetype *capacity,
92 size_t elementSize, size_t unusedCapacity = 0) noexcept
93{
94 size_t uCapacity = size_t(*capacity);
95 void *ptr = fittedMalloc(headerSize, &uCapacity, elementSize, unusedCapacity);
96 *capacity = qsizetype(uCapacity);
97 return ptr;
98}
99
100inline void *fittedRealloc(void *ptr, size_t headerSize, size_t *capacity,
101 size_t elementSize, size_t unusedCapacity) noexcept
102{
103 size_t newCapacity = *capacity;
104 size_t allocSize = fittedAllocSize(headerSize, &newCapacity,
105 elementSize, unusedCapacity, alignof(std::max_align_t));
106 if (Q_LIKELY(allocSize != 0)) {
107 void *newPtr = realloc(ptr, allocSize);
108 if (newPtr)
109 *capacity = newCapacity;
110 return newPtr;
111 } else {
112 return nullptr;
113 }
114}
115inline void *fittedRealloc(void *ptr, size_t headerSize, qsizetype *capacity,
116 size_t elementSize, size_t unusedCapacity = 0) noexcept
117{
118 size_t uCapacity = size_t(*capacity);
119 ptr = fittedRealloc(ptr, headerSize, &uCapacity, elementSize, unusedCapacity);
120 *capacity = qsizetype(uCapacity);
121 return ptr;
122}
123
124Q_CORE_EXPORT void sizedFree(void *ptr, size_t allocSize) noexcept;
125inline void sizedFree(void *ptr, size_t capacity, size_t elementSize) noexcept
126{
127 sizedFree(ptr, capacity * elementSize);
128}
129
130} // namespace QtPrivate
131
132QT_END_NAMESPACE
133
134#endif // QALLOC_H
Combined button and popup list for selecting options.
Q_CORE_EXPORT void sizedFree(void *ptr, size_t allocSize) noexcept
Definition qalloc.cpp:28
void sizedFree(void *ptr, size_t capacity, size_t elementSize) noexcept
Definition qalloc.h:125
size_t fittedAllocSize(size_t headerSize, size_t *capacity, size_t elementSize, size_t unusedCapacity, size_t alignment) noexcept
Computes the best allocation size for the requested minimum capacity, and updates capacity.
Definition qalloc.h:51
void * fittedRealloc(void *ptr, size_t headerSize, size_t *capacity, size_t elementSize, size_t unusedCapacity) noexcept
Definition qalloc.h:100
void * fittedMalloc(size_t headerSize, size_t *capacity, size_t elementSize, size_t unusedCapacity) noexcept
Definition qalloc.h:81