C/C++ API Reference
Loading...
Searching...
No Matches
fault_injecting_allocator.h
1// Copyright 2025 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 "pw_allocator/allocator.h"
17#include "pw_allocator/first_fit.h"
18
19namespace pw::allocator::test {
20
22
29 public:
30 explicit constexpr FaultInjectingAllocator(Allocator& allocator)
31 : Allocator(allocator.capabilities()),
32 allocator_(allocator),
33 allow_allocate_(true),
34 allow_resize_(true),
35 allow_reallocate_(true) {}
36
38 void EnableAll() {
39 allow_allocate_ = allow_resize_ = allow_reallocate_ = true;
40 }
41
44 void DisableAll() {
45 allow_allocate_ = allow_resize_ = allow_reallocate_ = false;
46 }
47
49 void EnableAllocate() { allow_allocate_ = true; }
50
52 void DisableAllocate() { allow_allocate_ = false; }
53
55 void EnableResize() { allow_resize_ = true; }
56
58 void DisableResize() { allow_resize_ = false; }
59
61 void EnableReallocate() { allow_reallocate_ = true; }
62
64 void DisableReallocate() { allow_reallocate_ = false; }
65
67 Allocator& real_allocator() { return allocator_; }
68
69 private:
70 void* DoAllocate(Layout layout) override {
71 return allow_allocate_ ? allocator_.Allocate(layout) : nullptr;
72 }
73 void DoDeallocate(void* ptr) override { allocator_.Deallocate(ptr); }
74
75 bool DoResize(void* ptr, size_t new_size) override {
76 return allow_resize_ && allocator_.Resize(ptr, new_size);
77 }
78
79 void* DoReallocate(void* ptr, Layout new_layout) override {
80 return allow_reallocate_ ? allocator_.Reallocate(ptr, new_layout) : nullptr;
81 }
82
83 Allocator& allocator_;
84
85 // Flags for whether to allow calls to pass through.
86 bool allow_allocate_;
87 bool allow_resize_;
88 bool allow_reallocate_;
89};
90
92
93} // namespace pw::allocator::test
Definition: allocator.h:36
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:287
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:321
void * Allocate(Layout layout)
Definition: allocator.h:44
Definition: layout.h:58
Definition: fault_injecting_allocator.h:28
void DisableReallocate()
Return nullptr for Reallocate calls.
Definition: fault_injecting_allocator.h:64
void EnableAllocate()
Forward Allocate calls to the allocator.
Definition: fault_injecting_allocator.h:49
void EnableResize()
Forward Resize calls to the allocator.
Definition: fault_injecting_allocator.h:55
void * DoAllocate(Layout layout) override
Definition: fault_injecting_allocator.h:70
void DisableResize()
Return false for Resize calls.
Definition: fault_injecting_allocator.h:58
Allocator & real_allocator()
Returns a reference to the wrapped allocator.
Definition: fault_injecting_allocator.h:67
void DisableAll()
Definition: fault_injecting_allocator.h:44
bool DoResize(void *ptr, size_t new_size) override
Definition: fault_injecting_allocator.h:75
void EnableReallocate()
Forward Reallocate calls to the allocator.
Definition: fault_injecting_allocator.h:61
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: fault_injecting_allocator.h:79
void DisableAllocate()
Return nullptr for Allocate calls.
Definition: fault_injecting_allocator.h:52
void EnableAll()
Forward Allocate, Resize, and Reallocate calls to the allocator.
Definition: fault_injecting_allocator.h:38
void Deallocate(void *ptr)
Definition: deallocator.h:60