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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [libmudflap/] [mf-hooks1.c] - Diff between revs 154 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 154 Rev 816
/* Mudflap: narrow-pointer bounds-checking by tree rewriting.
/* Mudflap: narrow-pointer bounds-checking by tree rewriting.
   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
   Contributed by Frank Ch. Eigler <fche@redhat.com>
   Contributed by Frank Ch. Eigler <fche@redhat.com>
   and Graydon Hoare <graydon@redhat.com>
   and Graydon Hoare <graydon@redhat.com>
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC is free software; you can redistribute it and/or modify it under
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
Software Foundation; either version 2, or (at your option) any later
version.
version.
 
 
In addition to the permissions in the GNU General Public License, the
In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file into combinations with other programs,
compiled version of this file into combinations with other programs,
and to distribute those combinations without any restriction coming
and to distribute those combinations without any restriction coming
from the use of this file.  (The General Public License restrictions
from the use of this file.  (The General Public License restrictions
do apply in other respects; for example, they cover modification of
do apply in other respects; for example, they cover modification of
the file, and distribution when not linked into a combine
the file, and distribution when not linked into a combine
executable.)
executable.)
 
 
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.  */
02110-1301, USA.  */
 
 
 
 
#include "config.h"
#include "config.h"
 
 
#ifndef HAVE_SOCKLEN_T
#ifndef HAVE_SOCKLEN_T
#define socklen_t int
#define socklen_t int
#endif
#endif
 
 
 
 
/* These attempt to coax various unix flavours to declare all our
/* These attempt to coax various unix flavours to declare all our
   needed tidbits in the system headers.  */
   needed tidbits in the system headers.  */
#if !defined(__FreeBSD__)  && !defined(__APPLE__)
#if !defined(__FreeBSD__)  && !defined(__APPLE__)
#define _POSIX_SOURCE
#define _POSIX_SOURCE
#endif /* Some BSDs break <sys/socket.h> if this is defined. */
#endif /* Some BSDs break <sys/socket.h> if this is defined. */
#define _GNU_SOURCE
#define _GNU_SOURCE
#define _XOPEN_SOURCE
#define _XOPEN_SOURCE
#define _BSD_TYPES
#define _BSD_TYPES
#define __EXTENSIONS__
#define __EXTENSIONS__
#define _ALL_SOURCE
#define _ALL_SOURCE
#define _LARGE_FILE_API
#define _LARGE_FILE_API
#define _XOPEN_SOURCE_EXTENDED 1
#define _XOPEN_SOURCE_EXTENDED 1
 
 
#include <string.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>
#include <assert.h>
#include <assert.h>
#include <errno.h>
#include <errno.h>
#include <limits.h>
#include <limits.h>
#include <time.h>
#include <time.h>
 
 
#include "mf-runtime.h"
#include "mf-runtime.h"
#include "mf-impl.h"
#include "mf-impl.h"
 
 
#ifdef _MUDFLAP
#ifdef _MUDFLAP
#error "Do not compile this file with -fmudflap!"
#error "Do not compile this file with -fmudflap!"
#endif
#endif
 
 
 
 
/* Memory allocation related hook functions.  Some of these are
/* Memory allocation related hook functions.  Some of these are
   intercepted via linker wrapping or symbol interposition.  Others
   intercepted via linker wrapping or symbol interposition.  Others
   use plain macros in mf-runtime.h.  */
   use plain macros in mf-runtime.h.  */
 
 
 
 
