Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Memory extensions -*- C++ -*-
  2. // Copyright (C) 2002-2020 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. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file ext/memory
  46. * This file is a GNU extension to the Standard C++ Library (possibly
  47. * containing extensions from the HP/SGI STL subset).
  48. */
  49. #ifndef _EXT_MEMORY
  50. #define _EXT_MEMORY 1
  51. #pragma GCC system_header
  52. #include <memory>
  53. #include <bits/stl_tempbuf.h>
  54. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  55. {
  56. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  57. using std::_Temporary_buffer;
  58. template<typename _InputIter, typename _Size, typename _ForwardIter>
  59. std::pair<_InputIter, _ForwardIter>
  60. __uninitialized_copy_n(_InputIter __first, _Size __count,
  61. _ForwardIter __result, std::input_iterator_tag)
  62. {
  63. _ForwardIter __cur = __result;
  64. __try
  65. {
  66. for (; __count > 0 ; --__count, ++__first, ++__cur)
  67. std::_Construct(&*__cur, *__first);
  68. return std::pair<_InputIter, _ForwardIter>(__first, __cur);
  69. }
  70. __catch(...)
  71. {
  72. std::_Destroy(__result, __cur);
  73. __throw_exception_again;
  74. }
  75. }
  76. template<typename _RandomAccessIter, typename _Size, typename _ForwardIter>
  77. inline std::pair<_RandomAccessIter, _ForwardIter>
  78. __uninitialized_copy_n(_RandomAccessIter __first, _Size __count,
  79. _ForwardIter __result,
  80. std::random_access_iterator_tag)
  81. {
  82. _RandomAccessIter __last = __first + __count;
  83. return (std::pair<_RandomAccessIter, _ForwardIter>
  84. (__last, std::uninitialized_copy(__first, __last, __result)));
  85. }
  86. template<typename _InputIter, typename _Size, typename _ForwardIter>
  87. inline std::pair<_InputIter, _ForwardIter>
  88. __uninitialized_copy_n(_InputIter __first, _Size __count,
  89. _ForwardIter __result)
  90. {
  91. return __gnu_cxx::__uninitialized_copy_n(__first, __count, __result,
  92. std::__iterator_category(__first));
  93. }
  94. /**
  95. * @brief Copies the range [first,last) into result.
  96. * @param __first An input iterator.
  97. * @param __count Length
  98. * @param __result An output iterator.
  99. * @return __result + (__first + __count)
  100. * @ingroup SGIextensions
  101. *
  102. * Like copy(), but does not require an initialized output range.
  103. */
  104. template<typename _InputIter, typename _Size, typename _ForwardIter>
  105. inline std::pair<_InputIter, _ForwardIter>
  106. uninitialized_copy_n(_InputIter __first, _Size __count,
  107. _ForwardIter __result)
  108. {
  109. return __gnu_cxx::__uninitialized_copy_n(__first, __count, __result,
  110. std::__iterator_category(__first));
  111. }
  112. // An alternative version of uninitialized_copy_n that constructs
  113. // and destroys objects with a user-provided allocator.
  114. template<typename _InputIter, typename _Size, typename _ForwardIter,
  115. typename _Allocator>
  116. std::pair<_InputIter, _ForwardIter>
  117. __uninitialized_copy_n_a(_InputIter __first, _Size __count,
  118. _ForwardIter __result,
  119. _Allocator __alloc)
  120. {
  121. _ForwardIter __cur = __result;
  122. __try
  123. {
  124. for (; __count > 0 ; --__count, ++__first, ++__cur)
  125. __alloc.construct(&*__cur, *__first);
  126. return std::pair<_InputIter, _ForwardIter>(__first, __cur);
  127. }
  128. __catch(...)
  129. {
  130. std::_Destroy(__result, __cur, __alloc);
  131. __throw_exception_again;
  132. }
  133. }
  134. template<typename _InputIter, typename _Size, typename _ForwardIter,
  135. typename _Tp>
  136. inline std::pair<_InputIter, _ForwardIter>
  137. __uninitialized_copy_n_a(_InputIter __first, _Size __count,
  138. _ForwardIter __result,
  139. std::allocator<_Tp>)
  140. {
  141. return __gnu_cxx::uninitialized_copy_n(__first, __count, __result);
  142. }
  143. /**
  144. * This class provides similar behavior and semantics of the standard
  145. * functions get_temporary_buffer() and return_temporary_buffer(), but
  146. * encapsulated in a type vaguely resembling a standard container.
  147. *
  148. * By default, a temporary_buffer<Iter> stores space for objects of
  149. * whatever type the Iter iterator points to. It is constructed from a
  150. * typical [first,last) range, and provides the begin(), end(), size()
  151. * functions, as well as requested_size(). For non-trivial types, copies
  152. * of *first will be used to initialize the storage.
  153. *
  154. * @c malloc is used to obtain underlying storage.
  155. *
  156. * Like get_temporary_buffer(), not all the requested memory may be
  157. * available. Ideally, the created buffer will be large enough to hold a
  158. * copy of [first,last), but if size() is less than requested_size(),
  159. * then this didn't happen.
  160. *
  161. * @ingroup SGIextensions
  162. */
  163. template <class _ForwardIterator, class _Tp
  164. = typename std::iterator_traits<_ForwardIterator>::value_type >
  165. struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
  166. {
  167. /// Requests storage large enough to hold a copy of [first,last).
  168. temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
  169. : _Temporary_buffer<_ForwardIterator, _Tp>(__first,
  170. std::distance(__first, __last))
  171. { }
  172. /// Destroys objects and frees storage.
  173. ~temporary_buffer() { }
  174. };
  175. _GLIBCXX_END_NAMESPACE_VERSION
  176. } // namespace
  177. #endif