xref: /aosp_15_r20/external/pigweed/pw_allocator/examples/linker_sections.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <array>
16 #include <cstdint>
17 #include <string_view>
18 
19 #include "examples/named_u32.h"
20 #include "pw_allocator/allocator.h"
21 #include "pw_allocator/block/detailed_block.h"
22 #include "pw_allocator/first_fit.h"
23 #include "pw_allocator/worst_fit.h"
24 #include "pw_unit_test/framework.h"
25 
26 namespace examples {
27 
28 // The "real" `PW_PLACE_IN_SECTION` can be found in pw_preprocessor/compiler.h.
29 // For the purposes of keeping this example simple and free of linker-scripts,
30 // the macro is replaced with a no-op version.
31 #ifdef PW_PLACE_IN_SECTION
32 #undef PW_PLACE_IN_SECTION
33 #endif
34 #define PW_PLACE_IN_SECTION(section)
35 
36 // DOCSTAG: [pw_allocator-examples-linker_sections-injection]
37 class NamedU32Factory {
38  public:
NamedU32Factory(pw::Allocator & allocator)39   explicit NamedU32Factory(pw::Allocator& allocator) : allocator_(allocator) {}
40 
MakeNamedU32(std::string_view name,uint32_t value)41   auto MakeNamedU32(std::string_view name, uint32_t value) {
42     return allocator_.MakeUnique<NamedU32>(name, value);
43   }
44 
45  private:
46   pw::Allocator& allocator_;
47 };
48 // DOCSTAG: [pw_allocator-examples-linker_sections-injection]
49 
50 // DOCSTAG: [pw_allocator-examples-linker_sections-placement]
51 // Set up an object that allocates from SRAM memory.
52 PW_PLACE_IN_SECTION(".sram") std::array<std::byte, 0x1000> sram_buffer;
53 using SramBlock = ::pw::allocator::FirstFitBlock<uint16_t>;
54 pw::allocator::FirstFitAllocator<SramBlock> sram_allocator(sram_buffer);
55 NamedU32Factory sram_factory(sram_allocator);
56 
57 // Set up an object that allocates from PSRAM memory.
58 PW_PLACE_IN_SECTION(".psram") std::array<std::byte, 0x2000> psram_buffer;
59 using PsramBlock = ::pw::allocator::WorstFitBlock<uint32_t>;
60 pw::allocator::WorstFitAllocator<PsramBlock> psram_allocator(psram_buffer);
61 NamedU32Factory psram_factory(psram_allocator);
62 // DOCSTAG: [pw_allocator-examples-linker_sections-placement]
63 
64 }  // namespace examples
65 
66 namespace pw::allocator {
67 
TEST(LinkerSectionExample,MakeObjects)68 TEST(LinkerSectionExample, MakeObjects) {
69   auto result1 = examples::sram_factory.MakeNamedU32("1", 1);
70   EXPECT_NE(result1, nullptr);
71 
72   auto result2 = examples::psram_factory.MakeNamedU32("2", 2);
73   EXPECT_NE(result2, nullptr);
74 }
75 
76 }  // namespace pw::allocator
77