summaryrefslogtreecommitdiffhomepage
path: root/src/handle-page.hpp
blob: c0397db792b2ad31645a3821a1350a7827aa8fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#pragma once
#include "handle-fwd.hpp"
#include "compat/defs.hpp"
#include <array>
#include <cr/BitArray.h>

namespace floormat::impl_handle {

template<typename Obj, uint32_t PageSize>
struct Item
{
    union { Obj object; };
    Handle<Obj, PageSize> handle;
    uint32_t next;
};

template<typename Obj, uint32_t PageSize>
class Page
{
    friend struct Handle<Obj, PageSize>;

    std::array<Item<Obj, PageSize>, PageSize> storage;
    BitArray used_map; // todo replace with a rewrite of std::bitset
    uint32_t start_index;
    uint32_t used_count = 0;
    uint32_t first_free = (uint32_t)-1;

public:
    fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(Page);

    explicit Page(uint32_t start_index);
    ~Page() noexcept;

    [[nodiscard]] Item<Obj, PageSize>& allocate();
    void deallocate(Handle<Obj, PageSize> obj);
    bool is_empty();
    bool is_full();
};

} // namespace floormat::impl_handle