|
|
@ -14,6 +14,14 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace gp{ |
|
|
|
/**
|
|
|
|
* @brief An allocator that uses the buddy algorithm to divide the space into allocatable memory. |
|
|
|
* |
|
|
|
* This is not resilient to memory fragmentation, but the smallest memory unit should always be allocatable unless the memory is actually full. |
|
|
|
* |
|
|
|
* @tparam max_msb The log2 rounded up of the maximum space you expect to address in bytes |
|
|
|
* @tparam align The smallest size of memory you expect the allocator to allocate |
|
|
|
*/ |
|
|
|
template<size_t max_msb = 24, size_t align = 8> |
|
|
|
class buddy : public allocator { |
|
|
|
struct twig { |
|
|
@ -42,16 +50,29 @@ namespace gp{ |
|
|
|
gp::buffer<char> data; |
|
|
|
const size_t max_depth; |
|
|
|
const size_t twig_explore_length; |
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The depth of the tree required to allocate |
|
|
|
*/ |
|
|
|
static constexpr size_t max_theoric_depth = max_msb - gp::math::msb(align); |
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The actual number of twigs to support the specified depth |
|
|
|
*/ |
|
|
|
static constexpr size_t required_twigs = (1 << (max_theoric_depth + 1)) - 1; |
|
|
|
|
|
|
|
/**
|
|
|
|
* ((max allocatable size - min allocatable size) ** 2 - 1) / 4 twigs in a bundle |
|
|
|
* err">@brief ((max allocatable size - min allocatable size) ** 2 - 1) / 4 twigs in a bundle |
|
|
|
**/ |
|
|
|
static constexpr size_t span_size = required_twigs / 4 + (required_twigs % 4 != 0); |
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The array of twigs (in bundles) |
|
|
|
*/ |
|
|
|
gp::array<bundle, span_size> stack; |
|
|
|
|
|
|
|
/**
|
|
|
|
* This code has been manually hecked and will always return. |
|
|
|
* This code has been manually checked and will always return. |
|
|
|
* If you find a case where it doesn't, please file an issue. |
|
|
|
**/ |
|
|
|
#pragma clang diagnostic push
|
|
|
|