0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-04 12:58:53 +01:00
construct/include/ircd/allocator.h

401 lines
9.6 KiB
C
Raw Normal View History

2016-11-29 16:23:38 +01:00
/*
* charybdis: 21st Century IRC++d
* util.h: Miscellaneous utilities
*
* Copyright (C) 2016 Charybdis Development Team
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#pragma once
#define HAVE_IRCD_ALLOCATOR_H
2017-09-21 07:55:01 +02:00
namespace ircd::allocator
2016-11-29 16:23:38 +01:00
{
2017-04-03 05:50:59 +02:00
struct state;
template<class T = char> struct dynamic;
2016-11-29 16:23:38 +01:00
template<class T = char, size_t = 512> struct fixed;
template<class T> struct node;
2016-11-29 16:23:38 +01:00
};
struct ircd::allocator::state
2016-11-29 16:23:38 +01:00
{
2017-04-03 05:50:59 +02:00
using word_t = unsigned long long;
using size_type = std::size_t;
2017-04-03 05:50:59 +02:00
size_t size { 0 };
word_t *avail { nullptr };
size_t last { 0 };
2017-04-03 05:50:59 +02:00
static uint byte(const uint &i) { return i / (sizeof(word_t) * 8); }
static uint bit(const uint &i) { return i % (sizeof(word_t) * 8); }
static word_t mask(const uint &pos) { return word_t(1) << bit(pos); }
2016-11-29 16:23:38 +01:00
bool test(const uint &pos) const { return avail[byte(pos)] & mask(pos); }
void bts(const uint &pos) { avail[byte(pos)] |= mask(pos); }
void btc(const uint &pos) { avail[byte(pos)] &= ~mask(pos); }
uint next(const size_t &n) const;
2017-04-03 05:50:59 +02:00
void deallocate(const uint &p, const size_t &n);
uint allocate(const size_t &n, const uint &hint = -1);
state(const size_t &size = 0,
word_t *const &avail = nullptr)
:size{size}
,avail{avail}
,last{0}
{}
};
template<class T,
size_t max>
struct ircd::allocator::fixed
2017-04-03 05:50:59 +02:00
:state
{
struct allocator;
std::array<word_t, max / 8> avail {{ 0 }};
std::array<T, max> buf alignas(16);
public:
allocator operator()();
operator allocator();
fixed()
:state{max, avail.data()}
{}
};
template<class T,
size_t size>
struct ircd::allocator::fixed<T, size>::allocator
{
2016-11-29 16:23:38 +01:00
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;
2017-04-03 05:50:59 +02:00
fixed *s;
public:
template<class U, size_t S> struct rebind
2016-11-29 16:23:38 +01:00
{
2017-04-03 05:50:59 +02:00
using other = typename fixed<U, S>::allocator;
2016-11-29 16:23:38 +01:00
};
size_type max_size() const { return size; }
auto address(reference x) const { return &x; }
auto address(const_reference x) const { return &x; }
2017-04-03 05:50:59 +02:00
pointer allocate(const size_type &n, const const_pointer &hint = nullptr)
{
const uint hintpos(hint? hint - s->buf.data() : -1);
return s->buf.data() + s->state::allocate(n, hint);
}
2016-11-29 16:23:38 +01:00
2017-04-03 05:50:59 +02:00
void deallocate(const pointer &p, const size_type &n)
{
const uint pos(p - s->buf.data());
s->state::deallocate(pos, n);
}
template<class U, size_t S>
allocator(const typename fixed<U, S>::allocator &) noexcept
:s{reinterpret_cast<fixed *>(s.s)}
{}
allocator(fixed &s) noexcept
:s{&s}
{}
2016-11-29 16:23:38 +01:00
allocator(allocator &&) = default;
allocator(const allocator &) = default;
2017-04-03 05:50:59 +02:00
friend bool operator==(const allocator &a, const allocator &b)
{
return &a == &b;
}
friend bool operator!=(const allocator &a, const allocator &b)
{
return &a == &b;
}
2016-11-29 16:23:38 +01:00
};
template<class T,
size_t size>
typename ircd::allocator::fixed<T, size>::allocator
ircd::allocator::fixed<T, size>::operator()()
{
return { *this };
}
template<class T,
size_t size>
ircd::allocator::fixed<T, size>::operator
allocator()
{
return { *this };
}
template<class T>
struct ircd::allocator::dynamic
:state
{
struct allocator;
size_t head_size, data_size;
std::unique_ptr<uint8_t[]> arena;
T *buf;
public:
allocator operator()();
operator allocator();
dynamic(const size_t &size)
:state{size}
,head_size{size / 8}
,data_size{sizeof(T) * size + 16}
,arena
{
new __attribute__((aligned(16))) uint8_t[head_size + data_size]
}
,buf
{
reinterpret_cast<T *>(arena.get() + head_size + (head_size % 16))
}
{
state::avail = reinterpret_cast<word_t *>(arena.get());
}
};
2017-04-03 05:50:59 +02:00
template<class T>
struct ircd::allocator::dynamic<T>::allocator
2017-04-03 05:50:59 +02:00
{
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;
2017-04-03 05:50:59 +02:00
dynamic *s;
2017-04-03 05:50:59 +02:00
public:
template<class U> struct rebind
{
using other = typename dynamic<U>::allocator;
};
2017-04-03 05:50:59 +02:00
size_type max_size() const { return s->size; }
auto address(reference x) const { return &x; }
auto address(const_reference x) const { return &x; }
2017-04-03 05:50:59 +02:00
pointer allocate(const size_type &n, const const_pointer &hint = nullptr)
{
const uint hintpos(hint? hint - s->buf : -1);
return s->buf + s->state::allocate(n, hintpos);
}
2016-11-29 16:23:38 +01:00
2017-04-03 05:50:59 +02:00
void deallocate(const pointer &p, const size_type &n)
{
const uint pos(p - s->buf);
s->state::deallocate(pos, n);
}
2016-11-29 16:23:38 +01:00
2017-04-03 05:50:59 +02:00
template<class U>
allocator(const typename dynamic<U>::allocator &) noexcept
:s{reinterpret_cast<dynamic *>(s.s)}
{}
allocator(dynamic &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>
typename ircd::allocator::dynamic<T>::allocator
ircd::allocator::dynamic<T>::operator()()
2016-11-29 16:23:38 +01:00
{
2017-04-03 05:50:59 +02:00
return { *this };
2016-11-29 16:23:38 +01:00
}
2017-04-03 05:50:59 +02:00
template<class T>
ircd::allocator::dynamic<T>::operator
allocator()
2016-11-29 16:23:38 +01:00
{
2017-04-03 05:50:59 +02:00
return { *this };
2016-11-29 16:23:38 +01:00
}
template<class T>
struct ircd::allocator::node
{
struct allocator;
struct monotonic;
T *next {nullptr};
node() = default;
};
template<class T>
struct ircd::allocator::node<T>::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;
node *s;
public:
template<class U> struct rebind
{
using other = typename node<U>::allocator;
};
size_type max_size() const { return std::numeric_limits<size_t>::max(); }
auto address(reference x) const { return &x; }
auto address(const_reference x) const { return &x; }
template<class U, class... args>
void construct(U *p, args&&... a)
{
2017-10-12 02:42:07 +02:00
new (p) U(std::forward<args>(a)...);
}
void construct(pointer p, const_reference val)
{
2017-10-12 02:42:07 +02:00
new (p) T(val);
}
pointer allocate(const size_type &n, const const_pointer &hint = nullptr)
{
assert(n == 1);
assert(hint == nullptr);
assert(s->next != nullptr);
return s->next;
}
void deallocate(const pointer &p, const size_type &n)
{
assert(n == 1);
}
template<class U>
allocator(const typename node<U>::allocator &s) noexcept
:s{reinterpret_cast<node *>(s.s)}
{
}
template<class U>
allocator(const U &s) noexcept
:s{reinterpret_cast<node *>(s.s)}
{
}
allocator(node &s) noexcept
:s{&s}
{
}
allocator() = default;
allocator(allocator &&) noexcept = 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;
}
};
2017-04-03 05:50:59 +02:00
inline void
ircd::allocator::state::deallocate(const uint &pos,
const size_type &n)
{
for(size_t i(0); i < n; ++i)
btc(pos + i);
}
inline uint
ircd::allocator::state::allocate(const size_type &n,
const uint &hint)
{
const auto next(this->next(n));
if(unlikely(next >= size)) // No block of n was found anywhere (next is past-the-end)
throw std::bad_alloc();
for(size_t i(0); i < n; ++i)
bts(next + i);
last = next + n;
return next;
}
inline uint
ircd::allocator::state::next(const size_t &n)
2016-11-29 16:23:38 +01:00
const
{
uint ret(last), rem(n);
for(; ret < size && rem; ++ret)
if(test(ret))
2016-11-29 16:23:38 +01:00
rem = n;
else
--rem;
if(likely(!rem))
return ret - n;
for(ret = 0, rem = n; ret < last && rem; ++ret)
if(test(ret))
2016-11-29 16:23:38 +01:00
rem = n;
else
--rem;
if(unlikely(rem)) // The allocator should throw std::bad_alloc if !rem
2016-11-29 16:23:38 +01:00
return size;
return ret - n;
}