[libstdc++] major oops in <memory> -> <ext/memory> move

Phil Edwards pedwards@dmapub.dma.org
Thu Jan 24 20:13:00 GMT 2002


When Paolo moved SGI's extensions from std_memory.h and stl_tempbuf.h into
<ext/memory>, he moved too much and none of us caught it.  The never-used
std::get/return_temporary_buffer() got shifted also.  Nobody noticed because,
well, these functions are never used.  They're tiny and easily overlooked.

(Note that no bug reports have been received on the subsequent snapshots.
/Nobody/ uses these functions...)

This patch moves them back into std_memory.  I also removed the unnecessary
inclusion of stl_tempbuf.h from a few places, since it now contains
almost nothing.

I doxygenated them (some more than others) while I was moving them, just
because I could.  (Doxygenating was what I was doing when I noticed the
accident.)

There's a testsuite entry for get/return_temporary_buffer now.  It doesn't
actually verify correct behavior (and notes this), because frankly I didn't
get around to it, and even more frankly it's hard to get a call to malloc()
wrong...  Anyhow, it will at least catch any future accidental moves.

Tested on i686-linux.  Committed to mainline.



2002-01-24  Phil Edwards  <pme@gcc.gnu.org>

	* 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.


Index: include/bits/stl_algo.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_algo.h,v
retrieving revision 1.18
diff -u -3 -p -r1.18 stl_algo.h
--- stl_algo.h	2002/01/03 04:44:06	1.18
+++ stl_algo.h	2002/01/25 04:06:01
@@ -62,6 +62,7 @@
 #define __GLIBCPP_INTERNAL_ALGO_H
 
 #include <bits/stl_heap.h>
+#include <bits/stl_tempbuf.h>     // for _Temporary_buffer
 
 // See concept_check.h for the __glibcpp_*_requires macros.
 
Index: include/bits/stl_tempbuf.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/stl_tempbuf.h,v
retrieving revision 1.12
diff -u -3 -p -r1.12 stl_tempbuf.h
--- stl_tempbuf.h	2002/01/04 17:02:26	1.12
+++ stl_tempbuf.h	2002/01/25 04:06:01
@@ -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 _ForwardIterator, class _Tp>
 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;
Index: include/ext/memory
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/ext/memory,v
retrieving revision 1.3
diff -u -3 -p -r1.3 memory
--- memory	2002/01/04 21:27:33	1.3
+++ memory	2002/01/25 04:06:01
@@ -58,6 +58,7 @@
 
 #pragma GCC system_header
 #include <memory>
+#include <bits/stl_tempbuf.h>
 
 namespace __gnu_cxx
 {
@@ -124,45 +125,14 @@ namespace __gnu_cxx
 				    __iterator_category(__first));
     }
 
-  template <class _Tp>
-  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 <class _Tp>
-  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 <class _Tp>
-  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 <class _ForwardIterator, 
 	    class _Tp 
-	    = typename std::iterator_traits<_ForwardIterator>::value_type
-  >
+	      = typename std::iterator_traits<_ForwardIterator>::value_type >
   struct temporary_buffer : public _Temporary_buffer<_ForwardIterator, _Tp>
   {
     temporary_buffer(_ForwardIterator __first, _ForwardIterator __last)
Index: include/ext/rope
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/ext/rope,v
retrieving revision 1.5
diff -u -3 -p -r1.5 rope
--- rope	2001/11/20 00:51:37	1.5
+++ rope	2002/01/25 04:06:01
@@ -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 <bits/stl_algobase.h>
-#include <bits/stl_tempbuf.h>
 #include <bits/stl_algo.h>
 #include <bits/stl_function.h>
 #include <bits/stl_numeric.h>
Index: include/ext/stl_hashtable.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/ext/stl_hashtable.h,v
retrieving revision 1.13
diff -u -3 -p -r1.13 stl_hashtable.h
--- stl_hashtable.h	2001/12/31 16:16:17	1.13
+++ stl_hashtable.h	2002/01/25 04:06:01
@@ -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 <bits/stl_algobase.h>
 #include <bits/stl_alloc.h>
 #include <bits/stl_construct.h>
-#include <bits/stl_tempbuf.h>
 #include <bits/stl_algo.h>
 #include <bits/stl_uninitialized.h>
 #include <bits/stl_function.h>
Index: include/std/std_algorithm.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/std/std_algorithm.h,v
retrieving revision 1.1
diff -u -3 -p -r1.1 std_algorithm.h
--- std_algorithm.h	2002/01/12 00:50:04	1.1
+++ std_algorithm.h	2002/01/25 04:06:01
@@ -1,6 +1,6 @@
 // <algorithm> -*- 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 <bits/stl_algobase.h>
 #include <bits/stl_construct.h>
 #include <bits/stl_uninitialized.h>
-#include <bits/stl_tempbuf.h>
 #include <bits/stl_algo.h>
 
 #endif /* _CPP_ALGORITHM */
Index: include/std/std_memory.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/std/std_memory.h,v
retrieving revision 1.1
diff -u -3 -p -r1.1 std_memory.h
--- std_memory.h	2002/01/12 00:50:04	1.1
+++ std_memory.h	2002/01/25 04:06:01
@@ -1,6 +1,6 @@
 // <memory> -*- 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 <bits/stl_alloc.h>
 #include <bits/stl_construct.h>
 #include <bits/stl_iterator_base_types.h> //for iterator_traits
-#include <bits/stl_tempbuf.h>
 #include <bits/stl_uninitialized.h>
 #include <bits/stl_raw_storage_iter.h>
 
 namespace std
 {
 
- template<class _Tp1> 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 <class _Tp>
+  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 <class _Tp>
+  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 <class _Tp>
+  void return_temporary_buffer(_Tp* __p) {
+    std::free(__p);
+  }
+
+
+template <class _Tp1>
+  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 _Tp> class auto_ptr {
+template <class _Tp>
+  class auto_ptr
+{
 private:
   _Tp* _M_ptr;
 
Index: testsuite/20_util/temporary_buffer.cc
===================================================================
RCS file: temporary_buffer.cc
diff -N temporary_buffer.cc
--- /dev/null	Tue May  5 13:32:27 1998
+++ temporary_buffer.cc	Thu Jan 24 20:06:01 2002
@@ -0,0 +1,50 @@
+// 2002-01-24  Phil Edwards  <pme@gcc.gnu.org>
+
+// 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 <memory>
+#include <testsuite_hooks.h>
+
+struct junk { char j[12]; };
+
+int main(void)
+{
+  bool test = true;
+
+  std::pair<junk*,ptrdiff_t>  results = std::get_temporary_buffer<junk>(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;
+}



More information about the Libstdc++ mailing list