C/C++ API Reference
Loading...
Searching...
No Matches
allocatable.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
18#include "pw_allocator/block/contiguous.h"
19#include "pw_allocator/block/result.h"
20#include "pw_allocator/hardening.h"
21#include "pw_allocator/layout.h"
22#include "pw_bytes/alignment.h"
23#include "pw_status/status.h"
24#include "pw_status/status_with_size.h"
25
26namespace pw::allocator {
27namespace internal {
28
29// Trivial base class for trait support.
31
32} // namespace internal
33
35
50template <typename Derived>
52 protected:
53 constexpr explicit AllocatableBlock() {
54 static_assert(is_contiguous_v<Derived>,
55 "Types derived from AllocatableBlock must also derive from "
56 "ContiguousBlock");
57 }
58
59 public:
61 constexpr bool IsFree() const;
62
63 // clang-format off
77 // clang-format on
78 constexpr StatusWithSize CanAlloc(Layout layout) const;
79
106 static constexpr BlockResult<Derived> AllocFirst(Derived*&& block,
107 Layout layout);
108
131 static constexpr BlockResult<Derived> AllocLast(Derived*&& block,
132 Layout layout);
133
153 constexpr BlockResult<Derived> Resize(size_t new_inner_size);
154
163 static constexpr BlockResult<Derived> Free(Derived*&& block);
164
165 protected:
167 constexpr StatusWithSize DoCanAlloc(Layout layout) const;
168
170 static constexpr BlockResult<Derived> DoAllocFirst(Derived*&& block,
171 Layout layout);
172
174 static constexpr BlockResult<Derived> DoAllocLast(Derived*&& block,
175 Layout layout);
176
178 constexpr BlockResult<Derived> DoResize(size_t new_inner_size,
179 bool shifted = false);
180
182 static constexpr BlockResult<Derived> DoFree(Derived*&& block);
183
184 private:
187
188 constexpr Derived* derived() { return static_cast<Derived*>(this); }
189 constexpr const Derived* derived() const {
190 return static_cast<const Derived*>(this);
191 }
192
193 // AlignableBlock calls DoCanAlloc, DoAllocLast, DoResize
194 template <typename>
195 friend class AlignableBlock;
196
197 // BlockWithLayout calls DoFree, DoResize
198 template <typename>
199 friend class BlockWithLayout;
200};
201
204template <typename BlockType>
205struct is_allocatable : std::is_base_of<internal::AllocatableBase, BlockType> {
206};
207
209template <typename BlockType>
211
213
214// Template method implementations.
215
216template <typename Derived>
217constexpr bool AllocatableBlock<Derived>::IsFree() const {
218 if constexpr (Hardening::kIncludesDebugChecks) {
219 derived()->CheckInvariants();
220 }
221 return derived()->IsFreeUnchecked();
222}
223
224template <typename Derived>
226 Layout layout) const {
227 if constexpr (Hardening::kIncludesDebugChecks) {
228 derived()->CheckInvariants();
229 }
230 return derived()->DoCanAlloc(layout);
231}
232
233template <typename Derived>
235 Layout layout) const {
236 if (!derived()->IsFree()) {
237 return StatusWithSize::FailedPrecondition();
238 }
239 if (layout.size() == 0) {
240 return StatusWithSize::InvalidArgument();
241 }
242 size_t extra = derived()->InnerSize();
243 size_t new_inner_size = AlignUp(layout.size(), Derived::kAlignment);
244 if (!CheckedSub(extra, new_inner_size, extra)) {
245 return StatusWithSize::ResourceExhausted();
246 }
247 return StatusWithSize(extra);
248}
249
250template <typename Derived>
252 Derived*&& block, Layout layout) {
253 if (block == nullptr || layout.size() == 0) {
255 }
256 if constexpr (Hardening::kIncludesRobustChecks) {
257 block->CheckInvariants();
258 }
259 if (!block->IsFree()) {
261 }
262 return Derived::DoAllocFirst(std::move(block), layout);
263}
264
265template <typename Derived>
267 Derived*&& block, Layout layout) {
268 size_t size = AlignUp(layout.size(), Derived::kAlignment);
269 layout = Layout(size, layout.alignment());
270 StatusWithSize can_alloc = block->DoCanAlloc(layout);
271 if (!can_alloc.ok()) {
272 return BlockResult<Derived>(block, can_alloc.status());
273 }
274 size_t extra = can_alloc.size();
275 BlockResult<Derived> result(block);
276 if (extra >= Derived::kMinOuterSize) {
277 // Split the large padding off the back.
278 block->DoSplitFirst(block->InnerSize() - extra);
279 result = BlockResult<Derived>(block, BlockResultNext::kSplitNew);
280 }
281 block->SetFree(false);
282 return result;
283}
284
285template <typename Derived>
287 Derived*&& block, Layout layout) {
288 if (block == nullptr || layout.size() == 0) {
290 }
291 if constexpr (Hardening::kIncludesRobustChecks) {
292 block->CheckInvariants();
293 }
294 if (!block->IsFree()) {
296 }
297 return Derived::DoAllocLast(std::move(block), layout);
298}
299
300template <typename Derived>
302 Derived*&& block, Layout layout) {
303 size_t size = AlignUp(layout.size(), Derived::kAlignment);
304 layout = Layout(size, layout.alignment());
305 StatusWithSize can_alloc = block->DoCanAlloc(layout);
306 if (!can_alloc.ok()) {
307 return BlockResult<Derived>(block, can_alloc.status());
308 }
309 size_t extra = can_alloc.size();
310 BlockResult<Derived> result(block);
311 Derived* prev = block->Prev();
312 if (extra >= Derived::kMinOuterSize) {
313 // Split the large padding off the front.
314 block = block->DoSplitLast(layout.size());
315 prev = block->Prev();
316 result = BlockResult<Derived>(block, BlockResultPrev::kSplitNew);
317
318 } else if (extra != 0 && prev != nullptr) {
319 // The small amount of padding can be appended to the previous block.
320 prev->DoResize(prev->InnerSize() + extra, true).IgnoreUnlessStrict();
321 block = prev->Next();
322 result =
323 BlockResult<Derived>(block, BlockResultPrev::kResizedLarger, extra);
324 }
325 block->SetFree(false);
326 return result;
327}
328
329template <typename Derived>
331 size_t new_inner_size) {
332 if constexpr (Hardening::kIncludesRobustChecks) {
333 derived()->CheckInvariants();
334 }
335 if (derived()->IsFree()) {
337 }
338 return derived()->DoResize(new_inner_size, /* shifted: */ false);
339}
340
341template <typename Derived>
343 size_t new_inner_size, bool) {
344 size_t old_inner_size = derived()->InnerSize();
345 new_inner_size = AlignUp(new_inner_size, Derived::kAlignment);
346 if (old_inner_size == new_inner_size) {
347 return BlockResult<Derived>(derived());
348 }
349
350 // Treat the block as free and try to combine it with the next block. At most
351 // one free block is expected to follow this block.
352 derived()->SetFree(true);
353 Derived* next = derived()->Next();
354 BlockResult<Derived> result(derived());
355 if (next != nullptr && next->IsFree()) {
356 derived()->DoMergeNext();
357 result = BlockResult<Derived>(derived(), BlockResultNext::kMerged);
358 }
359 size_t merged_inner_size = derived()->InnerSize();
360 if (merged_inner_size < new_inner_size) {
361 // The merged block is too small for the resized block. Restore the original
362 // blocks as needed.
363 if (merged_inner_size != old_inner_size) {
364 derived()->DoSplitFirst(old_inner_size);
365 }
366 derived()->SetFree(false);
368 }
369 if (new_inner_size + Derived::kMinOuterSize <= merged_inner_size) {
370 // There is enough room after the resized block for another trailing block.
371 derived()->DoSplitFirst(new_inner_size);
372 result = result.next() == BlockResultNext::kMerged
373 ? BlockResult<Derived>(derived(), BlockResultNext::kResized)
374 : BlockResult<Derived>(derived(), BlockResultNext::kSplitNew);
375 }
376 derived()->SetFree(false);
377 return result;
378}
379
380template <typename Derived>
382 Derived*&& block) {
383 if (block == nullptr) {
385 }
386 if constexpr (Hardening::kIncludesRobustChecks) {
387 block->CheckInvariants();
388 }
389 return Derived::DoFree(std::move(block));
390}
391
392template <typename Derived>
394 Derived*&& block) {
395 block->SetFree(true);
396 BlockResult<Derived> result(block);
397
398 // Try to merge the previous block with this one.
399 Derived* prev = block->Prev();
400 if (prev != nullptr && prev->IsFree()) {
401 prev->DoMergeNext();
402 block = prev;
403 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
404 }
405
406 // Try to merge this block with the next one.
407 Derived* next = block->Next();
408 if (next != nullptr && next->IsFree()) {
409 block->DoMergeNext();
410 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
411 }
412
413 if constexpr (Hardening::kIncludesDebugChecks) {
414 block->CheckInvariants();
415 }
416 return result;
417}
418
419} // namespace pw::allocator
static constexpr Status InvalidArgument()
Definition: status.h:164
static constexpr Status FailedPrecondition()
Definition: status.h:243
static constexpr Status ResourceExhausted()
Definition: status.h:230
Definition: status_with_size.h:51
constexpr bool ok() const
True if status() == OkStatus().
Definition: status_with_size.h:154
constexpr size_t size() const
Definition: status_with_size.h:148
Definition: allocatable.h:51
constexpr BlockResult< Derived > Resize(size_t new_inner_size)
Definition: allocatable.h:330
constexpr bool IsFree() const
Definition: allocatable.h:217
static constexpr BlockResult< Derived > DoFree(Derived *&&block)
Definition: allocatable.h:393
constexpr StatusWithSize CanAlloc(Layout layout) const
Definition: allocatable.h:225
static constexpr BlockResult< Derived > AllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:251
constexpr BlockResult< Derived > DoResize(size_t new_inner_size, bool shifted=false)
Definition: allocatable.h:342
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:301
static constexpr BlockResult< Derived > AllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:286
static constexpr BlockResult< Derived > Free(Derived *&&block)
Definition: allocatable.h:381
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:266
constexpr StatusWithSize DoCanAlloc(Layout layout) const
Definition: allocatable.h:234
Definition: result.h:106
Definition: layout.h:64
constexpr bool is_allocatable_v
Helper variable template for is_allocatable<BlockType>::value.
Definition: allocatable.h:210
constexpr size_t AlignUp(uintptr_t value, size_t alignment)
Returns the value rounded up to the nearest multiple of alignment.
Definition: alignment.h:54
constexpr bool CheckedSub(A a, B b, T &result)
Definition: checked_arithmetic.h:138
Definition: allocatable.h:30
Definition: allocatable.h:205