diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-06-14 15:05:34 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2019-12-08 10:29:42 +0100 |
commit | add048bc4eee8e4422fe2b434b4b817f56693d33 (patch) | |
tree | c83275a8046a64b38ce474a1f9a6f2297526aab5 /src/corelib/tools | |
parent | f2569c0ff75eb9a8418bb065c33c318f0a44c8ed (diff) |
Start moving QArrayData's size and data pointer to the main class
This requires that the allocation functions return two pointers: the d
pointer and the pointer to the actual data.
Ported QArrayDataPointer & SimpleVector to the inlined size & data.
For now, the size and offset members are not yet removed from
QArrayData, to let QVector, QByteArray and QString compile unmodified.
Change-Id: I8489300976723d75b8fd5831427b1e2bba486196
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qarraydata.cpp | 29 | ||||
-rw-r--r-- | src/corelib/tools/qarraydata.h | 81 | ||||
-rw-r--r-- | src/corelib/tools/qarraydataops.h | 6 | ||||
-rw-r--r-- | src/corelib/tools/qarraydatapointer.h | 118 | ||||
-rw-r--r-- | src/corelib/tools/qvector.h | 20 |
5 files changed, 165 insertions, 89 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index aa7fac15ef..8052c2ca69 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -198,16 +198,19 @@ static QArrayData *reallocateData(QArrayData *header, size_t allocSize, uint opt return header; } -QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, +void *QArrayData::allocate(QArrayData **dptr, size_t objectSize, size_t alignment, size_t capacity, ArrayOptions options) noexcept { + Q_ASSERT(dptr); // Alignment is a power of two Q_ASSERT(alignment >= alignof(QArrayData) && !(alignment & (alignment - 1))); - if (capacity == 0) + if (capacity == 0) { // optimization for empty headers - return const_cast<QArrayData *>(&qt_array_empty); + *dptr = const_cast<QArrayData *>(&qt_array_empty); + return sharedNullData(); + } size_t headerSize = sizeof(QArrayData); @@ -225,14 +228,17 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, options |= AllocatedDataType | MutableData; options &= ~ImmutableHeader; QArrayData *header = allocateData(allocSize, options); + quintptr data = 0; if (header) { - quintptr data = (quintptr(header) + sizeof(QArrayData) + alignment - 1) + // find where offset should point to so that data() is aligned to alignment bytes + data = (quintptr(header) + sizeof(QArrayData) + alignment - 1) & ~(alignment - 1); header->offset = data - quintptr(header); header->alloc = capacity; } - return header; + *dptr = header; + return reinterpret_cast<void *>(data); } QArrayData *QArrayData::prepareRawData(ArrayOptions options) Q_DECL_NOTHROW @@ -243,8 +249,9 @@ QArrayData *QArrayData::prepareRawData(ArrayOptions options) Q_DECL_NOTHROW return header; } -QArrayData *QArrayData::reallocateUnaligned(QArrayData *data, size_t objectSize, size_t capacity, - ArrayOptions options) noexcept +QPair<QArrayData *, void *> +QArrayData::reallocateUnaligned(QArrayData *data, void *dataPointer, + size_t objectSize, size_t capacity, ArrayOptions options) noexcept { Q_ASSERT(data); Q_ASSERT(data->isMutable()); @@ -252,12 +259,14 @@ QArrayData *QArrayData::reallocateUnaligned(QArrayData *data, size_t objectSize, size_t headerSize = sizeof(QArrayData); size_t allocSize = calculateBlockSize(capacity, objectSize, headerSize, options); + qptrdiff offset = reinterpret_cast<char *>(dataPointer) - reinterpret_cast<char *>(data); options |= AllocatedDataType | MutableData; - options &= ~ImmutableHeader; QArrayData *header = reallocateData(data, allocSize, options); - if (header) + if (header) { header->alloc = capacity; - return header; + dataPointer = reinterpret_cast<char *>(header) + offset; + } + return qMakePair(static_cast<QArrayData *>(header), dataPointer); } void QArrayData::deallocate(QArrayData *data, size_t objectSize, diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index 56d61340f0..6cf0ea1cb6 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -41,11 +41,14 @@ #ifndef QARRAYDATA_H #define QARRAYDATA_H -#include <QtCore/qrefcount.h> +#include <QtCore/qpair.h> +#include <QtCore/qatomic.h> #include <string.h> QT_BEGIN_NAMESPACE +template <class T> struct QTypedArrayData; + struct Q_CORE_EXPORT QArrayData { enum ArrayOption { @@ -169,10 +172,12 @@ struct Q_CORE_EXPORT QArrayData #if defined(Q_CC_GNU) __attribute__((__malloc__)) #endif - static QArrayData *allocate(size_t objectSize, size_t alignment, + static void *allocate(QArrayData **pdata, size_t objectSize, size_t alignment, size_t capacity, ArrayOptions options = DefaultAllocationFlags) noexcept; Q_REQUIRED_RESULT static QArrayData *reallocateUnaligned(QArrayData *data, size_t objectSize, size_t newCapacity, ArrayOptions newOptions = DefaultAllocationFlags) noexcept; + Q_REQUIRED_RESULT static QPair<QArrayData *, void *> reallocateUnaligned(QArrayData *data, void *dataPointer, + size_t objectSize, size_t newCapacity, ArrayOptions newOptions = DefaultAllocationFlags) Q_DECL_NOTHROW; Q_REQUIRED_RESULT static QArrayData *prepareRawData(ArrayOptions options = ArrayOptions(RawDataType)) Q_DECL_NOTHROW; static void deallocate(QArrayData *data, size_t objectSize, @@ -190,6 +195,23 @@ struct Q_CORE_EXPORT QArrayData Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions) +template <class T, size_t N> +struct QStaticArrayData +{ + // static arrays are of type RawDataType + QArrayData header; + T data[N]; +}; + +// Support for returning QArrayDataPointer<T> from functions +template <class T> +struct QArrayDataPointerRef +{ + QTypedArrayData<T> *ptr; + T *data; + uint size; +}; + template <class T> struct QTypedArrayData : QArrayData @@ -282,27 +304,26 @@ struct QTypedArrayData class AlignmentDummy { QArrayData header; T data; }; - Q_REQUIRED_RESULT static QTypedArrayData *allocate(size_t capacity, + Q_REQUIRED_RESULT static QPair<QTypedArrayData *, T *> allocate(size_t capacity, ArrayOptions options = DefaultAllocationFlags) { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); - void *result = QArrayData::allocate(sizeof(T), - alignof(AlignmentDummy), capacity, options); + QArrayData *d; + void *result = QArrayData::allocate(&d, sizeof(T), alignof(AlignmentDummy), capacity, options); #if (defined(Q_CC_GNU) && Q_CC_GNU >= 407) || QT_HAS_BUILTIN(__builtin_assume_aligned) result = __builtin_assume_aligned(result, Q_ALIGNOF(AlignmentDummy)); #endif - return static_cast<QTypedArrayData *>(result); + return qMakePair(static_cast<QTypedArrayData *>(d), static_cast<T *>(result)); } - static QTypedArrayData *reallocateUnaligned(QTypedArrayData *data, size_t capacity, + static QPair<QTypedArrayData *, T *> + reallocateUnaligned(QTypedArrayData *data, T *dataPointer, size_t capacity, ArrayOptions options = DefaultAllocationFlags) { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); - void *result = QArrayData::reallocateUnaligned(data, sizeof(T), capacity, options); -#if (defined(Q_CC_GNU) && Q_CC_GNU >= 407) || QT_HAS_BUILTIN(__builtin_assume_aligned) - result =__builtin_assume_aligned(result, Q_ALIGNOF(AlignmentDummy)); -#endif - return static_cast<QTypedArrayData *>(result); + QPair<QArrayData *, void *> pair = + QArrayData::reallocateUnaligned(data, dataPointer, sizeof(T), capacity, options); + return qMakePair(static_cast<QTypedArrayData *>(pair.first), static_cast<T *>(pair.second)); } static void deallocate(QArrayData *data) @@ -311,17 +332,18 @@ struct QTypedArrayData QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy)); } - static QTypedArrayData *fromRawData(const T *data, size_t n, + static QArrayDataPointerRef<T> fromRawData(const T *data, size_t n, ArrayOptions options = DefaultRawFlags) { Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData)); - QTypedArrayData *result = static_cast<QTypedArrayData *>(prepareRawData(options)); - if (result) { - Q_ASSERT(!result->isShared()); // No shared empty, please! - - result->offset = reinterpret_cast<const char *>(data) - - reinterpret_cast<const char *>(result); - result->size = int(n); + QArrayDataPointerRef<T> result = { + static_cast<QTypedArrayData *>(prepareRawData(options)), const_cast<T *>(data), uint(n) + }; + if (result.ptr) { + Q_ASSERT(!result.ptr->isShared()); // No shared empty, please! + result.ptr->offset = reinterpret_cast<const char *>(data) + - reinterpret_cast<const char *>(result.ptr); + result.ptr->size = int(n); } return result; } @@ -345,21 +367,6 @@ struct QTypedArrayData } }; -template <class T, size_t N> -struct QStaticArrayData -{ - // static arrays are of type RawDataType - QArrayData header; - T data[N]; -}; - -// Support for returning QArrayDataPointer<T> from functions -template <class T> -struct QArrayDataPointerRef -{ - QTypedArrayData<T> *ptr; -}; - #define Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \ { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, size, 0, offset } \ /**/ @@ -412,7 +419,9 @@ struct QArrayDataPointerRef \ QArrayDataPointerRef<Type> ref = \ { static_cast<QTypedArrayData<Type> *>( \ - const_cast<QArrayData *>(&literal.header)) }; \ + const_cast<QArrayData *>(&literal.header)), \ + const_cast<Type *>(literal.data), \ + Size }; \ /**/ namespace QtPrivate { diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index ac7fc7bc4e..8fe35952bf 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -47,6 +47,8 @@ QT_BEGIN_NAMESPACE +template <class T> struct QArrayDataPointer; + namespace QtPrivate { QT_WARNING_PUSH @@ -56,7 +58,7 @@ QT_WARNING_DISABLE_GCC("-Wstringop-overflow") template <class T> struct QPodArrayOps - : QTypedArrayData<T> + : public QArrayDataPointer<T> { typedef T parameter_type; @@ -148,7 +150,7 @@ QT_WARNING_POP template <class T> struct QGenericArrayOps - : QTypedArrayData<T> + : public QArrayDataPointer<T> { typedef const T ¶meter_type; diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index 86997985d1..c52b84f4ce 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -52,26 +52,38 @@ private: typedef QArrayDataOps<T> DataOps; public: + typedef typename Data::iterator iterator; + typedef typename Data::const_iterator const_iterator; + QArrayDataPointer() noexcept - : d(Data::sharedNull()) + : d(Data::sharedNull()), b(Data::sharedNullData()), size(0) { } QArrayDataPointer(const QArrayDataPointer &other) - : d(other.d->ref() - ? other.d - : other.clone(other.d->cloneFlags())) + : d(other.d), b(other.b), size(other.size) + { + if (!other.d->ref()) { + // must clone + QPair<Data *, T *> pair = other.clone(other.d->cloneFlags()); + d = pair.first; + b = pair.second; + } + } + + QArrayDataPointer(Data *header, T *data, size_t n = 0) + : d(header), b(data), size(n) { } - explicit QArrayDataPointer(QTypedArrayData<T> *ptr) - : d(ptr) + explicit QArrayDataPointer(QPair<QTypedArrayData<T> *, T *> data, size_t n = 0) + : d(data.first), b(data.second), size(n) { - Q_CHECK_PTR(ptr); + Q_CHECK_PTR(d); } QArrayDataPointer(QArrayDataPointerRef<T> ref) - : d(ref.ptr) + : d(ref.ptr), b(ref.data), size(ref.size) { } @@ -83,7 +95,7 @@ public: } QArrayDataPointer(QArrayDataPointer &&other) noexcept - : d(other.d) + : d(other.d), b(other.b), size(other.size) { other.d = Data::sharedNull(); } @@ -95,16 +107,28 @@ public: return *this; } - DataOps &operator*() const + DataOps &operator*() + { + Q_ASSERT(d); + return *static_cast<DataOps *>(this); + } + + DataOps *operator->() + { + Q_ASSERT(d); + return static_cast<DataOps *>(this); + } + + const DataOps &operator*() const { Q_ASSERT(d); - return *static_cast<DataOps *>(d); + return *static_cast<const DataOps *>(this); } - DataOps *operator->() const + const DataOps *operator->() const { Q_ASSERT(d); - return static_cast<DataOps *>(d); + return static_cast<const DataOps *>(this); } ~QArrayDataPointer() @@ -121,62 +145,92 @@ public: return d == Data::sharedNull(); } - Data *data() const - { - return d; - } + T *data() { return b; } + const T *data() const { return b; } + + iterator begin() { return data(); } + iterator end() { return data() + size; } + const_iterator begin() const { return data(); } + const_iterator end() const { return data() + size; } + const_iterator constBegin() const { return data(); } + const_iterator constEnd() const { return data() + size; } void swap(QArrayDataPointer &other) noexcept { qSwap(d, other.d); + qSwap(b, other.b); + qSwap(size, other.size); } void clear() { - QArrayDataPointer tmp(d); + QArrayDataPointer tmp(d, b, size); d = Data::sharedNull(); + b = reinterpret_cast<T *>(d); + size = 0; } bool detach() { if (d->needsDetach()) { - Data *copy = clone(d->detachFlags()); - QArrayDataPointer old(d); - d = copy; + QPair<Data *, T *> copy = clone(d->detachFlags()); + QArrayDataPointer old(d, b, size); + d = copy.first; + b = copy.second; return true; } return false; } + // forwards from QArrayData + int allocatedCapacity() { return d->allocatedCapacity(); } + int constAllocatedCapacity() const { return d->constAllocatedCapacity(); } + int refCounterValue() const { return d->refCounterValue(); } + bool ref() { return d->ref(); } + bool deref() { return d->deref(); } + bool isMutable() const { return d->isMutable(); } + bool isStatic() const { return d->isStatic(); } + bool isShared() const { return d->isShared(); } + bool needsDetach() const { return d->needsDetach(); } + size_t detachCapacity(size_t newSize) const { return d->detachCapacity(newSize); } + typename Data::ArrayOptions &flags() { return reinterpret_cast<typename Data::ArrayOptions &>(d->flags); } + typename Data::ArrayOptions flags() const { return typename Data::ArrayOption(d->flags); } + typename Data::ArrayOptions detachFlags() const { return d->detachFlags(); } + typename Data::ArrayOptions cloneFlags() const { return d->cloneFlags(); } + private: - Q_REQUIRED_RESULT Data *clone(QArrayData::ArrayOptions options) const + Q_REQUIRED_RESULT QPair<Data *, T *> clone(QArrayData::ArrayOptions options) const { - Data *x = Data::allocate(d->detachCapacity(d->size), options); - Q_CHECK_PTR(x); - QArrayDataPointer copy(x); - - if (d->size) - copy->copyAppend(d->begin(), d->end()); + QPair<Data *, T *> pair = Data::allocate(d->detachCapacity(size), + options); + Q_CHECK_PTR(pair.first); + QArrayDataPointer copy(pair.first, pair.second, 0); + if (size) + copy->copyAppend(begin(), end()); - Data *result = copy.d; + pair.first = copy.d; copy.d = Data::sharedNull(); - return result; + return pair; } Data *d; + T *b; + +public: + uint size; }; template <class T> inline bool operator==(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs) { - return lhs.data() == rhs.data(); + return lhs.data() == rhs.data() && lhs.size == rhs.size; } template <class T> inline bool operator!=(const QArrayDataPointer<T> &lhs, const QArrayDataPointer<T> &rhs) { - return lhs.data() != rhs.data(); + return lhs.data() != rhs.data() || lhs.size != rhs.size; } template <class T> diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index bf422e72d4..330bf8bb98 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -369,11 +369,11 @@ inline QVector<T>::QVector(const QVector<T> &v) d = v.d; } else { if (v.d->flags & Data::CapacityReserved) { - d = Data::allocate(v.d->allocatedCapacity()); + d = Data::allocate(v.d->allocatedCapacity()).first; Q_CHECK_PTR(d); d->flags |= Data::CapacityReserved; } else { - d = Data::allocate(v.d->size); + d = Data::allocate(v.d->size).first; Q_CHECK_PTR(d); } if (v.d->size) { @@ -394,9 +394,10 @@ void QVector<T>::detach() if (d->isStatic()) return; - if (d->needsDetach()) + if (d->needsDetach()) { realloc(d->allocatedCapacity(), d->detachFlags()); - Q_ASSERT(isDetached()); + Q_ASSERT(isDetached()); + } } template <typename T> @@ -497,7 +498,7 @@ QVector<T>::QVector(int asize) { Q_ASSERT_X(asize >= 0, "QVector::QVector", "Size must be greater than or equal to 0."); if (Q_LIKELY(asize > 0)) { - d = Data::allocate(asize); + d = Data::allocate(asize).first; Q_CHECK_PTR(d); d->size = asize; defaultConstruct(d->begin(), d->end()); @@ -511,7 +512,7 @@ QVector<T>::QVector(int asize, const T &t) { Q_ASSERT_X(asize >= 0, "QVector::QVector", "Size must be greater than or equal to 0."); if (asize > 0) { - d = Data::allocate(asize); + d = Data::allocate(asize).first; Q_CHECK_PTR(d); d->size = asize; T* i = d->end(); @@ -531,7 +532,7 @@ template <typename T> QVector<T>::QVector(std::initializer_list<T> args) { if (args.size() > 0) { - d = Data::allocate(args.size()); + d = Data::allocate(args.size()).first; Q_CHECK_PTR(d); // std::initializer_list<T>::iterator is guaranteed to be // const T* ([support.initlist]/1), so can be memcpy'ed away from by copyConstruct @@ -585,7 +586,8 @@ void QVector<T>::realloc(int aalloc, QArrayData::ArrayOptions options) QT_TRY { // allocate memory - x = Data::allocate(aalloc, options); + auto pair = Data::allocate(aalloc, options); + x = pair.first; Q_CHECK_PTR(x); // aalloc is bigger then 0 so it is not [un]sharedEmpty Q_ASSERT(!x->isStatic()); @@ -749,7 +751,7 @@ typename QVector<T>::iterator QVector<T>::insert(iterator before, T &&t) Q_ASSERT_X(isValidIterator(before), "QVector::insert", "The specified iterator argument 'before' is invalid"); const auto offset = std::distance(d->begin(), before); - if (!isDetached() || d->size + 1 > int(d->alloc)) + if (!isDetached() || d->size + 1 > int(d->allocatedCapacity())) realloc(d->size + 1, QArrayData::GrowsForward); if (!QTypeInfoQuery<T>::isRelocatable) { T *i = d->end(); |