2018-05-10 23:10:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2018-05-13 16:29:15 +02:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-05-12 14:24:25 +02:00
|
|
|
#include <stdlib.h>
|
2018-05-10 23:10:35 +02:00
|
|
|
|
2018-05-12 14:24:25 +02:00
|
|
|
void* alignedAlloc( unsigned bytes, unsigned alignment )
|
2018-05-10 23:10:35 +02:00
|
|
|
{
|
2018-05-13 20:29:47 +02:00
|
|
|
if( !bytes )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-10 23:10:35 +02:00
|
|
|
|
2018-05-13 20:29:47 +02:00
|
|
|
const unsigned maxBytes = 1024 * 1024 * 1024; //1GB is max on RPi
|
2018-05-10 23:10:35 +02:00
|
|
|
|
2018-05-13 20:29:47 +02:00
|
|
|
if( bytes > maxBytes )
|
|
|
|
{
|
|
|
|
return 0; //bad alloc
|
|
|
|
}
|
2018-05-10 23:10:35 +02:00
|
|
|
|
2018-05-13 20:29:47 +02:00
|
|
|
void* pv = 0;
|
2018-05-10 23:10:35 +02:00
|
|
|
|
2018-05-13 20:29:47 +02:00
|
|
|
if( posix_memalign( &pv, alignment, bytes ) )
|
|
|
|
{
|
|
|
|
pv = 0; //allocation failed
|
|
|
|
}
|
2018-05-10 23:10:35 +02:00
|
|
|
|
2018-05-13 20:29:47 +02:00
|
|
|
return pv;
|
2018-05-10 23:10:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void alignedFree( void* p )
|
|
|
|
{
|
2018-05-13 20:29:47 +02:00
|
|
|
free( p );
|
2018-05-10 23:10:35 +02:00
|
|
|
}
|
2018-05-13 16:29:15 +02:00
|
|
|
|
|
|
|
#if defined (__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|