From patchwork Fri Feb 3 20:24:47 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Kosnik X-Patchwork-Id: 139451 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]) by ozlabs.org (Postfix) with SMTP id 31770104792 for ; Sat, 4 Feb 2012 07:25:22 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1328905523; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: Mime-Version:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=smh4Pk1undbil4jHT4r5soIGSrQ=; b=SZF9gxnHuehvEmk IcAsvZVkT0QzFop7oLhAqew7k4miwD8ngHCtbgDG3jv/8dH68wzj36hJxBsa5Nab diRl0xTahZZjl9ja0GuQ2Tr3XWXNOcrkDYLmqp6mZsQFksy+bczoDJ0PD0SGNo+6 THZdtSUjBTnG117bInnZXbL57FY4= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References:Mime-Version:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=djD3U+y4KOkEs65dK39dAWboP27MXvTfF//DFU6bD2JpaZT5iwD2OrO2A6jLWq r9kGOJazzZJage+EubQvniQyP07fb3G5qZ70vF+H0G9eawLMPnAtlYDhWNyaMBI4 P/u1w5FwkjXXEhPrq7812kzbGCRD5xD4ERXLb1U/Fehj0=; Received: (qmail 1108 invoked by alias); 3 Feb 2012 20:25:12 -0000 Received: (qmail 1065 invoked by uid 22791); 3 Feb 2012 20:25:08 -0000 X-SWARE-Spam-Status: No, hits=-6.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 03 Feb 2012 20:24:49 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q13KOm0g031739 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 3 Feb 2012 15:24:49 -0500 Received: from shotwell (ovpn-113-78.phx2.redhat.com [10.3.113.78]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q13KOlDa004707 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Fri, 3 Feb 2012 15:24:48 -0500 Date: Fri, 3 Feb 2012 12:24:47 -0800 From: Benjamin Kosnik To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [v3] libstdc++/51811 Message-ID: <20120203122447.71b3a7cf@shotwell> In-Reply-To: <20120203200006.GK18768@tyan-ft48-01.lab.bos.redhat.com> References: <20120203115244.30bc43d8@shotwell> <20120203200006.GK18768@tyan-ft48-01.lab.bos.redhat.com> Mime-Version: 1.0 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 > Different patch attached? Indeed, sorry. Here's the right one. -benjamin 2012-02-03 Benjamin Kosnik PR libstdc++/51811 * include/bits/atomic_base.h (atomic<_Tp*>): Fix offsets. * testsuite/29_atomics/atomic/operators/51811.cc: New. * testsuite/29_atomics/atomic/operators/pointer_partial_void.cc: New. diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h index aa43bcc..9d5f4eb 100644 --- a/libstdc++-v3/include/bits/atomic_base.h +++ b/libstdc++-v3/include/bits/atomic_base.h @@ -1,6 +1,6 @@ // -*- C++ -*- header. -// Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc. +// Copyright (C) 2008, 2009, 2010, 2011, 2012 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 @@ -621,6 +621,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __pointer_type _M_p; + // Factored out to facilitate explicit specialization. + constexpr ptrdiff_t + _M_type_size(ptrdiff_t __d) { return __d * sizeof(_PTp); } + + constexpr ptrdiff_t + _M_type_size(ptrdiff_t __d) volatile { return __d * sizeof(_PTp); } + public: __atomic_base() noexcept = default; ~__atomic_base() noexcept = default; @@ -669,43 +676,51 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __pointer_type operator++() noexcept - { return __atomic_add_fetch(&_M_p, 1, memory_order_seq_cst); } + { return __atomic_add_fetch(&_M_p, _M_type_size(1), + memory_order_seq_cst); } __pointer_type operator++() volatile noexcept - { return __atomic_add_fetch(&_M_p, 1, memory_order_seq_cst); } + { return __atomic_add_fetch(&_M_p, _M_type_size(1), + memory_order_seq_cst); } __pointer_type operator--() noexcept - { return __atomic_sub_fetch(&_M_p, 1, memory_order_seq_cst); } + { return __atomic_sub_fetch(&_M_p, _M_type_size(1), + memory_order_seq_cst); } __pointer_type operator--() volatile noexcept - { return __atomic_sub_fetch(&_M_p, 1, memory_order_seq_cst); } + { return __atomic_sub_fetch(&_M_p, _M_type_size(1), + memory_order_seq_cst); } __pointer_type operator+=(ptrdiff_t __d) noexcept - { return __atomic_add_fetch(&_M_p, __d, memory_order_seq_cst); } + { return __atomic_add_fetch(&_M_p, _M_type_size(__d), + memory_order_seq_cst); } __pointer_type operator+=(ptrdiff_t __d) volatile noexcept - { return __atomic_add_fetch(&_M_p, __d, memory_order_seq_cst); } + { return __atomic_add_fetch(&_M_p, _M_type_size(__d), + memory_order_seq_cst); } __pointer_type operator-=(ptrdiff_t __d) noexcept - { return __atomic_sub_fetch(&_M_p, __d, memory_order_seq_cst); } + { return __atomic_sub_fetch(&_M_p, _M_type_size(__d), + memory_order_seq_cst); } __pointer_type operator-=(ptrdiff_t __d) volatile noexcept - { return __atomic_sub_fetch(&_M_p, __d, memory_order_seq_cst); } + { return __atomic_sub_fetch(&_M_p, _M_type_size(__d), + memory_order_seq_cst); } bool is_lock_free() const noexcept - { return __atomic_is_lock_free (sizeof (_M_p), &_M_p); } + { return __atomic_is_lock_free(_M_type_size(1), &_M_p); } bool is_lock_free() const volatile noexcept - { return __atomic_is_lock_free (sizeof (_M_p), &_M_p); } + { return __atomic_is_lock_free(_M_type_size(1), &_M_p); } void store(__pointer_type __p, @@ -789,22 +804,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __pointer_type fetch_add(ptrdiff_t __d, memory_order __m = memory_order_seq_cst) noexcept - { return __atomic_fetch_add(&_M_p, __d, __m); } + { return __atomic_fetch_add(&_M_p, _M_type_size(__d), __m); } __pointer_type fetch_add(ptrdiff_t __d, memory_order __m = memory_order_seq_cst) volatile noexcept - { return __atomic_fetch_add(&_M_p, __d, __m); } + { return __atomic_fetch_add(&_M_p, _M_type_size(__d), __m); } __pointer_type fetch_sub(ptrdiff_t __d, memory_order __m = memory_order_seq_cst) noexcept - { return __atomic_fetch_sub(&_M_p, __d, __m); } + { return __atomic_fetch_sub(&_M_p, _M_type_size(__d), __m); } __pointer_type fetch_sub(ptrdiff_t __d, memory_order __m = memory_order_seq_cst) volatile noexcept - { return __atomic_fetch_sub(&_M_p, __d, __m); } + { return __atomic_fetch_sub(&_M_p, _M_type_size(__d), __m); } }; // @} group atomics diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/operators/51811.cc b/libstdc++-v3/testsuite/29_atomics/atomic/operators/51811.cc new file mode 100644 index 0000000..7c234f2 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic/operators/51811.cc @@ -0,0 +1,90 @@ +// { dg-options "-std=gnu++0x" } + +// Copyright (C) 2012 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 +// . + +#include +#include //std::abs +#include + +// libstdc++/51811 +// pointer arithimetic vs. atomic<_Tp*> specialization +int main(void) +{ + bool test __attribute__((unused)) = true; + + using namespace std; + + typedef int value_type; + const size_t n = 2; + value_type value = 42; + atomic p, p2, p3; + + // operator++ + { + p = &value; + p2 = p++; + VERIFY (p != p2); + + value_type* vp(p); + value_type* vp2(p2); + ptrdiff_t dist = reinterpret_cast(vp) - reinterpret_cast(vp2); + VERIFY ( std::abs(dist) == sizeof(value_type)); + + p = &value; + p3 = ++p; + VERIFY (p == p3); + } + + // operator-- + { + p = &value; + p2 = p--; + VERIFY (p != p2); + + value_type* vp(p); + value_type* vp2(p2); + ptrdiff_t dist = reinterpret_cast(vp) - reinterpret_cast(vp2); + VERIFY ( std::abs(dist) == sizeof(value_type)); + + p = &value; + p3 = --p; + VERIFY (p == p3); + } + + // operator+= + { + p = &value; + value_type* vp(p); + p+=n; + value_type* vp2(p); + ptrdiff_t dist = reinterpret_cast(vp) - reinterpret_cast(vp2); + VERIFY ( std::abs(dist) == sizeof(value_type) * n); + } + + // operator-= + { + p = &value; + value_type* vp(p); + p-=n; + value_type* vp2(p); + ptrdiff_t dist = reinterpret_cast(vp) - reinterpret_cast(vp2); + VERIFY ( std::abs(dist) == sizeof(value_type) * n); + } + + return 0; +} diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc b/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc new file mode 100644 index 0000000..09a4d44 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic/operators/pointer_partial_void.cc @@ -0,0 +1,70 @@ +// { dg-options "-std=gnu++0x" } + +// Copyright (C) 2012 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 +// . + +#include +#include //std::abs +#include + +// pointer arithimetic vs. atomic. +// atomic vs. explicitly specialized w/o operators, like atomic_bool? +int main(void) +{ + // bool test __attribute__((unused)) = true; + + using namespace std; + + typedef int value_type; + const size_t n = 2; + value_type value = 42; + value_type* p = &value; + void* vp = p; + ptrdiff_t dist(0); + + atomic a(vp); + + // operator++ + void* vp2(a); + a++; + void* vp3(a); + dist = reinterpret_cast(vp2) - reinterpret_cast(vp3); + // VERIFY ( std::abs(dist) == sizeof(void*)); + + // operator-- + void* vp4(a); + a--; + void* vp5(a); + dist = reinterpret_cast(vp4) - reinterpret_cast(vp5); + // VERIFY ( std::abs(dist) == sizeof(void*)); + + // operator+= + void* vp6(a); + a+=n; + void* vp7(a); + dist = reinterpret_cast(vp6) - reinterpret_cast(vp7); + // VERIFY ( std::abs(dist) == sizeof(void*) * n); + + // operator-= + void* vp8(a); + a-=n; + void* vp9(a); + dist = reinterpret_cast(vp8) - reinterpret_cast(vp9); + //VERIFY ( std::abs(dist) == sizeof(void*) * n); + + return 0; +}