c97ebe06dc
Previously, the blocks owned by a GrBlockAllocator provided block-level metadata that users could control to track within a block. For types like GrTAllocator (and a collection coming down the pipe), they need to store a total count of allocations. Since GrBlockAllocator is max-aligned, having them hold on to a single extra int adds 4 to 12 bytes of padding for 8 and 16byte aligned platforms. But, the old Block size already has 4 bytes of extra padding since it was 28 bytes packed. This had been allowed to be allocation data, for requests that were 4-byte aligned or less. In practice, I think it's better to use that space for allocator-level metadata so that a total count, or other high-level data, can be packed into the data already held by GrBlockAllocator. Now GrTAllocator instances should be 8-16 bytes smaller on those same platforms. Also no longer writing into the struct-padded nether-realm, which is probably just a good thing on principle. Also cleans up how GrTAllocator's push_back() and emplace_back() are tested to make sure all options are clearly covered. Change-Id: If1da29132f3ec8df7a4056fcd834f760eb4693f5 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/303267 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Robert Phillips <robertphillips@google.com>
179 lines
5.4 KiB
C++
179 lines
5.4 KiB
C++
/*
|
|
* 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 "src/gpu/GrTAllocator.h"
|
|
#include "tests/Test.h"
|
|
|
|
namespace {
|
|
struct C {
|
|
C() : fID(-1) { ++gInstCnt; }
|
|
C(int id) : fID(id) { ++gInstCnt; }
|
|
C(C&& c) : C(c.fID) {}
|
|
C(const C& c) : C(c.fID) {}
|
|
|
|
C& operator=(C&&) = default;
|
|
C& operator=(const C&) = default;
|
|
|
|
~C() { --gInstCnt; }
|
|
|
|
int fID;
|
|
|
|
static int gInstCnt;
|
|
};
|
|
|
|
int C::gInstCnt = 0;
|
|
}
|
|
|
|
// Checks that the allocator has the correct count, etc and that the element IDs are correct.
|
|
// Then pops popCnt items and checks again.
|
|
template<int N>
|
|
static void check_allocator_helper(GrTAllocator<C, N>* allocator, int cnt, int popCnt,
|
|
skiatest::Reporter* reporter) {
|
|
REPORTER_ASSERT(reporter, (0 == cnt) == allocator->empty());
|
|
REPORTER_ASSERT(reporter, cnt == allocator->count());
|
|
REPORTER_ASSERT(reporter, cnt == C::gInstCnt);
|
|
|
|
int i = 0;
|
|
for (const C& c : allocator->items()) {
|
|
REPORTER_ASSERT(reporter, i == c.fID);
|
|
REPORTER_ASSERT(reporter, allocator->item(i).fID == i);
|
|
++i;
|
|
}
|
|
REPORTER_ASSERT(reporter, i == cnt);
|
|
|
|
if (cnt > 0) {
|
|
REPORTER_ASSERT(reporter, cnt-1 == allocator->back().fID);
|
|
}
|
|
|
|
if (popCnt > 0) {
|
|
for (int i = 0; i < popCnt; ++i) {
|
|
allocator->pop_back();
|
|
}
|
|
check_allocator_helper(allocator, cnt - popCnt, 0, reporter);
|
|
}
|
|
}
|
|
|
|
template<int N>
|
|
static void check_iterator_helper(GrTAllocator<C, N>* allocator, const std::vector<C*>& expected,
|
|
skiatest::Reporter* reporter) {
|
|
const GrTAllocator<C, N>* cAlloc = allocator;
|
|
REPORTER_ASSERT(reporter, (size_t) allocator->count() == expected.size());
|
|
// Forward+const
|
|
int i = 0;
|
|
for (const C& c : cAlloc->items()) {
|
|
REPORTER_ASSERT(reporter, (uintptr_t) &c == (uintptr_t) expected[i]);
|
|
++i;
|
|
}
|
|
REPORTER_ASSERT(reporter, (size_t) i == expected.size());
|
|
|
|
// Forward+non-const
|
|
i = 0;
|
|
for (C& c : allocator->items()) {
|
|
REPORTER_ASSERT(reporter, (uintptr_t) &c == (uintptr_t) expected[i]);
|
|
++i;
|
|
}
|
|
REPORTER_ASSERT(reporter, (size_t) i == expected.size());
|
|
|
|
// Reverse+const
|
|
i = (int) expected.size() - 1;
|
|
for (const C& c : cAlloc->ritems()) {
|
|
REPORTER_ASSERT(reporter, (uintptr_t) &c == (uintptr_t) expected[i]);
|
|
--i;
|
|
}
|
|
REPORTER_ASSERT(reporter, i == -1);
|
|
|
|
// Reverse+non-const
|
|
i = (int) expected.size() - 1;
|
|
for (C& c : allocator->ritems()) {
|
|
REPORTER_ASSERT(reporter, (uintptr_t) &c == (uintptr_t) expected[i]);
|
|
--i;
|
|
}
|
|
REPORTER_ASSERT(reporter, i == -1);
|
|
|
|
// Also test random access
|
|
for (int i = 0; i < allocator->count(); ++i) {
|
|
REPORTER_ASSERT(reporter, (uintptr_t) &allocator->item(i) == (uintptr_t) expected[i]);
|
|
REPORTER_ASSERT(reporter, (uintptr_t) &cAlloc->item(i) == (uintptr_t) expected[i]);
|
|
}
|
|
}
|
|
|
|
// Adds cnt items to the allocator, tests the cnts and iterators, pops popCnt items and checks
|
|
// again. Finally it resets the allocator and checks again.
|
|
template<int N>
|
|
static void check_allocator(GrTAllocator<C, N>* allocator, int cnt, int popCnt,
|
|
skiatest::Reporter* reporter) {
|
|
enum ItemInitializer : int {
|
|
kCopyCtor,
|
|
kMoveCtor,
|
|
kCopyAssign,
|
|
kMoveAssign,
|
|
kEmplace,
|
|
};
|
|
static constexpr int kInitCount = (int) kEmplace + 1;
|
|
|
|
SkASSERT(allocator);
|
|
SkASSERT(allocator->empty());
|
|
std::vector<C*> items;
|
|
for (int i = 0; i < cnt; ++i) {
|
|
switch((ItemInitializer) (i % kInitCount)) {
|
|
case kCopyCtor:
|
|
allocator->push_back(C(i));
|
|
break;
|
|
case kMoveCtor:
|
|
allocator->push_back(std::move(C(i)));
|
|
break;
|
|
case kCopyAssign:
|
|
allocator->push_back() = C(i);
|
|
break;
|
|
case kMoveAssign:
|
|
allocator->push_back() = std::move(C(i));
|
|
break;
|
|
case kEmplace:
|
|
allocator->emplace_back(i);
|
|
break;
|
|
}
|
|
items.push_back(&allocator->back());
|
|
}
|
|
check_iterator_helper(allocator, items, reporter);
|
|
check_allocator_helper(allocator, cnt, popCnt, reporter);
|
|
allocator->reset();
|
|
check_iterator_helper(allocator, {}, reporter);
|
|
check_allocator_helper(allocator, 0, 0, reporter);
|
|
}
|
|
|
|
template<int N>
|
|
static void run_allocator_test(GrTAllocator<C, N>* allocator, skiatest::Reporter* reporter) {
|
|
check_allocator(allocator, 0, 0, reporter);
|
|
check_allocator(allocator, 1, 1, reporter);
|
|
check_allocator(allocator, 2, 2, reporter);
|
|
check_allocator(allocator, 10, 1, reporter);
|
|
check_allocator(allocator, 10, 5, reporter);
|
|
check_allocator(allocator, 10, 10, reporter);
|
|
check_allocator(allocator, 100, 10, reporter);
|
|
}
|
|
|
|
DEF_TEST(GrTAllocator, reporter) {
|
|
// Test combinations of allocators with and without stack storage and with different block sizes
|
|
GrTAllocator<C> a1(1);
|
|
run_allocator_test(&a1, reporter);
|
|
|
|
GrTAllocator<C> a2(2);
|
|
run_allocator_test(&a2, reporter);
|
|
|
|
GrTAllocator<C> a5(5);
|
|
run_allocator_test(&a5, reporter);
|
|
|
|
GrTAllocator<C, 1> sa1;
|
|
run_allocator_test(&sa1, reporter);
|
|
|
|
GrTAllocator<C, 3> sa3;
|
|
run_allocator_test(&sa3, reporter);
|
|
|
|
GrTAllocator<C, 4> sa4;
|
|
run_allocator_test(&sa4, reporter);
|
|
}
|