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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/core/SkWeakRefCnt.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkData.h" 8 #include "SkData.h"
9 #include "SkFlattenableBuffers.h" 9 #include "SkFlattenableBuffers.h"
10 #include "SkOSFile.h" 10 #include "SkOSFile.h"
11 11
12 SK_DEFINE_INST_COUNT(SkData) 12 SK_DEFINE_INST_COUNT(SkData)
13 13
14 SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) { 14 SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context)
15 fPtr = ptr; 15 : fSize(size)
16 fSize = size; 16 , fPtr(ptr)
17 fReleaseProc = proc; 17 , fReleaseProc(proc)
18 fReleaseProcContext = context; 18 , fReleaseProcContext(context)
19 } 19 { }
20 20
21 SkData::~SkData() { 21 SkData::~SkData() {
22 if (fReleaseProc) { 22 if (fReleaseProc) {
23 fReleaseProc(fPtr, fSize, fReleaseProcContext); 23 fReleaseProc(fPtr, fSize, fReleaseProcContext);
24 } 24 }
25 } 25 }
26 26
27 bool SkData::equals(const SkData* other) const { 27 bool SkData::equals(const SkData* other) const {
28 if (NULL == other) { 28 if (NULL == other) {
29 return false; 29 return false;
(...skipping 21 matching lines...) Expand all
51 51
52 SkData* SkData::NewEmpty() { 52 SkData* SkData::NewEmpty() {
53 static SkData* gEmptyRef; 53 static SkData* gEmptyRef;
54 if (NULL == gEmptyRef) { 54 if (NULL == gEmptyRef) {
55 gEmptyRef = new SkData(NULL, 0, NULL, NULL); 55 gEmptyRef = new SkData(NULL, 0, NULL, NULL);
56 } 56 }
57 gEmptyRef->ref(); 57 gEmptyRef->ref();
58 return gEmptyRef; 58 return gEmptyRef;
59 } 59 }
60 60
61 // assumes fPtr was allocated via sk_malloc 61 /** Assumes ptr was allocated via sk_malloc, ignores the size and context. */
62 static void sk_free_releaseproc(const void* ptr, size_t, void*) { 62 static void sk_free_releaseproc(const void* ptr, size_t, void*) {
63 sk_free((void*)ptr); 63 sk_free((void*)ptr);
64 } 64 }
65 65
66 SkData* SkData::NewFromMalloc(const void* data, size_t length) { 66 SkData* SkData::NewFromMalloc(const void* data, size_t length) {
67 return new SkData(data, length, sk_free_releaseproc, NULL); 67 return new SkData(data, length, sk_free_releaseproc, NULL);
68 } 68 }
69 69
70 void SkData::internal_dispose() const {
71 if (this == this->fReleaseProcContext) {
72 this->internal_dispose_restore_refcnt_to_1();
73 this->~SkData();
74 sk_free(const_cast<SkData*>(this));
75 return;
76 }
77 this->INHERITED::internal_dispose();
78 }
79
70 SkData* SkData::NewWithCopy(const void* data, size_t length) { 80 SkData* SkData::NewWithCopy(const void* data, size_t length) {
71 if (0 == length) { 81 if (0 == length) {
72 return SkData::NewEmpty(); 82 return SkData::NewEmpty();
73 } 83 }
74 84
reed1 2013/06/12 13:39:31 The code is good, but lets add a comment block des
75 void* copy = sk_malloc_throw(length); // balanced in sk_free_releaseproc 85 size_t total_size = sizeof(SkData) + length;
86 char* self = reinterpret_cast<char*>(sk_malloc_throw(total_size));
87 char* copy = self + sizeof(SkData);
76 memcpy(copy, data, length); 88 memcpy(copy, data, length);
77 return new SkData(copy, length, sk_free_releaseproc, NULL); 89 return SkNEW_PLACEMENT_ARGS(self, SkData, (copy, length, NULL, self));
78 } 90 }
79 91
80 SkData* SkData::NewWithProc(const void* data, size_t length, 92 SkData* SkData::NewWithProc(const void* data, size_t length,
81 ReleaseProc proc, void* context) { 93 ReleaseProc proc, void* context) {
82 return new SkData(data, length, proc, context); 94 return new SkData(data, length, proc, context);
83 } 95 }
84 96
85 // assumes fPtr was allocated with sk_fmmap 97 // assumes fPtr was allocated with sk_fmmap
86 static void sk_mmap_releaseproc(const void* addr, size_t length, void*) { 98 static void sk_mmap_releaseproc(const void* addr, size_t length, void*) {
87 sk_fmunmap(addr, length); 99 sk_fmunmap(addr, length);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 167 }
156 return NewWithCopy(cstr, size); 168 return NewWithCopy(cstr, size);
157 } 169 }
158 170
159 /////////////////////////////////////////////////////////////////////////////// 171 ///////////////////////////////////////////////////////////////////////////////
160 172
161 void SkData::flatten(SkFlattenableWriteBuffer& buffer) const { 173 void SkData::flatten(SkFlattenableWriteBuffer& buffer) const {
162 buffer.writeByteArray(fPtr, fSize); 174 buffer.writeByteArray(fPtr, fSize);
163 } 175 }
164 176
165 SkData::SkData(SkFlattenableReadBuffer& buffer) { 177 SkData::SkData(SkFlattenableReadBuffer& buffer)
166 fSize = buffer.getArrayCount(); 178 : fSize(buffer.getArrayCount())
167 fReleaseProcContext = NULL; 179 , fPtr(fSize > 0 ? sk_malloc_throw(fSize) : NULL)
168 180 , fReleaseProc(fSize > 0 ? sk_free_releaseproc : NULL)
169 if (fSize > 0) { 181 , fReleaseProcContext(NULL)
170 fPtr = sk_malloc_throw(fSize); 182 {
171 fReleaseProc = sk_free_releaseproc;
172 } else {
173 fPtr = NULL;
174 fReleaseProc = NULL;
175 }
176
177 buffer.readByteArray(const_cast<void*>(fPtr)); 183 buffer.readByteArray(const_cast<void*>(fPtr));
178 } 184 }
179 185
180 /////////////////////////////////////////////////////////////////////////////// 186 ///////////////////////////////////////////////////////////////////////////////
181 /////////////////////////////////////////////////////////////////////////////// 187 ///////////////////////////////////////////////////////////////////////////////
182 188
183 #include "SkDataSet.h" 189 #include "SkDataSet.h"
184 #include "SkFlattenable.h" 190 #include "SkFlattenable.h"
185 #include "SkStream.h" 191 #include "SkStream.h"
186 192
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 } 335 }
330 336
331 SkDataSet* SkDataSet::NewEmpty() { 337 SkDataSet* SkDataSet::NewEmpty() {
332 static SkDataSet* gEmptySet; 338 static SkDataSet* gEmptySet;
333 if (NULL == gEmptySet) { 339 if (NULL == gEmptySet) {
334 gEmptySet = SkNEW_ARGS(SkDataSet, (NULL, 0)); 340 gEmptySet = SkNEW_ARGS(SkDataSet, (NULL, 0));
335 } 341 }
336 gEmptySet->ref(); 342 gEmptySet->ref();
337 return gEmptySet; 343 return gEmptySet;
338 } 344 }
OLDNEW
« 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