Class mapped_region
作用:
mapped_region类是用来将文件映射到内存中的。它提供了一种方便的方式来访问文件的内容,而无需将整个文件读入内存。这对于处理大型文件非常有用,因为它可以减少内存的使用量,并提高访问文件的速度。使用mapped_region类,您可以将文件的一部分映射到内存中,并像访问内存一样访问它。这使得对文件的读取和写入变得更加高效。
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
int main()
{
using namespace boost::interprocess;
// Open the file
file_mapping file("example.txt", read_write);
// Map the file to memory
mapped_region region(file, read_write);
// Get a pointer to the mapped memory
char* data = static_cast<char*>(region.get_address());
// Print the contents of the file
std::cout << data << std::endl;
return 0;
}
成员变量:
void* m_base; 映射内存区域起始地址
std::size_t m_size; 映射内存区域大小
std::size_t m_page_offset;
mode_t m_mode; 映射内存区域访问方式 read_only | read_write | copy_on_write | read_private | invalid_mode
#if defined(BOOST_INTERPROCESS_WINDOWS)
file_handle_t m_file_or_mapping_hnd;
#else
bool m_is_xsi; 是否为XSI进程间共享内存
#endif
公有成员函数及用法:
1、mapped_region &operator=(BOOST_RV_REF(mapped_region) other) BOOST_NOEXCEPT 赋值
2、void swap(mapped_region &other) BOOST_NOEXCEPT;
3 、std::size_t get_size() const BOOST_NOEXCEPT;
4、 void* get_address() const BOOST_NOEXCEPT;
5、 mode_t get_mode() const BOOST_NOEXCEPT;
6、bool flush(std::size_t mapping_offset = 0, std::size_t numbytes = 0, bool async = true); //async用来控制同步异步返回
7、bool shrink_by(std::size_t bytes, bool from_back = true); //缩小内存映射区域的大小、from_back控制是否从后面缩小
8、static std::size_t get_page_size() BOOST_NOEXCEPT; //返回页面大小,此大小是映射内存时的最小内存大小