]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | /* |
b32b8144 FG |
2 | Copyright 2014 Glen Joseph Fernandes |
3 | (glenjofe@gmail.com) | |
7c673cae | 4 | |
b32b8144 FG |
5 | Distributed under the Boost Software License, Version 1.0. |
6 | (http://www.boost.org/LICENSE_1_0.txt) | |
7c673cae FG |
7 | */ |
8 | #include <boost/align/aligned_allocator_adaptor.hpp> | |
9 | #include <boost/align/is_aligned.hpp> | |
10 | #include <boost/core/lightweight_test.hpp> | |
11 | #include <new> | |
12 | #include <cstring> | |
13 | ||
14 | template<class T> | |
15 | class A { | |
16 | public: | |
17 | typedef T value_type; | |
18 | typedef T* pointer; | |
7c673cae FG |
19 | typedef std::size_t size_type; |
20 | typedef std::ptrdiff_t difference_type; | |
b32b8144 | 21 | |
7c673cae FG |
22 | template<class U> |
23 | struct rebind { | |
24 | typedef A<U> other; | |
25 | }; | |
b32b8144 FG |
26 | |
27 | A(int state) | |
28 | : state_(state) { } | |
29 | ||
7c673cae FG |
30 | template<class U> |
31 | A(const A<U>& other) | |
b32b8144 FG |
32 | : state_(other.state()) { } |
33 | ||
34 | T* allocate(std::size_t size, const void* = 0) { | |
7c673cae FG |
35 | return static_cast<T*>(::operator new(sizeof(T) * size)); |
36 | } | |
b32b8144 FG |
37 | |
38 | void deallocate(T* ptr, std::size_t) { | |
7c673cae FG |
39 | ::operator delete(ptr); |
40 | } | |
b32b8144 FG |
41 | |
42 | void construct(T* ptr, const T& value) { | |
7c673cae FG |
43 | ::new(static_cast<void*>(ptr)) T(value); |
44 | } | |
b32b8144 FG |
45 | |
46 | void destroy(T* ptr) { | |
7c673cae FG |
47 | ptr->~T(); |
48 | } | |
b32b8144 FG |
49 | |
50 | int state() const { | |
51 | return state_; | |
52 | } | |
53 | ||
54 | private: | |
55 | int state_; | |
7c673cae FG |
56 | }; |
57 | ||
b32b8144 FG |
58 | template<class T, class U> |
59 | inline bool | |
60 | operator==(const A<T>& a, const A<U>& b) | |
7c673cae | 61 | { |
b32b8144 | 62 | return a.state() == b.state(); |
7c673cae FG |
63 | } |
64 | ||
b32b8144 FG |
65 | template<class T, class U> |
66 | inline bool | |
67 | operator!=(const A<T>& a, const A<U>& b) | |
7c673cae FG |
68 | { |
69 | return !(a == b); | |
70 | } | |
71 | ||
72 | template<std::size_t Alignment> | |
73 | void test_allocate() | |
74 | { | |
75 | { | |
b32b8144 | 76 | boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5); |
7c673cae FG |
77 | int* p = a.allocate(1); |
78 | BOOST_TEST(p != 0); | |
79 | BOOST_TEST(boost::alignment::is_aligned(p, Alignment)); | |
80 | std::memset(p, 0, 1); | |
81 | a.deallocate(p, 1); | |
82 | } | |
83 | { | |
b32b8144 FG |
84 | boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5); |
85 | int* p = a.allocate(1); | |
86 | int* q = a.allocate(1, p); | |
87 | BOOST_TEST(q != 0); | |
88 | BOOST_TEST(boost::alignment::is_aligned(q, Alignment)); | |
89 | std::memset(q, 0, 1); | |
90 | a.deallocate(q, 1); | |
91 | a.deallocate(p, 1); | |
7c673cae FG |
92 | } |
93 | { | |
b32b8144 | 94 | boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5); |
7c673cae FG |
95 | int* p = a.allocate(0); |
96 | a.deallocate(p, 0); | |
97 | } | |
98 | } | |
99 | ||
100 | template<std::size_t Alignment> | |
101 | void test_construct() | |
102 | { | |
b32b8144 | 103 | boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5); |
7c673cae FG |
104 | int* p = a.allocate(1); |
105 | a.construct(p, 1); | |
106 | BOOST_TEST(*p == 1); | |
107 | a.destroy(p); | |
108 | a.deallocate(p, 1); | |
109 | } | |
110 | ||
111 | template<std::size_t Alignment> | |
112 | void test_constructor() | |
113 | { | |
114 | { | |
b32b8144 FG |
115 | boost::alignment::aligned_allocator_adaptor<A<char>, Alignment> a(5); |
116 | boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> b(a); | |
117 | BOOST_TEST(b == a); | |
7c673cae FG |
118 | } |
119 | { | |
b32b8144 FG |
120 | A<int> a(5); |
121 | boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> b(a); | |
122 | BOOST_TEST(b.base() == a); | |
7c673cae FG |
123 | } |
124 | } | |
125 | ||
126 | template<std::size_t Alignment> | |
127 | void test_rebind() | |
128 | { | |
b32b8144 | 129 | boost::alignment::aligned_allocator_adaptor<A<int>, Alignment> a(5); |
7c673cae | 130 | typename boost::alignment::aligned_allocator_adaptor<A<int>, |
b32b8144 FG |
131 | Alignment>::template rebind<int>::other b(a); |
132 | BOOST_TEST(b == a); | |
7c673cae FG |
133 | } |
134 | ||
135 | template<std::size_t Alignment> | |
136 | void test() | |
137 | { | |
138 | test_allocate<Alignment>(); | |
139 | test_construct<Alignment>(); | |
140 | test_constructor<Alignment>(); | |
141 | test_rebind<Alignment>(); | |
142 | } | |
143 | ||
144 | int main() | |
145 | { | |
146 | test<1>(); | |
147 | test<2>(); | |
148 | test<4>(); | |
149 | test<8>(); | |
150 | test<16>(); | |
151 | test<32>(); | |
152 | test<64>(); | |
153 | test<128>(); | |
154 | ||
155 | return boost::report_errors(); | |
156 | } |