Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Unified Diff: include/core/SkTemplates.h

Issue 1069013002: add realloc method to SkAutoSTMalloc (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: feedback inc Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gyp/tests.gypi ('k') | tests/TemplatesTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkTemplates.h
diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h
index b18556b4524b8bb713b25747ff51929a97a8998e..1d22a642dae3ba959d4f7a9a9bee8fb4ff7072e7 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 {
« no previous file with comments | « gyp/tests.gypi ('k') | tests/TemplatesTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698