throw_allocator.h 24 KB

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