| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QRECYCLEPOOL_P_H |
| 41 | #define QRECYCLEPOOL_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | QT_BEGIN_NAMESPACE |
| 55 | |
| 56 | #define QRECYCLEPOOLCOOKIE 0x33218ADF |
| 57 | |
| 58 | template<typename T, int Step> |
| 59 | class QRecyclePoolPrivate |
| 60 | { |
| 61 | public: |
| 62 | QRecyclePoolPrivate() |
| 63 | : recyclePoolHold(true), outstandingItems(0), cookie(QRECYCLEPOOLCOOKIE), |
| 64 | currentPage(nullptr), nextAllocated(nullptr) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | bool recyclePoolHold; |
| 69 | int outstandingItems; |
| 70 | quint32 cookie; |
| 71 | |
| 72 | struct PoolType : public T { |
| 73 | union { |
| 74 | QRecyclePoolPrivate<T, Step> *pool; |
| 75 | PoolType *nextAllocated; |
| 76 | }; |
| 77 | }; |
| 78 | |
| 79 | struct Page { |
| 80 | Page *nextPage; |
| 81 | unsigned int free; |
| 82 | union { |
| 83 | char array[Step * sizeof(PoolType)]; |
| 84 | qint64 q_for_alignment_1; |
| 85 | double q_for_alignment_2; |
| 86 | }; |
| 87 | }; |
| 88 | |
| 89 | Page *currentPage; |
| 90 | PoolType *nextAllocated; |
| 91 | |
| 92 | inline T *allocate(); |
| 93 | static inline void dispose(T *); |
| 94 | inline void releaseIfPossible(); |
| 95 | }; |
| 96 | |
| 97 | template<typename T, int Step = 1024> |
| 98 | class QRecyclePool |
| 99 | { |
| 100 | public: |
| 101 | inline QRecyclePool(); |
| 102 | inline ~QRecyclePool(); |
| 103 | |
| 104 | inline T *New(); |
| 105 | template<typename T1> |
| 106 | inline T *New(const T1 &); |
| 107 | template<typename T1> |
| 108 | inline T *New(T1 &); |
| 109 | |
| 110 | static inline void Delete(T *); |
| 111 | |
| 112 | private: |
| 113 | QRecyclePoolPrivate<T, Step> *d; |
| 114 | }; |
| 115 | |
| 116 | template<typename T, int Step> |
| 117 | QRecyclePool<T, Step>::QRecyclePool() |
| 118 | : d(new QRecyclePoolPrivate<T, Step>()) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | template<typename T, int Step> |
| 123 | QRecyclePool<T, Step>::~QRecyclePool() |
| 124 | { |
| 125 | d->recyclePoolHold = false; |
| 126 | d->releaseIfPossible(); |
| 127 | } |
| 128 | |
| 129 | template<typename T, int Step> |
| 130 | T *QRecyclePool<T, Step>::New() |
| 131 | { |
| 132 | T *rv = d->allocate(); |
| 133 | new (rv) T; |
| 134 | return rv; |
| 135 | } |
| 136 | |
| 137 | template<typename T, int Step> |
| 138 | template<typename T1> |
| 139 | T *QRecyclePool<T, Step>::New(const T1 &a) |
| 140 | { |
| 141 | T *rv = d->allocate(); |
| 142 | new (rv) T(a); |
| 143 | return rv; |
| 144 | } |
| 145 | |
| 146 | template<typename T, int Step> |
| 147 | template<typename T1> |
| 148 | T *QRecyclePool<T, Step>::New(T1 &a) |
| 149 | { |
| 150 | T *rv = d->allocate(); |
| 151 | new (rv) T(a); |
| 152 | return rv; |
| 153 | } |
| 154 | |
| 155 | template<typename T, int Step> |
| 156 | void QRecyclePool<T, Step>::Delete(T *t) |
| 157 | { |
| 158 | t->~T(); |
| 159 | QRecyclePoolPrivate<T, Step>::dispose(t); |
| 160 | } |
| 161 | |
| 162 | template<typename T, int Step> |
| 163 | void QRecyclePoolPrivate<T, Step>::releaseIfPossible() |
| 164 | { |
| 165 | if (recyclePoolHold || outstandingItems) |
| 166 | return; |
| 167 | |
| 168 | Page *p = currentPage; |
| 169 | while (p) { |
| 170 | Page *n = p->nextPage; |
| 171 | free(p); |
| 172 | p = n; |
| 173 | } |
| 174 | |
| 175 | delete this; |
| 176 | } |
| 177 | |
| 178 | template<typename T, int Step> |
| 179 | T *QRecyclePoolPrivate<T, Step>::allocate() |
| 180 | { |
| 181 | PoolType *rv = nullptr; |
| 182 | if (nextAllocated) { |
| 183 | rv = nextAllocated; |
| 184 | nextAllocated = rv->nextAllocated; |
| 185 | } else if (currentPage && currentPage->free) { |
| 186 | rv = (PoolType *)(currentPage->array + (Step - currentPage->free) * sizeof(PoolType)); |
| 187 | currentPage->free--; |
| 188 | } else { |
| 189 | Page *p = (Page *)malloc(size: sizeof(Page)); |
| 190 | p->nextPage = currentPage; |
| 191 | p->free = Step; |
| 192 | currentPage = p; |
| 193 | |
| 194 | rv = (PoolType *)currentPage->array; |
| 195 | currentPage->free--; |
| 196 | } |
| 197 | |
| 198 | rv->pool = this; |
| 199 | ++outstandingItems; |
| 200 | return rv; |
| 201 | } |
| 202 | |
| 203 | template<typename T, int Step> |
| 204 | void QRecyclePoolPrivate<T, Step>::dispose(T *t) |
| 205 | { |
| 206 | PoolType *pt = static_cast<PoolType *>(t); |
| 207 | Q_ASSERT(pt->pool && pt->pool->cookie == QRECYCLEPOOLCOOKIE); |
| 208 | |
| 209 | QRecyclePoolPrivate<T, Step> *This = pt->pool; |
| 210 | pt->nextAllocated = This->nextAllocated; |
| 211 | This->nextAllocated = pt; |
| 212 | --This->outstandingItems; |
| 213 | This->releaseIfPossible(); |
| 214 | } |
| 215 | |
| 216 | QT_END_NAMESPACE |
| 217 | |
| 218 | #endif // QRECYCLEPOOL_P_H |
| 219 | |