Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4heap_p.h
Go to the documentation of this file.
1
// Copyright (C) 2016 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
// Qt-Security score:critical reason:low-level-memory-management
4
#
ifndef
QV4HEAP_P_H
5
#
define
QV4HEAP_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
<
private
/
qv4global_p
.
h
>
19
#
include
<
private
/
qv4mmdefs_p
.
h
>
20
#
include
<
private
/
qv4writebarrier_p
.
h
>
21
#
include
<
private
/
qv4vtable_p
.
h
>
22
#
include
<
QtCore
/
QSharedPointer
>
23
24
QT_BEGIN_NAMESPACE
25
26
namespace
QV4
{
27
28
namespace
Heap
{
29
30
template
<
typename
T, size_t o>
31
struct
Pointer
{
32
static
constexpr
size_t
offset
= o;
33
T
operator
->()
const
{
return
get
(
)
; }
34
operator
T ()
const
{
return
get
(
)
; }
35
36
Base
*
base
();
37
38
void
set
(EngineBase *e, T newVal) {
39
WriteBarrier::write(e, base(), &ptr,
reinterpret_cast
<Base *>(newVal));
40
}
41
42
T
get
()
const
{
return
reinterpret_cast
<T>(ptr); }
43
44
template
<
typename
Type>
45
Type *
cast
() {
return
static_cast
<Type *>(ptr); }
46
47
Base
*
heapObject
()
const
{
return
ptr; }
48
49
private
:
50
Base *ptr;
51
};
52
typedef
Pointer
<
char
*, 0>
V4PointerCheck
;
53
static_assert
(
std
::
is_trivially_copyable_v
<
V4PointerCheck
>);
54
static_assert
(
std
::
is_trivially_default_constructible_v
<
V4PointerCheck
>);
55
56
struct
Q_QML_EXPORT
Base
{
57
void
*
operator
new
(
size_t
) =
delete
;
58
59
static
void
markObjects
(
Base
*,
MarkStack
*);
60
61
Pointer
<
InternalClass
*, 0>
internalClass
;
62
63
inline
ReturnedValue
asReturnedValue
()
const
;
64
inline
void
mark
(
QV4
::
MarkStack
*
markStack
);
65
66
inline
bool
isMarked
()
const
{
67
const
HeapItem
*
h
=
reinterpret_cast
<
const
HeapItem
*>(
this
);
68
Chunk
*
c
=
h
->
chunk
();
69
Q_ASSERT
(!
Chunk
::
testBit
(
c
->
extendsBitmap
,
h
-
c
->
realBase
()));
70
return
Chunk
::
testBit
(
c
->
blackBitmap
,
h
-
c
->
realBase
());
71
}
72
inline
void
setMarkBit
() {
73
const
HeapItem
*
h
=
reinterpret_cast
<
const
HeapItem
*>(
this
);
74
Chunk
*
c
=
h
->
chunk
();
75
Q_ASSERT
(!
Chunk
::
testBit
(
c
->
extendsBitmap
,
h
-
c
->
realBase
()));
76
return
Chunk
::
setBit
(
c
->
blackBitmap
,
h
-
c
->
realBase
());
77
}
78
79
inline
bool
inUse
()
const
{
80
const
HeapItem
*
h
=
reinterpret_cast
<
const
HeapItem
*>(
this
);
81
Chunk
*
c
=
h
->
chunk
();
82
Q_ASSERT
(!
Chunk
::
testBit
(
c
->
extendsBitmap
,
h
-
c
->
realBase
()));
83
return
Chunk
::
testBit
(
c
->
objectBitmap
,
h
-
c
->
realBase
());
84
}
85
86
void
*
operator
new
(
size_t
,
Managed
*
m
) {
return
m
; }
87
void
*
operator
new
(
size_t
,
Base
*
m
) {
return
m
; }
88
void
operator
delete
(
void
*,
Base
*) {}
89
90
void
init
() {}
91
void
destroy
() {}
92
};
93
static_assert
(
std
::
is_trivially_copyable_v
<
Base
>);
94
static_assert
(
std
::
is_trivially_default_constructible_v
<
Base
>);
95
// This class needs to consist only of pointer sized members to allow
96
// for a size/offset translation when cross-compiling between 32- and
97
// 64-bit.
98
static_assert
(
std
::
is_standard_layout
<
Base
>::
value
);
99
static_assert
(offsetof(Base, internalClass) == 0);
100
static_assert
(
sizeof
(
Base
) ==
QT_POINTER_SIZE
);
101
102
inline
103
void
Base
::
mark
(
QV4
::
MarkStack
*
markStack
)
104
{
105
Q_ASSERT
(
inUse
());
106
const
HeapItem
*
h
=
reinterpret_cast
<
const
HeapItem
*>(
this
);
107
Chunk
*
c
=
h
->
chunk
();
108
size_t
index
=
h
-
c
->
realBase
();
109
Q_ASSERT
(!
Chunk
::
testBit
(
c
->
extendsBitmap
,
index
));
110
quintptr
*
bitmap
=
c
->
blackBitmap
+
Chunk
::
bitmapIndex
(
index
);
111
quintptr
bit
=
Chunk
::
bitForIndex
(
index
);
112
if
(!(*
bitmap
&
bit
)) {
113
*
bitmap
|=
bit
;
114
markStack
->
push
(
this
);
115
}
116
}
117
118
template
<
typename
T, size_t o>
119
Base
*
Pointer
<T, o>::
base
() {
120
Base *base =
reinterpret_cast
<Base *>(
this
) - (offset/
sizeof
(Base *));
121
Q_ASSERT(base->inUse());
122
return
base;
123
}
124
125
}
126
127
#
ifdef
QT_NO_QOBJECT
128
template
<
class
T
>
129
struct
QV4QPointer
{
130
};
131
#
else
132
template
<
class
T>
133
struct
QV4QPointer
{
134
void
init
()
135
{
136
d =
nullptr
;
137
qObject =
nullptr
;
138
}
139
140
void
init
(T *o)
141
{
142
Q_ASSERT(d ==
nullptr
);
143
Q_ASSERT(qObject ==
nullptr
);
144
if
(o) {
145
d = QtSharedPointer::ExternalRefCountData::getAndRef(o);
146
qObject = o;
147
}
148
}
149
150
void
destroy
()
151
{
152
if
(d && !d->weakref.deref())
153
delete
d;
154
d =
nullptr
;
155
qObject =
nullptr
;
156
}
157
158
T *
data
()
const
{
159
return
d ==
nullptr
|| d->strongref.loadRelaxed() == 0 ?
nullptr
: qObject;
160
}
161
operator
T*()
const
{
return
data
(
)
; }
162
inline
T*
operator
->()
const
{
return
data
(
)
; }
163
QV4QPointer
&
operator
=(T *o)
164
{
165
if
(d)
166
destroy
(
)
;
167
init(o);
168
return
*
this
;
169
}
170
171
bool
isNull
()
const
noexcept
172
{
173
return
!isValid() || d->strongref.loadRelaxed() == 0;
174
}
175
176
bool
isValid
()
const
noexcept
177
{
178
return
d !=
nullptr
&& qObject !=
nullptr
;
179
}
180
181
private
:
182
QtSharedPointer::ExternalRefCountData *d;
183
T *qObject;
184
};
185
static_assert
(
std
::
is_trivially_copyable_v
<
QV4QPointer
<
QObject
>>);
186
static_assert
(
std
::
is_trivially_default_constructible_v
<
QV4QPointer
<
QObject
>>);
187
#
endif
188
189
}
190
191
QT_END_NAMESPACE
192
193
#
endif
QV4::Heap
Definition
qv4compileddata_p.h:75
QV4::Heap::V4PointerCheck
Pointer< char *, 0 > V4PointerCheck
Definition
qv4heap_p.h:52
QV4
Definition
qjsvalue.h:24
QV4::Heap::Base
Definition
qv4heap_p.h:56
QV4::Heap::Pointer
Definition
qv4heap_p.h:31
QV4::Heap::Pointer::heapObject
Base * heapObject() const
Definition
qv4heap_p.h:47
QV4::Heap::Pointer::base
Base * base()
Definition
qv4heap_p.h:119
QV4::Heap::Pointer::cast
Type * cast()
Definition
qv4heap_p.h:45
QV4::Heap::Pointer::set
void set(EngineBase *e, T newVal)
Definition
qv4heap_p.h:38
QV4::Heap::Pointer::operator->
T operator->() const
Definition
qv4heap_p.h:33
QV4::Heap::Pointer::get
T get() const
Definition
qv4heap_p.h:42
QV4::Heap::Pointer::offset
static constexpr size_t offset
Definition
qv4heap_p.h:32
QV4::Heap::Pointer::operator T
operator T() const
Definition
qv4heap_p.h:34
QV4::QV4QPointer
Definition
qv4heap_p.h:133
QV4::QV4QPointer::init
void init()
Definition
qv4heap_p.h:134
QV4::QV4QPointer::isValid
bool isValid() const noexcept
Definition
qv4heap_p.h:176
QV4::QV4QPointer::destroy
void destroy()
Definition
qv4heap_p.h:150
QV4::QV4QPointer::init
void init(T *o)
Definition
qv4heap_p.h:140
QV4::QV4QPointer::data
T * data() const
Definition
qv4heap_p.h:158
QV4::QV4QPointer::operator->
T * operator->() const
Definition
qv4heap_p.h:162
QV4::QV4QPointer::operator T*
operator T*() const
Definition
qv4heap_p.h:161
QV4::QV4QPointer::isNull
bool isNull() const noexcept
Definition
qv4heap_p.h:171
QV4::QV4QPointer::operator=
QV4QPointer & operator=(T *o)
Definition
qv4heap_p.h:163
qtdeclarative
src
qml
memory
qv4heap_p.h
Generated on
for Qt by
1.16.1