From faab347af8c66bc5fa353f61113d65f45aded595 Mon Sep 17 00:00:00 2001 From: xwx1135370 Date: Wed, 6 Aug 2025 11:43:53 +0800 Subject: [PATCH] =?UTF-8?q?[Bug]:=20[=E9=BB=84=E8=93=9D=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E6=80=A7=E9=97=AE=E9=A2=98]reftracker=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit issue:https://gitee.com/openharmony/commonlibrary_c_utils/issues/ICRH2C?from=project-issue Signed-off-by: xwx1135370 --- base/include/refbase.h | 6 +++--- base/src/refbase.cpp | 18 ++++++++++++------ .../unittest/common/utils_refbase_test.cpp | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/base/include/refbase.h b/base/include/refbase.h index 68fb082..bcf7957 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 37eba34..8c5aea5 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 4041e2e..9fcdcae 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); -- Gitee