Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
27 public:
28 explicit constexpr FaultInjectingAllocator(Allocator& allocator)
29 : Allocator(allocator.capabilities()),
30 allocator_(allocator),
31 allow_allocate_(true),
32 allow_resize_(true),
33 allow_reallocate_(true) {}
34
36 void EnableAll() {
37 allow_allocate_ = allow_resize_ = allow_reallocate_ = true;
38 }
39
42 void DisableAll() {
43 allow_allocate_ = allow_resize_ = allow_reallocate_ = false;
44 }
45
47 void EnableAllocate() { allow_allocate_ = true; }
48
50 void DisableAllocate() { allow_allocate_ = false; }
51
53 void EnableResize() { allow_resize_ = true; }
54
56 void DisableResize() { allow_resize_ = false; }
57
59 void EnableReallocate() { allow_reallocate_ = true; }
60
62 void DisableReallocate() { allow_reallocate_ = false; }
63
65 Allocator& real_allocator() { return allocator_; }
66
67 private:
68 void* DoAllocate(Layout layout) override {
69 return allow_allocate_ ? allocator_.Allocate(layout) : nullptr;
70 }
71 void DoDeallocate(void* ptr) override { allocator_.Deallocate(ptr); }
72
73 bool DoResize(void* ptr, size_t new_size) override {
74 return allow_resize_ && allocator_.Resize(ptr, new_size);
75 }
76
77 void* DoReallocate(void* ptr, Layout new_layout) override {
78 return allow_reallocate_ ? allocator_.Reallocate(ptr, new_layout) : nullptr;
79 }
80
81 Allocator& allocator_;
82
83 // Flags for whether to allow calls to pass through.
84 bool allow_allocate_;
85 bool allow_resize_;
86 bool allow_reallocate_;
87};
88
89} // namespace pw::allocator::test
Definition: allocator.h:34
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:285
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:319
void * Allocate(Layout layout)
Definition: allocator.h:42
void Deallocate(void *ptr)
Definition: deallocator.h:58
Definition: layout.h:56
Definition: fault_injecting_allocator.h:26
void DisableReallocate()
Return nullptr for Reallocate calls.
Definition: fault_injecting_allocator.h:62
void EnableAllocate()
Forward Allocate calls to the allocator.
Definition: fault_injecting_allocator.h:47
void EnableResize()
Forward Resize calls to the allocator.
Definition: fault_injecting_allocator.h:53
void * DoAllocate(Layout layout) override
Definition: fault_injecting_allocator.h:68
void DisableResize()
Return false for Resize calls.
Definition: fault_injecting_allocator.h:56
Allocator & real_allocator()
Returns a reference to the wrapped allocator.
Definition: fault_injecting_allocator.h:65
void DisableAll()
Definition: fault_injecting_allocator.h:42
bool DoResize(void *ptr, size_t new_size) override
Definition: fault_injecting_allocator.h:73
void EnableReallocate()
Forward Reallocate calls to the allocator.
Definition: fault_injecting_allocator.h:59
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: fault_injecting_allocator.h:77
void DisableAllocate()
Return nullptr for Allocate calls.
Definition: fault_injecting_allocator.h:50
void EnableAll()
Forward Allocate, Resize, and Reallocate calls to the allocator.
Definition: fault_injecting_allocator.h:36