forked from Ares-Developers/YRpp
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathYRAllocator.h
74 lines (61 loc) · 1.43 KB
/
YRAllocator.h
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
// Memory allocation handler
#pragma once
#include <YRPPCore.h>
#include <Memory.h>
class MemoryBuffer
{
public:
constexpr MemoryBuffer() noexcept = default;
explicit MemoryBuffer(int size) noexcept
: MemoryBuffer(nullptr, size)
{ }
MemoryBuffer(void* pBuffer, int size) noexcept
: Buffer(pBuffer), Size(size)
{
if(!this->Buffer && this->Size > 0) {
this->Buffer = YRMemory::Allocate(static_cast<size_t>(size));
this->Allocated = true;
}
}
constexpr MemoryBuffer(MemoryBuffer const& other) noexcept
: Buffer(other.Buffer), Size(other.Size)
{ }
MemoryBuffer(MemoryBuffer&& other) noexcept
: Buffer(other.Buffer), Size(other.Size), Allocated(other.Allocated)
{
other.Allocated = false;
}
~MemoryBuffer() noexcept
{
if(this->Allocated) {
YRMemory::Deallocate(this->Buffer);
}
}
MemoryBuffer& operator = (MemoryBuffer const& other) noexcept
{
if(this != &other) {
MemoryBuffer tmp(static_cast<MemoryBuffer&&>(*this));
this->Buffer = other.Buffer;
this->Size = other.Size;
}
return *this;
}
MemoryBuffer& operator = (MemoryBuffer&& other) noexcept
{
*this = other;
auto const allocated = other.Allocated;
other.Allocated = false;
this->Allocated = allocated;
return *this;
}
void Clear() noexcept
{
MemoryBuffer tmp(static_cast<MemoryBuffer&&>(*this));
this->Buffer = nullptr;
this->Size = 0;
}
public:
void* Buffer{ nullptr };
int Size{ 0 };
bool Allocated{ false };
};