-
Notifications
You must be signed in to change notification settings - Fork 0
/
falloc.cpp
82 lines (79 loc) · 2.12 KB
/
falloc.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
#include "alloc.h"
FallbackAlloc::FallbackAlloc() : first(NULL), service(Service()) {};
FallbackAlloc::~FallbackAlloc() {};
void FallbackAlloc::init() {};
void FallbackAlloc::destroy()
{
assert(isFree() == true);
};
void* FallbackAlloc::alloc(size_t size)
{
FAPage* newPage = new FAPage;
newPage->pageStart = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
newPage->pageSize = size;
if (first == NULL)
first = newPage;
else
{
FAPage* anotherPage = first;
while (anotherPage->nextPage != NULL)
anotherPage = anotherPage->nextPage;
anotherPage->nextPage = newPage;
}
service.occupiedSize += size;
return newPage->pageStart;
}
bool FallbackAlloc::free(void* p)
{
bool freedSuccessfully = false;
FAPage* toFree = NULL;
if (p == first->pageStart)
{
toFree = first;
first = first->nextPage;
freedSuccessfully = true;
}
else
for (FAPage* anotherPage = first; anotherPage->nextPage != NULL; anotherPage = anotherPage->nextPage)
if (anotherPage->nextPage == p)
{
toFree = anotherPage->nextPage;
anotherPage->nextPage = anotherPage->nextPage->nextPage;
freedSuccessfully = true;
service.allocPages--;
break;
}
if (freedSuccessfully == true)
{
service.occupiedSize -= toFree->pageSize;
VirtualFree(p, 0, MEM_RELEASE);
delete toFree;
}
return freedSuccessfully;
}
//void FallbackAlloc::dumpBlocks() const
//{
// int i = 0;
// for (FAPage const* anotherPage = first; anotherPage != NULL; anotherPage = anotherPage->nextPage)
// {
// std::cout << "PAGE #" << i << ": SIZE = " << anotherPage->pageSize << '\n';
// i++;
// }
// std::cout << '\n';
//}
bool FallbackAlloc::isFree() const
{
return service.allocPages == 0;
}
size_t FallbackAlloc::getOccupiedSize() const
{
return service.occupiedSize;
}
void FallbackAlloc::dumpStat() const
{
std::cout << "FALLBACK ALLOCATOR STATS\n";
int i = 0;
std::cout << "\tPAGE#" << "\tSIZE\n";
for (FAPage const* anotherPage = first; anotherPage != NULL; anotherPage = anotherPage->nextPage)
std::cout << '\t' << i++ << '\t' << anotherPage->pageSize << '\n';
}