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 "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 // As a template argument these must have external linkage. | 83 SK_DECLARE_STATIC_ONCE_PTR(SkData, gEmpty); |
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 | |
89 SkData* SkData::NewEmpty() { | 84 SkData* SkData::NewEmpty() { |
90 return SkRef(empty.get()); | 85 return SkRef(gEmpty.get([]{return new SkData(nullptr, 0, nullptr, nullptr);
})); |
91 } | 86 } |
92 | 87 |
93 // assumes fPtr was allocated via sk_malloc | 88 // assumes fPtr was allocated via sk_malloc |
94 static void sk_free_releaseproc(const void* ptr, void*) { | 89 static void sk_free_releaseproc(const void* ptr, void*) { |
95 sk_free((void*)ptr); | 90 sk_free((void*)ptr); |
96 } | 91 } |
97 | 92 |
98 SkData* SkData::NewFromMalloc(const void* data, size_t length) { | 93 SkData* SkData::NewFromMalloc(const void* data, size_t length) { |
99 return new SkData(data, length, sk_free_releaseproc, nullptr); | 94 return new SkData(data, length, sk_free_releaseproc, nullptr); |
100 } | 95 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 /////////////////////////////////////////////////////////////////////////////// | 185 /////////////////////////////////////////////////////////////////////////////// |
191 | 186 |
192 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { | 187 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { |
193 SkAutoDataUnref data(SkData::NewUninitialized(size)); | 188 SkAutoDataUnref data(SkData::NewUninitialized(size)); |
194 if (stream->read(data->writable_data(), size) != size) { | 189 if (stream->read(data->writable_data(), size) != size) { |
195 return nullptr; | 190 return nullptr; |
196 } | 191 } |
197 return data.detach(); | 192 return data.detach(); |
198 } | 193 } |
199 | 194 |
OLD | NEW |