Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(496)

Side by Side Diff: chrome/app/file_pre_reader_win.cc

Issue 1412673006: Transform ImagePreReader into PreReadFile. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@a_get_module_path
Patch Set: fix style Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/app/file_pre_reader_win.h"
6
7 #include <windows.h>
8
9 #include "base/files/file.h"
10 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "base/win/windows_version.h"
14
15 namespace {
16
17 // A helper function to touch all pages in the range
18 // [base_addr, base_addr + length).
19 void TouchPagesInRange(const void* base_addr, size_t length) {
20 DCHECK(base_addr != NULL);
grt (UTC plus 2) 2015/11/03 04:02:15 DCHECK(base_addr);
fdoray 2015/11/03 21:23:39 Done.
21 DCHECK(length > 0);
grt (UTC plus 2) 2015/11/03 04:02:15 DCHECK_GT
fdoray 2015/11/03 21:23:39 Done.
22
23 // Get the system info so we know the page size. Also, make sure we use a
24 // non-zero value for the page size; GetSystemInfo() is hookable/patchable,
25 // and you never know what shenanigans someone could get up to.
26 SYSTEM_INFO system_info = {};
27 ::GetSystemInfo(&system_info);
28 if (system_info.dwPageSize == 0)
29 system_info.dwPageSize = 4096;
30
31 // We don't want to read outside the byte range (which could trigger an
32 // access violation), so let's figure out the exact locations of the first
33 // and final bytes we want to read.
34 volatile uint8_t const* touch_ptr =
35 reinterpret_cast<uint8_t const*>(base_addr);
36 volatile uint8_t const* final_touch_ptr = touch_ptr + length - 1;
37
38 // Read the memory in the range [touch_ptr, final_touch_ptr] with a stride
39 // of the system page size, to ensure that it's been paged in.
40 uint8_t dummy;
41 while (touch_ptr < final_touch_ptr) {
grt (UTC plus 2) 2015/11/03 04:02:15 for (; touch_ptr < final_touch_ptr; touch_ptr += s
fdoray 2015/11/03 21:23:39 Done.
42 dummy = *touch_ptr;
43 touch_ptr += system_info.dwPageSize;
44 }
45 dummy = *final_touch_ptr;
46 }
47
48 } // namespace
49
50 bool PreReadFile(const base::FilePath& file_path, int step_size) {
51 DCHECK(step_size > 0);
grt (UTC plus 2) 2015/11/03 04:02:14 DCHECK_GT
fdoray 2015/11/03 21:23:39 Done.
52 base::ThreadRestrictions::AssertIOAllowed();
53
54 if (base::win::GetVersion() > base::win::VERSION_XP) {
55 // Vista+ branch. On these OSes, the forced reads through the DLL actually
56 // slows warm starts. The solution is to sequentially read file contents.
57 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ |
58 base::File::FLAG_SEQUENTIAL_SCAN);
59 if (!file.IsValid())
60 return false;
61
62 LPVOID buffer = ::VirtualAlloc(NULL, static_cast<DWORD>(step_size),
grt (UTC plus 2) 2015/11/03 04:02:15 nullptr
fdoray 2015/11/03 21:23:39 Done.
63 MEM_COMMIT, PAGE_READWRITE);
64 if (buffer == NULL)
grt (UTC plus 2) 2015/11/03 04:02:15 nullptr or "if (!buffer)"
fdoray 2015/11/03 21:23:39 Done.
65 return false;
66
67 while (file.ReadAtCurrentPos(buffer, step_size) > 0) {
grt (UTC plus 2) 2015/11/03 04:02:15 nit: while(...);
fdoray 2015/11/03 21:23:39 while(); generates a pre-submit warning. "Empty l
grt (UTC plus 2) 2015/11/03 21:32:50 ah, i didn't know that. thanks.
68 }
69
70 ::VirtualFree(buffer, 0, MEM_RELEASE);
71 } else {
72 // WinXP branch. Here, reading the DLL from disk doesn't do what we want so
73 // instead we pull the pages into memory and touch pages at a stride. We use
74 // the system's page size as the stride, ignoring the passed in step_size,
75 // to make sure each page in the range is touched.
76 base::MemoryMappedFile file_memory_map;
77 if (!file_memory_map.Initialize(base::FilePath(file_path)))
grt (UTC plus 2) 2015/11/03 04:02:15 omit base::FilePath
fdoray 2015/11/03 21:23:39 Done.
78 return false;
79 TouchPagesInRange(file_memory_map.data(), file_memory_map.length());
80 }
81
82 return true;
83 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698