diff options
author | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-03-07 03:24:41 +0000 |
---|---|---|
committer | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-03-07 03:24:41 +0000 |
commit | a5572e5bb2a2bbeeb59de0741c2527869d365a0c (patch) | |
tree | dcc5ca5a5f15d9cb45e7b4a4681900fb9c41a90d /tests | |
parent | 1d328e46179b12149aec48ccdcf39bd853bb3ec5 (diff) |
Add a class to allocate small objects w/o extra calls to new.
Add SkSmallAllocator, a template for allocating small (as defined by the
instantiation) objects without extra calls to new. Add a helper macro to
make using it simple.
Remove SkTemplatesPriv.h, whose behavior is replaced by SkSmallAllocator.
The old SK_PLACEMENT_NEW had the following drawbacks:
- Easily confused with SkNEW_PLACEMENT.
- Requires passing around lots of void*s along with the storageSize.
- Requires using a separate class for deleting it.
- We had multiple ways Auto objects for deleting in different places.
- It always did a straight heap allocation on Windows, meaning Windows
did not get any advantages from the confusing code.
The new SkSmallAllocator simplifies things:
- It is clear about what it does.
- It takes care of the deletion in one place that is automatically
handled.
Further, the new class can be used to create more than one object. This
is in preparation for BUG=skia:1976, for which we would like to create
a new object without extra heap allocations. The plan is to create both
the blitter and the new object on the stack using the SkSmallAllocator.
Add a new test for SkSmallAllocator.
SkShader.h:
Move the private version of CreateBitmapShader to SkBitmapProcShader
(which already has the implementation) and remove the friend class
(which was only used to call this private function). This allows
SkSmallAllocator to reside in the private src/ directory.
SkBitmapProcShader:
Move CreateBitmapShader and the macro for the storage size here. With
the macro in a (private) header, the (private) headers with function
declarations (which now depend on the storage size used) can see the
macro.
Use SkSmallAllocator in CreateBitmapShader.
Change the macro to kBlitterStorageByteCount, since SkSmallAllocator
takes a byte count as its template parameter.
SkBlitter:
Use the SkSmallAllocator.
Remove Sk3DShader::fKillProc and SkAutoCallProc. Both of their
behaviors have been moved into SkSmallAllocator (SkAutoCallProc was
unnecessary anyway, because the only time we ever used it we also
called detach(), so its auto behavior never happened).
Create the Sk3DShader on the stack, if there's room.
Remove the helper version of Choose, which was unused.
SmallAllocatorTest:
Test for the new class.
The rest:
Use SkSmallAllocator.
BUG=skia:1976
R=reed@google.com, mtklein@google.com
Author: scroggo@google.com
Review URL: https://codereview.chromium.org/179343005
git-svn-id: http://skia.googlecode.com/svn/trunk@13696 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests')
-rw-r--r-- | tests/SmallAllocatorTest.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/SmallAllocatorTest.cpp b/tests/SmallAllocatorTest.cpp new file mode 100644 index 0000000000..30c8ffa94a --- /dev/null +++ b/tests/SmallAllocatorTest.cpp @@ -0,0 +1,83 @@ +/* + * Copyright 2014 Google, Inc + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkSmallAllocator.h" +#include "SkTypes.h" +#include "Test.h" + +class CountingClass { +public: + CountingClass() { + kCount++; + } + + ~CountingClass() { + kCount--; + } + + static int GetCount() { return kCount; } + +private: + static int kCount; +}; + +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) { + CountingClass* c = alloc.template createT<CountingClass>(); + REPORTER_ASSERT(reporter, c != NULL); + REPORTER_ASSERT(reporter, CountingClass::GetCount() == static_cast<int>(i+1)); + } + } + REPORTER_ASSERT(reporter, CountingClass::GetCount() == 0); +} + +// Tests that ensure that the destructor is called, whether the objects +// 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). + 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 { +}; + +class DummyContainer { +public: + explicit DummyContainer(Dummy* d) + :fDummy(d) + {} + + Dummy* getDummy() const { return fDummy; } + +private: + Dummy* fDummy; +}; + +// Test that using a createT with a constructor taking a pointer as a +// parameter works as expected. +DEF_TEST(SmallAllocator_pointer, reporter) { + SkSmallAllocator<1, 8> alloc; + Dummy d; + DummyContainer* container = alloc.createT<DummyContainer>(&d); + REPORTER_ASSERT(reporter, container != NULL); + REPORTER_ASSERT(reporter, container->getDummy() == &d); +} |