unsafe_array.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package reflect2
  2. import (
  3. "reflect"
  4. "unsafe"
  5. )
  6. type UnsafeArrayType struct {
  7. unsafeType
  8. elemRType unsafe.Pointer
  9. pElemRType unsafe.Pointer
  10. elemSize uintptr
  11. likePtr bool
  12. }
  13. func newUnsafeArrayType(cfg *frozenConfig, type1 reflect.Type) *UnsafeArrayType {
  14. return &UnsafeArrayType{
  15. unsafeType: *newUnsafeType(cfg, type1),
  16. elemRType: unpackEFace(type1.Elem()).data,
  17. pElemRType: unpackEFace(reflect.PtrTo(type1.Elem())).data,
  18. elemSize: type1.Elem().Size(),
  19. likePtr: likePtrType(type1),
  20. }
  21. }
  22. func (type2 *UnsafeArrayType) LikePtr() bool {
  23. return type2.likePtr
  24. }
  25. func (type2 *UnsafeArrayType) Indirect(obj interface{}) interface{} {
  26. objEFace := unpackEFace(obj)
  27. assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype)
  28. return type2.UnsafeIndirect(objEFace.data)
  29. }
  30. func (type2 *UnsafeArrayType) UnsafeIndirect(ptr unsafe.Pointer) interface{} {
  31. if type2.likePtr {
  32. return packEFace(type2.rtype, *(*unsafe.Pointer)(ptr))
  33. }
  34. return packEFace(type2.rtype, ptr)
  35. }
  36. func (type2 *UnsafeArrayType) SetIndex(obj interface{}, index int, elem interface{}) {
  37. objEFace := unpackEFace(obj)
  38. assertType("ArrayType.SetIndex argument 1", type2.ptrRType, objEFace.rtype)
  39. elemEFace := unpackEFace(elem)
  40. assertType("ArrayType.SetIndex argument 3", type2.pElemRType, elemEFace.rtype)
  41. type2.UnsafeSetIndex(objEFace.data, index, elemEFace.data)
  42. }
  43. func (type2 *UnsafeArrayType) UnsafeSetIndex(obj unsafe.Pointer, index int, elem unsafe.Pointer) {
  44. elemPtr := arrayAt(obj, index, type2.elemSize, "i < s.Len")
  45. typedmemmove(type2.elemRType, elemPtr, elem)
  46. }
  47. func (type2 *UnsafeArrayType) GetIndex(obj interface{}, index int) interface{} {
  48. objEFace := unpackEFace(obj)
  49. assertType("ArrayType.GetIndex argument 1", type2.ptrRType, objEFace.rtype)
  50. elemPtr := type2.UnsafeGetIndex(objEFace.data, index)
  51. return packEFace(type2.pElemRType, elemPtr)
  52. }
  53. func (type2 *UnsafeArrayType) UnsafeGetIndex(obj unsafe.Pointer, index int) unsafe.Pointer {
  54. return arrayAt(obj, index, type2.elemSize, "i < s.Len")
  55. }