Boost.Interprocess使用手册翻译之五:独立于映射地址的指针:offset_pt (Mapping Address Independent Pointer: offset_ptr)
来源:互联网 发布:淘宝日服苹果账号注册 编辑:程序博客网 时间:2024/04/28 20:01
五. 独立于映射地址的指针:offset_ptr
当创建共享内存和内存映射文件进行两个进程通信时,内存片段在各进程上能够被映射到不同的地址:
- #include<boost/interprocess/shared_memory_object.hpp>
- // ...
- using boost::interprocess;
- //Open a shared memory segment
- shared_memory_object shm_obj
- (open_only //open or create
- ,"shared_memory" //name
- ,read_only //read-only mode
- );
- //Map the whole shared memory
- mapped_region region
- ( shm //Memory-mappable object
- , read_write //Access mode
- );
- //This address can be different in each process
- void *addr = region.get_address();
这使得在映射区域上创建复杂对象很困难:一个位于某映射区域的C++类实例可能有一个指针指向位于同一映射区域的另一个对象。因为指针存储了一个绝对地址,因此除非所有进程在同样的地址上映射映射区域,放置该对象的进程的地址才有效。
为了在映射区域上模拟指针使用,使用者必须使用偏移量(两个对象间的距离)来代替绝对地址。两个对象在映射区域的偏移量对任何映射该映射区域的进程来说都是相同的,即使区域位于不同的基地址上。为方便使用偏移量,Boost.Interprocess提供了offset_ptr.
offset_ptr封装了所有必须的后台操作,进而提供了一个类似指针的接口。这个类的接口受到了Boost智能指针的启发,并且这种智能指针存储了两个指针地址间的偏移量(字节数距离)和它自己的指针。试想在一个通常的32位处理器下的结构:
- struct structure
- {
- int integer1; //The compiler places this at offset 0 in the structure
- offset_ptr<int> ptr; //The compiler places this at offset 4 in the structure
- int integer2; //The compiler places this at offset 8 in the structure
- };
- //...
- structure s;
- //Assign the address of "integer1" to "ptr".
- //"ptr" will store internally "-4":
- // (char*)&s.integer1 - (char*)&s.ptr;
- s.ptr = &s.integer1;
- //Assign the address of "integer2" to "ptr".
- //"ptr" will store internally "4":
- // (char*)&s.integer2 - (char*)&s.ptr;
- s.ptr = &s.integer2;
offset_ptr一个最大的问题是如何表示空指针。空指针不能使用偏移量来安全地表示,因为绝对地址0总是不在映射区域中。基于在各进程中内存片段能够被映射到不同基地址上的事实,地址0和offset_ptr在每个进程中的距离是不同的。
一些实现方案选择偏移量0(即:一个指向自己的offset_ptr)来代表空指针,但是这在许多情况下也是不行的,因为很多结构例如链表或STL容器中的节点都指向它们自己(例如,空容器中的尾节点),且需要0偏移值。一个替代方案是除了偏移量,还存储一个布尔值以指示是否指针为空。然而,这增大了这种指针的大小并且影响性能。
因此,offset_ptr定义偏移值1做为空指针,这意味着此类不能指向它自身this指针后的那个字节。
- using namespace boost::interprocess;
- offset_ptr<char> ptr;
- //Pointing to the next byte of it's own address
- //marks the smart pointer as null.
- ptr = (char*)&ptr + 1;
- //ptr is equal to null
- assert(!ptr);
- //This is the same as assigning the null value...
- ptr = 0;
- //ptr is also equal to null
- assert(!ptr);
在实际中,这种限制是不重要的,因为使用者几乎从不会去指向那个地址。
offset_ptr提供所有的类似于指针的操作,并且定义了random_access_iterator,因此它能被使用在需要随机访问迭代器的STL算法中,且能通过性状进行检测。更多关于此类成员变量和成员函数的详情,请参考offset_ptr reference
.
- Boost.Interprocess使用手册翻译之五:独立于映射地址的指针:offset_pt (Mapping Address Independent Pointer: offset_ptr)
- Boost.Interprocess使用手册翻译之五:独立于映射地址的指针:offset_pt (Mapping Address Independent Pointer: offset_ptr)
- Boost.Interprocess使用手册翻译之五:独立于映射地址的指针:offset_pt (Mapping Address Independent Pointer: offset_ptr)
- Boost.Interprocess使用手册翻译之五:独立于映射地址的指针:offset_pt (Mapping Address Independent Pointer: offset_ptr)
- Boost.Interprocess使用手册翻译之十一:所有权的智能指针(Ownership smart pointers)
- Boost.Interprocess使用手册翻译之十一:所有权的智能指针(Ownership smart pointers)
- Boost.Interprocess使用手册翻译之二:快速指南 (Quick Guide for the Impatient)
- Boost.Interprocess使用手册翻译之三:一些基本解释 (Some basic explanations)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- Boost.Interprocess使用手册翻译之六:同步机制(Synchronization mechanisms)
- Boost.Interprocess使用手册翻译之七:托管内存片段(Managed Memory Segments)
- Boost.Interprocess使用手册翻译之九:内存分配算法(Memory allocation algorithms)
- Boost.Interprocess使用手册翻译之十:直接输入输出流(iostream)格式化:vectorstream 和bufferstream
- Boost.Interprocess使用手册翻译之十二.架构和内部实现(Architecture and internals)
- Boost.Interprocess使用手册翻译之六:同步机制(Synchronization mechanisms) .
- Boost.Interprocess使用手册翻译之二:快速指南 (Quick Guide for the Impatient)
- Boost.Interprocess使用手册翻译之三:一些基本解释 (Some basic explanations)
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- IOS开发(45)之delegate和Notification的区别
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- 用INNO SETUP做安装程序时,如何自动注册一个自己开发的DLL文件??
- ICE Manual(Documentation for Ice 3.5)---The Ice Run Time in Detail(ICE Stats)
- 关于用Inno Setup打包的问题 Flags: uninsneveruninstall
- Boost.Interprocess使用手册翻译之五:独立于映射地址的指针:offset_pt (Mapping Address Independent Pointer: offset_ptr)
- Orcale权限、角色
- Boost.Interprocess使用手册翻译之六:同步机制(Synchronization mechanisms)
- Android中从SD卡中/拍照选择图片并进行剪裁的方法
- Android WebView删除缓存
- NSIS(Nullsoft Scriptable Install System)是一个开源的 Windows 系统下安装程序制作程序
- java开发文章类
- UIView(包括子类)的几个初始化时执行动作的时机
- 从零开始学习OpenCL开发(二)一个最简单的示例与简单性能分析