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

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: 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"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 /** This is a global marker function which does nothing itself.
71 * When set as the release proc and the ref count goes to zero,
72 * the destructor will be called, followed by sk_free(context).
73 */
74 static void sk_free_after_destructor_releaseproc(const void*, size_t, void*) {
75 // This function intentionally left blank.
reed1 2013/06/11 21:28:15 SkASSERT(!"don't call me");
bungeman-skia 2013/06/11 22:25:32 Removed this function.
76 }
77
78 void SkData::internal_dispose() const {
79 if (sk_free_after_destructor_releaseproc == this->fReleaseProc) {
80 this->internal_dispose_restore_refcnt_to_1();
81 void* context = this->fReleaseProcContext;
reed1 2013/06/11 21:28:15 Isn't context always == this? can we call free on
bungeman-skia 2013/06/11 22:25:32 Hmmmm... yes, we do magically know that. In fact,
82 this->~SkData();
83 sk_free(context);
84 return;
85 }
86 this->INHERITED::internal_dispose();
87 }
88
70 SkData* SkData::NewWithCopy(const void* data, size_t length) { 89 SkData* SkData::NewWithCopy(const void* data, size_t length) {
71 if (0 == length) { 90 if (0 == length) {
72 return SkData::NewEmpty(); 91 return SkData::NewEmpty();
73 } 92 }
74 93
75 void* copy = sk_malloc_throw(length); // balanced in sk_free_releaseproc 94 char* everything = reinterpret_cast<char*>(sk_malloc_throw(sizeof(SkData) + length));
reed1 2013/06/11 21:28:15 This is an 80-col file...
bungeman-skia 2013/06/11 22:25:32 Done.
95 char* copy = everything + sizeof(SkData);
76 memcpy(copy, data, length); 96 memcpy(copy, data, length);
77 return new SkData(copy, length, sk_free_releaseproc, NULL); 97 return SkNEW_PLACEMENT_ARGS(everything, SkData, (copy, length, sk_free_after _destructor_releaseproc, everything));
78 } 98 }
79 99
80 SkData* SkData::NewWithProc(const void* data, size_t length, 100 SkData* SkData::NewWithProc(const void* data, size_t length,
81 ReleaseProc proc, void* context) { 101 ReleaseProc proc, void* context) {
82 return new SkData(data, length, proc, context); 102 return new SkData(data, length, proc, context);
83 } 103 }
84 104
85 // assumes fPtr was allocated with sk_fmmap 105 // assumes fPtr was allocated with sk_fmmap
86 static void sk_mmap_releaseproc(const void* addr, size_t length, void*) { 106 static void sk_mmap_releaseproc(const void* addr, size_t length, void*) {
87 sk_fmunmap(addr, length); 107 sk_fmunmap(addr, length);
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 } 349 }
330 350
331 SkDataSet* SkDataSet::NewEmpty() { 351 SkDataSet* SkDataSet::NewEmpty() {
332 static SkDataSet* gEmptySet; 352 static SkDataSet* gEmptySet;
333 if (NULL == gEmptySet) { 353 if (NULL == gEmptySet) {
334 gEmptySet = SkNEW_ARGS(SkDataSet, (NULL, 0)); 354 gEmptySet = SkNEW_ARGS(SkDataSet, (NULL, 0));
335 } 355 }
336 gEmptySet->ref(); 356 gEmptySet->ref();
337 return gEmptySet; 357 return gEmptySet;
338 } 358 }
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