mirror of
https://github.com/matrix-construct/construct
synced 2025-02-17 01:00:10 +01:00
ircd::allocator: Add twolevel allocator; minor fixes.
This commit is contained in:
parent
6ab36254c2
commit
5e8fce2a40
1 changed files with 120 additions and 2 deletions
|
@ -24,6 +24,7 @@ namespace ircd::allocator
|
||||||
struct profile;
|
struct profile;
|
||||||
template<class T = char> struct dynamic;
|
template<class T = char> struct dynamic;
|
||||||
template<class T = char, size_t = 512> struct fixed;
|
template<class T = char, size_t = 512> struct fixed;
|
||||||
|
template<class T = char, size_t L0_SIZE = 512> struct twolevel;
|
||||||
template<class T> struct node;
|
template<class T> struct node;
|
||||||
|
|
||||||
profile &operator+=(profile &, const profile &);
|
profile &operator+=(profile &, const profile &);
|
||||||
|
@ -237,8 +238,9 @@ struct ircd::allocator::fixed<T, SIZE>::allocator
|
||||||
s->state::deallocate(p - base, n);
|
s->state::deallocate(p - base, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class U, size_t OTHER_SIZE>
|
template<class U,
|
||||||
allocator(const typename fixed<U, OTHER_SIZE>::allocator &) noexcept
|
size_t OTHER_SIZE = SIZE>
|
||||||
|
allocator(const typename fixed<U, OTHER_SIZE>::allocator &s) noexcept
|
||||||
:s{reinterpret_cast<fixed<T, SIZE> *>(s.s)}
|
:s{reinterpret_cast<fixed<T, SIZE> *>(s.s)}
|
||||||
{
|
{
|
||||||
static_assert(OTHER_SIZE == SIZE);
|
static_assert(OTHER_SIZE == SIZE);
|
||||||
|
@ -503,3 +505,119 @@ struct ircd::allocator::node<T>::allocator
|
||||||
return &a == &b;
|
return &a == &b;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// The twolevel allocator uses both a fixed allocator (first level) and then
|
||||||
|
/// the standard allocator (second level) when the fixed allocator is exhausted.
|
||||||
|
/// This has the intent that the fixed allocator will mostly be used, but the
|
||||||
|
/// fallback to the standard allocator is seamlessly available for robustness.
|
||||||
|
template<class T,
|
||||||
|
size_t L0_SIZE>
|
||||||
|
struct ircd::allocator::twolevel
|
||||||
|
{
|
||||||
|
struct allocator;
|
||||||
|
|
||||||
|
fixed<T, L0_SIZE> l0;
|
||||||
|
std::allocator<T> l1;
|
||||||
|
|
||||||
|
public:
|
||||||
|
allocator operator()();
|
||||||
|
operator allocator();
|
||||||
|
|
||||||
|
twolevel() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T,
|
||||||
|
size_t L0_SIZE>
|
||||||
|
struct ircd::allocator::twolevel<T, L0_SIZE>::allocator
|
||||||
|
{
|
||||||
|
using value_type = T;
|
||||||
|
using pointer = T *;
|
||||||
|
using const_pointer = const T *;
|
||||||
|
using reference = T &;
|
||||||
|
using const_reference = const T &;
|
||||||
|
using size_type = std::size_t;
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
|
||||||
|
twolevel *s;
|
||||||
|
|
||||||
|
public:
|
||||||
|
template<class U,
|
||||||
|
size_t OTHER_L0_SIZE = L0_SIZE>
|
||||||
|
struct rebind
|
||||||
|
{
|
||||||
|
using other = typename twolevel<U, OTHER_L0_SIZE>::allocator;
|
||||||
|
};
|
||||||
|
|
||||||
|
size_type max_size() const
|
||||||
|
{
|
||||||
|
return std::numeric_limits<size_type>::max();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto address(reference x) const
|
||||||
|
{
|
||||||
|
return &x;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto address(const_reference x) const
|
||||||
|
{
|
||||||
|
return &x;
|
||||||
|
}
|
||||||
|
|
||||||
|
pointer allocate(const size_type &n, const const_pointer &hint = nullptr)
|
||||||
|
{
|
||||||
|
assert(s);
|
||||||
|
return
|
||||||
|
s->l0.allocate(std::nothrow, n, hint)?:
|
||||||
|
s->l1.allocate(n, hint);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(const pointer &p, const size_type &n)
|
||||||
|
{
|
||||||
|
assert(s);
|
||||||
|
if(likely(s->l0.in_range(p)))
|
||||||
|
s->l0.deallocate(p, n);
|
||||||
|
else
|
||||||
|
s->l1.deallocate(p, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class U,
|
||||||
|
size_t OTHER_L0_SIZE = L0_SIZE>
|
||||||
|
allocator(const typename twolevel<U, OTHER_L0_SIZE>::allocator &s) noexcept
|
||||||
|
:s{reinterpret_cast<twolevel<T, L0_SIZE> *>(s.s)}
|
||||||
|
{
|
||||||
|
static_assert(OTHER_L0_SIZE == L0_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
allocator(twolevel &s) noexcept
|
||||||
|
:s{&s}
|
||||||
|
{}
|
||||||
|
|
||||||
|
allocator(allocator &&) = default;
|
||||||
|
allocator(const allocator &) = default;
|
||||||
|
|
||||||
|
friend bool operator==(const allocator &a, const allocator &b)
|
||||||
|
{
|
||||||
|
return &a == &b;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend bool operator!=(const allocator &a, const allocator &b)
|
||||||
|
{
|
||||||
|
return &a == &b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T,
|
||||||
|
size_t L0_SIZE>
|
||||||
|
typename ircd::allocator::twolevel<T, L0_SIZE>::allocator
|
||||||
|
ircd::allocator::twolevel<T, L0_SIZE>::operator()()
|
||||||
|
{
|
||||||
|
return ircd::allocator::twolevel<T, L0_SIZE>::allocator(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T,
|
||||||
|
size_t L0_SIZE>
|
||||||
|
ircd::allocator::twolevel<T, L0_SIZE>::operator
|
||||||
|
allocator()
|
||||||
|
{
|
||||||
|
return ircd::allocator::twolevel<T, L0_SIZE>::allocator(*this);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue