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

Unified Diff: src/core/SkData.cpp

Issue 15675025: One allocation for an SkData which makes a copy. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Use 'this' instead of function as special case trigger. Created 7 years, 6 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 | « include/core/SkWeakRefCnt.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkData.cpp
===================================================================
--- src/core/SkData.cpp (revision 9507)
+++ src/core/SkData.cpp (working copy)
@@ -11,12 +11,12 @@
SK_DEFINE_INST_COUNT(SkData)
-SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) {
- fPtr = ptr;
- fSize = size;
- fReleaseProc = proc;
- fReleaseProcContext = context;
-}
+SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context)
+ : fSize(size)
+ , fPtr(ptr)
+ , fReleaseProc(proc)
+ , fReleaseProcContext(context)
+ { }
SkData::~SkData() {
if (fReleaseProc) {
@@ -58,7 +58,7 @@
return gEmptyRef;
}
-// assumes fPtr was allocated via sk_malloc
+/** Assumes ptr was allocated via sk_malloc, ignores the size and context. */
static void sk_free_releaseproc(const void* ptr, size_t, void*) {
sk_free((void*)ptr);
}
@@ -67,14 +67,26 @@
return new SkData(data, length, sk_free_releaseproc, NULL);
}
+void SkData::internal_dispose() const {
+ if (this == this->fReleaseProcContext) {
+ this->internal_dispose_restore_refcnt_to_1();
+ this->~SkData();
+ sk_free(const_cast<SkData*>(this));
+ return;
+ }
+ this->INHERITED::internal_dispose();
+}
+
SkData* SkData::NewWithCopy(const void* data, size_t length) {
if (0 == length) {
return SkData::NewEmpty();
}
reed1 2013/06/12 13:39:31 The code is good, but lets add a comment block des
- void* copy = sk_malloc_throw(length); // balanced in sk_free_releaseproc
+ size_t total_size = sizeof(SkData) + length;
+ char* self = reinterpret_cast<char*>(sk_malloc_throw(total_size));
+ char* copy = self + sizeof(SkData);
memcpy(copy, data, length);
- return new SkData(copy, length, sk_free_releaseproc, NULL);
+ return SkNEW_PLACEMENT_ARGS(self, SkData, (copy, length, NULL, self));
}
SkData* SkData::NewWithProc(const void* data, size_t length,
@@ -162,18 +174,12 @@
buffer.writeByteArray(fPtr, fSize);
}
-SkData::SkData(SkFlattenableReadBuffer& buffer) {
- fSize = buffer.getArrayCount();
- fReleaseProcContext = NULL;
-
- if (fSize > 0) {
- fPtr = sk_malloc_throw(fSize);
- fReleaseProc = sk_free_releaseproc;
- } else {
- fPtr = NULL;
- fReleaseProc = NULL;
- }
-
+SkData::SkData(SkFlattenableReadBuffer& buffer)
+ : fSize(buffer.getArrayCount())
+ , fPtr(fSize > 0 ? sk_malloc_throw(fSize) : NULL)
+ , fReleaseProc(fSize > 0 ? sk_free_releaseproc : NULL)
+ , fReleaseProcContext(NULL)
+{
buffer.readByteArray(const_cast<void*>(fPtr));
}
« no previous file with comments | « include/core/SkWeakRefCnt.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698