testsuite_allocator.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. // -*- C++ -*-
  2. // Testing allocator for the C++ library testsuite.
  3. //
  4. // Copyright (C) 2002-2022 Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License along
  18. // with this library; see the file COPYING3. If not see
  19. // <http://www.gnu.org/licenses/>.
  20. //
  21. // This file provides an test instrumentation allocator that can be
  22. // used to verify allocation functionality of standard library
  23. // containers. 2002.11.25 smw
  24. #ifndef _GLIBCXX_TESTSUITE_ALLOCATOR_H
  25. #define _GLIBCXX_TESTSUITE_ALLOCATOR_H
  26. #include <bits/move.h>
  27. #include <ext/pointer.h>
  28. #include <ext/alloc_traits.h>
  29. #include <testsuite_hooks.h>
  30. #if __cplusplus >= 201703L
  31. # include <memory_resource>
  32. # include <new>
  33. #endif
  34. #if __cplusplus >= 201103L
  35. # include <unordered_map>
  36. namespace unord = std;
  37. #else
  38. # include <tr1/unordered_map>
  39. namespace unord = std::tr1;
  40. #endif
  41. namespace __gnu_test
  42. {
  43. // A common API for calling max_size() on an allocator in any -std mode.
  44. template<typename A>
  45. typename A::size_type
  46. max_size(const A& a)
  47. {
  48. #if __cplusplus >= 201103L
  49. return std::allocator_traits<A>::max_size(a);
  50. #else
  51. return a.max_size();
  52. #endif
  53. }
  54. class tracker_allocator_counter
  55. {
  56. public:
  57. typedef std::size_t size_type;
  58. static void
  59. allocate(size_type blocksize)
  60. { allocationCount_ += blocksize; }
  61. static void
  62. construct() { ++constructCount_; }
  63. static void
  64. destroy() { ++destructCount_; }
  65. static void
  66. deallocate(size_type blocksize)
  67. { deallocationCount_ += blocksize; }
  68. static size_type
  69. get_allocation_count() { return allocationCount_; }
  70. static size_type
  71. get_deallocation_count() { return deallocationCount_; }
  72. static int
  73. get_construct_count() { return constructCount_; }
  74. static int
  75. get_destruct_count() { return destructCount_; }
  76. static void
  77. reset()
  78. {
  79. allocationCount_ = 0;
  80. deallocationCount_ = 0;
  81. constructCount_ = 0;
  82. destructCount_ = 0;
  83. }
  84. private:
  85. static size_type allocationCount_;
  86. static size_type deallocationCount_;
  87. static int constructCount_;
  88. static int destructCount_;
  89. };
  90. // Helper to detect inconsistency between type used to instantiate an
  91. // allocator and the underlying allocator value_type.
  92. template<typename T, typename Alloc,
  93. typename = typename Alloc::value_type>
  94. struct check_consistent_alloc_value_type;
  95. template<typename T, typename Alloc>
  96. struct check_consistent_alloc_value_type<T, Alloc, T>
  97. { typedef T value_type; };
  98. // An allocator facade that intercepts allocate/deallocate/construct/destroy
  99. // calls and track them through the tracker_allocator_counter class. This
  100. // class is templated on the target object type, but tracker isn't.
  101. template<typename T, typename Alloc = std::allocator<T> >
  102. class tracker_allocator : public Alloc
  103. {
  104. private:
  105. typedef tracker_allocator_counter counter_type;
  106. typedef __gnu_cxx::__alloc_traits<Alloc> AllocTraits;
  107. public:
  108. typedef typename
  109. check_consistent_alloc_value_type<T, Alloc>::value_type value_type;
  110. typedef typename AllocTraits::pointer pointer;
  111. typedef typename AllocTraits::size_type size_type;
  112. template<class U>
  113. struct rebind
  114. {
  115. typedef tracker_allocator<U,
  116. typename AllocTraits::template rebind<U>::other> other;
  117. };
  118. #if __cplusplus >= 201103L
  119. tracker_allocator() = default;
  120. tracker_allocator(const tracker_allocator&) = default;
  121. tracker_allocator(tracker_allocator&&) = default;
  122. tracker_allocator& operator=(const tracker_allocator&) = default;
  123. tracker_allocator& operator=(tracker_allocator&&) = default;
  124. // Perfect forwarding constructor.
  125. template<typename... _Args>
  126. tracker_allocator(_Args&&... __args)
  127. : Alloc(std::forward<_Args>(__args)...)
  128. { }
  129. #else
  130. tracker_allocator()
  131. { }
  132. tracker_allocator(const tracker_allocator&)
  133. { }
  134. ~tracker_allocator()
  135. { }
  136. #endif
  137. template<class U>
  138. tracker_allocator(const tracker_allocator<U,
  139. typename AllocTraits::template rebind<U>::other>& alloc)
  140. _GLIBCXX_USE_NOEXCEPT
  141. : Alloc(alloc)
  142. { }
  143. pointer
  144. allocate(size_type n, const void* = 0)
  145. {
  146. pointer p = AllocTraits::allocate(*this, n);
  147. counter_type::allocate(n * sizeof(T));
  148. return p;
  149. }
  150. #if __cplusplus >= 201103L
  151. template<typename U, typename... Args>
  152. void
  153. construct(U* p, Args&&... args)
  154. {
  155. AllocTraits::construct(*this, p, std::forward<Args>(args)...);
  156. counter_type::construct();
  157. }
  158. template<typename U>
  159. void
  160. destroy(U* p)
  161. {
  162. AllocTraits::destroy(*this, p);
  163. counter_type::destroy();
  164. }
  165. #else
  166. void
  167. construct(pointer p, const T& value)
  168. {
  169. AllocTraits::construct(*this, p, value);
  170. counter_type::construct();
  171. }
  172. void
  173. destroy(pointer p)
  174. {
  175. AllocTraits::destroy(*this, p);
  176. counter_type::destroy();
  177. }
  178. #endif
  179. void
  180. deallocate(pointer p, size_type num)
  181. {
  182. counter_type::deallocate(num * sizeof(T));
  183. AllocTraits::deallocate(*this, p, num);
  184. }
  185. // Implement swap for underlying allocators that might need it.
  186. friend inline void
  187. swap(tracker_allocator& a, tracker_allocator& b)
  188. {
  189. using std::swap;
  190. Alloc& aa = a;
  191. Alloc& ab = b;
  192. swap(aa, ab);
  193. }
  194. };
  195. template<class T1, class Alloc1, class T2, class Alloc2>
  196. bool
  197. operator==(const tracker_allocator<T1, Alloc1>& lhs,
  198. const tracker_allocator<T2, Alloc2>& rhs) throw()
  199. {
  200. const Alloc1& alloc1 = lhs;
  201. const Alloc2& alloc2 = rhs;
  202. return alloc1 == alloc2;
  203. }
  204. template<class T1, class Alloc1, class T2, class Alloc2>
  205. bool
  206. operator!=(const tracker_allocator<T1, Alloc1>& lhs,
  207. const tracker_allocator<T2, Alloc2>& rhs) throw()
  208. { return !(lhs == rhs); }
  209. bool
  210. check_construct_destroy(const char* tag, int expected_c, int expected_d);
  211. template<typename Alloc>
  212. bool
  213. check_deallocate_null()
  214. {
  215. // Let's not core here...
  216. Alloc a;
  217. a.deallocate(0, 1);
  218. a.deallocate(0, 10);
  219. return true;
  220. }
  221. #if __cpp_exceptions
  222. template<typename Alloc>
  223. bool
  224. check_allocate_max_size()
  225. {
  226. Alloc a;
  227. try
  228. {
  229. (void) a.allocate(__gnu_test::max_size(a) + 1);
  230. }
  231. catch(std::bad_alloc&)
  232. {
  233. return true;
  234. }
  235. catch(...)
  236. {
  237. throw;
  238. }
  239. throw;
  240. }
  241. #endif
  242. // A simple allocator which can be constructed endowed of a given
  243. // "personality" (an integer), queried in operator== to simulate the
  244. // behavior of realworld "unequal" allocators (i.e., not exploiting
  245. // the provision in 20.1.5/4, first bullet). A global unordered_map,
  246. // filled at allocation time with (pointer, personality) pairs, is
  247. // then consulted to enforce the requirements in Table 32 about
  248. // deallocation vs allocator equality. Note that this allocator is
  249. // swappable, not copy assignable, consistently with Option 3 of DR 431
  250. // (see N1599).
  251. struct uneq_allocator_base
  252. {
  253. typedef unord::unordered_map<void*, int> map_type;
  254. // Avoid static initialization troubles and/or bad interactions
  255. // with tests linking testsuite_allocator.o and playing globally
  256. // with operator new/delete.
  257. static map_type&
  258. get_map()
  259. {
  260. static map_type alloc_map;
  261. return alloc_map;
  262. }
  263. };
  264. template<typename Tp, typename Alloc = std::allocator<Tp> >
  265. class uneq_allocator
  266. : private uneq_allocator_base,
  267. public Alloc
  268. {
  269. typedef __gnu_cxx::__alloc_traits<Alloc> AllocTraits;
  270. Alloc& base() { return *this; }
  271. const Alloc& base() const { return *this; }
  272. void swap_base(Alloc& b) { using std::swap; swap(b, this->base()); }
  273. public:
  274. typedef typename check_consistent_alloc_value_type<Tp, Alloc>::value_type
  275. value_type;
  276. typedef typename AllocTraits::size_type size_type;
  277. typedef typename AllocTraits::pointer pointer;
  278. #if __cplusplus >= 201103L
  279. typedef std::true_type propagate_on_container_swap;
  280. typedef std::false_type is_always_equal;
  281. #endif
  282. template<typename Tp1>
  283. struct rebind
  284. {
  285. typedef uneq_allocator<Tp1,
  286. typename AllocTraits::template rebind<Tp1>::other> other;
  287. };
  288. uneq_allocator() _GLIBCXX_USE_NOEXCEPT
  289. : personality(0) { }
  290. uneq_allocator(int person) _GLIBCXX_USE_NOEXCEPT
  291. : personality(person) { }
  292. #if __cplusplus >= 201103L
  293. uneq_allocator(const uneq_allocator&) = default;
  294. uneq_allocator(uneq_allocator&&) = default;
  295. #endif
  296. template<typename Tp1>
  297. uneq_allocator(const uneq_allocator<Tp1,
  298. typename AllocTraits::template rebind<Tp1>::other>& b)
  299. _GLIBCXX_USE_NOEXCEPT
  300. : personality(b.get_personality()) { }
  301. ~uneq_allocator() _GLIBCXX_USE_NOEXCEPT
  302. { }
  303. int get_personality() const { return personality; }
  304. pointer
  305. allocate(size_type n, const void* = 0)
  306. {
  307. pointer p = AllocTraits::allocate(*this, n);
  308. try
  309. {
  310. get_map().insert(map_type::value_type(reinterpret_cast<void*>(p),
  311. personality));
  312. }
  313. catch(...)
  314. {
  315. AllocTraits::deallocate(*this, p, n);
  316. __throw_exception_again;
  317. }
  318. return p;
  319. }
  320. void
  321. deallocate(pointer p, size_type n)
  322. {
  323. VERIFY( p );
  324. map_type::iterator it = get_map().find(reinterpret_cast<void*>(p));
  325. VERIFY( it != get_map().end() );
  326. // Enforce requirements in Table 32 about deallocation vs
  327. // allocator equality.
  328. VERIFY( it->second == personality );
  329. get_map().erase(it);
  330. AllocTraits::deallocate(*this, p, n);
  331. }
  332. #if __cplusplus >= 201103L
  333. // Not copy assignable...
  334. uneq_allocator&
  335. operator=(const uneq_allocator&) = delete;
  336. // ... but still moveable if base allocator is.
  337. uneq_allocator&
  338. operator=(uneq_allocator&&) = default;
  339. #else
  340. private:
  341. // Not assignable...
  342. uneq_allocator&
  343. operator=(const uneq_allocator&);
  344. #endif
  345. private:
  346. // ... yet swappable!
  347. friend inline void
  348. swap(uneq_allocator& a, uneq_allocator& b)
  349. {
  350. std::swap(a.personality, b.personality);
  351. a.swap_base(b);
  352. }
  353. template<typename Tp1>
  354. friend inline bool
  355. operator==(const uneq_allocator& a,
  356. const uneq_allocator<Tp1,
  357. typename AllocTraits::template rebind<Tp1>::other>& b)
  358. { return a.personality == b.personality; }
  359. template<typename Tp1>
  360. friend inline bool
  361. operator!=(const uneq_allocator& a,
  362. const uneq_allocator<Tp1,
  363. typename AllocTraits::template rebind<Tp1>::other>& b)
  364. { return !(a == b); }
  365. int personality;
  366. };
  367. #if __cplusplus >= 201103L
  368. // An uneq_allocator which can be used to test allocator propagation.
  369. template<typename Tp, bool Propagate, typename Alloc = std::allocator<Tp>>
  370. class propagating_allocator : public uneq_allocator<Tp, Alloc>
  371. {
  372. typedef __gnu_cxx::__alloc_traits<Alloc> AllocTraits;
  373. typedef uneq_allocator<Tp, Alloc> base_alloc;
  374. base_alloc& base() { return *this; }
  375. const base_alloc& base() const { return *this; }
  376. void swap_base(base_alloc& b) { swap(b, this->base()); }
  377. typedef std::integral_constant<bool, Propagate> trait_type;
  378. public:
  379. // default allocator_traits::rebind_alloc would select
  380. // uneq_allocator::rebind so we must define rebind here
  381. template<typename Up>
  382. struct rebind
  383. {
  384. typedef propagating_allocator<Up, Propagate,
  385. typename AllocTraits::template rebind<Up>::other> other;
  386. };
  387. propagating_allocator(int i) noexcept
  388. : base_alloc(i)
  389. { }
  390. template<typename Up>
  391. propagating_allocator(const propagating_allocator<Up, Propagate,
  392. typename AllocTraits::template rebind<Up>::other>& a)
  393. noexcept
  394. : base_alloc(a)
  395. { }
  396. propagating_allocator() noexcept = default;
  397. propagating_allocator(const propagating_allocator&) noexcept = default;
  398. propagating_allocator&
  399. operator=(const propagating_allocator& a) noexcept
  400. {
  401. static_assert(Propagate, "assigning propagating_allocator<T, true>");
  402. propagating_allocator(a).swap_base(*this);
  403. return *this;
  404. }
  405. template<bool P2>
  406. propagating_allocator&
  407. operator=(const propagating_allocator<Tp, P2, Alloc>& a) noexcept
  408. {
  409. static_assert(P2, "assigning propagating_allocator<T, true>");
  410. propagating_allocator(a).swap_base(*this);
  411. return *this;
  412. }
  413. // postcondition: LWG2593 a.get_personality() un-changed.
  414. propagating_allocator(propagating_allocator&& a) noexcept
  415. : base_alloc(std::move(a.base()))
  416. { }
  417. // postcondition: LWG2593 a.get_personality() un-changed
  418. propagating_allocator&
  419. operator=(propagating_allocator&& a) noexcept
  420. {
  421. propagating_allocator(std::move(a)).swap_base(*this);
  422. return *this;
  423. }
  424. typedef trait_type propagate_on_container_copy_assignment;
  425. typedef trait_type propagate_on_container_move_assignment;
  426. typedef trait_type propagate_on_container_swap;
  427. propagating_allocator select_on_container_copy_construction() const
  428. { return Propagate ? *this : propagating_allocator(); }
  429. };
  430. // Class template supporting the minimal interface that satisfies the
  431. // Allocator requirements, from example in [allocator.requirements]
  432. template <class Tp>
  433. struct SimpleAllocator
  434. {
  435. typedef Tp value_type;
  436. constexpr SimpleAllocator() noexcept { }
  437. template <class T>
  438. SimpleAllocator(const SimpleAllocator<T>&) { }
  439. Tp *allocate(std::size_t n)
  440. { return std::allocator<Tp>().allocate(n); }
  441. void deallocate(Tp *p, std::size_t n)
  442. { std::allocator<Tp>().deallocate(p, n); }
  443. };
  444. template <class T, class U>
  445. bool operator==(const SimpleAllocator<T>&, const SimpleAllocator<U>&)
  446. { return true; }
  447. template <class T, class U>
  448. bool operator!=(const SimpleAllocator<T>&, const SimpleAllocator<U>&)
  449. { return false; }
  450. template<typename T>
  451. struct default_init_allocator
  452. {
  453. using value_type = T;
  454. default_init_allocator() = default;
  455. template<typename U>
  456. default_init_allocator(const default_init_allocator<U>& a)
  457. : state(a.state)
  458. { }
  459. T*
  460. allocate(std::size_t n)
  461. { return std::allocator<T>().allocate(n); }
  462. void
  463. deallocate(T* p, std::size_t n)
  464. { std::allocator<T>().deallocate(p, n); }
  465. int state;
  466. };
  467. template<typename T, typename U>
  468. bool operator==(const default_init_allocator<T>& t,
  469. const default_init_allocator<U>& u)
  470. { return t.state == u.state; }
  471. template<typename T, typename U>
  472. bool operator!=(const default_init_allocator<T>& t,
  473. const default_init_allocator<U>& u)
  474. { return !(t == u); }
  475. #endif
  476. template<typename Tp>
  477. struct ExplicitConsAlloc : std::allocator<Tp>
  478. {
  479. ExplicitConsAlloc() { }
  480. template<typename Up>
  481. explicit
  482. ExplicitConsAlloc(const ExplicitConsAlloc<Up>&) { }
  483. template<typename Up>
  484. struct rebind
  485. { typedef ExplicitConsAlloc<Up> other; };
  486. };
  487. #if __cplusplus >= 201103L
  488. template<typename Tp>
  489. class CustomPointerAlloc : public std::allocator<Tp>
  490. {
  491. template<typename Up, typename Sp = __gnu_cxx::_Std_pointer_impl<Up>>
  492. using Ptr = __gnu_cxx::_Pointer_adapter<Sp>;
  493. public:
  494. CustomPointerAlloc() = default;
  495. template<typename Up>
  496. CustomPointerAlloc(const CustomPointerAlloc<Up>&) { }
  497. template<typename Up>
  498. struct rebind
  499. { typedef CustomPointerAlloc<Up> other; };
  500. typedef Ptr<Tp> pointer;
  501. typedef Ptr<const Tp> const_pointer;
  502. typedef Ptr<void> void_pointer;
  503. typedef Ptr<const void> const_void_pointer;
  504. pointer allocate(std::size_t n, const_void_pointer = {})
  505. { return pointer(std::allocator<Tp>::allocate(n)); }
  506. void deallocate(pointer p, std::size_t n)
  507. { std::allocator<Tp>::deallocate(std::addressof(*p), n); }
  508. };
  509. // A class type meeting *only* the Cpp17NullablePointer requirements.
  510. // Can be used as a base class for fancy pointers (like PointerBase, below)
  511. // or to wrap a built-in pointer type to remove operations not required
  512. // by the Cpp17NullablePointer requirements (dereference, increment etc.)
  513. template<typename Ptr>
  514. struct NullablePointer
  515. {
  516. // N.B. default constructor does not initialize value
  517. NullablePointer() = default;
  518. NullablePointer(std::nullptr_t) noexcept : value() { }
  519. explicit operator bool() const noexcept { return value != nullptr; }
  520. friend inline bool
  521. operator==(NullablePointer lhs, NullablePointer rhs) noexcept
  522. { return lhs.value == rhs.value; }
  523. friend inline bool
  524. operator!=(NullablePointer lhs, NullablePointer rhs) noexcept
  525. { return lhs.value != rhs.value; }
  526. protected:
  527. explicit NullablePointer(Ptr p) noexcept : value(p) { }
  528. Ptr value;
  529. };
  530. // NullablePointer<void> is an empty type that models Cpp17NullablePointer.
  531. template<>
  532. struct NullablePointer<void>
  533. {
  534. NullablePointer() = default;
  535. NullablePointer(std::nullptr_t) noexcept { }
  536. explicit NullablePointer(const volatile void*) noexcept { }
  537. explicit operator bool() const noexcept { return false; }
  538. friend inline bool
  539. operator==(NullablePointer, NullablePointer) noexcept
  540. { return true; }
  541. friend inline bool
  542. operator!=(NullablePointer, NullablePointer) noexcept
  543. { return false; }
  544. };
  545. // Utility for use as CRTP base class of custom pointer types
  546. template<typename Derived, typename T>
  547. struct PointerBase : NullablePointer<T*>
  548. {
  549. typedef T element_type;
  550. // typedefs for iterator_traits
  551. typedef T value_type;
  552. typedef std::ptrdiff_t difference_type;
  553. typedef std::random_access_iterator_tag iterator_category;
  554. typedef Derived pointer;
  555. typedef T& reference;
  556. using NullablePointer<T*>::NullablePointer;
  557. // Public (but explicit) constructor from raw pointer:
  558. explicit PointerBase(T* p) noexcept : NullablePointer<T*>(p) { }
  559. template<typename D, typename U,
  560. typename = decltype(static_cast<T*>(std::declval<U*>()))>
  561. PointerBase(const PointerBase<D, U>& p)
  562. : NullablePointer<T*>(p.operator->()) { }
  563. T& operator*() const { return *this->value; }
  564. T* operator->() const { return this->value; }
  565. T& operator[](difference_type n) const { return this->value[n]; }
  566. Derived& operator++() { ++this->value; return derived(); }
  567. Derived& operator--() { --this->value; return derived(); }
  568. Derived operator++(int) { return Derived(this->value++); }
  569. Derived operator--(int) { return Derived(this->value--); }
  570. Derived& operator+=(difference_type n)
  571. {
  572. this->value += n;
  573. return derived();
  574. }
  575. Derived& operator-=(difference_type n)
  576. {
  577. this->value -= n;
  578. return derived();
  579. }
  580. Derived
  581. operator+(difference_type n) const
  582. {
  583. Derived p(derived());
  584. return p += n;
  585. }
  586. Derived
  587. operator-(difference_type n) const
  588. {
  589. Derived p(derived());
  590. return p -= n;
  591. }
  592. private:
  593. friend std::ptrdiff_t operator-(PointerBase l, PointerBase r)
  594. { return l.value - r.value; }
  595. Derived&
  596. derived() { return static_cast<Derived&>(*this); }
  597. const Derived&
  598. derived() const { return static_cast<const Derived&>(*this); }
  599. };
  600. // implementation for pointer-to-void specializations
  601. template<typename T>
  602. struct PointerBase_void : NullablePointer<T*>
  603. {
  604. typedef T element_type;
  605. // typedefs for iterator_traits
  606. typedef T value_type;
  607. typedef std::ptrdiff_t difference_type;
  608. typedef std::random_access_iterator_tag iterator_category;
  609. using NullablePointer<T*>::NullablePointer;
  610. T* operator->() const { return this->value; }
  611. template<typename D, typename U,
  612. typename = decltype(static_cast<T*>(std::declval<U*>()))>
  613. PointerBase_void(const PointerBase<D, U>& p)
  614. : NullablePointer<T*>(p.operator->()) { }
  615. };
  616. template<typename Derived>
  617. struct PointerBase<Derived, void> : PointerBase_void<void>
  618. {
  619. using PointerBase_void::PointerBase_void;
  620. typedef Derived pointer;
  621. };
  622. template<typename Derived>
  623. struct PointerBase<Derived, const void> : PointerBase_void<const void>
  624. {
  625. using PointerBase_void::PointerBase_void;
  626. typedef Derived pointer;
  627. };
  628. #endif // C++11
  629. #if __cplusplus >= 201703L
  630. #if __cpp_aligned_new
  631. // A concrete memory_resource, with error checking.
  632. class memory_resource : public std::pmr::memory_resource
  633. {
  634. public:
  635. memory_resource()
  636. : lists(new allocation_lists)
  637. { }
  638. memory_resource(const memory_resource& r) noexcept
  639. : lists(r.lists)
  640. { lists->refcount++; }
  641. memory_resource& operator=(const memory_resource&) = delete;
  642. ~memory_resource()
  643. {
  644. if (lists->refcount-- == 1)
  645. delete lists; // last one out turns out the lights
  646. }
  647. struct bad_size { };
  648. struct bad_alignment { };
  649. struct bad_address { };
  650. // Deallocate everything (moving the tracking info to the freed list)
  651. void
  652. deallocate_everything()
  653. {
  654. while (lists->active)
  655. {
  656. auto a = lists->active;
  657. // Intentionally virtual dispatch, to inform derived classes:
  658. this->do_deallocate(a->p, a->bytes, a->alignment);
  659. }
  660. }
  661. // Clear the freed list
  662. void
  663. forget_freed_allocations()
  664. { lists->forget_allocations(lists->freed); }
  665. // Count how many allocations have been done and not freed.
  666. std::size_t
  667. number_of_active_allocations() const noexcept
  668. {
  669. std::size_t n = 0;
  670. for (auto a = lists->active; a != nullptr; a = a->next)
  671. ++n;
  672. return n;
  673. }
  674. protected:
  675. void*
  676. do_allocate(std::size_t bytes, std::size_t alignment) override
  677. {
  678. // TODO perform a single allocation and put the allocation struct
  679. // in the buffer using placement new? It means deallocation won't
  680. // actually return memory to the OS, as it will stay in lists->freed.
  681. //
  682. // TODO adjust the returned pointer to be minimally aligned?
  683. // e.g. if alignment==1 don't return something aligned to 2 bytes.
  684. // Maybe not worth it, at least monotonic_buffer_resource will
  685. // never ask upstream for anything with small alignment.
  686. void* p = ::operator new(bytes, std::align_val_t(alignment));
  687. lists->active = new allocation{p, bytes, alignment, lists->active};
  688. return p;
  689. }
  690. void
  691. do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override
  692. {
  693. allocation** aptr = &lists->active;
  694. while (*aptr)
  695. {
  696. allocation* a = *aptr;
  697. if (p == a->p)
  698. {
  699. if (bytes != a->bytes)
  700. _S_throw<bad_size>();
  701. if (alignment != a->alignment)
  702. _S_throw<bad_alignment>();
  703. #if __cpp_sized_deallocation
  704. ::operator delete(p, bytes, std::align_val_t(alignment));
  705. #else
  706. ::operator delete(p, std::align_val_t(alignment));
  707. #endif
  708. *aptr = a->next;
  709. a->next = lists->freed;
  710. lists->freed = a;
  711. return;
  712. }
  713. aptr = &a->next;
  714. }
  715. _S_throw<bad_address>();
  716. }
  717. bool
  718. do_is_equal(const std::pmr::memory_resource& r) const noexcept override
  719. {
  720. #if __cpp_rtti
  721. // Equality is determined by sharing the same allocation_lists object.
  722. if (auto p = dynamic_cast<const memory_resource*>(&r))
  723. return p->lists == lists;
  724. #else
  725. if (this == &r) // Is this the best we can do without RTTI?
  726. return true;
  727. #endif
  728. return false;
  729. }
  730. private:
  731. template<typename E>
  732. static void
  733. _S_throw()
  734. {
  735. #if __cpp_exceptions
  736. throw E();
  737. #else
  738. __builtin_abort();
  739. #endif
  740. }
  741. struct allocation
  742. {
  743. void* p;
  744. std::size_t bytes;
  745. std::size_t alignment;
  746. allocation* next;
  747. };
  748. // Maintain list of allocated blocks and list of freed blocks.
  749. // Copies of this memory_resource share the same ref-counted lists.
  750. struct allocation_lists
  751. {
  752. unsigned refcount = 1;
  753. allocation* active = nullptr;
  754. allocation* freed = nullptr;
  755. void forget_allocations(allocation*& list)
  756. {
  757. while (list)
  758. {
  759. auto p = list;
  760. list = list->next;
  761. delete p;
  762. }
  763. }
  764. ~allocation_lists()
  765. {
  766. forget_allocations(active); // Anything in this list is a leak!
  767. forget_allocations(freed);
  768. }
  769. };
  770. allocation_lists* lists;
  771. };
  772. #endif // aligned-new
  773. // Set the default resource, and restore the previous one on destruction.
  774. struct default_resource_mgr
  775. {
  776. explicit default_resource_mgr(std::pmr::memory_resource* r)
  777. : prev(std::pmr::set_default_resource(r))
  778. { }
  779. ~default_resource_mgr()
  780. { std::pmr::set_default_resource(prev); }
  781. std::pmr::memory_resource* prev;
  782. };
  783. #endif // C++17
  784. } // namespace __gnu_test
  785. #endif // _GLIBCXX_TESTSUITE_ALLOCATOR_H