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

Side by Side Diff: base/files/memory_mapped_file_win.cc

Issue 394313002: Add support for loading pak files from arbitrary file regions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address first willchan@ comments Created 6 years, 5 months 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <algorithm>
willchan no longer on Chromium 2014/07/22 20:45:47 This should go below memory_mapped_file.h, as per
6
5 #include "base/files/memory_mapped_file.h" 7 #include "base/files/memory_mapped_file.h"
6 8
7 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
8 #include "base/strings/string16.h" 10 #include "base/strings/string16.h"
9 #include "base/threading/thread_restrictions.h" 11 #include "base/threading/thread_restrictions.h"
10 12
11 namespace base { 13 namespace base {
12 14
13 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) { 15 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) {
14 } 16 }
15 17
16 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { 18 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) {
17 image_ = true; 19 image_ = true;
18 return Initialize(file_name); 20 return Initialize(file_name);
19 } 21 }
20 22
21 bool MemoryMappedFile::MapFileToMemory() { 23 bool MemoryMappedFile::MapFileRegionToMemory(const base::File::Region& region) {
22 ThreadRestrictions::AssertIOAllowed(); 24 ThreadRestrictions::AssertIOAllowed();
23 25
24 if (!file_.IsValid()) 26 if (!file_.IsValid())
25 return false; 27 return false;
26 28
27 int64 len = file_.GetLength();
28 if (len <= 0 || len > kint32max)
29 return false;
30 length_ = static_cast<size_t>(len);
31
32 int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY; 29 int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY;
33 30
34 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, 31 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL,
35 flags, 0, 0, NULL)); 32 flags, 0, 0, NULL));
36 if (!file_mapping_.IsValid()) 33 if (!file_mapping_.IsValid())
37 return false; 34 return false;
38 35
36 LARGE_INTEGER map_start = {0};
37 SIZE_T map_size = 0;
38 int32 data_offset = 0;
39
40 if (region.IsWholeFile()) {
41 int64 file_len = file_.GetLength();
42 if (file_len <= 0 || file_len > kint32max)
43 return false;
44 length_ = static_cast<size_t>(file_len);
45 } else {
46 // The region can be arbitrarily aligned. MapViewOfFile, instead, requires
47 // that the start address is aligned to the VM granularity (which is
48 // typically larger than a page size, for instance 32k).
49 // Also, conversely to POSIX's mmap, the |map_size| doesn't have to be
50 // aligned and must be less than or equal the mapped file size.
51 // We map here the outer region [|aligned_start|, |aligned_start+size|]
52 // which contains |region| and then add up the |data_offset| displacement.
53 int64 aligned_start = 0;
54 int64 ignored = 0;
55 CalculateVMAlignedBoundaries(
56 region.offset, region.size, &aligned_start, &ignored, &data_offset);
57 int64 size = region.size + data_offset;
58
59 // Ensure that the casts below in the MapViewOfFile call are sane.
60 if (aligned_start < 0 || size < 0 ||
61 static_cast<uint64>(size) > std::numeric_limits<SIZE_T>::max()) {
62 DLOG(ERROR) << "Region bounds are not valid for MapViewOfFile";
63 return false;
64 }
65 map_start.QuadPart = aligned_start;
66 map_size = static_cast<SIZE_T>(size);
67 length_ = static_cast<size_t>(region.size);
68 }
69
39 data_ = static_cast<uint8*>(::MapViewOfFile(file_mapping_.Get(), 70 data_ = static_cast<uint8*>(::MapViewOfFile(file_mapping_.Get(),
40 FILE_MAP_READ, 0, 0, 0)); 71 FILE_MAP_READ,
41 return data_ != NULL; 72 map_start.HighPart,
73 map_start.LowPart,
74 map_size));
75 if (data_ == NULL)
76 return false;
77 data_ += data_offset;
78 return true;
42 } 79 }
43 80
44 void MemoryMappedFile::CloseHandles() { 81 void MemoryMappedFile::CloseHandles() {
45 if (data_) 82 if (data_)
46 ::UnmapViewOfFile(data_); 83 ::UnmapViewOfFile(data_);
47 if (file_mapping_.IsValid()) 84 if (file_mapping_.IsValid())
48 file_mapping_.Close(); 85 file_mapping_.Close();
49 if (file_.IsValid()) 86 if (file_.IsValid())
50 file_.Close(); 87 file_.Close();
51 88
52 data_ = NULL; 89 data_ = NULL;
53 length_ = 0; 90 length_ = 0;
54 } 91 }
55 92
56 } // namespace base 93 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698