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 "SkLazyPtr.h" |
10 #include "SkOSFile.h" | 10 #include "SkOSFile.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 static void sk_inplace_sentinel_releaseproc(const void*, size_t, void*) { | |
16 // we should never get called, as we are just a sentinel | |
17 sk_throw(); | |
18 } | |
19 | |
20 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) { |
21 fPtr = const_cast<void*>(ptr); | 16 fPtr = const_cast<void*>(ptr); |
22 fSize = size; | 17 fSize = size; |
23 fReleaseProc = proc; | 18 fReleaseProc = proc; |
24 fReleaseProcContext = context; | 19 fReleaseProcContext = context; |
25 } | 20 } |
26 | 21 |
27 // This constructor means we are inline with our fPtr's contents. Thus we set fP
tr | 22 // This constructor means we are inline with our fPtr's contents. Thus we set fP
tr |
28 // to point right after this. We also set our releaseproc to sk_inplace_sentinel
_releaseproc, | 23 // to point right after this. We also set our releaseproc to sk_inplace_sentinel
_releaseproc, |
29 // since we need to handle "delete" ourselves. See internal_displose(). | 24 // since we need to handle "delete" ourselves. See internal_displose(). |
30 // | 25 // |
31 SkData::SkData(size_t size) { | 26 SkData::SkData(size_t size) { |
32 fPtr = (char*)(this + 1); // contents are immediately after this | 27 fPtr = (char*)(this + 1); // contents are immediately after this |
33 fSize = size; | 28 fSize = size; |
34 fReleaseProc = sk_inplace_sentinel_releaseproc; | 29 fReleaseProc = NULL; |
35 fReleaseProcContext = NULL; | 30 fReleaseProcContext = NULL; |
36 } | 31 } |
37 | 32 |
38 SkData::~SkData() { | 33 SkData::~SkData() { |
39 if (fReleaseProc) { | 34 if (fReleaseProc) { |
40 fReleaseProc(fPtr, fSize, fReleaseProcContext); | 35 fReleaseProc(fPtr, fSize, fReleaseProcContext); |
41 } | 36 } |
42 } | 37 } |
43 | 38 |
44 void SkData::internal_dispose() const { | |
45 if (sk_inplace_sentinel_releaseproc == fReleaseProc) { | |
46 const_cast<SkData*>(this)->fReleaseProc = NULL; // so we don't call i
t in our destructor | |
47 | |
48 this->internal_dispose_restore_refcnt_to_1(); | |
49 this->~SkData(); // explicitly call this for refcnt bookkeeping | |
50 | |
51 sk_free(const_cast<SkData*>(this)); | |
52 } else { | |
53 this->internal_dispose_restore_refcnt_to_1(); | |
54 SkDELETE(this); | |
55 } | |
56 } | |
57 | |
58 bool SkData::equals(const SkData* other) const { | 39 bool SkData::equals(const SkData* other) const { |
59 if (NULL == other) { | 40 if (NULL == other) { |
60 return false; | 41 return false; |
61 } | 42 } |
62 | 43 |
63 return fSize == other->fSize && !memcmp(fPtr, other->fPtr, fSize); | 44 return fSize == other->fSize && !memcmp(fPtr, other->fPtr, fSize); |
64 } | 45 } |
65 | 46 |
66 size_t SkData::copyRange(size_t offset, size_t length, void* buffer) const { | 47 size_t SkData::copyRange(size_t offset, size_t length, void* buffer) const { |
67 size_t available = fSize; | 48 size_t available = fSize; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 /////////////////////////////////////////////////////////////////////////////// | 183 /////////////////////////////////////////////////////////////////////////////// |
203 | 184 |
204 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { | 185 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { |
205 SkAutoDataUnref data(SkData::NewUninitialized(size)); | 186 SkAutoDataUnref data(SkData::NewUninitialized(size)); |
206 if (stream->read(data->writable_data(), size) != size) { | 187 if (stream->read(data->writable_data(), size) != size) { |
207 return NULL; | 188 return NULL; |
208 } | 189 } |
209 return data.detach(); | 190 return data.detach(); |
210 } | 191 } |
211 | 192 |
OLD | NEW |