pointer_unsafe_gogo.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
  2. // http://code.google.com/p/gogoprotobuf/gogoproto
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. // +build !appengine
  27. // This file contains the implementation of the proto field accesses using package unsafe.
  28. package proto
  29. import (
  30. "reflect"
  31. "unsafe"
  32. )
  33. func structPointer_InterfaceAt(p structPointer, f field, t reflect.Type) interface{} {
  34. point := unsafe.Pointer(uintptr(p) + uintptr(f))
  35. r := reflect.NewAt(t, point)
  36. return r.Interface()
  37. }
  38. func structPointer_InterfaceRef(p structPointer, f field, t reflect.Type) interface{} {
  39. point := unsafe.Pointer(uintptr(p) + uintptr(f))
  40. r := reflect.NewAt(t, point)
  41. if r.Elem().IsNil() {
  42. return nil
  43. }
  44. return r.Elem().Interface()
  45. }
  46. func copyUintPtr(oldptr, newptr uintptr, size int) {
  47. oldbytes := make([]byte, 0)
  48. oldslice := (*reflect.SliceHeader)(unsafe.Pointer(&oldbytes))
  49. oldslice.Data = oldptr
  50. oldslice.Len = size
  51. oldslice.Cap = size
  52. newbytes := make([]byte, 0)
  53. newslice := (*reflect.SliceHeader)(unsafe.Pointer(&newbytes))
  54. newslice.Data = newptr
  55. newslice.Len = size
  56. newslice.Cap = size
  57. copy(newbytes, oldbytes)
  58. }
  59. func structPointer_Copy(oldptr structPointer, newptr structPointer, size int) {
  60. copyUintPtr(uintptr(oldptr), uintptr(newptr), size)
  61. }
  62. func appendStructPointer(base structPointer, f field, typ reflect.Type) structPointer {
  63. size := typ.Elem().Size()
  64. oldHeader := structPointer_GetSliceHeader(base, f)
  65. newLen := oldHeader.Len + 1
  66. slice := reflect.MakeSlice(typ, newLen, newLen)
  67. bas := toStructPointer(slice)
  68. for i := 0; i < oldHeader.Len; i++ {
  69. newElemptr := uintptr(bas) + uintptr(i)*size
  70. oldElemptr := oldHeader.Data + uintptr(i)*size
  71. copyUintPtr(oldElemptr, newElemptr, int(size))
  72. }
  73. oldHeader.Data = uintptr(bas)
  74. oldHeader.Len = newLen
  75. oldHeader.Cap = newLen
  76. return structPointer(unsafe.Pointer(uintptr(unsafe.Pointer(bas)) + uintptr(uintptr(newLen-1)*size)))
  77. }
  78. // RefBool returns a *bool field in the struct.
  79. func structPointer_RefBool(p structPointer, f field) *bool {
  80. return (*bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  81. }
  82. // RefString returns the address of a string field in the struct.
  83. func structPointer_RefString(p structPointer, f field) *string {
  84. return (*string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  85. }
  86. func structPointer_FieldPointer(p structPointer, f field) structPointer {
  87. return structPointer(unsafe.Pointer(uintptr(p) + uintptr(f)))
  88. }
  89. func structPointer_GetRefStructPointer(p structPointer, f field) structPointer {
  90. return structPointer((*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  91. }
  92. func structPointer_GetSliceHeader(p structPointer, f field) *reflect.SliceHeader {
  93. return (*reflect.SliceHeader)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  94. }
  95. func structPointer_Add(p structPointer, size field) structPointer {
  96. return structPointer(unsafe.Pointer(uintptr(p) + uintptr(size)))
  97. }
  98. func structPointer_Len(p structPointer, f field) int {
  99. return len(*(*[]interface{})(unsafe.Pointer(structPointer_GetRefStructPointer(p, f))))
  100. }
  101. // refWord32 is the address of a 32-bit value field.
  102. type refWord32 *uint32
  103. func refWord32_IsNil(p refWord32) bool {
  104. return p == nil
  105. }
  106. func refWord32_Set(p refWord32, o *Buffer, x uint32) {
  107. if len(o.uint32s) == 0 {
  108. o.uint32s = make([]uint32, uint32PoolSize)
  109. }
  110. o.uint32s[0] = x
  111. *p = o.uint32s[0]
  112. o.uint32s = o.uint32s[1:]
  113. }
  114. func refWord32_Get(p refWord32) uint32 {
  115. return *p
  116. }
  117. func structPointer_RefWord32(p structPointer, f field) refWord32 {
  118. return refWord32((*uint32)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  119. }
  120. // refWord64 is like refWord32 but for 32-bit values.
  121. type refWord64 *uint64
  122. func refWord64_Set(p refWord64, o *Buffer, x uint64) {
  123. if len(o.uint64s) == 0 {
  124. o.uint64s = make([]uint64, uint64PoolSize)
  125. }
  126. o.uint64s[0] = x
  127. *p = o.uint64s[0]
  128. o.uint64s = o.uint64s[1:]
  129. }
  130. func refWord64_IsNil(p refWord64) bool {
  131. return p == nil
  132. }
  133. func refWord64_Get(p refWord64) uint64 {
  134. return *p
  135. }
  136. func structPointer_RefWord64(p structPointer, f field) refWord64 {
  137. return refWord64((*uint64)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  138. }