您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

176 行
6.9 KiB

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