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_assert/assert.h"
23#include "pw_bytes/span.h"
24#include "pw_result/result.h"
25
26namespace pw::allocator {
27
29
35class Pool : public Deallocator {
36 public:
37 constexpr Pool(const Capabilities& capabilities, const Layout& layout)
38 : Deallocator(capabilities), layout_(layout) {}
39
40 constexpr const Layout& layout() const { return layout_; }
41
47 void* Allocate() { return DoAllocate(); }
48
60 template <typename T,
61 int&... kExplicitGuard,
62 std::enable_if_t<!std::is_array_v<T>, int> = 0,
63 typename... Args>
64 [[nodiscard]] T* New(Args&&... args) {
65 PW_ASSERT(Layout::Of<T>() == layout_);
66 void* ptr = Allocate();
67 return ptr != nullptr ? new (ptr) T(std::forward<Args>(args)...) : nullptr;
68 }
69
70 template <typename T,
71 int&... kExplicitGuard,
72 typename ElementType = std::remove_extent_t<T>,
73 std::enable_if_t<is_bounded_array_v<T>, int> = 0>
74 [[nodiscard]] ElementType* New() {
75 return NewArray<ElementType>(std::extent_v<T>);
76 }
77
78 template <typename T,
79 int&... kExplicitGuard,
80 typename ElementType = std::remove_extent_t<T>,
81 std::enable_if_t<is_unbounded_array_v<T>, int> = 0>
82 [[nodiscard]] ElementType* New() {
83 return NewArray<ElementType>(layout_.size() / sizeof(ElementType));
84 }
86
98 template <typename T,
99 int&... kExplicitGuard,
100 std::enable_if_t<!std::is_array_v<T>, int> = 0,
101 typename... Args>
102 UniquePtr<T> MakeUnique(Args&&... args) {
103 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *this);
104 }
105
106 template <typename T,
107 int&... kExplicitGuard,
108 std::enable_if_t<is_bounded_array_v<T>, int> = 0>
109 UniquePtr<T> MakeUnique() {
110 using ElementType = std::remove_extent_t<T>;
111 return UniquePtr<T>(NewArray<ElementType>(std::extent_v<T>), *this);
112 }
113
114 template <typename T,
115 int&... kExplicitGuard,
116 std::enable_if_t<is_unbounded_array_v<T>, int> = 0>
117 UniquePtr<T> MakeUnique() {
118 using ElementType = std::remove_extent_t<T>;
119 size_t size = layout_.size() / sizeof(ElementType);
120 return UniquePtr<T>(NewArray<ElementType>(size), size, *this);
121 }
123
124 private:
126 virtual void* DoAllocate() = 0;
127
128 // Helper to create arrays.
129 template <typename ElementType>
130 [[nodiscard]] ElementType* NewArray(size_t count) {
131 Layout layout = Layout::Of<ElementType[]>(count);
132 PW_ASSERT(layout.size() == layout_.size());
133 PW_ASSERT(layout.alignment() <= layout_.alignment());
134 void* ptr = DoAllocate();
135 return ptr != nullptr ? new (ptr) ElementType[count] : nullptr;
136 }
137
138 const Layout layout_;
139};
140
142
143} // namespace pw::allocator
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: unique_ptr.h:43
Definition: capability.h:64
Definition: layout.h:58
Definition: pool.h:35
void * Allocate()
Definition: pool.h:47
UniquePtr< T > MakeUnique(Args &&... args)
Definition: pool.h:102
T * New(Args &&... args)
Definition: pool.h:64
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.