xref: /aosp_15_r20/external/pigweed/pw_allocator/examples/basic.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #include "examples/named_u32.h"
16*61c4878aSAndroid Build Coastguard Worker #include "pw_allocator/allocator.h"
17*61c4878aSAndroid Build Coastguard Worker #include "pw_allocator/testing.h"
18*61c4878aSAndroid Build Coastguard Worker #include "pw_unit_test/framework.h"
19*61c4878aSAndroid Build Coastguard Worker 
20*61c4878aSAndroid Build Coastguard Worker namespace examples {
21*61c4878aSAndroid Build Coastguard Worker 
22*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-basic-allocate]
23*61c4878aSAndroid Build Coastguard Worker using pw::allocator::Layout;
24*61c4878aSAndroid Build Coastguard Worker 
AllocateNamedU32(pw::Allocator & allocator)25*61c4878aSAndroid Build Coastguard Worker void* AllocateNamedU32(pw::Allocator& allocator) {
26*61c4878aSAndroid Build Coastguard Worker   return allocator.Allocate(Layout::Of<NamedU32>());
27*61c4878aSAndroid Build Coastguard Worker }
28*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-basic-allocate]
29*61c4878aSAndroid Build Coastguard Worker 
30*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-basic-deallocate]
DeallocateNamedU32(pw::Allocator & allocator,void * ptr)31*61c4878aSAndroid Build Coastguard Worker void DeallocateNamedU32(pw::Allocator& allocator, void* ptr) {
32*61c4878aSAndroid Build Coastguard Worker   allocator.Deallocate(ptr);
33*61c4878aSAndroid Build Coastguard Worker }
34*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-basic-deallocate]
35*61c4878aSAndroid Build Coastguard Worker 
36*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-basic-new_delete]
NewNamedU32(pw::Allocator & allocator,std::string_view name,uint32_t value)37*61c4878aSAndroid Build Coastguard Worker NamedU32* NewNamedU32(pw::Allocator& allocator,
38*61c4878aSAndroid Build Coastguard Worker                       std::string_view name,
39*61c4878aSAndroid Build Coastguard Worker                       uint32_t value) {
40*61c4878aSAndroid Build Coastguard Worker   return allocator.New<NamedU32>(name, value);
41*61c4878aSAndroid Build Coastguard Worker }
42*61c4878aSAndroid Build Coastguard Worker 
DeleteNamedU32(pw::Allocator & allocator,NamedU32 * named_u32)43*61c4878aSAndroid Build Coastguard Worker void DeleteNamedU32(pw::Allocator& allocator, NamedU32* named_u32) {
44*61c4878aSAndroid Build Coastguard Worker   allocator.Delete<NamedU32>(named_u32);
45*61c4878aSAndroid Build Coastguard Worker }
46*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-basic-new_delete]
47*61c4878aSAndroid Build Coastguard Worker 
48*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-basic-make_unique]
MakeNamedU32(pw::Allocator & allocator,std::string_view name,uint32_t value)49*61c4878aSAndroid Build Coastguard Worker pw::UniquePtr<NamedU32> MakeNamedU32(pw::Allocator& allocator,
50*61c4878aSAndroid Build Coastguard Worker                                      std::string_view name,
51*61c4878aSAndroid Build Coastguard Worker                                      uint32_t value) {
52*61c4878aSAndroid Build Coastguard Worker   return allocator.MakeUnique<NamedU32>(name, value);
53*61c4878aSAndroid Build Coastguard Worker }
54*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-basic-make_unique]
55*61c4878aSAndroid Build Coastguard Worker 
56*61c4878aSAndroid Build Coastguard Worker }  // namespace examples
57*61c4878aSAndroid Build Coastguard Worker 
58*61c4878aSAndroid Build Coastguard Worker namespace {
59*61c4878aSAndroid Build Coastguard Worker 
60*61c4878aSAndroid Build Coastguard Worker using AllocatorForTest = ::pw::allocator::test::AllocatorForTest<256>;
61*61c4878aSAndroid Build Coastguard Worker 
62*61c4878aSAndroid Build Coastguard Worker class BasicExampleTest : public ::testing::Test {
63*61c4878aSAndroid Build Coastguard Worker  protected:
64*61c4878aSAndroid Build Coastguard Worker   AllocatorForTest allocator_;
65*61c4878aSAndroid Build Coastguard Worker };
66*61c4878aSAndroid Build Coastguard Worker 
TEST_F(BasicExampleTest,AllocateNamedU32)67*61c4878aSAndroid Build Coastguard Worker TEST_F(BasicExampleTest, AllocateNamedU32) {
68*61c4878aSAndroid Build Coastguard Worker   void* ptr = examples::AllocateNamedU32(allocator_);
69*61c4878aSAndroid Build Coastguard Worker   ASSERT_NE(ptr, nullptr);
70*61c4878aSAndroid Build Coastguard Worker   examples::DeallocateNamedU32(allocator_, ptr);
71*61c4878aSAndroid Build Coastguard Worker }
72*61c4878aSAndroid Build Coastguard Worker 
TEST_F(BasicExampleTest,NewNamedU32)73*61c4878aSAndroid Build Coastguard Worker TEST_F(BasicExampleTest, NewNamedU32) {
74*61c4878aSAndroid Build Coastguard Worker   auto* named_u32 = examples::NewNamedU32(allocator_, "test1", 111);
75*61c4878aSAndroid Build Coastguard Worker   ASSERT_NE(named_u32, nullptr);
76*61c4878aSAndroid Build Coastguard Worker   EXPECT_STREQ(named_u32->name().data(), "test1");
77*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(named_u32->value(), 111U);
78*61c4878aSAndroid Build Coastguard Worker   examples::DeleteNamedU32(allocator_, named_u32);
79*61c4878aSAndroid Build Coastguard Worker }
80*61c4878aSAndroid Build Coastguard Worker 
TEST_F(BasicExampleTest,MakeNamedU32)81*61c4878aSAndroid Build Coastguard Worker TEST_F(BasicExampleTest, MakeNamedU32) {
82*61c4878aSAndroid Build Coastguard Worker   pw::UniquePtr<examples::NamedU32> named_u32 =
83*61c4878aSAndroid Build Coastguard Worker       examples::MakeNamedU32(allocator_, "test2", 222);
84*61c4878aSAndroid Build Coastguard Worker   ASSERT_NE(named_u32, nullptr);
85*61c4878aSAndroid Build Coastguard Worker   EXPECT_STREQ(named_u32->name().data(), "test2");
86*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(named_u32->value(), 222U);
87*61c4878aSAndroid Build Coastguard Worker }
88*61c4878aSAndroid Build Coastguard Worker 
89*61c4878aSAndroid Build Coastguard Worker }  // namespace
90