]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | (c) 2014 Glen Joseph Fernandes | |
3 | <glenjofe -at- gmail.com> | |
4 | ||
5 | Distributed under the Boost Software | |
6 | License, Version 1.0. | |
7 | http://boost.org/LICENSE_1_0.txt | |
8 | */ | |
9 | #include <boost/align/aligned_alloc.hpp> | |
10 | #include <boost/align/aligned_delete.hpp> | |
11 | #include <boost/align/alignment_of.hpp> | |
12 | #include <boost/core/lightweight_test.hpp> | |
13 | #include <new> | |
14 | ||
15 | template<class T> | |
16 | class type { | |
17 | public: | |
18 | static int count; | |
19 | type() | |
20 | : value() { | |
21 | count++; | |
22 | } | |
23 | ~type() { | |
24 | count--; | |
25 | } | |
26 | private: | |
27 | T value; | |
28 | }; | |
29 | ||
30 | template<class T> | |
31 | int type<T>::count = 0; | |
32 | ||
33 | template<class T> | |
34 | T* aligned_new() | |
35 | { | |
36 | void* p = boost::alignment::aligned_alloc(boost:: | |
37 | alignment::alignment_of<T>::value, sizeof(T)); | |
38 | if (p) { | |
39 | return ::new(p) T(); | |
40 | } else { | |
41 | return 0; | |
42 | } | |
43 | } | |
44 | ||
45 | template<class T> | |
46 | void test() | |
47 | { | |
48 | type<T>* p = aligned_new<type<T> >(); | |
49 | BOOST_TEST(type<T>::count == 1); | |
50 | boost::alignment::aligned_delete()(p); | |
51 | BOOST_TEST(type<T>::count == 0); | |
52 | } | |
53 | ||
54 | class C { }; | |
55 | union U { }; | |
56 | ||
57 | int main() | |
58 | { | |
59 | test<char>(); | |
60 | test<bool>(); | |
61 | test<short>(); | |
62 | test<int>(); | |
63 | test<long>(); | |
64 | test<float>(); | |
65 | test<double>(); | |
66 | test<long double>(); | |
67 | test<void*>(); | |
68 | test<void(*)()>(); | |
69 | test<C>(); | |
70 | test<int C::*>(); | |
71 | test<int (C::*)()>(); | |
72 | test<U>(); | |
73 | ||
74 | return boost::report_errors(); | |
75 | } |