pointer_unsafe_gogo.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. for j := 0; j < size; j++ {
  48. oldb := (*byte)(unsafe.Pointer(oldptr + uintptr(j)))
  49. *(*byte)(unsafe.Pointer(newptr + uintptr(j))) = *oldb
  50. }
  51. }
  52. func structPointer_Copy(oldptr structPointer, newptr structPointer, size int) {
  53. copyUintPtr(uintptr(oldptr), uintptr(newptr), size)
  54. }
  55. func appendStructPointer(base structPointer, f field, typ reflect.Type) structPointer {
  56. size := typ.Elem().Size()
  57. oldHeader := structPointer_GetSliceHeader(base, f)
  58. newLen := oldHeader.Len + 1
  59. slice := reflect.MakeSlice(typ, newLen, newLen)
  60. bas := toStructPointer(slice)
  61. for i := 0; i < oldHeader.Len; i++ {
  62. newElemptr := uintptr(bas) + uintptr(i)*size
  63. oldElemptr := oldHeader.Data + uintptr(i)*size
  64. copyUintPtr(oldElemptr, newElemptr, int(size))
  65. }
  66. oldHeader.Data = uintptr(bas)
  67. oldHeader.Len = newLen
  68. oldHeader.Cap = newLen
  69. return structPointer(unsafe.Pointer(uintptr(unsafe.Pointer(bas)) + uintptr(uintptr(newLen-1)*size)))
  70. }
  71. // RefBool returns a *bool field in the struct.
  72. func structPointer_RefBool(p structPointer, f field) *bool {
  73. return (*bool)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  74. }
  75. // RefString returns the address of a string field in the struct.
  76. func structPointer_RefString(p structPointer, f field) *string {
  77. return (*string)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  78. }
  79. func structPointer_FieldPointer(p structPointer, f field) structPointer {
  80. return structPointer(unsafe.Pointer(uintptr(p) + uintptr(f)))
  81. }
  82. func structPointer_GetRefStructPointer(p structPointer, f field) structPointer {
  83. return structPointer((*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  84. }
  85. func structPointer_GetSliceHeader(p structPointer, f field) *reflect.SliceHeader {
  86. return (*reflect.SliceHeader)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  87. }
  88. func structPointer_Add(p structPointer, size field) structPointer {
  89. return structPointer(unsafe.Pointer(uintptr(p) + uintptr(size)))
  90. }
  91. func structPointer_Len(p structPointer, f field) int {
  92. return len(*(*[]interface{})(unsafe.Pointer(structPointer_GetRefStructPointer(p, f))))
  93. }
  94. // refWord32 is the address of a 32-bit value field.
  95. type refWord32 *uint32
  96. func refWord32_IsNil(p refWord32) bool {
  97. return p == nil
  98. }
  99. func refWord32_Set(p refWord32, o *Buffer, x uint32) {
  100. if len(o.uint32s) == 0 {
  101. o.uint32s = make([]uint32, uint32PoolSize)
  102. }
  103. o.uint32s[0] = x
  104. *p = o.uint32s[0]
  105. o.uint32s = o.uint32s[1:]
  106. }
  107. func refWord32_Get(p refWord32) uint32 {
  108. return *p
  109. }
  110. func structPointer_RefWord32(p structPointer, f field) refWord32 {
  111. return refWord32((*uint32)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  112. }
  113. // refWord64 is like refWord32 but for 32-bit values.
  114. type refWord64 *uint64
  115. func refWord64_Set(p refWord64, o *Buffer, x uint64) {
  116. if len(o.uint64s) == 0 {
  117. o.uint64s = make([]uint64, uint64PoolSize)
  118. }
  119. o.uint64s[0] = x
  120. *p = o.uint64s[0]
  121. o.uint64s = o.uint64s[1:]
  122. }
  123. func refWord64_IsNil(p refWord64) bool {
  124. return p == nil
  125. }
  126. func refWord64_Get(p refWord64) uint64 {
  127. return *p
  128. }
  129. func structPointer_RefWord64(p structPointer, f field) refWord64 {
  130. return refWord64((*uint64)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  131. }