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 Mon, Dec 17, 2001 at 03:41:53PM -0600, Robert Lipe wrote:
> > Not sure what to do here, since allocating 24k isn't going
> > to be feasable on embedded targets, and this 24k is really
> > only needed when linking against shared libstdc++.
> 
> Is a shared libstdc++ really viable on any embedded target that can't
> find 24K?

No, but that's not the point.  The point is that we don't need
24k if we're not using a shared libstdc++, and that we don't 
know if we're not using a shared libstdc++.

If we try to reserve 24k "just in case", we'll make embedded
targets fail this test for no reason.

Actually, I think I have a solution: assume that any target
that defines STACK_SIZE to any value won't have hordes of
data space either, and won't support shared linking.

As an aside, the code here appears confused.  I'd check this
in, except that c++ is failing all tests again on Alpha.  :-(


r~


	* 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.

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/17 22:22: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,74 ----
  // 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.
  
! 
! extern "C" void abort();
! extern "C" void *memcpy(void *, const void *, __SIZE_TYPE__);
! 
! // 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 = 64;
! #else
! const int arena_size = 4096;
! #endif
! 
! static __SIZE_TYPE__ arena[arena_size];
  static unsigned pos;
  
+ // So we can force a failure when needed.
+ static int fail;
+ 
  extern "C" void *malloc (__SIZE_TYPE__ size)
  {
    __SIZE_TYPE__ *p = &arena[pos];
  
    if (fail)
      return 0;
! 
!   *p = size;
!   size = (size + sizeof (__SIZE_TYPE__) - 1) / sizeof (__SIZE_TYPE__);
!   pos += size + 1;
! 
!   // 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)
  {
!   void *r;
! 
!   if (p)
      {
!       __SIZE_TYPE__ *size_ptr= (__SIZE_TYPE__ *)p - 1;
!       __SIZE_TYPE__ old_size = *size_ptr;
! 
!       if (old_size >= size)
! 	{
! 	  r = p;
! 	  *size_ptr = 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]