3 #ifndef DUNE_DEBUG_ALLOCATOR_HH
4 #define DUNE_DEBUG_ALLOCATOR_HH
15 #if HAVE_SYS_MMAN_H and HAVE_MPROTECT
26 #ifndef DOXYGEN // hide implementation details from doxygen
30 extern const std::ptrdiff_t page_size;
32 struct AllocationManager
34 typedef std::size_t size_type;
35 typedef std::ptrdiff_t difference_type;
36 typedef void* pointer;
39 static void allocation_error(
const char* msg);
41 struct AllocationInfo;
42 friend struct AllocationInfo;
44 #define ALLOCATION_ASSERT(A) { if (!(A)) \
45 { allocation_error("Assertion " # A " failed");\
51 AllocationInfo(
const std::type_info &
t) : type(&t) {}
52 const std::type_info * type;
62 typedef MallocAllocator<AllocationInfo> Alloc;
63 typedef std::vector<AllocationInfo, Alloc> AllocationList;
64 AllocationList allocation_list;
67 void memprotect(
void* from, difference_type len,
int prot)
69 #if HAVE_SYS_MMAN_H and HAVE_MPROTECT
70 int result = mprotect(from, len, prot);
74 std::cerr <<
"ERROR: (" << result <<
": " << strerror(result) <<
")" << std::endl;
75 std::cerr <<
" Failed to ";
77 std::cerr <<
"protect ";
79 std::cerr <<
"unprotect ";
80 std::cerr <<
"memory range: "
82 <<
static_cast<void*
>(
83 static_cast<char*
>(from) + len)
91 std::cerr <<
"WARNING: memory protection not available" << std::endl;
99 AllocationList::iterator it;
101 for (it=allocation_list.begin(); it!=allocation_list.end(); it++)
105 std::cerr <<
"ERROR: found memory chunk still in use: " <<
106 it->capacity <<
" bytes at " << it->ptr << std::endl;
112 allocation_error(
"lost allocations");
116 T* allocate(size_type n)
throw(std::bad_alloc)
119 AllocationInfo ai(
typeid(T));
121 ai.capacity = n *
sizeof(T);
122 ai.pages = (ai.capacity) / page_size + 2;
124 size_type overlap = ai.capacity % page_size;
125 int result = posix_memalign(&(ai.page_ptr), page_size, ai.pages * page_size);
128 throw std::bad_alloc();
130 ai.ptr =
static_cast<char*
>(ai.page_ptr) + page_size - overlap;
132 memprotect(static_cast<char*>(ai.page_ptr) + (ai.pages-1) * page_size,
136 allocation_list.push_back(ai);
138 return static_cast<T*
>(ai.ptr);
142 void deallocate(T* ptr, size_type n = 0) throw()
147 (
char*)(ptr) - ((std::uintptr_t)(ptr) % page_size));
149 AllocationList::iterator it;
151 for (it=allocation_list.begin(); it!=allocation_list.end(); it++, i++)
153 if (it->page_ptr == page_ptr)
158 ALLOCATION_ASSERT(n == it->size);
159 ALLOCATION_ASSERT(ptr == it->ptr);
160 ALLOCATION_ASSERT(
true == it->not_free);
161 ALLOCATION_ASSERT(
typeid(T) == *(it->type));
163 it->not_free =
false;
164 #if DEBUG_ALLOCATOR_KEEP
166 memprotect(it->page_ptr,
167 (it->pages) * page_size,
171 memprotect(it->page_ptr,
172 (it->pages) * page_size,
174 std::free(it->page_ptr);
176 allocation_list.erase(it);
181 allocation_error(
"memory block not found");
184 #undef ALLOCATION_ASSERT
186 extern AllocationManager alloc_man;
201 template <
class U>
struct rebind {
251 const_pointer
address(const_reference x)
const
261 return DebugMemory::alloc_man.allocate<T>(n);
267 DebugMemory::alloc_man.deallocate<T>(p,n);
279 ::new((
void*)p)T(val);
281 #if ( HAVE_VARIADIC_TEMPLATES && HAVE_RVALUE_REFERENCES ) || DOXYGEN
282 template<
typename ... _Args>
287 ::new((
void *)p)T(std::forward<_Args>(__args) ...);
298 #ifdef DEBUG_NEW_DELETE
299 void *
operator new(
size_t size)
throw(std::bad_alloc)
302 void *p = Dune::DebugMemory::alloc_man.allocate<
char>(size);
303 #if DEBUG_NEW_DELETE > 2
304 std::cout <<
"NEW " << size
311 void operator delete(
void * p)
throw()
313 #if DEBUG_NEW_DELETE > 2
314 std::cout <<
"FREE " << p << std::endl;
316 Dune::DebugMemory::alloc_man.deallocate<
char>(
static_cast<char*
>(p));
319 #endif // DEBUG_NEW_DELETE
321 #endif // DUNE_DEBUG_ALLOCATOR_HH
Definition: debugallocator.hh:18
Definition: debugallocator.hh:235
T value_type
Definition: debugallocator.hh:234
T t
Definition: alignment.hh:38
Dune namespace.
Definition: alignment.hh:13
void destroy(pointer p)
destroy an object of type T (i.e. call the destructor)
Definition: debugallocator.hh:291
T * pointer
Definition: debugallocator.hh:230
Allocators implementation which performs different kind of memory checks.
Definition: debugallocator.hh:191
const void * const_pointer
Definition: debugallocator.hh:198
T & reference
Definition: debugallocator.hh:232
pointer allocate(size_type n, DebugAllocator< void >::const_pointer hint=0)
allocate n objects of type T
Definition: debugallocator.hh:257
void construct(pointer p, const T &val)
copy-construct an object of type T (i.e. make a placement new on p)
Definition: debugallocator.hh:277
DebugAllocator()
create a new DebugAllocator
Definition: debugallocator.hh:240
const T * const_pointer
Definition: debugallocator.hh:231
pointer address(reference x) const
Definition: debugallocator.hh:247
Allocators that use malloc/free.
const T & const_reference
Definition: debugallocator.hh:233
DebugAllocator< U > other
Definition: debugallocator.hh:236
DebugAllocator(const DebugAllocator< U > &)
copy construct from an other DebugAllocator, possibly for a different result type ...
Definition: debugallocator.hh:243
void deallocate(pointer p, size_type n)
deallocate n objects of type T at address p
Definition: debugallocator.hh:265
size_type max_size() const
max size for allocate
Definition: debugallocator.hh:271
std::size_t size_type
Definition: debugallocator.hh:228
std::ptrdiff_t difference_type
Definition: debugallocator.hh:229
Definition: debugallocator.hh:18
Definition: debugallocator.hh:18
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentional unused function parameters with.
Definition: unused.hh:18
void value_type
Definition: debugallocator.hh:200
const_pointer address(const_reference x) const
Definition: debugallocator.hh:251
DummyProtFlags
Definition: debugallocator.hh:18
DebugAllocator< U > other
Definition: debugallocator.hh:202
void * pointer
Definition: debugallocator.hh:197
void construct(pointer p, _Args &&...__args)
Definition: debugallocator.hh:285
~DebugAllocator()
cleanup this allocator
Definition: debugallocator.hh:245