Line |
Branch |
Exec |
Source |
1 |
|
|
// Copyright (c) 2021-2024 ChilliBits. All rights reserved. |
2 |
|
|
|
3 |
|
|
#pragma once |
4 |
|
|
|
5 |
|
|
#include <cstdlib> |
6 |
|
|
|
7 |
|
|
namespace spice::compiler { |
8 |
|
|
|
9 |
|
|
// Typedefs |
10 |
|
|
using byte = uint8_t; |
11 |
|
|
|
12 |
|
|
class MemoryManager { |
13 |
|
|
protected: |
14 |
|
|
~MemoryManager() = default; |
15 |
|
|
|
16 |
|
|
public: |
17 |
|
|
[[nodiscard]] virtual byte *allocate(size_t size) const = 0; |
18 |
|
|
virtual void deallocate(byte *ptr) const = 0; |
19 |
|
|
}; |
20 |
|
|
|
21 |
|
|
class DefaultMemoryManager final : public MemoryManager { |
22 |
|
|
public: |
23 |
|
66516 |
[[nodiscard]] byte *allocate(const size_t size) const override { return static_cast<byte *>(malloc(size)); } |
24 |
|
66516 |
void deallocate(byte *ptr) const override { free(ptr); } |
25 |
|
|
}; |
26 |
|
|
|
27 |
|
|
} // namespace spice::compiler |
28 |
|
|
|
29 |
|
|
// Overload new and delete operators for debugging purposes |
30 |
|
|
#ifdef SPICE_NEW_DELETE_OVERLOADED |
31 |
|
|
void *operator new(size_t n) noexcept(false) { return malloc(n); } |
32 |
|
|
void operator delete(void *ptr) noexcept { free(ptr); } |
33 |
|
|
#endif |
34 |
|
|
|