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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
// This is a GPU-backend specific test
#if SK_SUPPORT_GPU
#include "GrMemoryPool.h"
#include "SkRandom.h"
#include "SkTArray.h"
#include "SkTDArray.h"
#include "SkTemplates.h"
// A is the top of an inheritance tree of classes that overload op new and
// and delete to use a GrMemoryPool. The objects have values of different types
// that can be set and checked.
class A {
public:
A() {}
virtual void setValues(int v) {
fChar = static_cast<char>(v);
}
virtual bool checkValues(int v) {
return fChar == static_cast<char>(v);
}
virtual ~A() {}
void* operator new(size_t size) {
if (!gPool.get()) {
return ::operator new(size);
} else {
return gPool->allocate(size);
}
}
void operator delete(void* p) {
if (!gPool.get()) {
::operator delete(p);
} else {
return gPool->release(p);
}
}
static A* Create(SkRandom* r);
static void SetAllocator(size_t preallocSize, size_t minAllocSize) {
GrMemoryPool* pool = new GrMemoryPool(preallocSize, minAllocSize);
gPool.reset(pool);
}
static void ResetAllocator() {
gPool.reset(nullptr);
}
private:
static std::unique_ptr<GrMemoryPool> gPool;
char fChar;
};
std::unique_ptr<GrMemoryPool> A::gPool;
class B : public A {
public:
B() {}
virtual void setValues(int v) {
fDouble = static_cast<double>(v);
this->INHERITED::setValues(v);
}
virtual bool checkValues(int v) {
return fDouble == static_cast<double>(v) &&
this->INHERITED::checkValues(v);
}
virtual ~B() {}
private:
double fDouble;
typedef A INHERITED;
};
class C : public A {
public:
C() {}
virtual void setValues(int v) {
fInt64 = static_cast<int64_t>(v);
this->INHERITED::setValues(v);
}
virtual bool checkValues(int v) {
return fInt64 == static_cast<int64_t>(v) &&
this->INHERITED::checkValues(v);
}
virtual ~C() {}
private:
int64_t fInt64;
typedef A INHERITED;
};
// D derives from C and owns a dynamically created B
class D : public C {
public:
D() {
fB = new B();
}
virtual void setValues(int v) {
fVoidStar = reinterpret_cast<void*>(static_cast<intptr_t>(v));
this->INHERITED::setValues(v);
fB->setValues(v);
}
virtual bool checkValues(int v) {
return fVoidStar == reinterpret_cast<void*>(static_cast<intptr_t>(v)) &&
fB->checkValues(v) &&
this->INHERITED::checkValues(v);
}
virtual ~D() {
delete fB;
}
private:
void* fVoidStar;
B* fB;
typedef C INHERITED;
};
class E : public A {
public:
E() {}
virtual void setValues(int v) {
for (size_t i = 0; i < SK_ARRAY_COUNT(fIntArray); ++i) {
fIntArray[i] = v;
}
this->INHERITED::setValues(v);
}
virtual bool checkValues(int v) {
bool ok = true;
for (size_t i = 0; ok && i < SK_ARRAY_COUNT(fIntArray); ++i) {
if (fIntArray[i] != v) {
ok = false;
}
}
return ok && this->INHERITED::checkValues(v);
}
virtual ~E() {}
private:
int fIntArray[20];
typedef A INHERITED;
};
A* A::Create(SkRandom* r) {
switch (r->nextRangeU(0, 4)) {
case 0:
return new A;
case 1:
return new B;
case 2:
return new C;
case 3:
return new D;
case 4:
return new E;
default:
// suppress warning
return nullptr;
}
}
struct Rec {
A* fInstance;
int fValue;
};
DEF_TEST(GrMemoryPool, reporter) {
// prealloc and min alloc sizes for the pool
static const size_t gSizes[][2] = {
{0, 0},
{10 * sizeof(A), 20 * sizeof(A)},
{100 * sizeof(A), 100 * sizeof(A)},
{500 * sizeof(A), 500 * sizeof(A)},
{10000 * sizeof(A), 0},
{1, 100 * sizeof(A)},
};
// different percentages of creation vs deletion
static const float gCreateFraction[] = {1.f, .95f, 0.75f, .5f};
// number of create/destroys per test
static const int kNumIters = 20000;
// check that all the values stored in A objects are correct after this
// number of iterations
static const int kCheckPeriod = 500;
SkRandom r;
for (size_t s = 0; s < SK_ARRAY_COUNT(gSizes); ++s) {
A::SetAllocator(gSizes[s][0], gSizes[s][1]);
for (size_t c = 0; c < SK_ARRAY_COUNT(gCreateFraction); ++c) {
SkTDArray<Rec> instanceRecs;
for (int i = 0; i < kNumIters; ++i) {
float createOrDestroy = r.nextUScalar1();
if (createOrDestroy < gCreateFraction[c] ||
0 == instanceRecs.count()) {
Rec* rec = instanceRecs.append();
rec->fInstance = A::Create(&r);
rec->fValue = static_cast<int>(r.nextU());
rec->fInstance->setValues(rec->fValue);
} else {
int d = r.nextRangeU(0, instanceRecs.count() - 1);
Rec& rec = instanceRecs[d];
REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
delete rec.fInstance;
instanceRecs.removeShuffle(d);
}
if (0 == i % kCheckPeriod) {
for (int r = 0; r < instanceRecs.count(); ++r) {
Rec& rec = instanceRecs[r];
REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
}
}
}
for (int i = 0; i < instanceRecs.count(); ++i) {
Rec& rec = instanceRecs[i];
REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
delete rec.fInstance;
}
}
}
}
// GrMemoryPool requires that it's empty at the point of destruction. This helps
// achieving that by releasing all added memory in the destructor.
class AutoPoolReleaser {
public:
AutoPoolReleaser(GrMemoryPool& pool): fPool(pool) {
}
~AutoPoolReleaser() {
for (void* ptr: fAllocated) {
fPool.release(ptr);
}
}
void add(void* ptr) {
fAllocated.push_back(ptr);
}
private:
GrMemoryPool& fPool;
SkTArray<void*> fAllocated;
};
DEF_TEST(GrMemoryPoolAPI, reporter) {
constexpr size_t kSmallestMinAllocSize = GrMemoryPool::kSmallestMinAllocSize;
// Allocates memory until pool adds a new block (pool.size() changes).
auto allocateMemory = [](GrMemoryPool& pool, AutoPoolReleaser& r) {
size_t origPoolSize = pool.size();
while (pool.size() == origPoolSize) {
r.add(pool.allocate(31));
}
};
// Effective prealloc space capacity is >= kSmallestMinAllocSize.
{
GrMemoryPool pool(0, 0);
REPORTER_ASSERT(reporter, pool.preallocSize() == kSmallestMinAllocSize);
}
// Effective prealloc space capacity is >= minAllocSize.
{
constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 2;
GrMemoryPool pool(kSmallestMinAllocSize, kMinAllocSize);
REPORTER_ASSERT(reporter, pool.preallocSize() == kMinAllocSize);
}
// Effective block size capacity >= kSmallestMinAllocSize.
{
GrMemoryPool pool(kSmallestMinAllocSize, kSmallestMinAllocSize / 2);
AutoPoolReleaser r(pool);
allocateMemory(pool, r);
REPORTER_ASSERT(reporter, pool.size() == kSmallestMinAllocSize);
}
// Pool allocates exactly preallocSize on creation.
{
constexpr size_t kPreallocSize = kSmallestMinAllocSize * 5;
GrMemoryPool pool(kPreallocSize, 0);
REPORTER_ASSERT(reporter, pool.preallocSize() == kPreallocSize);
}
// Pool allocates exactly minAllocSize when it expands.
{
constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 7;
GrMemoryPool pool(0, kMinAllocSize);
AutoPoolReleaser r(pool);
allocateMemory(pool, r);
REPORTER_ASSERT(reporter, pool.size() == kMinAllocSize);
allocateMemory(pool, r);
REPORTER_ASSERT(reporter, pool.size() == 2 * kMinAllocSize);
}
// When asked to allocate amount > minAllocSize, pool allocates larger block
// to accommodate all internal structures.
{
constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 2;
GrMemoryPool pool(kSmallestMinAllocSize, kMinAllocSize);
AutoPoolReleaser r(pool);
REPORTER_ASSERT(reporter, pool.size() == 0);
constexpr size_t hugeSize = 10 * kMinAllocSize;
r.add(pool.allocate(hugeSize));
REPORTER_ASSERT(reporter, pool.size() > hugeSize);
// Block size allocated to accommodate huge request doesn't include any extra
// space, so next allocation request allocates a new block.
size_t hugeBlockSize = pool.size();
r.add(pool.allocate(0));
REPORTER_ASSERT(reporter, pool.size() == hugeBlockSize + kMinAllocSize);
}
}
DEF_TEST(GrObjectMemoryPoolAPI, reporter) {
struct Data {
int value[5];
};
using DataObjectPool = GrObjectMemoryPool<Data>;
constexpr size_t kSmallestMinAllocCount = DataObjectPool::kSmallestMinAllocCount;
// Allocates objects until pool adds a new block (pool.size() changes).
// Returns number of objects that fit into the current block (i.e. before pool.size()
// changed; newly allocated block always ends up with one object allocated from it).
auto allocateObjects = [](DataObjectPool& pool, AutoPoolReleaser& r) -> size_t {
size_t count = 0;
size_t origPoolSize = pool.size();
while (pool.size() == origPoolSize) {
r.add(pool.allocate());
count++;
}
return count - 1;
};
// Effective prealloc space capacity is >= kSmallestMinAllocCount.
{
DataObjectPool pool(kSmallestMinAllocCount / 3, 0);
AutoPoolReleaser r(pool);
size_t preallocCount = allocateObjects(pool, r);
REPORTER_ASSERT(reporter, preallocCount == kSmallestMinAllocCount);
}
// Effective prealloc space capacity is >= minAllocCount.
{
DataObjectPool pool(kSmallestMinAllocCount, 2 * kSmallestMinAllocCount);
AutoPoolReleaser r(pool);
size_t preallocCount = allocateObjects(pool, r);
REPORTER_ASSERT(reporter, preallocCount == 2 * kSmallestMinAllocCount);
}
// Effective block capacity is >= kSmallestMinAllocCount.
{
DataObjectPool pool(kSmallestMinAllocCount, kSmallestMinAllocCount / 2);
AutoPoolReleaser r(pool);
// Fill prealloc space
allocateObjects(pool, r);
size_t minAllocCount = 1 + allocateObjects(pool, r);
REPORTER_ASSERT(reporter, minAllocCount == kSmallestMinAllocCount);
}
// Pool allocates space for exactly preallocCount objects on creation.
{
constexpr size_t kPreallocCount = kSmallestMinAllocCount * 7 / 3;
DataObjectPool pool(kPreallocCount, 0);
AutoPoolReleaser r(pool);
size_t preallocCount = allocateObjects(pool, r);
REPORTER_ASSERT(reporter, preallocCount == kPreallocCount);
}
// Pool allocates space for minAllocCount objects when it adds a new block.
{
constexpr size_t kMinAllocCount = kSmallestMinAllocCount * 11 / 3;
DataObjectPool pool(0, kMinAllocCount);
AutoPoolReleaser r(pool);
// Fill prealloc space
allocateObjects(pool, r);
size_t firstBlockCount = 1 + allocateObjects(pool, r);
REPORTER_ASSERT(reporter, firstBlockCount == kMinAllocCount);
size_t secondBlockCount = 1 + allocateObjects(pool, r);
REPORTER_ASSERT(reporter, secondBlockCount == kMinAllocCount);
}
}
#endif
|