Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
5
#
include
"qv4persistent_p.h"
6
#
include
<
private
/
qv4mm_p
.
h
>
7
#
include
"qv4object_p.h"
8
#
include
"qv4qobjectwrapper_p.h"
9
#
include
"PageAllocation.h"
10
11
QT_BEGIN_NAMESPACE
12
13
namespace
QV4
{
14
15
struct
Page
;
16
struct
Header
{
17
WTF
::PageAllocation
alloc
;
18
ExecutionEngine *
engine
;
19
PersistentValueStorage *
storage
;
20
Page
**
prev
;
21
Page
*
next
;
22
Page
*
nextPartial
;
23
int
refCount
;
24
int
freeList
;
25
};
26
27
static
const
int
kEntriesPerPage
=
int
((
WTF
::
pageSize
() -
sizeof
(
Header
)) /
sizeof
(
Value
));
28
29
struct
Page
{
30
Header
header
;
31
Value
values
[1];
// Really kEntriesPerPage, but keep the compiler happy
32
};
33
34
Page
*
getPage
(
const
Value *val) {
35
return
reinterpret_cast
<Page *>(
reinterpret_cast
<quintptr>(val) & ~((quintptr)(WTF::pageSize() - 1)));
36
}
37
38
QML_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
47
QML_NEARLY_ALWAYS_INLINE
void
unlink
(
Page
*p)
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
55
Page
*
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
78
static
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
91
PersistentValueStorage
::
Iterator
::
Iterator
(
Page
*
p
,
int
idx
)
92
:
p
(
p
),
index
(
idx
)
93
{
94
if
(
p
)
95
++
p
->
header
.
refCount
;
96
}
97
98
PersistentValueStorage
::
Iterator
::
Iterator
(
const
PersistentValueStorage
::
Iterator
&
o
)
99
:
p
(
o
.
p
),
index
(
o
.
index
)
100
{
101
if
(
p
)
102
++
p
->
header
.
refCount
;
103
}
104
105
PersistentValueStorage
::
Iterator
&
PersistentValueStorage
::
Iterator
::
operator
=(
const
PersistentValueStorage
::
Iterator
&
o
)
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
117
PersistentValueStorage
::
Iterator
::~
Iterator
()
118
{
119
if
(
p
)
120
releasePage
(
p
);
121
}
122
123
PersistentValueStorage
::
Iterator
&
PersistentValueStorage
::
Iterator
::
operator
++() {
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
)
135
++
next
->
header
.
refCount
;
136
}
137
index
= 0;
138
return
*
this
;
139
}
140
141
Value
&
PersistentValueStorage
::
Iterator
::
operator
*()
142
{
143
return
p
->
values
[
index
];
144
}
145
146
PersistentValueStorage
::
PersistentValueStorage
(
ExecutionEngine
*
engine
)
147
:
engine
(
engine
),
148
firstPage
(
nullptr
)
149
{
150
}
151
152
PersistentValueStorage
::~
PersistentValueStorage
()
153
{
154
deallocateEmptyPages
();
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
;
166
Q_ASSERT
(
p
->
header
.
refCount
);
167
p
=
n
;
168
}
169
}
170
171
Value
*
PersistentValueStorage
::
allocate
()
172
{
173
Page
*
p
=
firstPartialPage
;
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)
180
firstPartialPage
=
p
->
header
.
nextPartial
;
181
182
++
p
->
header
.
refCount
;
183
184
v
->
setRawValue
(
Encode
::
undefined
());
185
186
return
v
;
187
}
188
189
void
PersistentValueStorage
::
freeUnchecked
(
Value
*
v
)
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
)
201
p
->
header
.
nextPartial
=
std
::
exchange
(
storage
->
firstPartialPage
,
p
);
202
203
*
v
=
Encode
(
p
->
header
.
freeList
);
204
p
->
header
.
freeList
=
v
-
p
->
values
;
205
releasePage
(
p
);
206
}
207
208
void
PersistentValueStorage
::
mark
(
MarkStack
*
markStack
)
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
*/
225
void
PersistentValueStorage
::
deallocateEmptyPages
()
226
{
227
Page
**
link
= &
firstPartialPage
;
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
) {
233
link
= &
p
->
header
.
nextPartial
;
234
continue
;
235
}
236
237
*
link
=
p
->
header
.
nextPartial
;
238
unlink
(
p
);
239
p
->
header
.
alloc
.
deallocate
();
240
}
241
}
242
243
ExecutionEngine
*
PersistentValueStorage
::
getEngine
(
const
Value
*
v
)
244
{
245
return
getPage
(
v
)->
header
.
engine
;
246
}
247
248
PersistentValue
::
PersistentValue
(
const
PersistentValue
&
other
)
249
:
val
(
nullptr
)
250
{
251
if
(
other
.
val
)
252
set
(
other
.
engine
(), *
other
.
val
);
253
}
254
255
PersistentValue
::
PersistentValue
(
ExecutionEngine
*
engine
,
const
Value
&
value
)
256
{
257
set
(
engine
,
value
);
258
}
259
260
PersistentValue
::
PersistentValue
(
ExecutionEngine
*
engine
,
ReturnedValue
value
)
261
{
262
set
(
engine
,
value
);
263
}
264
265
PersistentValue
::
PersistentValue
(
ExecutionEngine
*
engine
,
Object
*
object
)
266
:
val
(
nullptr
)
267
{
268
if
(!
object
)
269
return
;
270
set
(
engine
, *
object
);
271
}
272
273
PersistentValue
&
PersistentValue
::
operator
=(
const
PersistentValue
&
other
)
274
{
275
if
(!
val
) {
276
if
(!
other
.
val
)
277
return
*
this
;
278
val
=
other
.
engine
()->
memoryManager
->
m_persistentValues
->
allocate
();
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
291
PersistentValue
&
PersistentValue
::
operator
=(
const
WeakValue
&
other
)
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
302
set
(
other
.
engine
(), *
other
.
valueRef
());
303
return
*
this
;
304
}
305
306
PersistentValue
&
PersistentValue
::
operator
=(
Object
*
object
)
307
{
308
if
(!
object
) {
309
PersistentValueStorage
::
free
(
val
);
310
return
*
this
;
311
}
312
set
(
object
->
engine
(), *
object
);
313
return
*
this
;
314
}
315
316
void
PersistentValue
::
set
(
ExecutionEngine
*
engine
,
const
Value
&
value
)
317
{
318
if
(!
val
)
319
val
=
engine
->
memoryManager
->
m_persistentValues
->
allocate
();
320
QV4
::
WriteBarrier
::
markCustom
(
engine
, [&](
QV4
::
MarkStack
*
stack
){
321
if
(
QV4
::
WriteBarrier
::
isInsertionBarrier
&&
value
.
isManaged
())
322
value
.
heapObject
()->
mark
(
stack
);
323
});
324
*
val
=
value
;
325
}
326
327
void
PersistentValue
::
set
(
ExecutionEngine
*
engine
,
ReturnedValue
value
)
328
{
329
if
(!
val
)
330
val
=
engine
->
memoryManager
->
m_persistentValues
->
allocate
();
331
QV4
::
WriteBarrier
::
markCustom
(
engine
, [&](
QV4
::
MarkStack
*
stack
){
332
if
constexpr
(!
QV4
::
WriteBarrier
::
isInsertionBarrier
)
333
return
;
334
auto
val
=
Value
::
fromReturnedValue
(
value
);
335
if
(
val
.
isManaged
())
336
val
.
heapObject
()->
mark
(
stack
);
337
});
338
*
val
=
value
;
339
}
340
341
void
PersistentValue
::
set
(
ExecutionEngine
*
engine
,
Heap
::
Base
*
obj
)
342
{
343
if
(!
val
)
344
val
=
engine
->
memoryManager
->
m_persistentValues
->
allocate
();
345
QV4
::
WriteBarrier
::
markCustom
(
engine
, [&](
QV4
::
MarkStack
*
stack
){
346
if
constexpr
(
QV4
::
WriteBarrier
::
isInsertionBarrier
)
347
obj
->
mark
(
stack
);
348
});
349
350
*
val
=
obj
;
351
}
352
353
WeakValue
::
WeakValue
(
const
WeakValue
&
other
)
354
:
val
(
nullptr
)
355
{
356
if
(
other
.
val
) {
357
allocVal
(
other
.
engine
());
358
*
val
= *
other
.
val
;
359
}
360
}
361
362
WeakValue
::
WeakValue
(
ExecutionEngine
*
engine
,
const
Value
&
value
)
363
{
364
allocVal
(
engine
);
365
*
val
=
value
;
366
}
367
368
WeakValue
&
WeakValue
::
operator
=(
const
WeakValue
&
other
)
369
{
370
if
(!
val
) {
371
if
(!
other
.
val
)
372
return
*
this
;
373
allocVal
(
other
.
engine
());
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
386
WeakValue
::~
WeakValue
()
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
*/
402
void
WeakValue
::
set
(
ExecutionEngine
*
engine
,
const
Value
&
value
)
403
{
404
if
(!
val
)
405
allocVal
(
engine
);
406
QV4
::
WriteBarrier
::
markCustom
(
engine
, [&](
QV4
::
MarkStack
*
ms
) {
407
if
(
engine
->
memoryManager
->
gcStateMachine
->
state
<=
GCState
::
HandleQObjectWrappers
)
408
return
;
409
if
(
auto
*
h
=
value
.
heapObject
())
410
h
->
mark
(
ms
);
411
});
412
*
val
=
value
;
413
}
414
415
void
WeakValue
::
set
(
ExecutionEngine
*
engine
,
ReturnedValue
value
)
416
{
417
if
(!
val
)
418
allocVal
(
engine
);
419
QV4
::
WriteBarrier
::
markCustom
(
engine
, [&](
QV4
::
MarkStack
*
ms
) {
420
if
(
engine
->
memoryManager
->
gcStateMachine
->
state
<=
GCState
::
HandleQObjectWrappers
)
421
return
;
422
if
(
auto
*
h
=
QV4
::
Value
::
fromReturnedValue
(
value
).
heapObject
())
423
h
->
mark
(
ms
);
424
});
425
426
*
val
=
value
;
427
}
428
429
void
WeakValue
::
set
(
ExecutionEngine
*
engine
,
Heap
::
Base
*
obj
)
430
{
431
if
(!
val
)
432
allocVal
(
engine
);
433
QV4
::
WriteBarrier
::
markCustom
(
engine
, [&](
QV4
::
MarkStack
*
ms
) {
434
if
(
engine
->
memoryManager
->
gcStateMachine
->
state
<=
GCState
::
HandleQObjectWrappers
)
435
return
;
436
obj
->
mark
(
ms
);
437
});
438
*
val
=
obj
;
439
}
440
441
void
WeakValue
::
allocVal
(
ExecutionEngine
*
engine
)
442
{
443
val
=
engine
->
memoryManager
->
m_weakValues
->
allocate
();
444
}
445
446
void
WeakValue
::
markOnce
(
MarkStack
*
markStack
)
447
{
448
if
(!
val
)
449
return
;
450
val
->
mark
(
markStack
);
451
}
452
453
void
WeakValue
::
free
()
454
{
455
if
(!
val
)
456
return
;
457
458
ExecutionEngine
*
e
=
engine
();
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()
463
e
->
memoryManager
->
m_pendingFreedObjectWrapperValue
.
push_back
(
val
);
464
}
else
{
465
PersistentValueStorage
::
free
(
val
);
466
}
467
468
val
=
nullptr
;
469
}
470
471
}
// namespace QV4
472
473
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QV4
Definition
qjsvalue.h:24
QV4::unlink
QML_NEARLY_ALWAYS_INLINE void unlink(Page *p)
Definition
qv4persistent.cpp:47
QV4::allocatePage
Page * allocatePage(PersistentValueStorage *storage)
Definition
qv4persistent.cpp:55
QV4::getPage
Page * getPage(const Value *val)
Definition
qv4persistent.cpp:34
QV4::releasePage
static void releasePage(Page *p)
Definition
qv4persistent.cpp:78
QV4::kEntriesPerPage
static const int kEntriesPerPage
Definition
qv4persistent.cpp:27
QV4::insertInFront
QML_NEARLY_ALWAYS_INLINE void insertInFront(PersistentValueStorage *storage, Page *p)
Definition
qv4persistent.cpp:38
WTF
Definition
qv4engine_p.h:33
QV4::Header
Definition
qv4persistent.cpp:16
QV4::Header::alloc
WTF::PageAllocation alloc
Definition
qv4persistent.cpp:17
QV4::Header::refCount
int refCount
Definition
qv4persistent.cpp:23
QV4::Header::nextPartial
Page * nextPartial
Definition
qv4persistent.cpp:22
QV4::Header::engine
ExecutionEngine * engine
Definition
qv4persistent.cpp:18
QV4::Header::prev
Page ** prev
Definition
qv4persistent.cpp:20
QV4::Header::storage
PersistentValueStorage * storage
Definition
qv4persistent.cpp:19
QV4::Header::next
Page * next
Definition
qv4persistent.cpp:21
QV4::Header::freeList
int freeList
Definition
qv4persistent.cpp:24
QV4::Page
Definition
qv4persistent.cpp:29
QV4::Page::header
Header header
Definition
qv4persistent.cpp:30
QV4::Page::values
Value values[1]
Definition
qv4persistent.cpp:31
qtdeclarative
src
qml
jsruntime
qv4persistent.cpp
Generated on
for Qt by
1.18.0