C++11 [ptr.align]. This should probably not be inline. But for now this avoids any ABI changes.
* libstdc++-v3/testsuite/20_util/align/1.cc: New file. * libstdc++-v3/include/std/memory (align): New function. --- libstdc++-v3/include/std/memory | 35 +++++++++++++ libstdc++-v3/testsuite/20_util/align/1.cc | 82 +++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 libstdc++-v3/testsuite/20_util/align/1.cc diff --git a/libstdc++-v3/include/std/memory b/libstdc++-v3/include/std/memory index dafec0c..f9ae7b9 100644 --- a/libstdc++-v3/include/std/memory +++ b/libstdc++-v3/include/std/memory @@ -83,6 +83,41 @@ # if _GLIBCXX_USE_DEPRECATED # include <backward/auto_ptr.h> # endif + +/** + * @brief Fit aligned storage in buffer. + * + * [ptr.align] + * + * This function tries to fit __size storage with __alignment into + * the buffer __ptr of size __space bytes. If such a buffer fits + * then __ptr is changed to point to the storage and __space is + * reduced by the bytes needed to adjust the alignment. + * + * @param __alignment A fundamental or extended alignment value + of the desired storage. + * @param __size Size of the aligned storage. + * @param __ptr Pointer to a buffer of __space byte size. + * @param __space Size of the buffer pointed to by __ptr. + * @return the updated pointer if the aligned storage fits or + nullptr else. + */ +inline +void* +align(size_t __alignment, size_t __size, void*& __ptr, size_t& __space) +{ + const size_t __diff = __alignment - + reinterpret_cast<uintptr_t>(__ptr) % __alignment; + if (__diff + __size >= __space) + return nullptr; + else + { + __space -= __diff; + __ptr = static_cast<char*>(__ptr) + __diff; + return __ptr; + } +} + #else # include <backward/auto_ptr.h> #endif diff --git a/libstdc++-v3/testsuite/20_util/align/1.cc b/libstdc++-v3/testsuite/20_util/align/1.cc new file mode 100644 index 0000000..2e74806 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/align/1.cc @@ -0,0 +1,82 @@ +// { dg-options " -std=gnu++11 " } + +// 2014-04-16 Rüdiger Sonderfeld <ruedi...@c-plusplus.de> + +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software +// Foundation; either version 3, or (at your option) any later +// version. + +// This library is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// C++11 [ptr.align] (20.6.5): std::align + +#include <memory> +#include <cstdint> +#include <testsuite_hooks.h> + +#ifndef _GLIBCXX_ASSERT +# include <iostream> +# define STRINGIZE2(x) #x +# define STRINGIZE(x) STRINGIZE2(x) +# define CHECK(a, op, b) \ + do \ + { \ + if ( !( (a) op (b) ) ) \ + { \ + cerr << "Mismatch: " \ + << STRINGIZE(a) \ + << " (" << (a) << ") " \ + << STRINGIZE(op) << ' ' \ + << STRINGIZE(b) \ + << " (" << (b) << ")\n"; \ + } \ + } while(false) +#else +# define CHECK(a, op, b) VERIFY( (a) op (b) ) +#endif + +void test01() +{ + using namespace std; + bool test __attribute__((unused)) = true; + + size_t space = 100; + void* ptr = new char[space]; + char* const orig_ptr = static_cast<char*>(ptr); + char* old_ptr = orig_ptr; + const size_t orig_space = space; + size_t old_space = space; + const size_t alignment = 16; + const size_t size = 10; + while( void* const r = align(alignment, size, ptr, space) ) + { + CHECK( r, ==, ptr ); + uintptr_t p = reinterpret_cast<uintptr_t>(ptr); + CHECK( p % alignment, ==, 0 ); + char* const x = static_cast<char*>(ptr); + CHECK( x - old_ptr, ==, old_space - space ); + CHECK( (void*)x, <, (void*)(orig_ptr + orig_space) ); + CHECK( (void*)(x + size), <, (void*)(orig_ptr + orig_space) ); + ptr = x + size; + old_ptr = x; + old_space = space; + space -= size; + } + delete [] orig_ptr; +} + +int main() +{ + test01(); +} -- 1.9.2