dune-common
2.3.0
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
dune
common
mallocallocator.hh
Go to the documentation of this file.
1
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
// vi: set et ts=4 sw=2 sts=2:
3
#ifndef DUNE_MALLOC_ALLOCATOR_HH
4
#define DUNE_MALLOC_ALLOCATOR_HH
5
6
#include <exception>
7
#include <cstdlib>
8
#include <new>
9
#include <utility>
10
#include <
dune/common/unused.hh
>
11
16
namespace
Dune
17
{
22
template
<
class
T>
23
class
MallocAllocator
{
24
public
:
25
typedef
std::size_t
size_type
;
26
typedef
std::ptrdiff_t
difference_type
;
27
typedef
T*
pointer
;
28
typedef
const
T*
const_pointer
;
29
typedef
T&
reference
;
30
typedef
const
T&
const_reference
;
31
typedef
T
value_type
;
32
template
<
class
U>
struct
rebind
{
33
typedef
MallocAllocator<U>
other
;
34
};
35
37
MallocAllocator
() throw() {}
39
template
<
class
U>
40
MallocAllocator
(
const
MallocAllocator<U>
&) throw() {}
42
~MallocAllocator
() throw() {}
43
44
pointer
address
(
reference
x)
const
45
{
46
return
&x;
47
}
48
const_pointer
address
(
const_reference
x)
const
49
{
50
return
&x;
51
}
52
54
pointer
allocate
(
size_type
n,
55
const
void
* hint = 0)
56
{
57
DUNE_UNUSED_PARAMETER
(hint);
58
if
(n > this->
max_size
())
59
throw
std::bad_alloc();
60
61
pointer
ret =
static_cast<
pointer
>
(std::malloc(n *
sizeof
(T)));
62
if
(!ret)
63
throw
std::bad_alloc();
64
return
ret;
65
}
66
68
void
deallocate
(
pointer
p,
size_type
n)
69
{
70
DUNE_UNUSED_PARAMETER
(n);
71
std::free(p);
72
}
73
75
size_type
max_size
()
const
throw()
76
{
77
return
size_type
(-1) /
sizeof
(T);
78
}
79
81
void
construct
(
pointer
p,
const
T& val)
82
{
83
::new((
void
*)p)T(val);
84
}
85
#if ( HAVE_VARIADIC_TEMPLATES && HAVE_RVALUE_REFERENCES ) || DOXYGEN
86
87
88
template
<
typename
... _Args>
89
void
construct
(
pointer
p, _Args&&... __args)
90
{
91
::new((
void
*)p)T(std::forward<_Args>(__args) ...);
92
}
93
#endif
94
95
void
destroy
(
pointer
p)
96
{
97
p->~T();
98
}
99
};
100
}
101
102
#endif // DUNE_MALLOC_ALLOCATOR_HH
Generated on Mon Feb 10 2014 23:52:38 for dune-common by
1.8.1.2