pointer_unsafe.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright 2012 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. // +build !purego
  5. // This file contains the implementation of the proto field accesses using package unsafe.
  6. package proto
  7. import (
  8. "reflect"
  9. "sync/atomic"
  10. "unsafe"
  11. )
  12. const unsafeAllowed = true
  13. // A field identifies a field in a struct, accessible from a pointer.
  14. // In this implementation, a field is identified by its byte offset from the start of the struct.
  15. type field uintptr
  16. // toField returns a field equivalent to the given reflect field.
  17. func toField(f *reflect.StructField) field {
  18. return field(f.Offset)
  19. }
  20. // invalidField is an invalid field identifier.
  21. const invalidField = ^field(0)
  22. // zeroField is a noop when calling pointer.offset.
  23. const zeroField = field(0)
  24. // IsValid reports whether the field identifier is valid.
  25. func (f field) IsValid() bool {
  26. return f != invalidField
  27. }
  28. // The pointer type below is for the new table-driven encoder/decoder.
  29. // The implementation here uses unsafe.Pointer to create a generic pointer.
  30. // In pointer_reflect.go we use reflect instead of unsafe to implement
  31. // the same (but slower) interface.
  32. type pointer struct {
  33. p unsafe.Pointer
  34. }
  35. // size of pointer
  36. var ptrSize = unsafe.Sizeof(uintptr(0))
  37. // toPointer converts an interface of pointer type to a pointer
  38. // that points to the same target.
  39. func toPointer(i *Message) pointer {
  40. // Super-tricky - read pointer out of data word of interface value.
  41. // Saves ~25ns over the equivalent:
  42. // return valToPointer(reflect.ValueOf(*i))
  43. return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
  44. }
  45. // toAddrPointer converts an interface to a pointer that points to
  46. // the interface data.
  47. func toAddrPointer(i *interface{}, isptr, deref bool) (p pointer) {
  48. // Super-tricky - read or get the address of data word of interface value.
  49. if isptr {
  50. // The interface is of pointer type, thus it is a direct interface.
  51. // The data word is the pointer data itself. We take its address.
  52. p = pointer{p: unsafe.Pointer(uintptr(unsafe.Pointer(i)) + ptrSize)}
  53. } else {
  54. // The interface is not of pointer type. The data word is the pointer
  55. // to the data.
  56. p = pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
  57. }
  58. if deref {
  59. p.p = *(*unsafe.Pointer)(p.p)
  60. }
  61. return p
  62. }
  63. // valToPointer converts v to a pointer. v must be of pointer type.
  64. func valToPointer(v reflect.Value) pointer {
  65. return pointer{p: unsafe.Pointer(v.Pointer())}
  66. }
  67. // offset converts from a pointer to a structure to a pointer to
  68. // one of its fields.
  69. func (p pointer) offset(f field) pointer {
  70. // For safety, we should panic if !f.IsValid, however calling panic causes
  71. // this to no longer be inlineable, which is a serious performance cost.
  72. /*
  73. if !f.IsValid() {
  74. panic("invalid field")
  75. }
  76. */
  77. return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
  78. }
  79. func (p pointer) isNil() bool {
  80. return p.p == nil
  81. }
  82. func (p pointer) toInt64() *int64 {
  83. return (*int64)(p.p)
  84. }
  85. func (p pointer) toInt64Ptr() **int64 {
  86. return (**int64)(p.p)
  87. }
  88. func (p pointer) toInt64Slice() *[]int64 {
  89. return (*[]int64)(p.p)
  90. }
  91. func (p pointer) toInt32() *int32 {
  92. return (*int32)(p.p)
  93. }
  94. // See pointer_reflect.go for why toInt32Ptr/Slice doesn't exist.
  95. /*
  96. func (p pointer) toInt32Ptr() **int32 {
  97. return (**int32)(p.p)
  98. }
  99. func (p pointer) toInt32Slice() *[]int32 {
  100. return (*[]int32)(p.p)
  101. }
  102. */
  103. func (p pointer) getInt32Ptr() *int32 {
  104. return *(**int32)(p.p)
  105. }
  106. func (p pointer) setInt32Ptr(v int32) {
  107. *(**int32)(p.p) = &v
  108. }
  109. // getInt32Slice loads a []int32 from p.
  110. // The value returned is aliased with the original slice.
  111. // This behavior differs from the implementation in pointer_reflect.go.
  112. func (p pointer) getInt32Slice() []int32 {
  113. return *(*[]int32)(p.p)
  114. }
  115. // setInt32Slice stores a []int32 to p.
  116. // The value set is aliased with the input slice.
  117. // This behavior differs from the implementation in pointer_reflect.go.
  118. func (p pointer) setInt32Slice(v []int32) {
  119. *(*[]int32)(p.p) = v
  120. }
  121. // TODO: Can we get rid of appendInt32Slice and use setInt32Slice instead?
  122. func (p pointer) appendInt32Slice(v int32) {
  123. s := (*[]int32)(p.p)
  124. *s = append(*s, v)
  125. }
  126. func (p pointer) toUint64() *uint64 {
  127. return (*uint64)(p.p)
  128. }
  129. func (p pointer) toUint64Ptr() **uint64 {
  130. return (**uint64)(p.p)
  131. }
  132. func (p pointer) toUint64Slice() *[]uint64 {
  133. return (*[]uint64)(p.p)
  134. }
  135. func (p pointer) toUint32() *uint32 {
  136. return (*uint32)(p.p)
  137. }
  138. func (p pointer) toUint32Ptr() **uint32 {
  139. return (**uint32)(p.p)
  140. }
  141. func (p pointer) toUint32Slice() *[]uint32 {
  142. return (*[]uint32)(p.p)
  143. }
  144. func (p pointer) toBool() *bool {
  145. return (*bool)(p.p)
  146. }
  147. func (p pointer) toBoolPtr() **bool {
  148. return (**bool)(p.p)
  149. }
  150. func (p pointer) toBoolSlice() *[]bool {
  151. return (*[]bool)(p.p)
  152. }
  153. func (p pointer) toFloat64() *float64 {
  154. return (*float64)(p.p)
  155. }
  156. func (p pointer) toFloat64Ptr() **float64 {
  157. return (**float64)(p.p)
  158. }
  159. func (p pointer) toFloat64Slice() *[]float64 {
  160. return (*[]float64)(p.p)
  161. }
  162. func (p pointer) toFloat32() *float32 {
  163. return (*float32)(p.p)
  164. }
  165. func (p pointer) toFloat32Ptr() **float32 {
  166. return (**float32)(p.p)
  167. }
  168. func (p pointer) toFloat32Slice() *[]float32 {
  169. return (*[]float32)(p.p)
  170. }
  171. func (p pointer) toString() *string {
  172. return (*string)(p.p)
  173. }
  174. func (p pointer) toStringPtr() **string {
  175. return (**string)(p.p)
  176. }
  177. func (p pointer) toStringSlice() *[]string {
  178. return (*[]string)(p.p)
  179. }
  180. func (p pointer) toBytes() *[]byte {
  181. return (*[]byte)(p.p)
  182. }
  183. func (p pointer) toBytesSlice() *[][]byte {
  184. return (*[][]byte)(p.p)
  185. }
  186. func (p pointer) toExtensions() *XXX_InternalExtensions {
  187. return (*XXX_InternalExtensions)(p.p)
  188. }
  189. func (p pointer) toOldExtensions() *map[int32]Extension {
  190. return (*map[int32]Extension)(p.p)
  191. }
  192. // getPointerSlice loads []*T from p as a []pointer.
  193. // The value returned is aliased with the original slice.
  194. // This behavior differs from the implementation in pointer_reflect.go.
  195. func (p pointer) getPointerSlice() []pointer {
  196. // Super-tricky - p should point to a []*T where T is a
  197. // message type. We load it as []pointer.
  198. return *(*[]pointer)(p.p)
  199. }
  200. // setPointerSlice stores []pointer into p as a []*T.
  201. // The value set is aliased with the input slice.
  202. // This behavior differs from the implementation in pointer_reflect.go.
  203. func (p pointer) setPointerSlice(v []pointer) {
  204. // Super-tricky - p should point to a []*T where T is a
  205. // message type. We store it as []pointer.
  206. *(*[]pointer)(p.p) = v
  207. }
  208. // getPointer loads the pointer at p and returns it.
  209. func (p pointer) getPointer() pointer {
  210. return pointer{p: *(*unsafe.Pointer)(p.p)}
  211. }
  212. // setPointer stores the pointer q at p.
  213. func (p pointer) setPointer(q pointer) {
  214. *(*unsafe.Pointer)(p.p) = q.p
  215. }
  216. // append q to the slice pointed to by p.
  217. func (p pointer) appendPointer(q pointer) {
  218. s := (*[]unsafe.Pointer)(p.p)
  219. *s = append(*s, q.p)
  220. }
  221. // getInterfacePointer returns a pointer that points to the
  222. // interface data of the interface pointed by p.
  223. func (p pointer) getInterfacePointer() pointer {
  224. // Super-tricky - read pointer out of data word of interface value.
  225. return pointer{p: (*(*[2]unsafe.Pointer)(p.p))[1]}
  226. }
  227. // asPointerTo returns a reflect.Value that is a pointer to an
  228. // object of type t stored at p.
  229. func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
  230. return reflect.NewAt(t, p.p)
  231. }
  232. func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
  233. return (*unmarshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  234. }
  235. func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
  236. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  237. }
  238. func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
  239. return (*marshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  240. }
  241. func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
  242. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  243. }
  244. func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
  245. return (*mergeInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  246. }
  247. func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
  248. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  249. }
  250. func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
  251. return (*discardInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  252. }
  253. func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
  254. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  255. }