• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pw_allocator/tlsf_allocator.h"
16 
17 #include <cstdint>
18 
19 #include "pw_allocator/block_allocator_testing.h"
20 #include "pw_unit_test/framework.h"
21 
22 namespace {
23 
24 // Test fixtures.
25 
26 using ::pw::allocator::Layout;
27 using ::pw::allocator::test::Preallocation;
28 using BlockType = ::pw::allocator::TlsfBlock<uint16_t>;
29 using TlsfAllocator = ::pw::allocator::TlsfAllocator<BlockType>;
30 using BlockAllocatorTest =
31     ::pw::allocator::test::BlockAllocatorTest<TlsfAllocator>;
32 
33 class TlsfAllocatorTest : public BlockAllocatorTest {
34  public:
TlsfAllocatorTest()35   TlsfAllocatorTest() : BlockAllocatorTest(allocator_) {}
36 
37  private:
38   TlsfAllocator allocator_;
39 };
40 
41 // Unit tests.
42 
TEST_F(TlsfAllocatorTest,AutomaticallyInit)43 TEST_F(TlsfAllocatorTest, AutomaticallyInit) {
44   TlsfAllocator allocator(GetBytes());
45   AutomaticallyInit(allocator);
46 }
47 
TEST_F(TlsfAllocatorTest,ExplicitlyInit)48 TEST_F(TlsfAllocatorTest, ExplicitlyInit) {
49   TlsfAllocator allocator;
50   ExplicitlyInit(allocator);
51 }
52 
TEST_F(TlsfAllocatorTest,GetCapacity)53 TEST_F(TlsfAllocatorTest, GetCapacity) { GetCapacity(); }
54 
TEST_F(TlsfAllocatorTest,AllocateLarge)55 TEST_F(TlsfAllocatorTest, AllocateLarge) { AllocateLarge(); }
56 
TEST_F(TlsfAllocatorTest,AllocateSmall)57 TEST_F(TlsfAllocatorTest, AllocateSmall) { AllocateSmall(); }
58 
TEST_F(TlsfAllocatorTest,AllocateLargeAlignment)59 TEST_F(TlsfAllocatorTest, AllocateLargeAlignment) { AllocateLargeAlignment(); }
60 
TEST_F(TlsfAllocatorTest,AllocateAlignmentFailure)61 TEST_F(TlsfAllocatorTest, AllocateAlignmentFailure) {
62   AllocateAlignmentFailure();
63 }
64 
TEST_F(TlsfAllocatorTest,AllocatesBestCompatible)65 TEST_F(TlsfAllocatorTest, AllocatesBestCompatible) {
66   auto& allocator = GetAllocator({
67       {kLargeOuterSize, Preallocation::kFree},
68       {kSmallerOuterSize, Preallocation::kUsed},
69       {kSmallOuterSize, Preallocation::kFree},
70       {kSmallerOuterSize, Preallocation::kUsed},
71       {kLargerOuterSize, Preallocation::kFree},
72       {Preallocation::kSizeRemaining, Preallocation::kUsed},
73   });
74 
75   void* ptr1 = allocator.Allocate(Layout(kSmallInnerSize, 1));
76   EXPECT_LT(Fetch(1), ptr1);
77   EXPECT_LT(ptr1, Fetch(3));
78 
79   void* ptr2 = allocator.Allocate(Layout(kSmallInnerSize, 1));
80   EXPECT_LT(ptr2, Fetch(1));
81 
82   // A second small block fits in the leftovers of the first "Large" block.
83   void* ptr3 = allocator.Allocate(Layout(kSmallInnerSize, 1));
84   EXPECT_LT(ptr3, Fetch(1));
85 
86   allocator.Deallocate(ptr1);
87   allocator.Deallocate(ptr2);
88   allocator.Deallocate(ptr3);
89 }
90 
TEST_F(TlsfAllocatorTest,DeallocateNull)91 TEST_F(TlsfAllocatorTest, DeallocateNull) { DeallocateNull(); }
92 
TEST_F(TlsfAllocatorTest,DeallocateShuffled)93 TEST_F(TlsfAllocatorTest, DeallocateShuffled) { DeallocateShuffled(); }
94 
TEST_F(TlsfAllocatorTest,IterateOverBlocks)95 TEST_F(TlsfAllocatorTest, IterateOverBlocks) { IterateOverBlocks(); }
96 
TEST_F(TlsfAllocatorTest,ResizeNull)97 TEST_F(TlsfAllocatorTest, ResizeNull) { ResizeNull(); }
98 
TEST_F(TlsfAllocatorTest,ResizeLargeSame)99 TEST_F(TlsfAllocatorTest, ResizeLargeSame) { ResizeLargeSame(); }
100 
TEST_F(TlsfAllocatorTest,ResizeLargeSmaller)101 TEST_F(TlsfAllocatorTest, ResizeLargeSmaller) { ResizeLargeSmaller(); }
102 
TEST_F(TlsfAllocatorTest,ResizeLargeLarger)103 TEST_F(TlsfAllocatorTest, ResizeLargeLarger) { ResizeLargeLarger(); }
104 
TEST_F(TlsfAllocatorTest,ResizeLargeLargerFailure)105 TEST_F(TlsfAllocatorTest, ResizeLargeLargerFailure) {
106   ResizeLargeLargerFailure();
107 }
108 
TEST_F(TlsfAllocatorTest,ResizeSmallSame)109 TEST_F(TlsfAllocatorTest, ResizeSmallSame) { ResizeSmallSame(); }
110 
TEST_F(TlsfAllocatorTest,ResizeSmallSmaller)111 TEST_F(TlsfAllocatorTest, ResizeSmallSmaller) { ResizeSmallSmaller(); }
112 
TEST_F(TlsfAllocatorTest,ResizeSmallLarger)113 TEST_F(TlsfAllocatorTest, ResizeSmallLarger) { ResizeSmallLarger(); }
114 
TEST_F(TlsfAllocatorTest,ResizeSmallLargerFailure)115 TEST_F(TlsfAllocatorTest, ResizeSmallLargerFailure) {
116   ResizeSmallLargerFailure();
117 }
118 
TEST_F(TlsfAllocatorTest,MeasureFragmentation)119 TEST_F(TlsfAllocatorTest, MeasureFragmentation) { MeasureFragmentation(); }
120 
TEST_F(TlsfAllocatorTest,PoisonPeriodically)121 TEST_F(TlsfAllocatorTest, PoisonPeriodically) { PoisonPeriodically(); }
122 
123 }  // namespace
124