Pigweed
 
Loading...
Searching...
No Matches
pw::allocator::AllocatableBlock< Derived > Class Template Reference

#include <allocatable.h>

Inheritance diagram for pw::allocator::AllocatableBlock< Derived >:
pw::allocator::internal::AllocatableBase

Public Member Functions

constexpr bool IsFree () const
 
constexpr bool Used () const
 
constexpr StatusWithSize CanAlloc (Layout layout) const
 
constexpr BlockResult< Derived > Resize (size_t new_inner_size)
 

Static Public Member Functions

static constexpr BlockResult< Derived > AllocFirst (Derived *&&block, Layout layout)
 
static constexpr BlockResult< Derived > AllocLast (Derived *&&block, Layout layout)
 
static constexpr BlockResult< Derived > Free (Derived *&&block)
 

Protected Member Functions

constexpr StatusWithSize DoCanAlloc (Layout layout) const
 
constexpr BlockResult< Derived > DoResize (size_t new_inner_size, bool shifted=false)
 

Static Protected Member Functions

static constexpr BlockResult< Derived > DoAllocFirst (Derived *&&block, Layout layout)
 
static constexpr BlockResult< Derived > DoAllocLast (Derived *&&block, Layout layout)
 
static constexpr BlockResult< Derived > DoFree (Derived *&&block)
 

Friends

template<typename >
class AlignableBlock
 
template<typename >
class BlockWithLayout
 

Detailed Description

template<typename Derived>
class pw::allocator::AllocatableBlock< Derived >

Mix-in for blocks that can be allocated and freed.

Block mix-ins are stateless and trivially constructible. See BasicBlock for details on how mix-ins can be combined to implement blocks.

This mix-in requires its derived type also derive from ContiguousBlock and provide the following symbols:

  • size_t kMinOuterSize
    • Size of the smallest block that can be allocated.
  • bool IsFreeUnchecked() const
    • Returns whether the block is free or in use.
  • void SetFree(bool)
    • Sets whether the block is free or in use.

Member Function Documentation

◆ AllocFirst()

template<typename Derived >
constexpr BlockResult< Derived > pw::allocator::AllocatableBlock< Derived >::AllocFirst ( Derived *&&  block,
Layout  layout 
)
staticconstexpr

Splits an aligned block from the start of the block, and marks it as used.

If successful, block will be replaced by a block that has an inner size of at least inner_size, and whose starting address is aligned to an alignment boundary. If unsuccessful, block will be unmodified.

This method is static in order to consume the given block pointer. On success, a pointer to the new, smaller block is returned. In total, up to two additional blocks may be created: one to pad the returned block to an alignment boundary and one for the trailing space. On error, the original pointer is returned.

For larger alignments, the AllocLast method is generally preferable to this method, as this method may create an additional fragments both before and after the returned block in order to align the usable space.

Precondition
The block must not be in use.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The split completed successfully. The `BlockAllocType` indicates
*     how extra memory was distributed to other blocks.
* 
*     FAILED_PRECONDITION: This block is in use and cannot be split.
* 
*     RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
*     request. This may be due to a large requested size, or insufficient
*     remaining space to fulfill the requested alignment create a valid
*     leading block, and/or create a valid trailing block.
* 
*  

◆ AllocLast()

template<typename Derived >
constexpr BlockResult< Derived > pw::allocator::AllocatableBlock< Derived >::AllocLast ( Derived *&&  block,
Layout  layout 
)
staticconstexpr

Splits an aligned block from the end of the block, and marks it as used.

If successful, block will be replaced by a block that has an inner size of at least inner_size, and whose starting address is aligned to an alignment boundary. If unsuccessful, block will be unmodified.

This method is static in order to consume the given block pointer. On success, a pointer to the new, smaller block is returned. In total, up to two additional blocks may be created: one to pad the returned block to an alignment boundary and one for the trailing space. On error, the original pointer is returned.

Precondition
The block must not be in use.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The split completed successfully. The `BlockAllocType` indicates
*     how extra memory was distributed to other blocks.
* 
*     FAILED_PRECONDITION: This block is in use and cannot be split.
* 
*     RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
*     request. This may be due to a large requested size, or insufficient
*     remaining space to fulfill the requested alignment create a valid
*     leading block, and/or create a valid trailing block.
* 
*  

◆ CanAlloc()

template<typename Derived >
constexpr StatusWithSize pw::allocator::AllocatableBlock< Derived >::CanAlloc ( Layout  layout) const
constexpr

Checks if a block could be split from the block.

On error, this method will return the same status as AllocFirst or AllocLast without performing any modifications.

Precondition
The block must not be in use.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: Returns the number of bytes to shift this block in order to align
*     its usable space.
* 
*     FAILED_PRECONDITION: This block is in use and cannot be split.
* 
*     RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
*     request. This may be due to a large requested size, or insufficient
*     remaining space to fulfill the requested alignment create a valid
*     leading block, and/or create a valid trailing block.
* 
*  

◆ DoAllocFirst()

template<typename Derived >
constexpr BlockResult< Derived > pw::allocator::AllocatableBlock< Derived >::DoAllocFirst ( Derived *&&  block,
Layout  layout 
)
staticconstexprprotected

Splits an aligned block from the start of the block, and marks it as used.

If successful, block will be replaced by a block that has an inner size of at least inner_size, and whose starting address is aligned to an alignment boundary. If unsuccessful, block will be unmodified.

