xref: /aosp_15_r20/external/pigweed/pw_multibuf/simple_allocator_test.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 "pw_multibuf/simple_allocator.h"
16 
17 #include "gtest/gtest.h"
18 #include "pw_allocator/null_allocator.h"
19 #include "pw_allocator/testing.h"
20 
21 namespace pw::multibuf {
22 namespace {
23 
24 using ::pw::allocator::test::AllocatorForTest;
25 
26 constexpr size_t kArbitraryBufferSize = 1024;
27 constexpr size_t kArbitraryMetaSize = 1024;
28 
TEST(SimpleAllocator,AllocateWholeDataAreaSizeSucceeds)29 TEST(SimpleAllocator, AllocateWholeDataAreaSizeSucceeds) {
30   std::array<std::byte, kArbitraryBufferSize> data_area;
31   AllocatorForTest<kArbitraryMetaSize> meta_alloc;
32   SimpleAllocator simple_allocator(data_area, meta_alloc);
33   std::optional<MultiBuf> buf = simple_allocator.Allocate(kArbitraryBufferSize);
34   ASSERT_TRUE(buf.has_value());
35   EXPECT_EQ(buf->size(), kArbitraryBufferSize);
36 }
37 
TEST(SimpleAllocator,AllocateContiguousWholeDataAreaSizeSucceeds)38 TEST(SimpleAllocator, AllocateContiguousWholeDataAreaSizeSucceeds) {
39   std::array<std::byte, kArbitraryBufferSize> data_area;
40   AllocatorForTest<kArbitraryMetaSize> meta_alloc;
41   SimpleAllocator simple_allocator(data_area, meta_alloc);
42   std::optional<MultiBuf> buf =
43       simple_allocator.AllocateContiguous(kArbitraryBufferSize);
44   ASSERT_TRUE(buf.has_value());
45   EXPECT_EQ(buf->Chunks().size(), 1U);
46   EXPECT_EQ(buf->size(), kArbitraryBufferSize);
47 }
48 
TEST(SimpleAllocator,AllocateContiguousHalfDataAreaSizeTwiceSucceeds)49 TEST(SimpleAllocator, AllocateContiguousHalfDataAreaSizeTwiceSucceeds) {
50   std::array<std::byte, kArbitraryBufferSize> data_area;
51   AllocatorForTest<kArbitraryMetaSize> meta_alloc;
52   SimpleAllocator simple_allocator(data_area, meta_alloc);
53   std::optional<MultiBuf> buf =
54       simple_allocator.AllocateContiguous(kArbitraryBufferSize / 2);
55   ASSERT_TRUE(buf.has_value());
56   EXPECT_EQ(buf->Chunks().size(), 1U);
57   EXPECT_EQ(buf->size(), kArbitraryBufferSize / 2);
58 
59   std::optional<MultiBuf> buf2 =
60       simple_allocator.AllocateContiguous(kArbitraryBufferSize / 2);
61   ASSERT_TRUE(buf2.has_value());
62   EXPECT_EQ(buf2->Chunks().size(), 1U);
63   EXPECT_EQ(buf2->size(), kArbitraryBufferSize / 2);
64 }
65 
TEST(SimpleAllocator,AllocateTooLargeReturnsNullopt)66 TEST(SimpleAllocator, AllocateTooLargeReturnsNullopt) {
67   std::array<std::byte, kArbitraryBufferSize> data_area;
68   AllocatorForTest<kArbitraryMetaSize> meta_alloc;
69   SimpleAllocator simple_allocator(data_area, meta_alloc);
70   std::optional<MultiBuf> buf =
71       simple_allocator.Allocate(kArbitraryBufferSize + 1);
72   EXPECT_FALSE(buf.has_value());
73   std::optional<MultiBuf> contiguous_buf =
74       simple_allocator.Allocate(kArbitraryBufferSize + 1);
75   EXPECT_FALSE(contiguous_buf.has_value());
76 }
77 
TEST(SimpleAllocator,AllocateZeroWithNoMetadataOrDataReturnsEmptyMultiBuf)78 TEST(SimpleAllocator, AllocateZeroWithNoMetadataOrDataReturnsEmptyMultiBuf) {
79   std::array<std::byte, 0> data_area;
80   pw::allocator::NullAllocator meta_alloc;
81   SimpleAllocator simple_allocator(data_area, meta_alloc);
82   std::optional<MultiBuf> buf = simple_allocator.Allocate(0);
83   ASSERT_TRUE(buf.has_value());
84   EXPECT_EQ(buf->size(), 0U);
85 }
86 
TEST(SimpleAllocator,AllocateWithNoMetadataRoomReturnsNullopt)87 TEST(SimpleAllocator, AllocateWithNoMetadataRoomReturnsNullopt) {
88   std::array<std::byte, kArbitraryBufferSize> data_area;
89   pw::allocator::NullAllocator meta_alloc;
90   SimpleAllocator simple_allocator(data_area, meta_alloc);
91   std::optional<MultiBuf> buf = simple_allocator.Allocate(1);
92   EXPECT_FALSE(buf.has_value());
93 }
94 
TEST(SimpleAllocator,SecondLargeAllocationFailsUntilFirstAllocationReleased)95 TEST(SimpleAllocator, SecondLargeAllocationFailsUntilFirstAllocationReleased) {
96   std::array<std::byte, kArbitraryBufferSize> data_area;
97   AllocatorForTest<kArbitraryMetaSize> meta_alloc;
98   SimpleAllocator simple_allocator(data_area, meta_alloc);
99   const size_t alloc_size = kArbitraryBufferSize / 2 + 1;
100   std::optional<MultiBuf> buf = simple_allocator.Allocate(alloc_size);
101   ASSERT_TRUE(buf.has_value());
102   EXPECT_EQ(buf->size(), alloc_size);
103   EXPECT_FALSE(simple_allocator.Allocate(alloc_size).has_value());
104   // Release the first buffer
105   buf = std::nullopt;
106   EXPECT_TRUE(simple_allocator.Allocate(alloc_size).has_value());
107 }
108 
TEST(SimpleAllocator,AllocateSkipsMiddleAllocations)109 TEST(SimpleAllocator, AllocateSkipsMiddleAllocations) {
110   std::array<std::byte, kArbitraryBufferSize> data_area;
111   AllocatorForTest<kArbitraryMetaSize> meta_alloc;
112   SimpleAllocator simple_allocator(data_area, meta_alloc);
113   const size_t alloc_size = kArbitraryBufferSize / 3;
114   std::optional<MultiBuf> buf1 = simple_allocator.Allocate(alloc_size);
115   std::optional<MultiBuf> buf2 = simple_allocator.Allocate(alloc_size);
116   std::optional<MultiBuf> buf3 = simple_allocator.Allocate(alloc_size);
117   EXPECT_TRUE(buf1.has_value());
118   EXPECT_TRUE(buf2.has_value());
119   EXPECT_TRUE(buf3.has_value());
120   buf1 = std::nullopt;
121   buf3 = std::nullopt;
122   // Now `buf2` holds the middle third of data_area
123   std::optional<MultiBuf> split = simple_allocator.Allocate(alloc_size * 2);
124   ASSERT_TRUE(split.has_value());
125   EXPECT_EQ(split->size(), alloc_size * 2);
126   EXPECT_EQ(split->Chunks().size(), 2U);
127 }
128 
TEST(SimpleAllocator,FailedAllocationDoesNotHoldOntoChunks)129 TEST(SimpleAllocator, FailedAllocationDoesNotHoldOntoChunks) {
130   std::array<std::byte, kArbitraryBufferSize> data_area;
131   AllocatorForTest<kArbitraryMetaSize> meta_alloc;
132   SimpleAllocator simple_allocator(data_area, meta_alloc);
133   const size_t alloc_size = kArbitraryBufferSize / 2;
134   std::optional<MultiBuf> buf1 = simple_allocator.Allocate(alloc_size);
135   std::optional<MultiBuf> buf2 = simple_allocator.Allocate(alloc_size);
136   EXPECT_TRUE(buf1.has_value());
137   EXPECT_TRUE(buf2.has_value());
138   buf1 = std::nullopt;
139   // When this allocation is attempted, it will initially create a chunk for
140   // the first empty region prior to failing.
141   EXPECT_FALSE(simple_allocator.Allocate(kArbitraryBufferSize).has_value());
142   buf2 = std::nullopt;
143   // Ensure that all chunk holds are released by attempting an allocation.
144   EXPECT_TRUE(simple_allocator.Allocate(kArbitraryBufferSize).has_value());
145 }
146 
147 }  // namespace
148 }  // namespace pw::multibuf
149