pointer_unsafe.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package impl
  5. import (
  6. "reflect"
  7. "sync/atomic"
  8. "unsafe"
  9. )
  10. const UnsafeEnabled = true
  11. // Pointer is an opaque pointer type.
  12. type Pointer unsafe.Pointer
  13. // offset represents the offset to a struct field, accessible from a pointer.
  14. // The offset is the byte offset to the field from the start of the struct.
  15. type offset uintptr
  16. // offsetOf returns a field offset for the struct field.
  17. func offsetOf(f reflect.StructField, x exporter) offset {
  18. return offset(f.Offset)
  19. }
  20. // IsValid reports whether the offset is valid.
  21. func (f offset) IsValid() bool { return f != invalidOffset }
  22. // invalidOffset is an invalid field offset.
  23. var invalidOffset = ^offset(0)
  24. // zeroOffset is a noop when calling pointer.Apply.
  25. var zeroOffset = offset(0)
  26. // pointer is a pointer to a message struct or field.
  27. type pointer struct{ p unsafe.Pointer }
  28. // pointerOf returns p as a pointer.
  29. func pointerOf(p Pointer) pointer {
  30. return pointer{p: unsafe.Pointer(p)}
  31. }
  32. // pointerOfValue returns v as a pointer.
  33. func pointerOfValue(v reflect.Value) pointer {
  34. return pointer{p: unsafe.Pointer(v.Pointer())}
  35. }
  36. // pointerOfIface returns the pointer portion of an interface.
  37. func pointerOfIface(v any) pointer {
  38. type ifaceHeader struct {
  39. Type unsafe.Pointer
  40. Data unsafe.Pointer
  41. }
  42. return pointer{p: (*ifaceHeader)(unsafe.Pointer(&v)).Data}
  43. }
  44. // IsNil reports whether the pointer is nil.
  45. func (p pointer) IsNil() bool {
  46. return p.p == nil
  47. }
  48. // Apply adds an offset to the pointer to derive a new pointer
  49. // to a specified field. The pointer must be valid and pointing at a struct.
  50. func (p pointer) Apply(f offset) pointer {
  51. if p.IsNil() {
  52. panic("invalid nil pointer")
  53. }
  54. return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
  55. }
  56. // AsValueOf treats p as a pointer to an object of type t and returns the value.
  57. // It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
  58. func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
  59. return reflect.NewAt(t, p.p)
  60. }
  61. // AsIfaceOf treats p as a pointer to an object of type t and returns the value.
  62. // It is equivalent to p.AsValueOf(t).Interface()
  63. func (p pointer) AsIfaceOf(t reflect.Type) any {
  64. // TODO: Use tricky unsafe magic to directly create ifaceHeader.
  65. return p.AsValueOf(t).Interface()
  66. }
  67. func (p pointer) Bool() *bool { return (*bool)(p.p) }
  68. func (p pointer) BoolPtr() **bool { return (**bool)(p.p) }
  69. func (p pointer) BoolSlice() *[]bool { return (*[]bool)(p.p) }
  70. func (p pointer) Int32() *int32 { return (*int32)(p.p) }
  71. func (p pointer) Int32Ptr() **int32 { return (**int32)(p.p) }
  72. func (p pointer) Int32Slice() *[]int32 { return (*[]int32)(p.p) }
  73. func (p pointer) Int64() *int64 { return (*int64)(p.p) }
  74. func (p pointer) Int64Ptr() **int64 { return (**int64)(p.p) }
  75. func (p pointer) Int64Slice() *[]int64 { return (*[]int64)(p.p) }
  76. func (p pointer) Uint32() *uint32 { return (*uint32)(p.p) }
  77. func (p pointer) Uint32Ptr() **uint32 { return (**uint32)(p.p) }
  78. func (p pointer) Uint32Slice() *[]uint32 { return (*[]uint32)(p.p) }
  79. func (p pointer) Uint64() *uint64 { return (*uint64)(p.p) }
  80. func (p pointer) Uint64Ptr() **uint64 { return (**uint64)(p.p) }
  81. func (p pointer) Uint64Slice() *[]uint64 { return (*[]uint64)(p.p) }
  82. func (p pointer) Float32() *float32 { return (*float32)(p.p) }
  83. func (p pointer) Float32Ptr() **float32 { return (**float32)(p.p) }
  84. func (p pointer) Float32Slice() *[]float32 { return (*[]float32)(p.p) }
  85. func (p pointer) Float64() *float64 { return (*float64)(p.p) }
  86. func (p pointer) Float64Ptr() **float64 { return (**float64)(p.p) }
  87. func (p pointer) Float64Slice() *[]float64 { return (*[]float64)(p.p) }
  88. func (p pointer) String() *string { return (*string)(p.p) }
  89. func (p pointer) StringPtr() **string { return (**string)(p.p) }
  90. func (p pointer) StringSlice() *[]string { return (*[]string)(p.p) }
  91. func (p pointer) Bytes() *[]byte { return (*[]byte)(p.p) }
  92. func (p pointer) BytesPtr() **[]byte { return (**[]byte)(p.p) }
  93. func (p pointer) BytesSlice() *[][]byte { return (*[][]byte)(p.p) }
  94. func (p pointer) WeakFields() *weakFields { return (*weakFields)(p.p) }
  95. func (p pointer) Extensions() *map[int32]ExtensionField { return (*map[int32]ExtensionField)(p.p) }
  96. func (p pointer) Elem() pointer {
  97. return pointer{p: *(*unsafe.Pointer)(p.p)}
  98. }
  99. // PointerSlice loads []*T from p as a []pointer.
  100. // The value returned is aliased with the original slice.
  101. // This behavior differs from the implementation in pointer_reflect.go.
  102. func (p pointer) PointerSlice() []pointer {
  103. // Super-tricky - p should point to a []*T where T is a
  104. // message type. We load it as []pointer.
  105. return *(*[]pointer)(p.p)
  106. }
  107. // AppendPointerSlice appends v to p, which must be a []*T.
  108. func (p pointer) AppendPointerSlice(v pointer) {
  109. *(*[]pointer)(p.p) = append(*(*[]pointer)(p.p), v)
  110. }
  111. // SetPointer sets *p to v.
  112. func (p pointer) SetPointer(v pointer) {
  113. *(*unsafe.Pointer)(p.p) = (unsafe.Pointer)(v.p)
  114. }
  115. func (p pointer) growBoolSlice(addCap int) {
  116. sp := p.BoolSlice()
  117. s := make([]bool, 0, addCap+len(*sp))
  118. s = s[:len(*sp)]
  119. copy(s, *sp)
  120. *sp = s
  121. }
  122. func (p pointer) growInt32Slice(addCap int) {
  123. sp := p.Int32Slice()
  124. s := make([]int32, 0, addCap+len(*sp))
  125. s = s[:len(*sp)]
  126. copy(s, *sp)
  127. *sp = s
  128. }
  129. func (p pointer) growUint32Slice(addCap int) {
  130. p.growInt32Slice(addCap)
  131. }
  132. func (p pointer) growFloat32Slice(addCap int) {
  133. p.growInt32Slice(addCap)
  134. }
  135. func (p pointer) growInt64Slice(addCap int) {
  136. sp := p.Int64Slice()
  137. s := make([]int64, 0, addCap+len(*sp))
  138. s = s[:len(*sp)]
  139. copy(s, *sp)
  140. *sp = s
  141. }
  142. func (p pointer) growUint64Slice(addCap int) {
  143. p.growInt64Slice(addCap)
  144. }
  145. func (p pointer) growFloat64Slice(addCap int) {
  146. p.growInt64Slice(addCap)
  147. }
  148. // Static check that MessageState does not exceed the size of a pointer.
  149. const _ = uint(unsafe.Sizeof(unsafe.Pointer(nil)) - unsafe.Sizeof(MessageState{}))
  150. func (Export) MessageStateOf(p Pointer) *messageState {
  151. // Super-tricky - see documentation on MessageState.
  152. return (*messageState)(unsafe.Pointer(p))
  153. }
  154. func (ms *messageState) pointer() pointer {
  155. // Super-tricky - see documentation on MessageState.
  156. return pointer{p: unsafe.Pointer(ms)}
  157. }
  158. func (ms *messageState) messageInfo() *MessageInfo {
  159. mi := ms.LoadMessageInfo()
  160. if mi == nil {
  161. panic("invalid nil message info; this suggests memory corruption due to a race or shallow copy on the message struct")
  162. }
  163. return mi
  164. }
  165. func (ms *messageState) LoadMessageInfo() *MessageInfo {
  166. return (*MessageInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&ms.atomicMessageInfo))))
  167. }
  168. func (ms *messageState) StoreMessageInfo(mi *MessageInfo) {
  169. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&ms.atomicMessageInfo)), unsafe.Pointer(mi))
  170. }
  171. type atomicNilMessage struct{ p unsafe.Pointer } // p is a *messageReflectWrapper
  172. func (m *atomicNilMessage) Init(mi *MessageInfo) *messageReflectWrapper {
  173. if p := atomic.LoadPointer(&m.p); p != nil {
  174. return (*messageReflectWrapper)(p)
  175. }
  176. w := &messageReflectWrapper{mi: mi}
  177. atomic.CompareAndSwapPointer(&m.p, nil, (unsafe.Pointer)(w))
  178. return (*messageReflectWrapper)(atomic.LoadPointer(&m.p))
  179. }