我们如何使用c程序检查传递给磁盘驱动程序的缓冲区是否与扇区对齐?

时间:2016-12-15 12:37:58

标签: c linux algorithm linux-kernel linux-device-driver

如何使用c程序检查传递给磁盘驱动程序的缓冲区是否与扇区对齐?如果缓冲区不是扇区对齐的,那么我们如何使扇区对齐?

1 个答案:

答案 0 :(得分:1)

检查:

const long SECTOR_SIZE=512;  //MUST be a power of 2

bool isAligned(char *buf)
{
    long address = (long)(void *)buf;
    return ( (address & (SECTOR_SIZE-1)) == 0 );
}

对齐:请注意,在分配缓冲区时,必须分配SECTOR_SIZE-1个额外字节!释放缓冲区时,释放ORIGINAL指针!

char *align(char *buf)
{
    long address = (long)(void *)buf;
    return buf+((-address)&(SECTOR_SIZE-1));
}