在看《 The Linux Programming Interface 》( Linux/Unix 系统编程手册)第 13 章,第 6 小节,其中的示例代码:
/* direct_read.c Demonstrate the use of O_DIRECT to perform I/O bypassing the buffer cache ("direct I/O"). Usage: direct_read file length [offset [alignment]] This program is Linux-specific. */ #define _GNU_SOURCE /* Obtain O_DIRECT definition from <fcntl.h> */ #include <fcntl.h> #include <malloc.h> #include "tlpi_hdr.h" int main(int argc, char *argv[]) { int fd; ssize_t numRead; size_t length, alignment; off_t offset; char *buf; if (argc < 3 || strcmp(argv[1], "--help") == 0) usageErr("%s file length [offset [alignment]]\n", argv[0]); length = getLong(argv[2], GN_ANY_BASE, "length"); offset = (argc > 3) ? getLong(argv[3], GN_ANY_BASE, "offset") : 0; alignment = (argc > 4) ? getLong(argv[4], GN_ANY_BASE, "alignment") : 4096; fd = open(argv[1], O_RDONLY | O_DIRECT); if (fd == -1) errExit("open"); /* memalign() allocates a block of memory aligned on an address that is a multiple of its first argument. By specifying this argument as 2 * 'alignment' and then adding 'alignment' to the returned pointer, we ensure that 'buf' is aligned on a non-power-of-two multiple of 'alignment'. We do this to ensure that if, for example, we ask for a 256-byte aligned buffer, we don't accidentally get a buffer that is also aligned on a 512-byte boundary. */ buf = memalign(alignment * 2, length + alignment); if (buf == NULL) errExit("memalign"); buf += alignment; if (lseek(fd, offset, SEEK_SET) == -1) errExit("lseek"); numRead = read(fd, buf, length); if (numRead == -1) errExit("read"); printf("Read %ld bytes\n", (long) numRead); exit(EXIT_SUCCESS); }
是想展示以下三点:
中间大块的注释里有一句话:
we ensure that 'buf' is aligned on a non-power-of-two multiple of 'alignment'.
请问这个用意是什么呢?(因为我感觉这是不必要的,为什么非得让 buf 的开始地址对其为非 2 的幂呢?)
![]() | 1 blahgeek 2016-09-20 17:15:23 +08:00 这个代码不希望指定 256 的时候分配一个其实是 512 对齐的内存,所以他分配一个 2*256 对齐,长度为 len+256 的内存,然后取偏移 256 后的地址,保证不是 512 对齐的 |
3 hitmanx 2016-09-20 17:46:40 +08:00 |
![]() | 4 skydiver 2016-09-20 18:00:50 +08:00 很简单啊,因为磁盘的块大小是 512 字节,这个代码是故意分配一个不按照 512 对齐的 buf ,来展示一下这样会报错。 如果传 512 就不会报错,传 256 就会报错。 所以分配 256 的时候就故意不让它是 512 整数倍,这样才能给你看报错。 |