C/C++ API Reference
Loading...
Searching...
No Matches
bump_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/abstract_allocator.h"
19#include "pw_allocator/capability.h"
20#include "pw_bytes/span.h"
21
22namespace pw::allocator {
23namespace internal {
24
27 public:
28 virtual ~GenericOwned() = default;
29 void set_next(GenericOwned* next) { next_ = next; }
30 void Destroy() { DoDestroy(); }
31
32 private:
33 virtual void DoDestroy() = 0;
34
35 GenericOwned* next_ = nullptr;
36};
37
40template <typename T>
41class Owned : public GenericOwned {
42 public:
43 void set_object(T* object) { object_ = object; }
44
45 private:
46 void DoDestroy() override;
47
48 T* object_ = nullptr;
49};
50
51} // namespace internal
52
54
74 public:
75 static constexpr Capabilities kCapabilities = kSkipsDestroy;
76
78 constexpr BumpAllocator() : AbstractAllocator(kCapabilities) {}
79
81 explicit BumpAllocator(ByteSpan region) : BumpAllocator() { Init(region); }
82
83 ~BumpAllocator() override { Reset(); }
84
86 void Init(ByteSpan region);
87
98 template <typename T, int&... kExplicitGuard, typename... Args>
99 T* NewOwned(Args&&... args);
100
111 template <typename T, int&... kExplicitGuard, typename... Args>
112 [[nodiscard]] UniquePtr<T> MakeUniqueOwned(Args&&... args);
113
114 protected:
116 void* DoAllocate(Layout layout) override;
117
119 void DoDeallocate(void*) override;
120
122 size_t DoGetAllocated() const override { return allocated_; }
123
124 private:
126 void Reset();
127
128 size_t allocated_ = 0;
129 ByteSpan remaining_;
130 internal::GenericOwned* owned_ = nullptr;
131};
132
134
135// Template method implementations.
136
137namespace internal {
138
139template <typename T>
140void Owned<T>::DoDestroy() {
141 if (object_ != nullptr) {
142 std::destroy_at(object_);
143 object_ = nullptr;
144 }
145}
146
147} // namespace internal
148
149template <typename T, int&... kExplicitGuard, typename... Args>
150T* BumpAllocator::NewOwned(Args&&... args) {
151 internal::Owned<T>* owned = New<internal::Owned<T>>();
152 T* ptr = owned != nullptr ? New<T>(std::forward<Args>(args)...) : nullptr;
153 if (ptr != nullptr) {
154 owned->set_object(ptr);
155 owned->set_next(owned_);
156 owned_ = owned;
157 }
158 return ptr;
159}
160
161template <typename T, int&... kExplicitGuard, typename... Args>
163 return UniquePtr<T>(NewOwned<T>(std::forward<Args>(args)...), *this);
164}
165
166} // namespace pw::allocator
Definition: unique_ptr.h:44
Definition: abstract_allocator.h:30
Definition: bump_allocator.h:73
T * NewOwned(Args &&... args)
Definition: bump_allocator.h:150
size_t DoGetAllocated() const override
Definition: bump_allocator.h:122
UniquePtr< T > MakeUniqueOwned(Args &&... args)
Definition: bump_allocator.h:162
void Init(ByteSpan region)
Sets the memory region to be used by the allocator.
void * DoAllocate(Layout layout) override
constexpr BumpAllocator()
Constructs a BumpAllocator without initializing it.
Definition: bump_allocator.h:78
BumpAllocator(ByteSpan region)
Constructs a BumpAllocator and initializes it.
Definition: bump_allocator.h:81
void DoDeallocate(void *) override
Definition: capability.h:65
Definition: layout.h:64
Type-erased base class to allow destroying a generic instance of Owned.
Definition: bump_allocator.h:26
Definition: bump_allocator.h:41