将golang中的`ioctl`调用转换为代码。

huangapple go评论92阅读模式
英文:

Convert `ioctl` call in golang

问题

我正在尝试转换,或者至少理解这个 ioctl 调用:

#define NVME_URING_CMD_IO	_IOWR('N', 0x80, struct nvme_uring_cmd)

在 Golang 中是否有 _IOWR 的等效方式?也许可以使用 unix 包吗?

英文:

I'm trying to convert, or at least understand this ioctl call:

#define NVME_URING_CMD_IO	_IOWR('N', 0x80, struct nvme_uring_cmd)

Is there an equivalent of _IOWR in golang? maybe with the unix package?

答案1

得分: 0

好的,我已经成功将其转换了,感谢评论中的帮助:


const (
	IocNrBits           = 8
	IocTypeBits         = 8
	IocSizeBits         = 14
	IocNrShift          = 0
	IocRead     uintptr = 2
	IocWrite    uintptr = 2
)

const (
	IocTypeShift = IocNrShift + IocNrBits
	IocSizeShift = IocTypeShift + IocTypeBits
	IocDirshift  = IocSizeShift + IocSizeBits
)

func IOC(dir, t, nr, size uintptr) uintptr {
	return (dir << IocDirshift) |
		(t << IocTypeShift) |
		(nr << IocNrShift) |
		(size << IocSizeShift)
}

func IOWR(t, nr, size uintptr) uintptr {
	return IOC(IocRead|IocWrite, t, nr, size)
}

func NVME_URING_CMD_IO() uintptr {
	return IOWR('N', 0x80, 32)
}

英文:

Ok, I managed to convert it, thanks to help in comments:


const (
	IocNrBits           = 8
	IocTypeBits         = 8
	IocSizeBits         = 14
	IocNrShift          = 0
	IocRead     uintptr = 2
	IocWrite    uintptr = 2
)

const (
	IocTypeShift = IocNrShift + IocNrBits
	IocSizeShift = IocTypeShift + IocTypeBits
	IocDirshift  = IocSizeShift + IocSizeBits
)

func IOC(dir, t, nr, size uintptr) uintptr {
	return (dir &lt;&lt; IocDirshift) |
		(t &lt;&lt; IocTypeShift) |
		(nr &lt;&lt; IocNrShift) |
		(size &lt;&lt; IocSizeShift)
}

func IOWR(t, nr, size uintptr) uintptr {
	return IOC(IocRead|IocWrite, t, nr, size)
}

func NVME_URING_CMD_IO() uintptr {
	return IOWR(&#39;N&#39;, 0x80, 32)
}

huangapple
  • 本文由 发表于 2022年10月12日 00:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/74031563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定