1 //===-- sanitizer_ring_buffer.h ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Simple ring buffer.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_RING_BUFFER_H
14 #define SANITIZER_RING_BUFFER_H
16 #include "sanitizer_common.h"
18 namespace __sanitizer {
19 // RingBuffer<T>: fixed-size ring buffer optimized for speed of push().
20 // T should be a POD type and sizeof(T) should be divisible by sizeof(void*).
21 // At creation, all elements are zero.
25 COMPILER_CHECK(sizeof(T) % sizeof(void *) == 0);
26 static RingBuffer *New(uptr Size) {
27 void *Ptr = MmapOrDie(SizeInBytes(Size), "RingBuffer");
28 RingBuffer *RB = reinterpret_cast<RingBuffer*>(Ptr);
29 uptr End = reinterpret_cast<uptr>(Ptr) + SizeInBytes(Size);
30 RB->last_ = RB->next_ = reinterpret_cast<T*>(End - sizeof(T));
34 UnmapOrDie(this, SizeInBytes(size()));
38 reinterpret_cast<T *>(reinterpret_cast<uptr>(this) +
42 static uptr SizeInBytes(uptr Size) {
43 return Size * sizeof(T) + 2 * sizeof(T*);
46 uptr SizeInBytes() { return SizeInBytes(size()); }
51 // The condition below works only if sizeof(T) is divisible by sizeof(T*).
52 if (next_ <= reinterpret_cast<T*>(&next_))
56 T operator[](uptr Idx) const {
57 CHECK_LT(Idx, size());
58 sptr IdxNext = Idx + 1;
59 if (IdxNext > last_ - next_)
61 return next_[IdxNext];
67 RingBuffer(const RingBuffer&) = delete;
72 // L: last_, always points to the last data element.
73 // N: next_, initially equals to last_, is decremented on every push,
74 // wraps around if it's less or equal than its own address.
77 T data_[1]; // flexible array.
80 // A ring buffer with externally provided storage that encodes its state in 8
81 // bytes. Has significant constraints on size and alignment of storage.
82 // See a comment in hwasan/hwasan_thread_list.h for the motivation behind this.
83 #if SANITIZER_WORDSIZE == 64
85 class CompactRingBuffer {
86 // Top byte of long_ stores the buffer size in pages.
87 // Lower bytes store the address of the next buffer element.
88 static constexpr int kPageSizeBits = 12;
89 static constexpr int kSizeShift = 56;
90 static constexpr uptr kNextMask = (1ULL << kSizeShift) - 1;
92 uptr GetStorageSize() const { return (long_ >> kSizeShift) << kPageSizeBits; }
94 void Init(void *storage, uptr size) {
95 CHECK_EQ(sizeof(CompactRingBuffer<T>), sizeof(void *));
96 CHECK(IsPowerOfTwo(size));
97 CHECK_GE(size, 1 << kPageSizeBits);
98 CHECK_LE(size, 128 << kPageSizeBits);
99 CHECK_EQ(size % 4096, 0);
100 CHECK_EQ(size % sizeof(T), 0);
101 CHECK_EQ((uptr)storage % (size * 2), 0);
102 long_ = (uptr)storage | ((size >> kPageSizeBits) << kSizeShift);
105 void SetNext(const T *next) {
106 long_ = (long_ & ~kNextMask) | (uptr)next;
110 CompactRingBuffer(void *storage, uptr size) {
114 // A copy constructor of sorts.
115 CompactRingBuffer(const CompactRingBuffer &other, void *storage) {
116 uptr size = other.GetStorageSize();
117 internal_memcpy(storage, other.StartOfStorage(), size);
119 uptr Idx = other.Next() - (const T *)other.StartOfStorage();
120 SetNext((const T *)storage + Idx);
123 T *Next() const { return (T *)(long_ & kNextMask); }
125 void *StartOfStorage() const {
126 return (void *)((uptr)Next() & ~(GetStorageSize() - 1));
129 void *EndOfStorage() const {
130 return (void *)((uptr)StartOfStorage() + GetStorageSize());
133 uptr size() const { return GetStorageSize() / sizeof(T); }
139 next = (T *)((uptr)next & ~GetStorageSize());
143 T operator[](uptr Idx) const {
144 CHECK_LT(Idx, size());
145 const T *Begin = (const T *)StartOfStorage();
146 sptr StorageIdx = Next() - Begin;
147 StorageIdx -= (sptr)(Idx + 1);
149 StorageIdx += size();
150 return Begin[StorageIdx];
154 ~CompactRingBuffer() {}
155 CompactRingBuffer(const CompactRingBuffer &) = delete;
160 } // namespace __sanitizer
162 #endif // SANITIZER_RING_BUFFER_H