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
70 // clang-format off
84 // clang-format on
85 constexpr StatusWithSize CanAlloc(Layout layout) const;
86
113 static constexpr BlockResult<Derived> AllocFirst(Derived*&& block,
114 Layout layout);
115
138 static constexpr BlockResult<Derived> AllocLast(Derived*&& block,
139 Layout layout);
140
160 constexpr BlockResult<Derived> Resize(size_t new_inner_size);
161
170 static constexpr BlockResult<Derived> Free(Derived*&& block);
171
172 protected:
174 constexpr StatusWithSize DoCanAlloc(Layout layout) const;
175
177 static constexpr BlockResult<Derived> DoAllocFirst(Derived*&& block,
178 Layout layout);
179
181 static constexpr BlockResult<Derived> DoAllocLast(Derived*&& block,
182 Layout layout);
183
185 constexpr BlockResult<Derived> DoResize(size_t new_inner_size,
186 bool shifted = false);
187
189 static constexpr BlockResult<Derived> DoFree(Derived*&& block);
190
191 private:
192 using BlockResultPrev = internal::GenericBlockResult::Prev;
193 using BlockResultNext = internal::GenericBlockResult::Next;
194
195 constexpr Derived* derived() { return static_cast<Derived*>(this); }
196 constexpr const Derived* derived() const {
197 return static_cast<const Derived*>(this);
198 }
199
200 // AlignableBlock calls DoCanAlloc, DoAllocLast, DoResize
201 template <typename>
202 friend class AlignableBlock;
203
204 // BlockWithLayout calls DoFree, DoResize
205 template <typename>
206 friend class BlockWithLayout;
207};
208
211template <typename BlockType>
212struct is_allocatable : std::is_base_of<internal::AllocatableBase, BlockType> {
213};
214
216template <typename BlockType>
218
220
221// Template method implementations.
222
223template <typename Derived>
224constexpr bool AllocatableBlock<Derived>::IsFree() const {
225 if constexpr (Hardening::kIncludesDebugChecks) {
226 derived()->CheckInvariants();
227 }
228 return derived()->IsFreeUnchecked();
229}
230
231template <typename Derived>
233 Layout layout) const {
234 if constexpr (Hardening::kIncludesDebugChecks) {
235 derived()->CheckInvariants();
236 }
237 return derived()->DoCanAlloc(layout);
238}
239
240template <typename Derived>
242 Layout layout) const {
243 if (!derived()->IsFree()) {
244 return StatusWithSize::FailedPrecondition();
245 }
246 if (layout.size() == 0) {
247 return StatusWithSize::InvalidArgument();
248 }
249 size_t extra = derived()->InnerSize();
250 size_t new_inner_size = AlignUp(layout.size(), Derived::kAlignment);
251 if (!CheckedSub(extra, new_inner_size, extra)) {
252 return StatusWithSize::ResourceExhausted();
253 }
254 return StatusWithSize(extra);
255}
256
257template <typename Derived>
259 Derived*&& block, Layout layout) {
260 if (block == nullptr || layout.size() == 0) {
262 }
263 if constexpr (Hardening::kIncludesRobustChecks) {
264 block->CheckInvariants();
265 }
266 if (!block->IsFree()) {
268 }
269 return Derived::DoAllocFirst(std::move(block), layout);
270}
271
272template <typename Derived>
274 Derived*&& block, Layout layout) {
275 size_t size = AlignUp(layout.size(), Derived::kAlignment);
276 layout = Layout(size, layout.alignment());
277 StatusWithSize can_alloc = block->DoCanAlloc(layout);
278 if (!can_alloc.ok()) {
279 return BlockResult<Derived>(block, can_alloc.status());
280 }
281 size_t extra = can_alloc.size();
282 BlockResult<Derived> result(block);
283 if (extra >= Derived::kMinOuterSize) {
284 // Split the large padding off the back.
285 block->DoSplitFirst(block->InnerSize() - extra);
286 result = BlockResult<Derived>(block, BlockResultNext::kSplitNew);
287 }
288 block->SetFree(false);
289 return result;
290}
291
292template <typename Derived>
294 Derived*&& block, Layout layout) {
295 if (block == nullptr || layout.size() == 0) {
297 }
298 if constexpr (Hardening::kIncludesRobustChecks) {
299 block->CheckInvariants();
300 }
301 if (!block->IsFree()) {
303 }
304 return Derived::DoAllocLast(std::move(block), layout);
305}
306
307template <typename Derived>
309 Derived*&& block, Layout layout) {
310 size_t size = AlignUp(layout.size(), Derived::kAlignment);
311 layout = Layout(size, layout.alignment());
312 StatusWithSize can_alloc = block->DoCanAlloc(layout);
313 if (!can_alloc.ok()) {
314 return BlockResult<Derived>(block, can_alloc.status());
315 }
316 size_t extra = can_alloc.size();
317 BlockResult<Derived> result(block);
318 Derived* prev = block->Prev();
319 if (extra >= Derived::kMinOuterSize) {
320 // Split the large padding off the front.
321 block = block->DoSplitLast(layout.size());
322 prev = block->Prev();
323 result = BlockResult<Derived>(block, BlockResultPrev::kSplitNew);
324
325 } else if (extra != 0 && prev != nullptr) {
326 // The small amount of padding can be appended to the previous block.
327 prev->DoResize(prev->InnerSize() + extra, true).IgnoreUnlessStrict();
328 block = prev->Next();
329 result =
330 BlockResult<Derived>(block, BlockResultPrev::kResizedLarger, extra);
331 }
332 block->SetFree(false);
333 return result;
334}
335
336template <typename Derived>
338 size_t new_inner_size) {
339 if constexpr (Hardening::kIncludesRobustChecks) {
340 derived()->CheckInvariants();
341 }
342 if (derived()->IsFree()) {
344 }
345 return derived()->DoResize(new_inner_size, /* shifted: */ false);
346}
347
348template <typename Derived>
350 size_t new_inner_size, bool) {
351 size_t old_inner_size = derived()->InnerSize();
352 new_inner_size = AlignUp(new_inner_size, Derived::kAlignment);
353 if (old_inner_size == new_inner_size) {
354 return BlockResult<Derived>(derived());
355 }
356
357 // Treat the block as free and try to combine it with the next block. At most
358 // one free block is expected to follow this block.
359 derived()->SetFree(true);
360 Derived* next = derived()->Next();
361 BlockResult<Derived> result(derived());
362 if (next != nullptr && next->IsFree()) {
363 derived()->DoMergeNext();
364 result = BlockResult<Derived>(derived(), BlockResultNext::kMerged);
365 }
366 size_t merged_inner_size = derived()->InnerSize();
367 if (merged_inner_size < new_inner_size) {
368 // The merged block is too small for the resized block. Restore the original
369 // blocks as needed.
370 if (merged_inner_size != old_inner_size) {
371 derived()->DoSplitFirst(old_inner_size);
372 }
373 derived()->SetFree(false);
375 }
376 if (new_inner_size + Derived::kMinOuterSize <= merged_inner_size) {
377 // There is enough room after the resized block for another trailing block.
378 derived()->DoSplitFirst(new_inner_size);
379 result = result.next() == BlockResultNext::kMerged
380 ? BlockResult<Derived>(derived(), BlockResultNext::kResized)
381 : BlockResult<Derived>(derived(), BlockResultNext::kSplitNew);
382 }
383 derived()->SetFree(false);
384 return result;
385}
386
387template <typename Derived>
389 Derived*&& block) {
390 if (block == nullptr) {
392 }
393 if constexpr (Hardening::kIncludesRobustChecks) {
394 block->CheckInvariants();
395 }
396 return Derived::DoFree(std::move(block));
397}
398
399template <typename Derived>
401 Derived*&& block) {
402 block->SetFree(true);
403 BlockResult<Derived> result(block);
404
405 // Try to merge the previous block with this one.
406 Derived* prev = block->Prev();
407 if (prev != nullptr && prev->IsFree()) {
408 prev->DoMergeNext();
409 block = prev;
410 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
411 }
412
413 // Try to merge this block with the next one.
414 Derived* next = block->Next();
415 if (next != nullptr && next->IsFree()) {
416 block->DoMergeNext();
417 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
418 }
419
420 if constexpr (Hardening::kIncludesDebugChecks) {
421 block->CheckInvariants();
422 }
423 return result;
424}
425
426} // 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:337
constexpr bool IsFree() const
Definition: allocatable.h:224
constexpr bool Used() const
Definition: allocatable.h:68
static constexpr BlockResult< Derived > DoFree(Derived *&&block)
Definition: allocatable.h:400
constexpr StatusWithSize CanAlloc(Layout layout) const
Definition: allocatable.h:232
static constexpr BlockResult< Derived > AllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:258
constexpr BlockResult< Derived > DoResize(size_t new_inner_size, bool shifted=false)
Definition: allocatable.h:349
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:308
static constexpr BlockResult< Derived > AllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:293
static constexpr BlockResult< Derived > Free(Derived *&&block)
Definition: allocatable.h:388
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:273
constexpr StatusWithSize DoCanAlloc(Layout layout) const
Definition: allocatable.h:241
Definition: result.h:116
Definition: layout.h:58
constexpr bool is_allocatable_v
Helper variable template for is_allocatable<BlockType>::value.
Definition: allocatable.h:217
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:212