2010-07-22 10:25:14 +00:00
|
|
|
|
// This code is in the public domain -- Ignacio Casta<74>o <castano@gmail.com>
|
|
|
|
|
|
|
|
|
|
#include "Memory.h"
|
|
|
|
|
#include "Debug.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#define USE_EFENCE 0
|
|
|
|
|
|
|
|
|
|
#if USE_EFENCE
|
|
|
|
|
extern "C" void *EF_malloc(size_t size);
|
|
|
|
|
extern "C" void *EF_realloc(void * oldBuffer, size_t newSize);
|
|
|
|
|
extern "C" void EF_free(void * address);
|
|
|
|
|
#endif
|
2010-07-22 10:25:14 +00:00
|
|
|
|
|
|
|
|
|
using namespace nv;
|
|
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
|
#if NV_OVERRIDE_ALLOC
|
|
|
|
|
|
|
|
|
|
void * malloc(size_t size)
|
2010-07-22 10:25:14 +00:00
|
|
|
|
{
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#if USE_EFENCE
|
|
|
|
|
return EF_malloc(size);
|
|
|
|
|
#else
|
2010-07-22 10:25:14 +00:00
|
|
|
|
return ::malloc(size);
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#endif
|
2010-07-22 10:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
|
void * debug_malloc(size_t size, const char * file, int line)
|
2010-07-22 10:25:14 +00:00
|
|
|
|
{
|
|
|
|
|
NV_UNUSED(file);
|
|
|
|
|
NV_UNUSED(line);
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#if USE_EFENCE
|
|
|
|
|
return EF_malloc(size);
|
|
|
|
|
#else
|
2010-07-22 10:25:14 +00:00
|
|
|
|
return ::malloc(size);
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#endif
|
2010-07-22 10:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
|
void free(void * ptr)
|
2010-07-22 10:25:14 +00:00
|
|
|
|
{
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#if USE_EFENCE
|
|
|
|
|
return EF_free(const_cast<void *>(ptr));
|
|
|
|
|
#else
|
2010-07-22 10:25:14 +00:00
|
|
|
|
::free(const_cast<void *>(ptr));
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#endif
|
2010-07-22 10:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
|
void * realloc(void * ptr, size_t size)
|
2010-07-22 10:25:14 +00:00
|
|
|
|
{
|
|
|
|
|
nvDebugCheck(ptr != NULL || size != 0); // undefined realloc behavior.
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#if USE_EFENCE
|
|
|
|
|
return EF_realloc(ptr, size);
|
|
|
|
|
#else
|
2010-07-22 10:25:14 +00:00
|
|
|
|
return ::realloc(ptr, size);
|
2010-08-31 01:39:08 +00:00
|
|
|
|
#endif
|
2010-07-22 10:25:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-06 02:56:35 +00:00
|
|
|
|
/* No need to override this unless we want line info.
|
|
|
|
|
void * operator new (size_t size) throw()
|
|
|
|
|
{
|
|
|
|
|
return malloc(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void operator delete (void *p) throw()
|
|
|
|
|
{
|
|
|
|
|
free(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void * operator new [] (size_t size) throw()
|
|
|
|
|
{
|
|
|
|
|
return malloc(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void operator delete [] (void * p) throw()
|
|
|
|
|
{
|
|
|
|
|
free(p);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#if 0 // Code from Apple:
|
|
|
|
|
void* operator new(std::size_t sz) throw (std::bad_alloc)
|
|
|
|
|
{
|
|
|
|
|
void *result = std::malloc (sz == 0 ? 1 : sz);
|
|
|
|
|
if (result == NULL)
|
|
|
|
|
throw std::bad_alloc();
|
|
|
|
|
gNewCounter++;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
void operator delete(void* p) throw()
|
|
|
|
|
{
|
|
|
|
|
if (p == NULL)
|
|
|
|
|
return;
|
|
|
|
|
std::free (p);
|
|
|
|
|
gDeleteCounter++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* These are the 'nothrow' versions of the above operators.
|
|
|
|
|
The system version will try to call a std::new_handler if they
|
|
|
|
|
fail, but your overriding versions are not required to do this. */
|
|
|
|
|
void* operator new(std::size_t sz, const std::nothrow_t&) throw()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
void * result = ::operator new (sz); // calls our overridden operator new
|
|
|
|
|
return result;
|
|
|
|
|
} catch (std::bad_alloc &) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void operator delete(void* p, const std::nothrow_t&) throw()
|
|
|
|
|
{
|
|
|
|
|
::operator delete (p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // NV_OVERRIDE_ALLOC
|