| OLD | NEW |
| 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 "SkLazyPtr.h" |
| 10 #include "SkOSFile.h" |
| 9 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 10 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 11 #include "SkOSFile.h" | |
| 12 #include "SkOnce.h" | |
| 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 fPtr = ptr; |
| 16 fSize = size; | 16 fSize = size; |
| 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) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 42 length = available; | 42 length = available; |
| 43 } | 43 } |
| 44 SkASSERT(length > 0); | 44 SkASSERT(length > 0); |
| 45 | 45 |
| 46 memcpy(buffer, this->bytes() + offset, length); | 46 memcpy(buffer, this->bytes() + offset, length); |
| 47 return length; | 47 return length; |
| 48 } | 48 } |
| 49 | 49 |
| 50 /////////////////////////////////////////////////////////////////////////////// | 50 /////////////////////////////////////////////////////////////////////////////// |
| 51 | 51 |
| 52 static SkData* gEmptyDataRef = NULL; | 52 SkData* SkData::NewEmptyImpl() { |
| 53 static void cleanup_gEmptyDataRef() { gEmptyDataRef->unref(); } | 53 return new SkData(NULL, 0, NULL, NULL); |
| 54 | |
| 55 void SkData::NewEmptyImpl(int) { | |
| 56 gEmptyDataRef = new SkData(NULL, 0, NULL, NULL); | |
| 57 } | 54 } |
| 55 void SkData::DeleteEmpty(SkData* ptr) { SkDELETE(ptr); } |
| 58 | 56 |
| 59 SkData* SkData::NewEmpty() { | 57 SkData* SkData::NewEmpty() { |
| 60 SK_DECLARE_STATIC_ONCE(once); | 58 SK_DECLARE_STATIC_LAZY_PTR(SkData, empty, NewEmptyImpl, DeleteEmpty); |
| 61 SkOnce(&once, SkData::NewEmptyImpl, 0, cleanup_gEmptyDataRef); | 59 return SkRef(empty.get()); |
| 62 gEmptyDataRef->ref(); | |
| 63 return gEmptyDataRef; | |
| 64 } | 60 } |
| 65 | 61 |
| 66 // assumes fPtr was allocated via sk_malloc | 62 // assumes fPtr was allocated via sk_malloc |
| 67 static void sk_free_releaseproc(const void* ptr, size_t, void*) { | 63 static void sk_free_releaseproc(const void* ptr, size_t, void*) { |
| 68 sk_free((void*)ptr); | 64 sk_free((void*)ptr); |
| 69 } | 65 } |
| 70 | 66 |
| 71 SkData* SkData::NewFromMalloc(const void* data, size_t length) { | 67 SkData* SkData::NewFromMalloc(const void* data, size_t length) { |
| 72 return new SkData(data, length, sk_free_releaseproc, NULL); | 68 return new SkData(data, length, sk_free_releaseproc, NULL); |
| 73 } | 69 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 SkData* SkData::NewWithCString(const char cstr[]) { | 149 SkData* SkData::NewWithCString(const char cstr[]) { |
| 154 size_t size; | 150 size_t size; |
| 155 if (NULL == cstr) { | 151 if (NULL == cstr) { |
| 156 cstr = ""; | 152 cstr = ""; |
| 157 size = 1; | 153 size = 1; |
| 158 } else { | 154 } else { |
| 159 size = strlen(cstr) + 1; | 155 size = strlen(cstr) + 1; |
| 160 } | 156 } |
| 161 return NewWithCopy(cstr, size); | 157 return NewWithCopy(cstr, size); |
| 162 } | 158 } |
| OLD | NEW |