Commit cf785988 by Richard Henderson

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
parent 5bc27702
2001-12-20 Richard Henderson <rth@redhat.com>
* 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 <jakub@redhat.com> 2001-12-20 Jakub Jelinek <jakub@redhat.com>
* gcc.c-torture/compile/20011219-2.c: New test. * gcc.c-torture/compile/20011219-2.c: New test.
......
...@@ -2,43 +2,78 @@ ...@@ -2,43 +2,78 @@
// Copyright (C) 2000 Free Software Foundation, Inc. // Copyright (C) 2000 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 6 June 2000 <nathan@codesourcery.com> // Contributed by Nathan Sidwell 6 June 2000 <nathan@codesourcery.com>
// 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 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<object *>(&arena[pos]);
if (fail) if (fail)
return 0; return 0;
arena[pos] = size; p->size = size;
size = (size + 4 * sizeof (__SIZE_TYPE__) - 1) size = (size + __alignof__(object) + 1) & - __alignof__(object);
/ sizeof (__SIZE_TYPE__) & ~3; // Yes, this is a hack pos += size + sizeof(object);
pos += size + 4;
return p + 4; // 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 free (void *)
{ {
} }
extern "C" void *realloc (void *p, __SIZE_TYPE__ size)
extern "C" void *realloc (void *p, size_t size)
{ {
void *r = malloc (size); void *r;
unsigned int oldSize;
if (p)
{
object *o = reinterpret_cast<object *>(p) - 1;
size_t old_size = o->size;
if (r && p) if (old_size >= size)
{ {
oldSize = ((__SIZE_TYPE__ *)p)[-4]; r = p;
if (oldSize < size) o->size = size;
size = oldSize;
while (size--)
((char *)r)[size] = ((char *)p)[size];
} }
else
{
r = malloc (size);
memcpy (r, p, old_size);
free (p); free (p);
}
}
else
r = malloc (size);
return r; return r;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment