From fc43fbf578e34e9fd50de99aa77b0b1e3da4544d Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Sun, 28 Feb 2021 14:12:04 +0700 Subject: [PATCH] [K/N][New MM] Move MemoryState conversions to separate header --- kotlin-native/runtime/src/mm/cpp/Memory.cpp | 32 ++++---------- .../runtime/src/mm/cpp/MemoryPrivate.hpp | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+), 25 deletions(-) create mode 100644 kotlin-native/runtime/src/mm/cpp/MemoryPrivate.hpp diff --git a/kotlin-native/runtime/src/mm/cpp/Memory.cpp b/kotlin-native/runtime/src/mm/cpp/Memory.cpp index 8662afe2073..b321b8d907c 100644 --- a/kotlin-native/runtime/src/mm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/mm/cpp/Memory.cpp @@ -4,6 +4,7 @@ */ #include "Memory.h" +#include "MemoryPrivate.hpp" #include "Exceptions.h" #include "ExtraObjectData.hpp" @@ -20,13 +21,6 @@ using namespace kotlin; -// Delete all means of creating this type directly as it only serves -// as a typedef for `mm::ThreadRegistry::Node`. -extern "C" struct MemoryState : Pinned { - MemoryState() = delete; - ~MemoryState() = delete; -}; - // TODO: This name does not make sense anymore. // Delete all means of creating this type directly as it only serves // as a typedef for `mm::StableRefRegistry::Node`. @@ -40,14 +34,6 @@ namespace { // `reinterpret_cast` to it and back to the same type // will yield precisely the same pointer, so it's safe. -ALWAYS_INLINE MemoryState* ToMemoryState(mm::ThreadRegistry::Node* data) { - return reinterpret_cast(data); -} - -ALWAYS_INLINE mm::ThreadRegistry::Node* FromMemoryState(MemoryState* state) { - return reinterpret_cast(state); -} - ALWAYS_INLINE ForeignRefManager* ToForeignRefManager(mm::StableRefRegistry::Node* data) { return reinterpret_cast(data); } @@ -56,10 +42,6 @@ ALWAYS_INLINE mm::StableRefRegistry::Node* FromForeignRefManager(ForeignRefManag return reinterpret_cast(manager); } -ALWAYS_INLINE mm::ThreadData* GetThreadData(MemoryState* state) { - return FromMemoryState(state)->Get(); -} - } // namespace ObjHeader** ObjHeader::GetWeakCounterLocation() { @@ -105,11 +87,11 @@ ALWAYS_INLINE bool isShareable(const ObjHeader* obj) { } extern "C" MemoryState* InitMemory(bool firstRuntime) { - return ToMemoryState(mm::ThreadRegistry::Instance().RegisterCurrentThread()); + return mm::ToMemoryState(mm::ThreadRegistry::Instance().RegisterCurrentThread()); } extern "C" void DeinitMemory(MemoryState* state, bool destroyRuntime) { - mm::ThreadRegistry::Instance().Unregister(FromMemoryState(state)); + mm::ThreadRegistry::Instance().Unregister(mm::FromMemoryState(state)); } extern "C" void RestoreMemory(MemoryState*) { @@ -233,15 +215,15 @@ extern "C" RUNTIME_NOTHROW void LeaveFrame(ObjHeader** start, int parameters, in } extern "C" RUNTIME_NOTHROW void AddTLSRecord(MemoryState* memory, void** key, int size) { - GetThreadData(memory)->tls().AddRecord(key, size); + memory->GetThreadData()->tls().AddRecord(key, size); } extern "C" RUNTIME_NOTHROW void CommitTLSStorage(MemoryState* memory) { - GetThreadData(memory)->tls().Commit(); + memory->GetThreadData()->tls().Commit(); } extern "C" RUNTIME_NOTHROW void ClearTLS(MemoryState* memory) { - GetThreadData(memory)->tls().Clear(); + memory->GetThreadData()->tls().Clear(); } extern "C" RUNTIME_NOTHROW ObjHeader** LookupTLS(void** key, int index) { @@ -347,7 +329,7 @@ extern "C" void Kotlin_Any_share(ObjHeader* thiz) { } extern "C" RUNTIME_NOTHROW void PerformFullGC(MemoryState* memory) { - GetThreadData(memory)->gc().PerformFullGC(); + memory->GetThreadData()->gc().PerformFullGC(); } extern "C" RUNTIME_NOTHROW bool ClearSubgraphReferences(ObjHeader* root, bool checked) { diff --git a/kotlin-native/runtime/src/mm/cpp/MemoryPrivate.hpp b/kotlin-native/runtime/src/mm/cpp/MemoryPrivate.hpp new file mode 100644 index 00000000000..1efeb01e4dc --- /dev/null +++ b/kotlin-native/runtime/src/mm/cpp/MemoryPrivate.hpp @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#ifndef RUNTIME_MEMORYPRIVATE_HPP +#define RUNTIME_MEMORYPRIVATE_HPP + +#include "Utils.hpp" +#include "ThreadData.hpp" +#include "ThreadRegistry.hpp" + +using namespace kotlin; + +namespace kotlin { +namespace mm { + +// `reinterpret_cast` to it and back to the same type +// will yield precisely the same pointer, so it's safe. +ALWAYS_INLINE inline MemoryState* ToMemoryState(ThreadRegistry::Node* data) { + return reinterpret_cast(data); +} + +ALWAYS_INLINE inline ThreadRegistry::Node* FromMemoryState(MemoryState* state) { + return reinterpret_cast(state); +} + +} // namespace mm +} // namepace kotlin + +// Delete all means of creating this type directly as it only serves +// as a typedef for `mm::ThreadRegistry::Node`. +extern "C" struct MemoryState : kotlin::Pinned { + MemoryState() = delete; + ~MemoryState() = delete; + + ALWAYS_INLINE mm::ThreadData* GetThreadData() { + return mm::FromMemoryState(this)->Get(); + } +}; + +#endif //RUNTIME_MEMORYPRIVATE_HPP