Handle platforms (like Darwin 9) that do not have posix_memalign

This commit is contained in:
Quentin Smith
2012-03-23 02:13:55 -04:00
parent 26fc88634d
commit 10332ec61f
2 changed files with 19 additions and 1 deletions
+18
View File
@@ -50,9 +50,27 @@ static void * sse_alloc( int len )
{
void *ptr = NULL;
#if defined(HAVE_POSIX_MEMALIGN)
if( (0 != posix_memalign( (void **)&ptr, 16, len )) || (ptr == NULL) ) {
throw std::bad_alloc();
}
#else
// Some platforms will align malloc. Let's try that first.
if( ! (ptr = malloc(len)) ) {
throw std::bad_alloc();
}
if( (size_t)ptr & 0xF ) {
// The pointer wasn't 16-byte aligned, so try again with valloc
free(ptr);
if( ! (ptr = valloc(len)) ) {
throw std::bad_alloc();
}
if( (size_t)ptr & 0xF ) {
free(ptr);
throw std::bad_alloc();
}
}
#endif /* !defined(HAVE_POSIX_MEMALIGN) */
return ptr;
}