boost-interprocess共享内存源码学习-mapped_region

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; //返回页面大小,此大小是映射内存时的最小内存大小

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值