Bridge++
Ver. 2.0.2
aligned_allocator_impl.h
Go to the documentation of this file.
1
14
//====================================================================
15
16
#ifndef __ALINGED_ALLOCATOR_IMPL_H__
17
#define __ALINGED_ALLOCATOR_IMPL_H__
18
19
#include <stdlib.h>
20
#include <cstddef>
21
22
/*
23
cf.
24
Programing Language C++ 4th eidtion
25
Bjarne Stroustrup
26
sec. 34.4
27
28
see also
29
allocator.h in stdc++ lib
30
31
*/
32
template
<
typename
_Tp,
int
AlignmentSize>
33
struct
aligned_allocator_impl
34
{
35
typedef
size_t
size_type
;
36
typedef
ptrdiff_t
difference_type
;
37
typedef
_Tp *
pointer
;
38
typedef
const
_Tp *
const_pointer
;
39
typedef
_Tp&
reference
;
40
typedef
const
_Tp&
const_reference
;
41
typedef
_Tp
value_type
;
42
43
aligned_allocator_impl
() throw() {}
44
~aligned_allocator_impl
() throw() {}
45
46
template
<
typename
T>
47
using
allocator_t
=
aligned_allocator_impl<T, AlignmentSize>
;
48
template
<
typename
_Tp1>
49
struct
rebind
50
{
typedef
allocator_t<_Tp1>
other
; };
51
52
_Tp *
allocate
(
size_t
n)
53
{
54
void
*p = 0;
55
void
**memptr = &p;
56
int
err = posix_memalign(memptr, AlignmentSize,
sizeof
(_Tp) * n);
57
if
(err) {
58
throw
std::bad_alloc();
59
}
60
return
reinterpret_cast<
_Tp *
>
(p);
61
}
62
63
void
deallocate
(_Tp *p,
size_t
)
64
{
65
free(p);
66
}
67
};
68
69
template
<
typename
_T1,
int
S1,
typename
_T2,
int
S2>
70
inline
bool
71
operator==
(
const
aligned_allocator_impl<_T1, S1>
&,
72
const
aligned_allocator_impl<_T2, S2>
&)
73
{
return
true
; }
74
75
template
<
typename
_Tp,
int
S>
76
inline
bool
77
operator==
(
const
aligned_allocator_impl<_Tp, S>
&,
78
const
aligned_allocator_impl<_Tp, S>
&)
79
{
return
true
; }
80
81
template
<
typename
_T1,
int
S1,
typename
_T2,
int
S2>
82
inline
bool
83
operator!=
(
const
aligned_allocator_impl<_T1, S1>
&,
84
const
aligned_allocator_impl<_T2, S2>
&)
85
{
return
false
; }
86
87
template
<
typename
_Tp,
int
S>
88
inline
bool
89
operator!=
(
const
aligned_allocator_impl<_Tp, S>
&,
const
aligned_allocator_impl<_Tp, S>
&)
90
{
return
false
; }
91
92
93
94
template
<
typename
_Tp,
int
AlignmentSize,
int
OffsetSize>
95
struct
aligned_allocator_offset_impl
96
{
97
typedef
size_t
size_type
;
98
typedef
ptrdiff_t
difference_type
;
99
typedef
_Tp *
pointer
;
100
typedef
const
_Tp *
const_pointer
;
101
typedef
_Tp&
reference
;
102
typedef
const
_Tp&
const_reference
;
103
typedef
_Tp
value_type
;
104
105
aligned_allocator_offset_impl
() throw() {}
106
~aligned_allocator_offset_impl
() throw() {}
107
108
template
<
typename
T>
109
using
allocator_t
=
aligned_allocator_offset_impl<T, AlignmentSize, OffsetSize>
;
110
template
<
typename
_Tp1>
111
struct
rebind
112
{
typedef
allocator_t<_Tp1>
other
; };
113
114
_Tp *
allocate
(
size_t
n)
115
{
116
void
*p = 0;
117
void
**memptr = &p;
118
assert(AlignmentSize %
sizeof
(_Tp) == 0);
119
int
offset = OffsetSize /
sizeof
(_Tp);
120
int
err = posix_memalign(memptr, AlignmentSize,
sizeof
(_Tp) * (n + 2 * offset));
121
if
(err) {
122
throw
std::bad_alloc();
123
}
124
return
reinterpret_cast<
_Tp *
>
(p) + offset;
125
}
126
127
void
deallocate
(_Tp *p,
size_t
)
128
{
129
int
offset = OffsetSize /
sizeof
(_Tp);
130
free(p - offset);
131
}
132
};
133
134
template
<
typename
_T1,
int
S1,
int
O1,
typename
_T2,
int
S2,
int
O2>
135
inline
bool
136
operator==
(
const
aligned_allocator_offset_impl<_T1, S1, O1>
&,
137
const
aligned_allocator_offset_impl<_T2, S2, O2>
&)
138
{
return
true
; }
139
140
template
<
typename
_Tp,
int
S,
int
O>
141
inline
bool
142
operator==
(
const
aligned_allocator_offset_impl<_Tp, S, O>
&,
143
const
aligned_allocator_offset_impl<_Tp, S, O>
&)
144
{
return
true
; }
145
146
template
<
typename
_T1,
int
S1,
int
O1,
typename
_T2,
int
S2,
int
O2>
147
inline
bool
148
operator!=
(
const
aligned_allocator_offset_impl<_T1, S1, O1>
&,
149
const
aligned_allocator_offset_impl<_T2, S2, O2>
&)
150
{
return
false
; }
151
152
template
<
typename
_Tp,
int
S,
int
O>
153
inline
bool
154
operator!=
(
const
aligned_allocator_offset_impl<_Tp, S, O>
&,
const
aligned_allocator_offset_impl<_Tp, S, O>
&)
155
{
return
false
; }
156
157
#endif
aligned_allocator_impl::aligned_allocator_impl
aligned_allocator_impl()
Definition:
aligned_allocator_impl.h:43
aligned_allocator_offset_impl::value_type
_Tp value_type
Definition:
aligned_allocator_impl.h:103
aligned_allocator_impl::reference
_Tp & reference
Definition:
aligned_allocator_impl.h:39
aligned_allocator_offset_impl::reference
_Tp & reference
Definition:
aligned_allocator_impl.h:101
aligned_allocator_impl::allocate
_Tp * allocate(size_t n)
Definition:
aligned_allocator_impl.h:52
aligned_allocator_offset_impl::rebind
Definition:
aligned_allocator_impl.h:111
aligned_allocator_impl::rebind::other
allocator_t< _Tp1 > other
Definition:
aligned_allocator_impl.h:50
aligned_allocator_impl::const_reference
const typedef _Tp & const_reference
Definition:
aligned_allocator_impl.h:40
operator!=
bool operator!=(const aligned_allocator_impl< _T1, S1 > &, const aligned_allocator_impl< _T2, S2 > &)
Definition:
aligned_allocator_impl.h:83
aligned_allocator_impl::pointer
_Tp * pointer
Definition:
aligned_allocator_impl.h:37
aligned_allocator_offset_impl::difference_type
ptrdiff_t difference_type
Definition:
aligned_allocator_impl.h:98
aligned_allocator_offset_impl::~aligned_allocator_offset_impl
~aligned_allocator_offset_impl()
Definition:
aligned_allocator_impl.h:106
aligned_allocator_impl::const_pointer
const typedef _Tp * const_pointer
Definition:
aligned_allocator_impl.h:38
aligned_allocator_impl::difference_type
ptrdiff_t difference_type
Definition:
aligned_allocator_impl.h:36
aligned_allocator_impl::rebind
Definition:
aligned_allocator_impl.h:49
aligned_allocator_offset_impl::const_pointer
const typedef _Tp * const_pointer
Definition:
aligned_allocator_impl.h:100
aligned_allocator_offset_impl::deallocate
void deallocate(_Tp *p, size_t)
Definition:
aligned_allocator_impl.h:127
aligned_allocator_impl
Definition:
aligned_allocator_impl.h:33
aligned_allocator_impl::size_type
size_t size_type
Definition:
aligned_allocator_impl.h:35
aligned_allocator_offset_impl::allocate
_Tp * allocate(size_t n)
Definition:
aligned_allocator_impl.h:114
operator==
bool operator==(const aligned_allocator_impl< _T1, S1 > &, const aligned_allocator_impl< _T2, S2 > &)
Definition:
aligned_allocator_impl.h:71
aligned_allocator_impl::value_type
_Tp value_type
Definition:
aligned_allocator_impl.h:41
aligned_allocator_offset_impl::const_reference
const typedef _Tp & const_reference
Definition:
aligned_allocator_impl.h:102
aligned_allocator_offset_impl
Definition:
aligned_allocator_impl.h:95
aligned_allocator_impl::deallocate
void deallocate(_Tp *p, size_t)
Definition:
aligned_allocator_impl.h:63
aligned_allocator_impl::~aligned_allocator_impl
~aligned_allocator_impl()
Definition:
aligned_allocator_impl.h:44
aligned_allocator_offset_impl::rebind::other
allocator_t< _Tp1 > other
Definition:
aligned_allocator_impl.h:112
aligned_allocator_offset_impl::size_type
size_t size_type
Definition:
aligned_allocator_impl.h:97
aligned_allocator_offset_impl::pointer
_Tp * pointer
Definition:
aligned_allocator_impl.h:99
aligned_allocator_offset_impl::aligned_allocator_offset_impl
aligned_allocator_offset_impl()
Definition:
aligned_allocator_impl.h:105
src
aligned_allocator_impl.h
Generated on Sat Feb 10 2024 14:19:59 for Bridge++ by
1.8.17