OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkOSFile.h" | 8 #include "SkOSFile.h" |
9 | 9 |
| 10 #include "SkTemplates.h" |
| 11 |
10 #include <io.h> | 12 #include <io.h> |
11 #include <stdio.h> | 13 #include <stdio.h> |
12 #include <sys/stat.h> | 14 #include <sys/stat.h> |
13 | 15 |
14 typedef struct { | 16 typedef struct { |
15 ULONGLONG fVolume; | 17 ULONGLONG fVolume; |
16 ULONGLONG fLsbSize; | 18 ULONGLONG fLsbSize; |
17 ULONGLONG fMsbSize; | 19 ULONGLONG fMsbSize; |
18 } SkFILEID; | 20 } SkFILEID; |
19 | 21 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 private: | 60 private: |
59 HandleType fHandle; | 61 HandleType fHandle; |
60 }; | 62 }; |
61 typedef SkAutoTHandle<HANDLE, INVALID_HANDLE_VALUE, CloseHandle> SkAutoWinFile; | 63 typedef SkAutoTHandle<HANDLE, INVALID_HANDLE_VALUE, CloseHandle> SkAutoWinFile; |
62 typedef SkAutoTHandle<HANDLE, NULL, CloseHandle> SkAutoWinMMap; | 64 typedef SkAutoTHandle<HANDLE, NULL, CloseHandle> SkAutoWinMMap; |
63 | 65 |
64 void sk_fmunmap(const void* addr, size_t) { | 66 void sk_fmunmap(const void* addr, size_t) { |
65 UnmapViewOfFile(addr); | 67 UnmapViewOfFile(addr); |
66 } | 68 } |
67 | 69 |
68 void* sk_fmmap(SkFILE* f, size_t* length) { | 70 void* sk_fdmmap(int fileno, size_t* length) { |
69 size_t fileSize = sk_fgetsize(f); | |
70 if (0 == fileSize) { | |
71 return NULL; | |
72 } | |
73 | |
74 int fileno = _fileno((FILE*)f); | |
75 if (fileno < 0) { | |
76 return NULL; | |
77 } | |
78 | |
79 HANDLE file = (HANDLE)_get_osfhandle(fileno); | 71 HANDLE file = (HANDLE)_get_osfhandle(fileno); |
80 if (INVALID_HANDLE_VALUE == file) { | 72 if (INVALID_HANDLE_VALUE == file) { |
81 return NULL; | 73 return NULL; |
82 } | 74 } |
83 | 75 |
| 76 LARGE_INTEGER fileSize; |
| 77 if (0 == GetFileSizeEx(file, &fileSize)) { |
| 78 //TODO: use SK_TRACEHR(GetLastError(), "Could not get file size.") to re
port. |
| 79 return NULL; |
| 80 } |
| 81 if (!SkTFitsIn<size_t>(fileSize.QuadPart)) { |
| 82 return NULL; |
| 83 } |
| 84 |
84 SkAutoWinMMap mmap(CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL))
; | 85 SkAutoWinMMap mmap(CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL))
; |
85 if (!mmap.isValid()) { | 86 if (!mmap.isValid()) { |
86 //TODO: use SK_TRACEHR(GetLastError(), "Could not create file mapping.")
to report. | 87 //TODO: use SK_TRACEHR(GetLastError(), "Could not create file mapping.")
to report. |
87 return NULL; | 88 return NULL; |
88 } | 89 } |
89 | 90 |
90 // Eventually call UnmapViewOfFile | 91 // Eventually call UnmapViewOfFile |
91 void* addr = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0); | 92 void* addr = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0); |
92 if (NULL == addr) { | 93 if (NULL == addr) { |
93 //TODO: use SK_TRACEHR(GetLastError(), "Could not map view of file.") to
report. | 94 //TODO: use SK_TRACEHR(GetLastError(), "Could not map view of file.") to
report. |
94 return NULL; | 95 return NULL; |
95 } | 96 } |
96 | 97 |
97 *length = fileSize; | 98 *length = static_cast<size_t>(fileSize.QuadPart); |
98 return addr; | 99 return addr; |
99 } | 100 } |
| 101 |
| 102 int sk_fileno(SkFILE* f) { |
| 103 return _fileno((FILE*)f); |
| 104 } |
| 105 |
| 106 void* sk_fmmap(SkFILE* f, size_t* length) { |
| 107 int fileno = sk_fileno(f); |
| 108 if (fileno < 0) { |
| 109 return NULL; |
| 110 } |
| 111 |
| 112 return sk_fdmmap(fileno, length); |
| 113 } |
OLD | NEW |