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 "chrome/app/image_pre_reader_win.h" | 5 #include "chrome/app/image_pre_reader_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <limits> | 9 #include <limits> |
10 #include <vector> | 10 #include <vector> |
(...skipping 17 matching lines...) Expand all Loading... |
28 // A handy symbolic constant. | 28 // A handy symbolic constant. |
29 const size_t kOneHundredPercent = 100; | 29 const size_t kOneHundredPercent = 100; |
30 | 30 |
31 void StaticAssertions() { | 31 void StaticAssertions() { |
32 COMPILE_ASSERT(kMinHeaderBufferSize >= sizeof(IMAGE_DOS_HEADER), | 32 COMPILE_ASSERT(kMinHeaderBufferSize >= sizeof(IMAGE_DOS_HEADER), |
33 min_header_buffer_size_at_least_as_big_as_the_dos_header); | 33 min_header_buffer_size_at_least_as_big_as_the_dos_header); |
34 } | 34 } |
35 | 35 |
36 // This struct provides a deallocation functor for use with scoped_ptr<T> | 36 // This struct provides a deallocation functor for use with scoped_ptr<T> |
37 // allocated with ::VirtualAlloc(). | 37 // allocated with ::VirtualAlloc(). |
38 struct ScopedPtrVirtualFree { | 38 struct VirtualFreeDeleter { |
39 void operator() (void* ptr) { | 39 void operator() (void* ptr) { |
40 ::VirtualFree(ptr, 0, MEM_RELEASE); | 40 ::VirtualFree(ptr, 0, MEM_RELEASE); |
41 } | 41 } |
42 }; | 42 }; |
43 | 43 |
44 // A wrapper for the Win32 ::SetFilePointer() function with some error checking. | 44 // A wrapper for the Win32 ::SetFilePointer() function with some error checking. |
45 bool SetFilePointer(HANDLE file_handle, size_t position) { | 45 bool SetFilePointer(HANDLE file_handle, size_t position) { |
46 return position <= static_cast<size_t>(std::numeric_limits<LONG>::max()) && | 46 return position <= static_cast<size_t>(std::numeric_limits<LONG>::max()) && |
47 ::SetFilePointer(file_handle, | 47 ::SetFilePointer(file_handle, |
48 static_cast<LONG>(position), | 48 static_cast<LONG>(position), |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 &headers[nt_headers_start])->OptionalHeader.SizeOfHeaders; | 245 &headers[nt_headers_start])->OptionalHeader.SizeOfHeaders; |
246 if (!ReadMissingBytes(file, &headers, size_of_headers)) | 246 if (!ReadMissingBytes(file, &headers, size_of_headers)) |
247 return false; | 247 return false; |
248 | 248 |
249 // Now we have all of the headers. This is enough to let us use the PEImage | 249 // Now we have all of the headers. This is enough to let us use the PEImage |
250 // wrapper to query the structure of the image. | 250 // wrapper to query the structure of the image. |
251 base::win::PEImage pe_image(reinterpret_cast<HMODULE>(&headers[0])); | 251 base::win::PEImage pe_image(reinterpret_cast<HMODULE>(&headers[0])); |
252 CHECK(pe_image.VerifyMagic()); | 252 CHECK(pe_image.VerifyMagic()); |
253 | 253 |
254 // Allocate a buffer to hold the pre-read bytes. | 254 // Allocate a buffer to hold the pre-read bytes. |
255 scoped_ptr_malloc<uint8, ScopedPtrVirtualFree> buffer( | 255 scoped_ptr<uint8, VirtualFreeDeleter> buffer( |
256 reinterpret_cast<uint8*>( | 256 static_cast<uint8*>( |
257 ::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE))); | 257 ::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE))); |
258 if (buffer.get() == NULL) | 258 if (buffer.get() == NULL) |
259 return false; | 259 return false; |
260 | 260 |
261 // Iterate over each section, reading in a percentage of each. | 261 // Iterate over each section, reading in a percentage of each. |
262 const IMAGE_SECTION_HEADER* section = NULL; | 262 const IMAGE_SECTION_HEADER* section = NULL; |
263 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) { | 263 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) { |
264 CHECK_LE(reinterpret_cast<const uint8*>(section + 1), | 264 CHECK_LE(reinterpret_cast<const uint8*>(section + 1), |
265 &headers[0] + headers.size()); | 265 &headers[0] + headers.size()); |
266 if (!ReadThroughSection( | 266 if (!ReadThroughSection( |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 // Vista+ branch. On these OSes, we warm up the Image by reading its | 411 // Vista+ branch. On these OSes, we warm up the Image by reading its |
412 // file off the disk. | 412 // file off the disk. |
413 return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size); | 413 return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size); |
414 } | 414 } |
415 | 415 |
416 // WinXP branch. For XP, reading the image from disk doesn't do what we want | 416 // WinXP branch. For XP, reading the image from disk doesn't do what we want |
417 // so instead we pull the pages into memory by loading the DLL and touching | 417 // so instead we pull the pages into memory by loading the DLL and touching |
418 // initialized pages at a stride. | 418 // initialized pages at a stride. |
419 return PartialPreReadImageInMemory(file_path, percentage); | 419 return PartialPreReadImageInMemory(file_path, percentage); |
420 } | 420 } |
OLD | NEW |