• 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/buddy_allocator.h"
16 
17 #include <array>
18 #include <cstddef>
19 
20 #include "pw_unit_test/framework.h"
21 
22 namespace {
23 
24 // Test fixtures.
25 
26 using ::pw::allocator::BuddyAllocator;
27 using ::pw::allocator::Layout;
28 
29 class BuddyAllocatorTest : public ::testing::Test {
30  protected:
31   static constexpr size_t kBufferSize = 0x400;
32   static constexpr size_t kMinChunkSize = 16;
33   static constexpr size_t kNumBuckets = 5;
34   alignas(kMinChunkSize) std::array<std::byte, kBufferSize> buffer_;
35 };
36 
37 // Unit tests.
38 
TEST_F(BuddyAllocatorTest,ExplicitlyInit)39 TEST_F(BuddyAllocatorTest, ExplicitlyInit) {
40   BuddyAllocator<kMinChunkSize, kNumBuckets> allocator;
41   allocator.Init(buffer_);
42 }
43 
TEST_F(BuddyAllocatorTest,AllocateSmall)44 TEST_F(BuddyAllocatorTest, AllocateSmall) {
45   BuddyAllocator<kMinChunkSize, kNumBuckets> allocator(buffer_);
46   void* ptr = allocator.Allocate(Layout(kMinChunkSize / 2, 1));
47   ASSERT_NE(ptr, nullptr);
48   allocator.Deallocate(ptr);
49 }
50 
TEST_F(BuddyAllocatorTest,AllocateAllChunks)51 TEST_F(BuddyAllocatorTest, AllocateAllChunks) {
52   BuddyAllocator<kMinChunkSize, kNumBuckets> allocator(buffer_);
53   pw::Vector<void*, kBufferSize / kMinChunkSize> ptrs;
54   while (true) {
55     void* ptr = allocator.Allocate(Layout(1, 1));
56     if (ptr == nullptr) {
57       break;
58     }
59     ptrs.push_back(ptr);
60   }
61   while (!ptrs.empty()) {
62     allocator.Deallocate(ptrs.back());
63     ptrs.pop_back();
64   }
65 }
66 
TEST_F(BuddyAllocatorTest,AllocateLarge)67 TEST_F(BuddyAllocatorTest, AllocateLarge) {
68   BuddyAllocator<kMinChunkSize, kNumBuckets> allocator(buffer_);
69   void* ptr = allocator.Allocate(Layout(48, 16));
70   ASSERT_NE(ptr, nullptr);
71   allocator.Deallocate(ptr);
72 }
73 
TEST_F(BuddyAllocatorTest,AllocateExcessiveSize)74 TEST_F(BuddyAllocatorTest, AllocateExcessiveSize) {
75   BuddyAllocator<kMinChunkSize, kNumBuckets> allocator(buffer_);
76   void* ptr = allocator.Allocate(Layout(384, 1));
77   EXPECT_EQ(ptr, nullptr);
78 }
79 
TEST_F(BuddyAllocatorTest,AllocateExcessiveAlignment)80 TEST_F(BuddyAllocatorTest, AllocateExcessiveAlignment) {
81   BuddyAllocator<kMinChunkSize, kNumBuckets> allocator(buffer_);
82   void* ptr = allocator.Allocate(Layout(48, 32));
83   EXPECT_EQ(ptr, nullptr);
84 }
85 
86 }  // namespace
87