aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorGravatar Herb Derby <herb@google.com>2017-01-11 13:43:47 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-01-11 20:43:22 +0000
commit6ff51aedda6f3b4873c292d7e03e47ad656543f8 (patch)
treebba7e522ccf732addbf8a5627a4db8087ab9fa0b /tests
parent7704754049cac4794f27496efa90acea963b8881 (diff)
Introduce SkArenaAlloc - should be fast for POD types and RAII for types with dtors.
- Implementation. - Use in SkLinearPipeline. TBR=mtklein@google.com Change-Id: Ia8efd09b2f3139a57182889ba84d1610eae92749 Reviewed-on: https://skia-review.googlesource.com/6352 Reviewed-by: Herb Derby <herb@google.com> Commit-Queue: Herb Derby <herb@google.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/FixedAllocTest.cpp90
1 files changed, 89 insertions, 1 deletions
diff --git a/tests/FixedAllocTest.cpp b/tests/FixedAllocTest.cpp
index 0a00f00935..5c36b537fa 100644
--- a/tests/FixedAllocTest.cpp
+++ b/tests/FixedAllocTest.cpp
@@ -13,7 +13,8 @@ namespace {
static int created, destroyed;
struct Foo {
- Foo(int X, float Y) : x(X), y(Y) { created++; }
+ Foo() : x(-2), y(-3.0f) { created++; }
+ Foo(int X, float Y) : x(X), y(Y) { created++; }
~Foo() { destroyed++; }
int x;
@@ -111,3 +112,90 @@ DEF_TEST(FallbackAlloc, r) {
REPORTER_ASSERT(r, !in_buf(big));
REPORTER_ASSERT(r, !in_buf(smallB));
}
+
+struct WithDtor {
+ ~WithDtor() { }
+};
+
+DEF_TEST(ArenaAlloc, r) {
+ {
+ created = 0;
+ destroyed = 0;
+
+ SkArenaAlloc arena{nullptr, 0};
+ REPORTER_ASSERT(r, *arena.make<int>(3) == 3);
+ Foo* foo = arena.make<Foo>(3, 4.0f);
+ REPORTER_ASSERT(r, foo->x == 3);
+ REPORTER_ASSERT(r, foo->y == 4.0f);
+ REPORTER_ASSERT(r, created == 1);
+ REPORTER_ASSERT(r, destroyed == 0);
+ arena.makeArrayDefault<int>(10);
+ int* zeroed = arena.makeArray<int>(10);
+ for (int i = 0; i < 10; i++) {
+ REPORTER_ASSERT(r, zeroed[i] == 0);
+ }
+ Foo* fooArray = arena.makeArrayDefault<Foo>(10);
+ REPORTER_ASSERT(r, fooArray[3].x == -2);
+ REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
+ REPORTER_ASSERT(r, created == 11);
+ REPORTER_ASSERT(r, destroyed == 0);
+ arena.make<typename std::aligned_storage<10,8>::type>();
+ }
+ REPORTER_ASSERT(r, created == 11);
+ REPORTER_ASSERT(r, destroyed == 11);
+
+ {
+ created = 0;
+ destroyed = 0;
+ char block[1024];
+ SkArenaAlloc arena{block};
+
+ REPORTER_ASSERT(r, *arena.make<int>(3) == 3);
+ Foo* foo = arena.make<Foo>(3, 4.0f);
+ REPORTER_ASSERT(r, foo->x == 3);
+ REPORTER_ASSERT(r, foo->y == 4.0f);
+ REPORTER_ASSERT(r, created == 1);
+ REPORTER_ASSERT(r, destroyed == 0);
+ arena.makeArrayDefault<int>(10);
+ int* zeroed = arena.makeArray<int>(10);
+ for (int i = 0; i < 10; i++) {
+ REPORTER_ASSERT(r, zeroed[i] == 0);
+ }
+ Foo* fooArray = arena.makeArrayDefault<Foo>(10);
+ REPORTER_ASSERT(r, fooArray[3].x == -2);
+ REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
+ REPORTER_ASSERT(r, created == 11);
+ REPORTER_ASSERT(r, destroyed == 0);
+ arena.make<typename std::aligned_storage<10,8>::type>();
+ }
+ REPORTER_ASSERT(r, created == 11);
+ REPORTER_ASSERT(r, destroyed == 11);
+
+ {
+ created = 0;
+ destroyed = 0;
+ std::unique_ptr<char[]> block{new char[1024]};
+ SkArenaAlloc arena{block.get(), 1024};
+
+ REPORTER_ASSERT(r, *arena.make<int>(3) == 3);
+ Foo* foo = arena.make<Foo>(3, 4.0f);
+ REPORTER_ASSERT(r, foo->x == 3);
+ REPORTER_ASSERT(r, foo->y == 4.0f);
+ REPORTER_ASSERT(r, created == 1);
+ REPORTER_ASSERT(r, destroyed == 0);
+ arena.makeArrayDefault<int>(10);
+ int* zeroed = arena.makeArray<int>(10);
+ for (int i = 0; i < 10; i++) {
+ REPORTER_ASSERT(r, zeroed[i] == 0);
+ }
+ Foo* fooArray = arena.makeArrayDefault<Foo>(10);
+ REPORTER_ASSERT(r, fooArray[3].x == -2);
+ REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
+ REPORTER_ASSERT(r, created == 11);
+ REPORTER_ASSERT(r, destroyed == 0);
+ arena.make<typename std::aligned_storage<10,8>::type>();
+ }
+ REPORTER_ASSERT(r, created == 11);
+ REPORTER_ASSERT(r, destroyed == 11);
+
+}