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

Unified Diff: include/core/SkTypes.h

Issue 15558005: Add return param to SkAutoSMalloc::reset() that indicates whether the allocation changed or not. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: rename Created 7 years, 7 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkTypes.h
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h
index 4f6788c928b13427943e33b6cbe0c848b7721ad9..f881719a17b131a33e2027613995feb87ec2fe8b 100644
--- a/include/core/SkTypes.h
+++ b/include/core/SkTypes.h
@@ -484,14 +484,20 @@ public:
/**
* Reallocates the block to a new size. The ptr may or may not change.
*/
- void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink) {
+ void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
+ if (NULL != didChangeAlloc) {
+ *didChangeAlloc = false;
+ }
return fPtr;
}
sk_free(fPtr);
fPtr = size ? sk_malloc_throw(size) : NULL;
fSize = size;
+ if (NULL != didChangeAlloc) {
+ *didChangeAlloc = true;
+ }
return fPtr;
}
@@ -540,7 +546,7 @@ public:
*/
SkAutoSMalloc() {
fPtr = fStorage;
- fSize = 0;
+ fSize = kSize;
}
/**
@@ -550,7 +556,7 @@ public:
*/
explicit SkAutoSMalloc(size_t size) {
fPtr = fStorage;
- fSize = 0;
+ fSize = kSize;
this->reset(size);
}
@@ -579,21 +585,29 @@ public:
* heap.
*/
void* reset(size_t size,
- SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink) {
- if (size == fSize || (SkAutoMalloc::kReuse_OnShrink == shrink &&
- size < fSize)) {
- return fPtr;
+ SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
+ bool* didChangeAlloc = NULL) {
+ size = (size < kSize) ? kSize : size;
+ bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size < fSize);
+ if (NULL != didChangeAlloc) {
+ *didChangeAlloc = alloc;
}
-
- if (fPtr != (void*)fStorage) {
- sk_free(fPtr);
- }
-
- if (size <= kSize) {
- fPtr = fStorage;
- } else {
- fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
+ if (alloc) {
+ if (fPtr != (void*)fStorage) {
+ sk_free(fPtr);
+ }
+
+ if (size == kSize) {
+ SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
+ fPtr = fStorage;
+ } else {
+ fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
+ }
+
+ fSize = size;
}
+ SkASSERT(fSize >= size && fSize >= kSize);
+ SkASSERT((fPtr == fStorage) || fSize > kSize);
return fPtr;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698