throw_allocator.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. // -*- C++ -*-
  2. // Copyright (C) 2005-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
  21. // Permission to use, copy, modify, sell, and distribute this software
  22. // is hereby granted without fee, provided that the above copyright
  23. // notice appears in all copies, and that both that copyright notice
  24. // and this permission notice appear in supporting documentation. None
  25. // of the above authors, nor IBM Haifa Research Laboratories, make any
  26. // representation about the suitability of this software for any
  27. // purpose. It is provided "as is" without express or implied
  28. // warranty.
  29. /** @file ext/throw_allocator.h
  30. * This file is a GNU extension to the Standard C++ Library.
  31. *
  32. * Contains two exception-generating types (throw_value, throw_allocator)
  33. * intended to be used as value and allocator types while testing
  34. * exception safety in templatized containers and algorithms. The
  35. * allocator has additional log and debug features. The exception
  36. * generated is of type forced_exception_error.
  37. */
  38. #ifndef _THROW_ALLOCATOR_H
  39. #define _THROW_ALLOCATOR_H 1
  40. #include <cmath>
  41. #include <ctime>
  42. #include <map>
  43. #include <string>
  44. #include <ostream>
  45. #include <stdexcept>
  46. #include <utility>
  47. #include <bits/functexcept.h>
  48. #include <bits/move.h>
  49. #if __cplusplus >= 201103L
  50. # include <functional>
  51. # include <random>
  52. #else
  53. # include <tr1/functional>
  54. # include <tr1/random>
  55. #endif
  56. #include <ext/alloc_traits.h>
  57. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  58. {
  59. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  60. /**
  61. * @brief Thown by exception safety machinery.
  62. * @ingroup exceptions
  63. */
  64. struct forced_error : public std::exception
  65. { };
  66. // Substitute for forced_error object when -fno-exceptions.
  67. inline void
  68. __throw_forced_error()
  69. { _GLIBCXX_THROW_OR_ABORT(forced_error()); }
  70. /**
  71. * @brief Base class for checking address and label information
  72. * about allocations. Create a std::map between the allocated
  73. * address (void*) and a datum for annotations, which are a pair of
  74. * numbers corresponding to label and allocated size.
  75. */
  76. struct annotate_base
  77. {
  78. private:
  79. typedef std::pair<size_t, size_t> data_type;
  80. typedef std::map<void*, data_type> map_alloc_type;
  81. typedef map_alloc_type::value_type entry_type;
  82. typedef map_alloc_type::const_iterator const_iterator;
  83. typedef map_alloc_type::const_reference const_reference;
  84. #if __cplusplus >= 201103L
  85. typedef std::map<void*, size_t> map_construct_type;
  86. #endif
  87. public:
  88. annotate_base()
  89. {
  90. label();
  91. map_alloc();
  92. }
  93. static void
  94. set_label(size_t l)
  95. { label() = l; }
  96. static size_t
  97. get_label()
  98. { return label(); }
  99. void
  100. insert(void* p, size_t size)
  101. {
  102. entry_type entry = make_entry(p, size);
  103. if (!p)
  104. {
  105. std::string error("annotate_base::insert null insert!\n");
  106. log_to_string(error, entry);
  107. std::__throw_logic_error(error.c_str());
  108. }
  109. std::pair<map_alloc_type::iterator, bool> inserted
  110. = map_alloc().insert(entry);
  111. if (!inserted.second)
  112. {
  113. std::string error("annotate_base::insert double insert!\n");
  114. log_to_string(error, entry);
  115. log_to_string(error, *inserted.first);
  116. std::__throw_logic_error(error.c_str());
  117. }
  118. }
  119. void
  120. erase(void* p, size_t size)
  121. { map_alloc().erase(check_allocated(p, size)); }
  122. #if __cplusplus >= 201103L
  123. void
  124. insert_construct(void* p)
  125. {
  126. if (!p)
  127. {
  128. std::string error("annotate_base::insert_construct null!\n");
  129. std::__throw_logic_error(error.c_str());
  130. }
  131. auto inserted = map_construct().insert(std::make_pair(p, get_label()));
  132. if (!inserted.second)
  133. {
  134. std::string error("annotate_base::insert_construct double insert!\n");
  135. log_to_string(error, std::make_pair(p, get_label()));
  136. log_to_string(error, *inserted.first);
  137. std::__throw_logic_error(error.c_str());
  138. }
  139. }
  140. void
  141. erase_construct(void* p)
  142. { map_construct().erase(check_constructed(p)); }
  143. #endif
  144. // See if a particular address and allocation size has been saved.
  145. inline map_alloc_type::iterator
  146. check_allocated(void* p, size_t size)
  147. {
  148. map_alloc_type::iterator found = map_alloc().find(p);
  149. if (found == map_alloc().end())
  150. {
  151. std::string error("annotate_base::check_allocated by value "
  152. "null erase!\n");
  153. log_to_string(error, make_entry(p, size));
  154. std::__throw_logic_error(error.c_str());
  155. }
  156. if (found->second.second != size)
  157. {
  158. std::string error("annotate_base::check_allocated by value "
  159. "wrong-size erase!\n");
  160. log_to_string(error, make_entry(p, size));
  161. log_to_string(error, *found);
  162. std::__throw_logic_error(error.c_str());
  163. }
  164. return found;
  165. }
  166. // See if a given label has been allocated.
  167. inline void
  168. check(size_t label)
  169. {
  170. std::string found;
  171. {
  172. const_iterator beg = map_alloc().begin();
  173. const_iterator end = map_alloc().end();
  174. while (beg != end)
  175. {
  176. if (beg->second.first == label)
  177. log_to_string(found, *beg);
  178. ++beg;
  179. }
  180. }
  181. #if __cplusplus >= 201103L
  182. {
  183. auto beg = map_construct().begin();
  184. auto end = map_construct().end();
  185. while (beg != end)
  186. {
  187. if (beg->second == label)
  188. log_to_string(found, *beg);
  189. ++beg;
  190. }
  191. }
  192. #endif
  193. if (!found.empty())
  194. {
  195. std::string error("annotate_base::check by label\n");
  196. error += found;
  197. std::__throw_logic_error(error.c_str());
  198. }
  199. }
  200. // See if there is anything left allocated or constructed.
  201. inline static void
  202. check()
  203. {
  204. std::string found;
  205. {
  206. const_iterator beg = map_alloc().begin();
  207. const_iterator end = map_alloc().end();
  208. while (beg != end)
  209. {
  210. log_to_string(found, *beg);
  211. ++beg;
  212. }
  213. }
  214. #if __cplusplus >= 201103L
  215. {
  216. auto beg = map_construct().begin();
  217. auto end = map_construct().end();
  218. while (beg != end)
  219. {
  220. log_to_string(found, *beg);
  221. ++beg;
  222. }
  223. }
  224. #endif
  225. if (!found.empty())
  226. {
  227. std::string error("annotate_base::check \n");
  228. error += found;
  229. std::__throw_logic_error(error.c_str());
  230. }
  231. }
  232. #if __cplusplus >= 201103L
  233. inline map_construct_type::iterator
  234. check_constructed(void* p)
  235. {
  236. auto found = map_construct().find(p);
  237. if (found == map_construct().end())
  238. {
  239. std::string error("annotate_base::check_constructed not "
  240. "constructed!\n");
  241. log_to_string(error, std::make_pair(p, get_label()));
  242. std::__throw_logic_error(error.c_str());
  243. }
  244. return found;
  245. }
  246. inline void
  247. check_constructed(size_t label)
  248. {
  249. auto beg = map_construct().begin();
  250. auto end = map_construct().end();
  251. std::string found;
  252. while (beg != end)
  253. {
  254. if (beg->second == label)
  255. log_to_string(found, *beg);
  256. ++beg;
  257. }
  258. if (!found.empty())
  259. {
  260. std::string error("annotate_base::check_constructed by label\n");
  261. error += found;
  262. std::__throw_logic_error(error.c_str());
  263. }
  264. }
  265. #endif
  266. private:
  267. friend std::ostream&
  268. operator<<(std::ostream&, const annotate_base&);
  269. entry_type
  270. make_entry(void* p, size_t size)
  271. { return std::make_pair(p, data_type(get_label(), size)); }
  272. static void
  273. log_to_string(std::string& s, const_reference ref)
  274. {
  275. char buf[40];
  276. const char tab('\t');
  277. s += "label: ";
  278. unsigned long l = static_cast<unsigned long>(ref.second.first);
  279. __builtin_sprintf(buf, "%lu", l);
  280. s += buf;
  281. s += tab;
  282. s += "size: ";
  283. l = static_cast<unsigned long>(ref.second.second);
  284. __builtin_sprintf(buf, "%lu", l);
  285. s += buf;
  286. s += tab;
  287. s += "address: ";
  288. __builtin_sprintf(buf, "%p", ref.first);
  289. s += buf;
  290. s += '\n';
  291. }
  292. #if __cplusplus >= 201103L
  293. static void
  294. log_to_string(std::string& s, const std::pair<const void*, size_t>& ref)
  295. {
  296. char buf[40];
  297. const char tab('\t');
  298. s += "label: ";
  299. unsigned long l = static_cast<unsigned long>(ref.second);
  300. __builtin_sprintf(buf, "%lu", l);
  301. s += buf;
  302. s += tab;
  303. s += "address: ";
  304. __builtin_sprintf(buf, "%p", ref.first);
  305. s += buf;
  306. s += '\n';
  307. }
  308. #endif
  309. static size_t&
  310. label()
  311. {
  312. static size_t _S_label(std::numeric_limits<size_t>::max());
  313. return _S_label;
  314. }
  315. static map_alloc_type&
  316. map_alloc()
  317. {
  318. static map_alloc_type _S_map;
  319. return _S_map;
  320. }
  321. #if __cplusplus >= 201103L
  322. static map_construct_type&
  323. map_construct()
  324. {
  325. static map_construct_type _S_map;
  326. return _S_map;
  327. }
  328. #endif
  329. };
  330. inline std::ostream&
  331. operator<<(std::ostream& os, const annotate_base& __b)
  332. {
  333. std::string error;
  334. typedef annotate_base base_type;
  335. {
  336. base_type::const_iterator beg = __b.map_alloc().begin();
  337. base_type::const_iterator end = __b.map_alloc().end();
  338. for (; beg != end; ++beg)
  339. __b.log_to_string(error, *beg);
  340. }
  341. #if __cplusplus >= 201103L
  342. {
  343. auto beg = __b.map_construct().begin();
  344. auto end = __b.map_construct().end();
  345. for (; beg != end; ++beg)
  346. __b.log_to_string(error, *beg);
  347. }
  348. #endif
  349. return os << error;
  350. }
  351. /**
  352. * @brief Base struct for condition policy.
  353. *
  354. * Requires a public member function with the signature
  355. * void throw_conditionally()
  356. */
  357. struct condition_base
  358. {
  359. #if __cplusplus >= 201103L
  360. condition_base() = default;
  361. condition_base(const condition_base&) = default;
  362. condition_base& operator=(const condition_base&) = default;
  363. #endif
  364. virtual ~condition_base() { };
  365. };
  366. /**
  367. * @brief Base class for incremental control and throw.
  368. */
  369. struct limit_condition : public condition_base
  370. {
  371. // Scope-level adjustor objects: set limit for throw at the
  372. // beginning of a scope block, and restores to previous limit when
  373. // object is destroyed on exiting the block.
  374. struct adjustor_base
  375. {
  376. private:
  377. const size_t _M_orig;
  378. public:
  379. adjustor_base() : _M_orig(limit()) { }
  380. virtual
  381. ~adjustor_base() { set_limit(_M_orig); }
  382. };
  383. /// Never enter the condition.
  384. struct never_adjustor : public adjustor_base
  385. {
  386. never_adjustor() { set_limit(std::numeric_limits<size_t>::max()); }
  387. };
  388. /// Always enter the condition.
  389. struct always_adjustor : public adjustor_base
  390. {
  391. always_adjustor() { set_limit(count()); }
  392. };
  393. /// Enter the nth condition.
  394. struct limit_adjustor : public adjustor_base
  395. {
  396. limit_adjustor(const size_t __l) { set_limit(__l); }
  397. };
  398. // Increment _S_count every time called.
  399. // If _S_count matches the limit count, throw.
  400. static void
  401. throw_conditionally()
  402. {
  403. if (count() == limit())
  404. __throw_forced_error();
  405. ++count();
  406. }
  407. static size_t&
  408. count()
  409. {
  410. static size_t _S_count(0);
  411. return _S_count;
  412. }
  413. static size_t&
  414. limit()
  415. {
  416. static size_t _S_limit(std::numeric_limits<size_t>::max());
  417. return _S_limit;
  418. }
  419. // Zero the throw counter, set limit to argument.
  420. static void
  421. set_limit(const size_t __l)
  422. {
  423. limit() = __l;
  424. count() = 0;
  425. }
  426. };
  427. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  428. /**
  429. * @brief Base class for random probability control and throw.
  430. */
  431. struct random_condition : public condition_base
  432. {
  433. // Scope-level adjustor objects: set probability for throw at the
  434. // beginning of a scope block, and restores to previous
  435. // probability when object is destroyed on exiting the block.
  436. struct adjustor_base
  437. {
  438. private:
  439. const double _M_orig;
  440. public:
  441. adjustor_base() : _M_orig(probability()) { }
  442. virtual ~adjustor_base()
  443. { set_probability(_M_orig); }
  444. };
  445. /// Group condition.
  446. struct group_adjustor : public adjustor_base
  447. {
  448. group_adjustor(size_t size)
  449. { set_probability(1 - std::pow(double(1 - probability()),
  450. double(0.5 / (size + 1))));
  451. }
  452. };
  453. /// Never enter the condition.
  454. struct never_adjustor : public adjustor_base
  455. {
  456. never_adjustor() { set_probability(0); }
  457. };
  458. /// Always enter the condition.
  459. struct always_adjustor : public adjustor_base
  460. {
  461. always_adjustor() { set_probability(1); }
  462. };
  463. random_condition()
  464. {
  465. probability();
  466. engine();
  467. }
  468. static void
  469. set_probability(double __p)
  470. { probability() = __p; }
  471. static void
  472. throw_conditionally()
  473. {
  474. if (generate() < probability())
  475. __throw_forced_error();
  476. }
  477. void
  478. seed(unsigned long __s)
  479. { engine().seed(__s); }
  480. private:
  481. #if __cplusplus >= 201103L
  482. typedef std::uniform_real_distribution<double> distribution_type;
  483. typedef std::mt19937 engine_type;
  484. #else
  485. typedef std::tr1::uniform_real<double> distribution_type;
  486. typedef std::tr1::mt19937 engine_type;
  487. #endif
  488. static double
  489. generate()
  490. {
  491. #if __cplusplus >= 201103L
  492. const distribution_type distribution(0, 1);
  493. static auto generator = std::bind(distribution, engine());
  494. #else
  495. // Use variate_generator to get normalized results.
  496. typedef std::tr1::variate_generator<engine_type, distribution_type> gen_t;
  497. distribution_type distribution(0, 1);
  498. static gen_t generator(engine(), distribution);
  499. #endif
  500. double random = generator();
  501. if (random < distribution.min() || random > distribution.max())
  502. {
  503. std::string __s("random_condition::generate");
  504. __s += "\n";
  505. __s += "random number generated is: ";
  506. char buf[40];
  507. __builtin_sprintf(buf, "%f", random);
  508. __s += buf;
  509. std::__throw_out_of_range(__s.c_str());
  510. }
  511. return random;
  512. }
  513. static double&
  514. probability()
  515. {
  516. static double _S_p;
  517. return _S_p;
  518. }
  519. static engine_type&
  520. engine()
  521. {
  522. static engine_type _S_e;
  523. return _S_e;
  524. }
  525. };
  526. #endif // _GLIBCXX_USE_C99_STDINT_TR1
  527. /**
  528. * @brief Class with exception generation control. Intended to be
  529. * used as a value_type in templatized code.
  530. *
  531. * Note: Destructor not allowed to throw.
  532. */
  533. template<typename _Cond>
  534. struct throw_value_base : public _Cond
  535. {
  536. typedef _Cond condition_type;
  537. using condition_type::throw_conditionally;
  538. std::size_t _M_i;
  539. #ifndef _GLIBCXX_IS_AGGREGATE
  540. throw_value_base() : _M_i(0)
  541. { throw_conditionally(); }
  542. throw_value_base(const throw_value_base& __v) : _M_i(__v._M_i)
  543. { throw_conditionally(); }
  544. #if __cplusplus >= 201103L
  545. // Shall not throw.
  546. throw_value_base(throw_value_base&&) = default;
  547. #endif
  548. explicit throw_value_base(const std::size_t __i) : _M_i(__i)
  549. { throw_conditionally(); }
  550. #endif
  551. throw_value_base&
  552. operator=(const throw_value_base& __v)
  553. {
  554. throw_conditionally();
  555. _M_i = __v._M_i;
  556. return *this;
  557. }
  558. #if __cplusplus >= 201103L
  559. // Shall not throw.
  560. throw_value_base&
  561. operator=(throw_value_base&&) = default;
  562. #endif
  563. throw_value_base&
  564. operator++()
  565. {
  566. throw_conditionally();
  567. ++_M_i;
  568. return *this;
  569. }
  570. };
  571. template<typename _Cond>
  572. inline void
  573. swap(throw_value_base<_Cond>& __a, throw_value_base<_Cond>& __b)
  574. {
  575. typedef throw_value_base<_Cond> throw_value;
  576. throw_value::throw_conditionally();
  577. throw_value orig(__a);
  578. __a = __b;
  579. __b = orig;
  580. }
  581. // General instantiable types requirements.
  582. template<typename _Cond>
  583. inline bool
  584. operator==(const throw_value_base<_Cond>& __a,
  585. const throw_value_base<_Cond>& __b)
  586. {
  587. typedef throw_value_base<_Cond> throw_value;
  588. throw_value::throw_conditionally();
  589. bool __ret = __a._M_i == __b._M_i;
  590. return __ret;
  591. }
  592. template<typename _Cond>
  593. inline bool
  594. operator<(const throw_value_base<_Cond>& __a,
  595. const throw_value_base<_Cond>& __b)
  596. {
  597. typedef throw_value_base<_Cond> throw_value;
  598. throw_value::throw_conditionally();
  599. bool __ret = __a._M_i < __b._M_i;
  600. return __ret;
  601. }
  602. // Numeric algorithms instantiable types requirements.
  603. template<typename _Cond>
  604. inline throw_value_base<_Cond>
  605. operator+(const throw_value_base<_Cond>& __a,
  606. const throw_value_base<_Cond>& __b)
  607. {
  608. typedef throw_value_base<_Cond> throw_value;
  609. throw_value::throw_conditionally();
  610. throw_value __ret(__a._M_i + __b._M_i);
  611. return __ret;
  612. }
  613. template<typename _Cond>
  614. inline throw_value_base<_Cond>
  615. operator-(const throw_value_base<_Cond>& __a,
  616. const throw_value_base<_Cond>& __b)
  617. {
  618. typedef throw_value_base<_Cond> throw_value;
  619. throw_value::throw_conditionally();
  620. throw_value __ret(__a._M_i - __b._M_i);
  621. return __ret;
  622. }
  623. template<typename _Cond>
  624. inline throw_value_base<_Cond>
  625. operator*(const throw_value_base<_Cond>& __a,
  626. const throw_value_base<_Cond>& __b)
  627. {
  628. typedef throw_value_base<_Cond> throw_value;
  629. throw_value::throw_conditionally();
  630. throw_value __ret(__a._M_i * __b._M_i);
  631. return __ret;
  632. }
  633. /// Type throwing via limit condition.
  634. struct throw_value_limit : public throw_value_base<limit_condition>
  635. {
  636. typedef throw_value_base<limit_condition> base_type;
  637. #ifndef _GLIBCXX_IS_AGGREGATE
  638. throw_value_limit() { }
  639. throw_value_limit(const throw_value_limit& __other)
  640. : base_type(__other._M_i) { }
  641. #if __cplusplus >= 201103L
  642. throw_value_limit(throw_value_limit&&) = default;
  643. #endif
  644. explicit throw_value_limit(const std::size_t __i) : base_type(__i) { }
  645. #endif
  646. throw_value_limit&
  647. operator=(const throw_value_limit& __other)
  648. {
  649. base_type::operator=(__other);
  650. return *this;
  651. }
  652. #if __cplusplus >= 201103L
  653. throw_value_limit&
  654. operator=(throw_value_limit&&) = default;
  655. #endif
  656. };
  657. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  658. /// Type throwing via random condition.
  659. struct throw_value_random : public throw_value_base<random_condition>
  660. {
  661. typedef throw_value_base<random_condition> base_type;
  662. #ifndef _GLIBCXX_IS_AGGREGATE
  663. throw_value_random() { }
  664. throw_value_random(const throw_value_random& __other)
  665. : base_type(__other._M_i) { }
  666. #if __cplusplus >= 201103L
  667. throw_value_random(throw_value_random&&) = default;
  668. #endif
  669. explicit throw_value_random(const std::size_t __i) : base_type(__i) { }
  670. #endif
  671. throw_value_random&
  672. operator=(const throw_value_random& __other)
  673. {
  674. base_type::operator=(__other);
  675. return *this;
  676. }
  677. #if __cplusplus >= 201103L
  678. throw_value_random&
  679. operator=(throw_value_random&&) = default;
  680. #endif
  681. };
  682. #endif // _GLIBCXX_USE_C99_STDINT_TR1
  683. /**
  684. * @brief Allocator class with logging and exception generation control.
  685. * Intended to be used as an allocator_type in templatized code.
  686. * @ingroup allocators
  687. *
  688. * Note: Deallocate not allowed to throw.
  689. */
  690. template<typename _Tp, typename _Cond>
  691. class throw_allocator_base
  692. : public annotate_base, public _Cond
  693. {
  694. public:
  695. typedef std::size_t size_type;
  696. typedef std::ptrdiff_t difference_type;
  697. typedef _Tp value_type;
  698. typedef value_type* pointer;
  699. typedef const value_type* const_pointer;
  700. typedef value_type& reference;
  701. typedef const value_type& const_reference;
  702. #if __cplusplus >= 201103L
  703. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  704. // 2103. std::allocator propagate_on_container_move_assignment
  705. typedef std::true_type propagate_on_container_move_assignment;
  706. #endif
  707. private:
  708. typedef _Cond condition_type;
  709. std::allocator<value_type> _M_allocator;
  710. typedef __gnu_cxx::__alloc_traits<std::allocator<value_type> > traits;
  711. using condition_type::throw_conditionally;
  712. public:
  713. size_type
  714. max_size() const _GLIBCXX_USE_NOEXCEPT
  715. { return traits::max_size(_M_allocator); }
  716. pointer
  717. address(reference __x) const _GLIBCXX_NOEXCEPT
  718. { return std::__addressof(__x); }
  719. const_pointer
  720. address(const_reference __x) const _GLIBCXX_NOEXCEPT
  721. { return std::__addressof(__x); }
  722. _GLIBCXX_NODISCARD pointer
  723. allocate(size_type __n, const void* hint = 0)
  724. {
  725. if (__n > this->max_size())
  726. std::__throw_bad_alloc();
  727. throw_conditionally();
  728. pointer const a = traits::allocate(_M_allocator, __n, hint);
  729. insert(a, sizeof(value_type) * __n);
  730. return a;
  731. }
  732. #if __cplusplus >= 201103L
  733. template<typename _Up, typename... _Args>
  734. void
  735. construct(_Up* __p, _Args&&... __args)
  736. {
  737. traits::construct(_M_allocator, __p, std::forward<_Args>(__args)...);
  738. insert_construct(__p);
  739. }
  740. template<typename _Up>
  741. void
  742. destroy(_Up* __p)
  743. {
  744. erase_construct(__p);
  745. traits::destroy(_M_allocator, __p);
  746. }
  747. #else
  748. void
  749. construct(pointer __p, const value_type& val)
  750. { return _M_allocator.construct(__p, val); }
  751. void
  752. destroy(pointer __p)
  753. { _M_allocator.destroy(__p); }
  754. #endif
  755. void
  756. deallocate(pointer __p, size_type __n)
  757. {
  758. erase(__p, sizeof(value_type) * __n);
  759. _M_allocator.deallocate(__p, __n);
  760. }
  761. void
  762. check_allocated(pointer __p, size_type __n)
  763. {
  764. size_type __t = sizeof(value_type) * __n;
  765. annotate_base::check_allocated(__p, __t);
  766. }
  767. void
  768. check(size_type __n)
  769. { annotate_base::check(__n); }
  770. };
  771. template<typename _Tp, typename _Cond>
  772. inline bool
  773. operator==(const throw_allocator_base<_Tp, _Cond>&,
  774. const throw_allocator_base<_Tp, _Cond>&)
  775. { return true; }
  776. #if __cpp_impl_three_way_comparison < 201907L
  777. template<typename _Tp, typename _Cond>
  778. inline bool
  779. operator!=(const throw_allocator_base<_Tp, _Cond>&,
  780. const throw_allocator_base<_Tp, _Cond>&)
  781. { return false; }
  782. #endif
  783. /// Allocator throwing via limit condition.
  784. template<typename _Tp>
  785. struct throw_allocator_limit
  786. : public throw_allocator_base<_Tp, limit_condition>
  787. {
  788. template<typename _Tp1>
  789. struct rebind
  790. { typedef throw_allocator_limit<_Tp1> other; };
  791. throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { }
  792. throw_allocator_limit(const throw_allocator_limit&)
  793. _GLIBCXX_USE_NOEXCEPT { }
  794. template<typename _Tp1>
  795. throw_allocator_limit(const throw_allocator_limit<_Tp1>&)
  796. _GLIBCXX_USE_NOEXCEPT { }
  797. ~throw_allocator_limit() _GLIBCXX_USE_NOEXCEPT { }
  798. };
  799. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  800. /// Allocator throwing via random condition.
  801. template<typename _Tp>
  802. struct throw_allocator_random
  803. : public throw_allocator_base<_Tp, random_condition>
  804. {
  805. template<typename _Tp1>
  806. struct rebind
  807. { typedef throw_allocator_random<_Tp1> other; };
  808. throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { }
  809. throw_allocator_random(const throw_allocator_random&)
  810. _GLIBCXX_USE_NOEXCEPT { }
  811. template<typename _Tp1>
  812. throw_allocator_random(const throw_allocator_random<_Tp1>&)
  813. _GLIBCXX_USE_NOEXCEPT { }
  814. ~throw_allocator_random() _GLIBCXX_USE_NOEXCEPT { }
  815. };
  816. #endif // _GLIBCXX_USE_C99_STDINT_TR1
  817. _GLIBCXX_END_NAMESPACE_VERSION
  818. } // namespace
  819. #if __cplusplus >= 201103L
  820. # include <bits/functional_hash.h>
  821. namespace std _GLIBCXX_VISIBILITY(default)
  822. {
  823. /// Explicit specialization of std::hash for __gnu_cxx::throw_value_limit.
  824. template<>
  825. struct hash<__gnu_cxx::throw_value_limit>
  826. : public std::unary_function<__gnu_cxx::throw_value_limit, size_t>
  827. {
  828. size_t
  829. operator()(const __gnu_cxx::throw_value_limit& __val) const
  830. {
  831. __gnu_cxx::throw_value_limit::throw_conditionally();
  832. std::hash<std::size_t> __h;
  833. size_t __result = __h(__val._M_i);
  834. return __result;
  835. }
  836. };
  837. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  838. /// Explicit specialization of std::hash for __gnu_cxx::throw_value_random.
  839. template<>
  840. struct hash<__gnu_cxx::throw_value_random>
  841. : public std::unary_function<__gnu_cxx::throw_value_random, size_t>
  842. {
  843. size_t
  844. operator()(const __gnu_cxx::throw_value_random& __val) const
  845. {
  846. __gnu_cxx::throw_value_random::throw_conditionally();
  847. std::hash<std::size_t> __h;
  848. size_t __result = __h(__val._M_i);
  849. return __result;
  850. }
  851. };
  852. #endif
  853. } // end namespace std
  854. #endif
  855. #endif