Merge "libutils: LightRefBase: incStrongRequireStrong"
This commit is contained in:
commit
1c65d40154
4 changed files with 78 additions and 16 deletions
|
|
@ -141,6 +141,7 @@ cc_library {
|
||||||
"Errors.cpp",
|
"Errors.cpp",
|
||||||
"FileMap.cpp",
|
"FileMap.cpp",
|
||||||
"JenkinsHash.cpp",
|
"JenkinsHash.cpp",
|
||||||
|
"LightRefBase.cpp",
|
||||||
"NativeHandle.cpp",
|
"NativeHandle.cpp",
|
||||||
"Printer.cpp",
|
"Printer.cpp",
|
||||||
"RefBase.cpp",
|
"RefBase.cpp",
|
||||||
|
|
|
||||||
29
libutils/LightRefBase.cpp
Normal file
29
libutils/LightRefBase.cpp
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2021 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define LOG_TAG "LightRefBase"
|
||||||
|
|
||||||
|
#include <utils/LightRefBase.h>
|
||||||
|
|
||||||
|
#include <log/log.h>
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
void LightRefBase_reportIncStrongRequireStrongFailed(const void* thiz) {
|
||||||
|
LOG_ALWAYS_FATAL("incStrongRequireStrong() called on %p which isn't already owned", thiz);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android
|
||||||
|
|
@ -30,17 +30,34 @@ class SPFoo : virtual public RefBase {
|
||||||
~SPFoo() {
|
~SPFoo() {
|
||||||
*mDeleted = true;
|
*mDeleted = true;
|
||||||
}
|
}
|
||||||
private:
|
|
||||||
|
private:
|
||||||
bool* mDeleted;
|
bool* mDeleted;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST(StrongPointer, move) {
|
class SPLightFoo : virtual public VirtualLightRefBase {
|
||||||
|
public:
|
||||||
|
explicit SPLightFoo(bool* deleted_check) : mDeleted(deleted_check) { *mDeleted = false; }
|
||||||
|
|
||||||
|
~SPLightFoo() { *mDeleted = true; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool* mDeleted;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class StrongPointer : public ::testing::Test {};
|
||||||
|
|
||||||
|
using RefBaseTypes = ::testing::Types<SPFoo, SPLightFoo>;
|
||||||
|
TYPED_TEST_CASE(StrongPointer, RefBaseTypes);
|
||||||
|
|
||||||
|
TYPED_TEST(StrongPointer, move) {
|
||||||
bool isDeleted;
|
bool isDeleted;
|
||||||
sp<SPFoo> sp1 = sp<SPFoo>::make(&isDeleted);
|
sp<TypeParam> sp1 = sp<TypeParam>::make(&isDeleted);
|
||||||
SPFoo* foo = sp1.get();
|
TypeParam* foo = sp1.get();
|
||||||
ASSERT_EQ(1, foo->getStrongCount());
|
ASSERT_EQ(1, foo->getStrongCount());
|
||||||
{
|
{
|
||||||
sp<SPFoo> sp2 = std::move(sp1);
|
sp<TypeParam> sp2 = std::move(sp1);
|
||||||
ASSERT_EQ(1, foo->getStrongCount()) << "std::move failed, incremented refcnt";
|
ASSERT_EQ(1, foo->getStrongCount()) << "std::move failed, incremented refcnt";
|
||||||
ASSERT_EQ(nullptr, sp1.get()) << "std::move failed, sp1 is still valid";
|
ASSERT_EQ(nullptr, sp1.get()) << "std::move failed, sp1 is still valid";
|
||||||
// The strong count isn't increasing, let's double check the old object
|
// The strong count isn't increasing, let's double check the old object
|
||||||
|
|
@ -50,33 +67,42 @@ TEST(StrongPointer, move) {
|
||||||
ASSERT_FALSE(isDeleted) << "deleted too early! still has a reference!";
|
ASSERT_FALSE(isDeleted) << "deleted too early! still has a reference!";
|
||||||
{
|
{
|
||||||
// Now let's double check it deletes on time
|
// Now let's double check it deletes on time
|
||||||
sp<SPFoo> sp2 = std::move(sp1);
|
sp<TypeParam> sp2 = std::move(sp1);
|
||||||
}
|
}
|
||||||
ASSERT_TRUE(isDeleted) << "foo was leaked!";
|
ASSERT_TRUE(isDeleted) << "foo was leaked!";
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StrongPointer, NullptrComparison) {
|
TYPED_TEST(StrongPointer, NullptrComparison) {
|
||||||
sp<SPFoo> foo;
|
sp<TypeParam> foo;
|
||||||
ASSERT_EQ(foo, nullptr);
|
ASSERT_EQ(foo, nullptr);
|
||||||
ASSERT_EQ(nullptr, foo);
|
ASSERT_EQ(nullptr, foo);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StrongPointer, PointerComparison) {
|
TYPED_TEST(StrongPointer, PointerComparison) {
|
||||||
bool isDeleted;
|
bool isDeleted;
|
||||||
sp<SPFoo> foo = sp<SPFoo>::make(&isDeleted);
|
sp<TypeParam> foo = sp<TypeParam>::make(&isDeleted);
|
||||||
ASSERT_EQ(foo.get(), foo);
|
ASSERT_EQ(foo.get(), foo);
|
||||||
ASSERT_EQ(foo, foo.get());
|
ASSERT_EQ(foo, foo.get());
|
||||||
ASSERT_NE(nullptr, foo);
|
ASSERT_NE(nullptr, foo);
|
||||||
ASSERT_NE(foo, nullptr);
|
ASSERT_NE(foo, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StrongPointer, AssertStrongRefExists) {
|
TYPED_TEST(StrongPointer, Deleted) {
|
||||||
// uses some other refcounting method, or non at all
|
|
||||||
bool isDeleted;
|
bool isDeleted;
|
||||||
SPFoo* foo = new SPFoo(&isDeleted);
|
sp<TypeParam> foo = sp<TypeParam>::make(&isDeleted);
|
||||||
|
|
||||||
// can only get a valid sp<> object when you construct it as an sp<> object
|
auto foo2 = sp<TypeParam>::fromExisting(foo.get());
|
||||||
EXPECT_DEATH(sp<SPFoo>::fromExisting(foo), "");
|
|
||||||
|
|
||||||
|
EXPECT_FALSE(isDeleted);
|
||||||
|
foo = nullptr;
|
||||||
|
EXPECT_FALSE(isDeleted);
|
||||||
|
foo2 = nullptr;
|
||||||
|
EXPECT_TRUE(isDeleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
TYPED_TEST(StrongPointer, AssertStrongRefExists) {
|
||||||
|
bool isDeleted;
|
||||||
|
TypeParam* foo = new TypeParam(&isDeleted);
|
||||||
|
EXPECT_DEATH(sp<TypeParam>::fromExisting(foo), "");
|
||||||
delete foo;
|
delete foo;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,8 @@ namespace android {
|
||||||
|
|
||||||
class ReferenceRenamer;
|
class ReferenceRenamer;
|
||||||
|
|
||||||
|
void LightRefBase_reportIncStrongRequireStrongFailed(const void* thiz);
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class LightRefBase
|
class LightRefBase
|
||||||
{
|
{
|
||||||
|
|
@ -36,6 +38,11 @@ public:
|
||||||
inline void incStrong(__attribute__((unused)) const void* id) const {
|
inline void incStrong(__attribute__((unused)) const void* id) const {
|
||||||
mCount.fetch_add(1, std::memory_order_relaxed);
|
mCount.fetch_add(1, std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
inline void incStrongRequireStrong(__attribute__((unused)) const void* id) const {
|
||||||
|
if (0 == mCount.fetch_add(1, std::memory_order_relaxed)) {
|
||||||
|
LightRefBase_reportIncStrongRequireStrongFailed(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
inline void decStrong(__attribute__((unused)) const void* id) const {
|
inline void decStrong(__attribute__((unused)) const void* id) const {
|
||||||
if (mCount.fetch_sub(1, std::memory_order_release) == 1) {
|
if (mCount.fetch_sub(1, std::memory_order_release) == 1) {
|
||||||
std::atomic_thread_fence(std::memory_order_acquire);
|
std::atomic_thread_fence(std::memory_order_acquire);
|
||||||
|
|
@ -59,7 +66,6 @@ private:
|
||||||
mutable std::atomic<int32_t> mCount;
|
mutable std::atomic<int32_t> mCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// This is a wrapper around LightRefBase that simply enforces a virtual
|
// This is a wrapper around LightRefBase that simply enforces a virtual
|
||||||
// destructor to eliminate the template requirement of LightRefBase
|
// destructor to eliminate the template requirement of LightRefBase
|
||||||
class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> {
|
class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue