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
15 #include "pw_allocator/fallback_allocator.h"
16
17 #include "pw_allocator/capability.h"
18 #include "pw_assert/check.h"
19
20 namespace pw::allocator {
21
FallbackAllocator(Allocator & primary,Allocator & secondary)22 FallbackAllocator::FallbackAllocator(Allocator& primary, Allocator& secondary)
23 : Allocator(primary.capabilities() | secondary.capabilities()),
24 primary_(primary),
25 secondary_(secondary) {
26 PW_CHECK(primary.HasCapability(Capability::kImplementsRecognizes));
27 }
28
DoAllocate(Layout layout)29 void* FallbackAllocator::DoAllocate(Layout layout) {
30 void* ptr = primary_.Allocate(layout);
31 return ptr != nullptr ? ptr : secondary_.Allocate(layout);
32 }
33
DoDeallocate(void * ptr)34 void FallbackAllocator::DoDeallocate(void* ptr) {
35 if (Recognizes(primary_, ptr)) {
36 primary_.Deallocate(ptr);
37 } else {
38 secondary_.Deallocate(ptr);
39 }
40 }
41
DoDeallocate(void * ptr,Layout)42 void FallbackAllocator::DoDeallocate(void* ptr, Layout) { DoDeallocate(ptr); }
43
DoResize(void * ptr,size_t new_size)44 bool FallbackAllocator::DoResize(void* ptr, size_t new_size) {
45 return Recognizes(primary_, ptr) ? primary_.Resize(ptr, new_size)
46 : secondary_.Resize(ptr, new_size);
47 }
48
DoGetAllocated() const49 size_t FallbackAllocator::DoGetAllocated() const {
50 return primary_.GetAllocated() + secondary_.GetAllocated();
51 }
52
DoGetInfo(InfoType info_type,const void * ptr) const53 Result<Layout> FallbackAllocator::DoGetInfo(InfoType info_type,
54 const void* ptr) const {
55 Result<Layout> primary = GetInfo(primary_, info_type, ptr);
56 if (primary.ok() == (info_type != InfoType::kCapacity)) {
57 return primary;
58 }
59 Result<Layout> secondary = GetInfo(secondary_, info_type, ptr);
60 if (secondary.ok() == (info_type != InfoType::kCapacity)) {
61 return secondary;
62 }
63 if (info_type != InfoType::kCapacity) {
64 return Layout(primary->size() + secondary->size(),
65 std::max(primary->alignment(), secondary->alignment()));
66 } else {
67 return primary.status().IsUnimplemented() ? secondary : primary;
68 }
69 }
70
71 } // namespace pw::allocator
72