#if PIC
#if PIC
/* A special bootstrap variant. */
/* A special bootstrap variant. */
void *
void *
__mf_0fn_malloc (size_t c)
__mf_0fn_malloc (size_t c)
{
{
  enum foo { BS = 4096, NB=10 };
  enum foo { BS = 4096, NB=10 };
  static char bufs[NB][BS];
  static char bufs[NB][BS];
  static unsigned bufs_used[NB];
  static unsigned bufs_used[NB];
  unsigned i;
  unsigned i;
 
 
  for (i=0; i<NB; i++)
  for (i=0; i<NB; i++)
    {
    {
      if (! bufs_used[i] && c < BS)
      if (! bufs_used[i] && c < BS)
        {
        {
          bufs_used[i] = 1;
          bufs_used[i] = 1;
          return & bufs[i][0];
          return & bufs[i][0];
        }
        }
    }
    }
  return NULL;
  return NULL;
}
}
#endif
#endif
 
 
 
 
#undef malloc
#undef malloc
WRAPPER(void *, malloc, size_t c)
WRAPPER(void *, malloc, size_t c)
{
{
  size_t size_with_crumple_zones;
  size_t size_with_crumple_zones;
  DECLARE(void *, malloc, size_t c);
  DECLARE(void *, malloc, size_t c);
  void *result;
  void *result;
  BEGIN_PROTECT (malloc, c);
  BEGIN_PROTECT (malloc, c);
 
 
  size_with_crumple_zones =
  size_with_crumple_zones =
    CLAMPADD(c,CLAMPADD(__mf_opts.crumple_zone,
    CLAMPADD(c,CLAMPADD(__mf_opts.crumple_zone,
                        __mf_opts.crumple_zone));
                        __mf_opts.crumple_zone));
  BEGIN_MALLOC_PROTECT ();
  BEGIN_MALLOC_PROTECT ();
  result = (char *) CALL_REAL (malloc, size_with_crumple_zones);
  result = (char *) CALL_REAL (malloc, size_with_crumple_zones);
  END_MALLOC_PROTECT ();
  END_MALLOC_PROTECT ();
 
 
  if (LIKELY(result))
  if (LIKELY(result))
    {
    {
      result += __mf_opts.crumple_zone;
      result += __mf_opts.crumple_zone;
      __mf_register (result, c, __MF_TYPE_HEAP, "malloc region");
      __mf_register (result, c, __MF_TYPE_HEAP, "malloc region");
      /* XXX: register __MF_TYPE_NOACCESS for crumple zones.  */
      /* XXX: register __MF_TYPE_NOACCESS for crumple zones.  */
    }
    }
 
 
  return result;
  return result;
}
}
 
 
 
 
#ifdef PIC
#ifdef PIC
/* A special bootstrap variant. */
/* A special bootstrap variant. */
void *
void *
__mf_0fn_calloc (size_t c, size_t n)
__mf_0fn_calloc (size_t c, size_t n)
{
{
  return __mf_0fn_malloc (c * n);
  return __mf_0fn_malloc (c * n);
}
}
#endif
#endif
 
 
 
 
#undef calloc
#undef calloc
WRAPPER(void *, calloc, size_t c, size_t n)
WRAPPER(void *, calloc, size_t c, size_t n)
{
{
  size_t size_with_crumple_zones;
  size_t size_with_crumple_zones;
  DECLARE(void *, calloc, size_t, size_t);
  DECLARE(void *, calloc, size_t, size_t);
  DECLARE(void *, malloc, size_t);
  DECLARE(void *, malloc, size_t);
  DECLARE(void *, memset, void *, int, size_t);
  DECLARE(void *, memset, void *, int, size_t);
  char *result;
  char *result;
  BEGIN_PROTECT (calloc, c, n);
  BEGIN_PROTECT (calloc, c, n);
 
 
  size_with_crumple_zones =
  size_with_crumple_zones =
    CLAMPADD((c * n), /* XXX: CLAMPMUL */
    CLAMPADD((c * n), /* XXX: CLAMPMUL */
             CLAMPADD(__mf_opts.crumple_zone,
             CLAMPADD(__mf_opts.crumple_zone,
                      __mf_opts.crumple_zone));
                      __mf_opts.crumple_zone));
  BEGIN_MALLOC_PROTECT ();
  BEGIN_MALLOC_PROTECT ();
  result = (char *) CALL_REAL (malloc, size_with_crumple_zones);
  result = (char *) CALL_REAL (malloc, size_with_crumple_zones);
  END_MALLOC_PROTECT ();
  END_MALLOC_PROTECT ();
 
 
  if (LIKELY(result))
  if (LIKELY(result))
    memset (result, 0, size_with_crumple_zones);
    memset (result, 0, size_with_crumple_zones);
 
 
  if (LIKELY(result))
  if (LIKELY(result))
    {
    {
      result += __mf_opts.crumple_zone;
      result += __mf_opts.crumple_zone;
      __mf_register (result, c*n /* XXX: clamp */, __MF_TYPE_HEAP_I, "calloc region");
      __mf_register (result, c*n /* XXX: clamp */, __MF_TYPE_HEAP_I, "calloc region");
      /* XXX: register __MF_TYPE_NOACCESS for crumple zones.  */
      /* XXX: register __MF_TYPE_NOACCESS for crumple zones.  */
    }
    }
 
 
  return result;
  return result;
}
}
 
 
 
 
#if PIC
#if PIC
/* A special bootstrap variant. */
/* A special bootstrap variant. */
void *
void *
__mf_0fn_realloc (void *buf, size_t c)
__mf_0fn_realloc (void *buf, size_t c)
{
{
  return NULL;
  return NULL;
}
}
#endif
#endif
 
 
 
 
#undef realloc
#undef realloc
WRAPPER(void *, realloc, void *buf, size_t c)
WRAPPER(void *, realloc, void *buf, size_t c)
{
{
  DECLARE(void * , realloc, void *, size_t);
  DECLARE(void * , realloc, void *, size_t);
  size_t size_with_crumple_zones;
  size_t size_with_crumple_zones;
  char *base = buf;
  char *base = buf;
  unsigned saved_wipe_heap;
  unsigned saved_wipe_heap;
  char *result;
  char *result;
  BEGIN_PROTECT (realloc, buf, c);
  BEGIN_PROTECT (realloc, buf, c);
 
 
  if (LIKELY(buf))
  if (LIKELY(buf))
    base -= __mf_opts.crumple_zone;
    base -= __mf_opts.crumple_zone;
 
 
  size_with_crumple_zones =
  size_with_crumple_zones =
    CLAMPADD(c, CLAMPADD(__mf_opts.crumple_zone,
    CLAMPADD(c, CLAMPADD(__mf_opts.crumple_zone,
                         __mf_opts.crumple_zone));
                         __mf_opts.crumple_zone));
  BEGIN_MALLOC_PROTECT ();
  BEGIN_MALLOC_PROTECT ();
  result = (char *) CALL_REAL (realloc, base, size_with_crumple_zones);
  result = (char *) CALL_REAL (realloc, base, size_with_crumple_zones);
  END_MALLOC_PROTECT ();
  END_MALLOC_PROTECT ();
 
 
  /* Ensure heap wiping doesn't occur during this peculiar
  /* Ensure heap wiping doesn't occur during this peculiar
     unregister/reregister pair.  */
     unregister/reregister pair.  */
  LOCKTH ();
  LOCKTH ();
  __mf_set_state (reentrant);
  __mf_set_state (reentrant);
  saved_wipe_heap = __mf_opts.wipe_heap;
  saved_wipe_heap = __mf_opts.wipe_heap;
  __mf_opts.wipe_heap = 0;
  __mf_opts.wipe_heap = 0;
 
 
  if (LIKELY(buf))
  if (LIKELY(buf))
    __mfu_unregister (buf, 0, __MF_TYPE_HEAP_I);
    __mfu_unregister (buf, 0, __MF_TYPE_HEAP_I);
  /* NB: underlying region may have been __MF_TYPE_HEAP. */
  /* NB: underlying region may have been __MF_TYPE_HEAP. */
 
 
  if (LIKELY(result))
  if (LIKELY(result))
    {
    {
      result += __mf_opts.crumple_zone;
      result += __mf_opts.crumple_zone;
      __mfu_register (result, c, __MF_TYPE_HEAP_I, "realloc region");
      __mfu_register (result, c, __MF_TYPE_HEAP_I, "realloc region");
      /* XXX: register __MF_TYPE_NOACCESS for crumple zones.  */
      /* XXX: register __MF_TYPE_NOACCESS for crumple zones.  */
    }
    }
 
 
  /* Restore previous setting.  */
  /* Restore previous setting.  */
  __mf_opts.wipe_heap = saved_wipe_heap;
  __mf_opts.wipe_heap = saved_wipe_heap;
 
 
  __mf_set_state (active);
  __mf_set_state (active);
  UNLOCKTH ();
  UNLOCKTH ();
 
 
  return result;
  return result;
}
}
 
 
 
 
#if PIC
#if PIC
/* A special bootstrap variant. */
/* A special bootstrap variant. */
void
void
__mf_0fn_free (void *buf)
__mf_0fn_free (void *buf)
{
{
  return;
  return;
}
}
#endif
#endif
 
 
#undef free
#undef free
WRAPPER(void, free, void *buf)
WRAPPER(void, free, void *buf)
{
{
  /* Use a circular queue to delay some number (__mf_opts.free_queue_length) of free()s.  */
  /* Use a circular queue to delay some number (__mf_opts.free_queue_length) of free()s.  */
  static void *free_queue [__MF_FREEQ_MAX];
  static void *free_queue [__MF_FREEQ_MAX];
  static unsigned free_ptr = 0;
  static unsigned free_ptr = 0;
  static int freeq_initialized = 0;
  static int freeq_initialized = 0;
  DECLARE(void, free, void *);
  DECLARE(void, free, void *);
 
 
  BEGIN_PROTECT (free, buf);
  BEGIN_PROTECT (free, buf);
 
 
  if (UNLIKELY(buf == NULL))
  if (UNLIKELY(buf == NULL))
    return;
    return;
 
 
  LOCKTH ();
  LOCKTH ();
  if (UNLIKELY(!freeq_initialized))
  if (UNLIKELY(!freeq_initialized))
    {
    {
      memset (free_queue, 0,
      memset (free_queue, 0,
                     __MF_FREEQ_MAX * sizeof (void *));
                     __MF_FREEQ_MAX * sizeof (void *));
      freeq_initialized = 1;
      freeq_initialized = 1;
    }
    }
  UNLOCKTH ();
  UNLOCKTH ();
 
 
  __mf_unregister (buf, 0, __MF_TYPE_HEAP_I);
  __mf_unregister (buf, 0, __MF_TYPE_HEAP_I);
  /* NB: underlying region may have been __MF_TYPE_HEAP. */
  /* NB: underlying region may have been __MF_TYPE_HEAP. */
 
 
  if (UNLIKELY(__mf_opts.free_queue_length > 0))
  if (UNLIKELY(__mf_opts.free_queue_length > 0))
    {
    {
      char *freeme = NULL;
      char *freeme = NULL;
      LOCKTH ();
      LOCKTH ();
      if (free_queue [free_ptr] != NULL)
      if (free_queue [free_ptr] != NULL)
        {
        {
          freeme = free_queue [free_ptr];
          freeme = free_queue [free_ptr];
          freeme -= __mf_opts.crumple_zone;
          freeme -= __mf_opts.crumple_zone;
        }
        }
      free_queue [free_ptr] = buf;
      free_queue [free_ptr] = buf;
      free_ptr = (free_ptr == (__mf_opts.free_queue_length-1) ? 0 : free_ptr + 1);
      free_ptr = (free_ptr == (__mf_opts.free_queue_length-1) ? 0 : free_ptr + 1);
      UNLOCKTH ();
      UNLOCKTH ();
      if (freeme)
      if (freeme)
        {
        {
          if (__mf_opts.trace_mf_calls)
          if (__mf_opts.trace_mf_calls)
            {
            {
              VERBOSE_TRACE ("freeing deferred pointer %p (crumple %u)\n",
              VERBOSE_TRACE ("freeing deferred pointer %p (crumple %u)\n",
                             (void *) freeme,
                             (void *) freeme,
                             __mf_opts.crumple_zone);
                             __mf_opts.crumple_zone);
            }
            }
          BEGIN_MALLOC_PROTECT ();
          BEGIN_MALLOC_PROTECT ();
          CALL_REAL (free, freeme);
          CALL_REAL (free, freeme);
          END_MALLOC_PROTECT ();
          END_MALLOC_PROTECT ();
        }
        }
    }
    }
  else
  else
    {
    {
      /* back pointer up a bit to the beginning of crumple zone */
      /* back pointer up a bit to the beginning of crumple zone */
      char *base = (char *)buf;
      char *base = (char *)buf;
      base -= __mf_opts.crumple_zone;
      base -= __mf_opts.crumple_zone;
      if (__mf_opts.trace_mf_calls)
      if (__mf_opts.trace_mf_calls)
        {
        {
          VERBOSE_TRACE ("freeing pointer %p = %p - %u\n",
          VERBOSE_TRACE ("freeing pointer %p = %p - %u\n",
                         (void *) base,
                         (void *) base,
                         (void *) buf,
                         (void *) buf,
                         __mf_opts.crumple_zone);
                         __mf_opts.crumple_zone);
        }
        }
      BEGIN_MALLOC_PROTECT ();
      BEGIN_MALLOC_PROTECT ();
      CALL_REAL (free, base);
      CALL_REAL (free, base);
      END_MALLOC_PROTECT ();
      END_MALLOC_PROTECT ();
    }
    }
}
}
 
 
 
 
/* We can only wrap mmap if the target supports it.  Likewise for munmap.
/* We can only wrap mmap if the target supports it.  Likewise for munmap.
   We assume we have both if we have mmap.  */
   We assume we have both if we have mmap.  */
