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

#include <cstdlib>

namespace deimos
{

class SystemAllocatorImpl : public IAllocator
{
public:
    SystemAllocatorImpl() : IAllocator(MemoryScope{0}) {}

    void* Reallocate(
        void* old_ptr, int64 /* old_size */, int64 new_size,
        const char* /* file */, int32 /* line */) override
    {
        if (old_ptr == nullptr)
        {
            return new_size > 0 ? ::malloc((uint64)new_size) : nullptr;
        }

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

class AllocatorApiImpl : public AllocatorApi
{
    SystemAllocatorImpl m_system_impl;

public:
    AllocatorApiImpl()
    {
        system = &m_system_impl;
    }
};

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

} // namespace deimos