Pigweed
Loading...
Searching...
No Matches
allocator.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
18#include "pw_allocator/as_pmr_allocator.h"
19#include "pw_allocator/capability.h"
20#include "pw_allocator/deallocator.h"
21#include "pw_allocator/layout.h"
22#include "pw_allocator/unique_ptr.h"
23#include "pw_result/result.h"
24
25namespace pw {
26
32class Allocator : public Deallocator {
33 public:
40 void* Allocate(Layout layout) {
41 return layout.size() != 0 ? DoAllocate(layout) : nullptr;
42 }
43
51 template <typename T, int&... ExplicitGuard, typename... Args>
52 T* New(Args&&... args) {
53 void* ptr = Allocate(Layout::Of<T>());
54 return ptr != nullptr ? new (ptr) T(std::forward<Args>(args)...) : nullptr;
55 }
56
64 template <typename T, int&... ExplicitGuard, typename... Args>
65 [[nodiscard]] UniquePtr<T> MakeUnique(Args&&... args) {
66 return Deallocator::WrapUnique<T>(New<T>(std::forward<Args>(args)...));
67 }
68
81 bool Resize(void* ptr, size_t new_size) {
82 return ptr != nullptr && new_size != 0 && DoResize(ptr, new_size);
83 }
84
88 bool Resize(void* ptr, Layout layout, size_t new_size) {
89 return ptr != nullptr && new_size != 0 && DoResize(ptr, layout, new_size);
90 }
91
115 void* Reallocate(void* ptr, Layout new_layout) {
116 if (new_layout.size() == 0) {
117 return nullptr;
118 }
119 if (ptr == nullptr) {
120 return Allocate(new_layout);
121 }
122 return DoReallocate(ptr, new_layout);
123 }
124
128 void* Reallocate(void* ptr, Layout old_layout, size_t new_size) {
129 if (new_size == 0) {
130 return nullptr;
131 }
132 if (ptr == nullptr) {
133 return Allocate(Layout(new_size, old_layout.alignment()));
134 }
135 return DoReallocate(ptr, old_layout, new_size);
136 }
137
147 return allocator::AsPmrAllocator(*this);
148 }
149
150 protected:
152 constexpr Allocator() = default;
153
154 explicit constexpr Allocator(const Capabilities& capabilities)
155 : Deallocator(capabilities) {}
156
157 private:
162 virtual void* DoAllocate(Layout layout) = 0;
163
171 virtual bool DoResize(void* /*ptr*/, size_t /*new_size*/) { return false; }
172
176 virtual bool DoResize(void*, Layout, size_t) { return false; }
177
187 virtual void* DoReallocate(void* ptr, Layout new_layout);
188
192 virtual void* DoReallocate(void* ptr, Layout old_layout, size_t new_size);
193};
194
195namespace allocator {
196
197// Alias for module consumers using the older name for the above type.
198using Allocator = ::pw::Allocator;
199
200} // namespace allocator
201} // namespace pw
Definition: allocator.h:32
T * New(Args &&... args)
Definition: allocator.h:52
UniquePtr< T > MakeUnique(Args &&... args)
Definition: allocator.h:65
allocator::AsPmrAllocator as_pmr()
Definition: allocator.h:146
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:81
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:115
void * Reallocate(void *ptr, Layout old_layout, size_t new_size)
Definition: allocator.h:128
bool Resize(void *ptr, Layout layout, size_t new_size)
Definition: allocator.h:88
void * Allocate(Layout layout)
Definition: allocator.h:40
Abstract interface for releasing memory.
Definition: deallocator.h:26
Definition: unique_ptr.h:47
Definition: as_pmr_allocator.h:79
Definition: capability.h:64
Definition: layout.h:39
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27