forked from codeurzebs/fivem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestObjectPool.cpp
More file actions
194 lines (169 loc) · 5.86 KB
/
Copy pathTestObjectPool.cpp
File metadata and controls
194 lines (169 loc) · 5.86 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
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
#include <StdInc.h>
#include <random>
#include <catch_amalgamated.hpp>
#include <shared_mutex>
#include <citizen_util/object_pool.h>
#include "TestUtils.h"
namespace
{
constexpr uint16_t kObjectSize = 1024;
constexpr uint16_t kPoolElementOverhead = 8/*pool ptr*/ + 8/* atomic next ptr */;
constexpr uint16_t kObjectPoolElementSize = kObjectSize + kPoolElementOverhead;
class Object
{
std::array<uint8_t, kObjectSize> data{};
};
}
TEST_CASE("pool")
{
// each object needs a size of 1024, but the pool object_entry is 1040
// the initial size of the pool is 1041, because the pool needs to be larger then the object size
static fx::object_pool<Object, kObjectPoolElementSize + 1> objectPool;
Object* internalPtr;
{
// construct object from pool
Object* objectInstance = objectPool.allocate();
// destructs object instance, because out of scope
internalPtr = objectInstance;
objectPool.destruct(objectInstance);
}
{
// construct object from pool
Object* objectInstance = objectPool.allocate();
// the ptr is the same, because the object from earlier got recycled
REQUIRE(internalPtr == objectInstance);
// construct object from pool
Object* objectInstance2 = objectPool.allocate();
// this object is not coming from the same memory region as objectInstance
// because the pool memory region is full and so a new one is allocated for it
REQUIRE(reinterpret_cast<uint64_t>(objectInstance) - reinterpret_cast<uint64_t>(objectInstance2) != kObjectPoolElementSize);
objectPool.destruct(objectInstance);
objectPool.destruct(objectInstance2);
}
}
TEST_CASE("larger pool")
{
// each object needs a size of 1024, but the pool object_entry is 1040
// the initial size of the pool is 2081, because the test uses 2 objects as a sample
static fx::object_pool<Object, 2 * kObjectPoolElementSize + 1> objectPool;
Object* internalPtr;
{
// construct object from pool
Object* objectInstance = objectPool.allocate();
// destructs object instance, because out of scope
internalPtr = objectInstance;
objectPool.destruct(objectInstance);
}
{
// construct object from pool
Object* objectInstance = objectPool.allocate();
// the ptr has a offset of 1040 by the first allocated element, even when it was freed
// because the pool still has free memory for a second element
REQUIRE(reinterpret_cast<uint64_t>(objectInstance) - reinterpret_cast<uint64_t>(internalPtr) == kObjectPoolElementSize);
objectPool.destruct(objectInstance);
}
{
// construct object from pool
Object* objectInstance = objectPool.allocate();
// the ptr is the same, because the object from earlier got recycled and the pool has no more free size for new ptrs
REQUIRE(internalPtr == objectInstance);
objectPool.destruct(objectInstance);
}
}
TEST_CASE("dynamic pool allocations")
{
for (uint16_t i = 0; i < 1024; ++i)
{
fx::object_pool<Object, 2 * kObjectPoolElementSize + 1> objectPool;
Object* internalPtr;
{
// construct object from pool
Object* objectInstance = objectPool.allocate();
// destructs object instance, because out of scope
internalPtr = objectInstance;
objectPool.destruct(objectInstance);
}
{
// construct object from pool to require next allocation to reuse memory
// because the pool fits 2 elements in the initial size
Object* objectInstance = objectPool.allocate();
objectPool.destruct(objectInstance);
}
{
// construct object from pool
Object* objectInstance = objectPool.allocate();
// the ptr is the same, because the object from earlier got recycled and the pool has no more free size for new ptrs
REQUIRE(internalPtr == objectInstance);
objectPool.destruct(objectInstance);
}
}
}
namespace
{
class QueueObject
{
std::array<uint8_t, kObjectSize> data{};
public:
fx::detached_queue_key<QueueObject> free_queue_key;
};
}
TEST_CASE("detached_mpsc_queue")
{
fx::detached_mpsc_queue<QueueObject> queue;
uint32_t processorCount = std::thread::hardware_concurrency();
std::shared_mutex signalStartMutex;
signalStartMutex.lock();
// multi provider, single consumer queue gets multiple producer threads spawned
// and a single consumer
std::vector<std::thread> threads{};
// each producer thread will produce 1024 objects
std::atomic<uint32_t> remainingPops{1024 * processorCount};
// spawn producer threads
while (processorCount--)
{
threads.emplace_back([&signalStartMutex, &queue]()
{
std::shared_lock lck{signalStartMutex};
uint16_t amountOfRuns = 1024;
while (amountOfRuns--)
{
QueueObject* objectPtr = new QueueObject();
// pushes the object ptr to the queue using the ptr of the queue key
// this offset is later subtracted to get back to the original pointer
queue.push(&objectPtr->free_queue_key);
}
});
}
// consumer thread
threads.emplace_back([&signalStartMutex, &queue, &remainingPops]()
{
std::shared_lock lck{signalStartMutex};
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
while (remainingPops)
{
// the pop requires the member reference of the free queue key
// this is used to get the pointer to the QueueObject* from the queue key ptr with the offset
if (const QueueObject* objectPtrToPop = queue.pop(&QueueObject::free_queue_key))
{
delete objectPtrToPop;
--remainingPops;
}
// the timer cancels the while loop after 5 seconds in case the queue has a bug and no longer works
// to prevent freeze of the unit test
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>(end - begin).count() > 5)
{
break;
}
}
});
// unlock the mutex to let all threads start at the same time
signalStartMutex.unlock();
// we await the test case until all threads are finished with producing and consuming
for (auto& thread : threads)
{
thread.join();
}
// when all threads are done, the remaining pops should be 0
REQUIRE(remainingPops == 0);
}