This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
146 lines (110 loc) · 3.56 KB
/
Copy pathmain.cpp
File metadata and controls
146 lines (110 loc) · 3.56 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
144
145
146
#include <iostream>
#include <cassert>
#include <chrono>
// TODO: Implement and add this file
#include "DynamicAllocator.hpp"
#define DefaultTests
#define Benchmark
#define TestDynamicAllocator
#define TestNewDelete
int main(int argc, const char* argv[])
{
using IndexType = uint16_t;
using Pool = DynamicAllocator<8, IndexType>;
#ifdef DefaultTests
// A few test types to create in the pool
struct IAm2byte { int16_t x; };
struct IAm4byte { int32_t x; };
struct IAm8byte { int64_t x; };
struct IAmPolymorphic
{
virtual ~IAmPolymorphic() noexcept
{
std::cout << __func__ << std::endl;
}
};
// Pool with largest possible size w.r.t. index type
// Pool pool {(IndexType)-1 - 1};
// Capacity too large for the given index type,
// since max value is reserved as a null value
// Pool pool_ { (IndexType)-1 };
const int capacity = 4;
std::cout << "Creating pool with capacity " << capacity << "..." << std::endl;
Pool pool{ capacity };
pool.dump_pool();
auto nbr = pool.count_free(); //Mine
assert(nbr == 4); //Mine
std::cout << "Creating 2 objects..." << std::endl;
auto p1 = pool.create<IAm2byte>((int16_t)1);
pool.dump_pool(); //Mine
auto p2 = pool.create<IAm4byte>((int32_t)2);
pool.dump_pool();
assert(pool.count_free() == capacity - 2);
std::cout << "Creating 2 objects..." << std::endl;
auto p3 = pool.create<IAm8byte>((int64_t)3);
auto p4 = pool.create<IAmPolymorphic>();
pool.dump_pool();
assert(pool.count_free() == capacity - 4);
assert(p1->x == 1);
assert(p2->x == 2);
assert(p3->x == 3);
std::cout << "Destroying 2 objects..." << std::endl;
pool.destroy(p4);
pool.destroy(p3);
pool.dump_pool();
assert(pool.count_free() == capacity - 2);
std::cout << "Destroying 2 objects..." << std::endl;
pool.destroy(p1);
pool.destroy(p2);
pool.dump_pool();
assert(pool.count_free() == capacity);
/* DEBUG HEAD
*///End of incremental debug
#endif
#ifdef Benchmark
const IndexType N = (IndexType)-1 - 1;
int Nmult = 1000;
struct A { int a, b, c; };
A* ptrs[N];
#ifdef TestDynamicAllocator
{
// Start timings
std::cout << "Running DynamicAllocator tests..." << std::endl;
auto start = std::chrono::high_resolution_clock::now();
{
DynamicAllocator<sizeof(A), IndexType> pool{ N };
for (int n = 0; n < Nmult; n++)
{
for (int i = 0; i < N; i++)
ptrs[i] = pool.create<A>(0, 1, 2);
for (int i = 0; i < N; i++)
pool.destroy(ptrs[i]);
}
}
// Show timings
auto end = std::chrono::high_resolution_clock::now();
auto diff = end - start;
std::cout << std::chrono::duration <double, std::milli>(diff).count() << " ms" << std::endl;
}
#endif
#ifdef TestNewDelete
{
// Start timings
std::cout << "Running new/delete tests ..." << std::endl;
auto start = std::chrono::high_resolution_clock::now();
for (int n = 0; n < Nmult; n++)
{
for (int i = 0; i < N; i++)
ptrs[i] = new A{ 0, 1, 2 };
for (int i = 0; i < N; i++)
delete ptrs[i];
}
// Show timings
auto end = std::chrono::high_resolution_clock::now();
auto diff = end - start;
std::cout << std::chrono::duration <double, std::milli>(diff).count() << " ms" << std::endl;
}
#endif
#endif
return 0;
}