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

Unified 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: Move Region to MemoryMappedFile 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 side-by-side diff with in-line comments
Download patch
Index: base/files/memory_mapped_file_win.cc
diff --git a/base/files/memory_mapped_file_win.cc b/base/files/memory_mapped_file_win.cc
index f3822873bfdddb877bd73afd9fa2548ca5cd83f2..4e7e934e1c858823f13a2c7842a1daa0e390591b 100644
--- a/base/files/memory_mapped_file_win.cc
+++ b/base/files/memory_mapped_file_win.cc
@@ -18,17 +18,13 @@ bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) {
return Initialize(file_name);
}
-bool MemoryMappedFile::MapFileToMemory() {
+bool MemoryMappedFile::MapFileRegionToMemory(
+ const MemoryMappedFile::Region& region) {
ThreadRestrictions::AssertIOAllowed();
if (!file_.IsValid())
return false;
- int64 len = file_.GetLength();
- if (len <= 0 || len > kint32max)
- return false;
- length_ = static_cast<size_t>(len);
-
int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY;
file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL,
@@ -36,9 +32,49 @@ bool MemoryMappedFile::MapFileToMemory() {
if (!file_mapping_.IsValid())
return false;
+ LARGE_INTEGER map_start = {0};
+ SIZE_T map_size = 0;
+ int32 data_offset = 0;
+
+ if (region == MemoryMappedFile::Region::kWholeFile) {
+ int64 file_len = file_.GetLength();
+ if (file_len <= 0 || file_len > kint32max)
+ return false;
+ length_ = static_cast<size_t>(file_len);
+ } else {
+ // The region can be arbitrarily aligned. MapViewOfFile, instead, requires
+ // that the start address is aligned to the VM granularity (which is
+ // typically larger than a page size, for instance 32k).
+ // Also, conversely to POSIX's mmap, the |map_size| doesn't have to be
+ // aligned and must be less than or equal the mapped file size.
+ // We map here the outer region [|aligned_start|, |aligned_start+size|]
+ // which contains |region| and then add up the |data_offset| displacement.
+ int64 aligned_start = 0;
+ int64 ignored = 0;
+ CalculateVMAlignedBoundaries(
+ region.offset, region.size, &aligned_start, &ignored, &data_offset);
+ int64 size = region.size + data_offset;
+
+ // Ensure that the casts below in the MapViewOfFile call are sane.
+ if (aligned_start < 0 || size < 0 ||
+ static_cast<uint64>(size) > std::numeric_limits<SIZE_T>::max()) {
+ DLOG(ERROR) << "Region bounds are not valid for MapViewOfFile";
+ return false;
+ }
+ map_start.QuadPart = aligned_start;
+ map_size = static_cast<SIZE_T>(size);
+ length_ = static_cast<size_t>(region.size);
+ }
+
data_ = static_cast<uint8*>(::MapViewOfFile(file_mapping_.Get(),
- FILE_MAP_READ, 0, 0, 0));
- return data_ != NULL;
+ FILE_MAP_READ,
+ map_start.HighPart,
+ map_start.LowPart,
+ map_size));
+ if (data_ == NULL)
+ return false;
+ data_ += data_offset;
+ return true;
}
void MemoryMappedFile::CloseHandles() {

Powered by Google App Engine
This is Rietveld 408576698