FEI
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
base
fei_Pool_alloc.hpp
1
/*--------------------------------------------------------------------*/
2
/* Copyright 2005 Sandia Corporation. */
3
/* Under the terms of Contract DE-AC04-94AL85000, there is a */
4
/* non-exclusive license for use of this work by or on behalf */
5
/* of the U.S. Government. Export of this program may require */
6
/* a license from the United States Government. */
7
/*--------------------------------------------------------------------*/
8
9
#ifndef _fei_Pool_alloc_hpp_
10
#define _fei_Pool_alloc_hpp_
11
12
#include "fei_macros.hpp"
13
#include "fei_Pool.hpp"
14
#include <cstddef>
15
#include <cstdlib>
16
#include <limits>
17
#include <new>
18
#include <stdexcept>
19
#include <iostream>
20
21
fei_Pool* get_fei_mem_pool(
size_t
n);
22
33
template
<
typename
T>
34
class
fei_Pool_alloc {
35
private
:
36
fei_Pool* mem;
//pool of elements
37
size_t
n_;
38
39
public
:
40
typedef
T value_type;
41
typedef
std::size_t size_type;
42
typedef
T* pointer;
43
typedef
const
T* const_pointer;
44
typedef
T& reference;
45
typedef
const
T& const_reference;
46
typedef
std::ptrdiff_t difference_type;
47
48
// Boilerplate allocator stuff
49
template
<
typename
U>
50
struct
rebind
51
{
52
typedef
fei_Pool_alloc<U> other;
53
};
54
55
pointer address (reference value)
const
56
{
57
return
&value;
58
}
59
const_pointer address (const_reference value)
const
60
{
61
return
&value;
62
}
63
64
fei_Pool_alloc()
throw
();
65
fei_Pool_alloc(
const
T&)
throw
();
66
template
<
typename
U> fei_Pool_alloc(
const
fei_Pool_alloc<U>&)
throw
()
67
: mem(NULL),n_(0) {}
68
69
~fei_Pool_alloc()
throw
();
70
71
pointer allocate(size_type n,
const
void
* hint = NULL);
72
void
deallocate(pointer p, size_type n);
73
74
template
<
typename
U>
void
construct(U* p,
const
U& val)
75
{
new
(p) U(val); }
76
77
void
construct(pointer p,
const
T& val)
78
{
new
(p) T(val); }
79
80
template
<
typename
U>
void
destroy(U* p)
81
{ p->~U(); }
82
83
void
destroy(pointer p)
84
{ p->~T(); }
85
86
size_type max_size()
const
throw
() {
return
std::numeric_limits<size_type>::max(); }
87
88
};
89
90
template
<
typename
T> fei_Pool_alloc<T>::fei_Pool_alloc() throw() : mem(NULL),n_(0) {}
91
template
<
typename
T> fei_Pool_alloc<T>::fei_Pool_alloc(
const
T&)
throw
() : mem(NULL),n_(0) {}
92
93
template
<
typename
T> fei_Pool_alloc<T>::~fei_Pool_alloc() throw() {}
94
95
template
<
typename
T>
96
T* fei_Pool_alloc<T>::allocate(size_type n,
const
void
*)
97
{
98
if
(n==0)
return
NULL;
99
100
if
(n_ == 0) {
101
n_ = n;
102
mem = get_fei_mem_pool(n_*
sizeof
(T));
103
}
104
105
if
(n != n_) {
106
std::cerr <<
"fei_Pool_alloc ERROR, allocate given bad length ("
<<n
107
<<
"), must be "
<<n_<<
". throwing exception."
<<std::endl;
108
throw
std::bad_alloc();
109
}
110
return
static_cast<
T*
>
(mem->alloc());
111
}
112
113
template
<
typename
T>
114
void
fei_Pool_alloc<T>::deallocate(pointer p, size_type n)
115
{
116
if
(p == NULL || n == 0)
return
;
117
118
if
(n == n_) {
119
mem->free(p);
120
return
;
121
}
122
123
std::cerr <<
"fei_Pool_alloc ERROR, deallocate given bad length ("
<<n
124
<<
"), must be "
<<n_<<
". aborting."
<<std::endl;
125
std::abort();
126
}
127
128
template
<
typename
T>
129
inline
bool
operator==(
const
fei_Pool_alloc<T>
&,
130
const
fei_Pool_alloc<T>
&)
throw
()
131
{
return
true
; }
132
template
<
typename
T>
133
inline
bool
operator!=(
const
fei_Pool_alloc<T>
&,
134
const
fei_Pool_alloc<T>
&)
throw
()
135
{
return
false
; }
136
137
#endif
138
fei_Pool_alloc
Definition
fei_Pool_alloc.hpp:34
Generated by
1.17.0