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
24QT_BEGIN_NAMESPACE
25
26namespace QV4 {
27
28namespace Heap {
29
30template <typename T, size_t o>
31struct Pointer {
32 static constexpr size_t offset = o;
33 T operator->() const { return get(); }
34 operator T () const { return get(); }
35
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
49private:
50 Base *ptr;
51};
52typedef Pointer<char *, 0> V4PointerCheck;
55
56struct Q_QML_EXPORT Base {
57 void *operator new(size_t) = delete;
58
59 static void markObjects(Base *, MarkStack *);
60
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();
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();
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();
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};
93static_assert(std::is_trivially_copyable_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.
98static_assert(std::is_standard_layout<Base>::value);
99static_assert(offsetof(Base, internalClass) == 0);
100static_assert(sizeof(Base) == QT_POINTER_SIZE);
101
102inline
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();
112 if (!(*bitmap & bit)) {
113 *bitmap |= bit;
114 markStack->push(this);
115 }
116}
117
118template<typename T, size_t o>
119Base *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
128template <class T>
129struct QV4QPointer {
130};
131#else
132template <class T>
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(); }
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
181private:
182 QtSharedPointer::ExternalRefCountData *d;
183 T *qObject;
184};
187#endif
188
189}
190
191QT_END_NAMESPACE
192
193#endif
Pointer< char *, 0 > V4PointerCheck
Definition qv4heap_p.h:52
Definition qjsvalue.h:24
Base * heapObject() const
Definition qv4heap_p.h:47
void set(EngineBase *e, T newVal)
Definition qv4heap_p.h:38
T operator->() const
Definition qv4heap_p.h:33
static constexpr size_t offset
Definition qv4heap_p.h:32
bool isValid() const noexcept
Definition qv4heap_p.h:176
void init(T *o)
Definition qv4heap_p.h:140
T * data() const
Definition qv4heap_p.h:158
T * operator->() const
Definition qv4heap_p.h:162
operator T*() const
Definition qv4heap_p.h:161
bool isNull() const noexcept
Definition qv4heap_p.h:171
QV4QPointer & operator=(T *o)
Definition qv4heap_p.h:163