字节对齐算法
字节对齐是在分配内存时需要考虑的问题,两个小算法:
(1)最容易想到的算法:
unsigned int calc_align(unsigned int n,unsigned align){
if ( n / align * align == n)
return n;
return (n / align + 1) * align;
}
(2)更好的算法:
unsigned int calc_align(unsigned int n,unsigned align)
{
return ((n + align - 1) & (~(align - 1)));
}