summaryrefslogtreecommitdiff
path: root/deimos/core/temp_allocator.cpp
blob: d2b7900c959ebb131e6e28599dff8ede39e2091c (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "deimos/core/temp_allocator.h"

#include "deimos/core/api_registry.h"
#include "deimos/core/allocator.h"
#include "deimos/core/os.h"

static deimos::OsApi* os_api;

namespace deimos
{

class TempAllocatorImpl : public ITempAllocator
{
    static constexpr int64_t kDefaultAlign    = 8;
    static constexpr int64_t kPageSize        = 1 * Megabytes;
    static constexpr int64_t kReserveSize     = 256 * Megabytes;
    
    gsl::owner<void*> m_base{};
    void*             m_commit_end{};
    void*             m_reserve_end{};

    // @Todo Last allocation optimization
    void*             m_current{};
    
public:
    gsl::owner<void*> Reallocate(TempAllocatorTag tag,
        gsl::owner<void*> old_ptr, int64_t old_size, int64_t new_size) override
    {
        void* tag_ptr = std::bit_cast<void*>(tag.tag);
        Expects(tag_ptr >= m_base && tag_ptr <= m_commit_end && tag_ptr <= m_current);
        
        if (new_size <= old_size) { return (new_size == 0) ? nullptr : old_ptr; }

        void* new_current = OffsetBytes(m_current, AlignUp(new_size, kDefaultAlign));
        if (new_current > m_reserve_end)
        {
            deimos_Panic("Ran out of temporary memory");
            return nullptr;
        }

        if (new_current > m_commit_end)
        {
            const int64_t new_commit_size = AlignUp(
                (int64_t)(std::bit_cast<uintptr_t>(new_current) - std::bit_cast<uintptr_t>(m_base)), // NOLINT
                kPageSize);
            os_api->virtual_memory->Commit(m_base, new_commit_size);
            m_commit_end = OffsetBytes(m_base, new_commit_size);
            Ensures(m_commit_end <= m_reserve_end);
        }

        if (old_ptr != nullptr)
        {
            MemoryCopy(m_current, old_ptr, old_size);
        }
        
        return std::exchange(m_current, new_current);
    }
    
    TempAllocator Acquire()
    {
        if (m_base == nullptr)
        {
            m_base        = os_api->virtual_memory->Reserve(kReserveSize);
            m_current     = m_base;
            m_commit_end  = m_base;
            m_reserve_end = OffsetBytes(m_base, kReserveSize);
        }
        
        return TempAllocator(this, {std::bit_cast<uintptr_t>(m_current)});
    }
    
    void Release(TempAllocatorTag tag) override
    {
        void* rewind_base = std::bit_cast<void*>(tag.tag);
        Expects(rewind_base >= m_base && rewind_base <= m_commit_end);
        
        if (rewind_base < m_current)
        {
            m_current = rewind_base;
        }
        else
        {
            deimos_Panic("Invalid temporary allocator rewind");
        }
    }
};

static thread_local TempAllocatorImpl g_impl;

class TempAllocatorApiImpl : public TempAllocatorApi
{
public:
    TempAllocator Acquire() override
    {
        return g_impl.Acquire();
    }
};

void RegisterTempAllocatorApi(ApiRegistry* api_registry)
{
    os_api = api_registry->Get<OsApi>();
    
    auto* allocator = api_registry->Get<AllocatorApi>()->system;
    gsl::owner<TempAllocatorApi*> temp_allocator_api = allocator->New<TempAllocatorApiImpl>();
    api_registry->Set(temp_allocator_api);
}

} // namespace deimos