Skip to content

Commit 4ee45c5

Browse files
committed
Update
1 parent c16f5af commit 4ee45c5

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

examples_tests/10.AllocatorTest/main.cpp

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ constexpr size_t maxVirtualMemoryBufferSize = 2147483648; // 2GB
2121
class RandomNumberGenerator
2222
{
2323
public:
24+
RandomNumberGenerator()
25+
: mt(rd())
26+
{
27+
}
28+
2429
inline uint32_t getRndAllocCnt() { return allocsPerFrameRange(mt); }
2530
inline uint32_t getRndMaxAlign() { return (1u << maxAlignmentExpPerFrameRange(mt)); } //4096 is max
2631
inline uint32_t getRndBuffSize() { return buffSzRange(mt); }
@@ -31,7 +36,13 @@ class RandomNumberGenerator
3136
return dist(mt);
3237
}
3338

39+
inline std::mt19937& getMt()
40+
{
41+
return mt;
42+
}
43+
3444
private:
45+
std::random_device rd;
3546
std::mt19937 mt;
3647
const std::uniform_int_distribution<uint32_t> allocsPerFrameRange = std::uniform_int_distribution<uint32_t>(minTestsCnt, maxTestsCnt);
3748
const std::uniform_int_distribution<uint32_t> maxAlignmentExpPerFrameRange = std::uniform_int_distribution<uint32_t>(1, maxAlignmentExp);
@@ -87,18 +98,18 @@ class AllocatorHandler
8798
}
8899

89100
// randomly decide how many `multi_allocs` to do
90-
const uint32_t multiAllocCnt = rng.getRandomNumber(1u, 5u); //TODO: will change it later
101+
const uint32_t multiAllocCnt = rng.getRandomNumber(1u, 500u);
91102
for (uint32_t i = 0u; i < multiAllocCnt; i++)
92103
{
93104
outAddresses.clear();
94105
sizes.clear();
95106
alignments.clear();
96107

97108
// randomly decide how many allocs in a `multi_alloc` NOTE: must pick number less than `traits::max_multi_alloc`
98-
constexpr uint32_t upperBound = core::address_allocator_traits<AlctrType>::maxMultiOps;
99-
const uint32_t allocCntInMultiAlloc = rng.getRandomNumber(1u, upperBound);
109+
constexpr uint32_t upperBound = Traits::maxMultiOps;
110+
uint32_t allocCntInMultiAlloc = rng.getRandomNumber(1u, upperBound);
100111

101-
for (size_t i = 0u; i < allocCntInMultiAlloc; ++i)
112+
for (uint32_t j = 0u; j < allocCntInMultiAlloc; j++)
102113
{
103114
// randomly decide sizes (but always less than `address_allocator_traits::max_size`)
104115
outAddresses.emplace_back(AlctrType::invalid_address);
@@ -110,18 +121,18 @@ class AllocatorHandler
110121
}
111122
else
112123
{
113-
sizes.emplace_back(rng.getRandomNumber(1u, Traits::max_size(alctr)));
114-
alignments.emplace_back(rng.getRndMaxAlign());
124+
sizes.emplace_back(rng.getRandomNumber(1u, std::max(Traits::max_size(alctr), 1u)));
125+
alignments.emplace_back(rng.getRandomNumber(1u, randAllocParams.maxAlign));
115126
}
116127
}
117128

118129
Traits::multi_alloc_addr(alctr, allocCntInMultiAlloc, outAddresses.data(), sizes.data(), alignments.data());
119130

120131
// record all successful alloc addresses to the `core::vector`
121-
for (uint32_t i = 0u; i < outAddresses.size(); i++)
132+
for (uint32_t j = 0u; j < outAddresses.size(); j++)
122133
{
123-
if (outAddresses[i] != AlctrType::invalid_address)
124-
results.push_back({ outAddresses[i], sizes[i], alignments[i] });
134+
if (outAddresses[j] != AlctrType::invalid_address)
135+
results.push_back({ outAddresses[j], sizes[j], alignments[j] });
125136
}
126137

