From: Stephen M. Webb Date: Fri, 30 Jan 2004 03:43:00 +0000 (+0000) Subject: c_locale.h: Change ::malloc() to new char[]. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=917a9fd4d5ce9ee5c906abb10812a316b77fc31c;p=gcc.git c_locale.h: Change ::malloc() to new char[]. 2004-01-29 Stephen M. Webb * config/local/generic/c_locale.h: Change ::malloc() to new char[]. * config/local/gnu/c_locale.h: Change ::malloc() to new char[]. * include/bits/stl_tempbuf.h: Convert _Temporary_buffer to use std::get_temporary_buffer() instead of duplicating its code. Update to C++STYLE conventions. * include/std/std_memory.h (get_temporary_buffer): Use ::operator new() instead of std::malloc(). (return_temporary_buffer): Use ::operator delete() instead of std::free(). From-SVN: r76922 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 2065b92ef75..e973cce9ac4 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2004-01-29 Stephen M. Webb + + * config/local/generic/c_locale.h: Change ::malloc() to new char[]. + * config/local/gnu/c_locale.h: Change ::malloc() to new char[]. + * include/bits/stl_tempbuf.h: Convert _Temporary_buffer to use + std::get_temporary_buffer() instead of duplicating its code. + Update to C++STYLE conventions. + * include/std/std_memory.h (get_temporary_buffer): Use ::operator + new() instead of std::malloc(). + (return_temporary_buffer): Use ::operator delete() instead of + std::free(). + 2004-01-29 Benjamin Kosnik * include/bits/allocator.h: Temporary switch to new_allocator as diff --git a/libstdc++-v3/config/locale/generic/c_locale.h b/libstdc++-v3/config/locale/generic/c_locale.h index c20b98ba24d..9fadcc4d99c 100644 --- a/libstdc++-v3/config/locale/generic/c_locale.h +++ b/libstdc++-v3/config/locale/generic/c_locale.h @@ -1,6 +1,6 @@ // Wrapper for underlying C-language localization -*- C++ -*- -// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002, 2003, 2004 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 @@ -39,7 +39,6 @@ #pragma GCC system_header #include -#include // get std::malloc #include // get std::strlen #include // get std::snprintf or std::sprintf @@ -61,9 +60,8 @@ namespace std _Tv __v, const __c_locale&, int __prec = -1) { char* __old = std::setlocale(LC_ALL, NULL); - char* __sav = static_cast(std::malloc(std::strlen(__old) + 1)); - if (__sav) - std::strcpy(__sav, __old); + char* __sav = new char[std::strlen(__old) + 1]; + std::strcpy(__sav, __old); std::setlocale(LC_ALL, "C"); int __ret; @@ -79,7 +77,7 @@ namespace std __ret = std::sprintf(__out, __fmt, __v); #endif std::setlocale(LC_ALL, __sav); - std::free(__sav); + delete [] __sav; return __ret; } } diff --git a/libstdc++-v3/config/locale/gnu/c_locale.h b/libstdc++-v3/config/locale/gnu/c_locale.h index 069074eb9fe..fe899cfc2a5 100644 --- a/libstdc++-v3/config/locale/gnu/c_locale.h +++ b/libstdc++-v3/config/locale/gnu/c_locale.h @@ -1,6 +1,6 @@ // Wrapper for underlying C-language localization -*- C++ -*- -// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002, 2003, 2004 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 @@ -39,7 +39,6 @@ #pragma GCC system_header #include // get std::strlen -#include // get std::malloc #include // get std::snprintf or std::sprintf #include #include // For codecvt @@ -76,9 +75,8 @@ namespace std _Tv __v, const __c_locale&, int __prec = -1) { char* __old = std::setlocale(LC_ALL, NULL); - char* __sav = static_cast(std::malloc(std::strlen(__old) + 1)); - if (__sav) - std::strcpy(__sav, __old); + char* __sav = new char[std::strlen(__old) + 1]; + std::strcpy(__sav, __old); std::setlocale(LC_ALL, "C"); #endif @@ -99,7 +97,7 @@ namespace std __gnu_cxx::__uselocale(__old); #else std::setlocale(LC_ALL, __sav); - std::free(__sav); + delete [] __sav; #endif return __ret; } diff --git a/libstdc++-v3/include/bits/stl_tempbuf.h b/libstdc++-v3/include/bits/stl_tempbuf.h index 0526c0d7706..87080c6220f 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, 2002 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002, 2004 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 @@ -61,88 +61,107 @@ #ifndef _TEMPBUF_H #define _TEMPBUF_H 1 -namespace std -{ +#include -/** - * @if maint - * This class is used in two places: stl_algo.h and ext/memory, where it - * is wrapped as the temporary_buffer class. See temporary_buffer docs for - * more notes. - * @endif -*/ -template - class _Temporary_buffer +namespace std { - // concept requirements - __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept) - - ptrdiff_t _M_original_len; - 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; - - if (_M_len > (ptrdiff_t)(INT_MAX / sizeof(_Tp))) - _M_len = INT_MAX / sizeof(_Tp); - - while (_M_len > 0) { - _M_buffer = (_Tp*) malloc(_M_len * sizeof(_Tp)); - if (_M_buffer) - break; - _M_len /= 2; - } - } - - void _M_initialize_buffer(const _Tp&, __true_type) {} - void _M_initialize_buffer(const _Tp& val, __false_type) { - std::uninitialized_fill_n(_M_buffer, _M_len, val); - } - -public: - /// As per Table mumble. - ptrdiff_t size() const { return _M_len; } - /// Returns the size requested by the constructor; may be >size(). - ptrdiff_t requested_size() const { return _M_original_len; } - /// As per Table mumble. - _Tp* begin() { return _M_buffer; } - /// As per Table mumble. - _Tp* end() { return _M_buffer + _M_len; } - - _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) { - // Workaround for a __type_traits bug in the pre-7.3 compiler. - typedef typename __type_traits<_Tp>::has_trivial_default_constructor - _Trivial; - - try { - _M_len = std::distance(__first, __last); - _M_allocate_buffer(); - if (_M_len > 0) - _M_initialize_buffer(*__first, _Trivial()); - } - catch(...) + /** + * @if maint + * This class is used in two places: stl_algo.h and ext/memory, + * where it is wrapped as the temporary_buffer class. See + * temporary_buffer docs for more notes. + * @endif + */ + template + class _Temporary_buffer + { + // concept requirements + __glibcxx_class_requires(_ForwardIterator, _ForwardIteratorConcept) + + public: + typedef _Tp value_type; + typedef value_type* pointer; + typedef pointer iterator; + typedef ptrdiff_t size_type; + + protected: + size_type _M_original_len; + size_type _M_len; + pointer _M_buffer; + + void + _M_initialize_buffer(const _Tp&, __true_type) { } + + void + _M_initialize_buffer(const _Tp& val, __false_type) + { std::uninitialized_fill_n(_M_buffer, _M_len, val); } + + public: + /// As per Table mumble. + size_type + size() const + { return _M_len; } + + /// Returns the size requested by the constructor; may be >size(). + size_type + requested_size() const + { return _M_original_len; } + + /// As per Table mumble. + iterator + begin() + { return _M_buffer; } + + /// As per Table mumble. + iterator + end() + { return _M_buffer + _M_len; } + + /** + * Constructs a temporary buffer of a size somewhere between + * zero and the size of the given range. + */ + _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last); + + ~_Temporary_buffer() + { + std::_Destroy(_M_buffer, _M_buffer + _M_len); + std::return_temporary_buffer(_M_buffer); + } + + private: + // Disable copy constructor and assignment operator. + _Temporary_buffer(const _Temporary_buffer&); + void operator=(const _Temporary_buffer&); + }; + + + template + _Temporary_buffer<_ForwardIterator, _Tp>:: + _Temporary_buffer(_ForwardIterator __first, _ForwardIterator __last) + : _M_original_len(std::distance(__first, __last)), + _M_len(0) , _M_buffer(0) + { + // Workaround for a __type_traits bug in the pre-7.3 compiler. + typedef typename __type_traits<_Tp>::has_trivial_default_constructor + _Trivial; + + try + { + pair __p(get_temporary_buffer(_M_original_len)); + _M_buffer = __p.first; + _M_len = __p.second; + if (_M_len > 0) + _M_initialize_buffer(*__first, _Trivial()); + } + catch(...) { - std::free(_M_buffer); + std::return_temporary_buffer(_M_buffer); _M_buffer = 0; _M_len = 0; __throw_exception_again; } - } - - ~_Temporary_buffer() { - std::_Destroy(_M_buffer, _M_buffer + _M_len); - std::free(_M_buffer); - } - -private: - // Disable copy constructor and assignment operator. - _Temporary_buffer(const _Temporary_buffer&) {} - void operator=(const _Temporary_buffer&) {} -}; - + } } // namespace std #endif /* _TEMPBUF_H */ diff --git a/libstdc++-v3/include/bits/stl_uninitialized.h b/libstdc++-v3/include/bits/stl_uninitialized.h index e60e24a1c01..1111646fd74 100644 --- a/libstdc++-v3/include/bits/stl_uninitialized.h +++ b/libstdc++-v3/include/bits/stl_uninitialized.h @@ -65,9 +65,7 @@ namespace std { - // uninitialized_copy - template inline _ForwardIterator __uninitialized_copy_aux(_InputIterator __first, _InputIterator __last, @@ -76,17 +74,18 @@ namespace std { return std::copy(__first, __last, __result); } template - _ForwardIterator + inline _ForwardIterator __uninitialized_copy_aux(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, __false_type) { _ForwardIterator __cur = __result; - try { - for ( ; __first != __last; ++__first, ++__cur) - std::_Construct(&*__cur, *__first); - return __cur; - } + try + { + for ( ; __first != __last; ++__first, ++__cur) + std::_Construct(&*__cur, *__first); + return __cur; + } catch(...) { std::_Destroy(__result, __cur); @@ -105,7 +104,8 @@ namespace std */ template inline _ForwardIterator - uninitialized_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result) + uninitialized_copy(_InputIterator __first, _InputIterator __last, + _ForwardIterator __result) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD; @@ -131,13 +131,15 @@ namespace std // destructor is trivial. template inline void - __uninitialized_fill_aux(_ForwardIterator __first, _ForwardIterator __last, + __uninitialized_fill_aux(_ForwardIterator __first, + _ForwardIterator __last, const _Tp& __x, __true_type) { std::fill(__first, __last, __x); } template void - __uninitialized_fill_aux(_ForwardIterator __first, _ForwardIterator __last, + __uninitialized_fill_aux(_ForwardIterator __first, + _ForwardIterator __last, const _Tp& __x, __false_type) { _ForwardIterator __cur = __first; @@ -163,7 +165,8 @@ namespace std */ template inline void - uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __x) + uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, + const _Tp& __x) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; typedef typename __type_traits<_ValueType>::is_POD_type _Is_POD; @@ -176,9 +179,7 @@ namespace std inline _ForwardIterator __uninitialized_fill_n_aux(_ForwardIterator __first, _Size __n, const _Tp& __x, __true_type) - { - return std::fill_n(__first, __n, __x); - } + { return std::fill_n(__first, __n, __x); } template _ForwardIterator @@ -186,11 +187,12 @@ namespace std const _Tp& __x, __false_type) { _ForwardIterator __cur = __first; - try { - for ( ; __n > 0; --__n, ++__cur) - std::_Construct(&*__cur, __x); - return __cur; - } + try + { + for ( ; __n > 0; --__n, ++__cur) + std::_Construct(&*__cur, __x); + return __cur; + } catch(...) { std::_Destroy(__first, __cur); @@ -224,10 +226,13 @@ namespace std // copies [first2, last2) into // [result, result + (last1 - first1) + (last2 - first2)). - template + template inline _ForwardIterator - __uninitialized_copy_copy(_InputIterator1 __first1, _InputIterator1 __last1, - _InputIterator2 __first2, _InputIterator2 __last2, + __uninitialized_copy_copy(_InputIterator1 __first1, + _InputIterator1 __last1, + _InputIterator2 __first2, + _InputIterator2 __last2, _ForwardIterator __result) { _ForwardIterator __mid = std::uninitialized_copy(__first1, __last1, __result); @@ -270,10 +275,12 @@ namespace std _ForwardIterator __first2, _ForwardIterator __last2, const _Tp& __x) { - _ForwardIterator __mid2 = std::uninitialized_copy(__first1, __last1, __first2); - try { - std::uninitialized_fill(__mid2, __last2, __x); - } + _ForwardIterator __mid2 = std::uninitialized_copy(__first1, __last1, + __first2); + try + { + std::uninitialized_fill(__mid2, __last2, __x); + } catch(...) { std::_Destroy(__first2, __mid2); diff --git a/libstdc++-v3/include/std/std_memory.h b/libstdc++-v3/include/std/std_memory.h index eb2923276f7..4e6641ef673 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, 2002 Free Software Foundation, Inc. +// Copyright (C) 2001, 2002, 2004 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 @@ -78,33 +78,36 @@ namespace std while (__len > 0) { - _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp)); + _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), + nothrow)); if (__tmp != 0) return pair<_Tp*, ptrdiff_t>(__tmp, __len); __len /= 2; } - return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0); + return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0); } /** - * @brief This is a mostly-useless wrapper around malloc(). + * @brief Allocates a temporary buffer. * @param len The number of objects of type Tp. - * @return See full description. + * @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. + * 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. + * + * Provides the nothrow exception guarantee. */ template inline pair<_Tp*,ptrdiff_t> get_temporary_buffer(ptrdiff_t __len) - { return std::__get_temporary_buffer(__len, (_Tp*) 0); } + { return std::__get_temporary_buffer(__len, static_cast<_Tp*>(0)); } /** * @brief The companion to get_temporary_buffer(). @@ -116,12 +119,12 @@ namespace std template void return_temporary_buffer(_Tp* __p) - { std::free(__p); } + { ::operator delete(__p, nothrow); } /** - * A wrapper class to provide auto_ptr with reference semantics. For - * example, an auto_ptr can be assigned (or constructed from) the result of - * a function which returns an auto_ptr by value. + * A wrapper class to provide auto_ptr with reference semantics. + * For example, an auto_ptr can be assigned (or constructed from) + * the result of a function which returns an auto_ptr by value. * * All the auto_ptr_ref stuff should happen behind the scenes. */ @@ -140,23 +143,25 @@ namespace std * * The Standard says: *
-   *  An @c auto_ptr owns the object it holds a pointer to.  Copying an
-   *  @c auto_ptr copies the pointer and transfers ownership to the destination.
-   *  If more than one @c auto_ptr owns the same object at the same time the
-   *  behavior of the program is undefined.
+   *  An @c auto_ptr owns the object it holds a pointer to.  Copying
+   *  an @c auto_ptr copies the pointer and transfers ownership to the
+   *  destination.  If more than one @c auto_ptr owns the same object
+   *  at the same time the behavior of the program is undefined.
    *
