Pigweed
 
Loading...
Searching...
No Matches
allocator_async.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 <optional>
17
18#include "pw_async2/dispatcher.h" // IWYU pragma: keep
19#include "pw_async2/poll.h"
20#include "pw_multibuf/allocator.h"
21
22namespace pw::multibuf {
23
24class MultiBufAllocationFuture;
25
27 // : public MultiBufAllocator::MoreMemoryDelegate {
28 public:
30 : mbuf_allocator_(mbuf_allocator) {}
31
37
48
59 MultiBufAllocationFuture AllocateAsync(size_t min_size, size_t desired_size);
60
72
79 size_t desired_size);
80
81 private:
82 MultiBufAllocator& mbuf_allocator_;
83};
84
90 : public MultiBufAllocator::MemoryAvailableDelegate {
91 public:
93 : allocator_(&allocator),
94 min_size_(0),
95 desired_size_(0),
96 contiguity_requirement_(kAllowDiscontiguous) {}
98 size_t min_size,
99 size_t desired_size,
100 ContiguityRequirement contiguity_requirement)
101 : allocator_(&allocator),
102 min_size_(min_size),
103 desired_size_(desired_size),
104 contiguity_requirement_(contiguity_requirement) {}
105
108 ~MultiBufAllocationFuture() override;
109
110 void SetDesiredSize(size_t min_size) {
111 SetDesiredSizes(min_size, min_size, kAllowDiscontiguous);
112 }
113 void SetDesiredSizes(size_t min_size,
114 size_t desired_size,
115 ContiguityRequirement contiguity_requirement);
117
119 MultiBufAllocator& allocator() { return *allocator_; }
120 size_t min_size() const { return min_size_; }
121 size_t desired_size() const { return desired_size_; }
122 bool needs_contiguous() const {
123 return contiguity_requirement_ == kNeedsContiguous;
124 }
125
126 private:
127 friend class MultiBufAllocator;
128
129 // Handle new memory being available. Return true if our need has been met (so
130 // we can be destroyed by owner).
131 bool HandleMemoryAvailable(MultiBufAllocator& alloc,
132 size_t size_available,
133 size_t contiguous_size_available) const override;
134
136 async2::Poll<std::optional<MultiBuf>> TryAllocate();
137
138 // The allocator this future is tied to.
139 MultiBufAllocator* allocator_;
140
141 // The waker to wake when a suitably-sized allocation becomes available.
142 async2::Waker waker_;
143
144 // The properties of the kind of allocation being waited for.
145 //
146 // These properties can only be mutated while holding the allocator's lock,
147 // however the MultiBufAllocationFuture owner can freely read these values
148 // without needing to acquire the lock.
149 //
150 // The allocator may read these values so long as this value is listed and
151 // the allocator holds the lock.
152 size_t min_size_;
153 size_t desired_size_;
154 ContiguityRequirement contiguity_requirement_;
155};
156
157} // namespace pw::multibuf
Definition: dispatcher_base.h:52
Definition: poll.h:54
Definition: allocator_async.h:90
MultiBufAllocator & allocator()
Returns the allocator associated with this future.
Definition: allocator_async.h:119
Definition: allocator_async.h:26
MultiBufAllocationFuture AllocateContiguousAsync(size_t min_size, size_t desired_size)
MultiBufAllocationFuture AllocateAsync(size_t size)
MultiBufAllocatorAsync(MultiBufAllocatorAsync &)=delete
`MultiBufAllocatorAsync is not copyable or movable.
MultiBufAllocationFuture AllocateAsync(size_t min_size, size_t desired_size)
MultiBufAllocationFuture AllocateContiguousAsync(size_t size)
Definition: allocator.h:54