| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google, Inc | 2 * Copyright 2014 Google, Inc |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkSmallAllocator.h" | 8 #include "SkSmallAllocator.h" |
| 9 #include "SkTypes.h" | 9 #include "SkTypes.h" |
| 10 #include "Test.h" | 10 #include "Test.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 private: | 24 private: |
| 25 static int kCount; | 25 static int kCount; |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 int CountingClass::kCount; | 28 int CountingClass::kCount; |
| 29 | 29 |
| 30 template<uint32_t kMaxObjects, size_t kBytes> void test_allocator(skiatest::Repo
rter* reporter) { | 30 template<uint32_t kMaxObjects, size_t kBytes> void test_allocator(skiatest::Repo
rter* reporter) { |
| 31 { | 31 { |
| 32 SkSmallAllocator<kMaxObjects, kBytes> alloc; | 32 SkSmallAllocator<kMaxObjects, kBytes> alloc; |
| 33 for (uint32_t i = 0; i < kMaxObjects + 1; ++i) { | 33 for (uint32_t i = 0; i < kMaxObjects; ++i) { |
| 34 CountingClass* c = alloc.template createT<CountingClass>(); | 34 CountingClass* c = alloc.template createT<CountingClass>(); |
| 35 REPORTER_ASSERT(reporter, c != nullptr); | 35 REPORTER_ASSERT(reporter, c != nullptr); |
| 36 REPORTER_ASSERT(reporter, CountingClass::GetCount() == static_cast<i
nt>(i+1)); | 36 REPORTER_ASSERT(reporter, CountingClass::GetCount() == static_cast<i
nt>(i+1)); |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 REPORTER_ASSERT(reporter, CountingClass::GetCount() == 0); | 39 REPORTER_ASSERT(reporter, CountingClass::GetCount() == 0); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Tests that ensure that the destructor is called, whether the objects | 42 // Tests that ensure that the destructor is called, whether the objects |
| 43 // were created in fStorage or on the heap. | 43 // were created in fStorage or on the heap. |
| 44 DEF_TEST(SmallAllocator_destructor, reporter) { | 44 DEF_TEST(SmallAllocator_destructor, reporter) { |
| 45 // Four times as many bytes as objects will never require any heap | 45 // Four times as many bytes as objects will never require any heap |
| 46 // allocations (since SkAlign4(sizeof(CountingClass)) == 4). | 46 // allocations (since SkAlign4(sizeof(CountingClass)) == 4 and the allocator |
| 47 // will stop once it reaches kMaxObjects). |
| 47 test_allocator<5, 20>(reporter); | 48 test_allocator<5, 20>(reporter); |
| 48 test_allocator<10, 40>(reporter); | 49 test_allocator<10, 40>(reporter); |
| 49 test_allocator<20, 80>(reporter); | 50 test_allocator<20, 80>(reporter); |
| 50 | 51 |
| 52 #ifndef SK_DEBUG |
| 51 // Allowing less bytes than objects means some will be allocated on the | 53 // Allowing less bytes than objects means some will be allocated on the |
| 52 // heap. Don't run these in debug where we assert. | 54 // heap. Don't run these in debug where we assert. |
| 53 test_allocator<50, 20>(reporter); | 55 test_allocator<50, 20>(reporter); |
| 54 test_allocator<100, 20>(reporter); | 56 test_allocator<100, 20>(reporter); |
| 57 #endif |
| 55 } | 58 } |
| 56 | 59 |
| 57 class Dummy { | 60 class Dummy { |
| 58 }; | 61 }; |
| 59 | 62 |
| 60 class DummyContainer { | 63 class DummyContainer { |
| 61 public: | 64 public: |
| 62 explicit DummyContainer(Dummy* d) | 65 explicit DummyContainer(Dummy* d) |
| 63 :fDummy(d) | 66 :fDummy(d) |
| 64 {} | 67 {} |
| 65 | 68 |
| 66 Dummy* getDummy() const { return fDummy; } | 69 Dummy* getDummy() const { return fDummy; } |
| 67 | 70 |
| 68 private: | 71 private: |
| 69 Dummy* fDummy; | 72 Dummy* fDummy; |
| 70 }; | 73 }; |
| 71 | 74 |
| 72 // Test that using a createT with a constructor taking a pointer as a | 75 // Test that using a createT with a constructor taking a pointer as a |
| 73 // parameter works as expected. | 76 // parameter works as expected. |
| 74 DEF_TEST(SmallAllocator_pointer, reporter) { | 77 DEF_TEST(SmallAllocator_pointer, reporter) { |
| 75 SkSmallAllocator<1, 8> alloc; | 78 SkSmallAllocator<1, 8> alloc; |
| 76 Dummy d; | 79 Dummy d; |
| 77 DummyContainer* container = alloc.createT<DummyContainer>(&d); | 80 DummyContainer* container = alloc.createT<DummyContainer>(&d); |
| 78 REPORTER_ASSERT(reporter, container != nullptr); | 81 REPORTER_ASSERT(reporter, container != nullptr); |
| 79 REPORTER_ASSERT(reporter, container->getDummy() == &d); | 82 REPORTER_ASSERT(reporter, container->getDummy() == &d); |
| 80 } | 83 } |
| 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.createWithIniterT<DummyContainer>( | |
| 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 } | |
| OLD | NEW |