pointer_unsafe.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. "unsafe"
  37. )
  38. // NOTE: These type_Foo functions would more idiomatically be methods,
  39. // but Go does not allow methods on pointer types, and we must preserve
  40. // some pointer type for the garbage collector. We use these
  41. // funcs with clunky names as our poor approximation to methods.
  42. //
  43. // An alternative would be
  44. // type structPointer struct { p unsafe.Pointer }
  45. // but that does not registerize as well.
  46. // A structPointer is a pointer to a struct.
  47. type structPointer unsafe.Pointer
  48. // toStructPointer returns a structPointer equivalent to the given reflect value.
  49. func toStructPointer(v reflect.Value) structPointer {
  50. return structPointer(unsafe.Pointer(v.Pointer()))
  51. }
  52. // IsNil reports whether p is nil.
  53. func structPointer_IsNil(p structPointer) bool {
  54. return p == nil
  55. }
  56. // Interface returns the struct pointer, assumed to have element type t,
  57. // as an interface value.
  58. func structPointer_Interface(p structPointer, t reflect.Type) interface{} {
  59. return reflect.NewAt(t, unsafe.Pointer(p)).Interface()
  60. }
  61. // A field identifies a field in a struct, accessible from a structPointer.
  62. // In this implementation, a field is identified by its byte offset from the start of the struct.
  63. type field uintptr
  64. // toField returns a field equivalent to the given reflect field.
  65. func toField(f *reflect.StructField) field {
  66. return field(f.Offset)
  67. }
  68. // invalidField is an invalid field identifier.
  69. const invalidField = ^field(0)
  70. // IsValid reports whether the field identifier is valid.
  71. func (f field) IsValid() bool {
  72. return f != ^field(0)
  73. }
  74. // Bytes returns the address of a []byte field in the struct.
  75. func structPointer_Bytes(p structPointer, f field) *[]byte {
  76. return (*[]byte)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  77. }
  78. // BytesSlice returns the address of a [][]byte field in the struct.
  79. func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
  80. return (*[][]byte)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  81. }
  82. // Bool returns the address of a *bool field in the struct.
  83. func structPointer_Bool(p structPointer, f field) **bool {
  84. return (**bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  85. }
  86. // BoolVal returns the address of a bool field in the struct.
  87. func structPointer_BoolVal(p structPointer, f field) *bool {
  88. return (*bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  89. }
  90. // BoolSlice returns the address of a []bool field in the struct.
  91. func structPointer_BoolSlice(p structPointer, f field) *[]bool {
  92. return (*[]bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  93. }
  94. // String returns the address of a *string field in the struct.
  95. func structPointer_String(p structPointer, f field) **string {
  96. return (**string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  97. }
  98. // StringVal returns the address of a string field in the struct.
  99. func structPointer_StringVal(p structPointer, f field) *string {
  100. return (*string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  101. }
  102. // StringSlice returns the address of a []string field in the struct.
  103. func structPointer_StringSlice(p structPointer, f field) *[]string {
  104. return (*[]string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  105. }
  106. // ExtMap returns the address of an extension map field in the struct.
  107. func structPointer_Extensions(p structPointer, f field) *XXX_InternalExtensions {
  108. return (*XXX_InternalExtensions)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  109. }
  110. func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
  111. return (*map[int32]Extension)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  112. }
  113. // NewAt returns the reflect.Value for a pointer to a field in the struct.
  114. func structPointer_NewAt(p structPointer, f field, typ reflect.Type) reflect.Value {
  115. return reflect.NewAt(typ, unsafe.Pointer(uintptr(p)+uintptr(f)))
  116. }
  117. // SetStructPointer writes a *struct field in the struct.
  118. func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
  119. *(*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f))) = q
  120. }
  121. // GetStructPointer reads a *struct field in the struct.
  122. func structPointer_GetStructPointer(p structPointer, f field) structPointer {
  123. return *(*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  124. }
  125. // StructPointerSlice the address of a []*struct field in the struct.
  126. func structPointer_StructPointerSlice(p structPointer, f field) *structPointerSlice {
  127. return (*structPointerSlice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  128. }
  129. // A structPointerSlice represents a slice of pointers to structs (themselves submessages or groups).
  130. type structPointerSlice []structPointer
  131. func (v *structPointerSlice) Len() int { return len(*v) }
  132. func (v *structPointerSlice) Index(i int) structPointer { return (*v)[i] }
  133. func (v *structPointerSlice) Append(p structPointer) { *v = append(*v, p) }
  134. // A word32 is the address of a "pointer to 32-bit value" field.
  135. type word32 **uint32
  136. // IsNil reports whether *v is nil.
  137. func word32_IsNil(p word32) bool {
  138. return *p == nil
  139. }
  140. // Set sets *v to point at a newly allocated word set to x.
  141. func word32_Set(p word32, o *Buffer, x uint32) {
  142. if len(o.uint32s) == 0 {
  143. o.uint32s = make([]uint32, uint32PoolSize)
  144. }
  145. o.uint32s[0] = x
  146. *p = &o.uint32s[0]
  147. o.uint32s = o.uint32s[1:]
  148. }
  149. // Get gets the value pointed at by *v.
  150. func word32_Get(p word32) uint32 {
  151. return **p
  152. }
  153. // Word32 returns the address of a *int32, *uint32, *float32, or *enum field in the struct.
  154. func structPointer_Word32(p structPointer, f field) word32 {
  155. return word32((**uint32)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  156. }
  157. // A word32Val is the address of a 32-bit value field.
  158. type word32Val *uint32
  159. // Set sets *p to x.
  160. func word32Val_Set(p word32Val, x uint32) {
  161. *p = x
  162. }
  163. // Get gets the value pointed at by p.
  164. func word32Val_Get(p word32Val) uint32 {
  165. return *p
  166. }
  167. // Word32Val returns the address of a *int32, *uint32, *float32, or *enum field in the struct.
  168. func structPointer_Word32Val(p structPointer, f field) word32Val {
  169. return word32Val((*uint32)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  170. }
  171. // A word32Slice is a slice of 32-bit values.
  172. type word32Slice []uint32
  173. func (v *word32Slice) Append(x uint32) { *v = append(*v, x) }
  174. func (v *word32Slice) Len() int { return len(*v) }
  175. func (v *word32Slice) Index(i int) uint32 { return (*v)[i] }
  176. // Word32Slice returns the address of a []int32, []uint32, []float32, or []enum field in the struct.
  177. func structPointer_Word32Slice(p structPointer, f field) *word32Slice {
  178. return (*word32Slice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  179. }
  180. // word64 is like word32 but for 64-bit values.
  181. type word64 **uint64
  182. func word64_Set(p word64, o *Buffer, x uint64) {
  183. if len(o.uint64s) == 0 {
  184. o.uint64s = make([]uint64, uint64PoolSize)
  185. }
  186. o.uint64s[0] = x
  187. *p = &o.uint64s[0]
  188. o.uint64s = o.uint64s[1:]
  189. }
  190. func word64_IsNil(p word64) bool {
  191. return *p == nil
  192. }
  193. func word64_Get(p word64) uint64 {
  194. return **p
  195. }
  196. func structPointer_Word64(p structPointer, f field) word64 {
  197. return word64((**uint64)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  198. }
  199. // word64Val is like word32Val but for 64-bit values.
  200. type word64Val *uint64
  201. func word64Val_Set(p word64Val, o *Buffer, x uint64) {
  202. *p = x
  203. }
  204. func word64Val_Get(p word64Val) uint64 {
  205. return *p
  206. }
  207. func structPointer_Word64Val(p structPointer, f field) word64Val {
  208. return word64Val((*uint64)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  209. }
  210. // word64Slice is like word32Slice but for 64-bit values.
  211. type word64Slice []uint64
  212. func (v *word64Slice) Append(x uint64) { *v = append(*v, x) }
  213. func (v *word64Slice) Len() int { return len(*v) }
  214. func (v *word64Slice) Index(i int) uint64 { return (*v)[i] }
  215. func structPointer_Word64Slice(p structPointer, f field) *word64Slice {
  216. return (*word64Slice)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  217. }