C/C++ API Reference
Loading...
Searching...
No Matches
pool.h
1// Copyright 2024 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#include <cstdint>
18
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_assert/assert.h"
24#include "pw_bytes/span.h"
25#include "pw_result/result.h"
26
27namespace pw::allocator {
28
30
36class Pool : public Deallocator {
37 public:
38 constexpr const Layout& layout() const { return layout_; }
39
45 void* Allocate() { return DoAllocate(); }
46
58 template <typename T, int&... kExplicitGuard, typename... Args>
59 [[nodiscard]] std::enable_if_t<!std::is_array_v<T>, T*> New(Args&&... args);
60
61 template <typename T,
62 int&... kExplicitGuard,
63 typename ElementType = std::remove_extent_t<T>>
64 [[nodiscard]] std::enable_if_t<pw::is_bounded_array_v<T>, ElementType*> New();
65
66 template <typename T,
67 int&... kExplicitGuard,
68 typename ElementType = std::remove_extent_t<T>>
69 [[nodiscard]] std::enable_if_t<pw::is_unbounded_array_v<T>, ElementType*>
70 New();
72
84 template <typename T, int&... kExplicitGuard, typename... Args>
85 std::enable_if_t<!std::is_array_v<T>, UniquePtr<T>> MakeUnique(
86 Args&&... args);
87
88 template <typename T>
89 std::enable_if_t<pw::is_bounded_array_v<T>, UniquePtr<T>> MakeUnique();
90
91 template <typename T>
92 std::enable_if_t<pw::is_unbounded_array_v<T>, UniquePtr<T>> MakeUnique();
94
95 protected:
96 constexpr Pool(const Capabilities& capabilities, const Layout& layout)
97 : Deallocator(capabilities), layout_(layout) {}
98
100 virtual void* DoAllocate() = 0;
101
102 private:
103 // Helper to create arrays.
104 template <typename ElementType>
105 [[nodiscard]] ElementType* NewArray(size_t count);
106
107 const Layout layout_;
108};
109
111
112// Template method implementations.
113
114template <typename T, int&... kExplicitGuard, typename... Args>
115std::enable_if_t<!std::is_array_v<T>, T*> Pool::New(Args&&... args) {
116 PW_ASSERT(Layout::Of<T>() == layout_);
117 void* ptr = Allocate();
118 return ptr != nullptr ? new (ptr) T(std::forward<Args>(args)...) : nullptr;
119}
120
121template <typename T, int&... kExplicitGuard, typename ElementType>
122std::enable_if_t<pw::is_bounded_array_v<T>, ElementType*> Pool::New() {
123 return NewArray<ElementType>(std::extent_v<T>);
124}
125
126template <typename T, int&... kExplicitGuard, typename ElementType>
127std::enable_if_t<pw::is_unbounded_array_v<T>, ElementType*> Pool::New() {
128 return NewArray<ElementType>(layout_.size() / sizeof(ElementType));
129}
130
131template <typename T, int&... kExplicitGuard, typename... Args>
132std::enable_if_t<!std::is_array_v<T>, UniquePtr<T>> Pool::MakeUnique(
133 Args&&... args) {
134 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *this);
135}
136
137template <typename T>
138std::enable_if_t<pw::is_bounded_array_v<T>, UniquePtr<T>> Pool::MakeUnique() {
139 using ElementType = std::remove_extent_t<T>;
140 return UniquePtr<T>(NewArray<ElementType>(std::extent_v<T>), *this);
141}
142
143template <typename T>
144std::enable_if_t<pw::is_unbounded_array_v<T>, UniquePtr<T>> Pool::MakeUnique() {
145 using ElementType = std::remove_extent_t<T>;
146 size_t size = layout_.size() / sizeof(ElementType);
147 return UniquePtr<T>(NewArray<ElementType>(size), size, *this);
148}
149
150// Helper to create arrays.
151template <typename ElementType>
152ElementType* Pool::NewArray(size_t count) {
153 Layout layout = Layout::Of<ElementType[]>(count);
154 PW_ASSERT(layout.size() == layout_.size());
155 PW_ASSERT(layout.alignment() <= layout_.alignment());
156 void* ptr = DoAllocate();
157 return ptr != nullptr ? new (ptr) ElementType[count] : nullptr;
158}
159
160} // namespace pw::allocator
Abstract interface for releasing memory.
Definition: deallocator.h:30
Definition: unique_ptr.h:44
Definition: capability.h:65
Definition: layout.h:64
Definition: pool.h:36
void * Allocate()
Definition: pool.h:45
virtual void * DoAllocate()=0