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 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 } |
21 | 21 |
22 // 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 |
23 // 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, |
24 // since we need to handle "delete" ourselves. See internal_displose(). | 24 // since we need to handle "delete" ourselves. See internal_displose(). |
25 // | 25 // |
26 SkData::SkData(size_t size) { | 26 SkData::SkData(size_t size) { |
27 fPtr = (char*)(this + 1); // contents are immediately after this | 27 fPtr = (char*)(this + 1); // contents are immediately after this |
28 fSize = size; | 28 fSize = size; |
29 fReleaseProc = NULL; | 29 fReleaseProc = nullptr; |
30 fReleaseProcContext = NULL; | 30 fReleaseProcContext = nullptr; |
31 } | 31 } |
32 | 32 |
33 SkData::~SkData() { | 33 SkData::~SkData() { |
34 if (fReleaseProc) { | 34 if (fReleaseProc) { |
35 fReleaseProc(fPtr, fReleaseProcContext); | 35 fReleaseProc(fPtr, fReleaseProcContext); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 bool SkData::equals(const SkData* other) const { | 39 bool SkData::equals(const SkData* other) const { |
40 if (NULL == other) { | 40 if (nullptr == other) { |
41 return false; | 41 return false; |
42 } | 42 } |
43 | 43 |
44 return fSize == other->fSize && !memcmp(fPtr, other->fPtr, fSize); | 44 return fSize == other->fSize && !memcmp(fPtr, other->fPtr, fSize); |
45 } | 45 } |
46 | 46 |
47 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 { |
48 size_t available = fSize; | 48 size_t available = fSize; |
49 if (offset >= available || 0 == length) { | 49 if (offset >= available || 0 == length) { |
50 return 0; | 50 return 0; |
(...skipping 23 matching lines...) Expand all Loading... |
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 // As a template argument these must have external linkage. |
84 SkData* sk_new_empty_data() { return new SkData(NULL, 0, NULL, NULL); } | 84 SkData* sk_new_empty_data() { return new SkData(nullptr, 0, nullptr, nullptr); } |
85 namespace { void sk_unref_data(SkData* ptr) { return SkSafeUnref(ptr); } } | 85 namespace { void sk_unref_data(SkData* ptr) { return SkSafeUnref(ptr); } } |
86 | 86 |
87 SK_DECLARE_STATIC_LAZY_PTR(SkData, empty, sk_new_empty_data, sk_unref_data); | 87 SK_DECLARE_STATIC_LAZY_PTR(SkData, empty, sk_new_empty_data, sk_unref_data); |
88 | 88 |
89 SkData* SkData::NewEmpty() { | 89 SkData* SkData::NewEmpty() { |
90 return SkRef(empty.get()); | 90 return SkRef(empty.get()); |
91 } | 91 } |
92 | 92 |
93 // assumes fPtr was allocated via sk_malloc | 93 // assumes fPtr was allocated via sk_malloc |
94 static void sk_free_releaseproc(const void* ptr, void*) { | 94 static void sk_free_releaseproc(const void* ptr, void*) { |
95 sk_free((void*)ptr); | 95 sk_free((void*)ptr); |
96 } | 96 } |
97 | 97 |
98 SkData* SkData::NewFromMalloc(const void* data, size_t length) { | 98 SkData* SkData::NewFromMalloc(const void* data, size_t length) { |
99 return new SkData(data, length, sk_free_releaseproc, NULL); | 99 return new SkData(data, length, sk_free_releaseproc, nullptr); |
100 } | 100 } |
101 | 101 |
102 SkData* SkData::NewWithCopy(const void* src, size_t length) { | 102 SkData* SkData::NewWithCopy(const void* src, size_t length) { |
103 SkASSERT(src); | 103 SkASSERT(src); |
104 return PrivateNewWithCopy(src, length); | 104 return PrivateNewWithCopy(src, length); |
105 } | 105 } |
106 | 106 |
107 SkData* SkData::NewUninitialized(size_t length) { | 107 SkData* SkData::NewUninitialized(size_t length) { |
108 return PrivateNewWithCopy(NULL, length); | 108 return PrivateNewWithCopy(nullptr, length); |
109 } | 109 } |
110 | 110 |
111 SkData* SkData::NewWithProc(const void* ptr, size_t length, ReleaseProc proc, vo
id* context) { | 111 SkData* SkData::NewWithProc(const void* ptr, size_t length, ReleaseProc proc, vo
id* context) { |
112 return new SkData(ptr, length, proc, context); | 112 return new SkData(ptr, length, proc, context); |
113 } | 113 } |
114 | 114 |
115 // assumes fPtr was allocated with sk_fmmap | 115 // assumes fPtr was allocated with sk_fmmap |
116 static void sk_mmap_releaseproc(const void* addr, void* ctx) { | 116 static void sk_mmap_releaseproc(const void* addr, void* ctx) { |
117 size_t length = reinterpret_cast<size_t>(ctx); | 117 size_t length = reinterpret_cast<size_t>(ctx); |
118 sk_fmunmap(addr, length); | 118 sk_fmunmap(addr, length); |
119 } | 119 } |
120 | 120 |
121 SkData* SkData::NewFromFILE(SkFILE* f) { | 121 SkData* SkData::NewFromFILE(SkFILE* f) { |
122 size_t size; | 122 size_t size; |
123 void* addr = sk_fmmap(f, &size); | 123 void* addr = sk_fmmap(f, &size); |
124 if (NULL == addr) { | 124 if (nullptr == addr) { |
125 return NULL; | 125 return nullptr; |
126 } | 126 } |
127 | 127 |
128 return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, reinterpret_cast
<void*>(size)); | 128 return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, reinterpret_cast
<void*>(size)); |
129 } | 129 } |
130 | 130 |
131 SkData* SkData::NewFromFileName(const char path[]) { | 131 SkData* SkData::NewFromFileName(const char path[]) { |
132 SkFILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : NULL; | 132 SkFILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : nullptr; |
133 if (NULL == f) { | 133 if (nullptr == f) { |
134 return NULL; | 134 return nullptr; |
135 } | 135 } |
136 SkData* data = NewFromFILE(f); | 136 SkData* data = NewFromFILE(f); |
137 sk_fclose(f); | 137 sk_fclose(f); |
138 return data; | 138 return data; |
139 } | 139 } |
140 | 140 |
141 SkData* SkData::NewFromFD(int fd) { | 141 SkData* SkData::NewFromFD(int fd) { |
142 size_t size; | 142 size_t size; |
143 void* addr = sk_fdmmap(fd, &size); | 143 void* addr = sk_fdmmap(fd, &size); |
144 if (NULL == addr) { | 144 if (nullptr == addr) { |
145 return NULL; | 145 return nullptr; |
146 } | 146 } |
147 | 147 |
148 return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, NULL); | 148 return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, nullptr); |
149 } | 149 } |
150 | 150 |
151 // assumes context is a SkData | 151 // assumes context is a SkData |
152 static void sk_dataref_releaseproc(const void*, void* context) { | 152 static void sk_dataref_releaseproc(const void*, void* context) { |
153 SkData* src = reinterpret_cast<SkData*>(context); | 153 SkData* src = reinterpret_cast<SkData*>(context); |
154 src->unref(); | 154 src->unref(); |
155 } | 155 } |
156 | 156 |
157 SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) { | 157 SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) { |
158 /* | 158 /* |
(...skipping 12 matching lines...) Expand all Loading... |
171 } | 171 } |
172 SkASSERT(length > 0); | 172 SkASSERT(length > 0); |
173 | 173 |
174 src->ref(); // this will be balanced in sk_dataref_releaseproc | 174 src->ref(); // this will be balanced in sk_dataref_releaseproc |
175 return new SkData(src->bytes() + offset, length, sk_dataref_releaseproc, | 175 return new SkData(src->bytes() + offset, length, sk_dataref_releaseproc, |
176 const_cast<SkData*>(src)); | 176 const_cast<SkData*>(src)); |
177 } | 177 } |
178 | 178 |
179 SkData* SkData::NewWithCString(const char cstr[]) { | 179 SkData* SkData::NewWithCString(const char cstr[]) { |
180 size_t size; | 180 size_t size; |
181 if (NULL == cstr) { | 181 if (nullptr == cstr) { |
182 cstr = ""; | 182 cstr = ""; |
183 size = 1; | 183 size = 1; |
184 } else { | 184 } else { |
185 size = strlen(cstr) + 1; | 185 size = strlen(cstr) + 1; |
186 } | 186 } |
187 return NewWithCopy(cstr, size); | 187 return NewWithCopy(cstr, size); |
188 } | 188 } |
189 | 189 |
190 /////////////////////////////////////////////////////////////////////////////// | 190 /////////////////////////////////////////////////////////////////////////////// |
191 | 191 |
192 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { | 192 SkData* SkData::NewFromStream(SkStream* stream, size_t size) { |
193 SkAutoDataUnref data(SkData::NewUninitialized(size)); | 193 SkAutoDataUnref data(SkData::NewUninitialized(size)); |
194 if (stream->read(data->writable_data(), size) != size) { | 194 if (stream->read(data->writable_data(), size) != size) { |
195 return NULL; | 195 return nullptr; |
196 } | 196 } |
197 return data.detach(); | 197 return data.detach(); |
198 } | 198 } |
199 | 199 |
OLD | NEW |