18 std::optional<uintptr_t>
get(std::size_t
size) {
19 auto it = fl_.begin();
20 while (it != fl_.end()) {
21 if (it->size ==
size) {
25 }
else if (it->size >
size) {
36 std::optional<uintptr_t>
get(std::size_t
size, std::size_t alignment) {
43 if (*s % alignment == 0) {
49 std::size_t req_size =
size + alignment;
62 add(addr_got, addr_ret - addr_got);
63 add(addr_ret +
size, (addr_got + req_size) - (addr_ret +
size));
68 void add(uintptr_t addr, std::size_t
size) {
69 if (
size == 0)
return;
71 auto it = fl_.begin();
72 while (it != fl_.end()) {
73 if (addr +
size == it->addr) {
77 }
else if (addr + size < it->addr) {
78 fl_.insert(it, entry{addr,
size});
80 }
else if (addr == it->addr + it->size) {
82 auto next_it = std::next(it);
83 if (next_it != fl_.end() &&
84 next_it->addr == it->addr + it->size) {
85 it->size += next_it->size;
92 fl_.insert(it, entry{addr,
size});
95 std::size_t
count()
const {
return fl_.size(); }
103 std::list<entry> fl_;
106 ITYR_TEST_CASE(
"[ityr::common::freelist] freelist management") {
107 uintptr_t addr = 100;
108 std::size_t
size = 920;
109 freelist fl(addr,
size);
111 std::vector<uintptr_t> got;
114 for (std::size_t i = 0; i <
size / n; i++) {
122 for (std::size_t i = 0; i < got.size(); i++) {
123 for (std::size_t j = 0; j < got.size(); j++) {
126 got[j] + n <= got[i]));
132 std::random_device seed_gen;
133 std::mt19937 engine(seed_gen());
134 std::shuffle(got.begin(), got.end(), engine);
136 for (
auto&& s : got) {
Definition: freelist.hpp:13
std::optional< uintptr_t > get(std::size_t size, std::size_t alignment)
Definition: freelist.hpp:36
freelist(uintptr_t addr, std::size_t size)
Definition: freelist.hpp:16
std::size_t count() const
Definition: freelist.hpp:95
void add(uintptr_t addr, std::size_t size)
Definition: freelist.hpp:68
std::optional< uintptr_t > get(std::size_t size)
Definition: freelist.hpp:18
freelist()
Definition: freelist.hpp:15
#define ITYR_CHECK(cond)
Definition: util.hpp:48
Definition: allocator.hpp:16
T round_up_pow2(T x, T alignment)
Definition: util.hpp:142
constexpr auto size(const span< T > &s) noexcept
Definition: span.hpp:61