-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryManager.hpp
73 lines (51 loc) · 1.67 KB
/
MemoryManager.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
#ifndef MemoryManager_hpp
#define MemoryManager_hpp
#include "Debug.cpp"
#include <sys/mman.h>
using namespace std;
template<class T>
class MemoryManager
{
public:
//- do not use with types like STL containers using dynamic heap allocation
MemoryManager(uint16_t SegmentCount, uint16_t SegmentSize):
SegmentCount(SegmentCount),
SegmentSize(SegmentSize)
{
DBG(120, "Contructor");
SegmentOffset = 0;
allocateMemory();
}
~MemoryManager() {
DBG(120, "Destructor");
free(MemoryBaseAddress);
DBG(120, "Freed memory");
}
T* getNextMemPointer() {
SegmentOffset >= SegmentCount ? 0 : SegmentOffset++;
return getMemPointer(SegmentOffset);
}
T* getMemBaseAddress() {
return MemoryBaseAddress;
}
private:
uint16_t SegmentCount;
uint16_t SegmentSize;
uint16_t SegmentOffset;
T* MemoryBaseAddress;
void allocateMemory() {
unsigned int MemSizeBytes = SegmentCount*sizeof(T)*SegmentSize;
MemoryBaseAddress = static_cast<T*>(malloc(MemSizeBytes));
madvise(MemoryBaseAddress, MemSizeBytes, MADV_HUGEPAGE);
DBG(95, "Allocate Memory Address:" << static_cast<void*>(MemoryBaseAddress));
}
T* getMemPointer(uint16_t SegmentOffset) {
T* MemPointer = MemoryBaseAddress;
if (SegmentOffset < SegmentCount) {
MemPointer += (SegmentOffset*SegmentSize)*sizeof(T);
}
DBG(180, "Memory Base Address:" << static_cast<void*>(MemoryBaseAddress) << " SegmentOffset:" << SegmentOffset << " MemPointer Address:" << static_cast<void*>(MemPointer));
return MemPointer;
}
};
#endif