aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorGravatar herb <herb@google.com>2016-11-15 06:26:56 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2016-11-15 06:26:56 -0800
commitbf6d80a7a496f96589f1592ee5f644002fbb9f6d (patch)
tree86014d0b226a519bcd3929c331f903d36cdce066 /tests
parentf89aa0d254333ebf9daf95af55f11cfe5683961d (diff)
Make SkSmallAllocator obey the RAII invariants and move to heap structures when needed.
The biggest change is to the API which allowed code to bypass the destruction invariants. This destruction bypass feature was needed in only one use, and is totally encapsulated using createWithIniterT. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2488523003 Committed: https://skia.googlesource.com/skia/+/d5dc657b8c3ac916f98005dafdedafe02f023449 Committed: https://skia.googlesource.com/skia/+/c18b5f8f57a4efc5d5d1e399ed8bd3bd02c592ab Review-Url: https://codereview.chromium.org/2488523003
Diffstat (limited to 'tests')
-rw-r--r--tests/LayerDrawLooperTest.cpp21
-rw-r--r--tests/SmallAllocatorTest.cpp20
2 files changed, 30 insertions, 11 deletions
diff --git a/tests/LayerDrawLooperTest.cpp b/tests/LayerDrawLooperTest.cpp
index 4897fd2884..7b3aa29883 100644
--- a/tests/LayerDrawLooperTest.cpp
+++ b/tests/LayerDrawLooperTest.cpp
@@ -60,8 +60,11 @@ static void test_frontToBack(skiatest::Reporter* reporter) {
SkPaint paint;
auto looper(looperBuilder.detach());
SkSmallAllocator<1, 32> allocator;
- void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
- SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
+ SkDrawLooper::Context* context = allocator.createWithIniter(
+ looper->contextSize(),
+ [&](void* buffer) {
+ return looper->createContext(&canvas, buffer);
+ });
// The back layer should come first.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
@@ -100,8 +103,11 @@ static void test_backToFront(skiatest::Reporter* reporter) {
SkPaint paint;
auto looper(looperBuilder.detach());
SkSmallAllocator<1, 32> allocator;
- void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
- SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
+ SkDrawLooper::Context* context = allocator.createWithIniter(
+ looper->contextSize(),
+ [&](void* buffer) {
+ return looper->createContext(&canvas, buffer);
+ });
// The back layer should come first.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
@@ -140,8 +146,11 @@ static void test_mixed(skiatest::Reporter* reporter) {
SkPaint paint;
sk_sp<SkDrawLooper> looper(looperBuilder.detach());
SkSmallAllocator<1, 32> allocator;
- void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
- SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
+ SkDrawLooper::Context* context = allocator.createWithIniter(
+ looper->contextSize(),
+ [&](void* buffer) {
+ return looper->createContext(&canvas, buffer);
+ });
// The back layer should come first.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
diff --git a/tests/SmallAllocatorTest.cpp b/tests/SmallAllocatorTest.cpp
index 774e0c9e89..a5f45b1e8b 100644
--- a/tests/SmallAllocatorTest.cpp
+++ b/tests/SmallAllocatorTest.cpp
@@ -30,7 +30,7 @@ int CountingClass::kCount;
template<uint32_t kMaxObjects, size_t kBytes> void test_allocator(skiatest::Reporter* reporter) {
{
SkSmallAllocator<kMaxObjects, kBytes> alloc;
- for (uint32_t i = 0; i < kMaxObjects; ++i) {
+ for (uint32_t i = 0; i < kMaxObjects + 1; ++i) {
CountingClass* c = alloc.template createT<CountingClass>();
REPORTER_ASSERT(reporter, c != nullptr);
REPORTER_ASSERT(reporter, CountingClass::GetCount() == static_cast<int>(i+1));
@@ -43,18 +43,15 @@ template<uint32_t kMaxObjects, size_t kBytes> void test_allocator(skiatest::Repo
// were created in fStorage or on the heap.
DEF_TEST(SmallAllocator_destructor, reporter) {
// Four times as many bytes as objects will never require any heap
- // allocations (since SkAlign4(sizeof(CountingClass)) == 4 and the allocator
- // will stop once it reaches kMaxObjects).
+ // allocations (since SkAlign4(sizeof(CountingClass)) == 4).
test_allocator<5, 20>(reporter);
test_allocator<10, 40>(reporter);
test_allocator<20, 80>(reporter);
-#ifndef SK_DEBUG
// Allowing less bytes than objects means some will be allocated on the
// heap. Don't run these in debug where we assert.
test_allocator<50, 20>(reporter);
test_allocator<100, 20>(reporter);
-#endif
}
class Dummy {
@@ -81,3 +78,16 @@ DEF_TEST(SmallAllocator_pointer, reporter) {
REPORTER_ASSERT(reporter, container != nullptr);
REPORTER_ASSERT(reporter, container->getDummy() == &d);
}
+
+// Test that using a createWithIniterT works as expected.
+DEF_TEST(SmallAllocator_initer, reporter) {
+ SkSmallAllocator<1, 8> alloc;
+ Dummy d;
+ DummyContainer* container = alloc.createWithIniter(
+ sizeof(DummyContainer),
+ [&](void* storage) {
+ return new (storage) DummyContainer(&d);
+ });
+ REPORTER_ASSERT(reporter, container != nullptr);
+ REPORTER_ASSERT(reporter, container->getDummy() == &d);
+}