From patchwork Mon Oct 13 10:03:51 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 399110 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id EC107140095 for ; Mon, 13 Oct 2014 21:04:06 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:content-transfer-encoding:in-reply-to; q=dns; s= default; b=AYH0FsdGo/u7KcEjmKqiYUWJJrBXCICN9t5/8VO/++YDk0q5q0avv kCuij+uqbasBrKKkwJhiv/8RrtMT3uGtMFjyTI+3jjaiAITaoECow+fO/mLqPG6H QA9N+/3P+qNv+tNJ98nsIEezn1zdKVbtl/cPyFIY9I5VGPM2Hk/5Eg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:content-transfer-encoding:in-reply-to; s=default; bh=OCwdiyZJ50XFJpt1CsJQmSkTOPY=; b=gvZt54KZ0m9lyCxUQ4Ew0mO6TJeG Np9tlh5LjVkBA6zdRy/Hjhh1M4tWiw8LZT2OeqQH4ABl4ZyX68rLi5bN24CyUlsy Q3tmjvrgIzFEmy8hRVnZN+iep8ms4d6qmZOWCy2Sqq6yHtHCU2sV1dQdukLHxJAN 0INkQ8JlD59X37g= Received: (qmail 29215 invoked by alias); 13 Oct 2014 10:04:00 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 29175 invoked by uid 89); 13 Oct 2014 10:03:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 13 Oct 2014 10:03:57 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s9DA3r8l003941 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 13 Oct 2014 06:03:53 -0400 Received: from localhost (ovpn-116-121.ams2.redhat.com [10.36.116.121]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s9DA3qQ1003591; Mon, 13 Oct 2014 06:03:53 -0400 Date: Mon, 13 Oct 2014 11:03:51 +0100 From: Jonathan Wakely To: =?iso-8859-1?Q?R=FCdiger?= Sonderfeld Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [PATCH 1/2] libstdc++: Add std::align. Message-ID: <20141013100351.GI4197@redhat.com> References: <1962350.FSSUUJhQhP@descartes> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1962350.FSSUUJhQhP@descartes> User-Agent: Mutt/1.5.23 (2014-03-12) On 16/04/14 17:06 +0200, RĂ¼diger Sonderfeld wrote: >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. Fixed (the new function should be in namespace std!) and applied to trunk. Thanks for the patch. Tested x86_64-linux. commit d49cd05620599277845f89325ac8a43622650e8f Author: Jonathan Wakely Date: Mon Oct 13 10:50:16 2014 +0100 2014-10-13 R??diger Sonderfeld * include/std/memory (align): Define. * testsuite/20_util/align/1.cc: New. 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 #endif +#if __cplusplus >= 201103L +# include +# 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(__ptr) % __align; + if (__diff + __size >= __space) + return nullptr; + else + { + __space -= __diff; + __ptr = static_cast(__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 + +// 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 +// . + +// C++11 [ptr.align] (20.6.5): std::align + +#include +#include +#include + +void +test01() +{ + bool test __attribute__((unused)) = true; + + size_t space = 100; + void* ptr = new char[space]; + char* const orig_ptr = static_cast(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(ptr); + VERIFY( p % alignment == 0 ); + char* const x = static_cast(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(); +}