golang直接io的使用

156 阅读1分钟

原文链接:mp.weixin.qq.com/s/gW_3JD52r…

image.png

image.png image.png

const (
   // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
   O_RDONLY int = syscall.O_RDONLY // open the file read-only.
   O_WRONLY int = syscall.O_WRONLY // open the file write-only.
   O_RDWR   int = syscall.O_RDWR   // open the file read-write.
   // The remaining values may be or'ed in to control behavior.
   O_APPEND int = syscall.O_APPEND // append data to the file when writing.
   O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
   O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
   O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
   O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened.
)

image.png image.png image.png

const (
    AlignSize = 512
)

// 在 block 这个字节数组首地址,往后找,找到符合 AlignSize 对齐的地址,并返回
// 这里用到位操作,速度很快;
func alignment(block []byte, AlignSize int) int {
   return int(uintptr(unsafe.Pointer(&block[0])) & uintptr(AlignSize-1))
}

// 分配 BlockSize 大小的内存块
// 地址按照 512 对齐
func AlignedBlock(BlockSize int) []byte {
   // 分配一个,分配大小比实际需要的稍大
   block := make([]byte, BlockSize+AlignSize)

   // 计算这个 block 内存块往后多少偏移,地址才能对齐到 512 
   a := alignment(block, AlignSize)
   offset := 0
   if a != 0 {
      offset = AlignSize - a
   }

   // 偏移指定位置,生成一个新的 block,这个 block 将满足地址对齐 512;
   block = block[offset : offset+BlockSize]
   if BlockSize != 0 {
      // 最后做一次校验 
      a = alignment(block, AlignSize)
      if a != 0 {
         log.Fatal("Failed to align block")
      }
   }
   
   return block
}

所以,通过以上 AlignedBlock 函数分配出来的内存一定是 512 地址对齐的。

有啥缺点吗?

浪费空间嘛。 命名需要 4k 内存,实际分配了 4k+512 。 image.png image.png

开源库地址:github.com/ncw/directi…