Path: blob/main/contrib/llvm-project/libc/src/__support/block.h
213766 views
//===-- Implementation header for a block of memory -------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_BLOCK_H9#define LLVM_LIBC_SRC___SUPPORT_BLOCK_H1011#include "src/__support/CPP/algorithm.h"12#include "src/__support/CPP/cstddef.h"13#include "src/__support/CPP/limits.h"14#include "src/__support/CPP/new.h"15#include "src/__support/CPP/optional.h"16#include "src/__support/CPP/span.h"17#include "src/__support/CPP/type_traits.h"18#include "src/__support/libc_assert.h"19#include "src/__support/macros/config.h"20#include "src/__support/math_extras.h"2122#include <stdint.h>2324namespace LIBC_NAMESPACE_DECL {2526/// Returns the value rounded down to the nearest multiple of alignment.27LIBC_INLINE constexpr size_t align_down(size_t value, size_t alignment) {28// Note this shouldn't overflow since the result will always be <= value.29return (value / alignment) * alignment;30}3132/// Returns the value rounded up to the nearest multiple of alignment. May wrap33/// around.34LIBC_INLINE constexpr size_t align_up(size_t value, size_t alignment) {35return align_down(value + alignment - 1, alignment);36}3738using ByteSpan = cpp::span<LIBC_NAMESPACE::cpp::byte>;39using cpp::optional;4041/// Memory region with links to adjacent blocks.42///43/// The blocks store their offsets to the previous and next blocks. The latter44/// is also the block's size.45///46/// All blocks have their usable space aligned to some multiple of max_align_t.47/// This also implies that block outer sizes are aligned to max_align_t.48///49/// As an example, the diagram below represents two contiguous `Block`s. The50/// indices indicate byte offsets:51///52/// @code{.unparsed}53/// Block 1:54/// +---------------------+--------------+55/// | Header | Usable space |56/// +----------+----------+--------------+57/// | prev | next | |58/// | 0......3 | 4......7 | 8........227 |59/// | 00000000 | 00000230 | <app data> |60/// +----------+----------+--------------+61/// Block 2:62/// +---------------------+--------------+63/// | Header | Usable space |64/// +----------+----------+--------------+65/// | prev | next | |66/// | 0......3 | 4......7 | 8........827 |67/// | 00000230 | 00000830 | f7f7....f7f7 |68/// +----------+----------+--------------+69/// @endcode70///71/// As a space optimization, when a block is allocated, it consumes the prev72/// field of the following block:73///74/// Block 1 (used):75/// +---------------------+--------------+76/// | Header | Usable space |77/// +----------+----------+--------------+78/// | prev | next | |79/// | 0......3 | 4......7 | 8........230 |80/// | 00000000 | 00000230 | <app data> |81/// +----------+----------+--------------+82/// Block 2:83/// +---------------------+--------------+84/// | B1 | Header | Usable space |85/// +----------+----------+--------------+86/// | | next | |87/// | 0......3 | 4......7 | 8........827 |88/// | xxxxxxxx | 00000830 | f7f7....f7f7 |89/// +----------+----------+--------------+90///91/// The next offset of a block matches the previous offset of its next block.92/// The first block in a list is denoted by having a previous offset of `0`.93class Block {94// Masks for the contents of the next_ field.95static constexpr size_t PREV_FREE_MASK = 1 << 0;96static constexpr size_t LAST_MASK = 1 << 1;97static constexpr size_t SIZE_MASK = ~(PREV_FREE_MASK | LAST_MASK);9899public:100// No copy or move.101Block(const Block &other) = delete;102Block &operator=(const Block &other) = delete;103104/// Initializes a given memory region into a first block and a sentinel last105/// block. Returns the first block, which has its usable space aligned to106/// max_align_t.107static optional<Block *> init(ByteSpan region);108109/// @returns A pointer to a `Block`, given a pointer to the start of the110/// usable space inside the block.111///112/// This is the inverse of `usable_space()`.113///114/// @warning This method does not do any checking; passing a random115/// pointer will return a non-null pointer.116LIBC_INLINE static Block *from_usable_space(void *usable_space) {117auto *bytes = reinterpret_cast<cpp::byte *>(usable_space);118return reinterpret_cast<Block *>(bytes - sizeof(Block));119}120LIBC_INLINE static const Block *from_usable_space(const void *usable_space) {121const auto *bytes = reinterpret_cast<const cpp::byte *>(usable_space);122return reinterpret_cast<const Block *>(bytes - sizeof(Block));123}124125/// @returns The total size of the block in bytes, including the header.126LIBC_INLINE size_t outer_size() const { return next_ & SIZE_MASK; }127128LIBC_INLINE static size_t outer_size(size_t inner_size) {129// The usable region includes the prev_ field of the next block.130return inner_size - sizeof(prev_) + sizeof(Block);131}132133/// @returns The number of usable bytes inside the block were it to be134/// allocated.135LIBC_INLINE size_t inner_size() const {136if (!next())137return 0;138return inner_size(outer_size());139}140141/// @returns The number of usable bytes inside a block with the given outer142/// size were it to be allocated.143LIBC_INLINE static size_t inner_size(size_t outer_size) {144// The usable region includes the prev_ field of the next block.145return inner_size_free(outer_size) + sizeof(prev_);146}147148/// @returns The number of usable bytes inside the block if it remains free.149LIBC_INLINE size_t inner_size_free() const {150if (!next())151return 0;152return inner_size_free(outer_size());153}154155/// @returns The number of usable bytes inside a block with the given outer156/// size if it remains free.157LIBC_INLINE static size_t inner_size_free(size_t outer_size) {158return outer_size - sizeof(Block);159}160161/// @returns A pointer to the usable space inside this block.162///163/// Aligned to some multiple of max_align_t.164LIBC_INLINE cpp::byte *usable_space() {165auto *s = reinterpret_cast<cpp::byte *>(this) + sizeof(Block);166LIBC_ASSERT(reinterpret_cast<uintptr_t>(s) % alignof(max_align_t) == 0 &&167"usable space must be aligned to a multiple of max_align_t");168return s;169}170LIBC_INLINE const cpp::byte *usable_space() const {171const auto *s = reinterpret_cast<const cpp::byte *>(this) + sizeof(Block);172LIBC_ASSERT(reinterpret_cast<uintptr_t>(s) % alignof(max_align_t) == 0 &&173"usable space must be aligned to a multiple of max_align_t");174return s;175}176177// @returns The region of memory the block manages, including the header.178LIBC_INLINE ByteSpan region() {179return {reinterpret_cast<cpp::byte *>(this), outer_size()};180}181182/// Attempts to split this block.183///184/// If successful, the block will have an inner size of at least185/// `new_inner_size`. The remaining space will be returned as a new block,186/// with usable space aligned to `usable_space_alignment`. Note that the prev_187/// field of the next block counts as part of the inner size of the block.188/// `usable_space_alignment` must be a multiple of max_align_t.189optional<Block *> split(size_t new_inner_size,190size_t usable_space_alignment = alignof(max_align_t));191192/// Merges this block with the one that comes after it.193bool merge_next();194195/// @returns The block immediately after this one, or a null pointer if this196/// is the last block.197LIBC_INLINE Block *next() const {198if (next_ & LAST_MASK)199return nullptr;200return reinterpret_cast<Block *>(reinterpret_cast<uintptr_t>(this) +201outer_size());202}203204/// @returns The free block immediately before this one, otherwise nullptr.205LIBC_INLINE Block *prev_free() const {206if (!(next_ & PREV_FREE_MASK))207return nullptr;208return reinterpret_cast<Block *>(reinterpret_cast<uintptr_t>(this) - prev_);209}210211/// @returns Whether the block is unavailable for allocation.212LIBC_INLINE bool used() const { return !next() || !next()->prev_free(); }213214/// Marks this block as in use.215LIBC_INLINE void mark_used() {216LIBC_ASSERT(next() && "last block is always considered used");217next()->next_ &= ~PREV_FREE_MASK;218}219220/// Marks this block as free.221LIBC_INLINE void mark_free() {222LIBC_ASSERT(next() && "last block is always considered used");223next()->next_ |= PREV_FREE_MASK;224// The next block's prev_ field becomes alive, as it is no longer part of225// this block's used space.226*new (&next()->prev_) size_t = outer_size();227}228229LIBC_INLINE Block(size_t outer_size, bool is_last) : next_(outer_size) {230// Last blocks are not usable, so they need not have sizes aligned to231// max_align_t. Their lower bits must still be free, so they must be aligned232// to Block.233LIBC_ASSERT(234outer_size % (is_last ? alignof(Block) : alignof(max_align_t)) == 0 &&235"block sizes must be aligned");236LIBC_ASSERT(is_usable_space_aligned(alignof(max_align_t)) &&237"usable space must be aligned to a multiple of max_align_t");238if (is_last)239next_ |= LAST_MASK;240}241242LIBC_INLINE bool is_usable_space_aligned(size_t alignment) const {243return reinterpret_cast<uintptr_t>(usable_space()) % alignment == 0;244}245246// Returns the minimum inner size necessary for a block of that size to247// always be able to allocate at the given size and alignment.248//249// Returns 0 if there is no such size.250LIBC_INLINE static size_t min_size_for_allocation(size_t alignment,251size_t size) {252LIBC_ASSERT(alignment >= alignof(max_align_t) &&253alignment % alignof(max_align_t) == 0 &&254"alignment must be multiple of max_align_t");255256if (alignment == alignof(max_align_t))257return size;258259// We must create a new block inside this one (splitting). This requires a260// block header in addition to the requested size.261if (add_overflow(size, sizeof(Block), size))262return 0;263264// Beyond that, padding space may need to remain in this block to ensure265// that the usable space of the next block is aligned.266//267// Consider a position P of some lesser alignment, L, with maximal distance268// to the next position of some greater alignment, G, where G is a multiple269// of L. P must be one L unit past a G-aligned point. If it were one L-unit270// earlier, its distance would be zero. If it were one L-unit later, its271// distance would not be maximal. If it were not some integral number of L272// units away, it would not be L-aligned.273//274// So the maximum distance would be G - L. As a special case, if L is 1275// (unaligned), the max distance is G - 1.276//277// This block's usable space is aligned to max_align_t >= Block. With zero278// padding, the next block's usable space is sizeof(Block) past it, which is279// a point aligned to Block. Thus the max padding needed is alignment -280// alignof(Block).281if (add_overflow(size, alignment - alignof(Block), size))282return 0;283return size;284}285286// This is the return type for `allocate` which can split one block into up to287// three blocks.288struct BlockInfo {289// This is the newly aligned block. It will have the alignment requested by290// a call to `allocate` and at most `size`.291Block *block;292293// If the usable_space in the new block was not aligned according to the294// `alignment` parameter, we will need to split into this block and the295// `block` to ensure `block` is properly aligned. In this case, `prev` will296// be a pointer to this new "padding" block. `prev` will be nullptr if no297// new block was created or we were able to merge the block before the298// original block with the "padding" block.299Block *prev;300301// This is the remainder of the next block after splitting the `block`302// according to `size`. This can happen if there's enough space after the303// `block`.304Block *next;305};306307// Divide a block into up to 3 blocks according to `BlockInfo`. Behavior is308// undefined if allocation is not possible for the given size and alignment.309static BlockInfo allocate(Block *block, size_t alignment, size_t size);310311// These two functions may wrap around.312LIBC_INLINE static uintptr_t next_possible_block_start(313uintptr_t ptr, size_t usable_space_alignment = alignof(max_align_t)) {314return align_up(ptr + sizeof(Block), usable_space_alignment) -315sizeof(Block);316}317LIBC_INLINE static uintptr_t prev_possible_block_start(318uintptr_t ptr, size_t usable_space_alignment = alignof(max_align_t)) {319return align_down(ptr, usable_space_alignment) - sizeof(Block);320}321322private:323/// Construct a block to represent a span of bytes. Overwrites only enough324/// memory for the block header; the rest of the span is left alone.325LIBC_INLINE static Block *as_block(ByteSpan bytes) {326LIBC_ASSERT(reinterpret_cast<uintptr_t>(bytes.data()) % alignof(Block) ==3270 &&328"block start must be suitably aligned");329return ::new (bytes.data()) Block(bytes.size(), /*is_last=*/false);330}331332LIBC_INLINE static void make_last_block(cpp::byte *start) {333LIBC_ASSERT(reinterpret_cast<uintptr_t>(start) % alignof(Block) == 0 &&334"block start must be suitably aligned");335::new (start) Block(sizeof(Block), /*is_last=*/true);336}337338/// Offset from this block to the previous block. 0 if this is the first339/// block. This field is only alive when the previous block is free;340/// otherwise, its memory is reused as part of the previous block's usable341/// space.342size_t prev_ = 0;343344/// Offset from this block to the next block. Valid even if this is the last345/// block, since it equals the size of the block.346size_t next_ = 0;347348/// Information about the current state of the block is stored in the two low349/// order bits of the next_ value. These are guaranteed free by a minimum350/// alignment (and thus, alignment of the size) of 4. The lowest bit is the351/// `prev_free` flag, and the other bit is the `last` flag.352///353/// * If the `prev_free` flag is set, the block isn't the first and the354/// previous block is free.355/// * If the `last` flag is set, the block is the sentinel last block. It is356/// summarily considered used and has no next block.357358public:359/// Only for testing.360static constexpr size_t PREV_FIELD_SIZE = sizeof(prev_);361};362363static_assert(alignof(Block) >= 4,364"at least 2 bits must be available in block sizes for flags");365366LIBC_INLINE367optional<Block *> Block::init(ByteSpan region) {368if (!region.data())369return {};370371uintptr_t start = reinterpret_cast<uintptr_t>(region.data());372uintptr_t end = start + region.size();373if (end < start)374return {};375376uintptr_t block_start = next_possible_block_start(start);377if (block_start < start)378return {};379380uintptr_t last_start = prev_possible_block_start(end);381if (last_start >= end)382return {};383384if (block_start + sizeof(Block) > last_start)385return {};386387auto *last_start_ptr = reinterpret_cast<cpp::byte *>(last_start);388Block *block =389as_block({reinterpret_cast<cpp::byte *>(block_start), last_start_ptr});390make_last_block(last_start_ptr);391block->mark_free();392return block;393}394395LIBC_INLINE396Block::BlockInfo Block::allocate(Block *block, size_t alignment, size_t size) {397LIBC_ASSERT(alignment % alignof(max_align_t) == 0 &&398"alignment must be a multiple of max_align_t");399400BlockInfo info{block, /*prev=*/nullptr, /*next=*/nullptr};401402if (!info.block->is_usable_space_aligned(alignment)) {403Block *original = info.block;404// The padding block has no minimum size requirement.405optional<Block *> maybe_aligned_block = original->split(0, alignment);406LIBC_ASSERT(maybe_aligned_block.has_value() &&407"it should always be possible to split for alignment");408409if (Block *prev = original->prev_free()) {410// If there is a free block before this, we can merge the current one with411// the newly created one.412prev->merge_next();413} else {414info.prev = original;415}416417Block *aligned_block = *maybe_aligned_block;418LIBC_ASSERT(aligned_block->is_usable_space_aligned(alignment) &&419"The aligned block isn't aligned somehow.");420info.block = aligned_block;421}422423// Now get a block for the requested size.424if (optional<Block *> next = info.block->split(size))425info.next = *next;426427return info;428}429430LIBC_INLINE431optional<Block *> Block::split(size_t new_inner_size,432size_t usable_space_alignment) {433LIBC_ASSERT(usable_space_alignment % alignof(max_align_t) == 0 &&434"alignment must be a multiple of max_align_t");435if (used())436return {};437438// Compute the minimum outer size that produces a block of at least439// `new_inner_size`.440size_t min_outer_size = outer_size(cpp::max(new_inner_size, sizeof(prev_)));441442uintptr_t start = reinterpret_cast<uintptr_t>(this);443uintptr_t next_block_start =444next_possible_block_start(start + min_outer_size, usable_space_alignment);445if (next_block_start < start)446return {};447size_t new_outer_size = next_block_start - start;448LIBC_ASSERT(new_outer_size % alignof(max_align_t) == 0 &&449"new size must be aligned to max_align_t");450451if (outer_size() < new_outer_size ||452outer_size() - new_outer_size < sizeof(Block))453return {};454455ByteSpan new_region = region().subspan(new_outer_size);456next_ &= ~SIZE_MASK;457next_ |= new_outer_size;458459Block *new_block = as_block(new_region);460mark_free(); // Free status for this block is now stored in new_block.461new_block->next()->prev_ = new_region.size();462463LIBC_ASSERT(new_block->is_usable_space_aligned(usable_space_alignment) &&464"usable space must have requested alignment");465return new_block;466}467468LIBC_INLINE469bool Block::merge_next() {470if (used() || next()->used())471return false;472size_t new_size = outer_size() + next()->outer_size();473next_ &= ~SIZE_MASK;474next_ |= new_size;475next()->prev_ = new_size;476return true;477}478479} // namespace LIBC_NAMESPACE_DECL480481#endif // LLVM_LIBC_SRC___SUPPORT_BLOCK_H482483484