safe_mode.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /* Copyright 2003-2020 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_SAFE_MODE_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_SAFE_MODE_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. /* Safe mode machinery, in the spirit of Cay Hortmann's "Safe STL"
  14. * (http://www.horstmann.com/safestl.html).
  15. * In this mode, containers of type Container are derived from
  16. * safe_container<Container>, and their corresponding iterators
  17. * are wrapped with safe_iterator. These classes provide
  18. * an internal record of which iterators are at a given moment associated
  19. * to a given container, and properly mark the iterators as invalid
  20. * when the container gets destroyed.
  21. * Iterators are chained in a single attached list, whose header is
  22. * kept by the container. More elaborate data structures would yield better
  23. * performance, but I decided to keep complexity to a minimum since
  24. * speed is not an issue here.
  25. * Safe mode iterators automatically check that only proper operations
  26. * are performed on them: for instance, an invalid iterator cannot be
  27. * dereferenced. Additionally, a set of utilty macros and functions are
  28. * provided that serve to implement preconditions and cooperate with
  29. * the framework within the container.
  30. * Iterators can also be unchecked, i.e. they do not have info about
  31. * which container they belong in. This situation arises when the iterator
  32. * is restored from a serialization archive: only information on the node
  33. * is available, and it is not possible to determine to which container
  34. * the iterator is associated to. The only sensible policy is to assume
  35. * unchecked iterators are valid, though this can certainly generate false
  36. * positive safe mode checks.
  37. * This is not a full-fledged safe mode framework, and is only intended
  38. * for use within the limits of Boost.MultiIndex.
  39. */
  40. /* Assertion macros. These resolve to no-ops if
  41. * !defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE).
  42. */
  43. #if !defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  44. #undef BOOST_MULTI_INDEX_SAFE_MODE_ASSERT
  45. #define BOOST_MULTI_INDEX_SAFE_MODE_ASSERT(expr,error_code) ((void)0)
  46. #else
  47. #if !defined(BOOST_MULTI_INDEX_SAFE_MODE_ASSERT)
  48. #include <boost/assert.hpp>
  49. #define BOOST_MULTI_INDEX_SAFE_MODE_ASSERT(expr,error_code) BOOST_ASSERT(expr)
  50. #endif
  51. #endif
  52. #define BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(it) \
  53. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  54. safe_mode::check_valid_iterator(it), \
  55. safe_mode::invalid_iterator);
  56. #define BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(it) \
  57. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  58. safe_mode::check_dereferenceable_iterator(it), \
  59. safe_mode::not_dereferenceable_iterator);
  60. #define BOOST_MULTI_INDEX_CHECK_INCREMENTABLE_ITERATOR(it) \
  61. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  62. safe_mode::check_incrementable_iterator(it), \
  63. safe_mode::not_incrementable_iterator);
  64. #define BOOST_MULTI_INDEX_CHECK_DECREMENTABLE_ITERATOR(it) \
  65. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  66. safe_mode::check_decrementable_iterator(it), \
  67. safe_mode::not_decrementable_iterator);
  68. #define BOOST_MULTI_INDEX_CHECK_IS_OWNER(it,cont) \
  69. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  70. safe_mode::check_is_owner(it,cont), \
  71. safe_mode::not_owner);
  72. #define BOOST_MULTI_INDEX_CHECK_SAME_OWNER(it0,it1) \
  73. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  74. safe_mode::check_same_owner(it0,it1), \
  75. safe_mode::not_same_owner);
  76. #define BOOST_MULTI_INDEX_CHECK_VALID_RANGE(it0,it1) \
  77. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  78. safe_mode::check_valid_range(it0,it1), \
  79. safe_mode::invalid_range);
  80. #define BOOST_MULTI_INDEX_CHECK_OUTSIDE_RANGE(it,it0,it1) \
  81. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  82. safe_mode::check_outside_range(it,it0,it1), \
  83. safe_mode::inside_range);
  84. #define BOOST_MULTI_INDEX_CHECK_IN_BOUNDS(it,n) \
  85. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  86. safe_mode::check_in_bounds(it,n), \
  87. safe_mode::out_of_bounds);
  88. #define BOOST_MULTI_INDEX_CHECK_DIFFERENT_CONTAINER(cont0,cont1) \
  89. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  90. safe_mode::check_different_container(cont0,cont1), \
  91. safe_mode::same_container);
  92. #define BOOST_MULTI_INDEX_CHECK_EQUAL_ALLOCATORS(cont0,cont1) \
  93. BOOST_MULTI_INDEX_SAFE_MODE_ASSERT( \
  94. safe_mode::check_equal_allocators(cont0,cont1), \
  95. safe_mode::unequal_allocators);
  96. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  97. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  98. #include <algorithm>
  99. #include <boost/multi_index/detail/access_specifier.hpp>
  100. #include <boost/multi_index/detail/iter_adaptor.hpp>
  101. #include <boost/multi_index/safe_mode_errors.hpp>
  102. #include <boost/noncopyable.hpp>
  103. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  104. #include <boost/serialization/split_member.hpp>
  105. #include <boost/serialization/version.hpp>
  106. #endif
  107. #if defined(BOOST_HAS_THREADS)
  108. #include <boost/detail/lightweight_mutex.hpp>
  109. #endif
  110. namespace boost{
  111. namespace multi_index{
  112. namespace safe_mode{
  113. /* Checking routines. Assume the best for unchecked iterators
  114. * (i.e. they pass the checking when there is not enough info
  115. * to know.)
  116. */
  117. template<typename Iterator>
  118. inline bool check_valid_iterator(const Iterator& it)
  119. {
  120. return it.valid()||it.unchecked();
  121. }
  122. template<typename Iterator>
  123. inline bool check_dereferenceable_iterator(const Iterator& it)
  124. {
  125. return (it.valid()&&it!=it.owner()->end())||it.unchecked();
  126. }
  127. template<typename Iterator>
  128. inline bool check_incrementable_iterator(const Iterator& it)
  129. {
  130. return (it.valid()&&it!=it.owner()->end())||it.unchecked();
  131. }
  132. template<typename Iterator>
  133. inline bool check_decrementable_iterator(const Iterator& it)
  134. {
  135. return (it.valid()&&it!=it.owner()->begin())||it.unchecked();
  136. }
  137. template<typename Iterator>
  138. inline bool check_is_owner(
  139. const Iterator& it,const typename Iterator::container_type& cont)
  140. {
  141. return (it.valid()&&it.owner()==&cont)||it.unchecked();
  142. }
  143. template<typename Iterator>
  144. inline bool check_same_owner(const Iterator& it0,const Iterator& it1)
  145. {
  146. return (it0.valid()&&it1.valid()&&it0.owner()==it1.owner())||
  147. it0.unchecked()||it1.unchecked();
  148. }
  149. template<typename Iterator>
  150. inline bool check_valid_range(const Iterator& it0,const Iterator& it1)
  151. {
  152. if(!check_same_owner(it0,it1))return false;
  153. if(it0.valid()){
  154. Iterator last=it0.owner()->end();
  155. if(it1==last)return true;
  156. for(Iterator first=it0;first!=last;++first){
  157. if(first==it1)return true;
  158. }
  159. return false;
  160. }
  161. return true;
  162. }
  163. template<typename Iterator>
  164. inline bool check_outside_range(
  165. const Iterator& it,const Iterator& it0,const Iterator& it1)
  166. {
  167. if(!check_same_owner(it0,it1))return false;
  168. if(it0.valid()){
  169. Iterator last=it0.owner()->end();
  170. bool found=false;
  171. Iterator first=it0;
  172. for(;first!=last;++first){
  173. if(first==it1)break;
  174. /* crucial that this check goes after previous break */
  175. if(first==it)found=true;
  176. }
  177. if(first!=it1)return false;
  178. return !found;
  179. }
  180. return true;
  181. }
  182. template<typename Iterator,typename Difference>
  183. inline bool check_in_bounds(const Iterator& it,Difference n)
  184. {
  185. if(it.unchecked())return true;
  186. if(!it.valid()) return false;
  187. if(n>0) return it.owner()->end()-it>=n;
  188. else return it.owner()->begin()-it<=n;
  189. }
  190. template<typename Container>
  191. inline bool check_different_container(
  192. const Container& cont0,const Container& cont1)
  193. {
  194. return &cont0!=&cont1;
  195. }
  196. template<typename Container0,typename Container1>
  197. inline bool check_equal_allocators(
  198. const Container0& cont0,const Container1& cont1)
  199. {
  200. return cont0.get_allocator()==cont1.get_allocator();
  201. }
  202. /* Invalidates all iterators equivalent to that given. Safe containers
  203. * must call this when deleting elements: the safe mode framework cannot
  204. * perform this operation automatically without outside help.
  205. */
  206. template<typename Iterator>
  207. inline void detach_equivalent_iterators(Iterator& it)
  208. {
  209. if(it.valid()){
  210. {
  211. #if defined(BOOST_HAS_THREADS)
  212. boost::detail::lightweight_mutex::scoped_lock lock(it.cont->mutex);
  213. #endif
  214. Iterator *prev_,*next_;
  215. for(
  216. prev_=static_cast<Iterator*>(&it.cont->header);
  217. (next_=static_cast<Iterator*>(prev_->next))!=0;){
  218. if(next_!=&it&&*next_==it){
  219. prev_->next=next_->next;
  220. next_->cont=0;
  221. }
  222. else prev_=next_;
  223. }
  224. }
  225. it.detach();
  226. }
  227. }
  228. template<typename Container> class safe_container; /* fwd decl. */
  229. } /* namespace multi_index::safe_mode */
  230. namespace detail{
  231. class safe_container_base; /* fwd decl. */
  232. class safe_iterator_base
  233. {
  234. public:
  235. bool valid()const{return cont!=0;}
  236. bool unchecked()const{return unchecked_;}
  237. inline void detach();
  238. void uncheck()
  239. {
  240. detach();
  241. unchecked_=true;
  242. }
  243. protected:
  244. safe_iterator_base():cont(0),next(0),unchecked_(false){}
  245. explicit safe_iterator_base(safe_container_base* cont_):
  246. unchecked_(false)
  247. {
  248. attach(cont_);
  249. }
  250. safe_iterator_base(const safe_iterator_base& it):
  251. unchecked_(it.unchecked_)
  252. {
  253. attach(it.cont);
  254. }
  255. safe_iterator_base& operator=(const safe_iterator_base& it)
  256. {
  257. unchecked_=it.unchecked_;
  258. safe_container_base* new_cont=it.cont;
  259. if(cont!=new_cont){
  260. detach();
  261. attach(new_cont);
  262. }
  263. return *this;
  264. }
  265. ~safe_iterator_base()
  266. {
  267. detach();
  268. }
  269. const safe_container_base* owner()const{return cont;}
  270. BOOST_MULTI_INDEX_PRIVATE_IF_MEMBER_TEMPLATE_FRIENDS:
  271. friend class safe_container_base;
  272. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  273. template<typename> friend class safe_mode::safe_container;
  274. template<typename Iterator> friend
  275. void safe_mode::detach_equivalent_iterators(Iterator&);
  276. #endif
  277. inline void attach(safe_container_base* cont_);
  278. safe_container_base* cont;
  279. safe_iterator_base* next;
  280. bool unchecked_;
  281. };
  282. class safe_container_base:private noncopyable
  283. {
  284. public:
  285. safe_container_base(){}
  286. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
  287. friend class safe_iterator_base;
  288. #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
  289. template<typename Iterator> friend
  290. void safe_mode::detach_equivalent_iterators(Iterator&);
  291. #endif
  292. ~safe_container_base()
  293. {
  294. /* Detaches all remaining iterators, which by now will
  295. * be those pointing to the end of the container.
  296. */
  297. for(safe_iterator_base* it=header.next;it;it=it->next)it->cont=0;
  298. header.next=0;
  299. }
  300. void swap(safe_container_base& x)
  301. {
  302. for(safe_iterator_base* it0=header.next;it0;it0=it0->next)it0->cont=&x;
  303. for(safe_iterator_base* it1=x.header.next;it1;it1=it1->next)it1->cont=this;
  304. std::swap(header.cont,x.header.cont);
  305. std::swap(header.next,x.header.next);
  306. }
  307. safe_iterator_base header;
  308. #if defined(BOOST_HAS_THREADS)
  309. boost::detail::lightweight_mutex mutex;
  310. #endif
  311. };
  312. void safe_iterator_base::attach(safe_container_base* cont_)
  313. {
  314. cont=cont_;
  315. if(cont){
  316. #if defined(BOOST_HAS_THREADS)
  317. boost::detail::lightweight_mutex::scoped_lock lock(cont->mutex);
  318. #endif
  319. next=cont->header.next;
  320. cont->header.next=this;
  321. }
  322. }
  323. void safe_iterator_base::detach()
  324. {
  325. if(cont){
  326. #if defined(BOOST_HAS_THREADS)
  327. boost::detail::lightweight_mutex::scoped_lock lock(cont->mutex);
  328. #endif
  329. safe_iterator_base *prev_,*next_;
  330. for(prev_=&cont->header;(next_=prev_->next)!=this;prev_=next_){}
  331. prev_->next=next;
  332. cont=0;
  333. }
  334. }
  335. } /* namespace multi_index::detail */
  336. namespace safe_mode{
  337. /* In order to enable safe mode on a container:
  338. * - The container must derive from safe_container<container_type>,
  339. * - iterators must be generated via safe_iterator, which adapts a
  340. * preexistent unsafe iterator class.
  341. */
  342. template<typename Container>
  343. class safe_container;
  344. template<typename Iterator,typename Container>
  345. class safe_iterator:
  346. public detail::iter_adaptor<safe_iterator<Iterator,Container>,Iterator>,
  347. public detail::safe_iterator_base
  348. {
  349. typedef detail::iter_adaptor<safe_iterator,Iterator> super;
  350. typedef detail::safe_iterator_base safe_super;
  351. public:
  352. typedef Container container_type;
  353. typedef typename Iterator::reference reference;
  354. typedef typename Iterator::difference_type difference_type;
  355. safe_iterator(){}
  356. explicit safe_iterator(safe_container<container_type>* cont_):
  357. safe_super(cont_){}
  358. template<typename T0>
  359. safe_iterator(const T0& t0,safe_container<container_type>* cont_):
  360. super(Iterator(t0)),safe_super(cont_){}
  361. template<typename T0,typename T1>
  362. safe_iterator(
  363. const T0& t0,const T1& t1,safe_container<container_type>* cont_):
  364. super(Iterator(t0,t1)),safe_super(cont_){}
  365. safe_iterator(const safe_iterator& x):super(x),safe_super(x){}
  366. safe_iterator& operator=(const safe_iterator& x)
  367. {
  368. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
  369. this->base_reference()=x.base_reference();
  370. safe_super::operator=(x);
  371. return *this;
  372. }
  373. const container_type* owner()const
  374. {
  375. return
  376. static_cast<const container_type*>(
  377. static_cast<const safe_container<container_type>*>(
  378. this->safe_super::owner()));
  379. }
  380. /* get_node is not to be used by the user */
  381. typedef typename Iterator::node_type node_type;
  382. node_type* get_node()const{return this->base_reference().get_node();}
  383. private:
  384. friend class boost::multi_index::detail::iter_adaptor_access;
  385. reference dereference()const
  386. {
  387. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  388. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(*this);
  389. return *(this->base_reference());
  390. }
  391. bool equal(const safe_iterator& x)const
  392. {
  393. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  394. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
  395. BOOST_MULTI_INDEX_CHECK_SAME_OWNER(*this,x);
  396. return this->base_reference()==x.base_reference();
  397. }
  398. void increment()
  399. {
  400. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  401. BOOST_MULTI_INDEX_CHECK_INCREMENTABLE_ITERATOR(*this);
  402. ++(this->base_reference());
  403. }
  404. void decrement()
  405. {
  406. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  407. BOOST_MULTI_INDEX_CHECK_DECREMENTABLE_ITERATOR(*this);
  408. --(this->base_reference());
  409. }
  410. void advance(difference_type n)
  411. {
  412. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  413. BOOST_MULTI_INDEX_CHECK_IN_BOUNDS(*this,n);
  414. this->base_reference()+=n;
  415. }
  416. difference_type distance_to(const safe_iterator& x)const
  417. {
  418. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  419. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(x);
  420. BOOST_MULTI_INDEX_CHECK_SAME_OWNER(*this,x);
  421. return x.base_reference()-this->base_reference();
  422. }
  423. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  424. /* Serialization. Note that Iterator::save and Iterator:load
  425. * are assumed to be defined and public: at first sight it seems
  426. * like we could have resorted to the public serialization interface
  427. * for doing the forwarding to the adapted iterator class:
  428. * ar<<base_reference();
  429. * ar>>base_reference();
  430. * but this would cause incompatibilities if a saving
  431. * program is in safe mode and the loading program is not, or
  432. * viceversa --in safe mode, the archived iterator data is one layer
  433. * deeper, this is especially relevant with XML archives.
  434. * It'd be nice if Boost.Serialization provided some forwarding
  435. * facility for use by adaptor classes.
  436. */
  437. friend class boost::serialization::access;
  438. BOOST_SERIALIZATION_SPLIT_MEMBER()
  439. template<class Archive>
  440. void save(Archive& ar,const unsigned int version)const
  441. {
  442. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(*this);
  443. this->base_reference().save(ar,version);
  444. }
  445. template<class Archive>
  446. void load(Archive& ar,const unsigned int version)
  447. {
  448. this->base_reference().load(ar,version);
  449. safe_super::uncheck();
  450. }
  451. #endif
  452. };
  453. template<typename Container>
  454. class safe_container:public detail::safe_container_base
  455. {
  456. typedef detail::safe_container_base super;
  457. public:
  458. void detach_dereferenceable_iterators()
  459. {
  460. typedef typename Container::iterator iterator;
  461. iterator end_=static_cast<Container*>(this)->end();
  462. iterator *prev_,*next_;
  463. for(
  464. prev_=static_cast<iterator*>(&this->header);
  465. (next_=static_cast<iterator*>(prev_->next))!=0;){
  466. if(*next_!=end_){
  467. prev_->next=next_->next;
  468. next_->cont=0;
  469. }
  470. else prev_=next_;
  471. }
  472. }
  473. void swap(safe_container<Container>& x)
  474. {
  475. super::swap(x);
  476. }
  477. };
  478. } /* namespace multi_index::safe_mode */
  479. } /* namespace multi_index */
  480. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  481. namespace serialization{
  482. template<typename Iterator,typename Container>
  483. struct version<
  484. boost::multi_index::safe_mode::safe_iterator<Iterator,Container>
  485. >
  486. {
  487. BOOST_STATIC_CONSTANT(
  488. int,value=boost::serialization::version<Iterator>::value);
  489. };
  490. } /* namespace serialization */
  491. #endif
  492. } /* namespace boost */
  493. #endif /* BOOST_MULTI_INDEX_ENABLE_SAFE_MODE */
  494. #endif