From: Lucian Grijincu Date: Wed, 27 Feb 2013 23:44:37 +0000 (-0800) Subject: MemoryMapping::data() returns StringPiece ::range() returns ByteRange X-Git-Tag: v0.22.0~1051 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=cb610acb4aed654496281ba2d623e6d6a862a980;p=folly.git MemoryMapping::data() returns StringPiece ::range() returns ByteRange Summary: MemoryMapping::data() returns StringPiece ::range() returns ByteRange Test Plan: tests Reviewed By: philipp@fb.com FB internal diff: D720985 --- diff --git a/folly/MemoryMapping.h b/folly/MemoryMapping.h index 244727b4..3a355892 100644 --- a/folly/MemoryMapping.h +++ b/folly/MemoryMapping.h @@ -87,7 +87,6 @@ class MemoryMapping : boost::noncopyable { */ template Range asRange() const { - CHECK(mapStart_); size_t count = data_.size() / sizeof(T); return Range(static_cast( static_cast(data_.data())), @@ -104,14 +103,16 @@ class MemoryMapping : boost::noncopyable { /** * Return the memory area where the file was mapped. */ - ByteRange data() const { - return range(); + StringPiece data() const { + return asRange(); } bool mlocked() const { return locked_; } + int fd() const { return file_.fd(); } + protected: MemoryMapping(); @@ -143,7 +144,6 @@ class WritableMemoryMapping : public MemoryMapping { */ template Range asWritableRange() const { - CHECK(mapStart_); size_t count = data_.size() / sizeof(T); return Range(static_cast( static_cast(data_.data())), diff --git a/folly/test/MemoryMappingTest.cpp b/folly/test/MemoryMappingTest.cpp index 662c54c3..61dceb2a 100644 --- a/folly/test/MemoryMappingTest.cpp +++ b/folly/test/MemoryMappingTest.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include "folly/MemoryMapping.h" @@ -54,4 +55,73 @@ TEST(MemoryMapping, DoublyMapped) { EXPECT_EQ(*dr, 43 * M_PI); } +namespace { + +void writeStringToFileOrDie(const std::string& str, int fd) { + const char* b = str.c_str(); + size_t count = str.size(); + ssize_t total_bytes = 0; + ssize_t r; + do { + r = write(fd, b, count); + if (r == -1) { + if (errno == EINTR) { + continue; + } + PCHECK(r) << "write"; + } + + total_bytes += r; + b += r; + count -= r; + } while (r != 0 && count); +} + +} // anonymous namespace + +TEST(MemoryMapping, Simple) { + File f = File::temporary(); + writeStringToFileOrDie("hello", f.fd()); + + { + MemoryMapping m(f.fd()); + EXPECT_EQ("hello", m.data()); + } + { + MemoryMapping m(f.fd(), 1, 2); + EXPECT_EQ("el", m.data()); + } +} + +TEST(MemoryMapping, LargeFile) { + std::string fileData; + size_t fileSize = sysconf(_SC_PAGESIZE) * 3 + 10; + fileData.reserve(fileSize); + for (size_t i = 0; i < fileSize; i++) { + fileData.push_back(0xff & random()); + } + + File f = File::temporary(); + writeStringToFileOrDie(fileData, f.fd()); + + { + MemoryMapping m(f.fd()); + EXPECT_EQ(fileData, m.data()); + } + { + size_t size = sysconf(_SC_PAGESIZE) * 2; + StringPiece s(fileData.data() + 9, size - 9); + MemoryMapping m(f.fd(), 9, size - 9); + EXPECT_EQ(s.toString(), m.data()); + } +} + +TEST(MemoryMapping, ZeroLength) { + File f = File::temporary(); + MemoryMapping m(f.fd()); + EXPECT_TRUE(m.mlock(MemoryMapping::LockMode::MUST_LOCK)); + EXPECT_TRUE(m.mlocked()); + EXPECT_EQ(0, m.data().size()); +} + } // namespace folly