commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google, Inc |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkSmallAllocator.h" |
| 9 | #include "SkTypes.h" |
| 10 | #include "Test.h" |
| 11 | |
| 12 | class CountingClass { |
| 13 | public: |
| 14 | CountingClass() { |
| 15 | kCount++; |
| 16 | } |
| 17 | |
| 18 | ~CountingClass() { |
| 19 | kCount--; |
| 20 | } |
| 21 | |
| 22 | static int GetCount() { return kCount; } |
| 23 | |
| 24 | private: |
| 25 | static int kCount; |
| 26 | }; |
| 27 | |
| 28 | int CountingClass::kCount; |
| 29 | |
| 30 | template<uint32_t kMaxObjects, size_t kBytes> void test_allocator(skiatest::Reporter* reporter) { |
| 31 | { |
| 32 | SkSmallAllocator<kMaxObjects, kBytes> alloc; |
herb | bf6d80a | 2016-11-15 06:26:56 -0800 | [diff] [blame] | 33 | for (uint32_t i = 0; i < kMaxObjects + 1; ++i) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 34 | CountingClass* c = alloc.template createT<CountingClass>(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | REPORTER_ASSERT(reporter, c != nullptr); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, CountingClass::GetCount() == static_cast<int>(i+1)); |
| 37 | } |
| 38 | } |
| 39 | REPORTER_ASSERT(reporter, CountingClass::GetCount() == 0); |
| 40 | } |
| 41 | |
| 42 | // Tests that ensure that the destructor is called, whether the objects |
| 43 | // were created in fStorage or on the heap. |
| 44 | DEF_TEST(SmallAllocator_destructor, reporter) { |
| 45 | // Four times as many bytes as objects will never require any heap |
herb | bf6d80a | 2016-11-15 06:26:56 -0800 | [diff] [blame] | 46 | // allocations (since SkAlign4(sizeof(CountingClass)) == 4). |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 47 | test_allocator<5, 20>(reporter); |
| 48 | test_allocator<10, 40>(reporter); |
| 49 | test_allocator<20, 80>(reporter); |
| 50 | |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 51 | // Allowing less bytes than objects means some will be allocated on the |
| 52 | // heap. Don't run these in debug where we assert. |
| 53 | test_allocator<50, 20>(reporter); |
| 54 | test_allocator<100, 20>(reporter); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | class Dummy { |
| 58 | }; |
| 59 | |
| 60 | class DummyContainer { |
| 61 | public: |
| 62 | explicit DummyContainer(Dummy* d) |
| 63 | :fDummy(d) |
| 64 | {} |
| 65 | |
| 66 | Dummy* getDummy() const { return fDummy; } |
| 67 | |
| 68 | private: |
| 69 | Dummy* fDummy; |
| 70 | }; |
| 71 | |
| 72 | // Test that using a createT with a constructor taking a pointer as a |
| 73 | // parameter works as expected. |
| 74 | DEF_TEST(SmallAllocator_pointer, reporter) { |
| 75 | SkSmallAllocator<1, 8> alloc; |
| 76 | Dummy d; |
| 77 | DummyContainer* container = alloc.createT<DummyContainer>(&d); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, container != nullptr); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, container->getDummy() == &d); |
| 80 | } |
herb | bf6d80a | 2016-11-15 06:26:56 -0800 | [diff] [blame] | 81 | |
| 82 | // Test that using a createWithIniterT works as expected. |
| 83 | DEF_TEST(SmallAllocator_initer, reporter) { |
| 84 | SkSmallAllocator<1, 8> alloc; |
| 85 | Dummy d; |
| 86 | DummyContainer* container = alloc.createWithIniter( |
| 87 | sizeof(DummyContainer), |
| 88 | [&](void* storage) { |
| 89 | return new (storage) DummyContainer(&d); |
| 90 | }); |
| 91 | REPORTER_ASSERT(reporter, container != nullptr); |
| 92 | REPORTER_ASSERT(reporter, container->getDummy() == &d); |
| 93 | } |