ptr.go 872 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package sys
  2. import (
  3. "unsafe"
  4. "github.com/cilium/ebpf/internal/unix"
  5. )
  6. // NewPointer creates a 64-bit pointer from an unsafe Pointer.
  7. func NewPointer(ptr unsafe.Pointer) Pointer {
  8. return Pointer{ptr: ptr}
  9. }
  10. // NewSlicePointer creates a 64-bit pointer from a byte slice.
  11. func NewSlicePointer(buf []byte) Pointer {
  12. if len(buf) == 0 {
  13. return Pointer{}
  14. }
  15. return Pointer{ptr: unsafe.Pointer(&buf[0])}
  16. }
  17. // NewSlicePointer creates a 64-bit pointer from a byte slice.
  18. //
  19. // Useful to assign both the pointer and the length in one go.
  20. func NewSlicePointerLen(buf []byte) (Pointer, uint32) {
  21. return NewSlicePointer(buf), uint32(len(buf))
  22. }
  23. // NewStringPointer creates a 64-bit pointer from a string.
  24. func NewStringPointer(str string) Pointer {
  25. p, err := unix.BytePtrFromString(str)
  26. if err != nil {
  27. return Pointer{}
  28. }
  29. return Pointer{ptr: unsafe.Pointer(p)}
  30. }