Skip to content

Commit c16f5af

Browse files
committed
Slight refactor
1 parent 18b8980 commit c16f5af

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

examples_tests/10.AllocatorTest/main.cpp

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,23 @@ using namespace irr;
77
using namespace core;
88

99
#define ALLOCATOR_TEST
10-
//#define ALLOCATOR_TEST_OLD
1110
//#define ADDRESS_ALLOCATOR_TRAITS_TEST
1211
//#define ALLOC_PREF_TEST
1312

1413
#ifdef ALLOCATOR_TEST
1514

16-
struct AllocationCreationParameters
17-
{
18-
size_t multiAllocCnt; //! Specifies amount of adress to be allocated with certain choosen allocator
19-
size_t adressesToDeallocateCnt; //! Specifies amount of adress to be deallocated with certain choosen allocator. Must be less than all allocated and must pick number less than `traits::max_multi_free`, but we don't have `max_multi_free`
20-
};
21-
22-
constexpr size_t minAllocs = 10000u;
23-
constexpr size_t maxAllocs = 20000u;
24-
constexpr size_t maxSizeForSquaring = 1024u;
15+
constexpr size_t minTestsCnt = 10000u;
16+
constexpr size_t maxTestsCnt = 20000u;
2517
constexpr size_t maxAlignmentExp = 12u; // 4096
2618
constexpr size_t minVirtualMemoryBufferSize = 2048; // 2kB
2719
constexpr size_t maxVirtualMemoryBufferSize = 2147483648; // 2GB
28-
constexpr size_t maxOffset = 512;
29-
constexpr size_t maxAlignOffset = 64;
30-
constexpr size_t maxBlockSize = 600;
3120

