unsafe_slice.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package reflect2
  2. import (
  3. "reflect"
  4. "unsafe"
  5. )
  6. // sliceHeader is a safe version of SliceHeader used within this package.
  7. type sliceHeader struct {
  8. Data unsafe.Pointer
  9. Len int
  10. Cap int
  11. }
  12. type UnsafeSliceType struct {
  13. unsafeType
  14. elemRType unsafe.Pointer
  15. pElemRType unsafe.Pointer
  16. elemSize uintptr
  17. }
  18. func newUnsafeSliceType(cfg *frozenConfig, type1 reflect.Type) SliceType {
  19. elemType := type1.Elem()
  20. return &UnsafeSliceType{
  21. unsafeType: *newUnsafeType(cfg, type1),
  22. pElemRType: unpackEFace(reflect.PtrTo(elemType)).data,
  23. elemRType: unpackEFace(elemType).data,
  24. elemSize: elemType.Size(),
  25. }
  26. }
  27. func (type2 *UnsafeSliceType) Set(obj interface{}, val interface{}) {
  28. objEFace := unpackEFace(obj)
  29. assertType("Type.Set argument 1", type2.ptrRType, objEFace.rtype)
  30. valEFace := unpackEFace(val)
  31. assertType("Type.Set argument 2", type2.ptrRType, valEFace.rtype)
  32. type2.UnsafeSet(objEFace.data, valEFace.data)
  33. }
  34. func (type2 *UnsafeSliceType) UnsafeSet(ptr unsafe.Pointer, val unsafe.Pointer) {
  35. *(*sliceHeader)(ptr) = *(*sliceHeader)(val)
  36. }
  37. func (type2 *UnsafeSliceType) IsNil(obj interface{}) bool {
  38. if obj == nil {
  39. return true
  40. }
  41. objEFace := unpackEFace(obj)
  42. assertType("Type.IsNil argument 1", type2.ptrRType, objEFace.rtype)
  43. return type2.UnsafeIsNil(objEFace.data)
  44. }
  45. func (type2 *UnsafeSliceType) UnsafeIsNil(ptr unsafe.Pointer) bool {
  46. if ptr == nil {
  47. return true
  48. }
  49. return (*sliceHeader)(ptr).Data == nil
  50. }
  51. func (type2 *UnsafeSliceType) SetNil(obj interface{}) {
  52. objEFace := unpackEFace(obj)
  53. assertType("SliceType.SetNil argument 1", type2.ptrRType, objEFace.rtype)
  54. type2.UnsafeSetNil(objEFace.data)
  55. }
  56. func (type2 *UnsafeSliceType) UnsafeSetNil(ptr unsafe.Pointer) {
  57. header := (*sliceHeader)(ptr)
  58. header.Len = 0
  59. header.Cap = 0
  60. header.Data = nil
  61. }
  62. func (type2 *UnsafeSliceType) MakeSlice(length int, cap int) interface{} {
  63. return packEFace(type2.ptrRType, type2.UnsafeMakeSlice(length, cap))
  64. }
  65. func (type2 *UnsafeSliceType) UnsafeMakeSlice(length int, cap int) unsafe.Pointer {
  66. header := &sliceHeader{unsafe_NewArray(type2.elemRType, cap), length, cap}
  67. return unsafe.Pointer(header)
  68. }
  69. func (type2 *UnsafeSliceType) LengthOf(obj interface{}) int {
  70. objEFace := unpackEFace(obj)
  71. assertType("SliceType.Len argument 1", type2.ptrRType, objEFace.rtype)
  72. return type2.UnsafeLengthOf(objEFace.data)
  73. }
  74. func (type2 *UnsafeSliceType) UnsafeLengthOf(obj unsafe.Pointer) int {
  75. header := (*sliceHeader)(obj)
  76. return header.Len
  77. }
  78. func (type2 *UnsafeSliceType) SetIndex(obj interface{}, index int, elem interface{}) {
  79. objEFace := unpackEFace(obj)
  80. assertType("SliceType.SetIndex argument 1", type2.ptrRType, objEFace.rtype)
  81. elemEFace := unpackEFace(elem)
  82. assertType("SliceType.SetIndex argument 3", type2.pElemRType, elemEFace.rtype)
  83. type2.UnsafeSetIndex(objEFace.data, index, elemEFace.data)
  84. }
  85. func (type2 *UnsafeSliceType) UnsafeSetIndex(obj unsafe.Pointer, index int, elem unsafe.Pointer) {
  86. header := (*sliceHeader)(obj)
  87. elemPtr := arrayAt(header.Data, index, type2.elemSize, "i < s.Len")
  88. typedmemmove(type2.elemRType, elemPtr, elem)
  89. }
  90. func (type2 *UnsafeSliceType) GetIndex(obj interface{}, index int) interface{} {
  91. objEFace := unpackEFace(obj)
  92. assertType("SliceType.GetIndex argument 1", type2.ptrRType, objEFace.rtype)
  93. elemPtr := type2.UnsafeGetIndex(objEFace.data, index)
  94. return packEFace(type2.pElemRType, elemPtr)
  95. }
  96. func (type2 *UnsafeSliceType) UnsafeGetIndex(obj unsafe.Pointer, index int) unsafe.Pointer {
  97. header := (*sliceHeader)(obj)
  98. return arrayAt(header.Data, index, type2.elemSize, "i < s.Len")
  99. }
  100. func (type2 *UnsafeSliceType) Append(obj interface{}, elem interface{}) {
  101. objEFace := unpackEFace(obj)
  102. assertType("SliceType.Append argument 1", type2.ptrRType, objEFace.rtype)
  103. elemEFace := unpackEFace(elem)
  104. assertType("SliceType.Append argument 2", type2.pElemRType, elemEFace.rtype)
  105. type2.UnsafeAppend(objEFace.data, elemEFace.data)
  106. }
  107. func (type2 *UnsafeSliceType) UnsafeAppend(obj unsafe.Pointer, elem unsafe.Pointer) {
  108. header := (*sliceHeader)(obj)
  109. oldLen := header.Len
  110. type2.UnsafeGrow(obj, oldLen+1)
  111. type2.UnsafeSetIndex(obj, oldLen, elem)
  112. }
  113. func (type2 *UnsafeSliceType) Cap(obj interface{}) int {
  114. objEFace := unpackEFace(obj)
  115. assertType("SliceType.Cap argument 1", type2.ptrRType, objEFace.rtype)
  116. return type2.UnsafeCap(objEFace.data)
  117. }
  118. func (type2 *UnsafeSliceType) UnsafeCap(ptr unsafe.Pointer) int {
  119. return (*sliceHeader)(ptr).Cap
  120. }
  121. func (type2 *UnsafeSliceType) Grow(obj interface{}, newLength int) {
  122. objEFace := unpackEFace(obj)
  123. assertType("SliceType.Grow argument 1", type2.ptrRType, objEFace.rtype)
  124. type2.UnsafeGrow(objEFace.data, newLength)
  125. }
  126. func (type2 *UnsafeSliceType) UnsafeGrow(obj unsafe.Pointer, newLength int) {
  127. header := (*sliceHeader)(obj)
  128. if newLength <= header.Cap {
  129. header.Len = newLength
  130. return
  131. }
  132. newCap := calcNewCap(header.Cap, newLength)
  133. newHeader := (*sliceHeader)(type2.UnsafeMakeSlice(header.Len, newCap))
  134. typedslicecopy(type2.elemRType, *newHeader, *header)
  135. header.Data = newHeader.Data
  136. header.Cap = newHeader.Cap
  137. header.Len = newLength
  138. }
  139. func calcNewCap(cap int, expectedCap int) int {
  140. if cap == 0 {
  141. cap = expectedCap
  142. } else {
  143. for cap < expectedCap {
  144. if cap < 1024 {
  145. cap += cap
  146. } else {
  147. cap += cap / 4
  148. }
  149. }
  150. }
  151. return cap
  152. }