summaryrefslogtreecommitdiff
path: root/deimos/core/allocator.h
blob: 911f8e778aaa7875c792fd61c5cdabdeb2299b91 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once

#include "deimos/core/base.h"
#include "deimos/core/id_name.h"

namespace deimos
{

struct MemoryScope { uint32 id; };

struct IAllocator
{
    IAllocator() = default;

    deimos_NO_COPY_MOVE(IAllocator);

    virtual ~IAllocator() = default;

    [[nodiscard]]
    virtual void* Reallocate(
        void* old_ptr, int64 old_size, int64 new_size,
        MemoryScope scope, const SourceLocation& source_location = {}) = 0;
};


class Allocator
{
    IAllocator* m_allocator;
    const MemoryScope m_scope;

public:
    constexpr explicit Allocator(IAllocator* allocator, MemoryScope scope) :
        m_allocator{allocator}, m_scope{scope}
    {}

    deimos_NO_COPY_MOVE(Allocator);

    ~Allocator() = default;

    [[nodiscard]]
    constexpr void* Allocate(
        int64 new_size,
        const SourceLocation& source_location = {})
    {
        return m_allocator->Reallocate(nullptr, 0, new_size, m_scope, source_location);
    }

    [[nodiscard]]
    void* Reallocate(
        void* old_ptr, int64 old_size, int64 new_size,
        const SourceLocation& source_location = {})
    {
        return m_allocator->Reallocate(old_ptr, old_size, new_size, m_scope, source_location);
    }

    constexpr void Free(
        void* old_ptr, int64 old_size,
        const SourceLocation& source_location = {})
    {
        (void)m_allocator->Reallocate(old_ptr, old_size, 0, m_scope, source_location);
    }

    template<typename T, typename... Args>
    T* NewInner(
        const SourceLocation& source_location,
        Args&&... args)
    {
        void* ptr = Allocate(sizeof(T), source_location);
        return new(ptr) T(std::forward<Args>(args)...);
    }

    template<typename T>
    constexpr T* New(const SourceLocation& source_location = {})
    {
        return NewInner<T>(source_location);
    }

    template<typename T, typename A0>
    constexpr T* New(A0&& arg0, const SourceLocation& source_location = {})
    {
        return NewInner<T>(source_location, std::forward<A0>(arg0));
    }

    template<typename T, typename A0, typename A1>
    constexpr T* New(
        A0&& arg0, A1&& arg1,
        const SourceLocation& source_location = {})
    {
        return NewInner<T>(source_location,
            std::forward<A0>(arg0),
            std::forward<A1>(arg1));
    }

    template<typename T, typename A0, typename A1, typename A2>
    constexpr T* New(
        A0&& arg0, A1&& arg1, A2&& arg2,
        const SourceLocation& source_location = {})
    {
        return NewInner<T>(source_location,
            std::forward<A0>(arg0),
            std::forward<A1>(arg1),
            std::forward<A2>(arg2));
    }

    template<typename T, typename A0, typename A1, typename A2, typename A3>
    constexpr T* New(
        A0&& arg0, A1&& arg1, A2&& arg2, A3&& arg3,
        const SourceLocation& source_location = {})
    {
        return NewInner<T>(source_location,
            std::forward<A0>(arg0),
            std::forward<A1>(arg1),
            std::forward<A2>(arg2),
            std::forward<A3>(arg3));
    }

    template<typename T>
    void Delete(T* t, const SourceLocation& source_location = {})
    {
        if constexpr (!kIsTriviallyDestructible<T>)
        {
            t->~T();
        }
        Free(t, sizeof(T), source_location);
    }
};

class AllocatorApi
{
public:
    AllocatorApi() = default;

    deimos_NO_COPY_MOVE(AllocatorApi);

    virtual ~AllocatorApi() = default;

    static constexpr IdName kApiName{"deimos::AllocatorApi"};

    Allocator* system{};

    virtual Allocator* CreateChild(Allocator* parent, const char* description) = 0;
    virtual void DestroyChild(Allocator*) = 0;
};

} // namespace deimos