#ifdef HAVE_MMAP
#ifdef HAVE_MMAP
 
 
#if PIC
#if PIC
/* A special bootstrap variant. */
/* A special bootstrap variant. */
void *
void *
__mf_0fn_mmap (void *start, size_t l, int prot, int f, int fd, off_t off)
__mf_0fn_mmap (void *start, size_t l, int prot, int f, int fd, off_t off)
{
{
  return (void *) -1;
  return (void *) -1;
}
}
#endif
#endif
 
 
 
 
#undef mmap
#undef mmap
WRAPPER(void *, mmap,
WRAPPER(void *, mmap,
        void  *start,  size_t length, int prot,
        void  *start,  size_t length, int prot,
        int flags, int fd, off_t offset)
        int flags, int fd, off_t offset)
{
{
  DECLARE(void *, mmap, void *, size_t, int,
  DECLARE(void *, mmap, void *, size_t, int,
                            int, int, off_t);
                            int, int, off_t);
  void *result;
  void *result;
  BEGIN_PROTECT (mmap, start, length, prot, flags, fd, offset);
  BEGIN_PROTECT (mmap, start, length, prot, flags, fd, offset);
 
 
  result = CALL_REAL (mmap, start, length, prot,
  result = CALL_REAL (mmap, start, length, prot,
                        flags, fd, offset);
                        flags, fd, offset);
 
 
  /*
  /*
  VERBOSE_TRACE ("mmap (%08lx, %08lx, ...) => %08lx\n",
  VERBOSE_TRACE ("mmap (%08lx, %08lx, ...) => %08lx\n",
                 (uintptr_t) start, (uintptr_t) length,
                 (uintptr_t) start, (uintptr_t) length,
                 (uintptr_t) result);
                 (uintptr_t) result);
  */
  */
 
 
  if (result != (void *)-1)
  if (result != (void *)-1)
    {
    {
      /* Register each page as a heap object.  Why not register it all
      /* Register each page as a heap object.  Why not register it all
         as a single segment?  That's so that a later munmap() call
         as a single segment?  That's so that a later munmap() call
         can unmap individual pages.  XXX: would __MF_TYPE_GUESS make
         can unmap individual pages.  XXX: would __MF_TYPE_GUESS make
         this more automatic?  */
         this more automatic?  */
      size_t ps = getpagesize ();
      size_t ps = getpagesize ();
      uintptr_t base = (uintptr_t) result;
      uintptr_t base = (uintptr_t) result;
      uintptr_t offset;
      uintptr_t offset;
 
 
      for (offset=0; offset<length; offset+=ps)
      for (offset=0; offset<length; offset+=ps)
        {
        {
          /* XXX: We could map PROT_NONE to __MF_TYPE_NOACCESS. */
          /* XXX: We could map PROT_NONE to __MF_TYPE_NOACCESS. */
          /* XXX: Unaccessed HEAP pages are reported as leaks.  Is this
          /* XXX: Unaccessed HEAP pages are reported as leaks.  Is this
             appropriate for unaccessed mmap pages? */
             appropriate for unaccessed mmap pages? */
          __mf_register ((void *) CLAMPADD (base, offset), ps,
          __mf_register ((void *) CLAMPADD (base, offset), ps,
                         __MF_TYPE_HEAP_I, "mmap page");
                         __MF_TYPE_HEAP_I, "mmap page");
        }
        }
    }
    }
 
 
  return result;
  return result;
}
}
 
 
 
 
#if PIC
#if PIC
/* A special bootstrap variant. */
/* A special bootstrap variant. */
int
int
__mf_0fn_munmap (void *start, size_t length)
__mf_0fn_munmap (void *start, size_t length)
{
{
  return -1;
  return -1;
}
}
#endif
#endif
 
 
 
 
#undef munmap
#undef munmap
WRAPPER(int , munmap, void *start, size_t length)
WRAPPER(int , munmap, void *start, size_t length)
{
{
  DECLARE(int, munmap, void *, size_t);
  DECLARE(int, munmap, void *, size_t);
  int result;
  int result;
  BEGIN_PROTECT (munmap, start, length);
  BEGIN_PROTECT (munmap, start, length);
 
 
  result = CALL_REAL (munmap, start, length);
  result = CALL_REAL (munmap, start, length);
 
 
  /*
  /*
  VERBOSE_TRACE ("munmap (%08lx, %08lx, ...) => %08lx\n",
  VERBOSE_TRACE ("munmap (%08lx, %08lx, ...) => %08lx\n",
                 (uintptr_t) start, (uintptr_t) length,
                 (uintptr_t) start, (uintptr_t) length,
                 (uintptr_t) result);
                 (uintptr_t) result);
  */
  */
 
 
  if (result == 0)
  if (result == 0)
    {
    {
      /* Unregister each page as a heap object.  */
      /* Unregister each page as a heap object.  */
      size_t ps = getpagesize ();
      size_t ps = getpagesize ();
      uintptr_t base = (uintptr_t) start & (~ (ps - 1)); /* page align */
      uintptr_t base = (uintptr_t) start & (~ (ps - 1)); /* page align */
      uintptr_t offset;
      uintptr_t offset;
 
 
      for (offset=0; offset<length; offset+=ps)
      for (offset=0; offset<length; offset+=ps)
        __mf_unregister ((void *) CLAMPADD (base, offset), ps, __MF_TYPE_HEAP_I);
        __mf_unregister ((void *) CLAMPADD (base, offset), ps, __MF_TYPE_HEAP_I);
    }
    }
  return result;
  return result;
}
}
#endif /* HAVE_MMAP */
#endif /* HAVE_MMAP */
 
 
 
 
/* This wrapper is a little different, as it's called indirectly from
/* This wrapper is a little different, as it's called indirectly from
   __mf_fini also to clean up pending allocations.  */
   __mf_fini also to clean up pending allocations.  */
