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
25QT_BEGIN_NAMESPACE
26
27namespace 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 */
38Q_CORE_EXPORT Q_DECL_PURE_FUNCTION
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 */
50inline 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
80inline 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}
90inline 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
99inline void *fittedRealloc(void *ptr, size_t headerSize, size_t *capacity,
100 size_t elementSize, size_t unusedCapacity) noexcept
101{
102 size_t newCapacity = *capacity;
103 size_t allocSize = fittedAllocSize(headerSize, &newCapacity,
104 elementSize, unusedCapacity, alignof(std::max_align_t));
105 if (Q_LIKELY(allocSize != 0)) {
106 void *newPtr = realloc(ptr, allocSize);
107 if (newPtr)
108 *capacity = newCapacity;
109 return newPtr;
110 } else {
111 return nullptr;
112 }
113}
114inline void *fittedRealloc(void *ptr, size_t headerSize, qsizetype *capacity,
115 size_t elementSize, size_t unusedCapacity = 0) noexcept
116{
117 size_t uCapacity = size_t(*capacity);
118 ptr = fittedRealloc(ptr, headerSize, &uCapacity, elementSize, unusedCapacity);
119 *capacity = qsizetype(uCapacity);
120 return ptr;
121}
122
123Q_CORE_EXPORT void sizedFree(void *ptr, size_t allocSize) noexcept;
124inline void sizedFree(void *ptr, size_t capacity, size_t elementSize) noexcept
125{
126 sizedFree(ptr, capacity * elementSize);
127}
128
129} // namespace QtPrivate
130
131QT_END_NAMESPACE
132
133#endif // QALLOC_H
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:124
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:50
void * fittedRealloc(void *ptr, size_t headerSize, size_t *capacity, size_t elementSize, size_t unusedCapacity) noexcept
Definition qalloc.h:99
void * fittedMalloc(size_t headerSize, size_t *capacity, size_t elementSize, size_t unusedCapacity) noexcept
Definition qalloc.h:80