[PATCH] PR65122 extended alignment support in allocators

Jonathan Wakely jwakely@redhat.com
Fri Oct 14 12:03:00 GMT 2016


Now that the warning in PR77742 is fixed we can make std::allocator
use aligned new for types with extended alignment.

This only changes the allocators that actually allocate memory
themselves.  The others (array, extptr, the testsuite ones) don't need
any changes because they simply forward (de)allocate calls to an
underlying allocator such as std::allocator, so if the underlying one
works then they will too.

The default, new_allocator, is straightforward because it always uses
new/delete so can just use aligned new/delete as needed. For bitmap,
mt and pool I made them always use aligned new/delete for types with
extended alignment (they already fallback on new/delete for types that
exceed some maximum size, so this isn't so different).

For malloc_allocator I've used aligned_alloc if available. If not then
it tries malloc, but throws bad_alloc if malloc returns memory that
isn't suitably aligned for the type. That means it will work
_sometimes_ but not reliably. We could reproduce the logic from
libsupc++/new_opa.cc and try alternatives to aligned_alloc, but I'm
not sure how many people care about malloc_allocator enough to bother.

	PR libstdc++/65122
	* include/ext/malloc_allocator.h (malloc_allocator::allocate): Use
	aligned_alloc for types with extended alignment if available,
	otherwise throw bad_alloc if malloc doesn't return a suitable value.
	* include/ext/bitmap_allocator.h (bitmap_allocator::allocate)
	(bitmap_allocator::deallocate): Use aligned new/delete for types with
	extended alignment.
	* include/ext/mt_allocator.h (__mt_alloc::allocate)
	(__mt_alloc::deallocate): Likewise.
	* include/ext/new_allocator.h (new_allocator::allocate)
	(new_allocator::deallocate): Likewise.
	* include/ext/pool_allocator.h (__pool_alloc::allocate)
	(__pool_alloc::deallocate): Likewise.
	* testsuite/20_util/allocator/overaligned.cc: New test.
	* testsuite/ext/bitmap_allocator/overaligned.cc: New test.
	* testsuite/ext/malloc_allocator/overaligned.cc: New test.
	* testsuite/ext/mt_allocator/overaligned.cc: New test.
	* testsuite/ext/new_allocator/overaligned.cc: New test.
	* testsuite/ext/pool_allocator/overaligned.cc: New test.

Tested powerpc64le-linux, committed to trunk.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 18145 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20161014/31537d08/attachment.bin>


More information about the Gcc-patches mailing list