array_allocator.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // array allocator -*- C++ -*-
  2. // Copyright (C) 2004-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. /** @file ext/array_allocator.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _ARRAY_ALLOCATOR_H
  24. #define _ARRAY_ALLOCATOR_H 1
  25. #include <bits/c++config.h>
  26. #include <new>
  27. #include <bits/functexcept.h>
  28. #include <tr1/array>
  29. #include <bits/move.h>
  30. #if __cplusplus >= 201103L
  31. #include <type_traits>
  32. #endif
  33. // Suppress deprecated warning for this file.
  34. #pragma GCC diagnostic push
  35. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  36. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  37. {
  38. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  39. using std::size_t;
  40. using std::ptrdiff_t;
  41. /// Base class.
  42. template<typename _Tp>
  43. class array_allocator_base
  44. {
  45. public:
  46. typedef size_t size_type;
  47. typedef ptrdiff_t difference_type;
  48. typedef _Tp* pointer;
  49. typedef const _Tp* const_pointer;
  50. typedef _Tp& reference;
  51. typedef const _Tp& const_reference;
  52. typedef _Tp value_type;
  53. pointer
  54. address(reference __x) const _GLIBCXX_NOEXCEPT
  55. { return std::__addressof(__x); }
  56. const_pointer
  57. address(const_reference __x) const _GLIBCXX_NOEXCEPT
  58. { return std::__addressof(__x); }
  59. void
  60. deallocate(pointer, size_type)
  61. {
  62. // Does nothing.
  63. }
  64. size_type
  65. max_size() const _GLIBCXX_USE_NOEXCEPT
  66. { return size_t(-1) / sizeof(_Tp); }
  67. #if __cplusplus >= 201103L
  68. template<typename _Up, typename... _Args>
  69. void
  70. construct(_Up* __p, _Args&&... __args)
  71. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  72. template<typename _Up>
  73. void
  74. destroy(_Up* __p) { __p->~_Up(); }
  75. #else
  76. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  77. // 402. wrong new expression in [some_] allocator::construct
  78. void
  79. construct(pointer __p, const _Tp& __val)
  80. { ::new((void *)__p) value_type(__val); }
  81. void
  82. destroy(pointer __p) { __p->~_Tp(); }
  83. #endif
  84. } _GLIBCXX_DEPRECATED;
  85. /**
  86. * @brief An allocator that uses previously allocated memory.
  87. * This memory can be externally, globally, or otherwise allocated.
  88. * @ingroup allocators
  89. */
  90. template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
  91. class array_allocator : public array_allocator_base<_Tp>
  92. {
  93. public:
  94. typedef size_t size_type;
  95. typedef ptrdiff_t difference_type;
  96. typedef _Tp* pointer;
  97. typedef const _Tp* const_pointer;
  98. typedef _Tp& reference;
  99. typedef const _Tp& const_reference;
  100. typedef _Tp value_type;
  101. typedef _Array array_type;
  102. #if __cplusplus >= 201103L
  103. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  104. // 2103. std::allocator propagate_on_container_move_assignment
  105. typedef std::true_type propagate_on_container_move_assignment;
  106. typedef std::true_type is_always_equal;
  107. #endif
  108. private:
  109. array_type* _M_array;
  110. size_type _M_used;
  111. public:
  112. template<typename _Tp1, typename _Array1 = _Array>
  113. struct rebind
  114. {
  115. typedef array_allocator<_Tp1, _Array1> other _GLIBCXX_DEPRECATED;
  116. } _GLIBCXX_DEPRECATED;
  117. array_allocator(array_type* __array = 0) _GLIBCXX_USE_NOEXCEPT
  118. : _M_array(__array), _M_used(size_type()) { }
  119. array_allocator(const array_allocator& __o) _GLIBCXX_USE_NOEXCEPT
  120. : _M_array(__o._M_array), _M_used(__o._M_used) { }
  121. template<typename _Tp1, typename _Array1>
  122. array_allocator(const array_allocator<_Tp1, _Array1>&)
  123. _GLIBCXX_USE_NOEXCEPT
  124. : _M_array(0), _M_used(size_type()) { }
  125. ~array_allocator() _GLIBCXX_USE_NOEXCEPT { }
  126. pointer
  127. allocate(size_type __n, const void* = 0)
  128. {
  129. if (_M_array == 0 || _M_used + __n > _M_array->size())
  130. std::__throw_bad_alloc();
  131. pointer __ret = _M_array->begin() + _M_used;
  132. _M_used += __n;
  133. return __ret;
  134. }
  135. } _GLIBCXX_DEPRECATED;
  136. template<typename _Tp, typename _Array>
  137. inline bool
  138. operator==(const array_allocator<_Tp, _Array>&,
  139. const array_allocator<_Tp, _Array>&)
  140. { return true; }
  141. template<typename _Tp, typename _Array>
  142. inline bool
  143. operator!=(const array_allocator<_Tp, _Array>&,
  144. const array_allocator<_Tp, _Array>&)
  145. { return false; }
  146. _GLIBCXX_END_NAMESPACE_VERSION
  147. } // namespace
  148. #pragma GCC diagnostic pop
  149. #endif