debug_allocator.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Allocators -*- C++ -*-
  2. // Copyright (C) 2001-2023 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
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU 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. /*
  21. * Copyright (c) 1996-1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. */
  32. /** @file ext/debug_allocator.h
  33. * This file is a GNU extension to the Standard C++ Library.
  34. */
  35. #ifndef _DEBUG_ALLOCATOR_H
  36. #define _DEBUG_ALLOCATOR_H 1
  37. #include <bits/requires_hosted.h> // GNU extensions are currently omitted
  38. #include <stdexcept>
  39. #include <bits/functexcept.h>
  40. #include <ext/alloc_traits.h>
  41. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  42. {
  43. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  44. /**
  45. * @brief A meta-allocator with debugging bits.
  46. * @ingroup allocators
  47. *
  48. * This is precisely the allocator defined in the C++03 Standard.
  49. */
  50. template<typename _Alloc>
  51. class debug_allocator
  52. {
  53. template<typename> friend class debug_allocator;
  54. typedef __alloc_traits<_Alloc> _Traits;
  55. public:
  56. typedef typename _Traits::size_type size_type;
  57. typedef typename _Traits::difference_type difference_type;
  58. typedef typename _Traits::pointer pointer;
  59. typedef typename _Traits::const_pointer const_pointer;
  60. typedef typename _Traits::reference reference;
  61. typedef typename _Traits::const_reference const_reference;
  62. typedef typename _Traits::value_type value_type;
  63. template<typename _Up>
  64. class rebind
  65. {
  66. typedef typename _Traits::template rebind<_Up>::other __other;
  67. public:
  68. typedef debug_allocator<__other> other;
  69. };
  70. private:
  71. // _M_extra is the number of objects that correspond to the
  72. // extra space where debug information is stored.
  73. size_type _M_extra;
  74. _Alloc _M_allocator;
  75. template<typename _Alloc2,
  76. typename = typename __alloc_traits<_Alloc2>::template
  77. rebind<value_type>::other>
  78. struct __convertible
  79. { };
  80. template<typename _Alloc2>
  81. struct __convertible<_Alloc2, _Alloc>
  82. {
  83. typedef void* __type;
  84. };
  85. size_type _S_extra()
  86. {
  87. const std::size_t __obj_size = sizeof(value_type);
  88. return (sizeof(size_type) + __obj_size - 1) / __obj_size;
  89. }
  90. public:
  91. debug_allocator() : _M_extra(_S_extra()) { }
  92. template<typename _Alloc2>
  93. debug_allocator(const debug_allocator<_Alloc2>& __a2,
  94. typename __convertible<_Alloc2>::__type = 0)
  95. : _M_extra(_S_extra()), _M_allocator(__a2._M_allocator) { }
  96. debug_allocator(const _Alloc& __a)
  97. : _M_extra(_S_extra()), _M_allocator(__a) { }
  98. _GLIBCXX_NODISCARD pointer
  99. allocate(size_type __n)
  100. {
  101. pointer __res = _M_allocator.allocate(__n + _M_extra);
  102. size_type* __ps = reinterpret_cast<size_type*>(__res);
  103. *__ps = __n;
  104. return __res + _M_extra;
  105. }
  106. _GLIBCXX_NODISCARD pointer
  107. allocate(size_type __n, const void* __hint)
  108. {
  109. pointer __res = _M_allocator.allocate(__n + _M_extra, __hint);
  110. size_type* __ps = reinterpret_cast<size_type*>(__res);
  111. *__ps = __n;
  112. return __res + _M_extra;
  113. }
  114. void
  115. deallocate(pointer __p, size_type __n)
  116. {
  117. using std::__throw_runtime_error;
  118. if (__p)
  119. {
  120. pointer __real_p = __p - _M_extra;
  121. if (*reinterpret_cast<size_type*>(__real_p) != __n)
  122. __throw_runtime_error("debug_allocator::deallocate wrong size");
  123. _M_allocator.deallocate(__real_p, __n + _M_extra);
  124. }
  125. else
  126. __throw_runtime_error("debug_allocator::deallocate null pointer");
  127. }
  128. void
  129. construct(pointer __p, const value_type& __val)
  130. { _Traits::construct(_M_allocator, __p, __val); }
  131. #if __cplusplus >= 201103L
  132. template<typename _Tp, typename... _Args>
  133. void
  134. construct(_Tp* __p, _Args&&... __args)
  135. {
  136. _Traits::construct(_M_allocator, __p,
  137. std::forward<_Args>(__args)...);
  138. }
  139. #endif
  140. template<typename _Tp>
  141. void
  142. destroy(_Tp* __p)
  143. { _Traits::destroy(_M_allocator, __p); }
  144. size_type
  145. max_size() const throw()
  146. { return _Traits::max_size(_M_allocator) - _M_extra; }
  147. template<typename _Alloc2>
  148. friend bool
  149. operator==(const debug_allocator& __lhs,
  150. const debug_allocator<_Alloc2>& __rhs) _GLIBCXX_NOTHROW
  151. { return __lhs._M_allocator == debug_allocator(__rhs)._M_allocator; }
  152. #if __cpp_impl_three_way_comparison < 201907L
  153. template<typename _Alloc2>
  154. friend bool
  155. operator!=(const debug_allocator& __lhs,
  156. const debug_allocator<_Alloc2>& __rhs) _GLIBCXX_NOTHROW
  157. { return !(__lhs == __rhs); }
  158. #endif
  159. };
  160. _GLIBCXX_END_NAMESPACE_VERSION
  161. } // namespace
  162. #endif