File indexing completed on 2026-08-05 09:27:13
0001
0002
0003
0004
0005
0006 #ifndef QARRAYDATA_H
0007 #define QARRAYDATA_H
0008
0009 #include <QtCore/qpair.h>
0010 #include <QtCore/qatomic.h>
0011 #include <QtCore/qflags.h>
0012 #include <QtCore/qcontainerfwd.h>
0013 #include <string.h>
0014
0015 QT_BEGIN_NAMESPACE
0016
0017 #if __has_cpp_attribute(gnu::malloc)
0018 # define Q_DECL_MALLOCLIKE [[nodiscard, gnu::malloc]]
0019 #elif Q_CC_MSVC_ONLY
0020 # define Q_DECL_MALLOCLIKE __declspec(allocator) __declspec(restrict) [[nodiscard]]
0021 #else
0022 # define Q_DECL_MALLOCLIKE [[nodiscard]]
0023 #endif
0024
0025 template <class T> struct QTypedArrayData;
0026
0027 struct QArrayData
0028 {
0029 enum AllocationOption {
0030 Grow,
0031 KeepSize
0032 };
0033
0034 enum GrowthPosition {
0035 GrowsAtEnd,
0036 GrowsAtBeginning
0037 };
0038
0039 enum ArrayOption {
0040 ArrayOptionDefault = 0,
0041 CapacityReserved = 0x1
0042 };
0043 Q_DECLARE_FLAGS(ArrayOptions, ArrayOption)
0044
0045 QBasicAtomicInt ref_;
0046 ArrayOptions flags;
0047 qsizetype alloc;
0048
0049 qsizetype allocatedCapacity() noexcept
0050 {
0051 return alloc;
0052 }
0053
0054 qsizetype constAllocatedCapacity() const noexcept
0055 {
0056 return alloc;
0057 }
0058
0059
0060 bool ref() noexcept
0061 {
0062 ref_.refRelaxed();
0063 return true;
0064 }
0065
0066
0067 bool deref() noexcept
0068 {
0069 return ref_.deref();
0070 }
0071
0072 bool isShared() const noexcept
0073 {
0074 return ref_.loadRelaxed() != 1;
0075 }
0076
0077
0078
0079
0080 bool needsDetach() noexcept
0081 {
0082 return ref_.loadRelaxed() > 1;
0083 }
0084
0085 qsizetype detachCapacity(qsizetype newSize) const noexcept
0086 {
0087 if (flags & CapacityReserved && newSize < constAllocatedCapacity())
0088 return constAllocatedCapacity();
0089 return newSize;
0090 }
0091
0092 Q_DECL_MALLOCLIKE
0093 static Q_CORE_EXPORT void *allocate(QArrayData **pdata, qsizetype objectSize, qsizetype alignment,
0094 qsizetype capacity, AllocationOption option = QArrayData::KeepSize) noexcept;
0095 Q_DECL_MALLOCLIKE
0096 static Q_CORE_EXPORT void *allocate1(QArrayData **pdata, qsizetype capacity,
0097 AllocationOption option = QArrayData::KeepSize) noexcept;
0098 Q_DECL_MALLOCLIKE
0099 static Q_CORE_EXPORT void *allocate2(QArrayData **pdata, qsizetype capacity,
0100 AllocationOption option = QArrayData::KeepSize) noexcept;
0101
0102 [[nodiscard]] static Q_CORE_EXPORT std::pair<QArrayData *, void *> reallocateUnaligned(QArrayData *data, void *dataPointer,
0103 qsizetype objectSize, qsizetype newCapacity, AllocationOption option) noexcept;
0104 static Q_CORE_EXPORT void deallocate(QArrayData *data, qsizetype objectSize,
0105 qsizetype alignment) noexcept;
0106 };
0107
0108 Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions)
0109
0110 namespace QtPrivate {
0111
0112 #if defined(Q_PROCESSOR_X86_32) && defined(Q_CC_GNU)
0113
0114
0115
0116 constexpr size_t MaxPrimitiveAlignment = 2 * sizeof(void *);
0117 #else
0118 constexpr size_t MaxPrimitiveAlignment = alignof(std::max_align_t);
0119 #endif
0120
0121 struct alignas(MaxPrimitiveAlignment) AlignedQArrayData : QArrayData
0122 {
0123 };
0124 }
0125
0126 template <class T>
0127 struct QTypedArrayData
0128 : QArrayData
0129 {
0130 struct AlignmentDummy { QtPrivate::AlignedQArrayData header; T data; };
0131
0132 [[nodiscard]] static std::pair<QTypedArrayData *, T *> allocate(qsizetype capacity, AllocationOption option = QArrayData::KeepSize)
0133 {
0134 static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
0135 QArrayData *d;
0136 void *result;
0137 if constexpr (sizeof(T) == 1) {
0138
0139 result = allocate1(&d, capacity, option);
0140 } else if constexpr (sizeof(T) == 2) {
0141
0142 result = allocate2(&d, capacity, option);
0143 } else {
0144 result = QArrayData::allocate(&d, sizeof(T), alignof(AlignmentDummy), capacity, option);
0145 }
0146 #if __has_builtin(__builtin_assume_aligned)
0147
0148 result = __builtin_assume_aligned(result, Q_ALIGNOF(AlignmentDummy));
0149 #endif
0150 return {static_cast<QTypedArrayData *>(d), static_cast<T *>(result)};
0151 }
0152
0153 static std::pair<QTypedArrayData *, T *>
0154 reallocateUnaligned(QTypedArrayData *data, T *dataPointer, qsizetype capacity, AllocationOption option)
0155 {
0156 static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
0157 std::pair<QArrayData *, void *> pair =
0158 QArrayData::reallocateUnaligned(data, dataPointer, sizeof(T), capacity, option);
0159 return {static_cast<QTypedArrayData *>(pair.first), static_cast<T *>(pair.second)};
0160 }
0161
0162 static void deallocate(QArrayData *data) noexcept
0163 {
0164 static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
0165 QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy));
0166 }
0167
0168 static T *dataStart(QArrayData *data, qsizetype alignment) noexcept
0169 {
0170
0171 Q_ASSERT(alignment >= qsizetype(alignof(QArrayData)) && !(alignment & (alignment - 1)));
0172 void *start = reinterpret_cast<void *>(
0173 (quintptr(data) + sizeof(QArrayData) + alignment - 1) & ~(alignment - 1));
0174 return static_cast<T *>(start);
0175 }
0176
0177 constexpr static qsizetype maxSize() noexcept
0178 {
0179
0180 return (QtPrivate::MaxAllocSize - sizeof(QtPrivate::AlignedQArrayData) - 1) / sizeof(T);
0181 }
0182 constexpr static qsizetype max_size() noexcept
0183 {
0184 return maxSize();
0185 }
0186 };
0187
0188 namespace QtPrivate {
0189 struct Q_CORE_EXPORT QContainerImplHelper
0190 {
0191 enum CutResult { Null, Empty, Full, Subset };
0192 static constexpr CutResult mid(qsizetype originalLength, qsizetype *_position, qsizetype *_length)
0193 {
0194 qsizetype &position = *_position;
0195 qsizetype &length = *_length;
0196 if (position > originalLength) {
0197 position = 0;
0198 length = 0;
0199 return Null;
0200 }
0201
0202 if (position < 0) {
0203 if (length < 0 || length + position >= originalLength) {
0204 position = 0;
0205 length = originalLength;
0206 return Full;
0207 }
0208 if (length + position <= 0) {
0209 position = length = 0;
0210 return Null;
0211 }
0212 length += position;
0213 position = 0;
0214 } else if (size_t(length) > size_t(originalLength - position)) {
0215 length = originalLength - position;
0216 }
0217
0218 if (position == 0 && length == originalLength)
0219 return Full;
0220
0221 return length > 0 ? Subset : Empty;
0222 }
0223 };
0224 }
0225
0226 #undef Q_DECL_MALLOCLIKE
0227
0228 QT_END_NAMESPACE
0229
0230 #endif