Commit 426042f5 by Jonathan Wakely Committed by Jonathan Wakely

Adjust arguments to aligned_alloc or posix_memalign

	* libsupc++/new_opa.cc [_GLIBCXX_HAVE_POSIX_MEMALIGN] (aligned_alloc):
	Increase alignment if less than sizeof(void*).
	[_GLIBCXX_HAVE_ALIGNED_ALLOC] (operator new(size_t, align_val_t)):
	Increase size if not a multiple of alignment.

From-SVN: r240187
parent 2c3d35a6
2016-09-16 Jonathan Wakely <jwakely@redhat.com>
* libsupc++/new_opa.cc [_GLIBCXX_HAVE_POSIX_MEMALIGN] (aligned_alloc):
Increase alignment if less than sizeof(void*).
[_GLIBCXX_HAVE_ALIGNED_ALLOC] (operator new(size_t, align_val_t)):
Increase size if not a multiple of alignment.
2016-09-15 Jonathan Wakely <jwakely@redhat.com> 2016-09-15 Jonathan Wakely <jwakely@redhat.com>
* doc/xml/manual/debug_mode.xml: Minor editorial fixes. * doc/xml/manual/debug_mode.xml: Minor editorial fixes.
......
...@@ -39,6 +39,9 @@ static inline void* ...@@ -39,6 +39,9 @@ static inline void*
aligned_alloc (std::size_t al, std::size_t sz) aligned_alloc (std::size_t al, std::size_t sz)
{ {
void *ptr; void *ptr;
// The value of alignment shall be a power of two multiple of sizeof(void *).
if (al < sizeof(void*))
al = sizeof(void*);
int ret = posix_memalign (&ptr, al, sz); int ret = posix_memalign (&ptr, al, sz);
if (ret == 0) if (ret == 0)
return ptr; return ptr;
...@@ -58,13 +61,19 @@ _GLIBCXX_WEAK_DEFINITION void * ...@@ -58,13 +61,19 @@ _GLIBCXX_WEAK_DEFINITION void *
operator new (std::size_t sz, std::align_val_t al) operator new (std::size_t sz, std::align_val_t al)
{ {
void *p; void *p;
std::size_t align = (std::size_t)al;
/* malloc (0) is unpredictable; avoid it. */ /* malloc (0) is unpredictable; avoid it. */
if (sz == 0) if (sz == 0)
sz = 1; sz = 1;
while (__builtin_expect ((p = aligned_alloc ((std::size_t)al, sz)) == 0, #if _GLIBCXX_HAVE_ALIGNED_ALLOC
false)) /* C11: the value of size shall be an integral multiple of alignment. */
if (std::size_t rem = sz % align)
sz += align - rem;
#endif
while (__builtin_expect ((p = aligned_alloc (align, sz)) == 0, false))
{ {
new_handler handler = std::get_new_handler (); new_handler handler = std::get_new_handler ();
if (! handler) if (! handler)
......
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