This method is static in order to consume the given block pointer. On success, a pointer to the new, smaller block is returned. In total, up to two additional blocks may be created: one to pad the returned block to an alignment boundary and one for the trailing space. On error, the original pointer is returned.

For larger alignments, the AllocLast method is generally preferable to this method, as this method may create an additional fragments both before and after the returned block in order to align the usable space.

Precondition
The block must not be in use.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The split completed successfully. The `BlockAllocType` indicates
*     how extra memory was distributed to other blocks.
* 
*     FAILED_PRECONDITION: This block is in use and cannot be split.
* 
*     RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
*     request. This may be due to a large requested size, or insufficient
*     remaining space to fulfill the requested alignment create a valid
*     leading block, and/or create a valid trailing block.
* 
*  

◆ DoAllocLast()

template<typename Derived >
constexpr BlockResult< Derived > pw::allocator::AllocatableBlock< Derived >::DoAllocLast ( Derived *&&  block,
Layout  layout 
)
staticconstexprprotected

Splits an aligned block from the end of the block, and marks it as used.

If successful, block will be replaced by a block that has an inner size of at least inner_size, and whose starting address is aligned to an alignment boundary. If unsuccessful, block will be unmodified.

This method is static in order to consume the given block pointer. On success, a pointer to the new, smaller block is returned. In total, up to two additional blocks may be created: one to pad the returned block to an alignment boundary and one for the trailing space. On error, the original pointer is returned.

Precondition
The block must not be in use.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The split completed successfully. The `BlockAllocType` indicates
*     how extra memory was distributed to other blocks.
* 
*     FAILED_PRECONDITION: This block is in use and cannot be split.
* 
*     RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
*     request. This may be due to a large requested size, or insufficient
*     remaining space to fulfill the requested alignment create a valid
*     leading block, and/or create a valid trailing block.
* 
*  

◆ DoCanAlloc()

template<typename Derived >
constexpr StatusWithSize pw::allocator::AllocatableBlock< Derived >::DoCanAlloc ( Layout  layout) const
constexprprotected

Checks if a block could be split from the block.

On error, this method will return the same status as AllocFirst or AllocLast without performing any modifications.

Precondition
The block must not be in use.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: Returns the number of bytes to shift this block in order to align
*     its usable space.
* 
*     FAILED_PRECONDITION: This block is in use and cannot be split.
* 
*     RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
*     request. This may be due to a large requested size, or insufficient
*     remaining space to fulfill the requested alignment create a valid
*     leading block, and/or create a valid trailing block.
* 
*  

◆ DoFree()

template<typename Derived >
constexpr BlockResult< Derived > pw::allocator::AllocatableBlock< Derived >::DoFree ( Derived *&&  block)
staticconstexprprotected

Marks the block as free.

This method is static in order to consume the given block pointer. It returns a pointer to a freed block that is the result of merging the given block with either or both of its neighbors, if they were free.

Note: Freeing may modify the adjacent blocks if they are free. Allocators that track free blocks must be prepared to handle this merge.

◆ DoResize()

template<typename Derived >
constexpr BlockResult< Derived > pw::allocator::AllocatableBlock< Derived >::DoResize ( size_t  new_inner_size,
bool  shifted = false 
)
constexprprotected

Grows or shrinks the block.

If successful, block may be merged with the block after it in order to provide additional memory (when growing) or to merge released memory (when shrinking). If unsuccessful, block will be unmodified.

Note: Resizing may modify the block following this one if it is free. Allocators that track free blocks based on their size must be prepared to handle this size change.

Precondition
The block must be in use.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The resize completed successfully.
* 
*     FAILED_PRECONDITION: This block is not in use.
* 
*     RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
*     request. This may be due to a large requested size, or insufficient
*     remaining space to fulfill the requested alignment create a valid
*     leading block, and/or create a valid trailing block.
* 
*  

◆ Free()

template<typename Derived >
constexpr BlockResult< Derived > pw::allocator::AllocatableBlock< Derived >::Free ( Derived *&&  block)
staticconstexpr

Marks the block as free.

This method is static in order to consume the given block pointer. It returns a pointer to a freed block that is the result of merging the given block with either or both of its neighbors, if they were free.

Note: Freeing may modify the adjacent blocks if they are free. Allocators that track free blocks must be prepared to handle this merge.

◆ IsFree()

template<typename Derived >
constexpr bool pw::allocator::AllocatableBlock< Derived >::IsFree
constexpr
Returns
whether this block is free or is in use.

◆ Resize()

template<typename Derived >
constexpr BlockResult< Derived > pw::allocator::AllocatableBlock< Derived >::Resize ( size_t  new_inner_size)
constexpr

Grows or shrinks the block.

If successful, block may be merged with the block after it in order to provide additional memory (when growing) or to merge released memory (when shrinking). If unsuccessful, block will be unmodified.

Note: Resizing may modify the block following this one if it is free. Allocators that track free blocks based on their size must be prepared to handle this size change.

Precondition
The block must be in use.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: The resize completed successfully.
* 
*     FAILED_PRECONDITION: This block is not in use.
* 
*     RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
*     request. This may be due to a large requested size, or insufficient
*     remaining space to fulfill the requested alignment create a valid
*     leading block, and/or create a valid trailing block.
* 
*  

◆ Used()

template<typename Derived >
constexpr bool pw::allocator::AllocatableBlock< Derived >::Used ( ) const
inlineconstexpr

Indicates whether the block is in use is free.

This method will eventually be deprecated. Prefer IsFree.


The documentation for this class was generated from the following file: