From: Lucian Grijincu <lucian@fb.com>
Date: Thu, 28 May 2015 17:42:05 +0000 (-0700)
Subject: folly: MemoryMapping: madvise: round the end to lower bound
X-Git-Tag: v0.42.0~22
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=23bc29b8109d9dc11793367f6bdcfa1962471c27;p=folly.git

folly: MemoryMapping: madvise: round the end to lower bound

Test Plan: n/a

Reviewed By: ott@fb.com

Subscribers: ott, folly-diffs@, yfeldblum, tudort, chalfant

FB internal diff: D2100535

Tasks: 4421175

Signature: t1:2100535:1432674713:6f5f40a8462851b2b8972c68d34ae23aaf1e9340
---

diff --git a/folly/MemoryMapping.cpp b/folly/MemoryMapping.cpp
index e09c391e..35c8eda6 100644
--- a/folly/MemoryMapping.cpp
+++ b/folly/MemoryMapping.cpp
@@ -300,19 +300,20 @@ void MemoryMapping::advise(int advice, size_t offset, size_t length) const {
     << " length: " << length
     << " mapLength_: " << mapLength_;
 
-  if (length == 0) {
-    return;
+  // Include the entire start page: round down to page boundary.
+  const auto offMisalign = offset % options_.pageSize;
+  offset -= offMisalign;
+  length += offMisalign;
+
+  // Round the last page down to page boundary.
+  if (offset + length != size_t(mapLength_)) {
+    length -= length % options_.pageSize;
   }
 
-  auto offMisalign = offset % options_.pageSize;
-  if (offMisalign != 0) {
-    offset -= offMisalign;
-    length += offMisalign;
+  if (length == 0) {
+    return;
   }
 
-  length = (length + options_.pageSize - 1) & ~(options_.pageSize - 1);
-  length = std::min<size_t>(length, mapLength_ - offset);
-
   char* mapStart = static_cast<char*>(mapStart_) + offset;
   PLOG_IF(WARNING, ::madvise(mapStart, length, advice)) << "madvise";
 }