add fluid_align_ptr() for aligning pointers

This commit is contained in:
derselbst 2018-04-12 18:26:12 +02:00
parent 384f05e77c
commit cdbd508007
3 changed files with 48 additions and 0 deletions

View file

@ -602,4 +602,20 @@ void fluid_clear_fpe_i386(void);
/* System control */
void fluid_msleep(unsigned int msecs);
/**
* Advances the given \c ptr to the next \c alignment byte boundary.
* Make sure you've allocated an extra of \c alignment bytes to avoid a buffer overflow.
*
* @return Returned pointer is guarenteed to be aligned to \c alignment boundary and in range \f[ ptr <= returned_ptr < ptr + alignment \f].
*/
static FLUID_INLINE void* fluid_align_ptr(const void* ptr, unsigned int alignment)
{
uintptr_t ptr_int = (uintptr_t)ptr;
unsigned int offset = ptr_int % alignment;
unsigned int add = offset == 0 ? 0 // is already aligned, dont advance, else buffer overrun
: alignment - offset; // advance the pointer to the next alignment boundary
ptr_int += add;
return (void*)ptr_int;
}
#endif /* _FLUID_SYS_H */

View file

@ -13,6 +13,7 @@ ADD_FLUID_TEST(test_sample_cache)
ADD_FLUID_TEST(test_sfont_loading)
ADD_FLUID_TEST(test_sample_rate_change)
ADD_FLUID_TEST(test_preset_sample_loading)
ADD_FLUID_TEST(test_pointer_alignment)
if ( LIBSNDFILE_HASVORBIS )
ADD_FLUID_TEST(test_sf3_sfont_loading)

View file

@ -0,0 +1,31 @@
#include "test.h"
#include "utils/fluid_sys.h"
// test for fluid_align_ptr()
int main(void)
{
unsigned int align;
uintptr_t ptr, aligned_ptr;
for(align = 32; align <= 4*1024u; align <<= 1)
{
for(ptr = 0; ptr <= (align<<10); ptr++)
{
char* tmp = fluid_align_ptr((char*)ptr, align);
aligned_ptr = (uintptr_t)tmp;
// pointer must be aligned properly
TEST_ASSERT(aligned_ptr % align == 0);
// aligned pointer must not be smaller than ptr
TEST_ASSERT(aligned_ptr >= ptr);
// aligned pointer must not be bigger than alignment
TEST_ASSERT(aligned_ptr < ptr + align);
}
}
return EXIT_SUCCESS;
}