-
Notifications
You must be signed in to change notification settings - Fork 0
/
xalloctest.cpp
168 lines (140 loc) · 5.69 KB
/
xalloctest.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
#include "stdafx.h"
#include "xallocator.h"
#include "Fault.h"
#include <new>
#include <iostream>
#include <stdio.h>
#include <string.h>
// On VisualStudio, to disable the debug heap for faster performance when using
// the debugger use this option:
// Debugging > Environment _NO_DEBUG_HEAP=1
class MyClass
{
XALLOCATOR
// remaining class definition
};
class MyClassStatic
{
public:
MyClassStatic() { memory = xmalloc(100); }
~MyClassStatic() { xfree(memory); }
private:
void* memory;
};
static MyClassStatic myClassStatic;
static void out_of_memory()
{
// new-handler function called by Allocator when pool is out of memory
ASSERT();
}
static const int MAX_BLOCK_SIZE = 4000;
static const int MAX_ALLOCATIONS = 10000;
static void* memoryPtrs[MAX_ALLOCATIONS];
static void* memoryPtrs2[MAX_ALLOCATIONS];
typedef void* (*AllocFunc)(size_t size);
typedef void (*DeallocFunc)(void* ptr);
void Benchmark(const char* name, AllocFunc allocFunc, DeallocFunc deallocFunc);
//------------------------------------------------------------------------------
// _tmain
//------------------------------------------------------------------------------
int xallocator_test(void)
{
srand(1);
std::set_new_handler(out_of_memory);
// printf("Hier 1\n");
xalloc_stats();
// printf("Hier 2\n");
// If AUTOMATIC_XALLOCATOR_INIT_DESTROY defined then XallocInitDestroy() will
// call xalloc_init() automatically before main().
//xalloc_init();
// Allocate MyClass using fixed block allocator
MyClass* myClass = new MyClass();
delete myClass;
void* memory1 = xmalloc(100);
xfree(memory1);
char* memory2 = (char*)xmalloc(24);
strcpy(memory2, "TEST STRING");
memory2 = (char*)xrealloc(memory2, 124);
xfree(memory2);
// Benchmark will cause out_of_memory to be called if STATIC_POOLS defined
Benchmark("malloc/free (Run 1)", malloc, free);
Benchmark("malloc/free (Run 2)", malloc, free);
Benchmark("malloc/free (Run 3)", malloc, free);
Benchmark("xmalloc/xfree (Run 1)", xmalloc, xfree);
Benchmark("xmalloc/xfree (Run 2)", xmalloc, xfree);
Benchmark("xmalloc/xfree (Run 3)", xmalloc, xfree);
// printf("Hier 3\n");
xalloc_stats();
// printf("Hier 4\n");
// If AUTOMATIC_XALLOCATOR_INIT_DESTROY is defined, ~XallocDestroy() will call
// xalloc_destroy() automatically. Never call xalloc_destroy() manually except if
// using xallocator in a C-only application.
//xalloc_destroy();
return 0;
}
//------------------------------------------------------------------------------
// Benchmark
//------------------------------------------------------------------------------
#include <winsock2.h>
#include <psapi.h>
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_ALLOCATIONS blocks MAX_BLOCK_SIZE / 2 sized blocks
QueryPerformanceCounter(&StartingTime);
for (int i=0; i<MAX_ALLOCATIONS; 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_ALLOCATIONS blocks (every other one)
QueryPerformanceCounter(&StartingTime);
for (int i=0; i<MAX_ALLOCATIONS; 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_ALLOCATIONS blocks MAX_BLOCK_SIZE sized blocks
QueryPerformanceCounter(&StartingTime);
for (int i=0; i<MAX_ALLOCATIONS; 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_ALLOCATIONS blocks (every other one)
QueryPerformanceCounter(&StartingTime);
for (int i=1; i<MAX_ALLOCATIONS; 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_ALLOCATIONS blocks
QueryPerformanceCounter(&StartingTime);
for (int i=MAX_ALLOCATIONS-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 << std::endl;
SetProcessPriorityBoost(GetCurrentProcess(), false);
//#endif
}