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 "SkFlattenableBuffers.h" | 9 #include "SkFlattenableBuffers.h" |
10 #include "SkOSFile.h" | 10 #include "SkOSFile.h" |
11 | 11 |
12 #if SK_MMAP_SUPPORT | |
13 #include <unistd.h> | |
14 #include <sys/mman.h> | |
15 #include <fcntl.h> | |
16 #include <errno.h> | |
17 #include <unistd.h> | |
18 #else | |
19 #include <io.h> | |
20 #endif | |
21 | |
22 SK_DEFINE_INST_COUNT(SkData) | 12 SK_DEFINE_INST_COUNT(SkData) |
23 | 13 |
24 SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) { | 14 SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context) { |
25 fPtr = ptr; | 15 fPtr = ptr; |
26 fSize = size; | 16 fSize = size; |
27 fReleaseProc = proc; | 17 fReleaseProc = proc; |
28 fReleaseProcContext = context; | 18 fReleaseProcContext = context; |
29 } | 19 } |
30 | 20 |
31 SkData::~SkData() { | 21 SkData::~SkData() { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 void* copy = sk_malloc_throw(length); // balanced in sk_free_releaseproc | 75 void* copy = sk_malloc_throw(length); // balanced in sk_free_releaseproc |
86 memcpy(copy, data, length); | 76 memcpy(copy, data, length); |
87 return new SkData(copy, length, sk_free_releaseproc, NULL); | 77 return new SkData(copy, length, sk_free_releaseproc, NULL); |
88 } | 78 } |
89 | 79 |
90 SkData* SkData::NewWithProc(const void* data, size_t length, | 80 SkData* SkData::NewWithProc(const void* data, size_t length, |
91 ReleaseProc proc, void* context) { | 81 ReleaseProc proc, void* context) { |
92 return new SkData(data, length, proc, context); | 82 return new SkData(data, length, proc, context); |
93 } | 83 } |
94 | 84 |
| 85 // assumes fPtr was allocated with sk_fmmap |
| 86 static void sk_mmap_releaseproc(const void* addr, size_t length, void*) { |
| 87 sk_fmunmap(addr, length); |
| 88 } |
| 89 |
| 90 SkData* SkData::NewFromFILE(SkFILE* f) { |
| 91 size_t size; |
| 92 void* addr = sk_fmmap(f, &size); |
| 93 if (NULL == addr) { |
| 94 return NULL; |
| 95 } |
| 96 |
| 97 return SkData::NewWithProc(addr, size, sk_mmap_releaseproc, NULL); |
| 98 } |
| 99 |
95 // assumes context is a SkData | 100 // assumes context is a SkData |
96 static void sk_dataref_releaseproc(const void*, size_t, void* context) { | 101 static void sk_dataref_releaseproc(const void*, size_t, void* context) { |
97 SkData* src = reinterpret_cast<SkData*>(context); | 102 SkData* src = reinterpret_cast<SkData*>(context); |
98 src->unref(); | 103 src->unref(); |
99 } | 104 } |
100 | 105 |
101 #if SK_MMAP_SUPPORT | |
102 | |
103 static void sk_munmap_releaseproc(const void* addr, size_t length, void*) { | |
104 munmap(const_cast<void*>(addr), length); | |
105 } | |
106 | |
107 SkData* SkData::NewFromFILE(SkFILE* f) { | |
108 size_t size = sk_fgetsize(f); | |
109 if (0 == size) { | |
110 return NULL; | |
111 } | |
112 | |
113 int fd = fileno((FILE*)f); | |
114 if (fd < 0) { | |
115 return NULL; | |
116 } | |
117 | |
118 void* addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); | |
119 if (MAP_FAILED == addr) { | |
120 return NULL; | |
121 } | |
122 | |
123 return SkData::NewWithProc(addr, size, sk_munmap_releaseproc, NULL); | |
124 } | |
125 | |
126 #elif defined(SK_BUILD_FOR_WIN32) | |
127 | |
128 template <typename HandleType, HandleType InvalidValue, BOOL (WINAPI * Close)(Ha
ndleType)> | |
129 class SkAutoTHandle : SkNoncopyable { | |
130 public: | |
131 SkAutoTHandle(HandleType handle) : fHandle(handle) { } | |
132 ~SkAutoTHandle() { Close(fHandle); } | |
133 operator HandleType() { return fHandle; } | |
134 bool isValid() { return InvalidValue != fHandle; } | |
135 private: | |
136 HandleType fHandle; | |
137 }; | |
138 typedef SkAutoTHandle<HANDLE, INVALID_HANDLE_VALUE, CloseHandle> SkAutoWinFile; | |
139 typedef SkAutoTHandle<HANDLE, NULL, CloseHandle> SkAutoWinMMap; | |
140 | |
141 static void sk_munmap_releaseproc(const void* addr, size_t, void*) { | |
142 UnmapViewOfFile(addr); | |
143 } | |
144 | |
145 SkData* SkData::NewFromFILE(SkFILE* f) { | |
146 size_t size = sk_fgetsize(f); | |
147 if (0 == size) { | |
148 return NULL; | |
149 } | |
150 | |
151 int fileno = _fileno((FILE*)f); | |
152 if (fileno < 0) { | |
153 return NULL; | |
154 } | |
155 | |
156 HANDLE file = (HANDLE)_get_osfhandle(fileno); | |
157 if (INVALID_HANDLE_VALUE == file) { | |
158 return NULL; | |
159 } | |
160 | |
161 SkAutoWinMMap mmap(CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL))
; | |
162 if (!mmap.isValid()) { | |
163 //TODO: use SK_TRACEHR(GetLastError(), "Could not create file mapping.")
to report. | |
164 return NULL; | |
165 } | |
166 | |
167 // Eventually call UnmapViewOfFile | |
168 void* addr = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0); | |
169 if (NULL == addr) { | |
170 //TODO: use SK_TRACEHR(GetLastError(), "Could not map view of file.") to
report. | |
171 return NULL; | |
172 } | |
173 | |
174 return SkData::NewWithProc(addr, size, sk_munmap_releaseproc, NULL); | |
175 } | |
176 | |
177 #else | |
178 | |
179 SkData* SkData::NewFromFILE(SkFILE* f) { | |
180 return NULL; | |
181 } | |
182 | |
183 #endif | |
184 | |
185 SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) { | 106 SkData* SkData::NewSubset(const SkData* src, size_t offset, size_t length) { |
186 /* | 107 /* |
187 We could, if we wanted/need to, just make a deep copy of src's data, | 108 We could, if we wanted/need to, just make a deep copy of src's data, |
188 rather than referencing it. This would duplicate the storage (of the | 109 rather than referencing it. This would duplicate the storage (of the |
189 subset amount) but would possibly allow src to go out of scope sooner. | 110 subset amount) but would possibly allow src to go out of scope sooner. |
190 */ | 111 */ |
191 | 112 |
192 size_t available = src->size(); | 113 size_t available = src->size(); |
193 if (offset >= available || 0 == length) { | 114 if (offset >= available || 0 == length) { |
194 return SkData::NewEmpty(); | 115 return SkData::NewEmpty(); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 } | 309 } |
389 | 310 |
390 SkDataSet* SkDataSet::NewEmpty() { | 311 SkDataSet* SkDataSet::NewEmpty() { |
391 static SkDataSet* gEmptySet; | 312 static SkDataSet* gEmptySet; |
392 if (NULL == gEmptySet) { | 313 if (NULL == gEmptySet) { |
393 gEmptySet = SkNEW_ARGS(SkDataSet, (NULL, 0)); | 314 gEmptySet = SkNEW_ARGS(SkDataSet, (NULL, 0)); |
394 } | 315 } |
395 gEmptySet->ref(); | 316 gEmptySet->ref(); |
396 return gEmptySet; | 317 return gEmptySet; |
397 } | 318 } |
OLD | NEW |