re PR c++/16681 (array initialization in struct construct is a memory hog)
authorNathan Sidwell <nathan@codesourcery.com>
Thu, 9 Dec 2004 09:37:37 +0000 (09:37 +0000)
committerNathan Sidwell <nathan@gcc.gnu.org>
Thu, 9 Dec 2004 09:37:37 +0000 (09:37 +0000)
cp:
PR c++/16681
* init.c (build_zero_init): Build a RANGE_EXPR for an array
initializer.
testsuite:
PR c++/16681
* g++.dg/init/array15.C: New.
* g++.dg/init/array16.C: New.

From-SVN: r91928

gcc/cp/ChangeLog
gcc/cp/init.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/init/array15.C [new file with mode: 0644]
gcc/testsuite/g++.dg/init/array16.C [new file with mode: 0644]

index d37b9631dfd7db36e5bcba375b5d15dc9211fba1..2d73639bce1433cc824553b4d862c494fd5a1584 100644 (file)
@@ -1,3 +1,9 @@
+2004-12-09  Nathan Sidwell  <nathan@codesourcery.com>
+
+       PR c++/16681
+       * init.c (build_zero_init): Build a RANGE_EXPR for an array
+       initializer.
+
 2004-12-08  Kelley Cook  <kcook@gcc.gnu.org>
 
        * typeck.c: Remove DOS line endings.
index 95b57b2e8efb93b61b7e1caa93a46d88362019c2..70703efacacd0fe244f8729288b88ee234d737c2 100644 (file)
@@ -214,7 +214,6 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
     }
   else if (TREE_CODE (type) == ARRAY_TYPE)
     {
-      tree index;
       tree max_index;
       tree inits;
 
@@ -228,14 +227,16 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
       /* A zero-sized array, which is accepted as an extension, will
         have an upper bound of -1.  */
       if (!tree_int_cst_equal (max_index, integer_minus_one_node))
-       for (index = size_zero_node;
-            !tree_int_cst_lt (max_index, index);
-            index = size_binop (PLUS_EXPR, index, size_one_node))
-         inits = tree_cons (index,
-                            build_zero_init (TREE_TYPE (type),
-                                             /*nelts=*/NULL_TREE,
-                                             static_storage_p),
-                            inits);
+       {
+         tree elt_init = build_zero_init (TREE_TYPE (type),
+                                          /*nelts=*/NULL_TREE,
+                                          static_storage_p);
+         tree range = build2 (RANGE_EXPR,
+                              sizetype, size_zero_node, max_index);
+         
+         inits = tree_cons (range, elt_init, inits);
+       }
+      
       CONSTRUCTOR_ELTS (init) = nreverse (inits);
     }
   else
index f946bf10399c1ad1827bdbd9aec545f167dcb03f..d8ad0adbf58a070b3c5933600490020c12da2247 100644 (file)
@@ -1,3 +1,9 @@
+2004-12-09  Nathan Sidwell  <nathan@codesourcery.com>
+
+       PR c++/16681
+       * g++.dg/init/array15.C: New.
+       * g++.dg/init/array16.C: New.
+
 2004-12-08  Tobias Schlueter  <tobias.schlueter@physik.uni-muenchen.de>
 
        PR fortran/18826
diff --git a/gcc/testsuite/g++.dg/init/array15.C b/gcc/testsuite/g++.dg/init/array15.C
new file mode 100644 (file)
index 0000000..17160d0
--- /dev/null
@@ -0,0 +1,46 @@
+// { dg-do run }
+
+// Copyright (C) 2004 Free Software Foundation, Inc.
+// Contributed by Nathan Sidwell 8 Dec 2004 <nathan@codesourcery.com>
+
+// PR 16681 too much memory used
+// Origin:  Matt LaFary <lafary@activmedia.com>
+
+struct foo {
+  unsigned char buffer[4111222];
+  foo() ;
+  bool check () const;
+};
+
+foo::foo ()
+  : buffer()
+{}
+
+bool foo::check () const
+{
+  for (unsigned ix = sizeof (buffer); ix--;)
+    if (buffer[ix])
+      return false;
+  return true;
+}
+
+void *operator new (__SIZE_TYPE__ size, void *p)
+{
+  return p;
+}
+
+char heap[5000000];
+
+int main ()
+{
+  for (unsigned ix = sizeof (heap); ix--;)
+    heap[ix] = ix;
+  
+  foo *f = new (heap) foo ();
+
+  if (!f->check ())
+    return 1;
+  return 0;
+}
+
+  
diff --git a/gcc/testsuite/g++.dg/init/array16.C b/gcc/testsuite/g++.dg/init/array16.C
new file mode 100644 (file)
index 0000000..fa4c1b6
--- /dev/null
@@ -0,0 +1,106 @@
+// { dg-do run }
+
+// Copyright (C) 2004 Free Software Foundation, Inc.
+// Contributed by Nathan Sidwell 8 Dec 2004 <nathan@codesourcery.com>
+
+// PR 16681 too much memory used
+// Origin:  Matt LaFary <lafary@activmedia.com>
+
+
+struct elt 
+{
+  static int count;
+  static elt*ptr;
+  static int abort;
+  char c;
+  
+  elt ();
+  ~elt ();
+  
+};
+
+int elt::count;
+elt *elt::ptr;
+int elt::abort;
+
+elt::elt ()
+  :c ()
+{
+  if (count >= 0)
+    {
+      if (!ptr)
+       ptr = this;
+      if (count == 100)
+       throw 2;
+      if (this != ptr)
+       abort = 1;
+      count++;
+      ptr++;
+    }
+}
+
+elt::~elt ()
+{
+  if (count >= 0)
+    {
+      ptr--;
+      count--;
+      if (ptr != this)
+       abort = 2;
+    }
+}
+
+struct foo {
+  elt buffer[4111222];
+  foo() ;
+  bool check () const;
+};
+
+foo::foo ()
+  : buffer()
+{}
+
+bool foo::check () const
+{
+  for (unsigned ix = sizeof (buffer)/ sizeof (buffer[0]); ix--;)
+    if (buffer[ix].c)
+      return false;
+  return true;
+}
+
+void *operator new (__SIZE_TYPE__ size, void *p)
+{
+  return p;
+}
+
+char heap[5000000];
+
+int main ()
+{
+  for (unsigned ix = sizeof (heap); ix--;)
+    heap[ix] = ix;
+
+  try
+    {
+      foo *f = new (heap) foo ();
+      return 1;
+    }
+  catch (...)
+    {
+      if (elt::count)
+       return 2;
+      if (elt::abort)
+       return elt::abort + 3;
+    }
+
+  for (unsigned ix = sizeof (heap); ix--;)
+    heap[ix] = ix;
+
+  elt::count = -1;
+  foo *f = new (heap) foo ();
+  if (!f->check ())
+    return 3;
+  return 0;
+}
+
+