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 // Assert within a function, since `Derived` is not complete when this type
55 // is defined.
56 static_assert(is_contiguous_v<Derived>,
57 "Types derived from AllocatableBlock must also derive from "
58 "ContiguousBlock");
59 }
60
61 public:
63 constexpr bool IsFree() const;
64
68 constexpr bool Used() const { return !IsFree(); }
69
92 constexpr StatusWithSize CanAlloc(Layout layout) const;
93
127 static constexpr BlockResult<Derived> AllocFirst(Derived*&& block,
128 Layout layout);
129
159 static constexpr BlockResult<Derived> AllocLast(Derived*&& block,
160 Layout layout);
161
188 constexpr BlockResult<Derived> Resize(size_t new_inner_size);
189
198 static constexpr BlockResult<Derived> Free(Derived*&& block);
199
200 protected:
202 constexpr StatusWithSize DoCanAlloc(Layout layout) const;
203
205 static constexpr BlockResult<Derived> DoAllocFirst(Derived*&& block,
206 Layout layout);
207
209 static constexpr BlockResult<Derived> DoAllocLast(Derived*&& block,
210 Layout layout);
211
213 constexpr BlockResult<Derived> DoResize(size_t new_inner_size,
214 bool shifted = false);
215
217 static constexpr BlockResult<Derived> DoFree(Derived*&& block);
218
219 private:
220 using BlockResultPrev = internal::GenericBlockResult::Prev;
221 using BlockResultNext = internal::GenericBlockResult::Next;
222
223 constexpr Derived* derived() { return static_cast<Derived*>(this); }
224 constexpr const Derived* derived() const {
225 return static_cast<const Derived*>(this);
226 }
227
228 // AlignableBlock calls DoCanAlloc, DoAllocLast, DoResize
229 template <typename>
230 friend class AlignableBlock;
231
232 // BlockWithLayout calls DoFree, DoResize
233 template <typename>
234 friend class BlockWithLayout;
235};
236
239template <typename BlockType>
240struct is_allocatable : std::is_base_of<internal::AllocatableBase, BlockType> {
241};
242
244template <typename BlockType>
246
248
249// Template method implementations.
250
251template <typename Derived>
252constexpr bool AllocatableBlock<Derived>::IsFree() const {
253 if constexpr (Hardening::kIncludesDebugChecks) {
254 derived()->CheckInvariants();
255 }
256 return derived()->IsFreeUnchecked();
257}
258
259template <typename Derived>
261 Layout layout) const {
262 if constexpr (Hardening::kIncludesDebugChecks) {
263 derived()->CheckInvariants();
264 }
265 return derived()->DoCanAlloc(layout);
266}
267
268template <typename Derived>
270 Layout layout) const {
271 if (!derived()->IsFree()) {
272 return StatusWithSize::FailedPrecondition();
273 }
274 if (layout.size() == 0) {
275 return StatusWithSize::InvalidArgument();
276 }
277 size_t extra = derived()->InnerSize();
278 size_t new_inner_size = AlignUp(layout.size(), Derived::kAlignment);
279 if (!CheckedSub(extra, new_inner_size, extra)) {
280 return StatusWithSize::ResourceExhausted();
281 }
282 return StatusWithSize(extra);
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::DoAllocFirst(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 if (extra >= Derived::kMinOuterSize) {
312 // Split the large padding off the back.
313 block->DoSplitFirst(block->InnerSize() - extra);
314 result = BlockResult<Derived>(block, BlockResultNext::kSplitNew);
315 }
316 block->SetFree(false);
317 return result;
318}
319
320template <typename Derived>
322 Derived*&& block, Layout layout) {
323 if (block == nullptr || layout.size() == 0) {
325 }
326 if constexpr (Hardening::kIncludesRobustChecks) {
327 block->CheckInvariants();
328 }
329 if (!block->IsFree()) {
331 }
332 return Derived::DoAllocLast(std::move(block), layout);
333}
334
335template <typename Derived>
337 Derived*&& block, Layout layout) {
338 size_t size = AlignUp(layout.size(), Derived::kAlignment);
339 layout = Layout(size, layout.alignment());
340 StatusWithSize can_alloc = block->DoCanAlloc(layout);
341 if (!can_alloc.ok()) {
342 return BlockResult<Derived>(block, can_alloc.status());
343 }
344 size_t extra = can_alloc.size();
345 BlockResult<Derived> result(block);
346 Derived* prev = block->Prev();
347 if (extra >= Derived::kMinOuterSize) {
348 // Split the large padding off the front.
349 block = block->DoSplitLast(layout.size());
350 prev = block->Prev();
351 result = BlockResult<Derived>(block, BlockResultPrev::kSplitNew);
352
353 } else if (extra != 0 && prev != nullptr) {
354 // The small amount of padding can be appended to the previous block.
355 prev->DoResize(prev->InnerSize() + extra, true).IgnoreUnlessStrict();
356 block = prev->Next();
357 result =
358 BlockResult<Derived>(block, BlockResultPrev::kResizedLarger, extra);
359 }
360 block->SetFree(false);
361 return result;
362}
363
364template <typename Derived>
366 size_t new_inner_size) {
367 if constexpr (Hardening::kIncludesRobustChecks) {
368 derived()->CheckInvariants();
369 }
370 if (derived()->IsFree()) {
372 }
373 return derived()->DoResize(new_inner_size, /* shifted: */ false);
374}
375
376template <typename Derived>
378 size_t new_inner_size, bool) {
379 size_t old_inner_size = derived()->InnerSize();
380 new_inner_size = AlignUp(new_inner_size, Derived::kAlignment);
381 if (old_inner_size == new_inner_size) {
382 return BlockResult<Derived>(derived());
383 }
384
385 // Treat the block as free and try to combine it with the next block. At most
386 // one free block is expected to follow this block.
387 derived()->SetFree(true);
388 Derived* next = derived()->Next();
389 BlockResult<Derived> result(derived());
390 if (next != nullptr && next->IsFree()) {
391 derived()->DoMergeNext();
392 result = BlockResult<Derived>(derived(), BlockResultNext::kMerged);
393 }
394 size_t merged_inner_size = derived()->InnerSize();
395 if (merged_inner_size < new_inner_size) {
396 // The merged block is too small for the resized block. Restore the original
397 // blocks as needed.
398 if (merged_inner_size != old_inner_size) {
399 derived()->DoSplitFirst(old_inner_size);
400 }
401 derived()->SetFree(false);
403 }
404 if (new_inner_size + Derived::kMinOuterSize <= merged_inner_size) {
405 // There is enough room after the resized block for another trailing block.
406 derived()->DoSplitFirst(new_inner_size);
407 result = result.next() == BlockResultNext::kMerged
408 ? BlockResult<Derived>(derived(), BlockResultNext::kResized)
409 : BlockResult<Derived>(derived(), BlockResultNext::kSplitNew);
410 }
411 derived()->SetFree(false);
412 return result;
413}
414
415template <typename Derived>
417 Derived*&& block) {
418 if (block == nullptr) {
420 }
421 if constexpr (Hardening::kIncludesRobustChecks) {
422 block->CheckInvariants();
423 }
424 return Derived::DoFree(std::move(block));
425}
426
427template <typename Derived>
429 Derived*&& block) {
430 block->SetFree(true);
431 BlockResult<Derived> result(block);
432
433 // Try to merge the previous block with this one.
434 Derived* prev = block->Prev();
435 if (prev != nullptr && prev->IsFree()) {
436 prev->DoMergeNext();
437 block = prev;
438 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
439 }
440
441 // Try to merge this block with the next one.
442 Derived* next = block->Next();
443 if (next != nullptr && next->IsFree()) {
444 block->DoMergeNext();
445 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
446 }
447
448 if constexpr (Hardening::kIncludesDebugChecks) {
449 block->CheckInvariants();
450 }
451 return result;
452}
453
454} // namespace pw::allocator
static constexpr Status InvalidArgument()
Argument was malformed; e.g. invalid characters when parsing integer.
Definition: status.h:131
static constexpr Status FailedPrecondition()
System isn’t in the required state; e.g. deleting a non-empty directory.
Definition: status.h:162
static constexpr Status ResourceExhausted()
Definition: status.h:157
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:365
constexpr bool IsFree() const
Definition: allocatable.h:252
constexpr bool Used() const
Definition: allocatable.h:68
static constexpr BlockResult< Derived > DoFree(Derived *&&block)
Definition: allocatable.h:428
constexpr StatusWithSize CanAlloc(Layout layout) const
Definition: allocatable.h:260
static constexpr BlockResult< Derived > AllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:286
constexpr BlockResult< Derived > DoResize(size_t new_inner_size, bool shifted=false)
Definition: allocatable.h:377
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:336
static constexpr BlockResult< Derived > AllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:321
static constexpr BlockResult< Derived > Free(Derived *&&block)
Definition: allocatable.h:416
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:301
constexpr StatusWithSize DoCanAlloc(Layout layout) const
Definition: allocatable.h:269
Definition: result.h:116
Definition: layout.h:58
constexpr bool is_allocatable_v
Helper variable template for is_allocatable<BlockType>::value.
Definition: allocatable.h:245
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:240