value_unsafe.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. package protoreflect
  5. import (
  6. "unsafe"
  7. "google.golang.org/protobuf/internal/pragma"
  8. )
  9. type (
  10. ifaceHeader struct {
  11. _ [0]any // if interfaces have greater alignment than unsafe.Pointer, this will enforce it.
  12. Type unsafe.Pointer
  13. Data unsafe.Pointer
  14. }
  15. )
  16. var (
  17. nilType = typeOf(nil)
  18. boolType = typeOf(*new(bool))
  19. int32Type = typeOf(*new(int32))
  20. int64Type = typeOf(*new(int64))
  21. uint32Type = typeOf(*new(uint32))
  22. uint64Type = typeOf(*new(uint64))
  23. float32Type = typeOf(*new(float32))
  24. float64Type = typeOf(*new(float64))
  25. stringType = typeOf(*new(string))
  26. bytesType = typeOf(*new([]byte))
  27. enumType = typeOf(*new(EnumNumber))
  28. )
  29. // typeOf returns a pointer to the Go type information.
  30. // The pointer is comparable and equal if and only if the types are identical.
  31. func typeOf(t any) unsafe.Pointer {
  32. return (*ifaceHeader)(unsafe.Pointer(&t)).Type
  33. }
  34. // value is a union where only one type can be represented at a time.
  35. // The struct is 24B large on 64-bit systems and requires the minimum storage
  36. // necessary to represent each possible type.
  37. //
  38. // The Go GC needs to be able to scan variables containing pointers.
  39. // As such, pointers and non-pointers cannot be intermixed.
  40. type value struct {
  41. pragma.DoNotCompare // 0B
  42. // typ stores the type of the value as a pointer to the Go type.
  43. typ unsafe.Pointer // 8B
  44. // ptr stores the data pointer for a String, Bytes, or interface value.
  45. ptr unsafe.Pointer // 8B
  46. // num stores a Bool, Int32, Int64, Uint32, Uint64, Float32, Float64, or
  47. // Enum value as a raw uint64.
  48. //
  49. // It is also used to store the length of a String or Bytes value;
  50. // the capacity is ignored.
  51. num uint64 // 8B
  52. }
  53. func valueOfString(v string) Value {
  54. return Value{typ: stringType, ptr: unsafe.Pointer(unsafe.StringData(v)), num: uint64(len(v))}
  55. }
  56. func valueOfBytes(v []byte) Value {
  57. return Value{typ: bytesType, ptr: unsafe.Pointer(unsafe.SliceData(v)), num: uint64(len(v))}
  58. }
  59. func valueOfIface(v any) Value {
  60. p := (*ifaceHeader)(unsafe.Pointer(&v))
  61. return Value{typ: p.Type, ptr: p.Data}
  62. }
  63. func (v Value) getString() string {
  64. return unsafe.String((*byte)(v.ptr), v.num)
  65. }
  66. func (v Value) getBytes() []byte {
  67. return unsafe.Slice((*byte)(v.ptr), v.num)
  68. }
  69. func (v Value) getIface() (x any) {
  70. *(*ifaceHeader)(unsafe.Pointer(&x)) = ifaceHeader{Type: v.typ, Data: v.ptr}
  71. return x
  72. }