summaryrefslogtreecommitdiff
path: root/deimos/core/allocator.cpp
blob: 1a823c1a92e56027773d08f98453910cc750bd35 (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
#include "deimos/core/allocator.h"

#include <cstdlib>

namespace deimos
{

class SystemAllocatorImpl : public IAllocator
{
public:
    void* Reallocate(
        void* old_ptr, int64_t /* old_size */, int64_t new_size,
        MemoryScope /* scope */, const SourceLocation& /* source_location */) override
    {
        if (old_ptr == nullptr)
        {
            return new_size > 0 ? ::malloc((uint64_t)new_size) : nullptr;
        }

        if (new_size == 0)
        {
            if (old_ptr != nullptr)
            {
                ::free(old_ptr);
            }
        }
        else
        {
            return ::realloc(old_ptr, (uint64_t)new_size);
        }
        return nullptr;
    }
};

class AllocatorApiImpl : public AllocatorApi
{
    SystemAllocatorImpl m_system_impl;
    Allocator m_system;

public:
    constexpr AllocatorApiImpl() :
        m_system{ &m_system_impl, {0} }
    {
        system = &m_system;
    }

    Allocator* CreateChild(Allocator* parent, gsl::czstring /* description */) override
    {
        return parent;
    }

    void DestroyChild(Allocator*) override
    {
    }
};

AllocatorApi* BootstrapAllocatorApi()
{
    static constinit AllocatorApiImpl g_instance{};
    return &g_instance;
}

} // namespace deimos