From patchwork Wed Nov 9 21:14:58 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 124702 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 BCD1DB6F76 for ; Thu, 10 Nov 2011 08:15:20 +1100 (EST) Received: (qmail 5755 invoked by alias); 9 Nov 2011 21:15:16 -0000 Received: (qmail 5736 invoked by uid 22791); 9 Nov 2011 21:15:14 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SARE_SUB_OBFU_Q1 X-Spam-Check-By: sourceware.org Received: from mail-ww0-f51.google.com (HELO mail-ww0-f51.google.com) (74.125.82.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 09 Nov 2011 21:15:00 +0000 Received: by wwf27 with SMTP id 27so2723884wwf.8 for ; Wed, 09 Nov 2011 13:14:58 -0800 (PST) MIME-Version: 1.0 Received: by 10.216.132.94 with SMTP id n72mr762778wei.51.1320873298387; Wed, 09 Nov 2011 13:14:58 -0800 (PST) Received: by 10.216.63.83 with HTTP; Wed, 9 Nov 2011 13:14:58 -0800 (PST) In-Reply-To: <4EBA5394.8090304@oracle.com> References: <4EB9DB03.6080801@oracle.com> <4EBA5394.8090304@oracle.com> Date: Wed, 9 Nov 2011 21:14:58 +0000 Message-ID: Subject: Re: [v3] C++11 allocator reqs for vector in debug & profile mode From: Jonathan Wakely To: Paolo Carlini Cc: "libstdc++" , gcc-patches 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 On 9 November 2011 10:19, Paolo Carlini wrote: > Hi >> >> I checked, and it's currently broken :) >> >> We do the wrong thing for allocators with >> propagate_on_container_swap==true. >> >> I think the fix might be as simple as constructing the new container >> with a copy of the old one's allocator, so even if it gets swapped >> it's equal. Like so: * include/bits/allocator.h (__shrink_to_fit_aux::_S_do_it): Create the new object with the same allocator. * testsuite/23_containers/vector/capacity/shrink_to_fit2.cc: New. Tested x86_64-linux, committed to trunk. Thanks for reminding me about it. >> I'll deal with it asap. > > Thanks a lot! > > Paolo. > Index: include/bits/allocator.h =================================================================== --- include/bits/allocator.h (revision 181234) +++ include/bits/allocator.h (revision 181235) @@ -198,7 +198,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __try { _Tp(__make_move_if_noexcept_iterator(__c.begin()), - __make_move_if_noexcept_iterator(__c.end())).swap(__c); + __make_move_if_noexcept_iterator(__c.end()), + __c.get_allocator()).swap(__c); return true; } __catch(...) Index: testsuite/23_containers/vector/capacity/shrink_to_fit2.cc =================================================================== --- testsuite/23_containers/vector/capacity/shrink_to_fit2.cc (revision 0) +++ testsuite/23_containers/vector/capacity/shrink_to_fit2.cc (revision 181235) @@ -0,0 +1,61 @@ +// { dg-options "-std=gnu++0x" } + +// Copyright (C) 2011 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 Pred 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 +#include + +using __gnu_test::propagating_allocator; + +void test01() +{ + bool test __attribute__((unused)) = true; + + typedef propagating_allocator alloc_type; + alloc_type alloc(5); + + std::vector v(10u, 1, alloc); + v.reserve(100); + VERIFY( v.size() < v.capacity() ); + v.shrink_to_fit(); + VERIFY( v.size() == v.capacity() ); + VERIFY( v.get_allocator().get_personality() == alloc.get_personality() ); +} + +void test02() +{ + bool test __attribute__((unused)) = true; + + typedef propagating_allocator alloc_type; + alloc_type alloc(5); + + std::vector v(10u, 1, alloc); + v.reserve(100); + VERIFY( v.size() < v.capacity() ); + v.shrink_to_fit(); + VERIFY( v.size() == v.capacity() ); + VERIFY( v.get_allocator().get_personality() == alloc.get_personality() ); +} + +int main() +{ + test01(); + test02(); + return 0; +}