-
Notifications
You must be signed in to change notification settings - Fork 13
/
util_ptr_arith.go
96 lines (77 loc) · 2.27 KB
/
util_ptr_arith.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package gouring
import (
"unsafe"
)
type uint32Array = unsafe.Pointer // *uint32
func uint32Array_Index(u uint32Array, i uintptr) *uint32 {
return (*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(u)) + SizeofUint32*i))
}
type ioUringSqeArray = unsafe.Pointer // *IoUringSqe
func ioUringSqeArray_Index(u ioUringSqeArray, i uintptr) *IoUringSqe {
return (*IoUringSqe)(unsafe.Pointer(uintptr(unsafe.Pointer(u)) + SizeofIoUringSqe*i))
}
func ioUringSqe128Array_Index(u ioUringSqeArray, i uintptr) *IoUringSqe {
return (*IoUringSqe)(unsafe.Pointer(uintptr(unsafe.Pointer(u)) + (SizeofIoUringSqe+64)*i))
}
//
type ioUringCqeArray = unsafe.Pointer // *IoUringCqe
func ioUringCqeArray_Index(u ioUringCqeArray, i uintptr) *IoUringCqe {
return (*IoUringCqe)(unsafe.Pointer(uintptr(unsafe.Pointer(u)) + SizeofIoUringCqe*i))
}
func ioUringCqe32Array_Index(u ioUringCqeArray, i uintptr) *IoUringCqe {
return (*IoUringCqe)(unsafe.Pointer(uintptr(unsafe.Pointer(u)) + (SizeofIoUringCqe+SizeofIoUringCqe)*i))
}
//
type UserData [8]byte // uint64
func (u *UserData) SetUint64(v uint64) {
putUintptr(unsafe.Pointer(u), uintptr(v))
}
func (u *UserData) SetUintptr(v uintptr) {
putUintptr(unsafe.Pointer(u), v)
}
func (u *UserData) SetUnsafe(ptr unsafe.Pointer) {
putUnsafe(unsafe.Pointer(u), ptr)
}
func (u UserData) GetUnsafe() unsafe.Pointer {
return *(*unsafe.Pointer)(unsafe.Pointer(&u))
}
func (u UserData) GetUintptr() uintptr {
return uintptr(u.GetUnsafe())
}
func (u UserData) GetUint64() uint64 {
return uint64(u.GetUintptr())
}
func (u UserData) IsZero() bool {
return u.GetUintptr() == 0
}
// ---
func putUnsafe(ptr unsafe.Pointer, v unsafe.Pointer) {
*(*unsafe.Pointer)(ptr) = v
}
func putUintptr(ptr unsafe.Pointer, v uintptr) {
*(*uintptr)(ptr) = v
}
func putUint64(ptr unsafe.Pointer, v uint64) {
*(*uint64)(ptr) = v
}
func putUint32(ptr unsafe.Pointer, v uint32) {
*(*uint32)(ptr) = v
}
func putUint16(ptr unsafe.Pointer, v uint16) {
*(*uint16)(ptr) = v
}
func putUint8(ptr unsafe.Pointer, v uint8) {
*(*uint8)(ptr) = v
}
func putInt64(ptr unsafe.Pointer, v int64) {
*(*int64)(ptr) = v
}
func putInt32(ptr unsafe.Pointer, v int32) {
*(*int32)(ptr) = v
}
func putInt16(ptr unsafe.Pointer, v int16) {
*(*int16)(ptr) = v
}
func putInt8(ptr unsafe.Pointer, v int8) {
*(*int8)(ptr) = v
}