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 "SkOSFile.h" | 9 #include "SkOSFile.h" |
10 #include "SkOncePtr.h" | 10 #include "SkOncePtr.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 available -= offset; | 52 available -= offset; |
53 if (length > available) { | 53 if (length > available) { |
54 length = available; | 54 length = available; |
55 } | 55 } |
56 SkASSERT(length > 0); | 56 SkASSERT(length > 0); |
57 | 57 |
58 memcpy(buffer, this->bytes() + offset, length); | 58 memcpy(buffer, this->bytes() + offset, length); |
59 return length; | 59 return length; |
60 } | 60 } |
61 | 61 |
62 SkData* SkData::PrivateNewWithCopy(const void* srcOrNull, size_t length) { | 62 sk_sp<SkData> SkData::PrivateNewWithCopy(const void* srcOrNull, size_t length) { |
63 if (0 == length) { | 63 if (0 == length) { |
64 return SkData::NewEmpty(); | 64 return SkData::MakeEmpty(); |
65 } | 65 } |
66 | 66 |
67 const size_t actualLength = length + sizeof(SkData); | 67 const size_t actualLength = length + sizeof(SkData); |
68 if (actualLength < length) { | 68 if (actualLength < length) { |
69 // we overflowed | 69 // we overflowed |
70 sk_throw(); | 70 sk_throw(); |
71 } | 71 } |
72 | 72 |
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 sk_sp<SkData>(data); |
79 } | 79 } |
80 | 80 |
81 /////////////////////////////////////////////////////////////////////////////// | 81 /////////////////////////////////////////////////////////////////////////////// |
82 | 82 |
83 SK_DECLARE_STATIC_ONCE_PTR(SkData, gEmpty); | 83 SK_DECLARE_STATIC_ONCE_PTR(SkData, gEmpty); |
84 SkData* SkData::NewEmpty() { | 84 sk_sp<SkData> SkData::MakeEmpty() { |
85 return SkRef(gEmpty.get([]{return new SkData(nullptr, 0, nullptr, nullptr);
})); | 85 SkData* data = SkRef(gEmpty.get([]{return new SkData(nullptr, 0, nullptr, nu
llptr); })); |
| 86 return sk_sp<SkData>(data); |
86 } | 87 } |
87 | 88 |
88 // assumes fPtr was allocated via sk_malloc | 89 // assumes fPtr was allocated via sk_malloc |
89 static void sk_free_releaseproc(const void* ptr, void*) { | 90 static void sk_free_releaseproc(const void* ptr, void*) { |
90 sk_free((void*)ptr); | 91 sk_free((void*)ptr); |
91 } | 92 } |
92 | 93 |
93 SkData* SkData::NewFromMalloc(const void* data, size_t length) { | 94 sk_sp<SkData> SkData::MakeFromMalloc(const void* data, size_t length) { |
94 return new SkData(data, length, sk_free_releaseproc, nullptr); | 95 return sk_sp<SkData>(new SkData(data, length, sk_free_releaseproc, nullptr))
; |
95 } | 96 } |
96 | 97 |
97 SkData* SkData::NewWithCopy(const void* src, size_t length) { | 98 sk_sp<SkData> SkData::MakeWithCopy(const void* src, size_t length) { |
98 SkASSERT(src); | 99 SkASSERT(src); |
99 return PrivateNewWithCopy(src, length); | 100 return PrivateNewWithCopy(src, length); |
100 } | 101 } |
101 | 102 |
102 SkData* SkData::NewUninitialized(size_t length) { | 103 sk_sp<SkData> SkData::MakeUninitialized(size_t length) { |
103 return PrivateNewWithCopy(nullptr, length); | 104 return PrivateNewWithCopy(nullptr, length); |
104 } | 105 } |
105 | 106 |
106 SkData* SkData::NewWithProc(const void* ptr, size_t length, ReleaseProc proc, vo
id* context) { | 107 sk_sp<SkData> SkData::MakeWithProc(const void* ptr, size_t length, ReleaseProc p
roc, void* ctx) { |
107 return new SkData(ptr, length, proc, context); | 108 return sk_sp<SkData>(new SkData(ptr, length, proc, ctx)); |
108 } | 109 } |
109 | 110 |
110 // assumes fPtr was allocated with sk_fmmap | 111 // assumes fPtr was allocated with sk_fmmap |
111 static void sk_mmap_releaseproc(const void* addr, void* ctx) { | 112 static void sk_mmap_releaseproc(const void* addr, void* ctx) { |
112 size_t length = reinterpret_cast<size_t>(ctx); | 113 size_t length = reinterpret_cast<size_t>(ctx); |
113 sk_fmunmap(addr, length); | 114 sk_fmunmap(addr, length); |
114 } | 115 } |
115 | 116 |
116 SkData* SkData::NewFromFILE(FILE* f) { | 117 sk_sp<SkData> SkData::MakeFromFILE(FILE* f) { |
117 size_t size; | 118 size_t size; |
118 void* addr = sk_fmmap(f, &size); | 119 void* addr = sk_fmmap(f, &size); |
119 if (nullptr == addr) { | 120 if (nullptr == addr) { |
120 return nullptr; | 121 return nullptr; |
121 } | 122 } |
122 | 123 |
123 return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, reinterpret_cast
<void*>(size)); | 124 return SkData::MakeWithProc(addr, size, sk_mmap_releaseproc, reinterpret_cas
t<void*>(size)); |
124 } | 125 } |
125 | 126 |
126 SkData* SkData::NewFromFileName(const char path[]) { | 127 sk_sp<SkData> SkData::MakeFromFileName(const char path[]) { |
127 FILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : nullptr; | 128 FILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : nullptr; |
128 if (nullptr == f) { | 129 if (nullptr == f) { |
129 return nullptr; | 130 return nullptr; |
130 } | 131 } |
131 SkData* data = NewFromFILE(f); | 132 auto data = MakeFromFILE(f); |
132 sk_fclose(f); | 133 sk_fclose(f); |
133 return data; | 134 return data; |
134 } | 135 } |
135 | 136 |
136 SkData* SkData::NewFromFD(int fd) { | 137 sk_sp<SkData> SkData::MakeFromFD(int fd) { |
137 size_t size; | 138 size_t size; |
138 void* addr = sk_fdmmap(fd, &size); | 139 void* addr = sk_fdmmap(fd, &size); |
139 if (nullptr == addr) { | 140 if (nullptr == addr) { |
140 return nullptr; | 141 return nullptr; |
141 } | 142 } |
142 | 143 |
143 return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, nullptr); | 144 return SkData::MakeWithProc(addr, size, sk_mmap_releaseproc, nullptr); |
144 } | 145 } |
145 | 146 |
146 // assumes context is a SkData | 147 // assumes context is a SkData |
147 static void sk_dataref_releaseproc(const void*, void* context) { | 148 static void sk_dataref_releaseproc(const void*, void* context) { |
148 SkData* src = reinterpret_cast<SkData*>(context); | 149 SkData* src = reinterpret_cast<SkData*>(context); |
149 src->unref(); | 150 src->unref(); |
150 } | 151 } |
151 | 152 |
152 SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) { | 153 sk_sp<SkData> SkData::MakeSubset(const SkData* src, size_t offset, size_t length
) { |
153 /* | 154 /* |
154 We could, if we wanted/need to, just make a deep copy of src's data, | 155 We could, if we wanted/need to, just make a deep copy of src's data, |
155 rather than referencing it. This would duplicate the storage (of the | 156 rather than referencing it. This would duplicate the storage (of the |
156 subset amount) but would possibly allow src to go out of scope sooner. | 157 subset amount) but would possibly allow src to go out of scope sooner. |
157 */ | 158 */ |
158 | 159 |
159 size_t available = src->size(); | 160 size_t available = src->size(); |
160 if (offset >= available || 0 == length) { | 161 if (offset >= available || 0 == length) { |
161 return SkData::NewEmpty(); | 162 return SkData::MakeEmpty(); |
162 } | 163 } |
163 available -= offset; | 164 available -= offset; |
164 if (length > available) { | 165 if (length > available) { |
165 length = available; | 166 length = available; |
166 } | 167 } |
167 SkASSERT(length > 0); | 168 SkASSERT(length > 0); |
168 | 169 |
169 src->ref(); // this will be balanced in sk_dataref_releaseproc | 170 src->ref(); // this will be balanced in sk_dataref_releaseproc |
170 return new SkData(src->bytes() + offset, length, sk_dataref_releaseproc, | 171 return sk_sp<SkData>(new SkData(src->bytes() + offset, length, sk_dataref_re
leaseproc, |
171 const_cast<SkData*>(src)); | 172 const_cast<SkData*>(src))); |
172 } | 173 } |
173 | 174 |
174 SkData* SkData::NewWithCString(const char cstr[]) { | 175 sk_sp<SkData> SkData::MakeWithCString(const char cstr[]) { |
175 size_t size; | 176 size_t size; |
176 if (nullptr == cstr) { | 177 if (nullptr == cstr) { |
177 cstr = ""; | 178 cstr = ""; |
178 size = 1; | 179 size = 1; |
179 } else { | 180 } else { |
180 size = strlen(cstr) + 1; | 181 size = strlen(cstr) + 1; |
181 } | 182 } |
182 return NewWithCopy(cstr, size); | 183 return MakeWithCopy(cstr, size); |
183 } | 184 } |
184 | 185 |
185 /////////////////////////////////////////////////////////////////////////////// | 186 /////////////////////////////////////////////////////////////////////////////// |
186 | 187 |
187 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { | 188 sk_sp<SkData> SkData::MakeFromStream(SkStream* stream, size_t size) { |
188 SkAutoDataUnref data(SkData::NewUninitialized(size)); | 189 sk_sp<SkData> data(SkData::MakeUninitialized(size)); |
189 if (stream->read(data->writable_data(), size) != size) { | 190 if (stream->read(data->writable_data(), size) != size) { |
190 return nullptr; | 191 return nullptr; |
191 } | 192 } |
192 return data.detach(); | 193 return data; |
193 } | 194 } |
194 | 195 |
OLD | NEW |