diff options
author | joshualitt <joshualitt@chromium.org> | 2015-04-08 07:33:33 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-08 07:33:33 -0700 |
commit | f1f8895cbec1a9b7ec4eaa7c322aa03a4469612a (patch) | |
tree | c240af52b1b926f21e92cf28288a13efb76a82d2 | |
parent | 05e4abae89d0609af91eef5dddc237c835443c9a (diff) |
add realloc method to SkAutoSTMalloc
BUG=skia:
Review URL: https://codereview.chromium.org/1069013002
-rw-r--r-- | gyp/tests.gypi | 1 | ||||
-rw-r--r-- | include/core/SkTemplates.h | 28 | ||||
-rw-r--r-- | tests/TemplatesTest.cpp | 72 |
3 files changed, 91 insertions, 10 deletions
diff --git a/gyp/tests.gypi b/gyp/tests.gypi index 98bb6eb8c5..606aed20dc 100644 --- a/gyp/tests.gypi +++ b/gyp/tests.gypi @@ -216,6 +216,7 @@ '../tests/SVGDeviceTest.cpp', '../tests/TessellatingPathRendererTests.cpp', '../tests/TArrayTest.cpp', + '../tests/TemplatesTest.cpp', '../tests/TDPQueueTest.cpp', '../tests/Time.cpp', '../tests/TLSTest.cpp', diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h index b18556b452..1d22a642da 100644 --- a/include/core/SkTemplates.h +++ b/include/core/SkTemplates.h @@ -411,17 +411,13 @@ private: template <size_t N, typename T> class SkAutoSTMalloc : SkNoncopyable { public: - SkAutoSTMalloc() { - fPtr = NULL; - } + SkAutoSTMalloc() : fPtr(fTStorage) {} SkAutoSTMalloc(size_t count) { if (count > N) { fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP); - } else if (count) { - fPtr = fTStorage; } else { - fPtr = NULL; + fPtr = fTStorage; } } @@ -437,11 +433,9 @@ public: sk_free(fPtr); } if (count > N) { - fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP); - } else if (count) { - fPtr = fTStorage; + fPtr = (T*)sk_malloc_throw(count * sizeof(T)); } else { - fPtr = NULL; + fPtr = fTStorage; } return fPtr; } @@ -464,6 +458,20 @@ public: return fPtr[index]; } + // Reallocs the array, can be used to shrink the allocation. Makes no attempt to be intelligent + void realloc(size_t count) { + if (count > N) { + if (fPtr == fTStorage) { + fPtr = (T*)sk_malloc_throw(count * sizeof(T)); + memcpy(fPtr, fTStorage, N * sizeof(T)); + } else { + fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); + } + } else if (fPtr != fTStorage) { + fPtr = (T*)sk_realloc_throw(fPtr, count * sizeof(T)); + } + } + private: T* fPtr; union { diff --git a/tests/TemplatesTest.cpp b/tests/TemplatesTest.cpp new file mode 100644 index 0000000000..31859f3f0c --- /dev/null +++ b/tests/TemplatesTest.cpp @@ -0,0 +1,72 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkTemplates.h" +#include "Test.h" + +// Tests for some of the helpers in SkTemplates.h +static void test_automalloc_realloc(skiatest::Reporter* reporter) { + SkAutoSTMalloc<1, int> array; + + // test we have a valid pointer, should not crash + array[0] = 1; + REPORTER_ASSERT(reporter, array[0] == 1); + + // using realloc for init + array.realloc(1); + + array[0] = 1; + REPORTER_ASSERT(reporter, array[0] == 1); + + // verify realloc can grow + array.realloc(2); + REPORTER_ASSERT(reporter, array[0] == 1); + + // realloc can shrink + array.realloc(1); + REPORTER_ASSERT(reporter, array[0] == 1); + + // should not crash + array.realloc(0); + + // grow and shrink again + array.realloc(10); + for (int i = 0; i < 10; i++) { + array[i] = 10 - i; + } + array.realloc(20); + for (int i = 0; i < 10; i++) { + REPORTER_ASSERT(reporter, array[i] == 10 - i); + } + array.realloc(10); + for (int i = 0; i < 10; i++) { + REPORTER_ASSERT(reporter, array[i] == 10 - i); + } + + array.realloc(1); + REPORTER_ASSERT(reporter, array[0] = 10); + + // resets mixed with realloc, below stack alloc size + array.reset(0); + array.realloc(1); + array.reset(1); + + array[0] = 1; + REPORTER_ASSERT(reporter, array[0] == 1); + + // reset and realloc > stack size + array.reset(2); + array.realloc(3); + array[0] = 1; + REPORTER_ASSERT(reporter, array[0] == 1); + array.realloc(1); + REPORTER_ASSERT(reporter, array[0] == 1); +} + +DEF_TEST(Templates, reporter) { + test_automalloc_realloc(reporter); +} |