1 // { dg-do run }
2 
3 // Copyright (C) 2004 Free Software Foundation, Inc.
4 // Contributed by Nathan Sidwell 8 Dec 2004 <nathan@codesourcery.com>
5 
6 // PR 16681 too much memory used
7 // Origin:  Matt LaFary <lafary@activmedia.com>
8 
9 
10 struct elt
11 {
12   static int count;
13   static elt*ptr;
14   static int abort;
15   char c;
16 
17   elt ();
18   ~elt ();
19 
20 };
21 
22 int elt::count;
23 elt *elt::ptr;
24 int elt::abort;
25 
elt()26 elt::elt ()
27   :c ()
28 {
29   if (count >= 0)
30     {
31       if (!ptr)
32 	ptr = this;
33       if (count == 100)
34 	throw 2;
35       if (this != ptr)
36 	abort = 1;
37       count++;
38       ptr++;
39     }
40 }
41 
~elt()42 elt::~elt ()
43 {
44   if (count >= 0)
45     {
46       ptr--;
47       count--;
48       if (ptr != this)
49 	abort = 2;
50     }
51 }
52 
53 struct foo {
54   elt buffer[4111222];
55   foo() ;
56   bool check () const;
57 };
58 
foo()59 foo::foo ()
60   : buffer()
61 {}
62 
check()63 bool foo::check () const
64 {
65   for (unsigned ix = sizeof (buffer)/ sizeof (buffer[0]); ix--;)
66     if (buffer[ix].c)
67       return false;
68   return true;
69 }
70 
new(__SIZE_TYPE__ size,void * p)71 void *operator new (__SIZE_TYPE__ size, void *p)
72 {
73   return p;
74 }
75 
76 char heap[5000000];
77 
main()78 int main ()
79 {
80   for (unsigned ix = sizeof (heap); ix--;)
81     heap[ix] = ix;
82 
83   try
84     {
85       foo *f = new (heap) foo ();
86       return 1;
87     }
88   catch (...)
89     {
90       if (elt::count)
91 	return 2;
92       if (elt::abort)
93 	return elt::abort + 3;
94     }
95 
96   for (unsigned ix = sizeof (heap); ix--;)
97     heap[ix] = ix;
98 
99   elt::count = -1;
100   foo *f = new (heap) foo ();
101   if (!f->check ())
102     return 3;
103   return 0;
104 }
105 
106 
107