skia2/tests/GrTAllocatorTest.cpp

107 lines
3.1 KiB
C++
Raw Normal View History

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
#include "src/gpu/GrTAllocator.h"
#include "tests/Test.h"
namespace {
struct C {
C() : fID(-1) { ++gInstCnt; }
C(int id) : fID(id) { ++gInstCnt; }
C(C&&) = default;
C& operator=(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.
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
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);
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
int i = 0;
for (const C& c : allocator->items()) {
REPORTER_ASSERT(reporter, i == c.fID);
REPORTER_ASSERT(reporter, allocator->item(i).fID == i);
++i;
}
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
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);
}
}
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
// 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) {
SkASSERT(allocator);
SkASSERT(allocator->empty());
for (int i = 0; i < cnt; ++i) {
// Try both variations of push_back().
if (i % 1) {
allocator->push_back(C(i));
} else {
allocator->push_back() = C(i);
}
}
check_allocator_helper(allocator, cnt, popCnt, reporter);
allocator->reset();
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);
}
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
DEF_TEST(GrTAllocator, reporter) {
// Test combinations of allocators with and without stack storage and with different block
// sizes.
GrTAllocator<C> a1(1);
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
run_allocator_test(&a1, reporter);
GrTAllocator<C> a2(2);
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
run_allocator_test(&a2, reporter);
GrTAllocator<C> a5(5);
Implement GrTAllocator in terms of GrBlockAllocator Previously, the GrAllocator header defined three types: GrAllocator, GrTAllocator, and GrSTAllocator. GrAllocator was generic, and would manage the blocks of memory (fixed size to hold N * sizeof(item)). It stored an SkTArray of pointers to each block. GrTAllocator wrapped the GrAllocator to add template support for a specific T. GrSTAllocator extended GrTAllocator to include a template arg for inline storage. In this CL, GrAllocator is no longer defined, and its memory functionality is replaced with the use of GrBlockAllocator. For the most part, the implementation of GrTAllocator on top of GrBlockAllocator remains the same, although there is explicit array to the block pointers so indexing is slightly different. GrSTAllocator is also removed entirely, so that GrTAllocator's template includes initial storage size. The iteration over the allocated items is updated to wrap GrBlockAllocator's block iterator, and then iterate by index within each block. Documentation on GrAllocator already recommended using the iterator instead of random access, and these changes further reinforce it. It turns out that many of the use cases of GrAllocator were written to use random access, so many of the files outside of GrAllocator.h have just been updated to use the new for-range iteration instead of a random access for loop. Change-Id: I28b0bc277c323fd7035d4a8442ae67f058b2b64c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/272058 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Chris Dalton <csmartdalton@google.com>
2020-03-24 16:29:39 +00:00
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);
}