github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/unsafe/reflectx/runtime.go (about) 1 package reflectx 2 3 import ( 4 "reflect" 5 "unsafe" 6 7 "github.com/jxskiss/gopkg/v2/internal/linkname" 8 ) 9 10 // ArrayAt returns the i-th element of p, 11 // an array whose elements are elemSize bytes wide. 12 // The array pointed at by p must have at least i+1 elements: 13 // it is invalid (but impossible to check here) to pass i >= len, 14 // because then the result will point outside the array. 15 func ArrayAt(p unsafe.Pointer, i int, elemSize uintptr) unsafe.Pointer { 16 return unsafe.Add(p, uintptr(i)*elemSize) 17 } 18 19 // MakeSlice makes a new slice of the given reflect.Type and length, capacity. 20 func MakeSlice(elemTyp reflect.Type, length, capacity int) (slice any, header *SliceHeader) { 21 elemRType := ToRType(elemTyp) 22 data := linkname.Reflect_unsafe_NewArray(unsafe.Pointer(elemRType), capacity) 23 header = &SliceHeader{ 24 Data: data, 25 Len: length, 26 Cap: capacity, 27 } 28 slice = SliceOf(elemRType).PackInterface(unsafe.Pointer(header)) 29 return 30 } 31 32 // MapLen returns the length of the given map interface{} value. 33 // The provided m must be a map, else it panics. 34 func MapLen(m any) int { 35 if reflect.TypeOf(m).Kind() != reflect.Map { 36 panic("reflectx.MapLen: param m must be a map") 37 } 38 return linkname.Reflect_maplen(EfaceOf(&m).Word) 39 } 40 41 // TypedMemMove exports the typedmemmove function in reflect package. 42 func TypedMemMove(rtype *RType, dst, src unsafe.Pointer) { 43 linkname.Reflect_typedmemmove(unsafe.Pointer(rtype), dst, src) 44 } 45 46 // TypedSliceCopy exports the typedslicecopy function in reflect package. 47 func TypedSliceCopy(elemRType *RType, dst, src SliceHeader) int { 48 return linkname.Reflect_typedslicecopy(unsafe.Pointer(elemRType), dst, src) 49 }