-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.cpp
135 lines (133 loc) · 3.4 KB
/
manager.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "alloc.h"
MemoryAllocator::MemoryAllocator() { }
MemoryAllocator::~MemoryAllocator()
{
assert(service.wasDestroyCalled == service.wasInitCalled);
}
void MemoryAllocator::init()
{
fsAlloc16.init(16);
fsAlloc32.init(32);
fsAlloc64.init(64);
fsAlloc128.init(128);
fsAlloc256.init(256);
fsAlloc512.init(512);
cAlloc.init();
fAlloc.init();
service.wasInitCalled = true;
}
void MemoryAllocator::destroy()
{
assert(service.wasInitCalled == true);
assert(isFree() == true);
fsAlloc16.destroy();
fsAlloc32.destroy();
fsAlloc64.destroy();
fsAlloc128.destroy();
fsAlloc256.destroy();
fsAlloc512.destroy();
cAlloc.destroy();
fAlloc.destroy();
service.wasDestroyCalled = true;
}
void* MemoryAllocator::alloc(size_t size)
{
assert(service.wasInitCalled == true);
if (size <= 16)
return fsAlloc16.alloc();
if (size <= 32)
return fsAlloc32.alloc();
if (size <= 64)
return fsAlloc64.alloc();
if (size <= 128)
return fsAlloc128.alloc();
if (size <= 256)
return fsAlloc256.alloc();
if (size <= 512)
return fsAlloc512.alloc();
if (size <= 10485760)
return cAlloc.alloc(size);
return fAlloc.alloc(size);
}
void MemoryAllocator::free(void* p)
{
assert(service.wasInitCalled == true);
bool freedSuccessfully = fsAlloc16.free(p);
if (freedSuccessfully == true)
goto X;
freedSuccessfully = fsAlloc32.free(p);
if (freedSuccessfully == true)
goto X;
freedSuccessfully = fsAlloc64.free(p);
if (freedSuccessfully == true)
goto X;
freedSuccessfully = fsAlloc128.free(p);
if (freedSuccessfully == true)
goto X;
freedSuccessfully = fsAlloc256.free(p);
if (freedSuccessfully == true)
goto X;
freedSuccessfully = fsAlloc512.free(p);
if (freedSuccessfully == true)
goto X;
freedSuccessfully = cAlloc.free(p);
if (freedSuccessfully == true)
goto X;
freedSuccessfully = fAlloc.free(p);
X: assert(freedSuccessfully == true);
}
void MemoryAllocator::dumpStat() const
{
assert(service.wasInitCalled == true);
std::cout << "MEMORY ALLOCATOR STATS\n";
std::cout << "Total memory requested from the OS: " <<
fsAlloc16.getAllocatedSize() +
fsAlloc32.getAllocatedSize() +
fsAlloc64.getAllocatedSize() +
fsAlloc128.getAllocatedSize() +
fsAlloc256.getAllocatedSize() +
fsAlloc512.getAllocatedSize() +
cAlloc.getAllocatedSize() +
fAlloc.getOccupiedSize() << " bytes.\n";
std::cout << "Total memory occupied: " <<
fsAlloc16.getOccupiedSize() +
fsAlloc32.getOccupiedSize() +
fsAlloc64.getOccupiedSize() +
fsAlloc128.getOccupiedSize() +
fsAlloc256.getOccupiedSize() +
fsAlloc512.getOccupiedSize() +
cAlloc.getOccupiedSize() +
fAlloc.getOccupiedSize() << " bytes.\n";
fsAlloc16.dumpStat();
fsAlloc32.dumpStat();
fsAlloc64.dumpStat();
fsAlloc128.dumpStat();
fsAlloc256.dumpStat();
fsAlloc512.dumpStat();
cAlloc.dumpStat();
fAlloc.dumpStat();
std::cout << "\n\n";
}
void MemoryAllocator::dumpBlocks() const
{
fsAlloc16.dumpBlocks();
fsAlloc32.dumpBlocks();
fsAlloc64.dumpBlocks();
fsAlloc128.dumpBlocks();
fsAlloc256.dumpBlocks();
fsAlloc512.dumpBlocks();
cAlloc.dumpBlocks();
/*fAlloc.dumpBlocks();*/
std::cout << "\n";
}
bool MemoryAllocator::isFree() const
{
return fsAlloc16.isFree() &&
fsAlloc32.isFree() &&
fsAlloc64.isFree() &&
fsAlloc128.isFree() &&
fsAlloc256.isFree() &&
fsAlloc512.isFree() &&
cAlloc.isFree() &&
fAlloc.isFree();
}