void *
void *
__mf_wrap_alloca_indirect (size_t c)
__mf_wrap_alloca_indirect (size_t c)
{
{
  DECLARE (void *, malloc, size_t);
  DECLARE (void *, malloc, size_t);
  DECLARE (void, free, void *);
  DECLARE (void, free, void *);
 
 
  /* This struct, a linked list, tracks alloca'd objects.  The newest
  /* This struct, a linked list, tracks alloca'd objects.  The newest
     object is at the head of the list.  If we detect that we've
     object is at the head of the list.  If we detect that we've
     popped a few levels of stack, then the listed objects are freed
     popped a few levels of stack, then the listed objects are freed
     as needed.  NB: The tracking struct is allocated with
     as needed.  NB: The tracking struct is allocated with
     real_malloc; the user data with wrap_malloc.
     real_malloc; the user data with wrap_malloc.
  */
  */
  struct alloca_tracking { void *ptr; void *stack; struct alloca_tracking* next; };
  struct alloca_tracking { void *ptr; void *stack; struct alloca_tracking* next; };
  static struct alloca_tracking *alloca_history = NULL;
  static struct alloca_tracking *alloca_history = NULL;
 
 
  void *stack = __builtin_frame_address (0);
  void *stack = __builtin_frame_address (0);
  void *result;
  void *result;
  struct alloca_tracking *track;
  struct alloca_tracking *track;
 
 
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  VERBOSE_TRACE ("alloca stack level %p\n", (void *) stack);
  VERBOSE_TRACE ("alloca stack level %p\n", (void *) stack);
 
 
  /* XXX: thread locking! */
  /* XXX: thread locking! */
 
 
  /* Free any previously alloca'd blocks that belong to deeper-nested functions,
  /* Free any previously alloca'd blocks that belong to deeper-nested functions,
     which must therefore have exited by now.  */
     which must therefore have exited by now.  */
 
 
#define DEEPER_THAN < /* XXX: for x86; steal find_stack_direction() from libiberty/alloca.c */
#define DEEPER_THAN < /* XXX: for x86; steal find_stack_direction() from libiberty/alloca.c */
 
 
  while (alloca_history &&
  while (alloca_history &&
         ((uintptr_t) alloca_history->stack DEEPER_THAN (uintptr_t) stack))
         ((uintptr_t) alloca_history->stack DEEPER_THAN (uintptr_t) stack))
    {
    {
      struct alloca_tracking *next = alloca_history->next;
      struct alloca_tracking *next = alloca_history->next;
      __mf_unregister (alloca_history->ptr, 0, __MF_TYPE_HEAP);
      __mf_unregister (alloca_history->ptr, 0, __MF_TYPE_HEAP);
      BEGIN_MALLOC_PROTECT ();
      BEGIN_MALLOC_PROTECT ();
      CALL_REAL (free, alloca_history->ptr);
      CALL_REAL (free, alloca_history->ptr);
      CALL_REAL (free, alloca_history);
      CALL_REAL (free, alloca_history);
      END_MALLOC_PROTECT ();
      END_MALLOC_PROTECT ();
      alloca_history = next;
      alloca_history = next;
    }
    }
 
 
  /* Allocate new block.  */
  /* Allocate new block.  */
  result = NULL;
  result = NULL;
  if (LIKELY (c > 0)) /* alloca(0) causes no allocation.  */
  if (LIKELY (c > 0)) /* alloca(0) causes no allocation.  */
    {
    {
      BEGIN_MALLOC_PROTECT ();
      BEGIN_MALLOC_PROTECT ();
      track = (struct alloca_tracking *) CALL_REAL (malloc,
      track = (struct alloca_tracking *) CALL_REAL (malloc,
                                                    sizeof (struct alloca_tracking));
                                                    sizeof (struct alloca_tracking));
      END_MALLOC_PROTECT ();
      END_MALLOC_PROTECT ();
      if (LIKELY (track != NULL))
      if (LIKELY (track != NULL))
        {
        {
          BEGIN_MALLOC_PROTECT ();
          BEGIN_MALLOC_PROTECT ();
          result = CALL_REAL (malloc, c);
          result = CALL_REAL (malloc, c);
          END_MALLOC_PROTECT ();
          END_MALLOC_PROTECT ();
          if (UNLIKELY (result == NULL))
          if (UNLIKELY (result == NULL))
            {
            {
              BEGIN_MALLOC_PROTECT ();
              BEGIN_MALLOC_PROTECT ();
              CALL_REAL (free, track);
              CALL_REAL (free, track);
              END_MALLOC_PROTECT ();
              END_MALLOC_PROTECT ();
              /* Too bad.  XXX: What about errno?  */
              /* Too bad.  XXX: What about errno?  */
            }
            }
          else
          else
            {
            {
              __mf_register (result, c, __MF_TYPE_HEAP, "alloca region");
              __mf_register (result, c, __MF_TYPE_HEAP, "alloca region");
              track->ptr = result;
              track->ptr = result;
              track->stack = stack;
              track->stack = stack;
              track->next = alloca_history;
              track->next = alloca_history;
              alloca_history = track;
              alloca_history = track;
            }
            }
        }
        }
    }
    }
 
 
  return result;
  return result;
}
}
 
 
 
 
#undef alloca
#undef alloca
WRAPPER(void *, alloca, size_t c)
WRAPPER(void *, alloca, size_t c)
{
{
  return __mf_wrap_alloca_indirect (c);
  return __mf_wrap_alloca_indirect (c);
}
}
 
 
 
 

powered by: WebSVN 2.1.0

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