Skip to content

Commit c0b119a

Browse files
committed
Initial alignment_of preview
1 parent 35932df commit c0b119a

18 files changed

+306
-92
lines changed

doc/Jamfile.v2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import quickbook ;
1010

1111
doxygen reference
1212
:
13-
[ glob ../../../boost/align.hpp ]
1413
[ glob ../../../boost/align/*.hpp ]
1514
:
1615
<doxygen:param>ENABLE_PREPROCESSING=YES

doc/align.qbk

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ It provides a deleter, class `aligned_delete`, which
4242
makes use of `aligned_free`. It is suitable for use
4343
with constructed objects allocated with `aligned_alloc`.
4444

45+
It provides class template `alignment_of` for
46+
implementations which do not have the C++11 standard
47+
library `std::alignment_of` class template available.
48+
4549
It also provides function `is_aligned` to test the
4650
alignment of a pointer.
4751

@@ -75,6 +79,11 @@ The `aligned_delete` class can be used in place of the
7579
default deleter, `std::default_delete`, to destroy and
7680
free any objects allocated with `aligned_alloc`.
7781

82+
The `alignment_of` class template can be used to
83+
determine alignment of a type and is provided for
84+
standard library implementations which do not yet provide
85+
`std::alignment_of`.
86+
7887
The `is_aligned` function can be used for verification
7988
or to branch to a faster code path which requires
8089
that objects have a specified extended alignment.
@@ -279,6 +288,27 @@ std::unique_ptr<T,
279288

280289
[endsect]
281290

291+
[section Using alignment_of]
292+
293+
We want to assert, at compile time, that the alignment
294+
of a type, `T`, is at least as large as the alignment of
295+
a second type, `U`.
296+
297+
``
298+
#include <boost/align/alignment_of.hpp>
299+
``
300+
301+
Use the `alignment_of` class template to determine the
302+
alignment of the type.
303+
304+
``
305+
static_assert(boost::alignment::alignment_of<T>::
306+
value >= boost::alignment::alignment_of<U>::
307+
value, "");
308+
``
309+
310+
[endsect]
311+
282312
[section Using is_aligned]
283313

284314
We want to assert that the alignment of a pointer, `p`,

example/aligned_ptr.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
allocated with `aligned_alloc`.
2020
*/
2121

22-
#include <boost/align.hpp>
22+
#include <boost/align/aligned_delete.hpp>
2323
#include <memory>
2424

2525
template<class T>
2626
using aligned_ptr = std::unique_ptr<T,
27-
boost::aligned_delete>;
27+
boost::alignment::aligned_delete>;
2828
//]
2929

3030
#endif

example/aligned_vector.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
to specify an minimum extended alignment
2020
when used with any type.
2121
*/
22-
#include <boost/align.hpp>
22+
#include <boost/align/aligned_allocator.hpp>
2323
#include <vector>
2424

2525
template<class T, std::size_t Alignment = 1>
2626
using aligned_vector = std::vector<T,
27-
boost::aligned_allocator<T, Alignment> >;
27+
boost::alignment::aligned_allocator<T, Alignment> >;
2828
//]
2929

3030
#endif

example/make_aligned.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ template<class T, class... Args>
2626
inline aligned_ptr<T>
2727
make_aligned(Args&&... args)
2828
{
29-
auto p = boost::aligned_alloc(alignof(T),
29+
auto p = boost::alignment::aligned_alloc(alignof(T),
3030
sizeof(T));
3131
if (!p) {
3232
throw std::bad_alloc();
3333
}
3434
try {
35-
auto q = ::new(p)
36-
T(std::forward<Args>(args)...);
35+
auto q = ::new(p) T(std::forward<Args>(args)...);
3736
return aligned_ptr<T>(q);
3837
} catch (...) {
39-
boost::aligned_free(p);
38+
boost::alignment::aligned_free(p);
4039
throw;
4140
}
4241
}

include/boost/align.hpp

Lines changed: 0 additions & 63 deletions
This file was deleted.

include/boost/align/aligned_allocator.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include <boost/throw_exception.hpp>
2222
#include <boost/align/aligned_alloc.hpp>
2323
#include <boost/align/aligned_allocator_forward.hpp>
24+
#include <boost/align/alignment_of.hpp>
2425
#include <boost/align/detail/addressof.hpp>
25-
#include <boost/align/detail/alignment_of.hpp>
2626
#include <boost/align/detail/is_alignment_const.hpp>
2727
#include <boost/align/detail/max_align.hpp>
2828
#include <boost/align/detail/max_count_of.hpp>
@@ -87,8 +87,7 @@ namespace boost {
8787

8888
private:
8989
enum {
90-
TypeAlign = detail::
91-
alignment_of<value_type>::value,
90+
TypeAlign = alignment_of<value_type>::value,
9291

9392
MaxAlign = detail::
9493
max_align<Alignment, TypeAlign>::value

include/boost/align/aligned_allocator_adaptor.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include <boost/static_assert.hpp>
2121
#include <boost/align/align.hpp>
2222
#include <boost/align/aligned_allocator_adaptor_forward.hpp>
23+
#include <boost/align/alignment_of.hpp>
2324
#include <boost/align/detail/addressof.hpp>
24-
#include <boost/align/detail/alignment_of.hpp>
2525
#include <boost/align/detail/is_alignment_const.hpp>
2626
#include <boost/align/detail/max_align.hpp>
2727
#include <new>
@@ -104,11 +104,9 @@ namespace boost {
104104

105105
private:
106106
enum {
107-
TypeAlign = detail::
108-
alignment_of<value_type>::value,
107+
TypeAlign = alignment_of<value_type>::value,
109108

110-
PtrAlign = detail::
111-
alignment_of<CharPtr>::value,
109+
PtrAlign = alignment_of<CharPtr>::value,
112110

113111
BlockAlign = detail::
114112
max_align<PtrAlign, TypeAlign>::value,

include/boost/align/alignment_of.hpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright (c) 2014 Glen Joseph Fernandes
3+
glenfe at live dot com
4+
5+
Distributed under the Boost Software License,
6+
Version 1.0. (See accompanying file LICENSE_1_0.txt
7+
or copy at http://boost.org/LICENSE_1_0.txt)
8+
*/
9+
#ifndef BOOST_ALIGN_ALIGNMENT_OF_HPP
10+
#define BOOST_ALIGN_ALIGNMENT_OF_HPP
11+
12+
/**
13+
Class template alignment_of.
14+
15+
@file
16+
@author Glen Fernandes
17+
*/
18+
19+
#include <boost/config.hpp>
20+
21+
/**
22+
@cond
23+
*/
24+
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
25+
#include <boost/align/detail/alignment_of_cxx11.hpp>
26+
#elif BOOST_MSVC >= 1400
27+
#include <boost/align/detail/alignment_of_msvc.hpp>
28+
#elif defined(BOOST_CLANG)
29+
#include <boost/align/detail/alignment_of_clang.hpp>
30+
#elif (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR >= 3))
31+
#include <boost/align/detail/alignment_of_gcc.hpp>
32+
#elif defined(__ghs__) && (__GHS_VERSION_NUMBER >= 600)
33+
#include <boost/align/detail/alignment_of_gcc.hpp>
34+
#elif defined(_CODEGEARC__)
35+
#include <boost/align/detail/alignment_of_codegear.hpp>
36+
#else
37+
#include <boost/align/detail/alignment_of.hpp>
38+
#endif
39+
/**
40+
@endcond
41+
*/
42+
43+
/**
44+
Boost namespace.
45+
*/
46+
namespace boost {
47+
/**
48+
Alignment namespace.
49+
*/
50+
namespace alignment {
51+
/**
52+
Class template alignment_of.
53+
54+
@remark Value: `alignof(T)`.
55+
*/
56+
template<class T>
57+
struct alignment_of;
58+
}
59+
}
60+
61+
#endif

include/boost/align/detail/alignment_of.hpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@
99
#ifndef BOOST_ALIGN_DETAIL_ALIGNMENT_OF_HPP
1010
#define BOOST_ALIGN_DETAIL_ALIGNMENT_OF_HPP
1111

12-
#include <boost/config.hpp>
13-
14-
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
15-
#include <type_traits>
16-
#else
17-
#include <boost/type_traits/alignment_of.hpp>
18-
#endif
12+
#include <boost/align/detail/min_size.hpp>
1913

2014
namespace boost {
2115
namespace alignment {
2216
namespace detail {
23-
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
24-
using std::alignment_of;
25-
#else
26-
using boost::alignment_of;
27-
#endif
17+
template<class T>
18+
struct padded {
19+
char unit;
20+
T object;
21+
};
2822
}
23+
24+
template<class T>
25+
struct alignment_of {
26+
enum {
27+
value = detail::min_size<sizeof(T),
28+
sizeof(detail::padded<T>) - sizeof(T)>::value
29+
};
30+
};
2931
}
3032
}
3133

0 commit comments

Comments
 (0)