diff --git a/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp b/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp index e5ee6d2d0af..1695805bb9a 100644 --- a/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/legacymm/cpp/Memory.cpp @@ -328,7 +328,7 @@ public: } static int toIndex(const ObjHeader* obj, int stack) { - if (reinterpret_cast(obj) > 1) + if (!isNullOrMarker(obj)) return toIndex(containerFor(obj), stack); else return 4 + stack * 6; @@ -2158,7 +2158,7 @@ void setHeapRef(ObjHeader** location, const ObjHeader* object) { void zeroHeapRef(ObjHeader** location) { MEMORY_LOG("ZeroHeapRef %p\n", location) auto* value = *location; - if (reinterpret_cast(value) > 1) { + if (!isNullOrMarker(value)) { UPDATE_REF_EVENT(memoryState, value, nullptr, location, 0); *location = nullptr; ReleaseHeapRef(value); @@ -2186,7 +2186,7 @@ void updateHeapRef(ObjHeader** location, const ObjHeader* object) { addHeapRef(object); } *const_cast(location) = object; - if (reinterpret_cast(old) > 1) { + if (!isNullOrMarker(old)) { releaseHeapRef(old); } } @@ -2226,7 +2226,7 @@ void updateHeapRefsInsideOneArray(const ArrayHeader* array, int fromIndex, int t template void updateStackRef(ObjHeader** location, const ObjHeader* object) { UPDATE_REF_EVENT(memoryState, *location, object, location, 1) - RuntimeAssert(object != reinterpret_cast(1), "Markers disallowed here"); + RuntimeAssert(object != kInitializingSingleton, "Markers disallowed here"); if (Strict) { *const_cast(location) = object; } else { @@ -2397,7 +2397,7 @@ OBJ_GETTER(initSingleton, ObjHeader** location, const TypeInfo* typeInfo, void ( } } - ObjHeader* initializing = reinterpret_cast(1); + ObjHeader* initializing = kInitializingSingleton; // Spin lock. ObjHeader* value = nullptr; diff --git a/kotlin-native/runtime/src/main/cpp/Memory.h b/kotlin-native/runtime/src/main/cpp/Memory.h index fb948234d73..c06c87431d0 100644 --- a/kotlin-native/runtime/src/main/cpp/Memory.h +++ b/kotlin-native/runtime/src/main/cpp/Memory.h @@ -113,6 +113,11 @@ ALWAYS_INLINE bool isFrozen(const ObjHeader* obj); ALWAYS_INLINE bool isPermanentOrFrozen(const ObjHeader* obj); ALWAYS_INLINE bool isShareable(const ObjHeader* obj); +static inline ObjHeader* const kInitializingSingleton = reinterpret_cast(1); +ALWAYS_INLINE inline bool isNullOrMarker(const ObjHeader* obj) noexcept { + return reinterpret_cast(obj) <= 1; +} + class ForeignRefManager; typedef ForeignRefManager* ForeignRefContext; diff --git a/kotlin-native/runtime/src/mm/cpp/InitializationScheme.cpp b/kotlin-native/runtime/src/mm/cpp/InitializationScheme.cpp index b4e1104ace3..8f2204b64f8 100644 --- a/kotlin-native/runtime/src/mm/cpp/InitializationScheme.cpp +++ b/kotlin-native/runtime/src/mm/cpp/InitializationScheme.cpp @@ -42,7 +42,7 @@ OBJ_GETTER(mm::InitSingleton, ThreadData* threadData, ObjHeader** location, cons } } - ObjHeader* initializing = reinterpret_cast(1); + ObjHeader* initializing = kInitializingSingleton; // Spin lock. ObjHeader* value = nullptr; diff --git a/kotlin-native/runtime/src/mm/cpp/InitializationSchemeTest.cpp b/kotlin-native/runtime/src/mm/cpp/InitializationSchemeTest.cpp index f0ed3ccc44a..0325791a758 100644 --- a/kotlin-native/runtime/src/mm/cpp/InitializationSchemeTest.cpp +++ b/kotlin-native/runtime/src/mm/cpp/InitializationSchemeTest.cpp @@ -124,7 +124,7 @@ TEST_F(InitSingletonTest, InitSingleton) { ObjHeader* valueAtConstructor = nullptr; EXPECT_CALL(constructor(), Call(_)).WillOnce([&location, &stackLocation, &valueAtConstructor](ObjHeader* value) { EXPECT_THAT(value, stackLocation); - EXPECT_THAT(location, reinterpret_cast(1)); + EXPECT_THAT(location, kInitializingSingleton); valueAtConstructor = value; }); ObjHeader* value = InitSingleton(&location, 0, &stackLocation); @@ -176,12 +176,12 @@ TEST_F(InitSingletonTest, InitSingletonRecursive) { ObjHeader* result = InitSingleton(&location2, 0, &stackLocation2); EXPECT_THAT(result, stackLocation2); EXPECT_THAT(result, location2); - EXPECT_THAT(result, testing::Ne(reinterpret_cast(1))); + EXPECT_THAT(result, testing::Not(testing::Truly(isNullOrMarker))); } else { ObjHeader* result = InitSingleton(&location1, 0, &stackLocation1); EXPECT_THAT(result, stackLocation1); EXPECT_THAT(result, testing::Ne(location1)); - EXPECT_THAT(location1, reinterpret_cast(1)); + EXPECT_THAT(location1, kInitializingSingleton); } }); ObjHeader* value = InitSingleton(&location1, 0, &stackLocation1); @@ -217,8 +217,7 @@ TEST_F(InitSingletonTest, InitSingletonConcurrent) { } testing::Mock::VerifyAndClearExpectations(&constructor()); - EXPECT_THAT(location, testing::Ne(nullptr)); - EXPECT_THAT(location, testing::Ne(reinterpret_cast(1))); + EXPECT_THAT(location, testing::Not(testing::Truly(isNullOrMarker))); EXPECT_THAT(stackLocations, testing::Each(location)); EXPECT_THAT(actual, testing::Each(location)); }