debug_allocator.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Allocators -*- C++ -*-
  2. // Copyright (C) 2001-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
  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 <stdexcept>
  38. #include <bits/functexcept.h>
  39. #include <ext/alloc_traits.h>
  40. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  41. {
  42. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  43. using std::size_t;
  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 _Alloc2::template rebind<value_type>::other>
  77. struct __convertible
  78. { };
  79. template<typename _Alloc2>
  80. struct __convertible<_Alloc2, _Alloc>
  81. {
  82. typedef void* __type;
  83. };
  84. size_type _S_extra()
  85. {
  86. const size_t __obj_size = sizeof(value_type);
  87. return (sizeof(size_type) + __obj_size - 1) / __obj_size;
  88. }
  89. public:
  90. debug_allocator() : _M_extra(_S_extra()) { }
  91. template<typename _Alloc2>
  92. debug_allocator(const debug_allocator<_Alloc2>& __a2,
  93. typename __convertible<_Alloc2>::__type = 0)
  94. : _M_allocator(__a2._M_allocator), _M_extra(_S_extra()) { }
  95. debug_allocator(const _Alloc& __a)
  96. : _M_allocator(__a), _M_extra(_S_extra()) { }
  97. pointer
  98. allocate(size_type __n)
  99. {
  100. pointer __res = _M_allocator.allocate(__n + _M_extra);
  101. size_type* __ps = reinterpret_cast<size_type*>(__res);
  102. *__ps = __n;
  103. return __res + _M_extra;
  104. }
  105. pointer
  106. allocate(size_type __n, const void* __hint)
  107. {
  108. pointer __res = _M_allocator.allocate(__n + _M_extra, __hint);
  109. size_type* __ps = reinterpret_cast<size_type*>(__res);
  110. *__ps = __n;
  111. return __res + _M_extra;
  112. }
  113. void
  114. deallocate(pointer __p, size_type __n)
  115. {
  116. using std::__throw_runtime_error;
  117. if (__p)
  118. {
  119. pointer __real_p = __p - _M_extra;
  120. if (*reinterpret_cast<size_type*>(__real_p) != __n)
  121. __throw_runtime_error("debug_allocator::deallocate wrong size");
  122. _M_allocator.deallocate(__real_p, __n + _M_extra);
  123. }
  124. else
  125. __throw_runtime_error("debug_allocator::deallocate null pointer");
  126. }
  127. void
  128. construct(pointer __p, const value_type& __val)
  129. { _Traits::construct(_M_allocator, __p, __val); }
  130. #if __cplusplus >= 201103L
  131. template<typename _Tp, typename... _Args>
  132. void
  133. construct(_Tp* __p, _Args&&... __args)
  134. {
  135. _Traits::construct(_M_allocator, __p,
  136. std::forward<_Args>(__args)...);
  137. }
  138. #endif
  139. template<typename _Tp>
  140. void
  141. destroy(_Tp* __p)
  142. { _Traits::destroy(_M_allocator, __p); }
  143. size_type
  144. max_size() const throw()
  145. { return _Traits::max_size(_M_allocator) - _M_extra; }
  146. friend bool
  147. operator==(const debug_allocator& __lhs, const debug_allocator& __rhs)
  148. { return __lhs._M_allocator == __rhs._M_allocator; }
  149. };
  150. template<typename _Alloc>
  151. inline bool
  152. operator!=(const debug_allocator<_Alloc>& __lhs,
  153. const debug_allocator<_Alloc>& __rhs)
  154. { return !(__lhs == __rhs); }
  155. _GLIBCXX_END_NAMESPACE_VERSION
  156. } // namespace
  157. #endif