memory 7.0 KB

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