QtBase  v6.3.1
qarraydata.h
Go to the documentation of this file.
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 
49 
50 template <class T> struct QTypedArrayData;
51 
52 struct QArrayData
53 {
56  KeepSize
57  };
58 
62  };
63 
64  enum ArrayOption {
66  CapacityReserved = 0x1
67  };
68  Q_DECLARE_FLAGS(ArrayOptions, ArrayOption)
69 
71  ArrayOptions flags;
73 
75  {
76  return alloc;
77  }
78 
80  {
81  return alloc;
82  }
83 
85  bool ref() noexcept
86  {
87  ref_.ref();
88  return true;
89  }
90 
92  bool deref() noexcept
93  {
94  return ref_.deref();
95  }
96 
97  bool isShared() const noexcept
98  {
99  return ref_.loadRelaxed() != 1;
100  }
101 
102  // Returns true if a detach is necessary before modifying the data
103  // This method is intentionally not const: if you want to know whether
104  // detaching is necessary, you should be in a non-const function already
105  bool needsDetach() const noexcept
106  {
107  return ref_.loadRelaxed() > 1;
108  }
109 
110  qsizetype detachCapacity(qsizetype newSize) const noexcept
111  {
112  if (flags & CapacityReserved && newSize < constAllocatedCapacity())
113  return constAllocatedCapacity();
114  return newSize;
115  }
116 
117  [[nodiscard]]
118 #if defined(Q_CC_GNU)
119  __attribute__((__malloc__))
120 #endif
121  static Q_CORE_EXPORT void *allocate(QArrayData **pdata, qsizetype objectSize, qsizetype alignment,
123  [[nodiscard]] static Q_CORE_EXPORT QPair<QArrayData *, void *> reallocateUnaligned(QArrayData *data, void *dataPointer,
124  qsizetype objectSize, qsizetype newCapacity, AllocationOption option) noexcept;
125  static Q_CORE_EXPORT void deallocate(QArrayData *data, qsizetype objectSize,
126  qsizetype alignment) noexcept;
127 };
128 
129 Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions)
130 
131 template <class T>
133  : QArrayData
134 {
136 
138  {
139  static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
140  QArrayData *d;
141  void *result = QArrayData::allocate(&d, sizeof(T), alignof(AlignmentDummy), capacity, option);
142 #if __has_builtin(__builtin_assume_aligned)
143  result = __builtin_assume_aligned(result, Q_ALIGNOF(AlignmentDummy));
144 #endif
145  return qMakePair(static_cast<QTypedArrayData *>(d), static_cast<T *>(result));
146  }
147 
150  {
151  static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
153  QArrayData::reallocateUnaligned(data, dataPointer, sizeof(T), capacity, option);
154  return qMakePair(static_cast<QTypedArrayData *>(pair.first), static_cast<T *>(pair.second));
155  }
156 
157  static void deallocate(QArrayData *data) noexcept
158  {
159  static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
160  QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy));
161  }
162 
164  {
165  // Alignment is a power of two
166  Q_ASSERT(alignment >= qsizetype(alignof(QArrayData)) && !(alignment & (alignment - 1)));
167  void *start = reinterpret_cast<void *>(
168  (quintptr(data) + sizeof(QArrayData) + alignment - 1) & ~(alignment - 1));
169  return static_cast<T *>(start);
170  }
171 };
172 
173 namespace QtPrivate {
174 struct Q_CORE_EXPORT QContainerImplHelper
175 {
176  enum CutResult { Null, Empty, Full, Subset };
177  static constexpr CutResult mid(qsizetype originalLength, qsizetype *_position, qsizetype *_length)
178  {
179  qsizetype &position = *_position;
180  qsizetype &length = *_length;
181  if (position > originalLength) {
182  position = 0;
183  length = 0;
184  return Null;
185  }
186 
187  if (position < 0) {
188  if (length < 0 || length + position >= originalLength) {
189  position = 0;
190  length = originalLength;
191  return Full;
192  }
193  if (length + position <= 0) {
194  position = length = 0;
195  return Null;
196  }
197  length += position;
198  position = 0;
199  } else if (size_t(length) > size_t(originalLength - position)) {
200  length = originalLength - position;
201  }
202 
203  if (position == 0 && length == originalLength)
204  return Full;
205 
206  return length > 0 ? Subset : Empty;
207  }
208 };
209 }
210 
212 
213 #endif // include guard
bool ref() noexcept
Definition: qbasicatomic.h:101
bool deref() noexcept
Definition: qbasicatomic.h:102
T loadRelaxed() const noexcept
Definition: qbasicatomic.h:90
uint alignment
auto it unsigned count const
Definition: hb-iter.hh:848
#define __attribute__(x)
Definition: hb.hh:256
QHighDpiScaling::Point position(T, QHighDpiScaling::Point::Kind)
#define Q_ALIGNOF(x)
std::pair< T1, T2 > QPair
Definition: qcontainerfwd.h:56
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition: qflags.h:210
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition: qflags.h:227
size_t quintptr
Definition: qglobal.h:310
ptrdiff_t qsizetype
Definition: qglobal.h:308
GLenum GLuint GLenum GLsizei length
Definition: qopengl.h:270
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint start
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
GLuint GLenum option
Definition: qopenglext.h:5929
constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
Definition: qpair.h:55
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
Definition: hb-null.hh:93
qsizetype detachCapacity(qsizetype newSize) const noexcept
Definition: qarraydata.h:110
@ CapacityReserved
the capacity was reserved by the user, try to keep it
Definition: qarraydata.h:66
@ ArrayOptionDefault
Definition: qarraydata.h:65
bool needsDetach() const noexcept
Definition: qarraydata.h:105
bool deref() noexcept
Returns false if deallocation is necessary.
Definition: qarraydata.h:92
static Q_CORE_EXPORT QPair< QArrayData *, void * > reallocateUnaligned(QArrayData *data, void *dataPointer, qsizetype objectSize, qsizetype newCapacity, AllocationOption option) noexcept
Definition: qarraydata.cpp:240
bool isShared() const noexcept
Definition: qarraydata.h:97
qsizetype alloc
Definition: qarraydata.h:72
qsizetype constAllocatedCapacity() const noexcept
Definition: qarraydata.h:79
static Q_CORE_EXPORT void * allocate(QArrayData **pdata, qsizetype objectSize, qsizetype alignment, qsizetype capacity, AllocationOption option=QArrayData::KeepSize) noexcept
Definition: qarraydata.cpp:196
static Q_CORE_EXPORT void deallocate(QArrayData *data, qsizetype objectSize, qsizetype alignment) noexcept
Definition: qarraydata.cpp:270
qsizetype allocatedCapacity() noexcept
Definition: qarraydata.h:74
@ GrowsAtBeginning
Definition: qarraydata.h:61
bool ref() noexcept
Returns true if sharing took place.
Definition: qarraydata.h:85
QBasicAtomicInt ref_
Definition: qarraydata.h:70
static void deallocate(QArrayData *data) noexcept
Definition: qarraydata.h:157
static T * dataStart(QArrayData *data, qsizetype alignment) noexcept
Definition: qarraydata.h:163
static QPair< QTypedArrayData *, T * > reallocateUnaligned(QTypedArrayData *data, T *dataPointer, qsizetype capacity, AllocationOption option)
Definition: qarraydata.h:149
static QPair< QTypedArrayData *, T * > allocate(qsizetype capacity, AllocationOption option=QArrayData::KeepSize)
Definition: qarraydata.h:137
static constexpr CutResult mid(qsizetype originalLength, qsizetype *_position, qsizetype *_length)
Definition: qarraydata.h:177
Definition: main.cpp:38