127138
// run random dealloc function
@@ -162,25 +173,32 @@ class AllocatorHandler
162173
alignments.clear();
163174

164175
// randomly decide how many calls to `multi_free`
165-
const uint32_t multiFreeCnt = rng.getRandomNumber(1u, 10000u); //TODO
176+
const uint32_t multiFreeCnt = rng.getRandomNumber(1u, results.size());
166177

167178
//TODO:
168179
//shuffle results
169180

170-
for (uint32_t i = 0u; i < multiFreeCnt; i++)
181+
if (std::is_same<AlctrType, core::GeneralpurposeAddressAllocator<uint32_t>>::value)
182+
{
183+
std::shuffle(results.begin(), results.end(), rng.getMt());
184+
}
185+
186+
for (uint32_t i = 0u; (i < multiFreeCnt) && results.size(); i++)
171187
{
172188
// randomly how many addresses we should deallocate (but obvs less than all allocated) NOTE: must pick number less than `traits::max_multi_free`
173-
const uint32_t addressesToFreeUpperBound = min(core::address_allocator_traits<AlctrType>::maxMultiOps, results.size());
189+
const uint32_t addressesToFreeUpperBound = min(Traits::maxMultiOps, results.size());
174190
const uint32_t addressesToFreeCnt = rng.getRandomNumber(0u, addressesToFreeUpperBound);
175191

176-
for (uint32_t i = 0u; i < addressesToFreeCnt; i++)
192+
auto it = results.end();
193+
for (uint32_t j = 0u; j < addressesToFreeCnt; j++)
177194
{
178-
outAddresses.push_back(results[i].outAddr);
179-
sizes.push_back(results[i].size);
195+
it--;
196+
outAddresses.push_back(it->outAddr);
197+
sizes.push_back(it->size);
180198
}
181199

182200
Traits::multi_free_addr(alctr, addressesToFreeCnt, outAddresses.data(), sizes.data());
183-
results.erase(results.begin(), results.begin() + addressesToFreeCnt);
201+
results.erase(results.end() - addressesToFreeCnt, results.end());
184202
}
185203
}
186204

@@ -194,7 +212,7 @@ class AllocatorHandler
194212
randParams.alignOffset = rng.getRandomNumber(0u, randParams.maxAlign - 1u);
195213
randParams.offset = rng.getRandomNumber(0u, randParams.addressSpaceSize - 1u);
196214

197-
randParams.blockSz = rng.getRandomNumber(0u, (randParams.addressSpaceSize - randParams.offset) / 2u);
215+
randParams.blockSz = rng.getRandomNumber(1u, (randParams.addressSpaceSize - randParams.offset) / 2u);
198216
assert(randParams.blockSz > 0u);
199217

200218
return randParams;
@@ -217,19 +235,9 @@ void AllocatorHandler<core::LinearAddressAllocator<uint32_t>>::randFreeAllocated
217235
results.clear();
218236
}
219237

220-
template<>
221-
void AllocatorHandler<core::StackAddressAllocator<uint32_t>>::randFreeAllocatedAddresses(core::StackAddressAllocator<uint32_t>& alctr)
222-
{
223-
//TODO;
224-
}
225-
226238
int main()
227239
{
228-
//TODO:
229-
/*{
230-
AllocatorHandler<core::StackAddressAllocator<uint32_t>> stackAlctrHandler;
231-
stackAlctrHandler.executeAllocatorTest();
232-
}*/
240+
233241

234242
{
235243
AllocatorHandler<core::PoolAddressAllocator<uint32_t>> poolAlctrHandler;
@@ -240,6 +248,12 @@ int main()
240248
AllocatorHandler<core::LinearAddressAllocator<uint32_t>> linearAlctrHandler;
241249
linearAlctrHandler.executeAllocatorTest();
242250
}
251+
252+
//crashes..
253+
{
254+
AllocatorHandler<core::StackAddressAllocator<uint32_t>> stackAlctrHandler;
255+
stackAlctrHandler.executeAllocatorTest();
256+
}
243257

244258
//crashes..
245259
{

0 commit comments

Comments
 (0)