Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qrecyclepool_p.h
Go to the documentation of this file.
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
#include <QtCore/q20memory.h>
21
22
QT_BEGIN_NAMESPACE
23
24
#define QRECYCLEPOOLCOOKIE 0x33218ADF
25
26
template
<
typename
T,
int
Step>
27
class
QRecyclePoolPrivate
28
{
29
public
:
30
QRecyclePoolPrivate
()
31
:
recyclePoolHold
(true),
outstandingItems
(0),
cookie
(
QRECYCLEPOOLCOOKIE
),
32
currentPage
(
nullptr
),
nextAllocated
(
nullptr
)
33
{
34
}
35
36
bool
recyclePoolHold
;
37
int
outstandingItems
;
38
quint32
cookie
;
39
40
struct
PoolType
:
public
T {
41
union
{
42
QRecyclePoolPrivate<T, Step> *
pool
;
43
PoolType
*
nextAllocated
;
44
};
45
};
46
47
struct
Page
{
48
Page
*
nextPage
;
49
unsigned
int
free
;
50
union
{
51
char
array
[Step *
sizeof
(
PoolType
)];
52
qint64
q_for_alignment_1
;
53
double
q_for_alignment_2
;
54
};
55
};
56
57
Page
*
currentPage
;
58
PoolType
*
nextAllocated
;
59
60
inline
T *
allocate
();
61
static
inline
void
dispose
(T *);
62
inline
void
releaseIfPossible
();
63
};
64
65
template
<
typename
T,
int
Step = 1024>
66
class
QRecyclePool
67
{
68
public
:
69
inline
QRecyclePool
();
70
inline
~QRecyclePool
();
71
72
template
<
typename
...Args>
73
[[nodiscard]]
inline
T *
New
(Args&&...
args
);
74
75
static
inline
void
Delete
(T *);
76
77
private
:
78
QRecyclePoolPrivate<T, Step> *d;
79
};
80
81
template
<
typename
T,
int
Step>
82
QRecyclePool<T, Step>::QRecyclePool
()
83
:
d
(new
QRecyclePoolPrivate
<T, Step>())
84
{
85
}
86
87
template
<
typename
T,
int
Step>
88
QRecyclePool<T, Step>::~QRecyclePool
()
89
{
90
d
->recyclePoolHold =
false
;
91
d
->releaseIfPossible();
92
}
93
94
template
<
typename
T,
int
Step>
95
template
<
typename
...Args>
96
T *
QRecyclePool<T, Step>::New
(Args&&...
args
)
97
{
98
return
q20::construct_at
(
d
->allocate(), std::forward<Args>(
args
)...);
99
}
100
101
template
<
typename
T,
int
Step>
102
void
QRecyclePool<T, Step>::Delete
(T *
t
)
103
{
104
t
->~T();
105
QRecyclePoolPrivate<T, Step>::dispose
(
t
);
106
}
107
108
template
<
typename
T,
int
Step>
109
void
QRecyclePoolPrivate<T, Step>::releaseIfPossible
()
110
{
111
if
(recyclePoolHold || outstandingItems)
112
return
;
113
114
Page
*
p
= currentPage;
115
while
(
p
) {
116
Page
*
n
=
p
->
nextPage
;
117
free(
p
);
118
p
=
n
;
119
}
120
121
delete
this
;
122
}
123
124
template
<
typename
T,
int
Step>
125
T *
QRecyclePoolPrivate<T, Step>::allocate
()
126
{
127
PoolType
*rv =
nullptr
;
128
if
(nextAllocated) {
129
rv = nextAllocated;
130
nextAllocated = rv->
nextAllocated
;
131
}
else
if
(currentPage && currentPage->free) {
132
rv = (
PoolType
*)(currentPage->array + (Step - currentPage->free) *
sizeof
(
PoolType
));
133
currentPage->free--;
134
}
else
{
135
Page
*
p
= (
Page
*)malloc(
sizeof
(
Page
));
136
p
->
nextPage
= currentPage;
137
p
->
free
= Step;
138
currentPage =
p
;
139
140
rv = (
PoolType
*)currentPage->array;
141
currentPage->free--;
142
}
143
144
rv->pool =
this
;
145
++outstandingItems;
146
return
rv;
147
}
148
149
template
<
typename
T,
int
Step>
150
void
QRecyclePoolPrivate<T, Step>::dispose
(T *
t
)
151
{
152
PoolType
*pt =
static_cast<
PoolType
*
>
(
t
);
153
Q_ASSERT
(pt->
pool
&& pt->
pool
->cookie ==
QRECYCLEPOOLCOOKIE
);
154
155
QRecyclePoolPrivate<T, Step> *This = pt->
pool
;
156
pt->
nextAllocated
= This->nextAllocated;
157
This->nextAllocated = pt;
158
--This->outstandingItems;
159
This->releaseIfPossible();
160
}
161
162
QT_END_NAMESPACE
163
164
#endif
// QRECYCLEPOOL_P_H
QRecyclePoolPrivate
Definition
qrecyclepool_p.h:28
QRecyclePoolPrivate::recyclePoolHold
bool recyclePoolHold
Definition
qrecyclepool_p.h:36
QRecyclePoolPrivate::cookie
quint32 cookie
Definition
qrecyclepool_p.h:38
QRecyclePoolPrivate::nextAllocated
PoolType * nextAllocated
Definition
qrecyclepool_p.h:58
QRecyclePoolPrivate::outstandingItems
int outstandingItems
Definition
qrecyclepool_p.h:37
QRecyclePoolPrivate::currentPage
Page * currentPage
Definition
qrecyclepool_p.h:57
QRecyclePoolPrivate::allocate
T * allocate()
Definition
qrecyclepool_p.h:125
QRecyclePoolPrivate::dispose
static void dispose(T *)
Definition
qrecyclepool_p.h:150
QRecyclePoolPrivate::QRecyclePoolPrivate
QRecyclePoolPrivate()
Definition
qrecyclepool_p.h:30
QRecyclePoolPrivate::releaseIfPossible
void releaseIfPossible()
Definition
qrecyclepool_p.h:109
QRecyclePool
Definition
qrecyclepool_p.h:67
QRecyclePool::~QRecyclePool
~QRecyclePool()
Definition
qrecyclepool_p.h:88
QRecyclePool::QRecyclePool
QRecyclePool()
Definition
qrecyclepool_p.h:82
QRecyclePool::Delete
static void Delete(T *)
Definition
qrecyclepool_p.h:102
QRecyclePool::New
T * New(Args &&...args)
Definition
qrecyclepool_p.h:96
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qstandardpaths_haiku.cpp:21
QT_END_NAMESPACE
Definition
qsharedpointer.cpp:1590
q20::construct_at
T * construct_at(T *ptr, Args &&... args)
Definition
q20memory.h:41
n
GLfloat n
Definition
qopengles2ext.h:795
array
GLenum array
Definition
qopenglext.h:7028
t
GLdouble GLdouble t
Definition
qopenglext.h:243
p
GLfloat GLfloat p
[1]
Definition
qopenglext.h:12698
Q_ASSERT
#define Q_ASSERT(cond)
Definition
qrandom.cpp:47
QRECYCLEPOOLCOOKIE
#define QRECYCLEPOOLCOOKIE
Definition
qrecyclepool_p.h:24
quint32
unsigned int quint32
Definition
qtypes.h:50
qint64
long long qint64
Definition
qtypes.h:60
nullptr
QObject::connect nullptr
Definition
src_corelib_kernel_qobject.cpp:255
d
double d
Definition
src_corelib_text_qlocale.cpp:9
args
QJSValueList args
Definition
src_script_qjsengine.cpp:12
QRecyclePoolPrivate::Page
Definition
qrecyclepool_p.h:47
QRecyclePoolPrivate::Page::q_for_alignment_2
double q_for_alignment_2
Definition
qrecyclepool_p.h:53
QRecyclePoolPrivate::Page::nextPage
Page * nextPage
Definition
qrecyclepool_p.h:48
QRecyclePoolPrivate::Page::free
unsigned int free
Definition
qrecyclepool_p.h:49
QRecyclePoolPrivate::Page::q_for_alignment_1
qint64 q_for_alignment_1
Definition
qrecyclepool_p.h:52
QRecyclePoolPrivate::PoolType
Definition
qrecyclepool_p.h:40
QRecyclePoolPrivate::PoolType::nextAllocated
PoolType * nextAllocated
Definition
qrecyclepool_p.h:43
QRecyclePoolPrivate::PoolType::pool
QRecyclePoolPrivate< T, Step > * pool
Definition
qrecyclepool_p.h:42
qtdeclarative
src
qml
qml
ftw
qrecyclepool_p.h
Generated by
1.10.0