【发布时间】:2014-10-17 01:41:32
【问题描述】:
我看到一段代码是这样做磁盘 io 的:
static bool is_aligned(unsigned char *buffer) {
return ( ((unsigned long)buffer) & (DISK_PAGE_SIZE -1)) == 0;
}
void do_write_IO(int fd, unsignced char *buffer, unsigned long buffer_bytes) {
...
if (is_aligned(buffer)) {
write_to_file(fd, buffer, io_size);
} else {
// bounce buffer is an aligned memory space.
// if buffer not aligned, copy to an aligned address
// and do fs write. WHY ?
// What are the benefits ?
memcpy(bounce_buffer, buffer, io_size);
write_to_file(fd, bounce_buffer, io_size);
}
...
}
// Just call posix write, write output to fd, with bytes_to_write size.
static void write_to_file(int fd,
unsigned char *output,
unsigned long bytes_to_write)
{
__sync_fetch_and_add(&stat_bytes_written, bytes_to_write);
while(bytes_to_write) {
unsigned long bytes_written = write(fd, output, bytes_to_write);
if(bytes_written == -1UL) {
if(errno != EAGAIN) {
BOOST_LOG_TRIVIAL(fatal) <<
"Stream writeout unsuccessful:" << strerror(errno);
exit(-1);
}
}
else {
output += bytes_written;
bytes_to_write -= bytes_written;
}
}
}
我注意到它使用地址空间对齐的缓冲区写入文件。 那么,写入具有对齐缓冲区的文件有什么好处吗?
【问题讨论】:
标签: linux io filesystems posix memory-alignment