• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <memory>
18 
19 #include "gtest/gtest.h"
20 
21 #include "chre/util/memory.h"
22 #include "chre/util/system/intrusive_ref_base.h"
23 
24 #include "pw_intrusive_ptr/intrusive_ptr.h"
25 #include "pw_intrusive_ptr/recyclable.h"
26 
27 namespace {
28 
29 class TestBase : public chre::IntrusiveRefBase,
30                  public pw::Recyclable<TestBase> {
31  public:
~TestBase()32   ~TestBase() {
33     destructorCount++;
34   }
35 
pw_recycle()36   void pw_recycle() {
37     chre::memoryFreeAndDestroy(this);
38   }
39 
40   static int destructorCount;
41 };
42 int TestBase::destructorCount = 0;
43 
44 class IntrusiveRefBaseTest : public testing::Test {
45  public:
SetUp()46   void SetUp() override {
47     TestBase::destructorCount = 0;
48   }
49 };
50 
51 }  // namespace
52 
TEST_F(IntrusiveRefBaseTest,ObjectIsDestroyed)53 TEST_F(IntrusiveRefBaseTest, ObjectIsDestroyed) {
54   TestBase *object =
55       static_cast<TestBase *>(chre::memoryAlloc(sizeof(TestBase)));
56   ASSERT_NE(object, nullptr);
57   std::construct_at(object);
58 
59   {
60     pw::IntrusivePtr<TestBase> objectPtr(object);
61     EXPECT_EQ(0, TestBase::destructorCount);
62 
63     {
64       pw::IntrusivePtr<TestBase> objectPtr2(object);
65       EXPECT_EQ(0, TestBase::destructorCount);
66     }
67     EXPECT_EQ(0, TestBase::destructorCount);
68   }
69   EXPECT_EQ(1, TestBase::destructorCount);
70 }
71