Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
allocator.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 <optional>
18
19#include "pw_containers/intrusive_forward_list.h"
20#include "pw_multibuf/config.h"
21#include "pw_multibuf/multibuf_v1.h"
22#include "pw_result/result.h"
23#include "pw_sync/mutex.h"
24
25namespace pw::multibuf {
26
27enum class PW_MULTIBUF_DEPRECATED ContiguityRequirement {
28 kAllowDiscontiguous,
29 kNeedsContiguous,
30};
31
32inline constexpr ContiguityRequirement kAllowDiscontiguous =
33 ContiguityRequirement::kAllowDiscontiguous;
34inline constexpr ContiguityRequirement kNeedsContiguous =
35 ContiguityRequirement::kNeedsContiguous;
36
55class PW_MULTIBUF_DEPRECATED MultiBufAllocator {
56 public:
57 MultiBufAllocator() = default;
58
61 MultiBufAllocator& operator=(MultiBufAllocator&) = delete;
63 MultiBufAllocator& operator=(MultiBufAllocator&&) = delete;
64
65 virtual ~MultiBufAllocator() {}
66
76 std::optional<MultiBuf> Allocate(size_t size);
77
88 std::optional<MultiBuf> Allocate(size_t min_size, size_t desired_size);
89
101 std::optional<MultiBuf> AllocateContiguous(size_t size);
102
114 std::optional<MultiBuf> AllocateContiguous(size_t min_size,
115 size_t desired_size);
116
124 std::optional<size_t> GetBackingCapacity() { return DoGetBackingCapacity(); }
125
126 protected:
133 void MoreMemoryAvailable(size_t size_available,
134 size_t contiguous_size_available);
135
136 private:
137 friend class MultiBufAllocationFuture;
138
139 // Instances of this class are informed when more memory becomes available.
140 class MemoryAvailableDelegate
141 : public IntrusiveForwardList<MemoryAvailableDelegate>::Item {
142 public:
143 explicit MemoryAvailableDelegate() = default;
144 MemoryAvailableDelegate(MemoryAvailableDelegate&) = delete;
145 MemoryAvailableDelegate& operator=(MemoryAvailableDelegate&) = delete;
146 MemoryAvailableDelegate(MemoryAvailableDelegate&&) = delete;
147 MemoryAvailableDelegate& operator=(MemoryAvailableDelegate&&) = delete;
148 virtual ~MemoryAvailableDelegate() = default;
149
150 // Callback from allocator when new memory being available. Function should
151 // return true if object's need has been met which also indicates the object
152 // can be released by the allocator.
153 virtual bool HandleMemoryAvailable(MultiBufAllocator& alloc,
154 size_t size_available,
155 size_t contiguous_size_available) const
156 PW_EXCLUSIVE_LOCKS_REQUIRED(lock_) = 0;
157 };
158
159 void AddMemoryAvailableDelegate(MemoryAvailableDelegate& delegate)
160 PW_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
161 mem_delegates_.push_front(delegate);
162 }
163
164 void RemoveMemoryAvailableDelegate(MemoryAvailableDelegate& delegate)
165 PW_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
166 mem_delegates_.remove(delegate);
167 }
168
186 virtual pw::Result<MultiBuf> DoAllocate(
187 size_t min_size,
188 size_t desired_size,
189 ContiguityRequirement contiguity_requirement) = 0;
190
192 virtual std::optional<size_t> DoGetBackingCapacity() = 0;
193
194 sync::Mutex lock_;
196 PW_GUARDED_BY(lock_);
197};
198
199} // namespace pw::multibuf
Definition: intrusive_forward_list.h:86
Definition: allocator_async.h:91
Definition: allocator.h:55
std::optional< size_t > GetBackingCapacity()
Definition: allocator.h:124
std::optional< MultiBuf > AllocateContiguous(size_t size)
std::optional< MultiBuf > Allocate(size_t min_size, size_t desired_size)
virtual pw::Result< MultiBuf > DoAllocate(size_t min_size, size_t desired_size, ContiguityRequirement contiguity_requirement)=0
std::optional< MultiBuf > Allocate(size_t size)
virtual std::optional< size_t > DoGetBackingCapacity()=0
MultiBufAllocator(MultiBufAllocator &)=delete
`MultiBufAllocator is not copyable or movable.
std::optional< MultiBuf > AllocateContiguous(size_t min_size, size_t desired_size)
void MoreMemoryAvailable(size_t size_available, size_t contiguous_size_available)
Definition: mutex.h:42