| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <propvarutil.h> | 8 #include <propvarutil.h> |
| 9 #include <psapi.h> | 9 #include <psapi.h> |
| 10 #include <shellapi.h> | 10 #include <shellapi.h> |
| (...skipping 1093 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1104 bool success = false; | 1104 bool success = false; |
| 1105 HANDLE cp = GetCurrentProcess(); | 1105 HANDLE cp = GetCurrentProcess(); |
| 1106 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { | 1106 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { |
| 1107 *nt_path = FilePath(mapped_file_path); | 1107 *nt_path = FilePath(mapped_file_path); |
| 1108 success = true; | 1108 success = true; |
| 1109 } | 1109 } |
| 1110 ::UnmapViewOfFile(file_view); | 1110 ::UnmapViewOfFile(file_view); |
| 1111 return success; | 1111 return success; |
| 1112 } | 1112 } |
| 1113 | 1113 |
| 1114 bool PreReadImage(const wchar_t* file_path, size_t size_to_read, | |
| 1115 size_t step_size) { | |
| 1116 base::ThreadRestrictions::AssertIOAllowed(); | |
| 1117 if (base::win::GetVersion() > base::win::VERSION_XP) { | |
| 1118 // Vista+ branch. On these OSes, the forced reads through the DLL actually | |
| 1119 // slows warm starts. The solution is to sequentially read file contents | |
| 1120 // with an optional cap on total amount to read. | |
| 1121 base::win::ScopedHandle file( | |
| 1122 CreateFile(file_path, | |
| 1123 GENERIC_READ, | |
| 1124 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | |
| 1125 NULL, | |
| 1126 OPEN_EXISTING, | |
| 1127 FILE_FLAG_SEQUENTIAL_SCAN, | |
| 1128 NULL)); | |
| 1129 | |
| 1130 if (!file.IsValid()) | |
| 1131 return false; | |
| 1132 | |
| 1133 // Default to 1MB sequential reads. | |
| 1134 const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size), | |
| 1135 static_cast<DWORD>(1024*1024)); | |
| 1136 LPVOID buffer = ::VirtualAlloc(NULL, | |
| 1137 actual_step_size, | |
| 1138 MEM_COMMIT, | |
| 1139 PAGE_READWRITE); | |
| 1140 | |
| 1141 if (buffer == NULL) | |
| 1142 return false; | |
| 1143 | |
| 1144 DWORD len; | |
| 1145 size_t total_read = 0; | |
| 1146 while (::ReadFile(file, buffer, actual_step_size, &len, NULL) && | |
| 1147 len > 0 && | |
| 1148 (size_to_read ? total_read < size_to_read : true)) { | |
| 1149 total_read += static_cast<size_t>(len); | |
| 1150 } | |
| 1151 ::VirtualFree(buffer, 0, MEM_RELEASE); | |
| 1152 } else { | |
| 1153 // WinXP branch. Here, reading the DLL from disk doesn't do | |
| 1154 // what we want so instead we pull the pages into memory by loading | |
| 1155 // the DLL and touching pages at a stride. | |
| 1156 HMODULE dll_module = ::LoadLibraryExW( | |
| 1157 file_path, | |
| 1158 NULL, | |
| 1159 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES); | |
| 1160 | |
| 1161 if (!dll_module) | |
| 1162 return false; | |
| 1163 | |
| 1164 base::win::PEImage pe_image(dll_module); | |
| 1165 PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders(); | |
| 1166 size_t actual_size_to_read = size_to_read ? size_to_read : | |
| 1167 nt_headers->OptionalHeader.SizeOfImage; | |
| 1168 volatile uint8* touch = reinterpret_cast<uint8*>(dll_module); | |
| 1169 size_t offset = 0; | |
| 1170 while (offset < actual_size_to_read) { | |
| 1171 uint8 unused = *(touch + offset); | |
| 1172 offset += step_size; | |
| 1173 } | |
| 1174 FreeLibrary(dll_module); | |
| 1175 } | |
| 1176 | |
| 1177 return true; | |
| 1178 } | |
| 1179 | |
| 1180 } // namespace file_util | 1114 } // namespace file_util |
| OLD | NEW |