Extract initializing singleton marker

* Extract kInitializingSingleton marker
* Extract isNullOrMarker helper function
This commit is contained in:
Alexander Shabalin
2021-03-19 08:19:10 +00:00
committed by Space
parent b3ae30fdea
commit 5b1c30c198
4 changed files with 15 additions and 11 deletions
@@ -328,7 +328,7 @@ public:
}
static int toIndex(const ObjHeader* obj, int stack) {
if (reinterpret_cast<uintptr_t>(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<uintptr_t>(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<const ObjHeader**>(location) = object;
if (reinterpret_cast<uintptr_t>(old) > 1) {
if (!isNullOrMarker(old)) {
releaseHeapRef<Strict>(old);
}
}
@@ -2226,7 +2226,7 @@ void updateHeapRefsInsideOneArray(const ArrayHeader* array, int fromIndex, int t
template <bool Strict>
void updateStackRef(ObjHeader** location, const ObjHeader* object) {
UPDATE_REF_EVENT(memoryState, *location, object, location, 1)
RuntimeAssert(object != reinterpret_cast<ObjHeader*>(1), "Markers disallowed here");
RuntimeAssert(object != kInitializingSingleton, "Markers disallowed here");
if (Strict) {
*const_cast<const ObjHeader**>(location) = object;
} else {
@@ -2397,7 +2397,7 @@ OBJ_GETTER(initSingleton, ObjHeader** location, const TypeInfo* typeInfo, void (
}
}
ObjHeader* initializing = reinterpret_cast<ObjHeader*>(1);
ObjHeader* initializing = kInitializingSingleton;
// Spin lock.
ObjHeader* value = nullptr;
@@ -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<ObjHeader*>(1);
ALWAYS_INLINE inline bool isNullOrMarker(const ObjHeader* obj) noexcept {
return reinterpret_cast<uintptr_t>(obj) <= 1;
}
class ForeignRefManager;
typedef ForeignRefManager* ForeignRefContext;
@@ -42,7 +42,7 @@ OBJ_GETTER(mm::InitSingleton, ThreadData* threadData, ObjHeader** location, cons
}
}
ObjHeader* initializing = reinterpret_cast<ObjHeader*>(1);
ObjHeader* initializing = kInitializingSingleton;
// Spin lock.
ObjHeader* value = nullptr;
@@ -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<ObjHeader*>(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<ObjHeader*>(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<ObjHeader*>(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<ObjHeader*>(1)));
EXPECT_THAT(location, testing::Not(testing::Truly(isNullOrMarker)));
EXPECT_THAT(stackLocations, testing::Each(location));
EXPECT_THAT(actual, testing::Each(location));
}