3#ifndef DUNE_DEBUG_ALLOCATOR_HH
4#define DUNE_DEBUG_ALLOCATOR_HH
6#if __has_include(<sys/mman.h>)
9#define HAVE_SYS_MMAN_H 1
10#define HAVE_MPROTECT 1
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([[maybe_unused]]
void* from,
68 [[maybe_unused]] difference_type len,
69 [[maybe_unused]]
int prot)
71#if HAVE_SYS_MMAN_H && HAVE_MPROTECT
72 int result = mprotect(from, len, prot);
76 std::cerr <<
"ERROR: (" << result <<
": " << strerror(result) <<
")" << std::endl;
77 std::cerr <<
" Failed to ";
78 if (prot == PROT_NONE)
79 std::cerr <<
"protect ";
81 std::cerr <<
"unprotect ";
82 std::cerr <<
"memory range: "
84 <<
static_cast<void*
>(
85 static_cast<char*
>(from) + len)
90 std::cerr <<
"WARNING: memory protection not available" << std::endl;
98 AllocationList::iterator it;
100 for (it=allocation_list.begin(); it!=allocation_list.end(); it++)
104 std::cerr <<
"ERROR: found memory chunk still in use: " <<
105 it->capacity <<
" bytes at " << it->ptr << std::endl;
108 munmap(it->page_ptr, it->pages * page_size);
111 allocation_error(
"lost allocations");
115 T* allocate(size_type n)
118 AllocationInfo ai(
typeid(T));
120 ai.capacity = n *
sizeof(T);
121 ai.pages = (ai.capacity) / page_size + 2;
123 size_type overlap = ai.capacity % page_size;
124 ai.page_ptr = mmap(NULL, ai.pages * page_size,
125 PROT_READ | PROT_WRITE,
127 MAP_ANON | MAP_PRIVATE,
129 MAP_ANONYMOUS | MAP_PRIVATE,
132 if (MAP_FAILED == ai.page_ptr)
134 throw std::bad_alloc();
136 ai.ptr =
static_cast<char*
>(ai.page_ptr) + page_size - overlap;
138 memprotect(
static_cast<char*
>(ai.page_ptr) + (ai.pages-1) * page_size,
142 allocation_list.push_back(ai);
144 return static_cast<T*
>(ai.ptr);
148 void deallocate(T* ptr, size_type n = 0) noexcept
153 (
char*)(ptr) - ((std::uintptr_t)(ptr) % page_size));
155 AllocationList::iterator it;
157 for (it=allocation_list.begin(); it!=allocation_list.end(); it++, i++)
159 if (it->page_ptr == page_ptr)
164 ALLOCATION_ASSERT(n == it->size);
165 ALLOCATION_ASSERT(ptr == it->ptr);
166 ALLOCATION_ASSERT(
true == it->not_free);
167 ALLOCATION_ASSERT(
typeid(T) == *(it->type));
169 it->not_free =
false;
170#if DEBUG_ALLOCATOR_KEEP
172 memprotect(it->page_ptr,
173 (it->pages) * page_size,
177 memprotect(it->page_ptr,
178 (it->pages) * page_size,
179 PROT_READ | PROT_WRITE);
180 munmap(it->page_ptr, it->pages * page_size);
182 allocation_list.erase(it);
187 allocation_error(
"memory block not found");
190#undef ALLOCATION_ASSERT
192 extern AllocationManager alloc_man;
197 class DebugAllocator;
201 class DebugAllocator<void> {
203 typedef void* pointer;
204 typedef const void* const_pointer;
206 typedef void value_type;
207 template <
class U>
struct rebind {
208 typedef DebugAllocator<U> other;
232 class DebugAllocator {
234 typedef std::size_t size_type;
235 typedef std::ptrdiff_t difference_type;
237 typedef const T* const_pointer;
238 typedef T& reference;
239 typedef const T& const_reference;
240 typedef T value_type;
241 template <
class U>
struct rebind {
242 typedef DebugAllocator<U> other;
246 DebugAllocator() noexcept {}
249 DebugAllocator(
const DebugAllocator<U>&)
noexcept {}
251 ~DebugAllocator() noexcept {}
253 pointer address(reference x)
const
257 const_pointer address(const_reference x)
const
263 pointer allocate(size_type n,
264 [[maybe_unused]] DebugAllocator<void>::const_pointer hint = 0)
266 return DebugMemory::alloc_man.allocate<T>(n);
270 void deallocate(pointer p, size_type n)
272 DebugMemory::alloc_man.deallocate<T>(p,n);
276 size_type max_size() const noexcept
278 return size_type(-1) /
sizeof(T);
282 void construct(pointer p,
const T& val)
284 ::new((
void*)p)T(val);
288 template<
typename ... Args>
289 void construct(pointer p, Args&&... args)
291 ::new((
void *)p)T(std::forward<Args>(args) ...);
295 void destroy(pointer p)
304 operator==(
const DebugAllocator<T> &,
const DebugAllocator<T> &)
312 operator!=(
const DebugAllocator<T> &,
const DebugAllocator<T> &)
318#ifdef DEBUG_NEW_DELETE
319void *
operator new(
size_t size)
322 void *p = Dune::DebugMemory::alloc_man.allocate<
char>(size);
323#if DEBUG_NEW_DELETE > 2
324 std::cout <<
"NEW " << size
331void operator delete(
void * p)
noexcept
333#if DEBUG_NEW_DELETE > 2
334 std::cout <<
"FREE " << p << std::endl;
336 Dune::DebugMemory::alloc_man.deallocate<
char>(
static_cast<char*
>(p));
339void operator delete(
void * p,
size_t size)
noexcept
341#if DEBUG_NEW_DELETE > 2
342 std::cout <<
"FREE " << p << std::endl;
344 Dune::DebugMemory::alloc_man.deallocate<
char>(
static_cast<char*
>(p), size);
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:235
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:257
Allocators that use malloc/free.
Dune namespace.
Definition: alignedallocator.hh:11