@@ -21,6 +21,11 @@ constexpr size_t maxVirtualMemoryBufferSize = 2147483648; // 2GB
21
21
class RandomNumberGenerator
22
22
{
23
23
public:
24
+ RandomNumberGenerator ()
25
+ : mt(rd())
26
+ {
27
+ }
28
+
24
29
inline uint32_t getRndAllocCnt () { return allocsPerFrameRange (mt); }
25
30
inline uint32_t getRndMaxAlign () { return (1u << maxAlignmentExpPerFrameRange (mt)); } // 4096 is max
26
31
inline uint32_t getRndBuffSize () { return buffSzRange (mt); }
@@ -31,7 +36,13 @@ class RandomNumberGenerator
31
36
return dist (mt);
32
37
}
33
38
39
+ inline std::mt19937& getMt ()
40
+ {
41
+ return mt;
42
+ }
43
+
34
44
private:
45
+ std::random_device rd;
35
46
std::mt19937 mt;
36
47
const std::uniform_int_distribution<uint32_t > allocsPerFrameRange = std::uniform_int_distribution<uint32_t >(minTestsCnt, maxTestsCnt);
37
48
const std::uniform_int_distribution<uint32_t > maxAlignmentExpPerFrameRange = std::uniform_int_distribution<uint32_t >(1 , maxAlignmentExp);
@@ -87,18 +98,18 @@ class AllocatorHandler
87
98
}
88
99
89
100
// 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 );
91
102
for (uint32_t i = 0u ; i < multiAllocCnt; i++)
92
103
{
93
104
outAddresses.clear ();
94
105
sizes.clear ();
95
106
alignments.clear ();
96
107
97
108
// 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);
100
111
101
- for (size_t i = 0u ; i < allocCntInMultiAlloc; ++i )
112
+ for (uint32_t j = 0u ; j < allocCntInMultiAlloc; j++ )
102
113
{
103
114
// randomly decide sizes (but always less than `address_allocator_traits::max_size`)
104
115
outAddresses.emplace_back (AlctrType::invalid_address);
@@ -110,18 +121,18 @@ class AllocatorHandler
110
121
}
111
122
else
112
123
{
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 ));
115
126
}
116
127
}
117
128
118
129
Traits::multi_alloc_addr (alctr, allocCntInMultiAlloc, outAddresses.data (), sizes.data (), alignments.data ());
119
130
120
131
// 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 ++)
122
133
{
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 ] });
125
136
}
126
137
127
138
// run random dealloc function
@@ -162,25 +173,32 @@ class AllocatorHandler
162
173
alignments.clear ();
163
174
164
175
// 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 ());
166
177
167
178
// TODO:
168
179
// shuffle results
169
180
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++)
171
187
{
172
188
// 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 ());
174
190
const uint32_t addressesToFreeCnt = rng.getRandomNumber (0u , addressesToFreeUpperBound);
175
191
176
- for (uint32_t i = 0u ; i < addressesToFreeCnt; i++)
192
+ auto it = results.end ();
193
+ for (uint32_t j = 0u ; j < addressesToFreeCnt; j++)
177
194
{
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 );
180
198
}
181
199
182
200
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 () );
184
202
}
185
203
}
186
204
@@ -194,7 +212,7 @@ class AllocatorHandler
194
212
randParams.alignOffset = rng.getRandomNumber (0u , randParams.maxAlign - 1u );
195
213
randParams.offset = rng.getRandomNumber (0u , randParams.addressSpaceSize - 1u );
196
214
197
- randParams.blockSz = rng.getRandomNumber (0u , (randParams.addressSpaceSize - randParams.offset ) / 2u );
215
+ randParams.blockSz = rng.getRandomNumber (1u , (randParams.addressSpaceSize - randParams.offset ) / 2u );
198
216
assert (randParams.blockSz > 0u );
199
217
200
218
return randParams;
@@ -217,19 +235,9 @@ void AllocatorHandler<core::LinearAddressAllocator<uint32_t>>::randFreeAllocated
217
235
results.clear ();
218
236
}
219
237
220
- template <>
221
- void AllocatorHandler<core::StackAddressAllocator<uint32_t >>::randFreeAllocatedAddresses(core::StackAddressAllocator<uint32_t >& alctr)
222
- {
223
- // TODO;
224
- }
225
-
226
238
int main ()
227
239
{
228
- // TODO:
229
- /* {
230
- AllocatorHandler<core::StackAddressAllocator<uint32_t>> stackAlctrHandler;
231
- stackAlctrHandler.executeAllocatorTest();
232
- }*/
240
+
233
241
234
242
{
235
243
AllocatorHandler<core::PoolAddressAllocator<uint32_t >> poolAlctrHandler;
@@ -240,6 +248,12 @@ int main()
240
248
AllocatorHandler<core::LinearAddressAllocator<uint32_t >> linearAlctrHandler;
241
249
linearAlctrHandler.executeAllocatorTest ();
242
250
}
251
+
252
+ // crashes..
253
+ {
254
+ AllocatorHandler<core::StackAddressAllocator<uint32_t >> stackAlctrHandler;
255
+ stackAlctrHandler.executeAllocatorTest ();
256
+ }
243
257
244
258
// crashes..
245
259
{
0 commit comments