#include <stdlib.h>                   // for malloc and free

#include "SFBAssert.h"   /* for BUG_CODE */

void *operator new(size_t size) throw() { return malloc(size); }

void operator delete(void *p) throw() { free(p); }

int raise(int) { return 0; }

extern "C" int __aeabi_atexit(void *,
                              void (*)(void *),
                              void *)
{
  return 0;
}


extern "C" void __cxa_pure_virtual()
{
  BUG_CODE("pure virtual called",E_API_PURE_VIRTUAL);
}

extern "C"
int __cxa_guard_acquire() 
{
  BUG_CODE("cxa_guard_acquire called",E_API_CXA_GUARD);
}
 
extern "C"
void __cxa_guard_release()
{
  BUG_CODE("cxa_guard_release called",E_API_CXA_GUARD);
}

extern "C"
void __cxa_guard_abort()
{
  BUG_CODE("cxa_guard_abort called",E_API_CXA_GUARD);
}

// Mon Apr 5 05:17:59 2010 Ackley: These were being called because -lc
// was being searched too early, apparently.  Moving the -lc later in
// the command line, to after the -T for the load script, causes the
// real memcpy (etc) implementations to be found.  
//
// But, how to fix it?  All the sketch directories out there have the
// wrong-ordered command lines baked into them.  
//
// - Can we just make these weak symbols?  Apparently not, because
//   they still 'win' over the library definitions
//
// - Can we just call the built-ins directly, from here?  Apparently
//   not, because that just turns around and calls us again, leading
//   to an infinite recursion.
//
// - Can we put a copy of the actual compiler source code in here?
//   Well, not easily, because the real stuff is in assembler, and the
//   generic libiberty .c stuff isn't any better than the stupid
//   versions here..
//
// - Can we hack the loader script to do -lc at an appropriate time?
//   All the sketch makefiles still point back to the in-tree
//   ldscript..  YES, it appears we can.  Nuking this stuff.

#if 0
extern "C" void * memcpy (void * dstpp, const void * srcpp, size_t len)
{
  // Use incredibly slow and stupid byte copy until we understand
  // why this is being called!
  char * dst = (char *) dstpp;
  const char * src = (char *) srcpp;
  while (len--) 
    *dst++ = *src++;
  return dstpp;
}

extern "C" void * memset (void * dstpp, int c, size_t len)
{
  char * dst = (char *) dstpp;
  while (len--) 
    *dst++ = c;
  return dstpp;
}
#endif
