Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
25class MultiBufAllocationFuture;
26
27class PW_MULTIBUF_DEPRECATED MultiBufAllocatorAsync {
28 // : public MultiBufAllocator::MoreMemoryDelegate {
29 public:
31 : mbuf_allocator_(mbuf_allocator) {}
32
38
49
60 MultiBufAllocationFuture AllocateAsync(size_t min_size, size_t desired_size);
61
73
80 size_t desired_size);
81
82 private:
83 MultiBufAllocator& mbuf_allocator_;
84};
85
90class PW_MULTIBUF_DEPRECATED MultiBufAllocationFuture
91 : public MultiBufAllocator::MemoryAvailableDelegate {
92 public:
93 constexpr explicit MultiBufAllocationFuture(MultiBufAllocator& allocator)
94 : allocator_(&allocator),
95 min_size_(0),
96 desired_size_(0),
97 contiguity_requirement_(kAllowDiscontiguous) {}
99 size_t min_size,
100 size_t desired_size,
101 ContiguityRequirement contiguity_requirement)
102 : allocator_(&allocator),
103 min_size_(min_size),
104 desired_size_(desired_size),
105 contiguity_requirement_(contiguity_requirement) {}
106
109 ~MultiBufAllocationFuture() override;
110
111 void SetDesiredSize(size_t min_size) {
112 SetDesiredSizes(min_size, min_size, kAllowDiscontiguous);
113 }
114 void SetDesiredSizes(size_t min_size,
115 size_t desired_size,
116 ContiguityRequirement contiguity_requirement);
118
120 MultiBufAllocator& allocator() { return *allocator_; }
121 size_t min_size() const { return min_size_; }
122 size_t desired_size() const { return desired_size_; }
123 bool needs_contiguous() const {
124 return contiguity_requirement_ == kNeedsContiguous;
125 }
126
127 private:
128 friend class MultiBufAllocator;
129
130 // Handle new memory being available. Return true if our need has been met (so
131 // we can be destroyed by owner).
132 bool HandleMemoryAvailable(MultiBufAllocator& alloc,
133 size_t size_available,
134 size_t contiguous_size_available) const override;
135
137 async2::Poll<std::optional<MultiBuf>> TryAllocate();
138
139 // The allocator this future is tied to.
140 MultiBufAllocator* allocator_;
141
142 // The waker to wake when a suitably-sized allocation becomes available.
143 async2::Waker waker_;
144
145 // The properties of the kind of allocation being waited for.
146 //
147 // These properties can only be mutated while holding the allocator's lock,
148 // however the MultiBufAllocationFuture owner can freely read these values
149 // without needing to acquire the lock.
150 //
151 // The allocator may read these values so long as this value is listed and
152 // the allocator holds the lock.
153 size_t min_size_;
154 size_t desired_size_;
155 ContiguityRequirement contiguity_requirement_;
156};
157
158} // namespace pw::multibuf
Definition: context.h:49
Definition: poll.h:54
Definition: allocator_async.h:91
MultiBufAllocator & allocator()
Returns the allocator associated with this future.
Definition: allocator_async.h:120
Definition: allocator_async.h:27
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:55