2012-06-19 15:40:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "include/private/SkTDArray.h"
|
|
|
|
#include "include/private/SkTemplates.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/gpu/GrMemoryPool.h"
|
|
|
|
#include "tests/Test.h"
|
2012-06-19 15:40:27 +00:00
|
|
|
|
|
|
|
// 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:
|
2016-09-27 13:34:10 +00:00
|
|
|
A() {}
|
2012-06-19 15:40:27 +00:00
|
|
|
virtual void setValues(int v) {
|
2019-02-04 15:01:53 +00:00
|
|
|
fChar = static_cast<char>(v & 0xFF);
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
|
|
|
virtual bool checkValues(int v) {
|
2019-02-04 15:01:53 +00:00
|
|
|
return fChar == static_cast<char>(v & 0xFF);
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
2016-09-27 13:34:10 +00:00
|
|
|
virtual ~A() {}
|
2012-06-19 15:40:27 +00:00
|
|
|
|
|
|
|
void* operator new(size_t size) {
|
2019-12-12 15:58:47 +00:00
|
|
|
if (!gPool) {
|
2012-06-19 15:40:27 +00:00
|
|
|
return ::operator new(size);
|
|
|
|
} else {
|
|
|
|
return gPool->allocate(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* p) {
|
2019-12-12 15:58:47 +00:00
|
|
|
if (!gPool) {
|
2012-06-19 15:40:27 +00:00
|
|
|
::operator delete(p);
|
|
|
|
} else {
|
|
|
|
return gPool->release(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
static A* Create(SkRandom* r);
|
2012-06-19 15:40:27 +00:00
|
|
|
|
|
|
|
static void SetAllocator(size_t preallocSize, size_t minAllocSize) {
|
2019-12-12 15:58:47 +00:00
|
|
|
gPool = GrMemoryPool::Make(preallocSize, minAllocSize);
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 15:58:47 +00:00
|
|
|
static void ResetAllocator() { gPool.reset(); }
|
2012-06-19 15:40:27 +00:00
|
|
|
|
2020-03-17 14:14:48 +00:00
|
|
|
static void ValidatePool() {
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
gPool->validate();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
private:
|
2016-11-03 18:40:50 +00:00
|
|
|
static std::unique_ptr<GrMemoryPool> gPool;
|
2012-06-19 15:40:27 +00:00
|
|
|
char fChar;
|
|
|
|
};
|
2013-12-05 12:08:12 +00:00
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<GrMemoryPool> A::gPool;
|
2012-06-19 15:40:27 +00:00
|
|
|
|
|
|
|
class B : public A {
|
|
|
|
public:
|
2016-09-27 13:34:10 +00:00
|
|
|
B() {}
|
2020-07-21 21:03:56 +00:00
|
|
|
void setValues(int v) override {
|
2012-06-19 15:40:27 +00:00
|
|
|
fDouble = static_cast<double>(v);
|
|
|
|
this->INHERITED::setValues(v);
|
|
|
|
}
|
2020-07-21 21:03:56 +00:00
|
|
|
bool checkValues(int v) override {
|
2012-06-19 15:40:27 +00:00
|
|
|
return fDouble == static_cast<double>(v) &&
|
|
|
|
this->INHERITED::checkValues(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
double fDouble;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = A;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class C : public A {
|
|
|
|
public:
|
2016-09-27 13:34:10 +00:00
|
|
|
C() {}
|
2020-07-21 21:03:56 +00:00
|
|
|
void setValues(int v) override {
|
2012-06-19 15:40:27 +00:00
|
|
|
fInt64 = static_cast<int64_t>(v);
|
|
|
|
this->INHERITED::setValues(v);
|
|
|
|
}
|
2020-07-21 21:03:56 +00:00
|
|
|
bool checkValues(int v) override {
|
2012-06-19 15:40:27 +00:00
|
|
|
return fInt64 == static_cast<int64_t>(v) &&
|
|
|
|
this->INHERITED::checkValues(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int64_t fInt64;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = A;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// D derives from C and owns a dynamically created B
|
|
|
|
class D : public C {
|
|
|
|
public:
|
|
|
|
D() {
|
|
|
|
fB = new B();
|
|
|
|
}
|
2020-07-21 21:03:56 +00:00
|
|
|
void setValues(int v) override {
|
2015-08-07 00:33:16 +00:00
|
|
|
fVoidStar = reinterpret_cast<void*>(static_cast<intptr_t>(v));
|
2012-06-19 15:40:27 +00:00
|
|
|
this->INHERITED::setValues(v);
|
|
|
|
fB->setValues(v);
|
|
|
|
}
|
2020-07-21 21:03:56 +00:00
|
|
|
bool checkValues(int v) override {
|
2015-08-07 00:33:16 +00:00
|
|
|
return fVoidStar == reinterpret_cast<void*>(static_cast<intptr_t>(v)) &&
|
2012-06-19 15:40:27 +00:00
|
|
|
fB->checkValues(v) &&
|
|
|
|
this->INHERITED::checkValues(v);
|
|
|
|
}
|
2020-07-21 21:03:56 +00:00
|
|
|
~D() override {
|
2012-06-19 15:40:27 +00:00
|
|
|
delete fB;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void* fVoidStar;
|
|
|
|
B* fB;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = C;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class E : public A {
|
|
|
|
public:
|
|
|
|
E() {}
|
2020-07-21 21:03:56 +00:00
|
|
|
void setValues(int v) override {
|
2012-06-19 15:40:27 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fIntArray); ++i) {
|
|
|
|
fIntArray[i] = v;
|
|
|
|
}
|
|
|
|
this->INHERITED::setValues(v);
|
|
|
|
}
|
2020-07-21 21:03:56 +00:00
|
|
|
bool checkValues(int v) override {
|
2012-06-19 15:40:27 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
int fIntArray[20];
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = A;
|
2012-06-19 15:40:27 +00:00
|
|
|
};
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
A* A::Create(SkRandom* r) {
|
2012-06-19 15:40:27 +00:00
|
|
|
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
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2012-06-19 15:40:27 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-12 17:25:17 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
struct Rec {
|
|
|
|
A* fInstance;
|
|
|
|
int fValue;
|
|
|
|
};
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(GrMemoryPool, reporter) {
|
2012-06-19 15:40:27 +00:00
|
|
|
// 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)},
|
|
|
|
};
|
2020-03-17 14:14:48 +00:00
|
|
|
|
2012-06-19 15:40:27 +00:00
|
|
|
// 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;
|
|
|
|
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom r;
|
2012-06-19 15:40:27 +00:00
|
|
|
for (size_t s = 0; s < SK_ARRAY_COUNT(gSizes); ++s) {
|
|
|
|
A::SetAllocator(gSizes[s][0], gSizes[s][1]);
|
2020-03-17 14:14:48 +00:00
|
|
|
A::ValidatePool();
|
2012-06-19 15:40:27 +00:00
|
|
|
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) {
|
2020-03-17 14:14:48 +00:00
|
|
|
A::ValidatePool();
|
2021-08-12 14:48:09 +00:00
|
|
|
for (Rec& rec : instanceRecs) {
|
2012-06-19 15:40:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-12 14:48:09 +00:00
|
|
|
for (Rec& rec : instanceRecs) {
|
2012-06-19 15:40:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
|
|
|
|
delete rec.fInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:50:35 +00:00
|
|
|
// 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) {
|
2019-12-12 15:58:47 +00:00
|
|
|
constexpr size_t kSmallestMinAllocSize = GrMemoryPool::kMinAllocationSize;
|
2016-11-29 14:50:35 +00:00
|
|
|
|
2019-12-12 15:58:47 +00:00
|
|
|
// Allocates memory until pool adds a new block (pool->size() changes).
|
2016-11-29 14:50:35 +00:00
|
|
|
auto allocateMemory = [](GrMemoryPool& pool, AutoPoolReleaser& r) {
|
|
|
|
size_t origPoolSize = pool.size();
|
|
|
|
while (pool.size() == origPoolSize) {
|
|
|
|
r.add(pool.allocate(31));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-12 15:58:47 +00:00
|
|
|
// Effective prealloc space capacity is >= kMinAllocationSize.
|
2016-11-29 14:50:35 +00:00
|
|
|
{
|
2019-12-12 15:58:47 +00:00
|
|
|
auto pool = GrMemoryPool::Make(0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, pool->preallocSize() == kSmallestMinAllocSize);
|
2016-11-29 14:50:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 15:58:47 +00:00
|
|
|
// Effective block size capacity >= kMinAllocationSize.
|
2016-11-29 14:50:35 +00:00
|
|
|
{
|
2019-12-12 15:58:47 +00:00
|
|
|
auto pool = GrMemoryPool::Make(kSmallestMinAllocSize, kSmallestMinAllocSize / 2);
|
|
|
|
AutoPoolReleaser r(*pool);
|
2016-11-29 14:50:35 +00:00
|
|
|
|
2019-12-12 15:58:47 +00:00
|
|
|
allocateMemory(*pool, r);
|
|
|
|
REPORTER_ASSERT(reporter, pool->size() == kSmallestMinAllocSize);
|
2016-11-29 14:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pool allocates exactly preallocSize on creation.
|
|
|
|
{
|
|
|
|
constexpr size_t kPreallocSize = kSmallestMinAllocSize * 5;
|
2019-12-12 15:58:47 +00:00
|
|
|
auto pool = GrMemoryPool::Make(kPreallocSize, 0);
|
|
|
|
REPORTER_ASSERT(reporter, pool->preallocSize() == kPreallocSize);
|
2016-11-29 14:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pool allocates exactly minAllocSize when it expands.
|
|
|
|
{
|
|
|
|
constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 7;
|
2019-12-12 15:58:47 +00:00
|
|
|
auto pool = GrMemoryPool::Make(0, kMinAllocSize);
|
|
|
|
AutoPoolReleaser r(*pool);
|
2020-03-17 14:14:48 +00:00
|
|
|
REPORTER_ASSERT(reporter, pool->size() == 0);
|
2016-11-29 14:50:35 +00:00
|
|
|
|
2019-12-12 15:58:47 +00:00
|
|
|
allocateMemory(*pool, r);
|
|
|
|
REPORTER_ASSERT(reporter, pool->size() == kMinAllocSize);
|
2016-11-29 14:50:35 +00:00
|
|
|
|
2019-12-12 15:58:47 +00:00
|
|
|
allocateMemory(*pool, r);
|
|
|
|
REPORTER_ASSERT(reporter, pool->size() == 2 * kMinAllocSize);
|
2016-11-29 14:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When asked to allocate amount > minAllocSize, pool allocates larger block
|
|
|
|
// to accommodate all internal structures.
|
|
|
|
{
|
|
|
|
constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 2;
|
2019-12-12 15:58:47 +00:00
|
|
|
auto pool = GrMemoryPool::Make(kSmallestMinAllocSize, kMinAllocSize);
|
|
|
|
AutoPoolReleaser r(*pool);
|
2016-11-29 14:50:35 +00:00
|
|
|
|
2019-12-12 15:58:47 +00:00
|
|
|
REPORTER_ASSERT(reporter, pool->size() == 0);
|
2016-11-29 14:50:35 +00:00
|
|
|
|
|
|
|
constexpr size_t hugeSize = 10 * kMinAllocSize;
|
2019-12-12 15:58:47 +00:00
|
|
|
r.add(pool->allocate(hugeSize));
|
|
|
|
REPORTER_ASSERT(reporter, pool->size() > hugeSize);
|
2016-11-29 14:50:35 +00:00
|
|
|
|
|
|
|
// Block size allocated to accommodate huge request doesn't include any extra
|
|
|
|
// space, so next allocation request allocates a new block.
|
2019-12-12 15:58:47 +00:00
|
|
|
size_t hugeBlockSize = pool->size();
|
|
|
|
r.add(pool->allocate(0));
|
|
|
|
REPORTER_ASSERT(reporter, pool->size() == hugeBlockSize + kMinAllocSize);
|
2016-11-29 14:50:35 +00:00
|
|
|
}
|
|
|
|
}
|