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
qv4persistent.cpp
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:significant
4
6#include <private/qv4mm_p.h>
7#include "qv4object_p.h"
9#include "PageAllocation.h"
10
12
13namespace QV4 {
14
15struct Page;
16struct Header {
17 WTF::PageAllocation alloc;
18 ExecutionEngine *engine;
19 PersistentValueStorage *storage;
25};
26
27static const int kEntriesPerPage = int((WTF::pageSize() - sizeof(Header)) / sizeof(Value));
28
29struct Page {
31 Value values[1]; // Really kEntriesPerPage, but keep the compiler happy
32};
33
34Page *getPage(const Value *val) {
35 return reinterpret_cast<Page *>(reinterpret_cast<quintptr>(val) & ~((quintptr)(WTF::pageSize() - 1)));
36}
37
38QML_NEARLY_ALWAYS_INLINE void insertInFront(PersistentValueStorage *storage, Page *p)
39{
40 p->header.next = storage->firstPage;
41 p->header.prev = &storage->firstPage;
42 if (p->header.next)
43 p->header.next->header.prev = &p->header.next;
44 storage->firstPage = p;
45}
46
48{
49 if (p->header.prev)
50 *p->header.prev = p->header.next;
51 if (p->header.next)
52 p->header.next->header.prev = p->header.prev;
53}
54
55Page *allocatePage(PersistentValueStorage *storage)
56{
57 PageAllocation page = WTF::PageAllocation::allocate(WTF::pageSize());
58 Page *p = reinterpret_cast<Page *>(page.base());
59
60 Q_ASSERT(!((quintptr)p & (WTF::pageSize() - 1)));
61
62 p->header.engine = storage->engine;
63 p->header.storage = storage;
64 p->header.alloc = page;
65 p->header.refCount = 0;
66 p->header.freeList = 0;
67 insertInFront(storage, p);
68 p->header.nextPartial = storage->firstPartialPage;
69 storage->firstPartialPage = p;
70 for (int i = 0; i < kEntriesPerPage - 1; ++i) {
71 p->values[i] = Encode(i + 1);
72 }
73 p->values[kEntriesPerPage - 1] = Encode(-1);
74
75 return p;
76}
77
78static void releasePage(Page *p)
79{
80 if (--p->header.refCount)
81 return;
82
83 // Dangling pages aren't handled by deallocateEmptyPages(). These are pages that outlive
84 // their PersistentValueStorage. We have to deallocate them right away.
85 if (!p->header.storage) {
86 unlink(p);
87 p->header.alloc.deallocate();
88 }
89}
90
92 : p(p), index(idx)
93{
94 if (p)
95 ++p->header.refCount;
96}
97
104
106{
107 if (p)
108 releasePage(p);
109 p = o.p;
110 index = o.index;
111 if (p)
112 ++p->header.refCount;
113
114 return *this;
115}
116
122
124 while (p) {
125 while (index < kEntriesPerPage - 1) {
126 ++index;
127 if (!p->values[index].isEmpty())
128 return *this;
129 }
130 index = -1;
131 Page *next = p->header.next;
132 releasePage(p);
133 p = next;
134 if (next)
136 }
137 index = 0;
138 return *this;
139}
140
145
151
153{
155 for (Page *p = firstPage; p;) {
156 for (int i = 0; i < kEntriesPerPage; ++i) {
157 if (!p->values[i].isEmpty())
158 p->values[i] = Encode::undefined();
159 }
160 Page *n = p->header.next;
161 p->header.engine = nullptr;
162 p->header.storage = nullptr;
163 p->header.prev = nullptr;
164 p->header.next = nullptr;
165 p->header.nextPartial = nullptr;
167 p = n;
168 }
169}
170
172{
174 if (!p)
175 p = allocatePage(this);
176
177 Value *v = p->values + p->header.freeList;
178 p->header.freeList = v->int_32();
179 if (p->header.freeList == -1)
181
182 ++p->header.refCount;
183
185
186 return v;
187}
188
190{
191 Q_ASSERT(v);
192 Page *p = getPage(v);
193
194 // If the page was full, a first entry is freed up here. Link the page into
195 // the list of partial pages.
196 // It's generally possible to encounter a "floating" page, though. The
197 // PersistentValueStorage dtor has provisions for keeping non-empty pages
198 // around, and PersistenValueStorage::free() is static. Such a page cannot
199 // be linked into the list of partial pages anymore.
200 if (PersistentValueStorage *storage = (p->header.freeList == -1) ? p->header.storage : nullptr)
202
203 *v = Encode(p->header.freeList);
204 p->header.freeList = v - p->values;
205 releasePage(p);
206}
207
209{
210 for (Page *p = firstPage; p; p = p->header.next) {
211 for (int i = 0; i < kEntriesPerPage; ++i) {
212 if (Managed *m = p->values[i].as<Managed>())
213 m->mark(markStack);
214 }
215 }
216}
217
218/*!
219 \internal
220 Unmaps pages that hold neither values nor iterators. Pages that become empty
221 are not unmapped right away, so that allocating and freeing a value in a
222 loop doesn't map and unmap a page each time. Instead, the garbage collector
223 calls this at the end of each cycle.
224*/
226{
228
229 while (Page *p = *link) {
230 // refCount == 0 coincides with being empty here, as nothing should hold an iterator
231 // to a page at this point
232 if (p->header.refCount) {
234 continue;
235 }
236
238 unlink(p);
240 }
241}
242
247
249 : val(nullptr)
250{
251 if (other.val)
252 set(other.engine(), *other.val);
253}
254
259
264
266 : val(nullptr)
267{
268 if (!object)
269 return;
270 set(engine, *object);
271}
272
274{
275 if (!val) {
276 if (!other.val)
277 return *this;
279 }
280 if (!other.val) {
281 *val = Encode::undefined();
282 return *this;
283 }
284
285 Q_ASSERT(engine() == other.engine());
286
287 *val = *other.val;
288 return *this;
289}
290
292{
293 if (!val && !other.valueRef())
294 return *this;
295 if (!other.valueRef()) {
296 *val = Encode::undefined();
297 return *this;
298 }
299
300 Q_ASSERT(!engine() || engine() == other.engine());
301
303 return *this;
304}
305
307{
308 if (!object) {
310 return *this;
311 }
312 set(object->engine(), *object);
313 return *this;
314}
315
326
340
352
354 : val(nullptr)
355{
356 if (other.val) {
358 *val = *other.val;
359 }
360}
361
367
369{
370 if (!val) {
371 if (!other.val)
372 return *this;
374 }
375 if (!other.val) {
376 *val = Encode::undefined();
377 return *this;
378 }
379
380 Q_ASSERT(engine() == other.engine());
381
382 *val = *other.val;
383 return *this;
384}
385
387{
388 free();
389}
390
391/*
392 WeakValue::set shold normally not mark objects, after all a weak value
393 is not supposed to keep an object alive.
394 However, if we are past GCState::HandleQObjectWrappers, nothing will
395 reset weak values referencing unmarked values, but those values will
396 still be swept.
397 That lead to stale pointers, and potentially to crashes. To avoid this,
398 we mark the objects here (they might still get collected in the next gc
399 run).
400 This is especially important due to the way we handle QObjectWrappers.
401 */
403{
404 if (!val)
408 return;
409 if (auto *h = value.heapObject())
410 h->mark(ms);
411 });
412 *val = value;
413}
414
416{
417 if (!val)
421 return;
422 if (auto *h = QV4::Value::fromReturnedValue(value).heapObject())
423 h->mark(ms);
424 });
425
426 *val = value;
427}
428
430{
431 if (!val)
435 return;
436 obj->mark(ms);
437 });
438 *val = obj;
439}
440
442{
444}
445
447{
448 if (!val)
449 return;
451}
452
453void WeakValue::free()
454{
455 if (!val)
456 return;
457
459 if (e && val->as<QObjectWrapper>()) {
460 // Some QV4::QObjectWrapper Value will be freed in WeakValue::~WeakValue() before MemoryManager::sweep() is being called,
461 // in this case we will never have a chance to call detroyObject() on those QV4::QObjectWrapper objects.
462 // Here we don't free these Value immediately, instead we keep track of them to free them later in MemoryManager::sweep()
464 } else {
466 }
467
468 val = nullptr;
469}
470
471} // namespace QV4
472
473QT_END_NAMESPACE
Combined button and popup list for selecting options.
Definition qjsvalue.h:24
QML_NEARLY_ALWAYS_INLINE void unlink(Page *p)
Page * allocatePage(PersistentValueStorage *storage)
Page * getPage(const Value *val)
static void releasePage(Page *p)
static const int kEntriesPerPage
QML_NEARLY_ALWAYS_INLINE void insertInFront(PersistentValueStorage *storage, Page *p)
WTF::PageAllocation alloc
ExecutionEngine * engine
PersistentValueStorage * storage
Value values[1]