2023-04-03 11:16:17 +02:00
|
|
|
package sys
|
2023-01-17 21:59:04 +01:00
|
|
|
|
2023-04-03 11:16:17 +02:00
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/cilium/ebpf/internal/unix"
|
|
|
|
)
|
2023-01-17 21:59:04 +01:00
|
|
|
|
|
|
|
// NewPointer creates a 64-bit pointer from an unsafe Pointer.
|
|
|
|
func NewPointer(ptr unsafe.Pointer) Pointer {
|
|
|
|
return Pointer{ptr: ptr}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSlicePointer creates a 64-bit pointer from a byte slice.
|
|
|
|
func NewSlicePointer(buf []byte) Pointer {
|
|
|
|
if len(buf) == 0 {
|
|
|
|
return Pointer{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pointer{ptr: unsafe.Pointer(&buf[0])}
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:16:17 +02:00
|
|
|
// NewSlicePointer creates a 64-bit pointer from a byte slice.
|
|
|
|
//
|
|
|
|
// Useful to assign both the pointer and the length in one go.
|
|
|
|
func NewSlicePointerLen(buf []byte) (Pointer, uint32) {
|
|
|
|
return NewSlicePointer(buf), uint32(len(buf))
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:59:04 +01:00
|
|
|
// NewStringPointer creates a 64-bit pointer from a string.
|
|
|
|
func NewStringPointer(str string) Pointer {
|
2023-04-03 11:16:17 +02:00
|
|
|
p, err := unix.BytePtrFromString(str)
|
|
|
|
if err != nil {
|
2023-01-17 21:59:04 +01:00
|
|
|
return Pointer{}
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:16:17 +02:00
|
|
|
return Pointer{ptr: unsafe.Pointer(p)}
|
2023-01-17 21:59:04 +01:00
|
|
|
}
|