diff --git a/dune/common/debug_allocator.cc b/dune/common/debug_allocator.cc index 41f3d37cd62121bc99bc4ee38c8084e1c1ff9694..fbe0d2e6de6ae0fb5b92e7df0d0cccb0829dfc6d 100644 --- a/dune/common/debug_allocator.cc +++ b/dune/common/debug_allocator.cc @@ -10,12 +10,22 @@ #include <unistd.h> #include <cstdlib> -const Dune::DebugMemory::AllocationManager::difference_type Dune::DebugMemory::AllocationManager::page_size = getpagesize(); - -void Dune::DebugMemory::AllocationManager::allocation_error(const char* msg) +namespace Dune { - std::cerr << "Abort - Memory Corruption: " << msg << std::endl; - std::abort(); -} + namespace DebugMemory + { + // system constant for page size + const AllocationManager::difference_type AllocationManager::page_size = getpagesize(); + + // implement member functions + void AllocationManager::allocation_error(const char* msg) + { + std::cerr << "Abort - Memory Corruption: " << msg << std::endl; + std::abort(); + } + + // global instance of AllocationManager + AllocationManager alloc_man; -Dune::DebugMemory::AllocationManager Dune::DebugMemory::alloc_man; + } // end namespace DebugMemory +} // end namespace Dune diff --git a/dune/common/debug_allocator.hh b/dune/common/debug_allocator.hh index 67a6ba2d0b282c419445fc24d55367eadae95fdb..58c5fad33feb8786ab514ab153049370c046f589 100644 --- a/dune/common/debug_allocator.hh +++ b/dune/common/debug_allocator.hh @@ -259,17 +259,12 @@ namespace Dune void * operator new(size_t size) throw(std::bad_alloc) { // try to allocate size bytes - void *p = malloc(size); + void *p = Dune::DebugMemory::alloc_man.allocate<char>(size); #if DEBUG_NEW_DELETE > 2 std::cout << "NEW " << size << " -> " << p << std::endl; #endif - if (0 == p) - { - // report no memory - throw std::bad_alloc(); - } return p; } @@ -278,12 +273,7 @@ void operator delete(void * p) throw() #if DEBUG_NEW_DELETE > 2 std::cout << "FREE " << p << std::endl; #endif - // if (0 == p) - // { - // // report no memory - // throw std::bad_alloc; - // } - free(p); + Dune::DebugMemory::alloc_man.deallocate<char>(static_cast<char*>(p)); } #endif // DEBUG_NEW_DELETE diff --git a/dune/common/test/test-debug-alloc.cc b/dune/common/test/test-debug-alloc.cc index 59f802acbf5bfefdd84877afb82e11dff8d6978b..e69abb3c7bb2224927332b71205b50eaff0b8edd 100644 --- a/dune/common/test/test-debug-alloc.cc +++ b/dune/common/test/test-debug-alloc.cc @@ -18,6 +18,7 @@ class A public: A() { std::cout << "INIT A\n"; } int x; + void foo() {}; }; void basic_tests () @@ -68,12 +69,14 @@ void new_delete_tests() std::cout << "alloc A[2]\n"; A * z = new A[2]; + z->foo(); delete[] z; z = 0; std::cout << "alloc (buf) A[3]\n"; char * buf = (char*)malloc(128); A * z2 = new (buf) A[3]; + z2->foo(); free(buf); z2 = 0; }