OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [testsuite/] [g++.old-deja/] [g++.eh/] [badalloc1.C] - Blame information for rev 823

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 149 jeremybenn
// { dg-do run { xfail xstormy16-*-* *-*-darwin[1-7]* } }
2
// Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
3
// Contributed by Nathan Sidwell 6 June 2000 
4
 
5
// Check we can throw a bad_alloc exception when malloc dies.
6
 
7
typedef __SIZE_TYPE__ size_t;
8
extern "C" void abort();
9
extern "C" void *memcpy(void *, const void *, size_t);
10
 
11
// Assume that STACK_SIZE defined implies a system that does not have a
12
// large data space either, and additionally that we're not linking against
13
// a shared libstdc++ (which requires quite a bit more initialization space).
14
#ifdef STACK_SIZE
15
const int arena_size = 256;
16
#else
17
#if defined(__FreeBSD__) || defined(__sun__) || defined(__hpux__)
18
// FreeBSD, Solaris and HP-UX with threads require even more
19
// space at initialization time.  FreeBSD 5 now requires over 131072 bytes.
20
const int arena_size = 262144;
21
#else
22
const int arena_size = 32768;
23
#endif
24
#endif
25
 
26
struct object
27
{
28
  size_t size __attribute__((aligned));
29
};
30
 
31
static char arena[arena_size] __attribute__((aligned));
32
static size_t pos;
33
 
34
// So we can force a failure when needed.
35
static int fail;
36
 
37
extern "C" void *malloc (size_t size)
38
{
39
  object *p = reinterpret_cast(&arena[pos]);
40
 
41
  if (fail)
42
    return 0;
43
 
44
  p->size = size;
45
  size = (size + __alignof__(object) - 1) & - __alignof__(object);
46
  pos += size + sizeof(object);
47
 
48
  // Verify that we didn't run out of memory before getting initialized.
49
  if (pos > arena_size)
50
    abort ();
51
 
52
  return p + 1;
53
}
54
 
55
extern "C" void free (void *)
56
{
57
}
58
 
59
extern "C" void *realloc (void *p, size_t size)
60
{
61
  void *r;
62
 
63
  if (p)
64
    {
65
      object *o = reinterpret_cast(p) - 1;
66
      size_t old_size = o->size;
67
 
68
      if (old_size >= size)
69
        {
70
          r = p;
71
          o->size = size;
72
        }
73
      else
74
        {
75
          r = malloc (size);
76
          memcpy (r, p, old_size);
77
          free (p);
78
        }
79
    }
80
  else
81
    r = malloc (size);
82
 
83
  return r;
84
}
85
 
86
void fn_throw() throw(int)
87
{
88
  throw 1;
89
}
90
 
91
void fn_rethrow() throw(int)
92
{
93
  try{fn_throw();}
94
  catch(int a){
95
    throw;}
96
}
97
 
98
void fn_catchthrow() throw(int)
99
{
100
  try{fn_throw();}
101
  catch(int a){
102
    throw a + 1;}
103
}
104
 
105
int main()
106
{
107
  /* On some systems (including FreeBSD and Solaris 2.10),
108
     __cxa_get_globals will try to call "malloc" when threads are in
109
     use.  Therefore, we throw one exception up front so that
110
     __cxa_get_globals is all set up.  Ideally, this would not be
111
     necessary, but it is a well-known idiom, and using this technique
112
     means that we can still validate the fact that exceptions can be
113
     thrown when malloc fails.  */
114
  try{fn_throw();}
115
  catch(int a){}
116
 
117
  fail = 1;
118
 
119
  try{fn_throw();}
120
  catch(int a){}
121
 
122
  try{fn_rethrow();}
123
  catch(int a){}
124
 
125
  try{fn_catchthrow();}
126
  catch(int a){}
127
 
128
  return 0;
129
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.