pointer_unsafe.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2012 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. // +build !purego,!appengine,!js
  32. // This file contains the implementation of the proto field accesses using package unsafe.
  33. package proto
  34. import (
  35. "reflect"
  36. "sync/atomic"
  37. "unsafe"
  38. )
  39. const unsafeAllowed = true
  40. // A field identifies a field in a struct, accessible from a pointer.
  41. // In this implementation, a field is identified by its byte offset from the start of the struct.
  42. type field uintptr
  43. // toField returns a field equivalent to the given reflect field.
  44. func toField(f *reflect.StructField) field {
  45. return field(f.Offset)
  46. }
  47. // invalidField is an invalid field identifier.
  48. const invalidField = ^field(0)
  49. // zeroField is a noop when calling pointer.offset.
  50. const zeroField = field(0)
  51. // IsValid reports whether the field identifier is valid.
  52. func (f field) IsValid() bool {
  53. return f != invalidField
  54. }
  55. // The pointer type below is for the new table-driven encoder/decoder.
  56. // The implementation here uses unsafe.Pointer to create a generic pointer.
  57. // In pointer_reflect.go we use reflect instead of unsafe to implement
  58. // the same (but slower) interface.
  59. type pointer struct {
  60. p unsafe.Pointer
  61. }
  62. // size of pointer
  63. var ptrSize = unsafe.Sizeof(uintptr(0))
  64. // toPointer converts an interface of pointer type to a pointer
  65. // that points to the same target.
  66. func toPointer(i *Message) pointer {
  67. // Super-tricky - read pointer out of data word of interface value.
  68. // Saves ~25ns over the equivalent:
  69. // return valToPointer(reflect.ValueOf(*i))
  70. return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
  71. }
  72. // toAddrPointer converts an interface to a pointer that points to
  73. // the interface data.
  74. func toAddrPointer(i *interface{}, isptr bool) pointer {
  75. // Super-tricky - read or get the address of data word of interface value.
  76. if isptr {
  77. // The interface is of pointer type, thus it is a direct interface.
  78. // The data word is the pointer data itself. We take its address.
  79. return pointer{p: unsafe.Pointer(uintptr(unsafe.Pointer(i)) + ptrSize)}
  80. }
  81. // The interface is not of pointer type. The data word is the pointer
  82. // to the data.
  83. return pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
  84. }
  85. // valToPointer converts v to a pointer. v must be of pointer type.
  86. func valToPointer(v reflect.Value) pointer {
  87. return pointer{p: unsafe.Pointer(v.Pointer())}
  88. }
  89. // offset converts from a pointer to a structure to a pointer to
  90. // one of its fields.
  91. func (p pointer) offset(f field) pointer {
  92. // For safety, we should panic if !f.IsValid, however calling panic causes
  93. // this to no longer be inlineable, which is a serious performance cost.
  94. /*
  95. if !f.IsValid() {
  96. panic("invalid field")
  97. }
  98. */
  99. return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
  100. }
  101. func (p pointer) isNil() bool {
  102. return p.p == nil
  103. }
  104. func (p pointer) toInt64() *int64 {
  105. return (*int64)(p.p)
  106. }
  107. func (p pointer) toInt64Ptr() **int64 {
  108. return (**int64)(p.p)
  109. }
  110. func (p pointer) toInt64Slice() *[]int64 {
  111. return (*[]int64)(p.p)
  112. }
  113. func (p pointer) toInt32() *int32 {
  114. return (*int32)(p.p)
  115. }
  116. // See pointer_reflect.go for why toInt32Ptr/Slice doesn't exist.
  117. /*
  118. func (p pointer) toInt32Ptr() **int32 {
  119. return (**int32)(p.p)
  120. }
  121. func (p pointer) toInt32Slice() *[]int32 {
  122. return (*[]int32)(p.p)
  123. }
  124. */
  125. func (p pointer) getInt32Ptr() *int32 {
  126. return *(**int32)(p.p)
  127. }
  128. func (p pointer) setInt32Ptr(v int32) {
  129. *(**int32)(p.p) = &v
  130. }
  131. // getInt32Slice loads a []int32 from p.
  132. // The value returned is aliased with the original slice.
  133. // This behavior differs from the implementation in pointer_reflect.go.
  134. func (p pointer) getInt32Slice() []int32 {
  135. return *(*[]int32)(p.p)
  136. }
  137. // setInt32Slice stores a []int32 to p.
  138. // The value set is aliased with the input slice.
  139. // This behavior differs from the implementation in pointer_reflect.go.
  140. func (p pointer) setInt32Slice(v []int32) {
  141. *(*[]int32)(p.p) = v
  142. }
  143. // TODO: Can we get rid of appendInt32Slice and use setInt32Slice instead?
  144. func (p pointer) appendInt32Slice(v int32) {
  145. s := (*[]int32)(p.p)
  146. *s = append(*s, v)
  147. }
  148. func (p pointer) toUint64() *uint64 {
  149. return (*uint64)(p.p)
  150. }
  151. func (p pointer) toUint64Ptr() **uint64 {
  152. return (**uint64)(p.p)
  153. }
  154. func (p pointer) toUint64Slice() *[]uint64 {
  155. return (*[]uint64)(p.p)
  156. }
  157. func (p pointer) toUint32() *uint32 {
  158. return (*uint32)(p.p)
  159. }
  160. func (p pointer) toUint32Ptr() **uint32 {
  161. return (**uint32)(p.p)
  162. }
  163. func (p pointer) toUint32Slice() *[]uint32 {
  164. return (*[]uint32)(p.p)
  165. }
  166. func (p pointer) toBool() *bool {
  167. return (*bool)(p.p)
  168. }
  169. func (p pointer) toBoolPtr() **bool {
  170. return (**bool)(p.p)
  171. }
  172. func (p pointer) toBoolSlice() *[]bool {
  173. return (*[]bool)(p.p)
  174. }
  175. func (p pointer) toFloat64() *float64 {
  176. return (*float64)(p.p)
  177. }
  178. func (p pointer) toFloat64Ptr() **float64 {
  179. return (**float64)(p.p)
  180. }
  181. func (p pointer) toFloat64Slice() *[]float64 {
  182. return (*[]float64)(p.p)
  183. }
  184. func (p pointer) toFloat32() *float32 {
  185. return (*float32)(p.p)
  186. }
  187. func (p pointer) toFloat32Ptr() **float32 {
  188. return (**float32)(p.p)
  189. }
  190. func (p pointer) toFloat32Slice() *[]float32 {
  191. return (*[]float32)(p.p)
  192. }
  193. func (p pointer) toString() *string {
  194. return (*string)(p.p)
  195. }
  196. func (p pointer) toStringPtr() **string {
  197. return (**string)(p.p)
  198. }
  199. func (p pointer) toStringSlice() *[]string {
  200. return (*[]string)(p.p)
  201. }
  202. func (p pointer) toBytes() *[]byte {
  203. return (*[]byte)(p.p)
  204. }
  205. func (p pointer) toBytesSlice() *[][]byte {
  206. return (*[][]byte)(p.p)
  207. }
  208. func (p pointer) toExtensions() *XXX_InternalExtensions {
  209. return (*XXX_InternalExtensions)(p.p)
  210. }
  211. func (p pointer) toOldExtensions() *map[int32]Extension {
  212. return (*map[int32]Extension)(p.p)
  213. }
  214. // getPointerSlice loads []*T from p as a []pointer.
  215. // The value returned is aliased with the original slice.
  216. // This behavior differs from the implementation in pointer_reflect.go.
  217. func (p pointer) getPointerSlice() []pointer {
  218. // Super-tricky - p should point to a []*T where T is a
  219. // message type. We load it as []pointer.
  220. return *(*[]pointer)(p.p)
  221. }
  222. // setPointerSlice stores []pointer into p as a []*T.
  223. // The value set is aliased with the input slice.
  224. // This behavior differs from the implementation in pointer_reflect.go.
  225. func (p pointer) setPointerSlice(v []pointer) {
  226. // Super-tricky - p should point to a []*T where T is a
  227. // message type. We store it as []pointer.
  228. *(*[]pointer)(p.p) = v
  229. }
  230. // getPointer loads the pointer at p and returns it.
  231. func (p pointer) getPointer() pointer {
  232. return pointer{p: *(*unsafe.Pointer)(p.p)}
  233. }
  234. // setPointer stores the pointer q at p.
  235. func (p pointer) setPointer(q pointer) {
  236. *(*unsafe.Pointer)(p.p) = q.p
  237. }
  238. // append q to the slice pointed to by p.
  239. func (p pointer) appendPointer(q pointer) {
  240. s := (*[]unsafe.Pointer)(p.p)
  241. *s = append(*s, q.p)
  242. }
  243. // getInterfacePointer returns a pointer that points to the
  244. // interface data of the interface pointed by p.
  245. func (p pointer) getInterfacePointer() pointer {
  246. // Super-tricky - read pointer out of data word of interface value.
  247. return pointer{p: (*(*[2]unsafe.Pointer)(p.p))[1]}
  248. }
  249. // asPointerTo returns a reflect.Value that is a pointer to an
  250. // object of type t stored at p.
  251. func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
  252. return reflect.NewAt(t, p.p)
  253. }
  254. func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
  255. return (*unmarshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  256. }
  257. func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
  258. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  259. }
  260. func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
  261. return (*marshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  262. }
  263. func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
  264. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  265. }
  266. func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
  267. return (*mergeInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  268. }
  269. func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
  270. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  271. }
  272. func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
  273. return (*discardInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  274. }
  275. func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
  276. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  277. }