sanitizer_ring_buffer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //===-- sanitizer_ring_buffer.h ---------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Simple ring buffer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_RING_BUFFER_H
  13. #define SANITIZER_RING_BUFFER_H
  14. #include "sanitizer_common.h"
  15. namespace __sanitizer {
  16. // RingBuffer<T>: fixed-size ring buffer optimized for speed of push().
  17. // T should be a POD type and sizeof(T) should be divisible by sizeof(void*).
  18. // At creation, all elements are zero.
  19. template<class T>
  20. class RingBuffer {
  21. public:
  22. COMPILER_CHECK(sizeof(T) % sizeof(void *) == 0);
  23. static RingBuffer *New(uptr Size) {
  24. void *Ptr = MmapOrDie(SizeInBytes(Size), "RingBuffer");
  25. RingBuffer *RB = reinterpret_cast<RingBuffer*>(Ptr);
  26. uptr End = reinterpret_cast<uptr>(Ptr) + SizeInBytes(Size);
  27. RB->last_ = RB->next_ = reinterpret_cast<T*>(End - sizeof(T));
  28. return RB;
  29. }
  30. void Delete() {
  31. UnmapOrDie(this, SizeInBytes(size()));
  32. }
  33. uptr size() const {
  34. return last_ + 1 -
  35. reinterpret_cast<T *>(reinterpret_cast<uptr>(this) +
  36. 2 * sizeof(T *));
  37. }
  38. static uptr SizeInBytes(uptr Size) {
  39. return Size * sizeof(T) + 2 * sizeof(T*);
  40. }
  41. uptr SizeInBytes() { return SizeInBytes(size()); }
  42. void push(T t) {
  43. *next_ = t;
  44. next_--;
  45. // The condition below works only if sizeof(T) is divisible by sizeof(T*).
  46. if (next_ <= reinterpret_cast<T*>(&next_))
  47. next_ = last_;
  48. }
  49. T operator[](uptr Idx) const {
  50. CHECK_LT(Idx, size());
  51. sptr IdxNext = Idx + 1;
  52. if (IdxNext > last_ - next_)
  53. IdxNext -= size();
  54. return next_[IdxNext];
  55. }
  56. private:
  57. RingBuffer() {}
  58. ~RingBuffer() {}
  59. RingBuffer(const RingBuffer&) = delete;
  60. // Data layout:
  61. // LNDDDDDDDD
  62. // D: data elements.
  63. // L: last_, always points to the last data element.
  64. // N: next_, initially equals to last_, is decremented on every push,
  65. // wraps around if it's less or equal than its own address.
  66. T *last_;
  67. T *next_;
  68. T data_[1]; // flexible array.
  69. };
  70. // A ring buffer with externally provided storage that encodes its state in 8
  71. // bytes. Has significant constraints on size and alignment of storage.
  72. // See a comment in hwasan/hwasan_thread_list.h for the motivation behind this.
  73. #if SANITIZER_WORDSIZE == 64
  74. template <class T>
  75. class CompactRingBuffer {
  76. // Top byte of long_ stores the buffer size in pages.
  77. // Lower bytes store the address of the next buffer element.
  78. static constexpr int kPageSizeBits = 12;
  79. static constexpr int kSizeShift = 56;
  80. static constexpr uptr kNextMask = (1ULL << kSizeShift) - 1;
  81. uptr GetStorageSize() const { return (long_ >> kSizeShift) << kPageSizeBits; }
  82. void Init(void *storage, uptr size) {
  83. CHECK_EQ(sizeof(CompactRingBuffer<T>), sizeof(void *));
  84. CHECK(IsPowerOfTwo(size));
  85. CHECK_GE(size, 1 << kPageSizeBits);
  86. CHECK_LE(size, 128 << kPageSizeBits);
  87. CHECK_EQ(size % 4096, 0);
  88. CHECK_EQ(size % sizeof(T), 0);
  89. CHECK_EQ((uptr)storage % (size * 2), 0);
  90. long_ = (uptr)storage | ((size >> kPageSizeBits) << kSizeShift);
  91. }
  92. void SetNext(const T *next) {
  93. long_ = (long_ & ~kNextMask) | (uptr)next;
  94. }
  95. public:
  96. CompactRingBuffer(void *storage, uptr size) {
  97. Init(storage, size);
  98. }
  99. // A copy constructor of sorts.
  100. CompactRingBuffer(const CompactRingBuffer &other, void *storage) {
  101. uptr size = other.GetStorageSize();
  102. internal_memcpy(storage, other.StartOfStorage(), size);
  103. Init(storage, size);
  104. uptr Idx = other.Next() - (const T *)other.StartOfStorage();
  105. SetNext((const T *)storage + Idx);
  106. }
  107. T *Next() const { return (T *)(long_ & kNextMask); }
  108. void *StartOfStorage() const {
  109. return (void *)((uptr)Next() & ~(GetStorageSize() - 1));
  110. }
  111. void *EndOfStorage() const {
  112. return (void *)((uptr)StartOfStorage() + GetStorageSize());
  113. }
  114. uptr size() const { return GetStorageSize() / sizeof(T); }
  115. void push(T t) {
  116. T *next = Next();
  117. *next = t;
  118. next++;
  119. next = (T *)((uptr)next & ~GetStorageSize());
  120. SetNext(next);
  121. }
  122. const T &operator[](uptr Idx) const {
  123. CHECK_LT(Idx, size());
  124. const T *Begin = (const T *)StartOfStorage();
  125. sptr StorageIdx = Next() - Begin;
  126. StorageIdx -= (sptr)(Idx + 1);
  127. if (StorageIdx < 0)
  128. StorageIdx += size();
  129. return Begin[StorageIdx];
  130. }
  131. public:
  132. ~CompactRingBuffer() {}
  133. CompactRingBuffer(const CompactRingBuffer &) = delete;
  134. uptr long_;
  135. };
  136. #endif
  137. } // namespace __sanitizer
  138. #endif // SANITIZER_RING_BUFFER_H