在看《 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 的幂呢?)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.