github.com/nikandfor/tlog@v0.21.5-0.20231108111739-3ef89426a96d/low/runtime.go (about) 1 package low 2 3 import "unsafe" 4 5 type ( 6 eface struct { 7 t unsafe.Pointer 8 p unsafe.Pointer 9 } 10 11 sh struct { 12 p unsafe.Pointer 13 l int 14 } 15 ) 16 17 func UnpackInterface(v interface{}) (t, p unsafe.Pointer) { 18 e := ((*eface)(unsafe.Pointer(&v))) 19 return e.t, e.p 20 } 21 22 func InterfaceType(v interface{}) unsafe.Pointer { 23 return ((*eface)(unsafe.Pointer(&v))).t 24 } 25 26 func InterfaceData(v interface{}) unsafe.Pointer { 27 return ((*eface)(unsafe.Pointer(&v))).p 28 } 29 30 func UnsafeString(ptr unsafe.Pointer, l int) string { 31 return *(*string)(unsafe.Pointer(&sh{p: ptr, l: l})) 32 } 33 34 func UnsafeBytesToString(b []byte) string { 35 return *(*string)(unsafe.Pointer(&b)) 36 } 37 38 func NoEscapeBuffer(b []byte) []byte { 39 return *(*[]byte)(noescape(unsafe.Pointer(&b))) 40 } 41 42 // noescape hides a pointer from escape analysis. noescape is 43 // the identity function but escape analysis doesn't think the 44 // output depends on the input. noescape is inlined and currently 45 // compiles down to zero instructions. 46 // USE CAREFULLY! 47 // 48 //go:nosplit 49 func noescape(p unsafe.Pointer) unsafe.Pointer { 50 x := uintptr(p) 51 return unsafe.Pointer(x ^ 0) //nolint:staticcheck 52 }