Add array initializer
Generate an array with initialized values based on a function of the index. Change-Id: I1d7d83ba9cacde47b8c736f26dedc15294929937 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/289489 Reviewed-by: Mike Klein <mtklein@google.com> Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Herb Derby <herb@google.com>
This commit is contained in:
parent
682a2f4de6
commit
8f3963f281
@ -126,6 +126,19 @@ public:
|
||||
return array;
|
||||
}
|
||||
|
||||
template <typename T, typename Initializer>
|
||||
T* makeInitializedArray(size_t count, Initializer initializer) {
|
||||
AssertRelease(SkTFitsIn<uint32_t>(count));
|
||||
uint32_t safeCount = ToU32(count);
|
||||
T* array = (T*)this->commonArrayAlloc<T>(safeCount);
|
||||
|
||||
// Initialize array elements.
|
||||
for (size_t i = 0; i < safeCount; i++) {
|
||||
new (&array[i]) T(initializer(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// Only use makeBytesAlignedTo if none of the typed variants are impractical to use.
|
||||
void* makeBytesAlignedTo(size_t size, size_t align) {
|
||||
AssertRelease(SkTFitsIn<uint32_t>(size));
|
||||
|
@ -169,6 +169,14 @@ DEF_TEST(ArenaAlloc, r) {
|
||||
start.start = current;
|
||||
}
|
||||
|
||||
{
|
||||
SkSTArenaAlloc<64> arena;
|
||||
auto a = arena.makeInitializedArray<int>(8, [](size_t i ) { return i; });
|
||||
for (size_t i = 0; i < 8; i++) {
|
||||
REPORTER_ASSERT(r, a[i] == (int)i);
|
||||
}
|
||||
}
|
||||
|
||||
REPORTER_ASSERT(r, created == 128);
|
||||
REPORTER_ASSERT(r, destroyed == 128);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user