Skip to content
Snippets Groups Projects
Commit 686b65e2 authored by Christian Engwer's avatar Christian Engwer
Browse files

[DebugAllocator]

actually use AllocationManager in new/delete
... doesn't work yet, as the bookkeeping invokes new/delete again

[[Imported from SVN: r7008]]
parent bf329242
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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
......
......@@ -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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment