/usr/include/dune/common/debugallocator.hh is in libdune-common-dev 2.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_DEBUG_ALLOCATOR_HH
#define DUNE_DEBUG_ALLOCATOR_HH
#include <dune/common/unused.hh>
#include <exception>
#include <typeinfo>
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdint>
#include <cstdlib>
#include <new>
#if HAVE_SYS_MMAN_H && HAVE_MPROTECT
#include <sys/mman.h>
#else
enum DummyProtFlags { PROT_NONE, PROT_WRITE, PROT_READ };
#endif
#include "mallocallocator.hh"
namespace Dune
{
#ifndef DOXYGEN // hide implementation details from doxygen
namespace DebugMemory
{
extern const std::ptrdiff_t page_size;
struct AllocationManager
{
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef void* pointer;
protected:
static void allocation_error(const char* msg);
struct AllocationInfo;
friend struct AllocationInfo;
#define ALLOCATION_ASSERT(A) { if (!(A)) \
{ allocation_error("Assertion " # A " failed");\
}\
};
struct AllocationInfo
{
AllocationInfo(const std::type_info & t) : type(&t) {}
const std::type_info * type;
pointer page_ptr;
pointer ptr;
size_type pages;
size_type capacity;
size_type size;
bool not_free;
};
typedef MallocAllocator<AllocationInfo> Alloc;
typedef std::vector<AllocationInfo, Alloc> AllocationList;
AllocationList allocation_list;
private:
void memprotect(void* from, difference_type len, int prot)
{
#if HAVE_SYS_MMAN_H && HAVE_MPROTECT
int result = mprotect(from, len, prot);
if (result == -1)
{
std::cerr << "ERROR: (" << result << ": " << strerror(result) << ")" << std::endl;
std::cerr << " Failed to ";
if (prot == PROT_NONE)
std::cerr << "protect ";
else
std::cerr << "unprotect ";
std::cerr << "memory range: "
<< from << ", "
<< static_cast<void*>(
static_cast<char*>(from) + len)
<< std::endl;
abort();
}
#else
DUNE_UNUSED_PARAMETER(from);
DUNE_UNUSED_PARAMETER(len);
DUNE_UNUSED_PARAMETER(prot);
std::cerr << "WARNING: memory protection not available" << std::endl;
#endif
}
public:
~AllocationManager ()
{
AllocationList::iterator it;
bool error = false;
for (it=allocation_list.begin(); it!=allocation_list.end(); it++)
{
if (it->not_free)
{
std::cerr << "ERROR: found memory chunk still in use: " <<
it->capacity << " bytes at " << it->ptr << std::endl;
error = true;
}
munmap(it->page_ptr, it->pages * page_size);
}
if (error)
allocation_error("lost allocations");
}
template<typename T>
T* allocate(size_type n) throw(std::bad_alloc)
{
// setup chunk info
AllocationInfo ai(typeid(T));
ai.size = n;
ai.capacity = n * sizeof(T);
ai.pages = (ai.capacity) / page_size + 2;
ai.not_free = true;
size_type overlap = ai.capacity % page_size;
ai.page_ptr = mmap(NULL, ai.pages * page_size,
PROT_READ | PROT_WRITE,
#ifdef __APPLE__
MAP_ANON | MAP_PRIVATE,
#else
MAP_ANONYMOUS | MAP_PRIVATE,
#endif
-1, 0);
if (MAP_FAILED == ai.page_ptr)
{
throw std::bad_alloc();
}
ai.ptr = static_cast<char*>(ai.page_ptr) + page_size - overlap;
// write protect memory behind the actual data
memprotect(static_cast<char*>(ai.page_ptr) + (ai.pages-1) * page_size,
page_size,
PROT_NONE);
// remember the chunk
allocation_list.push_back(ai);
// return the ptr
return static_cast<T*>(ai.ptr);
}
template<typename T>
void deallocate(T* ptr, size_type n = 0) throw()
{
// compute page address
void* page_ptr =
static_cast<void*>(
(char*)(ptr) - ((std::uintptr_t)(ptr) % page_size));
// search list
AllocationList::iterator it;
unsigned int i = 0;
for (it=allocation_list.begin(); it!=allocation_list.end(); it++, i++)
{
if (it->page_ptr == page_ptr)
{
// std::cout << "found memory_block in allocation " << i << std::endl;
// sanity checks
if (n != 0)
ALLOCATION_ASSERT(n == it->size);
ALLOCATION_ASSERT(ptr == it->ptr);
ALLOCATION_ASSERT(true == it->not_free);
ALLOCATION_ASSERT(typeid(T) == *(it->type));
// free memory
it->not_free = false;
#if DEBUG_ALLOCATOR_KEEP
// write protect old memory
memprotect(it->page_ptr,
(it->pages) * page_size,
PROT_NONE);
#else
// unprotect old memory
memprotect(it->page_ptr,
(it->pages) * page_size,
PROT_READ | PROT_WRITE);
munmap(it->page_ptr, it->pages * page_size);
// remove chunk info
allocation_list.erase(it);
#endif
return;
}
}
allocation_error("memory block not found");
}
};
#undef ALLOCATION_ASSERT
extern AllocationManager alloc_man;
} // end namespace DebugMemory
#endif // DOXYGEN
template<class T>
class DebugAllocator;
// specialize for void
template <>
class DebugAllocator<void> {
public:
typedef void* pointer;
typedef const void* const_pointer;
// reference to void members are impossible.
typedef void value_type;
template <class U> struct rebind {
typedef DebugAllocator<U> other;
};
};
// actual implementation
/**
@ingroup Allocators
@brief Allocators implementation which performs different kind of memory checks
We check:
- access past the end
- only free memory which was allocated with this allocator
- list allocated memory chunks still in use upon destruction of the allocator
When defining DEBUG_ALLOCATOR_KEEP to 1, we also check
- double free
- access after free
When defining DEBUG_NEW_DELETE >= 1, we
- overload new/delte
- use the Debug memory management for new/delete
- DEBUG_NEW_DELETE > 2 gives extensive debug output
*/
template <class T>
class DebugAllocator {
public:
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
template <class U> struct rebind {
typedef DebugAllocator<U> other;
};
//! create a new DebugAllocator
DebugAllocator() throw() {}
//! copy construct from an other DebugAllocator, possibly for a different result type
template <class U>
DebugAllocator(const DebugAllocator<U>&) throw() {}
//! cleanup this allocator
~DebugAllocator() throw() {}
pointer address(reference x) const
{
return &x;
}
const_pointer address(const_reference x) const
{
return &x;
}
//! allocate n objects of type T
pointer allocate(size_type n,
DebugAllocator<void>::const_pointer hint = 0)
{
DUNE_UNUSED_PARAMETER(hint);
return DebugMemory::alloc_man.allocate<T>(n);
}
//! deallocate n objects of type T at address p
void deallocate(pointer p, size_type n)
{
DebugMemory::alloc_man.deallocate<T>(p,n);
}
//! max size for allocate
size_type max_size() const throw()
{
return size_type(-1) / sizeof(T);
}
//! copy-construct an object of type T (i.e. make a placement new on p)
void construct(pointer p, const T& val)
{
::new((void*)p)T(val);
}
//! construct an object of type T from variadic parameters
template<typename ... _Args>
void construct(pointer p, _Args&&... __args)
{
::new((void *)p)T(std::forward<_Args>(__args) ...);
}
//! destroy an object of type T (i.e. call the destructor)
void destroy(pointer p)
{
p->~T();
}
};
}
#ifdef DEBUG_NEW_DELETE
void * operator new(size_t size) throw(std::bad_alloc)
{
// try to allocate size bytes
void *p = Dune::DebugMemory::alloc_man.allocate<char>(size);
#if DEBUG_NEW_DELETE > 2
std::cout << "NEW " << size
<< " -> " << p
<< std::endl;
#endif
return p;
}
void operator delete(void * p) throw()
{
#if DEBUG_NEW_DELETE > 2
std::cout << "FREE " << p << std::endl;
#endif
Dune::DebugMemory::alloc_man.deallocate<char>(static_cast<char*>(p));
}
#endif // DEBUG_NEW_DELETE
#endif // DUNE_DEBUG_ALLOCATOR_HH
|