#include <exception> // for std::bad_alloc
#include <new>
#include <dlmalloc.h>
#include <stdlib.h>

using namespace std;

void* operator new (size_t size) throw (bad_alloc)
{
  void* ptr = dlmalloc(size);
  if (ptr == 0)
    throw bad_alloc();
  return ptr;
}

void* operator new[] (size_t size) throw (bad_alloc)
{
  void* ptr = dlmalloc(size);
  if (ptr == 0)
    throw bad_alloc();
  return ptr;
}

void operator delete (void* ptr) throw()
{
  dlfree (ptr);
}

void operator delete[] (void* ptr) throw()
{
  dlfree (ptr);
}

void* operator new (size_t size, const nothrow_t& dummy) throw()
{
  return dlmalloc(size);
}

void* operator new[] (size_t size, const nothrow_t& dummy) throw()
{
  return dlmalloc(size);
}

void operator delete(void* ptr, const nothrow_t& dummy) throw()
{
  dlfree (ptr);
}

void operator delete[](void* ptr, const nothrow_t& dummy) throw()
{
  dlfree (ptr);
}
