diff options
Diffstat (limited to 'src/qml/jsruntime/qv4persistent.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4persistent.cpp | 182 |
1 files changed, 106 insertions, 76 deletions
diff --git a/src/qml/jsruntime/qv4persistent.cpp b/src/qml/jsruntime/qv4persistent.cpp index 79c372348f..4f11d0a2ad 100644 --- a/src/qml/jsruntime/qv4persistent.cpp +++ b/src/qml/jsruntime/qv4persistent.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qv4persistent_p.h" #include <private/qv4mm_p.h> @@ -64,7 +28,7 @@ struct Page { Value values[1]; // Really kEntriesPerPage, but keep the compiler happy }; -Page *getPage(Value *val) { +Page *getPage(const Value *val) { return reinterpret_cast<Page *>(reinterpret_cast<quintptr>(val) & ~((quintptr)(WTF::pageSize() - 1))); } @@ -178,6 +142,7 @@ PersistentValueStorage::PersistentValueStorage(ExecutionEngine *engine) PersistentValueStorage::~PersistentValueStorage() { + clearFreePageHint(); Page *p = static_cast<Page *>(firstPage); while (p) { for (int i = 0; i < kEntriesPerPage; ++i) { @@ -195,7 +160,9 @@ PersistentValueStorage::~PersistentValueStorage() Value *PersistentValueStorage::allocate() { - Page *p = static_cast<Page *>(firstPage); + Page *p = static_cast<Page *>(freePageHint); + if (p && p->header.freeList == -1) + p = static_cast<Page *>(firstPage); while (p) { if (p->header.freeList != -1) break; @@ -207,9 +174,15 @@ Value *PersistentValueStorage::allocate() Value *v = p->values + p->header.freeList; p->header.freeList = v->int_32(); - if (p->header.freeList != -1 && p != firstPage) { - unlink(p); - insertInFront(this, p); + if (p->header.freeList != -1 && p != freePageHint) { + if (auto oldHint = static_cast<Page *>(freePageHint)) { + oldHint->header.refCount--; + // no need to free - if the old page were unused, + // we would have used it to serve the allocation + Q_ASSERT(oldHint->header.refCount); + } + freePageHint = p; + p->header.refCount++; } ++p->header.refCount; @@ -219,11 +192,9 @@ Value *PersistentValueStorage::allocate() return v; } -void PersistentValueStorage::free(Value *v) +void PersistentValueStorage::freeUnchecked(Value *v) { - if (!v) - return; - + Q_ASSERT(v); Page *p = getPage(v); *v = Encode(p->header.freeList); @@ -240,13 +211,23 @@ void PersistentValueStorage::mark(MarkStack *markStack) if (Managed *m = p->values[i].as<Managed>()) m->mark(markStack); } - markStack->drain(); p = p->header.next; } } -ExecutionEngine *PersistentValueStorage::getEngine(Value *v) +void PersistentValueStorage::clearFreePageHint() +{ + if (!freePageHint) + return; + auto page = static_cast<Page *>(freePageHint); + if (!--page->header.refCount) + freePage(page); + freePageHint = nullptr; + +} + +ExecutionEngine *PersistentValueStorage::getEngine(const Value *v) { return getPage(v)->header.engine; } @@ -262,22 +243,18 @@ void PersistentValueStorage::freePage(void *page) PersistentValue::PersistentValue(const PersistentValue &other) : val(nullptr) { - if (other.val) { - val = other.engine()->memoryManager->m_persistentValues->allocate(); - *val = *other.val; - } + if (other.val) + set(other.engine(), *other.val); } PersistentValue::PersistentValue(ExecutionEngine *engine, const Value &value) { - val = engine->memoryManager->m_persistentValues->allocate(); - *val = value; + set(engine, value); } PersistentValue::PersistentValue(ExecutionEngine *engine, ReturnedValue value) { - val = engine->memoryManager->m_persistentValues->allocate(); - *val = value; + set(engine, value); } PersistentValue::PersistentValue(ExecutionEngine *engine, Object *object) @@ -285,14 +262,7 @@ PersistentValue::PersistentValue(ExecutionEngine *engine, Object *object) { if (!object) return; - - val = engine->memoryManager->m_persistentValues->allocate(); - *val = object; -} - -PersistentValue::~PersistentValue() -{ - PersistentValueStorage::free(val); + set(engine, *object); } PersistentValue &PersistentValue::operator=(const PersistentValue &other) @@ -315,19 +285,16 @@ PersistentValue &PersistentValue::operator=(const PersistentValue &other) PersistentValue &PersistentValue::operator=(const WeakValue &other) { - if (!val) { - if (!other.valueRef()) - return *this; - val = other.engine()->memoryManager->m_persistentValues->allocate(); - } + if (!val && !other.valueRef()) + return *this; if (!other.valueRef()) { *val = Encode::undefined(); return *this; } - Q_ASSERT(engine() == other.engine()); + Q_ASSERT(!engine() || engine() == other.engine()); - *val = *other.valueRef(); + set(other.engine(), *other.valueRef()); return *this; } @@ -337,10 +304,7 @@ PersistentValue &PersistentValue::operator=(Object *object) PersistentValueStorage::free(val); return *this; } - if (!val) - val = object->engine()->memoryManager->m_persistentValues->allocate(); - - *val = object; + set(object->engine(), *object); return *this; } @@ -348,6 +312,10 @@ void PersistentValue::set(ExecutionEngine *engine, const Value &value) { if (!val) val = engine->memoryManager->m_persistentValues->allocate(); + QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack){ + if (QV4::WriteBarrier::isInsertionBarrier && value.isManaged()) + value.heapObject()->mark(stack); + }); *val = value; } @@ -355,6 +323,13 @@ void PersistentValue::set(ExecutionEngine *engine, ReturnedValue value) { if (!val) val = engine->memoryManager->m_persistentValues->allocate(); + QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack){ + if constexpr (!QV4::WriteBarrier::isInsertionBarrier) + return; + auto val = Value::fromReturnedValue(value); + if (val.isManaged()) + val.heapObject()->mark(stack); + }); *val = value; } @@ -362,6 +337,11 @@ void PersistentValue::set(ExecutionEngine *engine, Heap::Base *obj) { if (!val) val = engine->memoryManager->m_persistentValues->allocate(); + QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack){ + if constexpr (QV4::WriteBarrier::isInsertionBarrier) + obj->mark(stack); + }); + *val = obj; } @@ -403,6 +383,56 @@ WeakValue::~WeakValue() free(); } +/* + WeakValue::set shold normally not mark objects, after all a weak value + is not supposed to keep an object alive. + However, if we are past GCState::HandleQObjectWrappers, nothing will + reset weak values referencing unmarked values, but those values will + still be swept. + That lead to stale pointers, and potentially to crashes. To avoid this, + we mark the objects here (they might still get collected in the next gc + run). + This is especially important due to the way we handle QObjectWrappers. + */ +void WeakValue::set(ExecutionEngine *engine, const Value &value) +{ + if (!val) + allocVal(engine); + QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *ms) { + if (engine->memoryManager->gcStateMachine->state <= GCState::HandleQObjectWrappers) + return; + if (auto *h = value.heapObject()) + h->mark(ms); + }); + *val = value; +} + +void WeakValue::set(ExecutionEngine *engine, ReturnedValue value) +{ + if (!val) + allocVal(engine); + QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *ms) { + if (engine->memoryManager->gcStateMachine->state <= GCState::HandleQObjectWrappers) + return; + if (auto *h = QV4::Value::fromReturnedValue(value).heapObject()) + h->mark(ms); + }); + + *val = value; +} + +void WeakValue::set(ExecutionEngine *engine, Heap::Base *obj) +{ + if (!val) + allocVal(engine); + QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *ms) { + if (engine->memoryManager->gcStateMachine->state <= GCState::HandleQObjectWrappers) + return; + obj->mark(ms); + }); + *val = obj; +} + void WeakValue::allocVal(ExecutionEngine *engine) { val = engine->memoryManager->m_weakValues->allocate(); |