diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 5e849a4d0d2..62a66d09844 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,17 @@ +2002-01-24 Phil Edwards + + * include/bits/stl_tempbuf.h (_Temporary_buffer): Add doxygen hook. + * include/bits/stl_algo.h: Include stl_tempbuf.h. + * include/ext/memory: Do not include stl_tempbuf.h. + (temporary_buffer): Add doxygen hook. + (__get_temporary_buffer, get_temporary_buffer, + return_temporary_buffer): Move back to std:: header... + * include/std/std_memory.h: ...here. Do not include stl_tempbuf.h. + * include/ext/rope: Do not include stl_tempbuf.h. + * include/ext/stl_hashtable.h: Likewise. + * include/std/std_algorithm.h: Likewise. + * testsuite/20_util/temporary_buffer.cc: New file. + 2002-01-24 andrew@andypo.net (tweaks, test and commit by Loren J. Rittle ) diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index e8e9845f822..a8059b74a37 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -62,6 +62,7 @@ #define __GLIBCPP_INTERNAL_ALGO_H #include +#include // for _Temporary_buffer // See concept_check.h for the __glibcpp_*_requires macros. diff --git a/libstdc++-v3/include/bits/stl_tempbuf.h b/libstdc++-v3/include/bits/stl_tempbuf.h index 05565e02888..0bf4718df2e 100644 --- a/libstdc++-v3/include/bits/stl_tempbuf.h +++ b/libstdc++-v3/include/bits/stl_tempbuf.h @@ -1,6 +1,6 @@ // Temporary buffer implementation -*- C++ -*- -// Copyright (C) 2001 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -64,6 +64,12 @@ namespace std { +/** + * @maint + * This class is used in two places: stl_algo.h and ext/memory, where it + * is wrapped as the temporary_buffer class. + * @endmaint +*/ template class _Temporary_buffer { private: @@ -71,6 +77,7 @@ private: ptrdiff_t _M_len; _Tp* _M_buffer; + // this is basically get_temporary_buffer() all over again void _M_allocate_buffer() { _M_original_len = _M_len; _M_buffer = 0; diff --git a/libstdc++-v3/include/ext/memory b/libstdc++-v3/include/ext/memory index 73fa42783c9..3318611f048 100644 --- a/libstdc++-v3/include/ext/memory +++ b/libstdc++-v3/include/ext/memory @@ -58,6 +58,7 @@ #pragma GCC system_header #include +#include namespace __gnu_cxx { @@ -124,45 +125,14 @@ namespace __gnu_cxx __iterator_category(__first)); } - template - pair<_Tp*, ptrdiff_t> - __get_temporary_buffer(ptrdiff_t __len, _Tp*) - { - if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp))) - __len = INT_MAX / sizeof(_Tp); - - while (__len > 0) { - _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp)); - if (__tmp != 0) - return pair<_Tp*, ptrdiff_t>(__tmp, __len); - __len /= 2; - } - - return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0); - } /** - * This is a mostly-useless wrapper around malloc(). - */ - template - inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) { - return __get_temporary_buffer(__len, (_Tp*) 0); - } - - /** - * The companion to get_temporary_buffer(). - */ - template - void return_temporary_buffer(_Tp* __p) { - std::free(__p); - } - - // Class temporary_buffer is not part of the standard. It is an extension. - + * Must come back and figure out these notes. + * @ingroup SGIextensions + */ template ::value_type - > + = typename std::iterator_traits<_ForwardIterator>::value_type > struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp> { temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) diff --git a/libstdc++-v3/include/ext/rope b/libstdc++-v3/include/ext/rope index 78bb7dc8570..c52db59d5a1 100644 --- a/libstdc++-v3/include/ext/rope +++ b/libstdc++-v3/include/ext/rope @@ -1,6 +1,6 @@ // SGI's rope class -*- C++ -*- -// Copyright (C) 2001 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -49,7 +49,6 @@ #define __SGI_STL_ROPE #include -#include #include #include #include diff --git a/libstdc++-v3/include/ext/stl_hashtable.h b/libstdc++-v3/include/ext/stl_hashtable.h index 23176386aec..2d5c110cb14 100644 --- a/libstdc++-v3/include/ext/stl_hashtable.h +++ b/libstdc++-v3/include/ext/stl_hashtable.h @@ -1,6 +1,6 @@ // Hashtable implementation used by containers -*- C++ -*- -// Copyright (C) 2001 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -67,7 +67,6 @@ #include #include #include -#include #include #include #include diff --git a/libstdc++-v3/include/std/std_algorithm.h b/libstdc++-v3/include/std/std_algorithm.h index 746eb47541b..ff5d257d6e6 100644 --- a/libstdc++-v3/include/std/std_algorithm.h +++ b/libstdc++-v3/include/std/std_algorithm.h @@ -1,6 +1,6 @@ // -*- C++ -*- -// Copyright (C) 2001 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -66,7 +66,6 @@ #include #include #include -#include #include #endif /* _CPP_ALGORITHM */ diff --git a/libstdc++-v3/include/std/std_memory.h b/libstdc++-v3/include/std/std_memory.h index c9a2e59e48e..6ca31c214f6 100644 --- a/libstdc++-v3/include/std/std_memory.h +++ b/libstdc++-v3/include/std/std_memory.h @@ -1,6 +1,6 @@ // -*- C++ -*- -// Copyright (C) 2001 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -55,14 +55,71 @@ #include #include #include //for iterator_traits -#include #include #include namespace std { - template struct auto_ptr_ref { + /** + * @maint + * This is a helper function. The unused second parameter exists to + * permit the real get_temporary_buffer to use template parameter deduction. + * @endmaint + */ + template + pair<_Tp*, ptrdiff_t> + __get_temporary_buffer(ptrdiff_t __len, _Tp*) + { + if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp))) + __len = INT_MAX / sizeof(_Tp); + + while (__len > 0) { + _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp)); + if (__tmp != 0) + return pair<_Tp*, ptrdiff_t>(__tmp, __len); + __len /= 2; + } + + return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0); + } + + /** + * @brief This is a mostly-useless wrapper around malloc(). + * @param len The number of objects of type Tp. + * @return See full description. + * + * Reinventing the wheel, but this time with prettier spokes! + * + * This function tries to obtain storage for @c len adjacent Tp objects. + * The objects themselves are not constructed, of course. A pair<> is + * returned containing "the buffer s address and capacity (in the units of + * sizeof(Tp)), or a pair of 0 values if no storage can be obtained." + * Note that the capacity obtained may be less than that requested if the + * memory is unavailable; you should compare len with the .second return + * value. + */ + template + inline pair<_Tp*, ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) { + return __get_temporary_buffer(__len, (_Tp*) 0); + } + + /** + * @brief The companion to get_temporary_buffer(). + * @param p A buffer previously allocated by get_temporary_buffer. + * @return None. + * + * Frees the memory pointed to by p. + */ + template + void return_temporary_buffer(_Tp* __p) { + std::free(__p); + } + + +template + struct auto_ptr_ref +{ _Tp1* _M_ptr; auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {} }; @@ -70,7 +127,9 @@ namespace std /** * A simple smart pointer providing strict ownership semantics. (More later.) */ -template class auto_ptr { +template + class auto_ptr +{ private: _Tp* _M_ptr; diff --git a/libstdc++-v3/testsuite/20_util/temporary_buffer.cc b/libstdc++-v3/testsuite/20_util/temporary_buffer.cc new file mode 100644 index 00000000000..602b2fe80b7 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/temporary_buffer.cc @@ -0,0 +1,50 @@ +// 2002-01-24 Phil Edwards + +// Copyright (C) 2002 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING. If not, write to the Free +// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, +// USA. + +// 20.4.3 temporary buffers + +#include +#include + +struct junk { char j[12]; }; + +int main(void) +{ + bool test = true; + + std::pair results = std::get_temporary_buffer(5); + + if (results.second != 0) + { + // make sure it works: test the returned capacity, and then construct + // some junk in the buffer. + // XXX + VERIFY( results.first != 0 ); + } + else + { + // if it says it didn't work, make sure it didn't work + VERIFY( results.first == 0 ); + } + + std::return_temporary_buffer(results.first); + + return 0; +}