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 Pool(const Capabilities& capabilities, const Layout& layout)
39 : Deallocator(capabilities), layout_(layout) {}
40
41 constexpr const Layout& layout() const { return layout_; }
42
48 void* Allocate() { return DoAllocate(); }
49
61 template <typename T,
62 int&... kExplicitGuard,
63 std::enable_if_t<!std::is_array_v<T>, int> = 0,
64 typename... Args>
65 [[nodiscard]] T* New(Args&&... args) {
66 PW_ASSERT(Layout::Of<T>() == layout_);
67 void* ptr = Allocate();
68 return ptr != nullptr ? new (ptr) T(std::forward<Args>(args)...) : nullptr;
69 }
70
71 template <typename T,
72 int&... kExplicitGuard,
73 typename ElementType = std::remove_extent_t<T>,
74 std::enable_if_t<is_bounded_array_v<T>, int> = 0>
75 [[nodiscard]] ElementType* New() {
76 return NewArray<ElementType>(std::extent_v<T>);
77 }
78
79 template <typename T,
80 int&... kExplicitGuard,
81 typename ElementType = std::remove_extent_t<T>,
82 std::enable_if_t<is_unbounded_array_v<T>, int> = 0>
83 [[nodiscard]] ElementType* New() {
84 return NewArray<ElementType>(layout_.size() / sizeof(ElementType));
85 }
87
99 template <typename T,
100 int&... kExplicitGuard,
101 std::enable_if_t<!std::is_array_v<T>, int> = 0,
102 typename... Args>
103 UniquePtr<T> MakeUnique(Args&&... args) {
104 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *this);
105 }
106
107 template <typename T,
108 int&... kExplicitGuard,
109 std::enable_if_t<is_bounded_array_v<T>, int> = 0>
110 UniquePtr<T> MakeUnique() {
111 using ElementType = std::remove_extent_t<T>;
112 return UniquePtr<T>(NewArray<ElementType>(std::extent_v<T>), *this);
113 }
114
115 template <typename T,
116 int&... kExplicitGuard,
117 std::enable_if_t<is_unbounded_array_v<T>, int> = 0>
118 UniquePtr<T> MakeUnique() {
119 using ElementType = std::remove_extent_t<T>;
120 size_t size = layout_.size() / sizeof(ElementType);
121 return UniquePtr<T>(NewArray<ElementType>(size), size, *this);
122 }
124
125 private:
127 virtual void* DoAllocate() = 0;
128
129 // Helper to create arrays.
130 template <typename ElementType>
131 [[nodiscard]] ElementType* NewArray(size_t count) {
132 Layout layout = Layout::Of<ElementType[]>(count);
133 PW_ASSERT(layout.size() == layout_.size());
134 PW_ASSERT(layout.alignment() <= layout_.alignment());
135 void* ptr = DoAllocate();
136 return ptr != nullptr ? new (ptr) ElementType[count] : nullptr;
137 }
138
139 const Layout layout_;
140};
141
143
144} // namespace pw::allocator
Abstract interface for releasing memory.
Definition: deallocator.h:28
Definition: unique_ptr.h:44
Definition: capability.h:65
Definition: layout.h:58
Definition: pool.h:36
void * Allocate()
Definition: pool.h:48
UniquePtr< T > MakeUnique(Args &&... args)
Definition: pool.h:103
T * New(Args &&... args)
Definition: pool.h:65
virtual void * DoAllocate()=0
Virtual Allocate function that can be overridden by derived classes.
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.