From cf785988e051f4ed5732e2e263c161c702f5cbf9 Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Thu, 20 Dec 2001 16:18:26 -0800 Subject: [PATCH] badalloc1.C (arena_size): New. * g++.old-deja/g++.eh/badalloc1.C (arena_size): New. (arena): Use it. (malloc): Correct allocation logic. Abort if we fill up the arena before initialization complete. (realloc): Correct allocation logic. From-SVN: r48224 --- gcc/testsuite/ChangeLog | 30 ++++--- gcc/testsuite/g++.old-deja/g++.eh/badalloc1.C | 81 +++++++++++++------ 2 files changed, 77 insertions(+), 34 deletions(-) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index aaee1d55371..56f40ec8286 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2001-12-20 Richard Henderson + + * g++.old-deja/g++.eh/badalloc1.C (arena_size): New. + (arena): Use it. + (malloc): Correct allocation logic. Abort if we fill up the + arena before initialization complete. + (realloc): Correct allocation logic. + 2001-12-20 Jakub Jelinek * gcc.c-torture/compile/20011219-2.c: New test. @@ -52,7 +60,7 @@ Mon Dec 17 16:54:56 2001 Nicola Pero 2001-12-13 John David Anglin * g++.old-deja/g++.jason/template31.C: Remove templates for classes - std::__malloc_alloc_template<0> and + std::__malloc_alloc_template<0> and std::__default_alloc_template. Thu Dec 13 10:35:33 2001 Nicola Pero @@ -117,13 +125,13 @@ Thu Dec 13 10:35:33 2001 Nicola Pero 2001-10-08 Aldy Hernandez - * gcc.c-torture/execute/builtin-types-compatible-p.c: New. + * gcc.c-torture/execute/builtin-types-compatible-p.c: New. - * gcc.dg/builtin-choose-expr.c: New. + * gcc.dg/builtin-choose-expr.c: New. 2001-12-07 Aldy Hernandez - * gcc.dg/altivec-2.c: New. + * gcc.dg/altivec-2.c: New. 2001-12-07 Richard Henderson @@ -281,7 +289,7 @@ Fri Nov 23 15:55:44 2001 Jeffrey A Law (law@cygnus.com) 2001-11-17 Aldy Hernandez - * gcc.dg/altivec-1.c: Fix typo. + * gcc.dg/altivec-1.c: Fix typo. 2001-11-20 Joseph S. Myers @@ -306,7 +314,7 @@ Fri Nov 23 15:55:44 2001 Jeffrey A Law (law@cygnus.com) 2001-11-19 Aldy Hernandez - * gcc.dg/altivec-1.c: New. + * gcc.dg/altivec-1.c: New. 2001-11-19 Neil Booth @@ -434,7 +442,7 @@ Fri Nov 23 15:55:44 2001 Jeffrey A Law (law@cygnus.com) 2001-11-01 Stephane Carrez * gcc.c-torture/compile/20010327-1.c: Use __SIZE_TYPE__ instead - of unsigned long. + of unsigned long. 2001-10-30 Jakub Jelinek @@ -663,7 +671,7 @@ Mon Oct 29 21:19:53 2001 Nicola Pero 2001-09-15 Aldy Hernandez - * gcc.c-torture/execute/980223.c: Change type of addr from long + * gcc.c-torture/execute/980223.c: Change type of addr from long to char *. 2001-09-15 Hans-Peter Nilsson @@ -769,7 +777,7 @@ Mon Oct 29 21:19:53 2001 Nicola Pero 2001-08-25 Aldy Hernandez - * gcc.dg/asm-names.c (ymain): New. + * gcc.dg/asm-names.c (ymain): New. 2001-08-22 Geoffrey Keating @@ -788,7 +796,7 @@ Mon Oct 29 21:19:53 2001 Nicola Pero 2001-08-21 Aldy Hernandez - * gcc.c-torture/execute/divmod-1.c: Convert to ANSI. + * gcc.c-torture/execute/divmod-1.c: Convert to ANSI. 2001-08-20 Janis Johnson @@ -896,7 +904,7 @@ Mon Oct 29 21:19:53 2001 Nicola Pero * g77.dg/ffixed-line-length-7.f * g77.dg/ffixed-line-length-72.f * g77.dg/ffixed-line-length-none.f - * g77.dg/ffree-form-1.f + * g77.dg/ffree-form-1.f * g77.dg/fno-backslash.f * g77.dg/fno-f90-1.f * g77.dg/fno-fixed-form-1.f diff --git a/gcc/testsuite/g++.old-deja/g++.eh/badalloc1.C b/gcc/testsuite/g++.old-deja/g++.eh/badalloc1.C index 6b4a9283142..806856f24b1 100644 --- a/gcc/testsuite/g++.old-deja/g++.eh/badalloc1.C +++ b/gcc/testsuite/g++.old-deja/g++.eh/badalloc1.C @@ -2,43 +2,78 @@ // Copyright (C) 2000 Free Software Foundation, Inc. // Contributed by Nathan Sidwell 6 June 2000 -// Check we can throw a bad_alloc exception when malloc dies +// Check we can throw a bad_alloc exception when malloc dies. -static __SIZE_TYPE__ arena[64]; // so things can initialize +typedef __SIZE_TYPE__ size_t; +extern "C" void abort(); +extern "C" void *memcpy(void *, const void *, size_t); + +// Assume that STACK_SIZE defined implies a system that does not have a +// large data space either, and additionally that we're not linking against +// a shared libstdc++ (which requires quite a bit more initialization space). +#ifdef STACK_SIZE +const int arena_size = 256; +#else +const int arena_size = 32768; +#endif + +struct object +{ + size_t size __attribute__((aligned)); +}; + +static char arena[arena_size] __attribute__((aligned)); +static size_t pos; + +// So we can force a failure when needed. static int fail; -static unsigned pos; -extern "C" void *malloc (__SIZE_TYPE__ size) +extern "C" void *malloc (size_t size) { - __SIZE_TYPE__ *p = &arena[pos]; + object *p = reinterpret_cast(&arena[pos]); if (fail) return 0; - - arena[pos] = size; - size = (size + 4 * sizeof (__SIZE_TYPE__) - 1) - / sizeof (__SIZE_TYPE__) & ~3; // Yes, this is a hack - pos += size + 4; - return p + 4; + + p->size = size; + size = (size + __alignof__(object) + 1) & - __alignof__(object); + pos += size + sizeof(object); + + // Verify that we didn't run out of memory before getting initialized. + if (pos > arena_size) + abort (); + + return p + 1; } + extern "C" void free (void *) { - } -extern "C" void *realloc (void *p, __SIZE_TYPE__ size) + +extern "C" void *realloc (void *p, size_t size) { - void *r = malloc (size); - unsigned int oldSize; - - if (r && p) + void *r; + + if (p) { - oldSize = ((__SIZE_TYPE__ *)p)[-4]; - if (oldSize < size) - size = oldSize; - while (size--) - ((char *)r)[size] = ((char *)p)[size]; + object *o = reinterpret_cast(p) - 1; + size_t old_size = o->size; + + if (old_size >= size) + { + r = p; + o->size = size; + } + else + { + r = malloc (size); + memcpy (r, p, old_size); + free (p); + } } - free (p); + else + r = malloc (size); + return r; } -- 2.30.2