heap_allocator.h
Go to the documentation of this file.
#ifndef _RHEOLEF_HEAP_ALLOCATOR_H
#define _RHEOLEF_HEAP_ALLOCATOR_H
#include <memory>
#include <limits>
#include "rheolef/compiler.h"
#include "rheolef/pretty_name.h"
namespace rheolef {
/*Class:man
NAME: heap_allocator - heap-based allocator
DESCRIPTION:
Heap allocators are generally used when there is a lot of allocation and deallocation
of small objects.
For instance, this is often the case when dealing with @code{std::list} and @code{std::map}.
Heap-based allocator is conform to the STL specification of allocators.
It does not "free" the memory until the heap is destroyed.
This allocator handles an a priori unlimited area of memory: a sequence
of growing chunks are allocated.
For a limited memory handler in the same spirit, see "stack_allocator"(9).
EXAMPLE:
@example
typedef map <size_t, double, less<size_t>, heap_allocator<pair<size_t,double> > > map_type;
map_type a;
a.insert (make_pair (0, 3.14));
a.insert (make_pair (1, 1.17));
for (map_type::iterator iter = a.begin(), last = a.end(); iter != last; iter++) @{
cout << (*iter).first << " " << (*iter).second << endl;
@}
@end example
SEE ALSO: "stack_allocator"(9).
AUTHORS:
LJK-IMAG, 38041 Grenoble cedex 9, France
| Pierre.Saramito@imag.fr
DATE: 15 december 2010
End:
*/
#define _RHEOLEF_DO_NOT_USE_HEAP_ALLOCATOR
#ifdef _RHEOLEF_DO_NOT_USE_HEAP_ALLOCATOR
template<class T> using heap_allocator = std::allocator<T>;
#else // _RHEOLEF_DO_NOT_USE_HEAP_ALLOCATOR
//<verbatim:
template <typename T>
class heap_allocator {
protected:
struct handler_type; // forward declaration:
public:
// typedefs:
typedef 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;
// constructors:
heap_allocator() throw()
: handler (new handler_type)
{
}
heap_allocator (const heap_allocator& ha) throw()
: handler (ha.handler)
{
++handler->reference_count;
}
template <typename U>
heap_allocator (const heap_allocator<U>& ha) throw()
: handler ((typename heap_allocator<T>::handler_type*)(ha.handler))
{
++handler->reference_count;
}
~heap_allocator() throw()
{
check_macro (handler != NULL, "unexpected null mem_info");
if (--handler->reference_count == 0) delete handler;
}
// Rebind to allocators of other types
template <typename U>
struct rebind {
typedef heap_allocator<U> other;
};
// assignment:
heap_allocator& operator= (const heap_allocator& ha)
{
handler = ha.handler;
++handler->reference_count;
return *this;
}
// utility functions:
pointer address (reference r) const { return &r; }
const_pointer address (const_reference c) const { return &c; }
size_type max_size() const { return std::numeric_limits<size_t>::max() / sizeof(T); }
// in-place construction/destruction
void construct (pointer p, const_reference c)
{
// placement new operator:
new( reinterpret_cast<void*>(p) ) T(c);
}
// C++ 2011: default construct a value of type T at the location referenced by p
void construct (pointer p) { new ( reinterpret_cast<void*>(p) ) T(); }
void destroy (pointer p)
{
// call destructor directly:
(p)->~T();
}
// allocate raw memory
pointer allocate (size_type n, const void* = NULL)
{
return pointer (handler->raw_allocate (n*sizeof(T)));
}
void deallocate (pointer p, size_type n)
{
// No need to free heap memory
}
const handler_type* get_handler() const {
return handler;
}
// data:
protected:
handler_type* handler;
template <typename U> friend class heap_allocator;
};
//>verbatim:
// Comparison
template <typename T1>
bool operator== (const heap_allocator<T1>& lhs, const heap_allocator<T1>& rhs) throw()
{
return lhs.get_handler() == rhs.get_handler();
}
template <typename T1>
bool operator!= (const heap_allocator<T1>& lhs, const heap_allocator<T1>& rhs) throw()
{
return lhs.get_handler() != rhs.get_handler();
}
// ==========================================================================
// heap_allocation::handler class implementation
// ==========================================================================
template<class T>
struct heap_allocator<T>::handler_type {
// cstors:
handler_type ();
~handler_type();
// modifier:
unsigned char* raw_allocate (size_type size);
// data:
std::list<std::vector<unsigned char> > heap;
size_type heap_block_size;
size_type heap_block_last_free;
size_type reference_count;
static const size_type heap_block_size_init = 512*sizeof(T);
};
template<class T>
inline
heap_allocator<T>::handler_type::handler_type()
: heap (),
heap_block_size (heap_block_size_init),
heap_block_last_free (0),
reference_count (1)
{
heap.push_front (std::vector<unsigned char>(heap_block_size));
}
template<class T>
inline
unsigned char*
heap_allocator<T>::handler_type::raw_allocate (size_type size)
{
if (heap_block_last_free + size > heap_block_size) {
heap_block_size = std::max (size, 2*heap_block_size);
heap.push_front (std::vector<unsigned char>(heap_block_size));
heap_block_last_free = 0;
}
std::vector<unsigned char>& block = *(heap.begin());
unsigned char* p = &(block [heap_block_last_free]);
heap_block_last_free += size;
return p;
}
template<class T>
inline
heap_allocator<T>::handler_type::~handler_type()
{
heap.erase (heap.begin(), heap.end());
}
#endif // _RHEOLEF_DO_NOT_USE_HEAP_ALLOCATOR
}// namespace rheolef
#endif // _RHEOLEF_HEAP_ALLOCATOR_H
rheolef::heap_allocator::deallocate
void deallocate(pointer p, size_type n)
Definition: heap_allocator.h:149
rheolef::heap_allocator::handler_type::heap_block_size
size_type heap_block_size
Definition: heap_allocator.h:190
rheolef::heap_allocator::get_handler
const handler_type * get_handler() const
Definition: heap_allocator.h:153
rheolef::heap_allocator::const_pointer
const typedef T * const_pointer
Definition: heap_allocator.h:79
rheolef::heap_allocator::size_type
size_t size_type
Definition: heap_allocator.h:71
rheolef::operator!=
bool operator!=(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition: heap_allocator.h:172
rheolef::heap_allocator::heap_allocator
heap_allocator(const heap_allocator< U > &ha)
Definition: heap_allocator.h:96
check_macro
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
rheolef::heap_allocator::heap_allocator
heap_allocator(const heap_allocator &ha)
Definition: heap_allocator.h:90
rheolef::heap_allocator::reference
T & reference
Definition: heap_allocator.h:80
rheolef::heap_allocator::rebind
Definition: heap_allocator.h:108
rheolef::heap_allocator::pointer
T * pointer
Definition: heap_allocator.h:78
rheolef::heap_allocator::address
pointer address(reference r) const
Definition: heap_allocator.h:123
mkgeo_ball.c
c
Definition: mkgeo_ball.sh:153
rheolef::heap_allocator::handler_type::raw_allocate
unsigned char * raw_allocate(size_type size)
Definition: heap_allocator.h:208
rheolef::heap_allocator::handler_type::~handler_type
~handler_type()
Definition: heap_allocator.h:222
rheolef::heap_allocator::rebind::other
heap_allocator< U > other
Definition: heap_allocator.h:109
rheolef::operator==
bool operator==(const heap_allocator< T1 > &lhs, const heap_allocator< T1 > &rhs)
Definition: heap_allocator.h:167
p
Definition: sphere.icc:25
rheolef::heap_allocator::handler_type::reference_count
size_type reference_count
Definition: heap_allocator.h:192
rheolef::heap_allocator::handler_type
Definition: heap_allocator.h:181
rheolef::heap_allocator::allocate
pointer allocate(size_type n, const void *=NULL)
Definition: heap_allocator.h:145
rheolef::heap_allocator::const_reference
const typedef T & const_reference
Definition: heap_allocator.h:81
rheolef::heap_allocator::difference_type
std::ptrdiff_t difference_type
Definition: heap_allocator.h:77
rheolef::heap_allocator::heap_allocator
heap_allocator()
Definition: heap_allocator.h:86
rheolef
This file is part of Rheolef.
Definition: compiler_eigen.h:37
rheolef::heap_allocator::handler_type::heap_block_last_free
size_type heap_block_last_free
Definition: heap_allocator.h:191
rheolef::heap_allocator::handler_type::handler_type
handler_type()
Definition: heap_allocator.h:197
rheolef::heap_allocator::max_size
size_type max_size() const
Definition: heap_allocator.h:125
rheolef::heap_allocator::address
const_pointer address(const_reference c) const
Definition: heap_allocator.h:124
rheolef::heap_allocator::construct
void construct(pointer p, const_reference c)
Definition: heap_allocator.h:129
mkgeo_ball.n
n
Definition: mkgeo_ball.sh:150
rheolef::heap_allocator::operator=
heap_allocator & operator=(const heap_allocator &ha)
Definition: heap_allocator.h:114
rheolef::heap_allocator::destroy
void destroy(pointer p)
Definition: heap_allocator.h:137
rheolef::heap_allocator::construct
void construct(pointer p)
Definition: heap_allocator.h:135
rheolef::heap_allocator::~heap_allocator
~heap_allocator()
Definition: heap_allocator.h:101
rheolef::heap_allocator::value_type
T value_type
Definition: heap_allocator.h:82
rheolef::heap_allocator::handler_type::heap
std::list< std::vector< unsigned char > > heap
Definition: heap_allocator.h:189
rheolef::heap_allocator::handler
handler_type * handler
Definition: heap_allocator.h:160
T
Expr1::float_type T
Definition: field_expr.h:218
rheolef::heap_allocator
Definition: heap_allocator.h:69