public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR libstdc++/77691 increase allocation size to at least alignment
@ 2018-10-12  0:56 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2018-10-12  0:56 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 563 bytes --]

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.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3193 bytes --]

commit 100292bfb473df26817d99ffa56ad6728177732b
Author: Jonathan Wakely <jwakely@redhat.com>
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<char*>(__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<max_align_t>(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<short>(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<long>(p) );
   r1->deallocate(p, 4, alignof(long));
   VERIFY( bytes_allocated == 0 );

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-10-11 23:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-12  0:56 [PATCH] PR libstdc++/77691 increase allocation size to at least alignment Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).