pointer_unsafe.go 8.8 KB

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