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 <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
76class BumpAllocator : public Allocator {
77 public:
78 static constexpr Capabilities kCapabilities = kSkipsDestroy;
79
81 constexpr BumpAllocator() : Allocator(kCapabilities) {}
82
84 explicit BumpAllocator(ByteSpan region) : BumpAllocator() { Init(region); }
85
86 ~BumpAllocator() override { Reset(); }
87
89 void Init(ByteSpan region);
90
101 template <typename T, int&... kExplicitGuard, typename... Args>
102 T* NewOwned(Args&&... args) {
103 internal::Owned<T>* owned = New<internal::Owned<T>>();
104 T* ptr = owned != nullptr ? New<T>(std::forward<Args>(args)...) : nullptr;
105 if (ptr != nullptr) {
106 owned->set_object(ptr);
107 owned->set_next(owned_);
108 owned_ = owned;
109 }
110 return ptr;
111 }
112
123 template <typename T, int&... kExplicitGuard, typename... Args>
124 [[nodiscard]] UniquePtr<T> MakeUniqueOwned(Args&&... args) {
125 return WrapUnique<T>(NewOwned<T>(std::forward<Args>(args)...));
126 }
127
128 private:
130 void* DoAllocate(Layout layout) override;
131
133 void DoDeallocate(void*) override;
134
136 size_t DoGetAllocated() const override { return allocated_; }
137
139 void Reset();
140
141 size_t allocated_ = 0;
142 ByteSpan remaining_;
143 internal::GenericOwned* owned_ = nullptr;
144};
145
146} // namespace pw::allocator
Definition: allocator.h:34
Definition: bump_allocator.h:76
T * NewOwned(Args &&... args)
Definition: bump_allocator.h:102
size_t DoGetAllocated() const override
Definition: bump_allocator.h:136
UniquePtr< T > MakeUniqueOwned(Args &&... args)
Definition: bump_allocator.h:124
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:81
BumpAllocator(ByteSpan region)
Constructs a BumpAllocator and initializes it.
Definition: bump_allocator.h:84
void DoDeallocate(void *) override
Definition: capability.h:62
Definition: layout.h:56
Type-erased base class to allow destroying a generic instance of Owned.
Definition: bump_allocator.h:26
Definition: bump_allocator.h:41