-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (126 loc) · 3.89 KB
/
main.cpp
File metadata and controls
143 lines (126 loc) · 3.89 KB
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
136
137
138
139
140
141
142
143
#include<iostream>
#include<Windows.h>
#include "random.h"
#include "sm.h"
//----------------------------------------------------------------------------------------------
// Globals
//----------------------------------------------------------------------------------------------
RandomNumberGenerator g_rndGenerator;
//----------------------------------------------------------------------------------------------
// Configurations
//----------------------------------------------------------------------------------------------
#define DO_POOL_ALLOCATION true
#define DO_DEALLOCATIONS_ALWAYS false // Priority 1 - If this is true, value of DO_DEALLOCATIONS is ignored
#define DO_DEALLOCATIONS true // Priority 2
#define MAX_ALLOCATIONS 0xffffff
#define MAX_ALLOCATION_VALUE 128
const unsigned int POOL_SIZE = 0xfffff;
const unsigned int INITIAL_POOLS[] = { 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128};
//----------------------------------------------------------------------------------------------
// M A I N
//----------------------------------------------------------------------------------------------
int main()
{
if (DO_POOL_ALLOCATION)
{
/* Initialize Storage Manager */
printf("Allocation from POOL\n");
int numPools = sizeof(INITIAL_POOLS) / sizeof(INITIAL_POOLS[0]);
initStorageManager(POOL_SIZE, numPools, INITIAL_POOLS);
//displayPoolInfo();
}
else
{
printf("Allocation from HEAP\n");
}
/* Gennerate random memory allocations */
unsigned int i = 0;
unsigned int sz = 0;
vector<unsigned int> allocSize;
while (i < MAX_ALLOCATIONS)
{
do
{
sz = 8 + g_rndGenerator.generateRandom(MAX_ALLOCATION_VALUE -8 + 1);
} while (sz % 8 != 0);
allocSize.push_back(sz);
i++;
}//Random sizes generated
/* Start time tracking now */
DWORD count = GetTickCount();
unsigned int heapAllocs = 0;
unsigned int heapFrees = 0;
i = 0;
while (i < MAX_ALLOCATIONS)
{
if (DO_POOL_ALLOCATION)
{
char *ptr = SM_ALLOC_ARRAY(char, allocSize[i]);
if (ptr == nullptr)
{
printf("ERROR: ALLOC FAILED!\n");
abort();
}
if (DO_DEALLOCATIONS_ALWAYS)
{
SM_DEALLOC(ptr);
}
else if (DO_DEALLOCATIONS && g_rndGenerator.generateRandom(10) < 5)
{
SM_DEALLOC(ptr);
}
else
{
//DON'T DEALLOCATE
}
}
else
{
char *ptr = (char *)malloc(allocSize[i]);
if (ptr)
{
heapAllocs++;
}
else
{
printf("\n\n**HEAP ALLOCATION FAILURE!**\n");
abort();
}
if (DO_DEALLOCATIONS_ALWAYS)
{
free(ptr);
ptr == nullptr;
heapFrees++;
}
else if (DO_DEALLOCATIONS && g_rndGenerator.generateRandom(10) < 5)
{
free(ptr);
ptr == nullptr;
heapFrees++;
}
else
{
//DON'T DEALLOCATE
}
}
i++;
}
DWORD timetaken = GetTickCount() - count;
if (DO_POOL_ALLOCATION)
{
displayPoolInfo();
}
else
{
printf("\n");
printf("heapAllocs : %u\n", heapAllocs);
printf("heapFrees : %u", heapFrees);
}
cout << "\n\n** Interval = " << timetaken << " ms ****" << endl;
if (DO_POOL_ALLOCATION)
{
destroyStorageManager();
}
getchar();
return 0;
}