1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QCONTIGUOUSCACHE_H
43 #define QCONTIGUOUSCACHE_H
45 #include <QtCore/qatomic.h>
53 #undef QT_QCONTIGUOUSCACHE_DEBUG
56 struct Q_CORE_EXPORT QContiguousCacheData
66 // total is 24 bytes (HP-UX aCC: 40 bytes)
67 // the next entry is already aligned to 8 bytes
68 // there will be an 8 byte gap here if T requires 16-byte alignment
69 // (such as long double on 64-bit platforms, __int128, __float128)
71 static QContiguousCacheData *allocate(int size, int alignment);
72 static void free(QContiguousCacheData *data);
74 #ifdef QT_QCONTIGUOUSCACHE_DEBUG
80 struct QContiguousCacheTypedData: private QContiguousCacheData
82 // private inheritance to avoid aliasing warningss
85 static inline void free(QContiguousCacheTypedData *data) { QContiguousCacheData::free(data); }
89 class QContiguousCache {
90 typedef QContiguousCacheTypedData<T> Data;
91 union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; };
95 typedef value_type* pointer;
96 typedef const value_type* const_pointer;
97 typedef value_type& reference;
98 typedef const value_type& const_reference;
99 typedef qptrdiff difference_type;
100 typedef int size_type;
102 explicit QContiguousCache(int capacity = 0);
103 QContiguousCache(const QContiguousCache<T> &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
105 inline ~QContiguousCache() { if (!d) return; if (!d->ref.deref()) free(p); }
107 inline void detach() { if (d->ref.load() != 1) detach_helper(); }
108 inline bool isDetached() const { return d->ref.load() == 1; }
109 inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; }
111 QContiguousCache<T> &operator=(const QContiguousCache<T> &other);
112 #ifdef Q_COMPILER_RVALUE_REFS
113 inline QContiguousCache<T> &operator=(QContiguousCache<T> &&other)
114 { qSwap(d, other.d); return *this; }
116 inline void swap(QContiguousCache<T> &other) { qSwap(d, other.d); }
117 bool operator==(const QContiguousCache<T> &other) const;
118 inline bool operator!=(const QContiguousCache<T> &other) const { return !(*this == other); }
120 inline int capacity() const {return d->alloc; }
121 inline int count() const { return d->count; }
122 inline int size() const { return d->count; }
124 inline bool isEmpty() const { return d->count == 0; }
125 inline bool isFull() const { return d->count == d->alloc; }
126 inline int available() const { return d->alloc - d->count; }
129 void setCapacity(int size);
131 const T &at(int pos) const;
132 T &operator[](int i);
133 const T &operator[](int i) const;
135 void append(const T &value);
136 void prepend(const T &value);
137 void insert(int pos, const T &value);
139 inline bool containsIndex(int pos) const { return pos >= d->offset && pos - d->offset < d->count; }
140 inline int firstIndex() const { return d->offset; }
141 inline int lastIndex() const { return d->offset + d->count - 1; }
143 inline const T &first() const { Q_ASSERT(!isEmpty()); return p->array[d->start]; }
144 inline const T &last() const { Q_ASSERT(!isEmpty()); return p->array[(d->start + d->count -1) % d->alloc]; }
145 inline T &first() { Q_ASSERT(!isEmpty()); detach(); return p->array[d->start]; }
146 inline T &last() { Q_ASSERT(!isEmpty()); detach(); return p->array[(d->start + d->count -1) % d->alloc]; }
153 inline bool areIndexesValid() const
154 { return d->offset >= 0 && d->offset < INT_MAX - d->count && (d->offset % d->alloc) == d->start; }
156 inline void normalizeIndexes() { d->offset = d->start; }
158 #ifdef QT_QCONTIGUOUSCACHE_DEBUG
159 void dump() const { p->dump(); }
162 void detach_helper();
164 QContiguousCacheData *malloc(int aalloc);
166 int sizeOfTypedData() {
167 // this is more or less the same as sizeof(Data), except that it doesn't
168 // count the padding at the end
169 return reinterpret_cast<const char *>(&(reinterpret_cast<const Data *>(this))->array[1]) - reinterpret_cast<const char *>(this);
171 int alignOfTypedData() const
173 return qMax<int>(sizeof(void*), Q_ALIGNOF(Data));
177 template <typename T>
178 void QContiguousCache<T>::detach_helper()
180 union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
182 x.d = malloc(d->alloc);
184 x.d->count = d->count;
185 x.d->start = d->start;
186 x.d->offset = d->offset;
187 x.d->alloc = d->alloc;
188 x.d->sharable = true;
191 T *dest = x.p->array + x.d->start;
192 T *src = p->array + d->start;
193 int oldcount = x.d->count;
195 if (QTypeInfo<T>::isComplex) {
201 if (dest == x.p->array + x.d->alloc)
204 if (src == p->array + d->alloc)
213 template <typename T>
214 void QContiguousCache<T>::setCapacity(int asize)
216 if (asize == d->alloc)
219 union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
222 x.d->count = qMin(d->count, asize);
223 x.d->offset = d->offset + d->count - x.d->count;
225 x.d->start = x.d->offset % x.d->alloc;
229 int oldcount = x.d->count;
232 T *dest = x.p->array + (x.d->start + x.d->count-1) % x.d->alloc;
233 T *src = p->array + (d->start + d->count-1) % d->alloc;
235 if (QTypeInfo<T>::isComplex) {
240 if (dest == x.p->array)
241 dest = x.p->array + x.d->alloc;
244 src = p->array + d->alloc;
253 template <typename T>
254 void QContiguousCache<T>::clear()
256 if (d->ref.load() == 1) {
257 if (QTypeInfo<T>::isComplex) {
258 int oldcount = d->count;
259 T * i = p->array + d->start;
260 T * e = p->array + d->alloc;
268 d->count = d->start = d->offset = 0;
270 union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
271 x.d = malloc(d->alloc);
273 x.d->alloc = d->alloc;
274 x.d->count = x.d->start = x.d->offset = 0;
275 x.d->sharable = true;
276 if (!d->ref.deref()) free(p);
281 template <typename T>
282 inline QContiguousCacheData *QContiguousCache<T>::malloc(int aalloc)
284 return QContiguousCacheData::allocate(sizeOfTypedData() + (aalloc - 1) * sizeof(T), alignOfTypedData());
287 template <typename T>
288 QContiguousCache<T>::QContiguousCache(int cap)
293 d->count = d->start = d->offset = 0;
297 template <typename T>
298 QContiguousCache<T> &QContiguousCache<T>::operator=(const QContiguousCache<T> &other)
309 template <typename T>
310 bool QContiguousCache<T>::operator==(const QContiguousCache<T> &other) const
314 if (other.d->start != d->start
315 || other.d->count != d->count
316 || other.d->offset != d->offset
317 || other.d->alloc != d->alloc)
319 for (int i = firstIndex(); i <= lastIndex(); ++i)
320 if (!(at(i) == other.at(i)))
325 template <typename T>
326 void QContiguousCache<T>::free(Data *x)
328 if (QTypeInfo<T>::isComplex) {
329 int oldcount = d->count;
330 T * i = p->array + d->start;
331 T * e = p->array + d->alloc;
341 template <typename T>
342 void QContiguousCache<T>::append(const T &value)
345 if (QTypeInfo<T>::isComplex) {
346 if (d->count == d->alloc)
347 (p->array + (d->start+d->count) % d->alloc)->~T();
348 new (p->array + (d->start+d->count) % d->alloc) T(value);
350 p->array[(d->start+d->count) % d->alloc] = value;
353 if (d->count == d->alloc) {
355 d->start %= d->alloc;
363 void QContiguousCache<T>::prepend(const T &value)
369 d->start = d->alloc-1;
372 if (d->count != d->alloc)
375 if (d->count == d->alloc)
376 (p->array + d->start)->~T();
378 if (QTypeInfo<T>::isComplex)
379 new (p->array + d->start) T(value);
381 p->array[d->start] = value;
385 void QContiguousCache<T>::insert(int pos, const T &value)
387 Q_ASSERT_X(pos >= 0 && pos < INT_MAX, "QContiguousCache<T>::insert", "index out of range");
389 if (containsIndex(pos)) {
390 if(QTypeInfo<T>::isComplex)
391 new (p->array + pos % d->alloc) T(value);
393 p->array[pos % d->alloc] = value;
394 } else if (pos == d->offset-1)
396 else if (pos == d->offset+d->count)
399 // we don't leave gaps.
402 d->start = pos % d->alloc;
404 if (QTypeInfo<T>::isComplex)
405 new (p->array + d->start) T(value);
407 p->array[d->start] = value;
411 template <typename T>
412 inline const T &QContiguousCache<T>::at(int pos) const
413 { Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count, "QContiguousCache<T>::at", "index out of range"); return p->array[pos % d->alloc]; }
414 template <typename T>
415 inline const T &QContiguousCache<T>::operator[](int pos) const
416 { Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count, "QContiguousCache<T>::at", "index out of range"); return p->array[pos % d->alloc]; }
418 template <typename T>
419 inline T &QContiguousCache<T>::operator[](int pos)
422 if (!containsIndex(pos))
424 return p->array[pos % d->alloc];
427 template <typename T>
428 inline void QContiguousCache<T>::removeFirst()
430 Q_ASSERT(d->count > 0);
433 if (QTypeInfo<T>::isComplex)
434 (p->array + d->start)->~T();
435 d->start = (d->start + 1) % d->alloc;
439 template <typename T>
440 inline void QContiguousCache<T>::removeLast()
442 Q_ASSERT(d->count > 0);
445 if (QTypeInfo<T>::isComplex)
446 (p->array + (d->start + d->count) % d->alloc)->~T();
449 template <typename T>
450 inline T QContiguousCache<T>::takeFirst()
451 { T t = first(); removeFirst(); return t; }
453 template <typename T>
454 inline T QContiguousCache<T>::takeLast()
455 { T t = last(); removeLast(); return t; }