Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
33class Pool : public Deallocator {
34 public:
35 constexpr Pool(const Capabilities& capabilities, const Layout& layout)
36 : Deallocator(capabilities), layout_(layout) {}
37
38 constexpr const Layout& layout() const { return layout_; }
39
45 void* Allocate() { return DoAllocate(); }
46
58 template <typename T,
59 int&... kExplicitGuard,
60 std::enable_if_t<!std::is_array_v<T>, int> = 0,
61 typename... Args>
62 [[nodiscard]] T* New(Args&&... args) {
63 PW_ASSERT(Layout::Of<T>() == layout_);
64 void* ptr = Allocate();
65 return ptr != nullptr ? new (ptr) T(std::forward<Args>(args)...) : nullptr;
66 }
67
68 template <typename T,
69 int&... kExplicitGuard,
70 typename ElementType = std::remove_extent_t<T>,
71 std::enable_if_t<is_bounded_array_v<T>, int> = 0>
72 [[nodiscard]] ElementType* New() {
73 return NewArray<ElementType>(std::extent_v<T>);
74 }
75
76 template <typename T,
77 int&... kExplicitGuard,
78 typename ElementType = std::remove_extent_t<T>,
79 std::enable_if_t<is_unbounded_array_v<T>, int> = 0>
80 [[nodiscard]] ElementType* New() {
81 return NewArray<ElementType>(layout_.size() / sizeof(ElementType));
82 }
84
96 template <typename T,
97 int&... kExplicitGuard,
98 std::enable_if_t<!std::is_array_v<T>, int> = 0,
99 typename... Args>
100 UniquePtr<T> MakeUnique(Args&&... args) {
101 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *this);
102 }
103
104 template <typename T,
105 int&... kExplicitGuard,
106 std::enable_if_t<is_bounded_array_v<T>, int> = 0>
107 UniquePtr<T> MakeUnique() {
108 using ElementType = std::remove_extent_t<T>;
109 return UniquePtr<T>(NewArray<ElementType>(std::extent_v<T>), *this);
110 }
111
112 template <typename T,
113 int&... kExplicitGuard,
114 std::enable_if_t<is_unbounded_array_v<T>, int> = 0>
115 UniquePtr<T> MakeUnique() {
116 using ElementType = std::remove_extent_t<T>;
117 size_t size = layout_.size() / sizeof(ElementType);
118 return UniquePtr<T>(NewArray<ElementType>(size), size, *this);
119 }
121
122 private:
124 virtual void* DoAllocate() = 0;
125
126 // Helper to create arrays.
127 template <typename ElementType>
128 [[nodiscard]] ElementType* NewArray(size_t count) {
129 Layout layout = Layout::Of<ElementType[]>(count);
130 PW_ASSERT(layout.size() == layout_.size());
131 PW_ASSERT(layout.alignment() <= layout_.alignment());
132 void* ptr = DoAllocate();
133 return ptr != nullptr ? new (ptr) ElementType[count] : nullptr;
134 }
135
136 const Layout layout_;
137};
138
139} // namespace pw::allocator
Abstract interface for releasing memory.
Definition: deallocator.h:27
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
Definition: unique_ptr.h:41
Definition: capability.h:62
Definition: layout.h:56
Definition: pool.h:33
void * Allocate()
Definition: pool.h:45
UniquePtr< T > MakeUnique(Args &&... args)
Definition: pool.h:100
T * New(Args &&... args)
Definition: pool.h:62
virtual void * DoAllocate()=0
Virtual Allocate function that can be overridden by derived classes.