-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMemoryManager.h
51 lines (42 loc) · 1.1 KB
/
MemoryManager.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
#ifndef MEMORY_MANAGER_H
#define MEMORY_MANAGER_H
#include <vector>
using namespace std;
struct FreeMemoryBlock
{
uint32 startAddress;
uint32 endAddress;
uint32 numBytes;
};
struct AllocatedMemoryBlock
{
uint32 startAddress;
uint32 numBytes;
};
class MemoryManager
{
public:
MemoryManager() {}
~MemoryManager() {}
void AddAllocatedBlock(const uint32 startAddress, const uint32 numBytes);
void Add(const uint32 startAddress, const uint32 endAddress, uint32 index = 0);
uint32 Allocate(uint32 requestedBytes, uint32 requestedAlignment);
void Free(const uint32 address);
inline void Reset()
{
freeBlockVector.clear();
allocatedBlockVector.clear();
}
private:
uint32 AlignAddress(const uint32 address, const uint32 alignment)
{
return (address + alignment - 1) & (~(alignment - 1));
}
bool TestForInvalidPowerOfTwo(const uint32 requestedAlignment)
{
return (requestedAlignment & (requestedAlignment - 1)) != 0;
}
vector<FreeMemoryBlock> freeBlockVector;
vector<AllocatedMemoryBlock> allocatedBlockVector;
};
#endif