presence.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2024 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 impl
  5. import (
  6. "sync/atomic"
  7. "unsafe"
  8. )
  9. // presenceSize represents the size of a presence set, which should be the largest index of the set+1
  10. type presenceSize uint32
  11. // presence is the internal representation of the bitmap array in a generated protobuf
  12. type presence struct {
  13. // This is a pointer to the beginning of an array of uint32
  14. P unsafe.Pointer
  15. }
  16. func (p presence) toElem(num uint32) (ret *uint32) {
  17. const (
  18. bitsPerByte = 8
  19. siz = unsafe.Sizeof(*ret)
  20. )
  21. // p.P points to an array of uint32, num is the bit in this array that the
  22. // caller wants to check/manipulate. Calculate the index in the array that
  23. // contains this specific bit. E.g.: 76 / 32 = 2 (integer division).
  24. offset := uintptr(num) / (siz * bitsPerByte) * siz
  25. return (*uint32)(unsafe.Pointer(uintptr(p.P) + offset))
  26. }
  27. // Present checks for the presence of a specific field number in a presence set.
  28. func (p presence) Present(num uint32) bool {
  29. return Export{}.Present(p.toElem(num), num)
  30. }
  31. // SetPresent adds presence for a specific field number in a presence set.
  32. func (p presence) SetPresent(num uint32, size presenceSize) {
  33. Export{}.SetPresent(p.toElem(num), num, uint32(size))
  34. }
  35. // SetPresentUnatomic adds presence for a specific field number in a presence set without using
  36. // atomic operations. Only to be called during unmarshaling.
  37. func (p presence) SetPresentUnatomic(num uint32, size presenceSize) {
  38. Export{}.SetPresentNonAtomic(p.toElem(num), num, uint32(size))
  39. }
  40. // ClearPresent removes presence for a specific field number in a presence set.
  41. func (p presence) ClearPresent(num uint32) {
  42. Export{}.ClearPresent(p.toElem(num), num)
  43. }
  44. // LoadPresenceCache (together with PresentInCache) allows for a
  45. // cached version of checking for presence without re-reading the word
  46. // for every field. It is optimized for efficiency and assumes no
  47. // simltaneous mutation of the presence set (or at least does not have
  48. // a problem with simultaneous mutation giving inconsistent results).
  49. func (p presence) LoadPresenceCache() (current uint32) {
  50. if p.P == nil {
  51. return 0
  52. }
  53. return atomic.LoadUint32((*uint32)(p.P))
  54. }
  55. // PresentInCache reads presence from a cached word in the presence
  56. // bitmap. It caches up a new word if the bit is outside the
  57. // word. This is for really fast iteration through bitmaps in cases
  58. // where we either know that the bitmap will not be altered, or we
  59. // don't care about inconsistencies caused by simultaneous writes.
  60. func (p presence) PresentInCache(num uint32, cachedElement *uint32, current *uint32) bool {
  61. if num/32 != *cachedElement {
  62. o := uintptr(num/32) * unsafe.Sizeof(uint32(0))
  63. q := (*uint32)(unsafe.Pointer(uintptr(p.P) + o))
  64. *current = atomic.LoadUint32(q)
  65. *cachedElement = num / 32
  66. }
  67. return (*current & (1 << (num % 32))) > 0
  68. }
  69. // AnyPresent checks if any field is marked as present in the bitmap.
  70. func (p presence) AnyPresent(size presenceSize) bool {
  71. n := uintptr((size + 31) / 32)
  72. for j := uintptr(0); j < n; j++ {
  73. o := j * unsafe.Sizeof(uint32(0))
  74. q := (*uint32)(unsafe.Pointer(uintptr(p.P) + o))
  75. b := atomic.LoadUint32(q)
  76. if b > 0 {
  77. return true
  78. }
  79. }
  80. return false
  81. }
  82. // toRaceDetectData finds the preceding RaceDetectHookData in a
  83. // message by using pointer arithmetic. As the type of the presence
  84. // set (bitmap) varies with the number of fields in the protobuf, we
  85. // can not have a struct type containing the array and the
  86. // RaceDetectHookData. instead the RaceDetectHookData is placed
  87. // immediately before the bitmap array, and we find it by walking
  88. // backwards in the struct.
  89. //
  90. // This method is only called from the race-detect version of the code,
  91. // so RaceDetectHookData is never an empty struct.
  92. func (p presence) toRaceDetectData() *RaceDetectHookData {
  93. var template struct {
  94. d RaceDetectHookData
  95. a [1]uint32
  96. }
  97. o := (uintptr(unsafe.Pointer(&template.a)) - uintptr(unsafe.Pointer(&template.d)))
  98. return (*RaceDetectHookData)(unsafe.Pointer(uintptr(p.P) - o))
  99. }
  100. func atomicLoadShadowPresence(p **[]byte) *[]byte {
  101. return (*[]byte)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(p))))
  102. }
  103. func atomicStoreShadowPresence(p **[]byte, v *[]byte) {
  104. atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(p)), nil, unsafe.Pointer(v))
  105. }
  106. // findPointerToRaceDetectData finds the preceding RaceDetectHookData
  107. // in a message by using pointer arithmetic. For the methods called
  108. // directy from generated code, we don't have a pointer to the
  109. // beginning of the presence set, but a pointer inside the array. As
  110. // we know the index of the bit we're manipulating (num), we can
  111. // calculate which element of the array ptr is pointing to. With that
  112. // information we find the preceding RaceDetectHookData and can
  113. // manipulate the shadow bitmap.
  114. //
  115. // This method is only called from the race-detect version of the
  116. // code, so RaceDetectHookData is never an empty struct.
  117. func findPointerToRaceDetectData(ptr *uint32, num uint32) *RaceDetectHookData {
  118. var template struct {
  119. d RaceDetectHookData
  120. a [1]uint32
  121. }
  122. o := (uintptr(unsafe.Pointer(&template.a)) - uintptr(unsafe.Pointer(&template.d))) + uintptr(num/32)*unsafe.Sizeof(uint32(0))
  123. return (*RaceDetectHookData)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) - o))
  124. }