diff --git a/base/include/refbase.h b/base/include/refbase.h index 68fb082d3a62f76feb3e3bd3bc0b1ae1687291b7..bcf795744e4ce3896c31ee28dd568640c443aa91 100644 --- a/base/include/refbase.h +++ b/base/include/refbase.h @@ -240,7 +240,7 @@ public: */ void ExtendObjectLifetime(); -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM using CanPromote = std::function; void SetCanPromote(const CanPromote& canPromote); @@ -277,7 +277,7 @@ private: std::atomic atomicAttempt_; // Number of attempts RefPtrCallback callback_ = nullptr; // Callback function to deallocate the corresponding RefBase object static constexpr unsigned int FLAG_EXTEND_LIFE_TIME = 0x00000002; // Extended life-time bit to be set via logic-OR -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM CanPromote canPromote_ = nullptr; #endif #ifdef DEBUG_REFBASE @@ -611,7 +611,7 @@ public: */ void EnableTrackerWithDomainId(unsigned int domainId); -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM virtual bool CanPromote(); #endif diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp index 37eba341d8b3e46c90c0da67a0b56a0bd9e2d8b6..8c5aea5b5cb666e21fdc3c88a7f7573cc9cae3bc 100644 --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -230,7 +230,7 @@ bool RefCounter::IsRefPtrValid() return callback_ != nullptr; } -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM void RefCounter::SetCanPromote(const CanPromote &canPromote) { canPromote_ = canPromote; @@ -402,7 +402,9 @@ bool RefCounter::AttemptIncStrongRef(const void *objectId, int &outCount) // if the object already had strong references.just promoting it. while ((curCount > 0) && (curCount != INITIAL_PRIMARY_VALUE)) { if (atomicStrong_.compare_exchange_weak(curCount, curCount + 1, std::memory_order_relaxed)) { +#if ((defined DEBUG_REFBASE) && (defined PRINT_TRACK_AT_ONCE)) RefBaseDebugPrint(curCount, __builtin_return_address(0), objectId, "++", "atomicStrong_"); +#endif goto ATTEMPT_SUCCESS; } // someone else changed the counter.re-acquire the counter value. @@ -413,7 +415,9 @@ bool RefCounter::AttemptIncStrongRef(const void *objectId, int &outCount) // this object has a "normal" life-time, while (curCount > 0) { if (atomicStrong_.compare_exchange_weak(curCount, curCount + 1, std::memory_order_relaxed)) { +#if ((defined DEBUG_REFBASE) && (defined PRINT_TRACK_AT_ONCE)) RefBaseDebugPrint(curCount, __builtin_return_address(0), objectId, "++", "atomicStrong_"); +#endif goto ATTEMPT_SUCCESS; } curCount = atomicStrong_.load(std::memory_order_relaxed); @@ -421,13 +425,15 @@ bool RefCounter::AttemptIncStrongRef(const void *objectId, int &outCount) } if (IsLifeTimeExtended()) { -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM if (!IsCanPromoteValid() || !canPromote_()) { return false; } #endif curCount = atomicStrong_.fetch_add(1, std::memory_order_relaxed); +#if ((defined DEBUG_REFBASE) && (defined PRINT_TRACK_AT_ONCE)) RefBaseDebugPrint(curCount, __builtin_return_address(0), objectId, "++", "atomicStrong_"); +#endif } ATTEMPT_SUCCESS: @@ -474,7 +480,7 @@ RefBase::RefBase() : refs_(new RefCounter()) { refs_->IncRefCount(); refs_->SetCallback([this] { this->RefPtrCallback(); }); -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM refs_->SetCanPromote([this] { return this->CanPromote(); }); #endif } @@ -485,13 +491,13 @@ RefBase::RefBase(const RefBase &) if (refs_ != nullptr) { refs_->IncRefCount(); refs_->SetCallback([this] { this->RefPtrCallback(); }); -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM refs_->SetCanPromote([this] { return this->CanPromote(); }); #endif } } -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM bool RefBase::CanPromote() { return true; @@ -520,7 +526,7 @@ RefBase &RefBase::operator=(const RefBase &) if (refs_ != nullptr) { refs_->IncRefCount(); refs_->SetCallback([this] { this->RefPtrCallback(); }); -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM refs_->SetCanPromote([this] { return this->CanPromote(); }); #endif } diff --git a/base/test/unittest/common/utils_refbase_test.cpp b/base/test/unittest/common/utils_refbase_test.cpp index 4041e2e2aa431d619c155403ddadfc6413cf981f..9fcdcae5501a02085219fc9888eddd79df28eddc 100644 --- a/base/test/unittest/common/utils_refbase_test.cpp +++ b/base/test/unittest/common/utils_refbase_test.cpp @@ -1337,7 +1337,7 @@ HWTEST_F(UtilsRefbaseTest, testRefbaseWithDomainIdDebug004, TestSize.Level1) HWTEST_F(UtilsRefbaseTest, testRefCounter001, TestSize.Level1) { RefCounter *refCounterTest = new RefCounter(); -#ifdef OHOS_PLATFORM +#ifndef EMULATOR_PLATFORM refCounterTest->RemoveCanPromote(); bool result = refCounterTest->IsCanPromoteValid(); EXPECT_EQ(result, false);