diff --git a/frame/base/bli_pool.c b/frame/base/bli_pool.c index 9c7230faa..305cd3234 100644 --- a/frame/base/bli_pool.c +++ b/frame/base/bli_pool.c @@ -282,13 +282,22 @@ void bli_pool_alloc_block( siz_t block_size, buf_sys = bli_malloc( block_size + align_size ); buf_align = buf_sys; - // Advance the pointer to achieve the necessary alignment, if it - // is not already aligned. - if ( bli_is_unaligned_to( ( uintptr_t )buf_sys, ( uintptr_t )align_size ) ) + // Advance the pointer to achieve the necessary alignment, if it is not + // already aligned. + if ( bli_is_unaligned_to( buf_sys, align_size ) ) { - // Notice that this works even if the alignment is not a power of two. - buf_align += ( ( uintptr_t )align_size - - ( ( uintptr_t )buf_sys % align_size ) ); + // C99's stdint.h guarantees that a void* can be safely cast to a + // uintptr_t and then back to a void*, hence the casting of buf_sys + // and align_size to uintptr_t. buf_align is initially cast to char* + // to allow pointer arithmetic in units of bytes, and then advanced + // to the next nearest alignment boundary, and finally cast back to + // void* before being stored. Notice that the arithmetic works even + // if the alignment value is not a power of two. + buf_align = ( void* )( ( char* )buf_align + + ( ( uintptr_t )align_size - + ( uintptr_t )buf_sys % + ( uintptr_t )align_size ) + ); } // Save the results in the pblk_t structure. diff --git a/frame/include/bli_param_macro_defs.h b/frame/include/bli_param_macro_defs.h index f1924d920..40a054ffa 100644 --- a/frame/include/bli_param_macro_defs.h +++ b/frame/include/bli_param_macro_defs.h @@ -42,15 +42,15 @@ #define bli_is_aligned_to( p, size ) \ \ - ( ( siz_t )(p) % ( siz_t )(size) == 0 ) + ( ( uintptr_t )(p) % ( uintptr_t )(size) == 0 ) #define bli_is_unaligned_to( p, size ) \ \ - ( ( siz_t )(p) % ( siz_t )(size) != 0 ) + ( ( uintptr_t )(p) % ( uintptr_t )(size) != 0 ) #define bli_offset_from_alignment( p, size ) \ \ - ( ( siz_t )(p) % ( siz_t )(size) ) + ( ( uintptr_t )(p) % ( uintptr_t )(size) ) // datatype