-   *  The uses of @c auto_ptr include providing temporary exception-safety for
-   *  dynamically allocated memory, passing ownership of dynamically allocated
-   *  memory to a function, and returning dynamically allocated memory from a
-   *  function.  @c auto_ptr does not meet the CopyConstructible and Assignable
-   *  requirements for Standard Library container
-   *  elements and thus instantiating a Standard Library container with an
-   *  @c auto_ptr results in undefined behavior.
+   *  The uses of @c auto_ptr include providing temporary
+   *  exception-safety for dynamically allocated memory, passing
+   *  ownership of dynamically allocated memory to a function, and
+   *  returning dynamically allocated memory from a function.  @c
+   *  auto_ptr does not meet the CopyConstructible and Assignable
+   *  requirements for Standard Library container elements and thus
+   *  instantiating a Standard Library container with an @c auto_ptr
+   *  results in undefined behavior.
    *  
* Quoted from [20.4.5]/3. * - * Good examples of what can and cannot be done with auto_ptr can be found - * in the libstdc++ testsuite. + * Good examples of what can and cannot be done with auto_ptr can + * be found in the libstdc++ testsuite. * * @if maint * _GLIBCXX_RESOLVE_LIB_DEFECTS @@ -196,7 +201,8 @@ namespace std * @brief An %auto_ptr can be constructed from another %auto_ptr. * @param a Another %auto_ptr of a different but related type. * - * A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type. + * A pointer-to-Tp1 must be convertible to a + * pointer-to-Tp/element_type. * * This object now @e owns the object previously owned by @a a, * which has given up ownsership. @@ -238,9 +244,9 @@ namespace std } /** - * When the %auto_ptr goes out of scope, the object it owns is deleted. - * If it no longer owns anything (i.e., @c get() is @c NULL), then this - * has no effect. + * When the %auto_ptr goes out of scope, the object it owns is + * deleted. If it no longer owns anything (i.e., @c get() is + * @c NULL), then this has no effect. * * @if maint * The C++ standard says there is supposed to be an empty throw @@ -284,8 +290,8 @@ namespace std * @return The raw pointer being managed. * * You can get a copy of the pointer that this object owns, for - * situations such as passing to a function which only accepts a raw - * pointer. + * situations such as passing to a function which only accepts + * a raw pointer. * * @note This %auto_ptr still owns the memory. */ @@ -297,8 +303,8 @@ namespace std * @return The raw pointer being managed. * * You can get a copy of the pointer that this object owns, for - * situations such as passing to a function which only accepts a raw - * pointer. + * situations such as passing to a function which only accepts + * a raw pointer. * * @note This %auto_ptr no longer owns the memory. When this object * goes out of scope, nothing will happen. @@ -315,8 +321,8 @@ namespace std * @brief Forcibly deletes the managed object. * @param p A pointer (defaults to NULL). * - * This object now @e owns the object pointed to by @a p. The previous - * object has been deleted. + * This object now @e owns the object pointed to by @a p. The + * previous object has been deleted. */ void reset(element_type* __p = 0) throw() diff --git a/libstdc++-v3/testsuite/20_util/auto_ptr/assign_neg.cc b/libstdc++-v3/testsuite/20_util/auto_ptr/assign_neg.cc index 15a6ae43986..55291676f3d 100644 --- a/libstdc++-v3/testsuite/20_util/auto_ptr/assign_neg.cc +++ b/libstdc++-v3/testsuite/20_util/auto_ptr/assign_neg.cc @@ -1,6 +1,6 @@ // { dg-do compile } -// Copyright (C) 2002, 2003 Free Software Foundation +// Copyright (C) 2002, 2003, 2004 Free Software Foundation // // 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 @@ -46,5 +46,5 @@ main() test01(); return 0; } -// { dg-error "candidates" "" { target *-*-* } 217 } -// { dg-error "std::auto_ptr" "" { target *-*-* } 347 } +// { dg-error "candidates" "" { target *-*-* } 223 } +// { dg-error "std::auto_ptr" "" { target *-*-* } 353 }