-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
87 lines (67 loc) · 2.06 KB
/
main.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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include "MManager.h"
using namespace std;
class myTestClass
{
public:
int m_id;
int m_random;
int m_random2;
};
int main()
{
int numAlloc = 2000000;
int currentAllocCount = 0;
printf("-Started the Memory Manager.\n");
MManager<myTestClass> * pManager = new MManager<myTestClass>();
printf("--Max. number of Objects that we can save : %d \n",pManager->GetCapacity());
clock_t startTime = clock();
printf("Started PreFill\n");
for (unsigned i = 0; i < 0.5 * pManager->GetCapacity(); ++i)
{
myTestClass * pObj = pManager->CreateObject();
pObj->m_id = i;
pObj->m_random = (pManager->GetCapacity() - i * 20 ) << 4;
pObj->m_random2 = pObj->m_random * 0.333;
currentAllocCount++;
}
printf("Started random alloc dealloc\n");
int i = 0;
while(currentAllocCount < numAlloc)
{
++i;
bool alloc = (rand() % 2 == 0) ? true : false;
if(alloc && pManager->IsFull())
alloc = false;
if(!alloc && pManager->IsEmpty())
alloc = true;
if(alloc)
{
myTestClass * pObj = pManager->CreateObject();
pObj->m_id = i;
pObj->m_random = (pManager->GetCapacity() - i * 20 ) << 4;
pObj->m_random2 = pObj->m_random * 0.333;
currentAllocCount++;
}
else
{
int nIndex = (pManager->GetCount() > 1) ? rand() % (pManager->GetCount() - 1) : 0;
myTestClass * pObj = (*pManager)[nIndex];
pManager->RemoveObject(pObj);
}
}
printf("Started RemoveAll\n");
while(!pManager->IsEmpty())
{
myTestClass * pObj = (*pManager)[0];
pManager->RemoveObject(pObj);
}
assert(pManager->GetCount() == 0);
clock_t endTime = clock();
double elapsedTime = (double)(endTime - startTime) / CLOCKS_PER_SEC;
printf("\nTime taken for test to complete: %f seconds\n", elapsedTime);
return 0;
}