aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRüdiger Sonderfeld <ruediger@c-plusplus.de>2014-10-13 10:05:21 +0000
committerJonathan Wakely <redi@gcc.gnu.org>2014-10-13 11:05:21 +0100
commit2f6ca9d31799133f80e446bd1faad5d9a67bac00 (patch)
tree21a4df0944b6d76df0f6ecb5665980ec2311a0e7
parent2a5273835024d375fecd273fd066becab39d82af (diff)
downloadgcc-2f6ca9d31799133f80e446bd1faad5d9a67bac00.zip
gcc-2f6ca9d31799133f80e446bd1faad5d9a67bac00.tar.gz
gcc-2f6ca9d31799133f80e446bd1faad5d9a67bac00.tar.bz2
memory (align): Define.
2014-10-13 Rüdiger Sonderfeld <ruediger@c-plusplus.de> * include/std/memory (align): Define. * testsuite/20_util/align/1.cc: New. From-SVN: r216143
-rw-r--r--libstdc++-v3/ChangeLog5
-rw-r--r--libstdc++-v3/include/std/memory42
-rw-r--r--libstdc++-v3/testsuite/20_util/align/1.cc61
3 files changed, 108 insertions, 0 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 508c9d3..37f29f7 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,8 @@
+2014-10-13 Rüdiger Sonderfeld <ruediger@c-plusplus.de>
+
+ * include/std/memory (align): Define.
+ * testsuite/20_util/align/1.cc: New.
+
2014-10-13 Marc Glisse <marc.glisse@inria.fr>
PR libstdc++/61347
diff --git a/libstdc++-v3/include/std/memory b/libstdc++-v3/include/std/memory
index dafec0c..affc8b1 100644
--- a/libstdc++-v3/include/std/memory
+++ b/libstdc++-v3/include/std/memory
@@ -87,4 +87,46 @@
# include <backward/auto_ptr.h>
#endif
+#if __cplusplus >= 201103L
+# include <cstdint>
+# ifdef _GLIBCXX_USE_C99_STDINT_TR1
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+/**
+ * @brief Fit aligned storage in buffer.
+ *
+ * [ptr.align]
+ *
+ * This function tries to fit @a __size bytes of storage with alignment
+ * @a __align into the buffer @a __ptr of size @a __space bytes. If such
+ * a buffer fits then @a __ptr is changed to point to the first byte of the
+ * aligned storage and @a __space is reduced by the bytes used for alignment.
+ *
+ * @param __align A fundamental or extended alignment value.
+ * @param __size Size of the aligned storage required.
+ * @param __ptr Pointer to a buffer of @a __space bytes.
+ * @param __space Size of the buffer pointed to by @a __ptr.
+ * @return the updated pointer if the aligned storage fits, otherwise nullptr.
+ */
+inline void*
+align(size_t __align, size_t __size, void*& __ptr, size_t& __space) noexcept
+{
+ const size_t __diff = __align - reinterpret_cast<uintptr_t>(__ptr) % __align;
+ if (__diff + __size >= __space)
+ return nullptr;
+ else
+ {
+ __space -= __diff;
+ __ptr = static_cast<char*>(__ptr) + __diff;
+ return __ptr;
+ }
+}
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
+#endif // _GLIBCXX_USE_C99_STDINT_TR1
+#endif // C++11
+
#endif /* _GLIBCXX_MEMORY */
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..d1f94e9
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/align/1.cc
@@ -0,0 +1,61 @@
+// { dg-options " -std=gnu++11 " }
+
+// 2014-04-16 Rüdiger Sonderfeld <ruediger@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>
+
+void
+test01()
+{
+ 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 = std::align(alignment, size, ptr, space) )
+ {
+ VERIFY( r == ptr );
+ uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
+ VERIFY( p % alignment == 0 );
+ char* const x = static_cast<char*>(ptr);
+ VERIFY( x - old_ptr == old_space - space );
+ VERIFY( (void*)x < (void*)(orig_ptr + orig_space) );
+ VERIFY( (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();
+}