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/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 if (object_ != nullptr) {
48 std::destroy_at(object_);
49 object_ = nullptr;
50 }
51 }
52
53 T* object_ = nullptr;
54};
55
56} // namespace internal
57
59
78class BumpAllocator : public Allocator {
79 public:
80 static constexpr Capabilities kCapabilities = kSkipsDestroy;
81
83 constexpr BumpAllocator() : Allocator(kCapabilities) {}
84
86 explicit BumpAllocator(ByteSpan region) : BumpAllocator() { Init(region); }
87
88 ~BumpAllocator() override { Reset(); }
89
91 void Init(ByteSpan region);
92
103 template <typename T, int&... kExplicitGuard, typename... Args>
104 T* NewOwned(Args&&... args) {
105 internal::Owned<T>* owned = New<internal::Owned<T>>();
106 T* ptr = owned != nullptr ? New<T>(std::forward<Args>(args)...) : nullptr;
107 if (ptr != nullptr) {
108 owned->set_object(ptr);
109 owned->set_next(owned_);
110 owned_ = owned;
111 }
112 return ptr;
113 }
114
125 template <typename T, int&... kExplicitGuard, typename... Args>
126 [[nodiscard]] UniquePtr<T> MakeUniqueOwned(Args&&... args) {
127 return UniquePtr<T>(NewOwned<T>(std::forward<Args>(args)...), *this);
128 }
129
130 private:
132 void* DoAllocate(Layout layout) override;
133
135 void DoDeallocate(void*) override;
136
138 size_t DoGetAllocated() const override { return allocated_; }
139
141 void Reset();
142
143 size_t allocated_ = 0;
144 ByteSpan remaining_;
145 internal::GenericOwned* owned_ = nullptr;
146};
147
149
150} // namespace pw::allocator
Definition: allocator.h:36
Definition: bump_allocator.h:78
T * NewOwned(Args &&... args)
Definition: bump_allocator.h:104
size_t DoGetAllocated() const override
Definition: bump_allocator.h:138
UniquePtr< T > MakeUniqueOwned(Args &&... args)
Definition: bump_allocator.h:126
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:83
BumpAllocator(ByteSpan region)
Constructs a BumpAllocator and initializes it.
Definition: bump_allocator.h:86
void DoDeallocate(void *) override
Definition: capability.h:64
Definition: layout.h:58
Type-erased base class to allow destroying a generic instance of Owned.
Definition: bump_allocator.h:26
Definition: bump_allocator.h:41