You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pointer_unsafe.go 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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, deref bool) (p 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. p = pointer{p: unsafe.Pointer(uintptr(unsafe.Pointer(i)) + ptrSize)}
  80. } else {
  81. // The interface is not of pointer type. The data word is the pointer
  82. // to the data.
  83. p = pointer{p: (*[2]unsafe.Pointer)(unsafe.Pointer(i))[1]}
  84. }
  85. if deref {
  86. p.p = *(*unsafe.Pointer)(p.p)
  87. }
  88. return p
  89. }
  90. // valToPointer converts v to a pointer. v must be of pointer type.
  91. func valToPointer(v reflect.Value) pointer {
  92. return pointer{p: unsafe.Pointer(v.Pointer())}
  93. }
  94. // offset converts from a pointer to a structure to a pointer to
  95. // one of its fields.
  96. func (p pointer) offset(f field) pointer {
  97. // For safety, we should panic if !f.IsValid, however calling panic causes
  98. // this to no longer be inlineable, which is a serious performance cost.
  99. /*
  100. if !f.IsValid() {
  101. panic("invalid field")
  102. }
  103. */
  104. return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
  105. }
  106. func (p pointer) isNil() bool {
  107. return p.p == nil
  108. }
  109. func (p pointer) toInt64() *int64 {
  110. return (*int64)(p.p)
  111. }
  112. func (p pointer) toInt64Ptr() **int64 {
  113. return (**int64)(p.p)
  114. }
  115. func (p pointer) toInt64Slice() *[]int64 {
  116. return (*[]int64)(p.p)
  117. }
  118. func (p pointer) toInt32() *int32 {
  119. return (*int32)(p.p)
  120. }
  121. // See pointer_reflect.go for why toInt32Ptr/Slice doesn't exist.
  122. /*
  123. func (p pointer) toInt32Ptr() **int32 {
  124. return (**int32)(p.p)
  125. }
  126. func (p pointer) toInt32Slice() *[]int32 {
  127. return (*[]int32)(p.p)
  128. }
  129. */
  130. func (p pointer) getInt32Ptr() *int32 {
  131. return *(**int32)(p.p)
  132. }
  133. func (p pointer) setInt32Ptr(v int32) {
  134. *(**int32)(p.p) = &v
  135. }
  136. // getInt32Slice loads a []int32 from p.
  137. // The value returned is aliased with the original slice.
  138. // This behavior differs from the implementation in pointer_reflect.go.
  139. func (p pointer) getInt32Slice() []int32 {
  140. return *(*[]int32)(p.p)
  141. }
  142. // setInt32Slice stores a []int32 to p.
  143. // The value set is aliased with the input slice.
  144. // This behavior differs from the implementation in pointer_reflect.go.
  145. func (p pointer) setInt32Slice(v []int32) {
  146. *(*[]int32)(p.p) = v
  147. }
  148. // TODO: Can we get rid of appendInt32Slice and use setInt32Slice instead?
  149. func (p pointer) appendInt32Slice(v int32) {
  150. s := (*[]int32)(p.p)
  151. *s = append(*s, v)
  152. }
  153. func (p pointer) toUint64() *uint64 {
  154. return (*uint64)(p.p)
  155. }
  156. func (p pointer) toUint64Ptr() **uint64 {
  157. return (**uint64)(p.p)
  158. }
  159. func (p pointer) toUint64Slice() *[]uint64 {
  160. return (*[]uint64)(p.p)
  161. }
  162. func (p pointer) toUint32() *uint32 {
  163. return (*uint32)(p.p)
  164. }
  165. func (p pointer) toUint32Ptr() **uint32 {
  166. return (**uint32)(p.p)
  167. }
  168. func (p pointer) toUint32Slice() *[]uint32 {
  169. return (*[]uint32)(p.p)
  170. }
  171. func (p pointer) toBool() *bool {
  172. return (*bool)(p.p)
  173. }
  174. func (p pointer) toBoolPtr() **bool {
  175. return (**bool)(p.p)
  176. }
  177. func (p pointer) toBoolSlice() *[]bool {
  178. return (*[]bool)(p.p)
  179. }
  180. func (p pointer) toFloat64() *float64 {
  181. return (*float64)(p.p)
  182. }
  183. func (p pointer) toFloat64Ptr() **float64 {
  184. return (**float64)(p.p)
  185. }
  186. func (p pointer) toFloat64Slice() *[]float64 {
  187. return (*[]float64)(p.p)
  188. }
  189. func (p pointer) toFloat32() *float32 {
  190. return (*float32)(p.p)
  191. }
  192. func (p pointer) toFloat32Ptr() **float32 {
  193. return (**float32)(p.p)
  194. }
  195. func (p pointer) toFloat32Slice() *[]float32 {
  196. return (*[]float32)(p.p)
  197. }
  198. func (p pointer) toString() *string {
  199. return (*string)(p.p)
  200. }
  201. func (p pointer) toStringPtr() **string {
  202. return (**string)(p.p)
  203. }
  204. func (p pointer) toStringSlice() *[]string {
  205. return (*[]string)(p.p)
  206. }
  207. func (p pointer) toBytes() *[]byte {
  208. return (*[]byte)(p.p)
  209. }
  210. func (p pointer) toBytesSlice() *[][]byte {
  211. return (*[][]byte)(p.p)
  212. }
  213. func (p pointer) toExtensions() *XXX_InternalExtensions {
  214. return (*XXX_InternalExtensions)(p.p)
  215. }
  216. func (p pointer) toOldExtensions() *map[int32]Extension {
  217. return (*map[int32]Extension)(p.p)
  218. }
  219. // getPointerSlice loads []*T from p as a []pointer.
  220. // The value returned is aliased with the original slice.
  221. // This behavior differs from the implementation in pointer_reflect.go.
  222. func (p pointer) getPointerSlice() []pointer {
  223. // Super-tricky - p should point to a []*T where T is a
  224. // message type. We load it as []pointer.
  225. return *(*[]pointer)(p.p)
  226. }
  227. // setPointerSlice stores []pointer into p as a []*T.
  228. // The value set is aliased with the input slice.
  229. // This behavior differs from the implementation in pointer_reflect.go.
  230. func (p pointer) setPointerSlice(v []pointer) {
  231. // Super-tricky - p should point to a []*T where T is a
  232. // message type. We store it as []pointer.
  233. *(*[]pointer)(p.p) = v
  234. }
  235. // getPointer loads the pointer at p and returns it.
  236. func (p pointer) getPointer() pointer {
  237. return pointer{p: *(*unsafe.Pointer)(p.p)}
  238. }
  239. // setPointer stores the pointer q at p.
  240. func (p pointer) setPointer(q pointer) {
  241. *(*unsafe.Pointer)(p.p) = q.p
  242. }
  243. // append q to the slice pointed to by p.
  244. func (p pointer) appendPointer(q pointer) {
  245. s := (*[]unsafe.Pointer)(p.p)
  246. *s = append(*s, q.p)
  247. }
  248. // getInterfacePointer returns a pointer that points to the
  249. // interface data of the interface pointed by p.
  250. func (p pointer) getInterfacePointer() pointer {
  251. // Super-tricky - read pointer out of data word of interface value.
  252. return pointer{p: (*(*[2]unsafe.Pointer)(p.p))[1]}
  253. }
  254. // asPointerTo returns a reflect.Value that is a pointer to an
  255. // object of type t stored at p.
  256. func (p pointer) asPointerTo(t reflect.Type) reflect.Value {
  257. return reflect.NewAt(t, p.p)
  258. }
  259. func atomicLoadUnmarshalInfo(p **unmarshalInfo) *unmarshalInfo {
  260. return (*unmarshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  261. }
  262. func atomicStoreUnmarshalInfo(p **unmarshalInfo, v *unmarshalInfo) {
  263. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  264. }
  265. func atomicLoadMarshalInfo(p **marshalInfo) *marshalInfo {
  266. return (*marshalInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  267. }
  268. func atomicStoreMarshalInfo(p **marshalInfo, v *marshalInfo) {
  269. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  270. }
  271. func atomicLoadMergeInfo(p **mergeInfo) *mergeInfo {
  272. return (*mergeInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  273. }
  274. func atomicStoreMergeInfo(p **mergeInfo, v *mergeInfo) {
  275. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  276. }
  277. func atomicLoadDiscardInfo(p **discardInfo) *discardInfo {
  278. return (*discardInfo)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  279. }
  280. func atomicStoreDiscardInfo(p **discardInfo, v *discardInfo) {
  281. atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(p)), unsafe.Pointer(v))
  282. }