C/C++ API Reference
Loading...
Searching...
No Matches
testing.h
1// Copyright 2023 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#pragma once
15
16#include <cstddef>
17#include <mutex>
18
19#include "pw_allocator/allocator.h"
20#include "pw_allocator/buffer.h"
21#include "pw_allocator/first_fit.h"
22#include "pw_allocator/hardening.h"
23#include "pw_allocator/metrics.h"
24#include "pw_allocator/tracking_allocator.h"
25#include "pw_assert/assert.h"
26#include "pw_bytes/span.h"
27#include "pw_result/result.h"
28#include "pw_status/status.h"
29#include "pw_tokenizer/tokenize.h"
30#include "pw_unit_test/framework.h"
31
32namespace pw::allocator::test {
33
35
36static_assert(Hardening::kIncludesDebugChecks,
37 "Tests must use a config that enables strict validation");
38
39// A token that can be used in tests.
40inline constexpr pw::tokenizer::Token kToken = PW_TOKENIZE_STRING("test");
41
43template <typename BlockType>
44void FreeAll(typename BlockType::Range range) {
45 BlockType* block = *(range.begin());
46 if (block == nullptr) {
47 return;
48 }
49
50 // Rewind to the first block.
51 BlockType* prev = block->Prev();
52 while (prev != nullptr) {
53 block = prev;
54 prev = block->Prev();
55 }
56
57 // Free and merge blocks.
58 while (block != nullptr) {
59 if (!block->IsFree()) {
60 auto result = BlockType::Free(std::move(block));
61 block = result.block();
62 }
63 block = block->Next();
64 }
65}
66
68template <size_t kBufferSize,
69 typename BlockType_ = FirstFitBlock<uint32_t>,
70 typename MetricsType = internal::AllMetrics>
72 public:
73 using BlockType = BlockType_;
75
76 // Since the unbderlying first-fit allocator uses an intrusive free list, all
77 // allocations will be at least this size.
78 static constexpr size_t kMinSize = BlockType::kAlignment;
79
81 : Allocator(AllocatorType::kCapabilities), tracker_(kToken, *allocator_) {
83 allocator_->Init(allocator_.as_bytes());
84 }
85
86 ~AllocatorForTest() override { FreeAll<BlockType>(blocks()); }
87
88 typename BlockType::Range blocks() const { return allocator_->blocks(); }
89 typename BlockType::Range blocks() { return allocator_->blocks(); }
90
91 const metric::Group& metric_group() const { return tracker_.metric_group(); }
92 metric::Group& metric_group() { return tracker_.metric_group(); }
93
94 const MetricsType& metrics() const { return tracker_.metrics(); }
95
96 size_t allocate_size() const { return allocate_size_; }
97 void* deallocate_ptr() const { return deallocate_ptr_; }
98 size_t deallocate_size() const { return deallocate_size_; }
99 void* resize_ptr() const { return resize_ptr_; }
100 size_t resize_old_size() const { return resize_old_size_; }
101 size_t resize_new_size() const { return resize_new_size_; }
102
105 allocate_size_ = 0;
106 deallocate_ptr_ = nullptr;
107 deallocate_size_ = 0;
108 resize_ptr_ = nullptr;
109 resize_old_size_ = 0;
110 resize_new_size_ = 0;
111 }
112
114 void Exhaust() {
115 for (auto* block : allocator_->blocks()) {
116 if (block->IsFree()) {
117 auto result = BlockType::AllocLast(std::move(block),
118 Layout(block->InnerSize(), 1));
119 PW_ASSERT(result.status() == OkStatus());
120
121 using Prev = internal::GenericBlockResult::Prev;
122 PW_ASSERT(result.prev() == Prev::kUnchanged);
123
124 using Next = internal::GenericBlockResult::Next;
125 PW_ASSERT(result.next() == Next::kUnchanged);
126 }
127 }
128 }
129
132 return allocator_->MeasureFragmentation();
133 }
134
135 private:
137 void* DoAllocate(Layout layout) override {
138 allocate_size_ = layout.size();
139 void* ptr = tracker_.Allocate(layout);
140 return ptr;
141 }
142
144 void DoDeallocate(void* ptr) override {
145 Result<Layout> requested = GetRequestedLayout(ptr);
146 deallocate_ptr_ = ptr;
147 deallocate_size_ = requested.ok() ? requested->size() : 0;
148 tracker_.Deallocate(ptr);
149 }
150
152 void DoDeallocate(void* ptr, Layout) override { DoDeallocate(ptr); }
153
155 bool DoResize(void* ptr, size_t new_size) override {
156 Result<Layout> requested = GetRequestedLayout(ptr);
157 resize_ptr_ = ptr;
158 resize_old_size_ = requested.ok() ? requested->size() : 0;
159 resize_new_size_ = new_size;
160 return tracker_.Resize(ptr, new_size);
161 }
162
164 size_t DoGetAllocated() const override { return tracker_.GetAllocated(); }
165
167 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
168 return GetInfo(tracker_, info_type, ptr);
169 }
170
173 size_t allocate_size_;
174 void* deallocate_ptr_;
175 size_t deallocate_size_;
176 void* resize_ptr_;
177 size_t resize_old_size_;
178 size_t resize_new_size_;
179};
180
182
183} // namespace pw::allocator::test
Definition: allocator.h:36
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
Definition: poll.h:25
Definition: detailed_block.h:88
Definition: first_fit.h:41
Definition: layout.h:58
Definition: tracking_allocator.h:49
Definition: buffer.h:54
An AllocatorForTest that is automatically initialized on construction.
Definition: testing.h:71
size_t DoGetAllocated() const override
Definition: testing.h:164
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: testing.h:167
void * DoAllocate(Layout layout) override
Definition: testing.h:137
Fragmentation MeasureFragmentation() const
Returns fragmentation information for the block allocator's memory region.
Definition: testing.h:131
void ResetParameters()
Resets the recorded parameters to an initial state.
Definition: testing.h:104
void DoDeallocate(void *ptr) override
Definition: testing.h:144
void Exhaust()
Allocates all the memory from this object.
Definition: testing.h:114
bool DoResize(void *ptr, size_t new_size) override
Definition: testing.h:155
void DoDeallocate(void *ptr, Layout) override
Definition: testing.h:152
void FreeAll(typename BlockType::Range range)
Free all the blocks reachable by the given block. Useful for test cleanup.
Definition: testing.h:44
#define PW_TOKENIZE_STRING(...)
Definition: tokenize.h:67
constexpr Status OkStatus()
Definition: status.h:235
Definition: fragmentation.h:46
Definition: metrics.h:173