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