Extract initializing singleton marker
* Extract kInitializingSingleton marker * Extract isNullOrMarker helper function
This commit is contained in:
committed by
Space
parent
b3ae30fdea
commit
5b1c30c198
@@ -328,7 +328,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int toIndex(const ObjHeader* obj, int stack) {
|
static int toIndex(const ObjHeader* obj, int stack) {
|
||||||
if (reinterpret_cast<uintptr_t>(obj) > 1)
|
if (!isNullOrMarker(obj))
|
||||||
return toIndex(containerFor(obj), stack);
|
return toIndex(containerFor(obj), stack);
|
||||||
else
|
else
|
||||||
return 4 + stack * 6;
|
return 4 + stack * 6;
|
||||||
@@ -2158,7 +2158,7 @@ void setHeapRef(ObjHeader** location, const ObjHeader* object) {
|
|||||||
void zeroHeapRef(ObjHeader** location) {
|
void zeroHeapRef(ObjHeader** location) {
|
||||||
MEMORY_LOG("ZeroHeapRef %p\n", location)
|
MEMORY_LOG("ZeroHeapRef %p\n", location)
|
||||||
auto* value = *location;
|
auto* value = *location;
|
||||||
if (reinterpret_cast<uintptr_t>(value) > 1) {
|
if (!isNullOrMarker(value)) {
|
||||||
UPDATE_REF_EVENT(memoryState, value, nullptr, location, 0);
|
UPDATE_REF_EVENT(memoryState, value, nullptr, location, 0);
|
||||||
*location = nullptr;
|
*location = nullptr;
|
||||||
ReleaseHeapRef(value);
|
ReleaseHeapRef(value);
|
||||||
@@ -2186,7 +2186,7 @@ void updateHeapRef(ObjHeader** location, const ObjHeader* object) {
|
|||||||
addHeapRef(object);
|
addHeapRef(object);
|
||||||
}
|
}
|
||||||
*const_cast<const ObjHeader**>(location) = object;
|
*const_cast<const ObjHeader**>(location) = object;
|
||||||
if (reinterpret_cast<uintptr_t>(old) > 1) {
|
if (!isNullOrMarker(old)) {
|
||||||
releaseHeapRef<Strict>(old);
|
releaseHeapRef<Strict>(old);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2226,7 +2226,7 @@ void updateHeapRefsInsideOneArray(const ArrayHeader* array, int fromIndex, int t
|
|||||||
template <bool Strict>
|
template <bool Strict>
|
||||||
void updateStackRef(ObjHeader** location, const ObjHeader* object) {
|
void updateStackRef(ObjHeader** location, const ObjHeader* object) {
|
||||||
UPDATE_REF_EVENT(memoryState, *location, object, location, 1)
|
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) {
|
if (Strict) {
|
||||||
*const_cast<const ObjHeader**>(location) = object;
|
*const_cast<const ObjHeader**>(location) = object;
|
||||||
} else {
|
} 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.
|
// Spin lock.
|
||||||
ObjHeader* value = nullptr;
|
ObjHeader* value = nullptr;
|
||||||
|
|||||||
@@ -113,6 +113,11 @@ ALWAYS_INLINE bool isFrozen(const ObjHeader* obj);
|
|||||||
ALWAYS_INLINE bool isPermanentOrFrozen(const ObjHeader* obj);
|
ALWAYS_INLINE bool isPermanentOrFrozen(const ObjHeader* obj);
|
||||||
ALWAYS_INLINE bool isShareable(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;
|
class ForeignRefManager;
|
||||||
typedef ForeignRefManager* ForeignRefContext;
|
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.
|
// Spin lock.
|
||||||
ObjHeader* value = nullptr;
|
ObjHeader* value = nullptr;
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ TEST_F(InitSingletonTest, InitSingleton) {
|
|||||||
ObjHeader* valueAtConstructor = nullptr;
|
ObjHeader* valueAtConstructor = nullptr;
|
||||||
EXPECT_CALL(constructor(), Call(_)).WillOnce([&location, &stackLocation, &valueAtConstructor](ObjHeader* value) {
|
EXPECT_CALL(constructor(), Call(_)).WillOnce([&location, &stackLocation, &valueAtConstructor](ObjHeader* value) {
|
||||||
EXPECT_THAT(value, stackLocation);
|
EXPECT_THAT(value, stackLocation);
|
||||||
EXPECT_THAT(location, reinterpret_cast<ObjHeader*>(1));
|
EXPECT_THAT(location, kInitializingSingleton);
|
||||||
valueAtConstructor = value;
|
valueAtConstructor = value;
|
||||||
});
|
});
|
||||||
ObjHeader* value = InitSingleton(&location, 0, &stackLocation);
|
ObjHeader* value = InitSingleton(&location, 0, &stackLocation);
|
||||||
@@ -176,12 +176,12 @@ TEST_F(InitSingletonTest, InitSingletonRecursive) {
|
|||||||
ObjHeader* result = InitSingleton(&location2, 0, &stackLocation2);
|
ObjHeader* result = InitSingleton(&location2, 0, &stackLocation2);
|
||||||
EXPECT_THAT(result, stackLocation2);
|
EXPECT_THAT(result, stackLocation2);
|
||||||
EXPECT_THAT(result, location2);
|
EXPECT_THAT(result, location2);
|
||||||
EXPECT_THAT(result, testing::Ne(reinterpret_cast<ObjHeader*>(1)));
|
EXPECT_THAT(result, testing::Not(testing::Truly(isNullOrMarker)));
|
||||||
} else {
|
} else {
|
||||||
ObjHeader* result = InitSingleton(&location1, 0, &stackLocation1);
|
ObjHeader* result = InitSingleton(&location1, 0, &stackLocation1);
|
||||||
EXPECT_THAT(result, stackLocation1);
|
EXPECT_THAT(result, stackLocation1);
|
||||||
EXPECT_THAT(result, testing::Ne(location1));
|
EXPECT_THAT(result, testing::Ne(location1));
|
||||||
EXPECT_THAT(location1, reinterpret_cast<ObjHeader*>(1));
|
EXPECT_THAT(location1, kInitializingSingleton);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ObjHeader* value = InitSingleton(&location1, 0, &stackLocation1);
|
ObjHeader* value = InitSingleton(&location1, 0, &stackLocation1);
|
||||||
@@ -217,8 +217,7 @@ TEST_F(InitSingletonTest, InitSingletonConcurrent) {
|
|||||||
}
|
}
|
||||||
testing::Mock::VerifyAndClearExpectations(&constructor());
|
testing::Mock::VerifyAndClearExpectations(&constructor());
|
||||||
|
|
||||||
EXPECT_THAT(location, testing::Ne(nullptr));
|
EXPECT_THAT(location, testing::Not(testing::Truly(isNullOrMarker)));
|
||||||
EXPECT_THAT(location, testing::Ne(reinterpret_cast<ObjHeader*>(1)));
|
|
||||||
EXPECT_THAT(stackLocations, testing::Each(location));
|
EXPECT_THAT(stackLocations, testing::Each(location));
|
||||||
EXPECT_THAT(actual, testing::Each(location));
|
EXPECT_THAT(actual, testing::Each(location));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user