Commit 27dfcb86 authored by BRAMAS Berenger's avatar BRAMAS Berenger

Must add the nothrow c++ new operators to deal with not c++11 allocation

parent 24974b1d
......@@ -23,11 +23,43 @@ FMemStats FMemStats::controler;
#ifdef SCALFMM_USE_MEM_STATS
// Regular scalar new
void* operator new(std::size_t n) {
void* const allocated = std::malloc(n + 8);
void* const allocated = std::malloc(n + sizeof(size_t));
if(allocated){
*(static_cast<size_t*>(allocated)) = n;
FMemStats::controler.allocate(n);
return static_cast<unsigned char*>(allocated) + 8;
return static_cast<unsigned char*>(allocated) + sizeof(size_t);
}
throw std::bad_alloc();
return allocated;
}
void* operator new[]( std::size_t n ) {
void* const allocated = std::malloc(n + sizeof(size_t));
if(allocated){
*(static_cast<size_t*>(allocated)) = n;
FMemStats::controler.allocate(n);
return static_cast<unsigned char*>(allocated) + sizeof(size_t);
}
throw std::bad_alloc();
return allocated;
}
void* operator new ( std::size_t n, const std::nothrow_t& tag){
void* const allocated = std::malloc(n + sizeof(size_t));
if(allocated){
*(static_cast<size_t*>(allocated)) = n;
FMemStats::controler.allocate(n);
return static_cast<unsigned char*>(allocated) + sizeof(size_t);
}
return allocated;
}
void* operator new[] ( std::size_t n, const std::nothrow_t& tag){
void* const allocated = std::malloc(n + sizeof(size_t));
if(allocated){
*(static_cast<size_t*>(allocated)) = n;
FMemStats::controler.allocate(n);
return static_cast<unsigned char*>(allocated) + sizeof(size_t);
}
return allocated;
}
......@@ -35,29 +67,29 @@ FMemStats FMemStats::controler;
// Regular scalar delete
void operator delete(void* p) noexcept{
if(p){
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
std::free(static_cast<unsigned char*>(p) - 8);
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - sizeof(size_t))) );
std::free(static_cast<unsigned char*>(p) - sizeof(size_t));
}
}
void operator delete[](void* p) noexcept{
if(p){
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
std::free(static_cast<unsigned char*>(p) - 8);
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - sizeof(size_t))) );
std::free(static_cast<unsigned char*>(p) - sizeof(size_t));
}
}
void operator delete ( void* p, const std::nothrow_t& /*tag*/) {
if(p){
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
std::free(static_cast<unsigned char*>(p) - 8);
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - sizeof(size_t))) );
std::free(static_cast<unsigned char*>(p) - sizeof(size_t));
}
}
void operator delete[]( void* p, const std::nothrow_t& /*tag*/) {
if(p){
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - 8)) );
std::free(static_cast<unsigned char*>(p) - 8);
FMemStats::controler.deallocate( *(reinterpret_cast<size_t*>(static_cast<unsigned char*>(p) - sizeof(size_t))) );
std::free(static_cast<unsigned char*>(p) - sizeof(size_t));
}
}
......
......@@ -32,6 +32,11 @@
#include <stdexcept>
#warning You are using mem stats
void* operator new(std::size_t n);
void* operator new[]( std::size_t n );
void* operator new ( std::size_t n, const std::nothrow_t& tag);
void* operator new[]( std::size_t n, const std::nothrow_t& tag);
void operator delete(void* p) noexcept;
void operator delete[](void* p) noexcept;
void operator delete ( void* ptr, const std::nothrow_t& tag);
......@@ -77,6 +82,9 @@ private:
#ifdef SCALFMM_USE_MEM_STATS
friend void* operator new(std::size_t n);
friend void* operator new[]( std::size_t n );
friend void* operator new ( std::size_t n, const std::nothrow_t& tag);
friend void* operator new[]( std::size_t n, const std::nothrow_t& tag);
friend void operator delete(void* p) noexcept;
friend void operator delete[](void* p) noexcept;
friend void operator delete ( void* ptr, const std::nothrow_t& tag);
......
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