Commit 2383ed02 by Jonathan Wakely Committed by Jonathan Wakely

Minor optimisations in operator new(size_t, align_val_t)

	* libsupc++/new_opa.cc (operator new(size_t, align_val_t)): Use
	__is_pow2 to check for valid alignment. Avoid branching when rounding
	size to multiple of alignment.

From-SVN: r263515
parent 7997ede2
2018-08-13 Jonathan Wakely <jwakely@redhat.com> 2018-08-13 Jonathan Wakely <jwakely@redhat.com>
* libsupc++/new_opa.cc (operator new(size_t, align_val_t)): Use
__is_pow2 to check for valid alignment. Avoid branching when rounding
size to multiple of alignment.
* include/Makefile.am: Install <bit> and <version> for freestanding. * include/Makefile.am: Install <bit> and <version> for freestanding.
* include/Makefile.in: Regenerate. * include/Makefile.in: Regenerate.
* testsuite/17_intro/freestanding.cc: Check for <bit> and <version>. * testsuite/17_intro/freestanding.cc: Check for <bit> and <version>.
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <bits/exception_defines.h> #include <bits/exception_defines.h>
#include <bit>
#include "new" #include "new"
#if !_GLIBCXX_HAVE_ALIGNED_ALLOC && !_GLIBCXX_HAVE__ALIGNED_MALLOC \ #if !_GLIBCXX_HAVE_ALIGNED_ALLOC && !_GLIBCXX_HAVE__ALIGNED_MALLOC \
...@@ -105,7 +106,7 @@ operator new (std::size_t sz, std::align_val_t al) ...@@ -105,7 +106,7 @@ operator new (std::size_t sz, std::align_val_t al)
/* Alignment must be a power of two. */ /* Alignment must be a power of two. */
/* XXX This should be checked by the compiler (PR 86878). */ /* XXX This should be checked by the compiler (PR 86878). */
if (__builtin_expect (align & (align - 1), false)) if (__builtin_expect (!std::__ispow2(align), false))
_GLIBCXX_THROW_OR_ABORT(bad_alloc()); _GLIBCXX_THROW_OR_ABORT(bad_alloc());
/* malloc (0) is unpredictable; avoid it. */ /* malloc (0) is unpredictable; avoid it. */
...@@ -120,8 +121,7 @@ operator new (std::size_t sz, std::align_val_t al) ...@@ -120,8 +121,7 @@ operator new (std::size_t sz, std::align_val_t al)
align = sizeof(void*); align = sizeof(void*);
# endif # endif
/* C11: the value of size shall be an integral multiple of alignment. */ /* C11: the value of size shall be an integral multiple of alignment. */
if (std::size_t rem = sz & (align - 1)) sz = (sz + align - 1) & ~(align - 1);
sz += align - rem;
#endif #endif
void *p; void *p;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment