Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-13 09:05:55

0001 // Copyright (C) 2020 The Qt Company Ltd.
0002 // Copyright (C) 2019 Intel Corporation.
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 
0005 #ifndef QARRAYDATA_H
0006 #define QARRAYDATA_H
0007 
0008 #include <QtCore/qpair.h>
0009 #include <QtCore/qatomic.h>
0010 #include <QtCore/qflags.h>
0011 #include <QtCore/qcontainerfwd.h>
0012 #include <string.h>
0013 
0014 QT_BEGIN_NAMESPACE
0015 
0016 #if __has_cpp_attribute(gnu::malloc)
0017 #  define Q_DECL_MALLOCLIKE [[nodiscard, gnu::malloc]]
0018 #else
0019 #  define Q_DECL_MALLOCLIKE [[nodiscard]]
0020 #endif
0021 
0022 template <class T> struct QTypedArrayData;
0023 
0024 struct QArrayData
0025 {
0026     enum AllocationOption {
0027         Grow,
0028         KeepSize
0029     };
0030 
0031     enum GrowthPosition {
0032         GrowsAtEnd,
0033         GrowsAtBeginning
0034     };
0035 
0036    enum ArrayOption {
0037         ArrayOptionDefault = 0,
0038         CapacityReserved     = 0x1  //!< the capacity was reserved by the user, try to keep it
0039     };
0040     Q_DECLARE_FLAGS(ArrayOptions, ArrayOption)
0041 
0042     QBasicAtomicInt ref_;
0043     ArrayOptions flags;
0044     qsizetype alloc;
0045 
0046     qsizetype allocatedCapacity() noexcept
0047     {
0048         return alloc;
0049     }
0050 
0051     qsizetype constAllocatedCapacity() const noexcept
0052     {
0053         return alloc;
0054     }
0055 
0056     /// Returns true if sharing took place
0057     bool ref() noexcept
0058     {
0059         ref_.ref();
0060         return true;
0061     }
0062 
0063     /// Returns false if deallocation is necessary
0064     bool deref() noexcept
0065     {
0066         return ref_.deref();
0067     }
0068 
0069     bool isShared() const noexcept
0070     {
0071         return ref_.loadRelaxed() != 1;
0072     }
0073 
0074     // Returns true if a detach is necessary before modifying the data
0075     // This method is intentionally not const: if you want to know whether
0076     // detaching is necessary, you should be in a non-const function already
0077     bool needsDetach() noexcept
0078     {
0079         return ref_.loadRelaxed() > 1;
0080     }
0081 
0082     qsizetype detachCapacity(qsizetype newSize) const noexcept
0083     {
0084         if (flags & CapacityReserved && newSize < constAllocatedCapacity())
0085             return constAllocatedCapacity();
0086         return newSize;
0087     }
0088 
0089     Q_DECL_MALLOCLIKE
0090     static Q_CORE_EXPORT void *allocate(QArrayData **pdata, qsizetype objectSize, qsizetype alignment,
0091             qsizetype capacity, AllocationOption option = QArrayData::KeepSize) noexcept;
0092     Q_DECL_MALLOCLIKE
0093     static Q_CORE_EXPORT void *allocate1(QArrayData **pdata, qsizetype capacity,
0094                                          AllocationOption option = QArrayData::KeepSize) noexcept;
0095     Q_DECL_MALLOCLIKE
0096     static Q_CORE_EXPORT void *allocate2(QArrayData **pdata, qsizetype capacity,
0097                                          AllocationOption option = QArrayData::KeepSize) noexcept;
0098 
0099     [[nodiscard]] static Q_CORE_EXPORT std::pair<QArrayData *, void *> reallocateUnaligned(QArrayData *data, void *dataPointer,
0100             qsizetype objectSize, qsizetype newCapacity, AllocationOption option) noexcept;
0101     static Q_CORE_EXPORT void deallocate(QArrayData *data, qsizetype objectSize,
0102             qsizetype alignment) noexcept;
0103 };
0104 
0105 Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions)
0106 
0107 namespace QtPrivate {
0108 // QArrayData with strictest alignment requirements supported by malloc()
0109 #if defined(Q_PROCESSOR_X86_32) && defined(Q_CC_GNU)
0110 // GCC's definition is incorrect since GCC 8 (commit r240248 in SVN; commit
0111 // 63012d9a57edc950c5f30242d1e19318b5708060 in Git). This is applied to all
0112 // GCC-like compilers in case they decide to follow GCC's lead in being wrong.
0113 constexpr size_t MaxPrimitiveAlignment = 2 * sizeof(void *);
0114 #else
0115 constexpr size_t MaxPrimitiveAlignment = alignof(std::max_align_t);
0116 #endif
0117 
0118 struct alignas(MaxPrimitiveAlignment) AlignedQArrayData : QArrayData
0119 {
0120 };
0121 }
0122 
0123 template <class T>
0124 struct QTypedArrayData
0125     : QArrayData
0126 {
0127     struct AlignmentDummy { QtPrivate::AlignedQArrayData header; T data; };
0128 
0129     [[nodiscard]] static std::pair<QTypedArrayData *, T *> allocate(qsizetype capacity, AllocationOption option = QArrayData::KeepSize)
0130     {
0131         static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
0132         QArrayData *d;
0133         void *result;
0134         if constexpr (sizeof(T) == 1) {
0135             // necessarily, alignof(T) == 1
0136             result = allocate1(&d, capacity, option);
0137         } else if constexpr (sizeof(T) == 2) {
0138             // alignof(T) may be 1, but that makes no difference
0139             result = allocate2(&d, capacity, option);
0140         } else {
0141             result = QArrayData::allocate(&d, sizeof(T), alignof(AlignmentDummy), capacity, option);
0142         }
0143 #if __has_builtin(__builtin_assume_aligned)
0144         // and yet we do offer results that have stricter alignment
0145         result = __builtin_assume_aligned(result, Q_ALIGNOF(AlignmentDummy));
0146 #endif
0147         return {static_cast<QTypedArrayData *>(d), static_cast<T *>(result)};
0148     }
0149 
0150     static std::pair<QTypedArrayData *, T *>
0151     reallocateUnaligned(QTypedArrayData *data, T *dataPointer, qsizetype capacity, AllocationOption option)
0152     {
0153         static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
0154         std::pair<QArrayData *, void *> pair =
0155                 QArrayData::reallocateUnaligned(data, dataPointer, sizeof(T), capacity, option);
0156         return {static_cast<QTypedArrayData *>(pair.first), static_cast<T *>(pair.second)};
0157     }
0158 
0159     static void deallocate(QArrayData *data) noexcept
0160     {
0161         static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
0162         QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy));
0163     }
0164 
0165     static T *dataStart(QArrayData *data, qsizetype alignment) noexcept
0166     {
0167         // Alignment is a power of two
0168         Q_ASSERT(alignment >= qsizetype(alignof(QArrayData)) && !(alignment & (alignment - 1)));
0169         void *start =  reinterpret_cast<void *>(
0170             (quintptr(data) + sizeof(QArrayData) + alignment - 1) & ~(alignment - 1));
0171         return static_cast<T *>(start);
0172     }
0173 
0174     constexpr static qsizetype maxSize() noexcept
0175     {
0176         // -1 to deal with the pointer one-past-the-end
0177         return (QtPrivate::MaxAllocSize - sizeof(QtPrivate::AlignedQArrayData) - 1) / sizeof(T);
0178     }
0179     constexpr static qsizetype max_size() noexcept
0180     {
0181         return maxSize();
0182     }
0183 };
0184 
0185 namespace QtPrivate {
0186 struct Q_CORE_EXPORT QContainerImplHelper
0187 {
0188     enum CutResult { Null, Empty, Full, Subset };
0189     static constexpr CutResult mid(qsizetype originalLength, qsizetype *_position, qsizetype *_length)
0190     {
0191         qsizetype &position = *_position;
0192         qsizetype &length = *_length;
0193         if (position > originalLength) {
0194             position = 0;
0195             length = 0;
0196             return Null;
0197         }
0198 
0199         if (position < 0) {
0200             if (length < 0 || length + position >= originalLength) {
0201                 position = 0;
0202                 length = originalLength;
0203                 return Full;
0204             }
0205             if (length + position <= 0) {
0206                 position = length = 0;
0207                 return Null;
0208             }
0209             length += position;
0210             position = 0;
0211         } else if (size_t(length) > size_t(originalLength - position)) {
0212             length = originalLength - position;
0213         }
0214 
0215         if (position == 0 && length == originalLength)
0216             return Full;
0217 
0218         return length > 0 ? Subset : Empty;
0219     }
0220 };
0221 }
0222 
0223 #undef Q_DECL_MALLOCLIKE
0224 
0225 QT_END_NAMESPACE
0226 
0227 #endif // include guard