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

Unified Diff: base/files/memory_mapped_file.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.cc
diff --git a/base/files/memory_mapped_file.cc b/base/files/memory_mapped_file.cc
index ace4e112628519137e7f56fbfb36f6176c2e8e83..e9656495844212d1fea11c16cabdca9118e1a540 100644
--- a/base/files/memory_mapped_file.cc
+++ b/base/files/memory_mapped_file.cc
@@ -6,9 +6,26 @@
#include "base/files/file_path.h"
#include "base/logging.h"
+#include "base/sys_info.h"
namespace base {
+const MemoryMappedFile::Region MemoryMappedFile::Region::kWholeFile;
+
+MemoryMappedFile::Region::Region() : offset(0), size(0) {
+}
+
+MemoryMappedFile::Region::Region(int64 offset, int64 size)
+ : offset(offset), size(size) {
+ DCHECK_GE(offset, 0);
+ DCHECK_GT(size, 0);
+}
+
+bool MemoryMappedFile::Region::operator==(
+ const MemoryMappedFile::Region& other) const {
+ return other.offset == offset && other.size == size;
+}
+
MemoryMappedFile::~MemoryMappedFile() {
CloseHandles();
}
@@ -24,7 +41,7 @@ bool MemoryMappedFile::Initialize(const FilePath& file_name) {
return false;
}
- if (!MapFileToMemory()) {
+ if (!MapFileRegionToMemory(Region::kWholeFile)) {
CloseHandles();
return false;
}
@@ -33,12 +50,16 @@ bool MemoryMappedFile::Initialize(const FilePath& file_name) {
}
bool MemoryMappedFile::Initialize(File file) {
+ return Initialize(file.Pass(), Region::kWholeFile);
+}
+
+bool MemoryMappedFile::Initialize(File file, const Region& region) {
if (IsValid())
return false;
file_ = file.Pass();
- if (!MapFileToMemory()) {
+ if (!MapFileRegionToMemory(region)) {
CloseHandles();
return false;
}
@@ -50,4 +71,18 @@ bool MemoryMappedFile::IsValid() const {
return data_ != NULL;
}
+// static
+void MemoryMappedFile::CalculateVMAlignedBoundaries(int64 start,
+ int64 size,
+ int64* aligned_start,
+ int64* aligned_size,
+ int32* offset) {
+ // Sadly, on Windows, the mmap alignment is not just equal to the page size.
+ const int64 mask = static_cast<int64>(SysInfo::VMAllocationGranularity()) - 1;
+ DCHECK_LT(mask, std::numeric_limits<int32>::max());
+ *offset = start & mask;
+ *aligned_start = start & ~mask;
+ *aligned_size = (size + *offset + mask) & ~mask;
+}
+
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698