From patchwork Thu Oct 11 23:21:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 982771 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-487404-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="ifYD2bvE"; dkim-atps=neutral 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 42WRmp61lHz9s7W for ; Fri, 12 Oct 2018 10:21:30 +1100 (AEDT) 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:subject:message-id:mime-version:content-type; q=dns; s= default; b=TR7AE7QoIKKV51NzRx70KS/wWkU7f7mnOXIWIYY0bBC6QtlNvNqQ+ AjrkrH4oD9QkblwfBgXMpVQk8bQqNtarbW5DgBpYY7ESqE1tbh1Qv0wVbCORmbkO k1uMu9aSTvfVZJn1NGYVMK4HNL2E51gfL/tb6oqhQNYpAfSiSwaVjc= 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:subject:message-id:mime-version:content-type; s= default; bh=/efwTCoviyqMKcgpsfTOk0ELlJg=; b=ifYD2bvEYPzx67mgEAR7 UeU9jpKFiDiqz05FBrVpKSHy/mTNEO6xNyY+leJvbz22VdYuNXMzJ/BbI69YBU3w E4TZdpHFSVKe/4lajNo1A8J8aHaY5Ml7yMZpOjXSk6nDRS/9iXUTtvuZoqbap3AY wtfXGTYd/UcQD2Lp0qPKjuE= Received: (qmail 74388 invoked by alias); 11 Oct 2018 23:21:23 -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 74366 invoked by uid 89); 11 Oct 2018 23:21:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= 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 ESMTP; Thu, 11 Oct 2018 23:21:21 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 29EE83001910; Thu, 11 Oct 2018 23:21:20 +0000 (UTC) Received: from localhost (unknown [10.33.36.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id C94BD63640; Thu, 11 Oct 2018 23:21:19 +0000 (UTC) Date: Fri, 12 Oct 2018 00:21:19 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR libstdc++/77691 increase allocation size to at least alignment Message-ID: <20181011232118.GA15240@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) It's not safe to assume that malloc(n) returns memory aligned to more than n, so when relying on the guaranteed alignment of malloc ensure that the number of bytes allocated is at least as large as the alignment. PR libstdc++/77691 * include/experimental/memory_resource (__resource_adaptor_imp): Do not allocate sizes smaller than alignment when relying on guaranteed alignment. * testsuite/experimental/memory_resource/new_delete_resource.cc: Adjust expected number of bytes allocated for alignof(max_align_t). Tested x86_64-linux, committed to trunk. commit 100292bfb473df26817d99ffa56ad6728177732b Author: Jonathan Wakely Date: Fri Oct 12 00:10:27 2018 +0100 PR libstdc++/77691 increase allocation size to at least alignment It's not safe to assume that malloc(n) returns memory aligned to more than n, so when relying on the guaranteed alignment of malloc ensure that the number of bytes allocated is at least as large as the alignment. PR libstdc++/77691 * include/experimental/memory_resource (__resource_adaptor_imp): Do not allocate sizes smaller than alignment when relying on guaranteed alignment. * testsuite/experimental/memory_resource/new_delete_resource.cc: Adjust expected number of bytes allocated for alignof(max_align_t). diff --git a/libstdc++-v3/include/experimental/memory_resource b/libstdc++-v3/include/experimental/memory_resource index ccb45bfa335..fd40d2cf45b 100644 --- a/libstdc++-v3/include/experimental/memory_resource +++ b/libstdc++-v3/include/experimental/memory_resource @@ -421,7 +421,12 @@ namespace pmr { do_allocate(size_t __bytes, size_t __alignment) override { if (__alignment <= __guaranteed_alignment<_Alloc>::value) - return _M_alloc.allocate(__bytes); + { + if (__bytes < __alignment) + __bytes = __alignment; + return _M_alloc.allocate(__bytes); + } + const _AlignMgr __mgr(__bytes, __alignment); // Assume _M_alloc returns 1-byte aligned memory, so allocate enough @@ -437,6 +442,8 @@ namespace pmr { auto __ptr = static_cast(__p); if (__alignment <= __guaranteed_alignment<_Alloc>::value) { + if (__bytes < __alignment) + __bytes = __alignment; _M_alloc.deallocate(__ptr, __bytes); return; } diff --git a/libstdc++-v3/testsuite/experimental/memory_resource/new_delete_resource.cc b/libstdc++-v3/testsuite/experimental/memory_resource/new_delete_resource.cc index 11667b1d138..3af3861d1a0 100644 --- a/libstdc++-v3/testsuite/experimental/memory_resource/new_delete_resource.cc +++ b/libstdc++-v3/testsuite/experimental/memory_resource/new_delete_resource.cc @@ -109,11 +109,13 @@ test03() using std::size_t; void* p = nullptr; + auto max = [](int n, int a) { return n > a ? n : a; }; + bytes_allocated = 0; memory_resource* r1 = new_delete_resource(); - p = r1->allocate(1); - VERIFY( bytes_allocated == 1 ); + p = r1->allocate(1); // uses alignment = alignof(max_align_t) + VERIFY( bytes_allocated <= alignof(max_align_t) ); VERIFY( aligned(p) ); r1->deallocate(p, 1); VERIFY( bytes_allocated == 0 ); @@ -125,13 +127,13 @@ test03() VERIFY( bytes_allocated == 0 ); p = r1->allocate(3, alignof(short)); - VERIFY( bytes_allocated == 3 ); + VERIFY( bytes_allocated == max(3, alignof(short)) ); VERIFY( aligned(p) ); r1->deallocate(p, 3, alignof(short)); VERIFY( bytes_allocated == 0 ); p = r1->allocate(4, alignof(long)); - VERIFY( bytes_allocated == 4 ); + VERIFY( bytes_allocated == max(4, alignof(long)) ); VERIFY( aligned(p) ); r1->deallocate(p, 4, alignof(long)); VERIFY( bytes_allocated == 0 );