Pigweed
 
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/multibuf.h"
21#include "pw_result/result.h"
22#include "pw_sync/mutex.h"
23
24namespace pw::multibuf {
25
26enum class ContiguityRequirement {
27 kAllowDiscontiguous,
28 kNeedsContiguous,
29};
30
31inline constexpr ContiguityRequirement kAllowDiscontiguous =
32 ContiguityRequirement::kAllowDiscontiguous;
33inline constexpr ContiguityRequirement kNeedsContiguous =
34 ContiguityRequirement::kNeedsContiguous;
35
55 public:
56 MultiBufAllocator() = default;
57
60 MultiBufAllocator& operator=(MultiBufAllocator&) = delete;
62 MultiBufAllocator& operator=(MultiBufAllocator&&) = delete;
63
64 virtual ~MultiBufAllocator() {}
65
75 std::optional<MultiBuf> Allocate(size_t size);
76
87 std::optional<MultiBuf> Allocate(size_t min_size, size_t desired_size);
88
100 std::optional<MultiBuf> AllocateContiguous(size_t size);
101
113 std::optional<MultiBuf> AllocateContiguous(size_t min_size,
114 size_t desired_size);
115
123 std::optional<size_t> GetBackingCapacity() { return DoGetBackingCapacity(); }
124
125 protected:
132 void MoreMemoryAvailable(size_t size_available,
133 size_t contiguous_size_available);
134
135 private:
136 friend class MultiBufAllocationFuture;
137
138 // Instances of this class are informed when more memory becomes available.
139 class MemoryAvailableDelegate
140 : public IntrusiveForwardList<MemoryAvailableDelegate>::Item {
141 public:
142 explicit MemoryAvailableDelegate() = default;
143 MemoryAvailableDelegate(MemoryAvailableDelegate&) = delete;
144 MemoryAvailableDelegate& operator=(MemoryAvailableDelegate&) = delete;
145 MemoryAvailableDelegate(MemoryAvailableDelegate&&) = delete;
146 MemoryAvailableDelegate& operator=(MemoryAvailableDelegate&&) = delete;
147 virtual ~MemoryAvailableDelegate() = default;
148
149 // Callback from allocator when new memory being available. Function should
150 // return true if object's need has been met which also indicates the object
151 // can be released by the allocator.
152 virtual bool HandleMemoryAvailable(MultiBufAllocator& alloc,
153 size_t size_available,
154 size_t contiguous_size_available) const
155 PW_EXCLUSIVE_LOCKS_REQUIRED(lock_) = 0;
156 };
157
158 void AddMemoryAvailableDelegate(MemoryAvailableDelegate& delegate)
159 PW_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
160 mem_delegates_.push_front(delegate);
161 }
162
163 void RemoveMemoryAvailableDelegate(MemoryAvailableDelegate& delegate)
164 PW_EXCLUSIVE_LOCKS_REQUIRED(lock_) {
165 mem_delegates_.remove(delegate);
166 }
167
185 virtual pw::Result<MultiBuf> DoAllocate(
186 size_t min_size,
187 size_t desired_size,
188 ContiguityRequirement contiguity_requirement) = 0;
189
191 virtual std::optional<size_t> DoGetBackingCapacity() = 0;
192
193 sync::Mutex lock_;
195 PW_GUARDED_BY(lock_);
196};
197
198} // namespace pw::multibuf
Definition: intrusive_forward_list.h:86
bool remove(const T &item)
Definition: intrusive_forward_list.h:282
void push_front(T &item)
Inserts the item at the start of the list.
Definition: intrusive_forward_list.h:229
Definition: allocator_async.h:90
Definition: allocator.h:54
std::optional< size_t > GetBackingCapacity()
Definition: allocator.h:123
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