-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbump-allocator.hpp
107 lines (88 loc) · 2.26 KB
/
bump-allocator.hpp
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
#pragma once
#include <nall/memory.hpp>
namespace nall {
struct bump_allocator {
static constexpr u32 executable = 1 << 0;
static constexpr u32 zero_fill = 1 << 1;
~bump_allocator() {
reset();
}
explicit operator bool() const {
return _memory;
}
auto reset() -> void {
if(_owner) memory::unmap(_memory, _capacity);
_memory = nullptr;
_capacity = 0;
_offset = 0;
_owner = false;
}
auto resize(u32 capacity, u32 flags = 0, u8* buffer = nullptr) -> bool {
reset();
if(buffer) {
if(flags & executable) {
memory::protect(buffer, capacity, true);
}
if(flags & zero_fill) {
memset(buffer, 0x00, capacity);
}
} else {
buffer = (u8*)memory::map(capacity, flags & executable);
if(!buffer) return false;
_owner = true;
}
_memory = buffer;
_capacity = capacity;
return true;
}
//release all acquired memory
auto release(u32 flags = 0) -> void {
_offset = 0;
if(flags & zero_fill) memset(_memory, 0x00, _capacity);
}
auto capacity() const -> u32 {
return _capacity;
}
auto available() const -> u32 {
return _capacity - _offset;
}
//for allocating blocks of known size
auto acquire(u32 size) -> u8* {
#ifdef DEBUG
struct out_of_memory {};
if((nextOffset(size)) > _capacity) throw out_of_memory{};
#endif
auto memory = _memory + _offset;
_offset = nextOffset(size); //alignment
return memory;
}
//for allocating blocks of unknown size (eg for a dynamic recompiler code block)
auto acquire() -> u8* {
#ifdef DEBUG
struct out_of_memory {};
if(_offset > _capacity) throw out_of_memory{};
#endif
return _memory + _offset;
}
//size can be reserved once the block size is known
auto reserve(u32 size) -> void {
#ifdef DEBUG
struct out_of_memory {};
if((nextOffset(size)) > _capacity) throw out_of_memory{};
#endif
_offset = nextOffset(size); //alignment
}
auto tryAcquire(u32 size) -> u8* {
if((nextOffset(size)) > _capacity) return nullptr;
return acquire(size);
}
private:
auto nextOffset(u32 size) const -> u32 {
return _offset + size + 15 & ~15;
}
u8* _memory = nullptr;
u32 _capacity = 0;
u32 _offset = 0;
bool _owner = false;
};
}