github.com/hedzr/evendeep@v0.4.8/internal/syscalls/uintptr.go (about) 1 package syscalls 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "strconv" 7 "unsafe" 8 ) 9 10 func UintptrToString(p uintptr) string { 11 u := uintptrToUint(p) 12 return "0x" + strconv.FormatUint(u, 16) 13 } 14 15 func UintptrFromString(s string) uintptr { 16 if s[0:2] == "0x" { 17 u, e := strconv.ParseUint(s[2:], 16, 64) 18 if e != nil { 19 return uintptr(0) 20 } 21 return uintptr(u) 22 } 23 24 u, e := strconv.ParseUint(s, 16, 64) 25 if e != nil { 26 return uintptr(0) 27 } 28 return uintptr(u) 29 } 30 31 func UintptrToUint(u uintptr) uint64 { return uintptrToUint(u) } 32 func uintptrToUint(u uintptr) uint64 { 33 size := unsafe.Sizeof(u) 34 switch size { 35 case 4: //nolint:gomnd //simple case 36 return uint64(uint32(u)) 37 case 8: //nolint:gomnd //simple case 38 return uint64(u) 39 default: 40 panic(fmt.Sprintf("unknown uintptr size: %v", size)) 41 } 42 } 43 44 //nolint:deadcode,unused //future code 45 func toBytes1(p uintptr) []byte { 46 size := unsafe.Sizeof(p) 47 b := make([]byte, size) 48 switch size { 49 case 4: //nolint:gomnd //simple case 50 binary.LittleEndian.PutUint32(b, uint32(p)) 51 case 8: //nolint:gomnd //simple case 52 binary.LittleEndian.PutUint64(b, uint64(p)) 53 default: 54 panic(fmt.Sprintf("unknown uintptr size: %v", size)) 55 } 56 return b 57 } 58 59 //nolint:deadcode,unused //future code 60 func toBytes2(u *uintptr) []byte { 61 const sizeOfUintPtr = unsafe.Sizeof(uintptr(0)) 62 return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:] 63 }