Pigweed
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 "pw_allocator/allocator.h"
17#include "pw_allocator/capability.h"
18#include "pw_bytes/span.h"
19
20namespace pw::allocator {
21namespace internal {
22
25 public:
26 virtual ~GenericOwned() = default;
27 void set_next(GenericOwned* next) { next_ = next; }
28 void Destroy() { DoDestroy(); }
29
30 private:
31 virtual void DoDestroy() = 0;
32
33 GenericOwned* next_ = nullptr;
34};
35
38template <typename T>
39class Owned : public GenericOwned {
40 public:
41 void set_object(T* object) { object_ = object; }
42
43 private:
44 void DoDestroy() override {
45 if (object_ != nullptr) {
46 std::destroy_at(object_);
47 object_ = nullptr;
48 }
49 }
50
51 T* object_ = nullptr;
52};
53
54} // namespace internal
55
74class BumpAllocator : public Allocator {
75 public:
76 static constexpr Capabilities kCapabilities = kSkipsDestroy;
77
79 constexpr BumpAllocator() : Allocator(kCapabilities) {}
80
82 explicit BumpAllocator(ByteSpan region) : BumpAllocator() { Init(region); }
83
84 ~BumpAllocator() override { Reset(); }
85
87 void Init(ByteSpan region);
88
99 template <typename T, int&... ExplicitGuard, typename... Args>
100 T* NewOwned(Args&&... args) {
101 internal::Owned<T>* owned = New<internal::Owned<T>>();
102 T* ptr = owned != nullptr ? New<T>(std::forward<Args>(args)...) : nullptr;
103 if (ptr != nullptr) {
104 owned->set_object(ptr);
105 owned->set_next(owned_);
106 owned_ = owned;
107 }
108 return ptr;
109 }
110
121 template <typename T, int&... ExplicitGuard, typename... Args>
122 [[nodiscard]] UniquePtr<T> MakeUniqueOwned(Args&&... args) {
123 return WrapUnique<T>(NewOwned<T>(std::forward<Args>(args)...));
124 }
125
126 private:
128 void* DoAllocate(Layout layout) override;
129
131 void DoDeallocate(void*) override;
132
134 void Reset();
135
136 ByteSpan remaining_;
137 internal::GenericOwned* owned_ = nullptr;
138};
139
140} // namespace pw::allocator
Definition: allocator.h:32
Definition: unique_ptr.h:47
Definition: bump_allocator.h:74
T * NewOwned(Args &&... args)
Definition: bump_allocator.h:100
UniquePtr< T > MakeUniqueOwned(Args &&... args)
Definition: bump_allocator.h:122
void Init(ByteSpan region)
Sets the memory region to be used by the allocator.
constexpr BumpAllocator()
Constructs a BumpAllocator without initializing it.
Definition: bump_allocator.h:79
BumpAllocator(ByteSpan region)
Constructs a BumpAllocator and initializes it.
Definition: bump_allocator.h:82
Definition: capability.h:64
Definition: layout.h:39
Type-erased base class to allow destroying a generic instance of Owned.
Definition: bump_allocator.h:24
Definition: bump_allocator.h:39