diff options
author | Herb Derby <herb@google.com> | 2017-01-13 11:30:44 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-01-13 18:45:19 +0000 |
commit | 0497f088bb41338b1b1400556b9b690decc846fa (patch) | |
tree | df3e230fc4bdc59b5660ea82ad242b0ab31d8494 /tests/FixedAllocTest.cpp | |
parent | b569056be7d4638598b6ae9f676f077990909931 (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: Ie014184469b217132b0307b5a9ae40c0c60e5fc9
Reviewed-on: https://skia-review.googlesource.com/6921
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Herb Derby <herb@google.com>
Diffstat (limited to 'tests/FixedAllocTest.cpp')
-rw-r--r-- | tests/FixedAllocTest.cpp | 91 |
1 files changed, 90 insertions, 1 deletions
diff --git a/tests/FixedAllocTest.cpp b/tests/FixedAllocTest.cpp index 0a00f00935..75bc232b4b 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,91 @@ 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); + +} |