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