*/
template<class T>
Range<const T*> asRange() const {
- CHECK(mapStart_);
size_t count = data_.size() / sizeof(T);
return Range<const T*>(static_cast<const T*>(
static_cast<const void*>(data_.data())),
/**
* Return the memory area where the file was mapped.
*/
- ByteRange data() const {
- return range();
+ StringPiece data() const {
+ return asRange<const char>();
}
bool mlocked() const {
return locked_;
}
+ int fd() const { return file_.fd(); }
+
protected:
MemoryMapping();
*/
template<class T>
Range<T*> asWritableRange() const {
- CHECK(mapStart_);
size_t count = data_.size() / sizeof(T);
return Range<T*>(static_cast<T*>(
static_cast<void*>(data_.data())),
* limitations under the License.
*/
+#include <cstdlib>
#include <gtest/gtest.h>
#include "folly/MemoryMapping.h"
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