memory 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Memory extensions -*- C++ -*-
  2. // Copyright (C) 2002-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. *
  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 <bits/requires_hosted.h> // GNU extensions are currently omitted
  53. #include <memory>
  54. #include <bits/stl_tempbuf.h>
  55. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  56. {
  57. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  58. using std::_Temporary_buffer;
  59. template<typename _InputIter, typename _Size, typename _ForwardIter>
  60. std::pair<_InputIter, _ForwardIter>
  61. __uninitialized_copy_n(_InputIter __first, _Size __count,
  62. _ForwardIter __result, std::input_iterator_tag)
  63. {
  64. _ForwardIter __cur = __result;
  65. __try
  66. {
  67. for (; __count > 0 ; --__count, ++__first, ++__cur)
  68. std::_Construct(&*__cur, *__first);
  69. return std::pair<_InputIter, _ForwardIter>(__first, __cur);
  70. }
  71. __catch(...)
  72. {
  73. std::_Destroy(__result, __cur);
  74. __throw_exception_again;
  75. }
  76. }
  77. template<typename _RandomAccessIter, typename _Size, typename _ForwardIter>
  78. inline std::pair<_RandomAccessIter, _ForwardIter>
  79. __uninitialized_copy_n(_RandomAccessIter __first, _Size __count,
  80. _ForwardIter __result,
  81. std::random_access_iterator_tag)
  82. {
  83. _RandomAccessIter __last = __first + __count;
  84. return (std::pair<_RandomAccessIter, _ForwardIter>
  85. (__last, std::uninitialized_copy(__first, __last, __result)));
  86. }
  87. template<typename _InputIter, typename _Size, typename _ForwardIter>
  88. inline std::pair<_InputIter, _ForwardIter>
  89. __uninitialized_copy_n(_InputIter __first, _Size __count,
  90. _ForwardIter __result)
  91. {
  92. return __gnu_cxx::__uninitialized_copy_n(__first, __count, __result,
  93. std::__iterator_category(__first));
  94. }
  95. /**
  96. * @brief Copies the range [first,last) into result.
  97. * @param __first An input iterator.
  98. * @param __count Length
  99. * @param __result An output iterator.
  100. * @return __result + (__first + __count)
  101. * @ingroup SGIextensions
  102. *
  103. * Like copy(), but does not require an initialized output range.
  104. */
  105. template<typename _InputIter, typename _Size, typename _ForwardIter>
  106. inline std::pair<_InputIter, _ForwardIter>
  107. uninitialized_copy_n(_InputIter __first, _Size __count,
  108. _ForwardIter __result)
  109. {
  110. return __gnu_cxx::__uninitialized_copy_n(__first, __count, __result,
  111. std::__iterator_category(__first));
  112. }
  113. // An alternative version of uninitialized_copy_n that constructs
  114. // and destroys objects with a user-provided allocator.
  115. template<typename _InputIter, typename _Size, typename _ForwardIter,
  116. typename _Allocator>
  117. std::pair<_InputIter, _ForwardIter>
  118. __uninitialized_copy_n_a(_InputIter __first, _Size __count,
  119. _ForwardIter __result,
  120. _Allocator __alloc)
  121. {
  122. _ForwardIter __cur = __result;
  123. __try
  124. {
  125. for (; __count > 0 ; --__count, ++__first, ++__cur)
  126. __alloc.construct(&*__cur, *__first);
  127. return std::pair<_InputIter, _ForwardIter>(__first, __cur);
  128. }
  129. __catch(...)
  130. {
  131. std::_Destroy(__result, __cur, __alloc);
  132. __throw_exception_again;
  133. }
  134. }
  135. template<typename _InputIter, typename _Size, typename _ForwardIter,
  136. typename _Tp>
  137. inline std::pair<_InputIter, _ForwardIter>
  138. __uninitialized_copy_n_a(_InputIter __first, _Size __count,
  139. _ForwardIter __result,
  140. std::allocator<_Tp>)
  141. {
  142. return __gnu_cxx::uninitialized_copy_n(__first, __count, __result);
  143. }
  144. /**
  145. * This class provides similar behavior and semantics of the standard
  146. * functions get_temporary_buffer() and return_temporary_buffer(), but
  147. * encapsulated in a type vaguely resembling a standard container.
  148. *
  149. * By default, a temporary_buffer<Iter> stores space for objects of
  150. * whatever type the Iter iterator points to. It is constructed from a
  151. * typical [first,last) range, and provides the begin(), end(), size()
  152. * functions, as well as requested_size(). For non-trivial types, copies
  153. * of *first will be used to initialize the storage.
  154. *
  155. * @c malloc is used to obtain underlying storage.
  156. *
  157. * Like get_temporary_buffer(), not all the requested memory may be
  158. * available. Ideally, the created buffer will be large enough to hold a
  159. * copy of [first,last), but if size() is less than requested_size(),
  160. * then this didn't happen.
  161. *
  162. * @ingroup SGIextensions
  163. */
  164. template <class _ForwardIterator, class _Tp
  165. = typename std::iterator_traits<_ForwardIterator>::value_type >
  166. struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
  167. {
  168. /// Requests storage large enough to hold a copy of [first,last).
  169. temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
  170. : _Temporary_buffer<_ForwardIterator, _Tp>(__first,
  171. std::distance(__first, __last))
  172. { }
  173. /// Destroys objects and frees storage.
  174. ~temporary_buffer() { }
  175. };
  176. _GLIBCXX_END_NAMESPACE_VERSION
  177. } // namespace
  178. #endif