Commit 94763647 by Nathan Sidwell Committed by Nathan Sidwell

re PR c++/16681 (array initialization in struct construct is a memory hog)

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
parent 42fabf21
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> 2004-12-08 Kelley Cook <kcook@gcc.gnu.org>
* typeck.c: Remove DOS line endings. * typeck.c: Remove DOS line endings.
......
...@@ -214,7 +214,6 @@ build_zero_init (tree type, tree nelts, bool static_storage_p) ...@@ -214,7 +214,6 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
} }
else if (TREE_CODE (type) == ARRAY_TYPE) else if (TREE_CODE (type) == ARRAY_TYPE)
{ {
tree index;
tree max_index; tree max_index;
tree inits; tree inits;
...@@ -228,14 +227,16 @@ build_zero_init (tree type, tree nelts, bool static_storage_p) ...@@ -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 /* A zero-sized array, which is accepted as an extension, will
have an upper bound of -1. */ have an upper bound of -1. */
if (!tree_int_cst_equal (max_index, integer_minus_one_node)) if (!tree_int_cst_equal (max_index, integer_minus_one_node))
for (index = size_zero_node; {
!tree_int_cst_lt (max_index, index); tree elt_init = build_zero_init (TREE_TYPE (type),
index = size_binop (PLUS_EXPR, index, size_one_node)) /*nelts=*/NULL_TREE,
inits = tree_cons (index, static_storage_p);
build_zero_init (TREE_TYPE (type), tree range = build2 (RANGE_EXPR,
/*nelts=*/NULL_TREE, sizetype, size_zero_node, max_index);
static_storage_p),
inits); inits = tree_cons (range, elt_init, inits);
}
CONSTRUCTOR_ELTS (init) = nreverse (inits); CONSTRUCTOR_ELTS (init) = nreverse (inits);
} }
else else
......
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> 2004-12-08 Tobias Schlueter <tobias.schlueter@physik.uni-muenchen.de>
PR fortran/18826 PR fortran/18826
......
// { 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;
}
// { 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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment