-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
200 lines (166 loc) · 7.02 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "fb_allocator.h"
#include "my_allocator.h"
#include "bm_allocator.h"
#include "Fault.h"
#include <iostream>
#include <string.h>
// @see https://github.com/endurodave/C_Allocator
// David Lafreniere
using namespace std;
// Benchmark allocators defs
typedef void* (*AllocFunc)(int size);
typedef void(*DeallocFunc)(void* ptr);
static const int MAX_BLOCKS = 10000;
static const int MAX_BLOCK_SIZE = 4096;
void* memoryPtrs[MAX_BLOCKS];
void* memoryPtrs2[MAX_BLOCKS];
void Benchmark(const char* name, AllocFunc allocFunc, DeallocFunc deallocFunc);
void* AllocHeap(int size);
void DeallocHeap(void* ptr);
void* AllocFbAllocator(int size);
void DeallocFbAllocator(void* ptr);
void* AllocXAllocator(int size);
void DeallocXAllocator(void* ptr);
struct MyData
{
CHAR data[128];
};
ALLOC_DEFINE(TestAlloc, 16, 5)
ALLOC_DEFINE(BenchmarkFbAlloc, MAX_BLOCK_SIZE, MAX_BLOCKS*2)
int main()
{
// Initialize allocator module
ALLOC_Init();
// Allocate and free memory using TestAlloc allocator
void* data = ALLOC_Alloc(TestAlloc, 16);
memset(data, 0, 16);
ALLOC_Free(TestAlloc, data);
// Allocate memory using MYALLOC allocator
void* mem1 = MYALLOC_Alloc(28);
void* mem2 = MYALLOC_Calloc(1, 32);
mem2 = MYALLOC_Realloc(mem2, 128);
memset(mem1, 0, 28);
memset(mem2, 0, 128);
MYALLOC_Free(mem1);
MYALLOC_Free(mem2);
MyData* myData = (MyData*)MYALLOC_Alloc(sizeof(MyData));
memset(myData->data, 0, 128);
MYALLOC_Free(myData);
ASSERT_TRUE(TestAllocObj.blocksInUse == 0);
Benchmark("Heap (Run 1)", AllocHeap, DeallocHeap);
Benchmark("Heap (Run 2)", AllocHeap, DeallocHeap);
Benchmark("Heap (Run 3)", AllocHeap, DeallocHeap);
Benchmark("fb_allocator (Run 1)", AllocFbAllocator, DeallocFbAllocator);
Benchmark("fb_allocator (Run 2)", AllocFbAllocator, DeallocFbAllocator);
Benchmark("fb_allocator (Run 3)", AllocFbAllocator, DeallocFbAllocator);
Benchmark("bm_allocator (Run 1)", AllocXAllocator, DeallocXAllocator);
Benchmark("bm_allocator (Run 2)", AllocXAllocator, DeallocXAllocator);
Benchmark("bm_allocator (Run 3)", AllocXAllocator, DeallocXAllocator);
return 0;
}
//------------------------------------------------------------------------------
// AllocHeap
//------------------------------------------------------------------------------
void* AllocHeap(int size)
{
return malloc(size);
}
//------------------------------------------------------------------------------
// DeallocHeap
//------------------------------------------------------------------------------
void DeallocHeap(void* ptr)
{
free(ptr);
}
//------------------------------------------------------------------------------
// AllocFbAllocator
//------------------------------------------------------------------------------
void* AllocFbAllocator(int size)
{
return ALLOC_Alloc(BenchmarkFbAlloc, size);
}
//------------------------------------------------------------------------------
// DeallocFbAllocator
//------------------------------------------------------------------------------
void DeallocFbAllocator(void* ptr)
{
ALLOC_Free(BenchmarkFbAlloc, ptr);
}
//------------------------------------------------------------------------------
// AllocXAllocator
//------------------------------------------------------------------------------
void* AllocXAllocator(int size)
{
return BMALLOC_Alloc(size);
}
//------------------------------------------------------------------------------
// DeallocXAllocator
//------------------------------------------------------------------------------
void DeallocXAllocator(void* ptr)
{
return BMALLOC_Free(ptr);
}
//------------------------------------------------------------------------------
// Benchmark
//------------------------------------------------------------------------------
void Benchmark(const char* name, AllocFunc allocFunc, DeallocFunc deallocFunc)
{
#if WIN32
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds, TotalElapsedMicroseconds = { 0 };
LARGE_INTEGER Frequency;
SetProcessPriorityBoost(GetCurrentProcess(), true);
QueryPerformanceFrequency(&Frequency);
// Allocate MAX_BLOCKS blocks MAX_BLOCK_SIZE / 2 sized blocks
QueryPerformanceCounter(&StartingTime);
for (int i = 0; i<MAX_BLOCKS; i++)
memoryPtrs[i] = allocFunc(MAX_BLOCK_SIZE / 2);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " allocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
// Deallocate MAX_BLOCKS blocks (every other one)
QueryPerformanceCounter(&StartingTime);
for (int i = 0; i<MAX_BLOCKS; i += 2)
deallocFunc(memoryPtrs[i]);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " deallocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
// Allocate MAX_BLOCKS blocks MAX_BLOCK_SIZE sized blocks
QueryPerformanceCounter(&StartingTime);
for (int i = 0; i<MAX_BLOCKS; i++)
memoryPtrs2[i] = allocFunc(MAX_BLOCK_SIZE);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " allocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
// Deallocate MAX_BLOCKS blocks (every other one)
QueryPerformanceCounter(&StartingTime);
for (int i = 1; i<MAX_BLOCKS; i += 2)
deallocFunc(memoryPtrs[i]);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " deallocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
// Deallocate MAX_BLOCKS blocks
QueryPerformanceCounter(&StartingTime);
for (int i = MAX_BLOCKS - 1; i >= 0; i--)
deallocFunc(memoryPtrs2[i]);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << name << " deallocate time: " << ElapsedMicroseconds.QuadPart << std::endl;
TotalElapsedMicroseconds.QuadPart += ElapsedMicroseconds.QuadPart;
std::cout << name << " TOTAL TIME: " << TotalElapsedMicroseconds.QuadPart << "\n" << std::endl;
SetProcessPriorityBoost(GetCurrentProcess(), false);
#endif
}