| 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" |
| 9 #include "SkOSFile.h" | 10 #include "SkOSFile.h" |
| 10 #include "SkOncePtr.h" | |
| 11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkStream.h" | 12 #include "SkStream.h" |
| 13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
| 14 | 14 |
| 15 SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) { | 15 SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) { |
| 16 fPtr = const_cast<void*>(ptr); | 16 fPtr = const_cast<void*>(ptr); |
| 17 fSize = size; | 17 fSize = size; |
| 18 fReleaseProc = proc; | 18 fReleaseProc = proc; |
| 19 fReleaseProcContext = context; | 19 fReleaseProcContext = context; |
| 20 } | 20 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 char* storage = (char*)sk_malloc_throw(actualLength); | 73 char* storage = (char*)sk_malloc_throw(actualLength); |
| 74 SkData* data = new (storage) SkData(length); | 74 SkData* data = new (storage) SkData(length); |
| 75 if (srcOrNull) { | 75 if (srcOrNull) { |
| 76 memcpy(data->writable_data(), srcOrNull, length); | 76 memcpy(data->writable_data(), srcOrNull, length); |
| 77 } | 77 } |
| 78 return data; | 78 return data; |
| 79 } | 79 } |
| 80 | 80 |
| 81 /////////////////////////////////////////////////////////////////////////////// | 81 /////////////////////////////////////////////////////////////////////////////// |
| 82 | 82 |
| 83 SK_DECLARE_STATIC_ONCE_PTR(SkData, gEmpty); | 83 // As a template argument these must have external linkage. |
| 84 SkData* sk_new_empty_data() { return new SkData(nullptr, 0, nullptr, nullptr); } |
| 85 namespace { void sk_unref_data(SkData* ptr) { return SkSafeUnref(ptr); } } |
| 86 |
| 87 SK_DECLARE_STATIC_LAZY_PTR(SkData, empty, sk_new_empty_data, sk_unref_data); |
| 88 |
| 84 SkData* SkData::NewEmpty() { | 89 SkData* SkData::NewEmpty() { |
| 85 return SkRef(gEmpty.get([]{return new SkData(nullptr, 0, nullptr, nullptr);
})); | 90 return SkRef(empty.get()); |
| 86 } | 91 } |
| 87 | 92 |
| 88 // assumes fPtr was allocated via sk_malloc | 93 // assumes fPtr was allocated via sk_malloc |
| 89 static void sk_free_releaseproc(const void* ptr, void*) { | 94 static void sk_free_releaseproc(const void* ptr, void*) { |
| 90 sk_free((void*)ptr); | 95 sk_free((void*)ptr); |
| 91 } | 96 } |
| 92 | 97 |
| 93 SkData* SkData::NewFromMalloc(const void* data, size_t length) { | 98 SkData* SkData::NewFromMalloc(const void* data, size_t length) { |
| 94 return new SkData(data, length, sk_free_releaseproc, nullptr); | 99 return new SkData(data, length, sk_free_releaseproc, nullptr); |
| 95 } | 100 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 /////////////////////////////////////////////////////////////////////////////// | 190 /////////////////////////////////////////////////////////////////////////////// |
| 186 | 191 |
| 187 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { | 192 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { |
| 188 SkAutoDataUnref data(SkData::NewUninitialized(size)); | 193 SkAutoDataUnref data(SkData::NewUninitialized(size)); |
| 189 if (stream->read(data->writable_data(), size) != size) { | 194 if (stream->read(data->writable_data(), size) != size) { |
| 190 return nullptr; | 195 return nullptr; |
| 191 } | 196 } |
| 192 return data.detach(); | 197 return data.detach(); |
| 193 } | 198 } |
| 194 | 199 |
| OLD | NEW |