1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Copyright (C) 2019 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QARRAYDATA_H
42#define QARRAYDATA_H
43
44#include <QtCore/qpair.h>
45#include <QtCore/qatomic.h>
46#include <string.h>
47
48QT_BEGIN_NAMESPACE
49
50template <class T> struct QTypedArrayData;
51
52struct Q_CORE_EXPORT QArrayData
53{
54 enum ArrayOption {
55 /// this option is used by the allocate() function
56 DefaultAllocationFlags = 0,
57 CapacityReserved = 0x1, //!< the capacity was reserved by the user, try to keep it
58 GrowsForward = 0x2, //!< allocate with eyes towards growing through append()
59 GrowsBackwards = 0x4 //!< allocate with eyes towards growing through prepend()
60 };
61 Q_DECLARE_FLAGS(ArrayOptions, ArrayOption)
62
63 QBasicAtomicInt ref_;
64 uint flags;
65 qsizetype alloc;
66
67 inline qsizetype allocatedCapacity() noexcept
68 {
69 return alloc;
70 }
71
72 inline qsizetype constAllocatedCapacity() const noexcept
73 {
74 return alloc;
75 }
76
77 /// Returns true if sharing took place
78 bool ref() noexcept
79 {
80 ref_.ref();
81 return true;
82 }
83
84 /// Returns false if deallocation is necessary
85 bool deref() noexcept
86 {
87 return ref_.deref();
88 }
89
90 bool isShared() const noexcept
91 {
92 return ref_.loadRelaxed() != 1;
93 }
94
95 // Returns true if a detach is necessary before modifying the data
96 // This method is intentionally not const: if you want to know whether
97 // detaching is necessary, you should be in a non-const function already
98 bool needsDetach() const noexcept
99 {
100 return ref_.loadRelaxed() > 1;
101 }
102
103 qsizetype detachCapacity(qsizetype newSize) const noexcept
104 {
105 if (flags & CapacityReserved && newSize < constAllocatedCapacity())
106 return constAllocatedCapacity();
107 return newSize;
108 }
109
110 ArrayOptions detachFlags() const noexcept
111 {
112 ArrayOptions result = DefaultAllocationFlags;
113 if (flags & CapacityReserved)
114 result |= CapacityReserved;
115 return result;
116 }
117
118 [[nodiscard]]
119#if defined(Q_CC_GNU)
120 __attribute__((__malloc__))
121#endif
122 static void *allocate(QArrayData **pdata, qsizetype objectSize, qsizetype alignment,
123 qsizetype capacity, ArrayOptions options = DefaultAllocationFlags) noexcept;
124 [[nodiscard]] static QPair<QArrayData *, void *> reallocateUnaligned(QArrayData *data, void *dataPointer,
125 qsizetype objectSize, qsizetype newCapacity, ArrayOptions newOptions = DefaultAllocationFlags) noexcept;
126 static void deallocate(QArrayData *data, qsizetype objectSize,
127 qsizetype alignment) noexcept;
128};
129
130Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions)
131
132template <class T>
133struct QTypedArrayData
134 : QArrayData
135{
136 class iterator {
137 T *i = nullptr;
138 public:
139 typedef std::random_access_iterator_tag iterator_category;
140 typedef qsizetype difference_type;
141 typedef T value_type;
142 typedef T *pointer;
143 typedef T &reference;
144
145 inline constexpr iterator() = default;
146 inline iterator(T *n) : i(n) {}
147 inline T &operator*() const { return *i; }
148 inline T *operator->() const { return i; }
149 inline T &operator[](qsizetype j) const { return *(i + j); }
150 inline constexpr bool operator==(iterator o) const { return i == o.i; }
151 inline constexpr bool operator!=(iterator o) const { return i != o.i; }
152 inline constexpr bool operator<(iterator other) const { return i < other.i; }
153 inline constexpr bool operator<=(iterator other) const { return i <= other.i; }
154 inline constexpr bool operator>(iterator other) const { return i > other.i; }
155 inline constexpr bool operator>=(iterator other) const { return i >= other.i; }
156 inline constexpr bool operator==(pointer p) const { return i == p; }
157 inline constexpr bool operator!=(pointer p) const { return i != p; }
158 inline iterator &operator++() { ++i; return *this; }
159 inline iterator operator++(int) { T *n = i; ++i; return n; }
160 inline iterator &operator--() { i--; return *this; }
161 inline iterator operator--(int) { T *n = i; i--; return n; }
162 inline iterator &operator+=(qsizetype j) { i+=j; return *this; }
163 inline iterator &operator-=(qsizetype j) { i-=j; return *this; }
164 inline iterator operator+(qsizetype j) const { return iterator(i+j); }
165 inline iterator operator-(qsizetype j) const { return iterator(i-j); }
166 friend inline iterator operator+(qsizetype j, iterator k) { return k + j; }
167 inline qsizetype operator-(iterator j) const { return i - j.i; }
168 inline operator T*() const { return i; }
169 };
170
171 class const_iterator {
172 const T *i = nullptr;
173 public:
174 typedef std::random_access_iterator_tag iterator_category;
175 typedef qsizetype difference_type;
176 typedef T value_type;
177 typedef const T *pointer;
178 typedef const T &reference;
179
180 inline constexpr const_iterator() = default;
181 inline const_iterator(const T *n) : i(n) {}
182 inline constexpr const_iterator(iterator o): i(o) {}
183 inline const T &operator*() const { return *i; }
184 inline const T *operator->() const { return i; }
185 inline const T &operator[](qsizetype j) const { return *(i + j); }
186 inline constexpr bool operator==(const_iterator o) const { return i == o.i; }
187 inline constexpr bool operator!=(const_iterator o) const { return i != o.i; }
188 inline constexpr bool operator<(const_iterator other) const { return i < other.i; }
189 inline constexpr bool operator<=(const_iterator other) const { return i <= other.i; }
190 inline constexpr bool operator>(const_iterator other) const { return i > other.i; }
191 inline constexpr bool operator>=(const_iterator other) const { return i >= other.i; }
192 inline constexpr bool operator==(iterator o) const { return i == const_iterator(o).i; }
193 inline constexpr bool operator!=(iterator o) const { return i != const_iterator(o).i; }
194 inline constexpr bool operator==(pointer p) const { return i == p; }
195 inline constexpr bool operator!=(pointer p) const { return i != p; }
196 inline const_iterator &operator++() { ++i; return *this; }
197 inline const_iterator operator++(int) { const T *n = i; ++i; return n; }
198 inline const_iterator &operator--() { i--; return *this; }
199 inline const_iterator operator--(int) { const T *n = i; i--; return n; }
200 inline const_iterator &operator+=(qsizetype j) { i+=j; return *this; }
201 inline const_iterator &operator-=(qsizetype j) { i-=j; return *this; }
202 inline const_iterator operator+(qsizetype j) const { return const_iterator(i+j); }
203 inline const_iterator operator-(qsizetype j) const { return const_iterator(i-j); }
204 friend inline const_iterator operator+(qsizetype j, const_iterator k) { return k + j; }
205 inline qsizetype operator-(const_iterator j) const { return i - j.i; }
206 inline operator const T*() const { return i; }
207 };
208
209 struct AlignmentDummy { QArrayData header; T data; };
210
211 [[nodiscard]] static QPair<QTypedArrayData *, T *> allocate(qsizetype capacity,
212 ArrayOptions options = DefaultAllocationFlags)
213 {
214 static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
215 QArrayData *d;
216 void *result = QArrayData::allocate(&d, sizeof(T), alignof(AlignmentDummy), capacity, options);
217#if (defined(Q_CC_GNU) && Q_CC_GNU >= 407) || QT_HAS_BUILTIN(__builtin_assume_aligned)
218 result = __builtin_assume_aligned(result, Q_ALIGNOF(AlignmentDummy));
219#endif
220 return qMakePair(static_cast<QTypedArrayData *>(d), static_cast<T *>(result));
221 }
222
223 static QPair<QTypedArrayData *, T *>
224 reallocateUnaligned(QTypedArrayData *data, T *dataPointer, qsizetype capacity,
225 ArrayOptions options = DefaultAllocationFlags)
226 {
227 static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
228 QPair<QArrayData *, void *> pair =
229 QArrayData::reallocateUnaligned(data, dataPointer, sizeof(T), capacity, options);
230 return qMakePair(static_cast<QTypedArrayData *>(pair.first), static_cast<T *>(pair.second));
231 }
232
233 static void deallocate(QArrayData *data) noexcept
234 {
235 static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
236 QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy));
237 }
238
239 static T *dataStart(QArrayData *data, qsizetype alignment) noexcept
240 {
241 // Alignment is a power of two
242 Q_ASSERT(alignment >= qsizetype(alignof(QArrayData)) && !(alignment & (alignment - 1)));
243 void *start = reinterpret_cast<void *>(
244 (quintptr(data) + sizeof(QArrayData) + alignment - 1) & ~(alignment - 1));
245 return static_cast<T *>(start);
246 }
247};
248
249namespace QtPrivate {
250struct Q_CORE_EXPORT QContainerImplHelper
251{
252 enum CutResult { Null, Empty, Full, Subset };
253 static constexpr CutResult mid(qsizetype originalLength, qsizetype *_position, qsizetype *_length)
254 {
255 qsizetype &position = *_position;
256 qsizetype &length = *_length;
257 if (position > originalLength) {
258 position = 0;
259 length = 0;
260 return Null;
261 }
262
263 if (position < 0) {
264 if (length < 0 || length + position >= originalLength) {
265 position = 0;
266 length = originalLength;
267 return Full;
268 }
269 if (length + position <= 0) {
270 position = length = 0;
271 return Null;
272 }
273 length += position;
274 position = 0;
275 } else if (size_t(length) > size_t(originalLength - position)) {
276 length = originalLength - position;
277 }
278
279 if (position == 0 && length == originalLength)
280 return Full;
281
282 return length > 0 ? Subset : Empty;
283 }
284};
285}
286
287QT_END_NAMESPACE
288
289#endif // include guard
290