From 5f1d4eae4f4984a8ac0b2501df7d2e062610a1c1 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sun, 16 Feb 2014 11:02:37 -0800 Subject: [PATCH] Factor out alignment code into sp_align --- .../smart_ptr/detail/array_allocator.hpp | 29 +++++++-------- include/boost/smart_ptr/detail/sp_align.hpp | 37 +++++++++++++++++++ 2 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 include/boost/smart_ptr/detail/sp_align.hpp diff --git a/include/boost/smart_ptr/detail/array_allocator.hpp b/include/boost/smart_ptr/detail/array_allocator.hpp index 2a60528..8f1e632 100644 --- a/include/boost/smart_ptr/detail/array_allocator.hpp +++ b/include/boost/smart_ptr/detail/array_allocator.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -139,19 +140,17 @@ namespace boost { enum { M = boost::alignment_of::value }; - std::size_t n1 = count * sizeof(value_type) + M - 1; - std::size_t n2 = size * sizeof(type); + std::size_t n1 = count * sizeof(value_type); + std::size_t n2 = size * sizeof(type) + M - 1; CA ca(*this); #if !defined(BOOST_NO_CXX11_ALLOCATOR) void* p1 = CT::allocate(ca, n1 + n2, value); #else void* p1 = ca.allocate(n1 + n2, value); #endif - char* p2 = static_cast(p1) + n1; - while (std::size_t(p2) % M != 0) { - p2--; - } - *data.result = reinterpret_cast(p2); + void* p2 = static_cast(p1) + n1; + p2 = sp_align(M, p2); + *data.result = static_cast(p2); return static_cast(p1); } @@ -159,8 +158,8 @@ namespace boost { enum { M = boost::alignment_of::value }; - std::size_t n1 = count * sizeof(value_type) + M - 1; - std::size_t n2 = size * sizeof(type); + std::size_t n1 = count * sizeof(value_type); + std::size_t n2 = size * sizeof(type) + M - 1; char* p1 = reinterpret_cast(memory); CA ca(*this); #if !defined(BOOST_NO_CXX11_ALLOCATOR) @@ -315,14 +314,12 @@ namespace boost { enum { M = boost::alignment_of::value }; - std::size_t n1 = count * sizeof(value_type) + M - 1; - std::size_t n2 = size * sizeof(type); + std::size_t n1 = count * sizeof(value_type); + std::size_t n2 = size * sizeof(type) + M - 1; void* p1 = ::operator new(n1 + n2); - char* p2 = static_cast(p1) + n1; - while (std::size_t(p2) % M != 0) { - p2--; - } - *data.result = reinterpret_cast(p2); + void* p2 = static_cast(p1) + n1; + p2 = sp_align(M, p2); + *data.result = static_cast(p2); return static_cast(p1); } diff --git a/include/boost/smart_ptr/detail/sp_align.hpp b/include/boost/smart_ptr/detail/sp_align.hpp new file mode 100644 index 0000000..b78bdc0 --- /dev/null +++ b/include/boost/smart_ptr/detail/sp_align.hpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2014 Glen Joseph Fernandes + * glenfe at live dot com + * + * Distributed under the Boost Software License, + * Version 1.0. (See accompanying file LICENSE_1_0.txt + * or copy at http://boost.org/LICENSE_1_0.txt) + */ +#ifndef BOOST_SMART_PTR_DETAIL_SP_ALIGN_HPP +#define BOOST_SMART_PTR_DETAIL_SP_ALIGN_HPP + +#include +#if !defined(BOOST_NO_CXX11_STD_ALIGN) +#include +#endif + +namespace boost { + namespace detail { +#if !defined(BOOST_NO_CXX11_STD_ALIGN) + inline void* sp_align(std::size_t alignment, void* ptr) { + std::size_t n1 = alignment - 1; + std::align(alignment, 0, ptr, n1); + return ptr; + } +#else + inline void* sp_align(std::size_t alignment, void* ptr) { + std::size_t n1 = (std::size_t)ptr % alignment; + if (n1 != 0) { + ptr = (char*)ptr + alignment - n1; + } + return ptr; + } +#endif + } +} + +#endif