1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QRECYCLEPOOL_P_H |
5 | #define QRECYCLEPOOL_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/private/qglobal_p.h> |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | #define QRECYCLEPOOLCOOKIE 0x33218ADF |
23 | |
24 | template<typename T, int Step> |
25 | class QRecyclePoolPrivate |
26 | { |
27 | public: |
28 | QRecyclePoolPrivate() |
29 | : recyclePoolHold(true), outstandingItems(0), cookie(QRECYCLEPOOLCOOKIE), |
30 | currentPage(nullptr), nextAllocated(nullptr) |
31 | { |
32 | } |
33 | |
34 | bool recyclePoolHold; |
35 | int outstandingItems; |
36 | quint32 cookie; |
37 | |
38 | struct PoolType : public T { |
39 | union { |
40 | QRecyclePoolPrivate<T, Step> *pool; |
41 | PoolType *nextAllocated; |
42 | }; |
43 | }; |
44 | |
45 | struct Page { |
46 | Page *nextPage; |
47 | unsigned int free; |
48 | union { |
49 | char array[Step * sizeof(PoolType)]; |
50 | qint64 q_for_alignment_1; |
51 | double q_for_alignment_2; |
52 | }; |
53 | }; |
54 | |
55 | Page *currentPage; |
56 | PoolType *nextAllocated; |
57 | |
58 | inline T *allocate(); |
59 | static inline void dispose(T *); |
60 | inline void releaseIfPossible(); |
61 | }; |
62 | |
63 | template<typename T, int Step = 1024> |
64 | class QRecyclePool |
65 | { |
66 | public: |
67 | inline QRecyclePool(); |
68 | inline ~QRecyclePool(); |
69 | |
70 | inline T *New() = delete; |
71 | template<typename T1> |
72 | inline T *New(const T1 &); |
73 | template<typename T1> |
74 | inline T *New(T1 &); |
75 | |
76 | static inline void Delete(T *); |
77 | |
78 | private: |
79 | QRecyclePoolPrivate<T, Step> *d; |
80 | }; |
81 | |
82 | template<typename T, int Step> |
83 | QRecyclePool<T, Step>::QRecyclePool() |
84 | : d(new QRecyclePoolPrivate<T, Step>()) |
85 | { |
86 | } |
87 | |
88 | template<typename T, int Step> |
89 | QRecyclePool<T, Step>::~QRecyclePool() |
90 | { |
91 | d->recyclePoolHold = false; |
92 | d->releaseIfPossible(); |
93 | } |
94 | |
95 | template<typename T, int Step> |
96 | template<typename T1> |
97 | T *QRecyclePool<T, Step>::New(const T1 &a) |
98 | { |
99 | T *rv = d->allocate(); |
100 | return new (rv) T(a); |
101 | } |
102 | |
103 | template<typename T, int Step> |
104 | template<typename T1> |
105 | T *QRecyclePool<T, Step>::New(T1 &a) |
106 | { |
107 | T *rv = d->allocate(); |
108 | return new (rv) T(a); |
109 | } |
110 | |
111 | template<typename T, int Step> |
112 | void QRecyclePool<T, Step>::Delete(T *t) |
113 | { |
114 | t->~T(); |
115 | QRecyclePoolPrivate<T, Step>::dispose(t); |
116 | } |
117 | |
118 | template<typename T, int Step> |
119 | void QRecyclePoolPrivate<T, Step>::releaseIfPossible() |
120 | { |
121 | if (recyclePoolHold || outstandingItems) |
122 | return; |
123 | |
124 | Page *p = currentPage; |
125 | while (p) { |
126 | Page *n = p->nextPage; |
127 | free(p); |
128 | p = n; |
129 | } |
130 | |
131 | delete this; |
132 | } |
133 | |
134 | template<typename T, int Step> |
135 | T *QRecyclePoolPrivate<T, Step>::allocate() |
136 | { |
137 | PoolType *rv = nullptr; |
138 | if (nextAllocated) { |
139 | rv = nextAllocated; |
140 | nextAllocated = rv->nextAllocated; |
141 | } else if (currentPage && currentPage->free) { |
142 | rv = (PoolType *)(currentPage->array + (Step - currentPage->free) * sizeof(PoolType)); |
143 | currentPage->free--; |
144 | } else { |
145 | Page *p = (Page *)malloc(size: sizeof(Page)); |
146 | p->nextPage = currentPage; |
147 | p->free = Step; |
148 | currentPage = p; |
149 | |
150 | rv = (PoolType *)currentPage->array; |
151 | currentPage->free--; |
152 | } |
153 | |
154 | rv->pool = this; |
155 | ++outstandingItems; |
156 | return rv; |
157 | } |
158 | |
159 | template<typename T, int Step> |
160 | void QRecyclePoolPrivate<T, Step>::dispose(T *t) |
161 | { |
162 | PoolType *pt = static_cast<PoolType *>(t); |
163 | Q_ASSERT(pt->pool && pt->pool->cookie == QRECYCLEPOOLCOOKIE); |
164 | |
165 | QRecyclePoolPrivate<T, Step> *This = pt->pool; |
166 | pt->nextAllocated = This->nextAllocated; |
167 | This->nextAllocated = pt; |
168 | --This->outstandingItems; |
169 | This->releaseIfPossible(); |
170 | } |
171 | |
172 | QT_END_NAMESPACE |
173 | |
174 | #endif // QRECYCLEPOOL_P_H |
175 | |