Rheolef
7.2
an efficient C++ finite element environment
Loading...
Searching...
No Matches
heap_object.h
Go to the documentation of this file.
1
#ifndef _RHEOLEF_HEAP_OBJECT_H
2
#define _RHEOLEF_HEAP_OBJECT_H
23
24
#include "rheolef/compiler.h"
25
namespace
rheolef
{
26
27
template
<
class
T>
28
class
heap_object
{
29
public
:
30
typedef
size_t
size_type
;
31
heap_object
(
size_type
sizeof_bucket =
sizeof
(
T
));
32
~heap_object
();
33
T
*
new_bucket
()
const
;
34
#ifdef TO_CLEAN
35
T
* malloc()
const
{
return
new_bucket
(); }
// for boost::object_pool compatibility
36
#endif
// TO_CLEAN
37
size_type
size
()
const
{
return
_counter
; }
38
void
reinitialize
(size_type sizeof_bucket =
sizeof
(
T
));
39
void
clear
();
40
protected
:
41
mutable
std::list<std::vector<char> >
_heap
;
42
mutable
size_type
_heap_block_size
;
43
mutable
size_type
_heap_block_last_free
;
44
size_type
_sizeof_bucket
;
45
mutable
size_type
_counter
;
46
static
const
size_type
_heap_block_size_init
= 10;
47
};
48
template
<
class
T>
49
inline
50
heap_object<T>::heap_object
(
size_type
sz)
51
:
_heap
(),
52
_heap_block_size
(
_heap_block_size_init
),
53
_heap_block_last_free
(0),
54
_sizeof_bucket
(sz),
55
_counter
(0)
56
{
57
_heap
.push_front(std::vector<char>(
_heap_block_size
*
_sizeof_bucket
));
58
}
59
template
<
class
T>
60
inline
61
void
62
heap_object<T>::reinitialize
(
size_type
sz)
63
{
64
clear
();
65
_heap_block_size
=
heap_object<T>::_heap_block_size_init
;
66
_heap_block_last_free
= 0;
67
_sizeof_bucket
= sz;
68
_counter
= 0;
69
_heap
.push_front(std::vector<char>(
_heap_block_size
*
_sizeof_bucket
));
70
}
71
template
<
class
T>
72
inline
73
T
*
74
heap_object<T>::new_bucket
()
const
75
{
76
if
(
_heap_block_last_free
==
_heap_block_size
) {
77
_heap_block_size
*= 2;
78
_heap
.push_front(std::vector<char>(
_heap_block_size
*
_sizeof_bucket
));
79
_heap_block_last_free
= 0;
80
}
81
std::vector<char>& block = *(
_heap
.begin());
82
char
*
p
= &block [
_heap_block_last_free
*
_sizeof_bucket
];
83
_heap_block_last_free
++;
84
new
((
void
*)
p
)
T
();
// call default T constructor at p
85
_counter
++;
86
return
(
T
*)
p
;
87
}
88
template
<
class
T>
89
inline
90
heap_object<T>::~heap_object
()
91
{
92
clear
();
93
}
94
template
<
class
T>
95
void
96
heap_object<T>::clear
()
97
{
98
size_type
n =
_heap_block_size_init
;
99
for
(std::list<std::vector<char> >::reverse_iterator i =
_heap
.rbegin();
100
_counter
!= 0 && i !=
_heap
.rend(); i++, n *= 2) {
101
std::vector<char>& block = *i;
102
char
*
p
= &(block[0]);
103
for
(
size_type
c = 0;
_counter
!= 0 && c < n; c++,
p
+=
_sizeof_bucket
) {
104
((
T
*)
p
)->~T();
105
_counter
--;
106
}
107
}
108
_heap
.erase(
_heap
.begin(),
_heap
.end());
109
}
110
}
// namespace rheolef
111
#endif
// _RHEOLEF_HEAP_OBJECT_H
rheolef::heap_object::_heap
std::list< std::vector< char > > _heap
Definition
heap_object.h:41
rheolef::heap_object::_counter
size_type _counter
Definition
heap_object.h:45
rheolef::heap_object::heap_object
heap_object(size_type sizeof_bucket=sizeof(T))
Definition
heap_object.h:50
rheolef::heap_object::size_type
size_t size_type
Definition
heap_object.h:30
rheolef::heap_object::size
size_type size() const
Definition
heap_object.h:37
rheolef::heap_object::_heap_block_size_init
static const size_type _heap_block_size_init
Definition
heap_object.h:46
rheolef::heap_object::reinitialize
void reinitialize(size_type sizeof_bucket=sizeof(T))
Definition
heap_object.h:62
rheolef::heap_object::_sizeof_bucket
size_type _sizeof_bucket
Definition
heap_object.h:44
rheolef::heap_object::~heap_object
~heap_object()
Definition
heap_object.h:90
rheolef::heap_object::new_bucket
T * new_bucket() const
Definition
heap_object.h:74
rheolef::heap_object::_heap_block_last_free
size_type _heap_block_last_free
Definition
heap_object.h:43
rheolef::heap_object::clear
void clear()
Definition
heap_object.h:96
rheolef::heap_object::_heap_block_size
size_type _heap_block_size
Definition
heap_object.h:42
T
Expr1::float_type T
Definition
field_expr.h:230
rheolef
This file is part of Rheolef.
Definition
compiler_eigen.h:39
p
Definition
sphere.icc:25