_tempbuf.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996,1997
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_TEMPBUF_H
  29. #define _STLP_INTERNAL_TEMPBUF_H
  30. #ifndef _STLP_CLIMITS
  31. # include <climits>
  32. #endif
  33. #ifndef _STLP_INTERNAL_CSTDLIB
  34. # include <stl/_cstdlib.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_UNINITIALIZED_H
  37. # include <stl/_uninitialized.h>
  38. #endif
  39. _STLP_BEGIN_NAMESPACE
  40. template <class _Tp>
  41. pair<_Tp*, ptrdiff_t> _STLP_CALL
  42. __get_temporary_buffer(ptrdiff_t __len, _Tp*);
  43. #ifndef _STLP_NO_EXPLICIT_FUNCTION_TMPL_ARGS
  44. template <class _Tp>
  45. inline pair<_Tp*, ptrdiff_t> _STLP_CALL get_temporary_buffer(ptrdiff_t __len) {
  46. return __get_temporary_buffer(__len, (_Tp*) 0);
  47. }
  48. # if ! defined(_STLP_NO_EXTENSIONS)
  49. // This overload is not required by the standard; it is an extension.
  50. // It is supported for backward compatibility with the HP STL, and
  51. // because not all compilers support the language feature (explicit
  52. // function template arguments) that is required for the standard
  53. // version of get_temporary_buffer.
  54. template <class _Tp>
  55. inline pair<_Tp*, ptrdiff_t> _STLP_CALL
  56. get_temporary_buffer(ptrdiff_t __len, _Tp*) {
  57. return __get_temporary_buffer(__len, (_Tp*) 0);
  58. }
  59. # endif
  60. #endif
  61. template <class _Tp>
  62. inline void _STLP_CALL return_temporary_buffer(_Tp* __p) {
  63. // SunPro brain damage
  64. free((char*)__p);
  65. }
  66. template <class _ForwardIterator, class _Tp>
  67. class _Temporary_buffer {
  68. private:
  69. ptrdiff_t _M_original_len;
  70. ptrdiff_t _M_len;
  71. _Tp* _M_buffer;
  72. void _M_allocate_buffer() {
  73. _M_original_len = _M_len;
  74. _M_buffer = 0;
  75. if (_M_len > (ptrdiff_t)(INT_MAX / sizeof(_Tp)))
  76. _M_len = INT_MAX / sizeof(_Tp);
  77. while (_M_len > 0) {
  78. _M_buffer = (_Tp*) malloc(_M_len * sizeof(_Tp));
  79. if (_M_buffer)
  80. break;
  81. _M_len /= 2;
  82. }
  83. }
  84. void _M_initialize_buffer(const _Tp&, const __true_type&) {}
  85. void _M_initialize_buffer(const _Tp& val, const __false_type&) {
  86. uninitialized_fill_n(_M_buffer, _M_len, val);
  87. }
  88. public:
  89. ptrdiff_t size() const { return _M_len; }
  90. ptrdiff_t requested_size() const { return _M_original_len; }
  91. _Tp* begin() { return _M_buffer; }
  92. _Tp* end() { return _M_buffer + _M_len; }
  93. _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) {
  94. // Workaround for a __type_traits bug in the pre-7.3 compiler.
  95. # if defined(__sgi) && !defined(__GNUC__) && _COMPILER_VERSION < 730
  96. typedef typename __type_traits<_Tp>::is_POD_type _Trivial;
  97. # else
  98. typedef typename __type_traits<_Tp>::has_trivial_default_constructor _Trivial;
  99. # endif
  100. _STLP_TRY {
  101. _M_len = _STLP_STD::distance(__first, __last);
  102. _M_allocate_buffer();
  103. if (_M_len > 0)
  104. _M_initialize_buffer(*__first, _Trivial());
  105. }
  106. _STLP_UNWIND(free(_M_buffer); _M_buffer = 0; _M_len = 0)
  107. }
  108. ~_Temporary_buffer() {
  109. _STLP_STD::_Destroy_Range(_M_buffer, _M_buffer + _M_len);
  110. free(_M_buffer);
  111. }
  112. private:
  113. // Disable copy constructor and assignment operator.
  114. _Temporary_buffer(const _Temporary_buffer<_ForwardIterator, _Tp>&) {}
  115. void operator=(const _Temporary_buffer<_ForwardIterator, _Tp>&) {}
  116. };
  117. # ifndef _STLP_NO_EXTENSIONS
  118. // Class temporary_buffer is not part of the standard. It is an extension.
  119. template <class _ForwardIterator,
  120. class _Tp
  121. #ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
  122. = typename iterator_traits<_ForwardIterator>::value_type
  123. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  124. >
  125. struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
  126. {
  127. temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
  128. : _Temporary_buffer<_ForwardIterator, _Tp>(__first, __last) {}
  129. ~temporary_buffer() {}
  130. };
  131. # endif /* _STLP_NO_EXTENSIONS */
  132. _STLP_END_NAMESPACE
  133. # ifndef _STLP_LINK_TIME_INSTANTIATION
  134. # include <stl/_tempbuf.c>
  135. # endif
  136. #endif /* _STLP_INTERNAL_TEMPBUF_H */
  137. // Local Variables:
  138. // mode:C++
  139. // End: