value_unsafe.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !purego,!appengine
  5. package protoreflect
  6. import (
  7. "unsafe"
  8. "google.golang.org/protobuf/internal/pragma"
  9. )
  10. type (
  11. stringHeader struct {
  12. Data unsafe.Pointer
  13. Len int
  14. }
  15. sliceHeader struct {
  16. Data unsafe.Pointer
  17. Len int
  18. Cap int
  19. }
  20. ifaceHeader struct {
  21. Type unsafe.Pointer
  22. Data unsafe.Pointer
  23. }
  24. )
  25. var (
  26. nilType = typeOf(nil)
  27. boolType = typeOf(*new(bool))
  28. int32Type = typeOf(*new(int32))
  29. int64Type = typeOf(*new(int64))
  30. uint32Type = typeOf(*new(uint32))
  31. uint64Type = typeOf(*new(uint64))
  32. float32Type = typeOf(*new(float32))
  33. float64Type = typeOf(*new(float64))
  34. stringType = typeOf(*new(string))
  35. bytesType = typeOf(*new([]byte))
  36. enumType = typeOf(*new(EnumNumber))
  37. )
  38. // typeOf returns a pointer to the Go type information.
  39. // The pointer is comparable and equal if and only if the types are identical.
  40. func typeOf(t interface{}) unsafe.Pointer {
  41. return (*ifaceHeader)(unsafe.Pointer(&t)).Type
  42. }
  43. // value is a union where only one type can be represented at a time.
  44. // The struct is 24B large on 64-bit systems and requires the minimum storage
  45. // necessary to represent each possible type.
  46. //
  47. // The Go GC needs to be able to scan variables containing pointers.
  48. // As such, pointers and non-pointers cannot be intermixed.
  49. type value struct {
  50. pragma.DoNotCompare // 0B
  51. // typ stores the type of the value as a pointer to the Go type.
  52. typ unsafe.Pointer // 8B
  53. // ptr stores the data pointer for a String, Bytes, or interface value.
  54. ptr unsafe.Pointer // 8B
  55. // num stores a Bool, Int32, Int64, Uint32, Uint64, Float32, Float64, or
  56. // Enum value as a raw uint64.
  57. //
  58. // It is also used to store the length of a String or Bytes value;
  59. // the capacity is ignored.
  60. num uint64 // 8B
  61. }
  62. func valueOfString(v string) Value {
  63. p := (*stringHeader)(unsafe.Pointer(&v))
  64. return Value{typ: stringType, ptr: p.Data, num: uint64(len(v))}
  65. }
  66. func valueOfBytes(v []byte) Value {
  67. p := (*sliceHeader)(unsafe.Pointer(&v))
  68. return Value{typ: bytesType, ptr: p.Data, num: uint64(len(v))}
  69. }
  70. func valueOfIface(v interface{}) Value {
  71. p := (*ifaceHeader)(unsafe.Pointer(&v))
  72. return Value{typ: p.Type, ptr: p.Data}
  73. }
  74. func (v Value) getString() (x string) {
  75. *(*stringHeader)(unsafe.Pointer(&x)) = stringHeader{Data: v.ptr, Len: int(v.num)}
  76. return x
  77. }
  78. func (v Value) getBytes() (x []byte) {
  79. *(*sliceHeader)(unsafe.Pointer(&x)) = sliceHeader{Data: v.ptr, Len: int(v.num), Cap: int(v.num)}
  80. return x
  81. }
  82. func (v Value) getIface() (x interface{}) {
  83. *(*ifaceHeader)(unsafe.Pointer(&x)) = ifaceHeader{Type: v.typ, Data: v.ptr}
  84. return x
  85. }