This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Fix g++.eh/badalloc.C failure


On Tue, Dec 18, 2001 at 09:59:15AM +0000, Nathan Sidwell wrote:
> These '4's are an attempt at padding the allocation to some large alignment.

I see.  In which case, this alternate uses 'aligned' to grab
BIGGEST_ALIGNMENT.

It passes on alphaev6-linux.


r~



Index: g++.old-deja/g++.eh/badalloc1.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.eh/badalloc1.C,v
retrieving revision 1.5
diff -c -p -d -r1.5 badalloc1.C
*** badalloc1.C	2001/11/15 17:48:07	1.5
--- badalloc1.C	2001/12/21 00:17:26
***************
*** 2,44 ****
  // Copyright (C) 2000 Free Software Foundation, Inc.
  // Contributed by Nathan Sidwell 6 June 2000 <nathan@codesourcery.com>
  
! // Check we can throw a bad_alloc exception when malloc dies
  
! static __SIZE_TYPE__ arena[64]; // so things can initialize
  static int fail;
- static unsigned pos;
  
! extern "C" void *malloc (__SIZE_TYPE__ size)
  {
!   __SIZE_TYPE__ *p = &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;
  }
  extern "C" void free (void *)
  {
-   
  }
! extern "C" void *realloc (void *p, __SIZE_TYPE__ size)
  {
!   void *r = malloc (size);
!   unsigned int oldSize;
!   
!   if (r && p)
      {
!       oldSize = ((__SIZE_TYPE__ *)p)[-4];
!       if (oldSize < size)
!         size = oldSize;
!       while (size--)
!         ((char *)r)[size] = ((char *)p)[size];
      }
!   free (p);
    return r;
  }
  
--- 2,79 ----
  // Copyright (C) 2000 Free Software Foundation, Inc.
  // Contributed by Nathan Sidwell 6 June 2000 <nathan@codesourcery.com>
  
! // Check we can throw a bad_alloc exception when malloc dies.
  
! 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;
  
! extern "C" void *malloc (size_t size)
  {
!   object *p = reinterpret_cast<object *>(&arena[pos]);
  
    if (fail)
      return 0;
! 
!   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_t size)
  {
!   void *r;
! 
!   if (p)
      {
!       object *o = reinterpret_cast<object *>(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);
! 	}
      }
!   else
!     r = malloc (size);
! 
    return r;
  }
  


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]