C/C++ API Reference
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#include "pw_multibuf/config.h"
22
23namespace pw::multibuf {
24
26
27class MultiBufAllocationFuture;
28
29class PW_MULTIBUF_DEPRECATED MultiBufAllocatorAsync {
30 // : public MultiBufAllocator::MoreMemoryDelegate {
31 public:
33 : mbuf_allocator_(mbuf_allocator) {}
34
40
51
62 MultiBufAllocationFuture AllocateAsync(size_t min_size, size_t desired_size);
63
75
82 size_t desired_size);
83
84 private:
85 MultiBufAllocator& mbuf_allocator_;
86};
87
92class PW_MULTIBUF_DEPRECATED MultiBufAllocationFuture
93 : public MultiBufAllocator::MemoryAvailableDelegate {
94 public:
95 constexpr explicit MultiBufAllocationFuture(MultiBufAllocator& allocator)
96 : allocator_(&allocator),
97 min_size_(0),
98 desired_size_(0),
99 contiguity_requirement_(kAllowDiscontiguous) {}
101 size_t min_size,
102 size_t desired_size,
103 ContiguityRequirement contiguity_requirement)
104 : allocator_(&allocator),
105 min_size_(min_size),
106 desired_size_(desired_size),
107 contiguity_requirement_(contiguity_requirement) {}
108
111 ~MultiBufAllocationFuture() override;
112
113 void SetDesiredSize(size_t min_size) {
114 SetDesiredSizes(min_size, min_size, kAllowDiscontiguous);
115 }
116 void SetDesiredSizes(size_t min_size,
117 size_t desired_size,
118 ContiguityRequirement contiguity_requirement);
120
122 MultiBufAllocator& allocator() { return *allocator_; }
123 size_t min_size() const { return min_size_; }
124 size_t desired_size() const { return desired_size_; }
125 bool needs_contiguous() const {
126 return contiguity_requirement_ == kNeedsContiguous;
127 }
128
129 private:
130 friend class MultiBufAllocator;
131
132 // Handle new memory being available. Return true if our need has been met (so
133 // we can be destroyed by owner).
134 bool HandleMemoryAvailable(MultiBufAllocator& alloc,
135 size_t size_available,
136 size_t contiguous_size_available) const override;
137
139 async2::PollOptional<MultiBuf> TryAllocate();
140
141 // The allocator this future is tied to.
142 MultiBufAllocator* allocator_;
143
144 // The waker to wake when a suitably-sized allocation becomes available.
145 async2::Waker waker_;
146
147 // The properties of the kind of allocation being waited for.
148 //
149 // These properties can only be mutated while holding the allocator's lock,
150 // however the MultiBufAllocationFuture owner can freely read these values
151 // without needing to acquire the lock.
152 //
153 // The allocator may read these values so long as this value is listed and
154 // the allocator holds the lock.
155 size_t min_size_;
156 size_t desired_size_;
157 ContiguityRequirement contiguity_requirement_;
158};
159
161
162} // namespace pw::multibuf
Definition: context.h:55
Definition: poll.h:60
Definition: allocator_async.h:93
MultiBufAllocator & allocator()
Returns the allocator associated with this future.
Definition: allocator_async.h:122
Definition: allocator_async.h:29
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:57