From patchwork Wed Apr 16 15:44:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=BCdiger_Sonderfeld?= X-Patchwork-Id: 339625 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 C3DED140099 for ; Thu, 17 Apr 2014 01:44:54 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; q=dns; s= default; b=wVHyXKQ24sHtZqbLoMEDFgCYuze7WIvBqnU/3DrLamqhFL6fxDEI9 xvg3pOiiemGD/mh4qG4eBVdCKjG/g49ZJv9gSVc95cwuR/1YOTaivOpJcuarud61 2Ae4/6ez2RRCELqfDRLF099fSkNOAoQWuE+gu7FtklamJzNdE9dwJ8= 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:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=dnhyjytba5NGupgYvxx5ZWQ15LU=; b=W2hdd3FPI+jUPtBR9rWyzmVKFRvn oKxhSMPVW12mdpixItqFRVoTXgERhOboxxULA+o0oXdrfbTtJZQRsg5nqI5bfYEn HfCDgGA7KW1hBxAKIdUtXsT9nMCsMTY/cITbSIZ2RWio2fOHo87T/j/AwpCLgrXY RNVE2CjFYYztEoM= Received: (qmail 13800 invoked by alias); 16 Apr 2014 15:44:46 -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 13773 invoked by uid 89); 16 Apr 2014 15:44:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: ptmx.org Received: from ptmx.org (HELO ptmx.org) (178.63.28.110) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Wed, 16 Apr 2014 15:44:39 +0000 Received: from localhost (localhost [127.0.0.1]) by ptmx.org (Postfix) with ESMTP id A87A029DBD; Wed, 16 Apr 2014 17:44:35 +0200 (CEST) Received: from ptmx.org ([127.0.0.1]) by localhost (ptmx.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qPNnkQ172968; Wed, 16 Apr 2014 17:44:33 +0200 (CEST) Received: from descartes.localnet (chello080108246092.7.14.vie.surfer.at [80.108.246.92]) by ptmx.org (Postfix) with ESMTPSA id C00AD1FF84; Wed, 16 Apr 2014 17:44:32 +0200 (CEST) From: =?ISO-8859-1?Q?R=FCdiger?= Sonderfeld To: Jonathan Wakely Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCHv2 2/2] libstdc++: Add std::aligned_union. Date: Wed, 16 Apr 2014 17:44:20 +0200 Message-ID: <2914265.gtnDFNoboG@descartes> User-Agent: KMail/4.11.5 (Linux/3.11.0-18-generic; KDE/4.11.5; x86_64; ; ) In-Reply-To: <20140416152909.GA6807@redhat.com> References: <20140416152909.GA6807@redhat.com> MIME-Version: 1.0 > Thanks! I was hoping to implement it the straightforward way, but was > thwarted by http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59012 There are certainly nicer ways to implement it. At least in C++14 there should be a usable constexpr std::max instead of the verbose ?: usage. Maybe sizeof/alignof could be used with the parameter pack expansion. I fixed the other issues below. Regards, RĂ¼diger -- 8< ----------------------------------------------------------- >8 -- C++11: [meta.trans.other] * libstdc++-v3/testsuite/20_util/aligned_union/1.cc: New file. * libstdc++-v3/include/std/type_traits (__strictest_alignment): New helper struct. (aligned_union): New struct (C++11). (aligned_union_t): New type alias (C++14). --- libstdc++-v3/include/std/type_traits | 43 ++++++++++++++ libstdc++-v3/testsuite/20_util/aligned_union/1.cc | 72 +++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 libstdc++-v3/testsuite/20_util/aligned_union/1.cc diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 4b434a6..4441290 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -1837,6 +1837,46 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION }; }; + template + struct __strictest_alignment + { + static const size_t _S_alignment = 0; + static const size_t _S_size = 0; + }; + + template + struct __strictest_alignment<_T, _Types...> + { + static const size_t _S_alignment = + alignof(_T) > __strictest_alignment<_Types...>::_S_alignment ? + alignof(_T) : __strictest_alignment<_Types...>::_S_alignment; + static const size_t _S_size = + sizeof(_T) > __strictest_alignment<_Types...>::_S_size ? + sizeof(_T) : __strictest_alignment<_Types...>::_S_size; + }; + + /** + * @brief Provide aligned storage for types. + * + * [meta.trans.other] + * + * Provides aligned storage for any of the provided types of at + * least size _Len. + * + * @see aligned_storage + */ + template + struct aligned_union + { + /// The value of the strictest alignment of _Types. + static const size_t alignment_value = + __strictest_alignment<_Types...>::_M_alignment; + static const size_t _S_len = + _Len > __strictest_alignment<_Types...>::_S_size ? + _Len : __strictest_alignment<_Types...>::_S_size; + /// The storage. + typedef typename aligned_storage<_S_len, alignment_value>::type type; + }; // Decay trait for arrays and functions, used for perfect forwarding // in make_pair, make_tuple, etc. @@ -2173,6 +2213,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __alignof__(typename __aligned_storage_msa<_Len>::__type)> using aligned_storage_t = typename aligned_storage<_Len, _Align>::type; + template + using aligned_union_t = typename aligned_union<_Len, _Types...>::type; + /// Alias template for decay template using decay_t = typename decay<_Tp>::type; diff --git a/libstdc++-v3/testsuite/20_util/aligned_union/1.cc b/libstdc++-v3/testsuite/20_util/aligned_union/1.cc new file mode 100644 index 0000000..5285bb0 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/aligned_union/1.cc @@ -0,0 +1,72 @@ +// { dg-options " -std=gnu++11 " } +// { dg-do compile } + +// 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 [meta.trans.other] 20.9.7.6: aligned_union + +#include +#include + +struct MSAlignType { } __attribute__((__aligned__)); + +template + struct mymax + { + static const std::size_t alignment = 0; + static const std::size_t size = 0; + }; + +template + struct mymax + { + static const std::size_t alignment = alignof(L) > mymax::alignment + ? alignof(L) : mymax::alignment; + static const std::size_t size = sizeof(L) > mymax::size + ? sizeof(L) : mymax::size; + }; + +void test01() +{ + using std::aligned_union; + using std::alignment_of; + using std::size_t; + using namespace __gnu_test; + + const size_t max_a = mymax::alignment; + const size_t max_s = mymax::size; + + typedef aligned_union<0, char, short, int, double, int[4], + ClassType, MSAlignType> au_type; + static_assert(au_type::alignment_value == max_a, "Alignment value"); + static_assert(sizeof(au_type::type) >= max_s, "Storage size"); + + typedef aligned_union au_type2; + static_assert(sizeof(au_type2::type) >= max_s+100, + "Storage size (at least len)"); +} -- 1.9.2