Skip to content
Snippets Groups Projects
Commit 484bb2d7 authored by Dominic Kempf's avatar Dominic Kempf
Browse files

Update the allocator and its doxgen docu

parent 299e8a63
No related branches found
No related tags found
No related merge requests found
......@@ -290,85 +290,80 @@ namespace Dune
}
};
template <class T>
/** @brief a debug allocator that initializes its memory with random bits
* @tparam T the type to allocate storage for
* based on a template by N. Josuttis
*
* Implicitly relying on allocated memory to contain zeroes is an important
* source of errors, as different systems might behave different and such.
* Tools as valgrind are of great use, but dont find all problems. This allocator
* simply wraps new and delete, but fills all allocated memory with random bits.
* This should help testing and debugging code, as in a correct implementation
* using this allocator mustnt change the result at all.
*/
template <class T>
class InitAllocator
{
public:
// type definitions
typedef T value_type;
typedef T* pointer;
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// rebind allocator to type U
template <class U>
struct rebind
{
typedef InitAllocator<U> other;
};
// return address of values
//! return address of values
pointer address (reference value) const
{
return &value;
}
const_pointer address (const_reference value) const
{
return &value;
}
/* constructors and destructor
* - nothing to do because the allocator has no state
*/
//! constructors and destructors do nothing
InitAllocator() throw() {}
InitAllocator(const InitAllocator&) throw() {}
template <class U>
InitAllocator(const InitAllocator<U>&) throw() {}
~InitAllocator() throw() {}
// return maximum number of elements that can be allocated
//! return maximum number of elements that can be allocated
size_type max_size () const throw()
{
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
// allocate but don't initialize num elements of type T
//! allocate and initialize with random bits num elements of type T
pointer allocate (size_type num, const void* = 0)
{
srand(time(NULL));
pointer ret = (pointer)(::operator new(num*sizeof(T)));
for (size_type i=0; i<num; i++)
{
char c[sizeof(T)];
for (size_type j=0; j<sizeof(T); j++)
c[j] = rand()%256;
new(ret+i) value_type(*reinterpret_cast<pointer>(c));
}
for (size_type i=0; i<num*sizeof(T); i++)
*(reinterpret_cast<char*>(ret)+i) = rand()%256;
return ret;
}
// initialize elements of allocated storage p with value value
//! initialize elements of allocated storage p with value value
void construct (pointer p, const T& value)
{
// initialize memory with placement new
new((void*)p)T(value);
}
// destroy elements of initialized storage p
//! destroy elements of initialized storage p
void destroy (pointer p)
{
// destroy objects by calling their destructor
p->~T();
}
// deallocate storage p of deleted elements
//! deallocate storage p of deleted elements
void deallocate (pointer p, size_type num)
{
::operator delete((void*)p);
......
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