pointer_unsafe_gogo.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, The GoGo Authors. All rights reserved.
  4. // http://github.com/gogo/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. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. // +build !appengine,!js
  29. // This file contains the implementation of the proto field accesses using package unsafe.
  30. package proto
  31. import (
  32. "reflect"
  33. "unsafe"
  34. )
  35. func structPointer_InterfaceAt(p structPointer, f field, t reflect.Type) interface{} {
  36. point := unsafe.Pointer(uintptr(p) + uintptr(f))
  37. r := reflect.NewAt(t, point)
  38. return r.Interface()
  39. }
  40. func structPointer_InterfaceRef(p structPointer, f field, t reflect.Type) interface{} {
  41. point := unsafe.Pointer(uintptr(p) + uintptr(f))
  42. r := reflect.NewAt(t, point)
  43. if r.Elem().IsNil() {
  44. return nil
  45. }
  46. return r.Elem().Interface()
  47. }
  48. func copyUintPtr(oldptr, newptr uintptr, size int) {
  49. oldbytes := make([]byte, 0)
  50. oldslice := (*reflect.SliceHeader)(unsafe.Pointer(&oldbytes))
  51. oldslice.Data = oldptr
  52. oldslice.Len = size
  53. oldslice.Cap = size
  54. newbytes := make([]byte, 0)
  55. newslice := (*reflect.SliceHeader)(unsafe.Pointer(&newbytes))
  56. newslice.Data = newptr
  57. newslice.Len = size
  58. newslice.Cap = size
  59. copy(newbytes, oldbytes)
  60. }
  61. func structPointer_Copy(oldptr structPointer, newptr structPointer, size int) {
  62. copyUintPtr(uintptr(oldptr), uintptr(newptr), size)
  63. }
  64. func appendStructPointer(base structPointer, f field, typ reflect.Type) structPointer {
  65. size := typ.Elem().Size()
  66. oldHeader := structPointer_GetSliceHeader(base, f)
  67. oldSlice := reflect.NewAt(typ, unsafe.Pointer(oldHeader)).Elem()
  68. newLen := oldHeader.Len + 1
  69. newSlice := reflect.MakeSlice(typ, newLen, newLen)
  70. reflect.Copy(newSlice, oldSlice)
  71. bas := toStructPointer(newSlice)
  72. oldHeader.Data = uintptr(bas)
  73. oldHeader.Len = newLen
  74. oldHeader.Cap = newLen
  75. return structPointer(unsafe.Pointer(uintptr(unsafe.Pointer(bas)) + uintptr(uintptr(newLen-1)*size)))
  76. }
  77. func structPointer_FieldPointer(p structPointer, f field) structPointer {
  78. return structPointer(unsafe.Pointer(uintptr(p) + uintptr(f)))
  79. }
  80. func structPointer_GetRefStructPointer(p structPointer, f field) structPointer {
  81. return structPointer((*structPointer)(unsafe.Pointer(uintptr(p) + uintptr(f))))
  82. }
  83. func structPointer_GetSliceHeader(p structPointer, f field) *reflect.SliceHeader {
  84. return (*reflect.SliceHeader)(unsafe.Pointer(uintptr(p) + uintptr(f)))
  85. }
  86. func structPointer_Add(p structPointer, size field) structPointer {
  87. return structPointer(unsafe.Pointer(uintptr(p) + uintptr(size)))
  88. }
  89. func structPointer_Len(p structPointer, f field) int {
  90. return len(*(*[]interface{})(unsafe.Pointer(structPointer_GetRefStructPointer(p, f))))
  91. }
  92. func structPointer_StructRefSlice(p structPointer, f field, size uintptr) *structRefSlice {
  93. return &structRefSlice{p: p, f: f, size: size}
  94. }
  95. // A structRefSlice represents a slice of structs (themselves submessages or groups).
  96. type structRefSlice struct {
  97. p structPointer
  98. f field
  99. size uintptr
  100. }
  101. func (v *structRefSlice) Len() int {
  102. return structPointer_Len(v.p, v.f)
  103. }
  104. func (v *structRefSlice) Index(i int) structPointer {
  105. ss := structPointer_GetStructPointer(v.p, v.f)
  106. ss1 := structPointer_GetRefStructPointer(ss, 0)
  107. return structPointer_Add(ss1, field(uintptr(i)*v.size))
  108. }