pointer_unsafe.go 9.4 KB

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