, locked_(false) {
}
+MemoryMapping::MemoryMapping(MemoryMapping&& other)
+ : mapStart_(nullptr)
+ , mapLength_(0)
+ , pageSize_(0)
+ , locked_(false) {
+ swap(other);
+}
+
MemoryMapping::MemoryMapping(File file, off_t offset, off_t length,
off_t pageSize)
: mapStart_(nullptr)
}
}
+MemoryMapping& MemoryMapping::operator=(MemoryMapping other) {
+ swap(other);
+ return *this;
+}
+
+void MemoryMapping::swap(MemoryMapping& other) {
+ using std::swap;
+ swap(this->file_, other.file_);
+ swap(this->mapStart_, other.mapStart_);
+ swap(this->mapLength_, other.mapLength_);
+ swap(this->pageSize_, other.pageSize_);
+ swap(this->locked_, other.locked_);
+ swap(this->data_, other.data_);
+}
+
WritableMemoryMapping::WritableMemoryMapping(
File file, off_t offset, off_t length, off_t pageSize) {
init(std::move(file), offset, length, pageSize, PROT_READ | PROT_WRITE, true);
}
+void swap(MemoryMapping& a, MemoryMapping& b) { a.swap(b); }
+
} // namespace folly
off_t length=-1,
off_t pageSize=0);
+ MemoryMapping(MemoryMapping&&);
+
virtual ~MemoryMapping();
+ MemoryMapping& operator=(MemoryMapping);
+
+ void swap(MemoryMapping& other);
+
/**
* Lock the pages in memory
*/
* If dontneed is true, the kernel is instructed to release these pages
* (per madvise(MADV_DONTNEED)).
*/
- void munlock(bool dontneed=false);
+ void munlock(bool dontneed = false);
/**
* Hint that these pages will be scanned linearly.
}
};
+void swap(MemoryMapping&, MemoryMapping&);
+
} // namespace folly
#endif /* FOLLY_MEMORYMAPPING_H_ */
}
}
+TEST(MemoryMapping, Move) {
+ File f = File::temporary();
+ {
+ WritableMemoryMapping m(File(f.fd()), 0, sizeof(double) * 2);
+ double volatile* d = m.asWritableRange<double>().data();
+ d[0] = 37 * M_PI;
+ WritableMemoryMapping m2(std::move(m));
+ double volatile* d2 = m2.asWritableRange<double>().data();
+ d2[1] = 39 * M_PI;
+ }
+ {
+ MemoryMapping m(File(f.fd()), 0, sizeof(double));
+ const double volatile* d = m.asRange<double>().data();
+ EXPECT_EQ(d[0], 37 * M_PI);
+ MemoryMapping m2(std::move(m));
+ const double volatile* d2 = m2.asRange<double>().data();
+ EXPECT_EQ(d2[1], 39 * M_PI);
+ }
+}
+
TEST(MemoryMapping, DoublyMapped) {
File f = File::temporary();
// two mappings of the same memory, different addresses.