folly: MemoryMapping: madvise: round the end to lower bound
authorLucian Grijincu <lucian@fb.com>
Thu, 28 May 2015 17:42:05 +0000 (10:42 -0700)
committerNoam Lerner <noamler@fb.com>
Wed, 3 Jun 2015 16:47:05 +0000 (09:47 -0700)
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

folly/MemoryMapping.cpp

index e09c391ef5729f3ad70d7f65b0aeb7388c4add9f..35c8eda69db9f424f3b0e3ce247629055a7f7ed6 100644 (file)
@@ -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";
 }