intersegment_ptr.hpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_INTERSEGMENT_PTR_HPP
  11. #define BOOST_INTERPROCESS_INTERSEGMENT_PTR_HPP
  12. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/interprocess_fwd.hpp>
  18. #include <boost/interprocess/detail/utilities.hpp>
  19. #include <boost/interprocess/detail/math_functions.hpp>
  20. #include <boost/interprocess/detail/cast_tags.hpp>
  21. #include <boost/assert.hpp>
  22. #include <boost/interprocess/sync/scoped_lock.hpp>
  23. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  24. #include <boost/interprocess/containers/flat_map.hpp>
  25. #include <boost/interprocess/containers/vector.hpp> //vector
  26. #include <boost/interprocess/containers/set.hpp> //set
  27. #include <boost/detail/no_exceptions_support.hpp>
  28. #include <boost/interprocess/detail/mpl.hpp>
  29. #include <climits>
  30. #include <iterator>
  31. #include <boost/static_assert.hpp> //BOOST_STATIC_ASSERT
  32. #include <climits> //CHAR_BIT
  33. #include <boost/integer/static_log2.hpp>
  34. #include <boost/assert.hpp> //BOOST_ASSERT
  35. #include <boost/interprocess/detail/multi_segment_services.hpp>
  36. //!\file
  37. //!
  38. namespace boost {
  39. //Predeclarations
  40. template <class T>
  41. struct has_trivial_constructor;
  42. template <class T>
  43. struct has_trivial_destructor;
  44. namespace interprocess {
  45. template <class T>
  46. struct is_multisegment_ptr;
  47. struct intersegment_base
  48. {
  49. typedef intersegment_base self_t;
  50. BOOST_STATIC_ASSERT((sizeof(std::size_t) == sizeof(void*)));
  51. BOOST_STATIC_ASSERT((sizeof(void*)*CHAR_BIT == 32 || sizeof(void*)*CHAR_BIT == 64));
  52. static const std::size_t size_t_bits = (sizeof(void*)*CHAR_BIT == 32) ? 32 : 64;
  53. static const std::size_t ctrl_bits = 2;
  54. static const std::size_t align_bits = 12;
  55. static const std::size_t align = std::size_t(1) << align_bits;
  56. static const std::size_t max_segment_size_bits = size_t_bits - 2;
  57. static const std::size_t max_segment_size = std::size_t(1) << max_segment_size_bits;
  58. static const std::size_t begin_bits = max_segment_size_bits - align_bits;
  59. static const std::size_t pow_size_bits_helper = static_log2<max_segment_size_bits>::value;
  60. static const std::size_t pow_size_bits =
  61. (max_segment_size_bits == (std::size_t(1) << pow_size_bits_helper)) ?
  62. pow_size_bits_helper : pow_size_bits_helper + 1;
  63. static const std::size_t frc_size_bits =
  64. size_t_bits - ctrl_bits - begin_bits - pow_size_bits;
  65. BOOST_STATIC_ASSERT(((size_t_bits - pow_size_bits - frc_size_bits) >= ctrl_bits ));
  66. static const std::size_t relative_size_bits =
  67. size_t_bits - max_segment_size_bits - ctrl_bits;
  68. static const std::size_t is_pointee_outside = 0;
  69. static const std::size_t is_in_stack = 1;
  70. static const std::size_t is_relative = 2;
  71. static const std::size_t is_segmented = 3;
  72. static const std::size_t is_max_mode = 4;
  73. intersegment_base()
  74. {
  75. this->set_mode(is_pointee_outside);
  76. this->set_null();
  77. }
  78. struct relative_addressing
  79. {
  80. std::size_t ctrl : 2;
  81. std::size_t pow : pow_size_bits;
  82. std::size_t frc : frc_size_bits;
  83. std::size_t beg : begin_bits;
  84. std::ptrdiff_t off : sizeof(std::ptrdiff_t)*CHAR_BIT - 2;
  85. std::ptrdiff_t bits : 2;
  86. };
  87. struct direct_addressing
  88. {
  89. std::size_t ctrl : 2;
  90. std::size_t dummy : sizeof(std::size_t)*CHAR_BIT - 2;
  91. void * addr;
  92. };
  93. struct segmented_addressing
  94. {
  95. std::size_t ctrl : 2;
  96. std::size_t segment : sizeof(std::size_t)*CHAR_BIT - 2;
  97. std::size_t off : sizeof(std::size_t)*CHAR_BIT - 2;
  98. std::size_t bits : 2;
  99. };
  100. union members_t{
  101. relative_addressing relative;
  102. direct_addressing direct;
  103. segmented_addressing segmented;
  104. } members;
  105. BOOST_STATIC_ASSERT(sizeof(members_t) == 2*sizeof(std::size_t));
  106. void *relative_calculate_begin_addr() const
  107. {
  108. const std::size_t mask = ~(align - 1);
  109. std::size_t beg = this->members.relative.beg;
  110. return reinterpret_cast<void*>((((std::size_t)this) & mask) - (beg << align_bits));
  111. }
  112. void relative_set_begin_from_base(void *addr)
  113. {
  114. BOOST_ASSERT(addr < static_cast<void*>(this));
  115. std::size_t off = reinterpret_cast<char*>(this) - reinterpret_cast<char*>(addr);
  116. members.relative.beg = off >> align_bits;
  117. }
  118. //!Obtains the address pointed by the
  119. //!object
  120. std::size_t relative_size() const
  121. {
  122. std::size_t pow = members.relative.pow;
  123. std::size_t size = (std::size_t(1u) << pow);
  124. BOOST_ASSERT(pow >= frc_size_bits);
  125. size |= members.relative.frc << (pow - frc_size_bits);
  126. return size;
  127. }
  128. static std::size_t calculate_size(std::size_t orig_size, std::size_t &pow, std::size_t &frc)
  129. {
  130. if(orig_size < align)
  131. orig_size = align;
  132. orig_size = ipcdetail::get_rounded_size_po2(orig_size, align);
  133. pow = ipcdetail::floor_log2(orig_size);
  134. std::size_t low_size = (std::size_t(1) << pow);
  135. std::size_t diff = orig_size - low_size;
  136. BOOST_ASSERT(pow >= frc_size_bits);
  137. std::size_t rounded = ipcdetail::get_rounded_size_po2
  138. (diff, (std::size_t)(1u << (pow - frc_size_bits)));
  139. if(rounded == low_size){
  140. ++pow;
  141. frc = 0;
  142. rounded = 0;
  143. }
  144. else{
  145. frc = rounded >> (pow - frc_size_bits);
  146. }
  147. BOOST_ASSERT(((frc << (pow - frc_size_bits)) & (align-1))==0);
  148. return low_size + rounded;
  149. }
  150. std::size_t get_mode()const
  151. { return members.direct.ctrl; }
  152. void set_mode(std::size_t mode)
  153. {
  154. BOOST_ASSERT(mode < is_max_mode);
  155. members.direct.ctrl = mode;
  156. }
  157. //!Returns true if object represents
  158. //!null pointer
  159. bool is_null() const
  160. {
  161. return (this->get_mode() < is_relative) &&
  162. !members.direct.dummy &&
  163. !members.direct.addr;
  164. }
  165. //!Sets the object to represent
  166. //!the null pointer
  167. void set_null()
  168. {
  169. if(this->get_mode() >= is_relative){
  170. this->set_mode(is_pointee_outside);
  171. }
  172. members.direct.dummy = 0;
  173. members.direct.addr = 0;
  174. }
  175. static std::size_t round_size(std::size_t orig_size)
  176. {
  177. std::size_t pow, frc;
  178. return calculate_size(orig_size, pow, frc);
  179. }
  180. };
  181. //!Configures intersegment_ptr with the capability to address:
  182. //!2^(sizeof(std::size_t)*CHAR_BIT/2) segment groups
  183. //!2^(sizeof(std::size_t)*CHAR_BIT/2) segments per group.
  184. //!2^(sizeof(std::size_t)*CHAR_BIT/2)-1 bytes maximum per segment.
  185. //!The mapping is implemented through flat_maps synchronized with mutexes.
  186. template <class Mutex>
  187. struct flat_map_intersegment
  188. : public intersegment_base
  189. {
  190. typedef flat_map_intersegment<Mutex> self_t;
  191. void set_from_pointer(const volatile void *ptr)
  192. { this->set_from_pointer(const_cast<const void *>(ptr)); }
  193. //!Obtains the address pointed
  194. //!by the object
  195. void *to_raw_pointer() const
  196. {
  197. if(is_null()){
  198. return 0;
  199. }
  200. switch(this->get_mode()){
  201. case is_relative:
  202. return const_cast<char*>(reinterpret_cast<const char*>(this)) + members.relative.off;
  203. break;
  204. case is_segmented:
  205. {
  206. segment_info_t segment_info;
  207. std::size_t offset;
  208. void *this_base;
  209. get_segment_info_and_offset(this, segment_info, offset, this_base);
  210. char *base = static_cast<char*>(segment_info.group->address_of(this->members.segmented.segment));
  211. return base + this->members.segmented.off;
  212. }
  213. break;
  214. case is_in_stack:
  215. case is_pointee_outside:
  216. return members.direct.addr;
  217. break;
  218. default:
  219. return 0;
  220. break;
  221. }
  222. }
  223. //!Calculates the distance between two basic_intersegment_ptr-s.
  224. //!This only works with two basic_intersegment_ptr pointing
  225. //!to the same segment. Otherwise undefined
  226. std::ptrdiff_t diff(const self_t &other) const
  227. { return static_cast<char*>(this->to_raw_pointer()) - static_cast<char*>(other.to_raw_pointer()); }
  228. //!Returns true if both point to
  229. //!the same object
  230. bool equal(const self_t &y) const
  231. { return this->to_raw_pointer() == y.to_raw_pointer(); }
  232. //!Returns true if *this is less than other.
  233. //!This only works with two basic_intersegment_ptr pointing
  234. //!to the same segment group. Otherwise undefined. Never throws
  235. bool less(const self_t &y) const
  236. { return this->to_raw_pointer() < y.to_raw_pointer(); }
  237. void swap(self_t &other)
  238. {
  239. void *ptr_this = this->to_raw_pointer();
  240. void *ptr_other = other.to_raw_pointer();
  241. other.set_from_pointer(ptr_this);
  242. this->set_from_pointer(ptr_other);
  243. }
  244. //!Sets the object internals to represent the
  245. //!address pointed by ptr
  246. void set_from_pointer(const void *ptr)
  247. {
  248. if(!ptr){
  249. this->set_null();
  250. return;
  251. }
  252. std::size_t mode = this->get_mode();
  253. if(mode == is_in_stack){
  254. members.direct.addr = const_cast<void*>(ptr);
  255. return;
  256. }
  257. if(mode == is_relative){
  258. char *beg_addr = static_cast<char*>(this->relative_calculate_begin_addr());
  259. std::size_t seg_size = this->relative_size();
  260. if(ptr >= beg_addr && ptr < (beg_addr + seg_size)){
  261. members.relative.off = static_cast<const char*>(ptr) - reinterpret_cast<const char*>(this);
  262. return;
  263. }
  264. }
  265. std::size_t ptr_offset;
  266. std::size_t this_offset;
  267. segment_info_t ptr_info;
  268. segment_info_t this_info;
  269. void *ptr_base;
  270. void *this_base;
  271. get_segment_info_and_offset(this, this_info, this_offset, this_base);
  272. if(!this_info.group){
  273. this->set_mode(is_in_stack);
  274. this->members.direct.addr = const_cast<void*>(ptr);
  275. }
  276. else{
  277. get_segment_info_and_offset(ptr, ptr_info, ptr_offset, ptr_base);
  278. if(ptr_info.group != this_info.group){
  279. this->set_mode(is_pointee_outside);
  280. this->members.direct.addr = const_cast<void*>(ptr);
  281. }
  282. else if(ptr_info.id == this_info.id){
  283. this->set_mode(is_relative);
  284. members.relative.off = (static_cast<const char*>(ptr) - reinterpret_cast<const char*>(this));
  285. this->relative_set_begin_from_base(this_base);
  286. std::size_t pow, frc;
  287. std::size_t s = calculate_size(this_info.size, pow, frc);
  288. (void)s;
  289. BOOST_ASSERT(this_info.size == s);
  290. this->members.relative.pow = pow;
  291. this->members.relative.frc = frc;
  292. }
  293. else{
  294. this->set_mode(is_segmented);
  295. this->members.segmented.segment = ptr_info.id;
  296. this->members.segmented.off = ptr_offset;
  297. }
  298. }
  299. }
  300. //!Sets the object internals to represent the address pointed
  301. //!by another flat_map_intersegment
  302. void set_from_other(const self_t &other)
  303. {
  304. this->set_from_pointer(other.to_raw_pointer());
  305. }
  306. //!Increments internal
  307. //!offset
  308. void inc_offset(std::ptrdiff_t bytes)
  309. {
  310. this->set_from_pointer(static_cast<char*>(this->to_raw_pointer()) + bytes);
  311. }
  312. //!Decrements internal
  313. //!offset
  314. void dec_offset(std::ptrdiff_t bytes)
  315. {
  316. this->set_from_pointer(static_cast<char*>(this->to_raw_pointer()) - bytes);
  317. }
  318. //////////////////////////////////////
  319. //////////////////////////////////////
  320. //////////////////////////////////////
  321. flat_map_intersegment()
  322. : intersegment_base()
  323. {}
  324. ~flat_map_intersegment()
  325. {}
  326. private:
  327. class segment_group_t
  328. {
  329. struct segment_data
  330. {
  331. void *addr;
  332. std::size_t size;
  333. };
  334. vector<segment_data> m_segments;
  335. multi_segment_services &m_ms_services;
  336. public:
  337. segment_group_t(multi_segment_services &ms_services)
  338. : m_ms_services(ms_services)
  339. {}
  340. void push_back(void *addr, std::size_t size)
  341. {
  342. segment_data d = { addr, size };
  343. m_segments.push_back(d);
  344. }
  345. void pop_back()
  346. {
  347. BOOST_ASSERT(!m_segments.empty());
  348. m_segments.erase(--m_segments.end());
  349. }
  350. void *address_of(std::size_t segment_id)
  351. {
  352. BOOST_ASSERT(segment_id < (std::size_t)m_segments.size());
  353. return m_segments[segment_id].addr;
  354. }
  355. void clear_segments()
  356. { m_segments.clear(); }
  357. std::size_t get_size() const
  358. { return m_segments.size(); }
  359. multi_segment_services &get_multi_segment_services() const
  360. { return m_ms_services; }
  361. friend bool operator< (const segment_group_t&l, const segment_group_t &r)
  362. { return &l.m_ms_services < &r.m_ms_services; }
  363. };
  364. struct segment_info_t
  365. {
  366. std::size_t size;
  367. std::size_t id;
  368. segment_group_t *group;
  369. segment_info_t()
  370. : size(0), id(0), group(0)
  371. {}
  372. };
  373. typedef set<segment_group_t> segment_groups_t;
  374. typedef boost::interprocess::flat_map
  375. <const void *
  376. ,segment_info_t
  377. ,std::less<const void *> > ptr_to_segment_info_t;
  378. struct mappings_t : Mutex
  379. {
  380. //!Mutex to preserve integrity in multi-threaded
  381. //!enviroments
  382. typedef Mutex mutex_type;
  383. //!Maps base addresses and segment information
  384. //!(size and segment group and id)*
  385. ptr_to_segment_info_t m_ptr_to_segment_info;
  386. ~mappings_t()
  387. {
  388. //Check that all mappings have been erased
  389. BOOST_ASSERT(m_ptr_to_segment_info.empty());
  390. }
  391. };
  392. //Static members
  393. static mappings_t s_map;
  394. static segment_groups_t s_groups;
  395. public:
  396. typedef segment_group_t* segment_group_id;
  397. //!Returns the segment and offset
  398. //!of an address
  399. static void get_segment_info_and_offset(const void *ptr, segment_info_t &segment, std::size_t &offset, void *&base)
  400. {
  401. //------------------------------------------------------------------
  402. boost::interprocess::scoped_lock<typename mappings_t::mutex_type> lock(s_map);
  403. //------------------------------------------------------------------
  404. base = 0;
  405. if(s_map.m_ptr_to_segment_info.empty()){
  406. segment = segment_info_t();
  407. offset = reinterpret_cast<const char*>(ptr) - static_cast<const char*>(0);
  408. return;
  409. }
  410. //Find the first base address greater than ptr
  411. typename ptr_to_segment_info_t::iterator it
  412. = s_map.m_ptr_to_segment_info.upper_bound(ptr);
  413. if(it == s_map.m_ptr_to_segment_info.begin()){
  414. segment = segment_info_t();
  415. offset = reinterpret_cast<const char*>(ptr) - static_cast<const char *>(0);
  416. }
  417. //Go to the previous one
  418. --it;
  419. char * segment_base = const_cast<char*>(reinterpret_cast<const char*>(it->first));
  420. std::size_t segment_size = it->second.size;
  421. if(segment_base <= reinterpret_cast<const char*>(ptr) &&
  422. (segment_base + segment_size) >= reinterpret_cast<const char*>(ptr)){
  423. segment = it->second;
  424. offset = reinterpret_cast<const char*>(ptr) - segment_base;
  425. base = segment_base;
  426. }
  427. else{
  428. segment = segment_info_t();
  429. offset = reinterpret_cast<const char*>(ptr) - static_cast<const char*>(0);
  430. }
  431. }
  432. //!Associates a segment defined by group/id with a base address and size.
  433. //!Returns false if the group is not found or there is an error
  434. static void insert_mapping(segment_group_id group_id, void *ptr, std::size_t size)
  435. {
  436. //------------------------------------------------------------------
  437. boost::interprocess::scoped_lock<typename mappings_t::mutex_type> lock(s_map);
  438. //------------------------------------------------------------------
  439. typedef typename ptr_to_segment_info_t::value_type value_type;
  440. typedef typename ptr_to_segment_info_t::iterator iterator;
  441. typedef std::pair<iterator, bool> it_b_t;
  442. segment_info_t info;
  443. info.group = group_id;
  444. info.size = size;
  445. info.id = group_id->get_size();
  446. it_b_t ret = s_map.m_ptr_to_segment_info.insert(value_type(ptr, info));
  447. BOOST_ASSERT(ret.second);
  448. value_eraser<ptr_to_segment_info_t> v_eraser(s_map.m_ptr_to_segment_info, ret.first);
  449. group_id->push_back(ptr, size);
  450. v_eraser.release();
  451. }
  452. static bool erase_last_mapping(segment_group_id group_id)
  453. {
  454. //------------------------------------------------------------------
  455. boost::interprocess::scoped_lock<typename mappings_t::mutex_type> lock(s_map);
  456. //------------------------------------------------------------------
  457. if(!group_id->get_size()){
  458. return false;
  459. }
  460. else{
  461. void *addr = group_id->address_of(group_id->get_size()-1);
  462. group_id->pop_back();
  463. std::size_t erased = s_map.m_ptr_to_segment_info.erase(addr);
  464. (void)erased;
  465. BOOST_ASSERT(erased);
  466. return true;
  467. }
  468. }
  469. static segment_group_id new_segment_group(multi_segment_services *services)
  470. {
  471. { //------------------------------------------------------------------
  472. boost::interprocess::scoped_lock<typename mappings_t::mutex_type> lock(s_map);
  473. //------------------------------------------------------------------
  474. typedef typename segment_groups_t::iterator iterator;
  475. std::pair<iterator, bool> ret =
  476. s_groups.insert(segment_group_t(*services));
  477. BOOST_ASSERT(ret.second);
  478. return &*ret.first;
  479. }
  480. }
  481. static bool delete_group(segment_group_id id)
  482. {
  483. { //------------------------------------------------------------------
  484. boost::interprocess::scoped_lock<typename mappings_t::mutex_type> lock(s_map);
  485. //------------------------------------------------------------------
  486. bool success = 1u == s_groups.erase(segment_group_t(*id));
  487. if(success){
  488. typedef typename ptr_to_segment_info_t::iterator ptr_to_segment_info_it;
  489. ptr_to_segment_info_it it(s_map.m_ptr_to_segment_info.begin());
  490. while(it != s_map.m_ptr_to_segment_info.end()){
  491. if(it->second.group == id){
  492. it = s_map.m_ptr_to_segment_info.erase(it);
  493. }
  494. else{
  495. ++it;
  496. }
  497. }
  498. }
  499. return success;
  500. }
  501. }
  502. };
  503. //!Static map-segment_info associated with
  504. //!flat_map_intersegment<>
  505. template <class Mutex>
  506. typename flat_map_intersegment<Mutex>::mappings_t
  507. flat_map_intersegment<Mutex>::s_map;
  508. //!Static segment group container associated with
  509. //!flat_map_intersegment<>
  510. template <class Mutex>
  511. typename flat_map_intersegment<Mutex>::segment_groups_t
  512. flat_map_intersegment<Mutex>::s_groups;
  513. //!A smart pointer that can point to a pointee that resides in another memory
  514. //!memory mapped or shared memory segment.
  515. template <class T>
  516. class intersegment_ptr : public flat_map_intersegment<interprocess_mutex>
  517. {
  518. typedef flat_map_intersegment<interprocess_mutex> PT;
  519. typedef intersegment_ptr<T> self_t;
  520. typedef PT base_t;
  521. void unspecified_bool_type_func() const {}
  522. typedef void (self_t::*unspecified_bool_type)() const;
  523. public:
  524. typedef T * pointer;
  525. typedef typename ipcdetail::add_reference<T>::type reference;
  526. typedef T value_type;
  527. typedef std::ptrdiff_t difference_type;
  528. typedef std::random_access_iterator_tag iterator_category;
  529. public: //Public Functions
  530. //!Constructor from raw pointer (allows "0" pointer conversion).
  531. //!Never throws.
  532. intersegment_ptr(pointer ptr = 0)
  533. { base_t::set_from_pointer(ptr); }
  534. //!Constructor from other pointer.
  535. //!Never throws.
  536. template <class U>
  537. intersegment_ptr(U *ptr){ base_t::set_from_pointer(pointer(ptr)); }
  538. //!Constructor from other intersegment_ptr
  539. //!Never throws
  540. intersegment_ptr(const intersegment_ptr& ptr)
  541. { base_t::set_from_other(ptr); }
  542. //!Constructor from other intersegment_ptr. If pointers of pointee types are
  543. //!convertible, intersegment_ptrs will be convertibles. Never throws.
  544. template<class T2>
  545. intersegment_ptr(const intersegment_ptr<T2> &ptr)
  546. { pointer p(ptr.get()); (void)p; base_t::set_from_other(ptr); }
  547. //!Emulates static_cast operator.
  548. //!Never throws.
  549. template<class U>
  550. intersegment_ptr(const intersegment_ptr<U> &r, ipcdetail::static_cast_tag)
  551. { base_t::set_from_pointer(static_cast<T*>(r.get())); }
  552. //!Emulates const_cast operator.
  553. //!Never throws.
  554. template<class U>
  555. intersegment_ptr(const intersegment_ptr<U> &r, ipcdetail::const_cast_tag)
  556. { base_t::set_from_pointer(const_cast<T*>(r.get())); }
  557. //!Emulates dynamic_cast operator.
  558. //!Never throws.
  559. template<class U>
  560. intersegment_ptr(const intersegment_ptr<U> &r, ipcdetail::dynamic_cast_tag)
  561. { base_t::set_from_pointer(dynamic_cast<T*>(r.get())); }
  562. //!Emulates reinterpret_cast operator.
  563. //!Never throws.
  564. template<class U>
  565. intersegment_ptr(const intersegment_ptr<U> &r, ipcdetail::reinterpret_cast_tag)
  566. { base_t::set_from_pointer(reinterpret_cast<T*>(r.get())); }
  567. //!Obtains raw pointer from offset.
  568. //!Never throws.
  569. pointer get()const
  570. { return static_cast<pointer>(base_t::to_raw_pointer()); }
  571. //!Pointer-like -> operator. It can return 0 pointer.
  572. //!Never throws.
  573. pointer operator->() const
  574. { return self_t::get(); }
  575. //!Dereferencing operator, if it is a null intersegment_ptr behavior
  576. //!is undefined. Never throws.
  577. reference operator* () const
  578. { return *(self_t::get()); }
  579. //!Indexing operator.
  580. //!Never throws.
  581. reference operator[](std::ptrdiff_t idx) const
  582. { return self_t::get()[idx]; }
  583. //!Assignment from pointer (saves extra conversion).
  584. //!Never throws.
  585. intersegment_ptr& operator= (pointer from)
  586. { base_t::set_from_pointer(from); return *this; }
  587. //!Assignment from other intersegment_ptr.
  588. //!Never throws.
  589. intersegment_ptr& operator= (const intersegment_ptr &ptr)
  590. { base_t::set_from_other(ptr); return *this; }
  591. //!Assignment from related intersegment_ptr. If pointers of pointee types
  592. //!are assignable, intersegment_ptrs will be assignable. Never throws.
  593. template <class T2>
  594. intersegment_ptr& operator= (const intersegment_ptr<T2> & ptr)
  595. {
  596. pointer p(ptr.get()); (void)p;
  597. base_t::set_from_other(ptr); return *this;
  598. }
  599. //!intersegment_ptr + std::ptrdiff_t.
  600. //!Never throws.
  601. intersegment_ptr operator+ (std::ptrdiff_t idx) const
  602. {
  603. intersegment_ptr result (*this);
  604. result.inc_offset(idx*sizeof(T));
  605. return result;
  606. }
  607. //!intersegment_ptr - std::ptrdiff_t.
  608. //!Never throws.
  609. intersegment_ptr operator- (std::ptrdiff_t idx) const
  610. {
  611. intersegment_ptr result (*this);
  612. result.dec_offset(idx*sizeof(T));
  613. return result;
  614. }
  615. //!intersegment_ptr += std::ptrdiff_t.
  616. //!Never throws.
  617. intersegment_ptr &operator+= (std::ptrdiff_t offset)
  618. { base_t::inc_offset(offset*sizeof(T)); return *this; }
  619. //!intersegment_ptr -= std::ptrdiff_t.
  620. //!Never throws.
  621. intersegment_ptr &operator-= (std::ptrdiff_t offset)
  622. { base_t::dec_offset(offset*sizeof(T)); return *this; }
  623. //!++intersegment_ptr.
  624. //!Never throws.
  625. intersegment_ptr& operator++ (void)
  626. { base_t::inc_offset(sizeof(T)); return *this; }
  627. //!intersegment_ptr++.
  628. //!Never throws.
  629. intersegment_ptr operator++ (int)
  630. { intersegment_ptr temp(*this); ++*this; return temp; }
  631. //!--intersegment_ptr.
  632. //!Never throws.
  633. intersegment_ptr& operator-- (void)
  634. { base_t::dec_offset(sizeof(T)); return *this; }
  635. //!intersegment_ptr--.
  636. //!Never throws.
  637. intersegment_ptr operator-- (int)
  638. { intersegment_ptr temp(*this); --*this; return temp; }
  639. //!Safe bool conversion operator.
  640. //!Never throws.
  641. operator unspecified_bool_type() const
  642. { return base_t::is_null()? 0 : &self_t::unspecified_bool_type_func; }
  643. //!Not operator. Not needed in theory, but improves portability.
  644. //!Never throws.
  645. bool operator! () const
  646. { return base_t::is_null(); }
  647. //!Swaps two intersegment_ptr-s. More efficient than std::swap.
  648. //!Never throws.
  649. void swap(intersegment_ptr &other)
  650. { base_t::swap(other); }
  651. //!Calculates the distance between two intersegment_ptr-s.
  652. //!This only works with two basic_intersegment_ptr pointing
  653. //!to the same segment. Otherwise undefined
  654. template <class T2>
  655. std::ptrdiff_t _diff(const intersegment_ptr<T2> &other) const
  656. { return base_t::diff(other); }
  657. //!Returns true if both point to the
  658. //!same object
  659. template <class T2>
  660. bool _equal(const intersegment_ptr<T2>&other) const
  661. { return base_t::equal(other); }
  662. //!Returns true if *this is less than other.
  663. //!This only works with two basic_intersegment_ptr pointing
  664. //!to the same segment group. Otherwise undefined. Never throws
  665. template <class T2>
  666. bool _less(const intersegment_ptr<T2> &other) const
  667. { return base_t::less(other); }
  668. };
  669. //!Compares the equality of two intersegment_ptr-s.
  670. //!Never throws.
  671. template <class T1, class T2> inline
  672. bool operator ==(const intersegment_ptr<T1> &left,
  673. const intersegment_ptr<T2> &right)
  674. {
  675. //Make sure both pointers can be compared
  676. bool e = typename intersegment_ptr<T1>::pointer(0) ==
  677. typename intersegment_ptr<T2>::pointer(0);
  678. (void)e;
  679. return left._equal(right);
  680. }
  681. //!Returns true if *this is less than other.
  682. //!This only works with two basic_intersegment_ptr pointing
  683. //!to the same segment group. Otherwise undefined. Never throws
  684. template <class T1, class T2> inline
  685. bool operator <(const intersegment_ptr<T1> &left,
  686. const intersegment_ptr<T2> &right)
  687. {
  688. //Make sure both pointers can be compared
  689. bool e = typename intersegment_ptr<T1>::pointer(0) <
  690. typename intersegment_ptr<T2>::pointer(0);
  691. (void)e;
  692. return left._less(right);
  693. }
  694. template<class T1, class T2> inline
  695. bool operator!= (const intersegment_ptr<T1> &pt1,
  696. const intersegment_ptr<T2> &pt2)
  697. { return !(pt1 ==pt2); }
  698. //!intersegment_ptr<T1> <= intersegment_ptr<T2>.
  699. //!Never throws.
  700. template<class T1, class T2> inline
  701. bool operator<= (const intersegment_ptr<T1> &pt1,
  702. const intersegment_ptr<T2> &pt2)
  703. { return !(pt1 > pt2); }
  704. //!intersegment_ptr<T1> > intersegment_ptr<T2>.
  705. //!Never throws.
  706. template<class T1, class T2> inline
  707. bool operator> (const intersegment_ptr<T1> &pt1,
  708. const intersegment_ptr<T2> &pt2)
  709. { return (pt2 < pt1); }
  710. //!intersegment_ptr<T1> >= intersegment_ptr<T2>.
  711. //!Never throws.
  712. template<class T1, class T2> inline
  713. bool operator>= (const intersegment_ptr<T1> &pt1,
  714. const intersegment_ptr<T2> &pt2)
  715. { return !(pt1 < pt2); }
  716. //!operator<<
  717. template<class E, class T, class U> inline
  718. std::basic_ostream<E, T> & operator<<
  719. (std::basic_ostream<E, T> & os, const intersegment_ptr<U> & p)
  720. { return os << p.get(); }
  721. //!operator>>
  722. template<class E, class T, class U> inline
  723. std::basic_istream<E, T> & operator>>
  724. (std::basic_istream<E, T> & os, intersegment_ptr<U> & p)
  725. { U * tmp; return os >> tmp; p = tmp; }
  726. //!std::ptrdiff_t + intersegment_ptr.
  727. //!The result is another pointer of the same segment
  728. template<class T> inline
  729. intersegment_ptr<T> operator+
  730. (std::ptrdiff_t diff, const intersegment_ptr<T>& right)
  731. { return right + diff; }
  732. //!intersegment_ptr - intersegment_ptr.
  733. //!This only works with two intersegment_ptr-s that point to the
  734. //!same segment
  735. template <class T, class T2> inline
  736. std::ptrdiff_t operator- (const intersegment_ptr<T> &pt,
  737. const intersegment_ptr<T2> &pt2)
  738. { return pt._diff(pt2)/sizeof(T); }
  739. //! swap specialization
  740. template<class T> inline
  741. void swap (boost::interprocess::intersegment_ptr<T> &pt,
  742. boost::interprocess::intersegment_ptr<T> &pt2)
  743. { pt.swap(pt2); }
  744. //!to_raw_pointer() enables boost::mem_fn to recognize intersegment_ptr.
  745. //!Never throws.
  746. template<class T> inline
  747. T * to_raw_pointer(boost::interprocess::intersegment_ptr<T> const & p)
  748. { return p.get(); }
  749. //!Simulation of static_cast between pointers.
  750. //!Never throws.
  751. template<class T, class U> inline
  752. boost::interprocess::intersegment_ptr<T> static_pointer_cast(const boost::interprocess::intersegment_ptr<U> &r)
  753. { return boost::interprocess::intersegment_ptr<T>(r, boost::interprocess::ipcdetail::static_cast_tag()); }
  754. //!Simulation of const_cast between pointers.
  755. //!Never throws.
  756. template<class T, class U> inline
  757. boost::interprocess::intersegment_ptr<T> const_pointer_cast(const boost::interprocess::intersegment_ptr<U> &r)
  758. { return boost::interprocess::intersegment_ptr<T>(r, boost::interprocess::ipcdetail::const_cast_tag()); }
  759. //!Simulation of dynamic_cast between pointers.
  760. //!Never throws.
  761. template<class T, class U> inline
  762. boost::interprocess::intersegment_ptr<T> dynamic_pointer_cast(const boost::interprocess::intersegment_ptr<U> &r)
  763. { return boost::interprocess::intersegment_ptr<T>(r, boost::interprocess::ipcdetail::dynamic_cast_tag()); }
  764. //!Simulation of reinterpret_cast between pointers.
  765. //!Never throws.
  766. template<class T, class U> inline
  767. boost::interprocess::intersegment_ptr<T> reinterpret_pointer_cast(const boost::interprocess::intersegment_ptr<U> &r)
  768. { return boost::interprocess::intersegment_ptr<T>(r, boost::interprocess::ipcdetail::reinterpret_cast_tag()); }
  769. //!Trait class to detect if an smart pointer has
  770. //!multi-segment addressing capabilities.
  771. template <class T>
  772. struct is_multisegment_ptr
  773. <boost::interprocess::intersegment_ptr<T> >
  774. {
  775. static const bool value = true;
  776. };
  777. } //namespace interprocess {
  778. #if defined(_MSC_VER) && (_MSC_VER < 1400)
  779. //!to_raw_pointer() enables boost::mem_fn to recognize intersegment_ptr.
  780. //!Never throws.
  781. template<class T> inline
  782. T * to_raw_pointer(boost::interprocess::intersegment_ptr<T> const & p)
  783. { return p.get(); }
  784. #endif
  785. //!has_trivial_constructor<> == true_type specialization
  786. //!for optimizations
  787. template <class T>
  788. struct has_trivial_constructor
  789. < boost::interprocess::intersegment_ptr<T> >
  790. : public true_type{};
  791. //!has_trivial_destructor<> == true_type specialization
  792. //!for optimizations
  793. template <class T>
  794. struct has_trivial_destructor
  795. < boost::interprocess::intersegment_ptr<T> >
  796. : public true_type{};
  797. } //namespace boost {
  798. #if 0
  799. //bits
  800. //-> is_segmented
  801. //-> is_relative
  802. //-> is_in_stack
  803. //-> is_pointee_outside
  804. //Data
  805. //segmented:
  806. //
  807. // std::size_t ctrl : CTRL_BITS;
  808. // std::size_t segment : MAX_SEGMENT_BITS;
  809. // std::size_t offset;
  810. //RELATIVE_SIZE_BITS = SIZE_T_BITS -
  811. // MAX_SEGMENT_BITS -
  812. // CTRL_BITS 10 10
  813. //MAX_SEGMENT_SIZE = SIZE_T_BITS - ALIGN_BITS 20 52
  814. //SIZE_T_BITS - 1 - ALIGN_BITS 19 51
  815. //POW_SIZE_BITS = upper_log2
  816. // (SIZE_T_BITS - 1 - ALIGN_BITS) 5 6
  817. //FRC_SIZE_BITS = SIZE_T_BITS - CTRL_BITS
  818. // MAX_SEGMENT_SIZE_ALIGNBITS - POW_SIZE_BITS 6 5
  819. //relative:
  820. //
  821. // std::size_t ctrl : CTRL_BITS; 2 2
  822. // std::size_t size_pow : POW_SIZE_BITS 5 6
  823. // std::size_t size_frc : FRC_SIZE_BITS; 6 5
  824. // std::size_t start : MAX_SEGMENT_SIZE_ALIGNBITS;19 51
  825. // std::ptrdiff_t distance : SIZE_T_BITS; 32 64
  826. //direct:
  827. //
  828. // std::size_t ctrl : CTRL_BITS; 2 2
  829. // std::size_t dummy : SIZE_T_BITS - CTRL_BITS 30 62
  830. // void *addr : SIZE_T_BITS; 32 64
  831. //32 bits systems:
  832. //Page alignment: 2**12
  833. //
  834. //!Obtains the address pointed by the
  835. //!object
  836. void *to_raw_pointer() const
  837. {
  838. if(this->is_pointee_outside() || this->is_in_stack()){
  839. return raw_address();
  840. }
  841. else if(this->is_relative()){
  842. return (const_cast<char*>(reinterpret_cast<const char*>(this))) + this->relative_pointee_offset();
  843. }
  844. else{
  845. group_manager *m = get_segment_group_manager(addr);
  846. char *base = static_cast<char*>(m->get_id_address(this->segmented_id()));
  847. return base + this->segmented_offset();
  848. }
  849. }
  850. void set_from_pointer(const void *ptr)
  851. {
  852. if(!ptr){
  853. this->set_pointee_outside();
  854. this->raw_address(ptr);
  855. }
  856. else if(this->is_in_stack()){
  857. this->raw_address(ptr);
  858. }
  859. else if(this->is_relative() &&
  860. ( (ptr >= this->relative_start())
  861. &&(ptr < this->relative_start() + this->relative_size()))
  862. ){
  863. this->relative_offset(ptr - this);
  864. }
  865. else{
  866. segment_info_t ptr_info = get_id_from_addr(ptr);
  867. segment_info_t this_info = get_id_from_addr(this);
  868. if(ptr_info.segment_group != this_info.segment_group){
  869. if(!ptr_info.segment_group){
  870. this->set_in_stack();
  871. }
  872. else{
  873. this->set_pointee_outside();
  874. }
  875. }
  876. else if(ptr_info.segment_id == this_info.segment_id){
  877. set_relative();
  878. this->relative_size (ptr_info.size);
  879. this->relative_offset(static_cast<const char*>(ptr) - reinterpret_cast<const char*>(this));
  880. this->relative_start (ptr_info.base);
  881. }
  882. }
  883. }
  884. void set_from_other(const self_t &other)
  885. { this->set_from_pointer(other.to_raw_pointer()); }
  886. #endif
  887. #include <boost/interprocess/detail/config_end.hpp>
  888. #endif //#ifndef BOOST_INTERPROCESS_INTERSEGMENT_PTR_HPP