3221
class RandomNumberGenerator
3322
{
3423
public:
3524
inline uint32_t getRndAllocCnt() { return allocsPerFrameRange(mt); }
36-
inline uint32_t getRndSize() { return sizePerFrameRange(mt); }
37-
inline uint32_t getRndMaxAlign() { return (1u << maxAlignmentExpPerFrameRange(mt)); } //128 is max
25+
inline uint32_t getRndMaxAlign() { return (1u << maxAlignmentExpPerFrameRange(mt)); } //4096 is max
3826
inline uint32_t getRndBuffSize() { return buffSzRange(mt); }
39-
inline uint32_t getRndOffset() { return offsetPerFrameRange(mt); }
40-
inline uint32_t getRndAlignOffset() { return alignOffsetPerFrameRange(mt); }
41-
inline uint32_t getRndBlockSize() { return allocsPerFrameRange(mt); }
4227

4328
inline uint32_t getRandomNumber(uint32_t rangeBegin, uint32_t rangeEnd)
4429
{
@@ -48,30 +33,21 @@ class RandomNumberGenerator
4833

4934
private:
5035
std::mt19937 mt;
51-
const std::uniform_int_distribution<uint32_t> allocsPerFrameRange = std::uniform_int_distribution<uint32_t>(minAllocs, maxAllocs);
52-
const std::uniform_int_distribution<uint32_t> sizePerFrameRange = std::uniform_int_distribution<uint32_t>(1u, maxSizeForSquaring);
36+
const std::uniform_int_distribution<uint32_t> allocsPerFrameRange = std::uniform_int_distribution<uint32_t>(minTestsCnt, maxTestsCnt);
5337
const std::uniform_int_distribution<uint32_t> maxAlignmentExpPerFrameRange = std::uniform_int_distribution<uint32_t>(1, maxAlignmentExp);
54-
5538
const std::uniform_int_distribution<uint32_t> buffSzRange = std::uniform_int_distribution<uint32_t>(minVirtualMemoryBufferSize, maxVirtualMemoryBufferSize);
56-
const std::uniform_int_distribution<uint32_t> offsetPerFrameRange = std::uniform_int_distribution<uint32_t>(0u, maxOffset);
57-
const std::uniform_int_distribution<uint32_t> alignOffsetPerFrameRange = std::uniform_int_distribution<uint32_t>(0u, maxAlignOffset);
58-
const std::uniform_int_distribution<uint32_t> blockSizePerFrameRange = std::uniform_int_distribution<uint32_t>(1, maxBlockSize);
5939
};
6040

6141
template<typename AlctrType>
6242
class AllocatorHandler
6343
{
6444
using Traits = core::address_allocator_traits<AlctrType>;
45+
6546
public:
66-
67-
AllocatorHandler(AllocationCreationParameters& allocationCreationParameters)
68-
: creationParameters(allocationCreationParameters)
69-
{
70-
}
71-
7247
void executeAllocatorTest()
7348
{
74-
for (size_t i = 0; i < creationParameters.multiAllocCnt; ++i)
49+
const uint32_t testsCnt = rng.getRndAllocCnt();
50+
for (size_t i = 0; i < testsCnt; ++i)
7551
executeForFrame();
7652
}
7753

@@ -194,7 +170,8 @@ class AllocatorHandler
194170
for (uint32_t i = 0u; i < multiFreeCnt; i++)
195171
{
196172
// randomly how many addresses we should deallocate (but obvs less than all allocated) NOTE: must pick number less than `traits::max_multi_free`
197-
const uint32_t addressesToFreeCnt = rng.getRandomNumber(0u, results.size()); //that should be restrained somehow, I think... so it it less likely to free all of allocated addresses
173+
const uint32_t addressesToFreeUpperBound = min(core::address_allocator_traits<AlctrType>::maxMultiOps, results.size());
174+
const uint32_t addressesToFreeCnt = rng.getRandomNumber(0u, addressesToFreeUpperBound);
198175

199176
for (uint32_t i = 0u; i < addressesToFreeCnt; i++)
200177
{
@@ -225,7 +202,6 @@ class AllocatorHandler
225202

226203
private:
227204
RandomNumberGenerator rng;
228-
AllocationCreationParameters creationParameters;
229205
core::vector<AllocationData> results;
230206

231207
//these hold inputs for `multi_alloc_addr` and `multi_free_addr`
@@ -249,29 +225,25 @@ void AllocatorHandler<core::StackAddressAllocator<uint32_t>>::randFreeAllocatedA
249225

250226
int main()
251227
{
252-
AllocationCreationParameters creationParams;
253-
creationParams.multiAllocCnt = 1000; // TODO
254-
creationParams.adressesToDeallocateCnt = 1000; // TODO
255-
256228
//TODO:
257229
/*{
258-
AllocatorHandler<core::StackAddressAllocator<uint32_t>> stackAlctrHandler(creationParams);
230+
AllocatorHandler<core::StackAddressAllocator<uint32_t>> stackAlctrHandler;
259231
stackAlctrHandler.executeAllocatorTest();
260232
}*/
261233

262234
{
263-
AllocatorHandler<core::PoolAddressAllocator<uint32_t>> poolAlctrHandler(creationParams);
235+
AllocatorHandler<core::PoolAddressAllocator<uint32_t>> poolAlctrHandler;
264236
poolAlctrHandler.executeAllocatorTest();
265237
}
266238

267239
{
268-
AllocatorHandler<core::LinearAddressAllocator<uint32_t>> linearAlctrHandler(creationParams);
240+
AllocatorHandler<core::LinearAddressAllocator<uint32_t>> linearAlctrHandler;
269241
linearAlctrHandler.executeAllocatorTest();
270242
}
271243

272244
//crashes..
273245
{
274-
AllocatorHandler<core::GeneralpurposeAddressAllocator<uint32_t>> generalpurposeAlctrHandler(creationParams);
246+
AllocatorHandler<core::GeneralpurposeAddressAllocator<uint32_t>> generalpurposeAlctrHandler;
275247
generalpurposeAlctrHandler.executeAllocatorTest();
276248
}
277249
}

0 commit comments

Comments
 (0)