1 //===-- sanitizer_ring_buffer.h ---------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_RING_BUFFER_H
13 #define SANITIZER_RING_BUFFER_H
15 #include "sanitizer_common.h"
17 namespace __sanitizer
{
18 // RingBuffer<T>: fixed-size ring buffer optimized for speed of push().
19 // T should be a POD type and sizeof(T) should be divisible by sizeof(void*).
20 // At creation, all elements are zero.
24 COMPILER_CHECK(sizeof(T
) % sizeof(void *) == 0);
25 static RingBuffer
*New(uptr Size
) {
26 void *Ptr
= MmapOrDie(SizeInBytes(Size
), "RingBuffer");
27 RingBuffer
*RB
= reinterpret_cast<RingBuffer
*>(Ptr
);
28 uptr End
= reinterpret_cast<uptr
>(Ptr
) + SizeInBytes(Size
);
29 RB
->last_
= RB
->next_
= reinterpret_cast<T
*>(End
- sizeof(T
));
33 UnmapOrDie(this, SizeInBytes(size()));
37 reinterpret_cast<T
*>(reinterpret_cast<uptr
>(this) +
41 static uptr
SizeInBytes(uptr Size
) {
42 return Size
* sizeof(T
) + 2 * sizeof(T
*);
45 uptr
SizeInBytes() { return SizeInBytes(size()); }
50 // The condition below works only if sizeof(T) is divisible by sizeof(T*).
51 if (next_
<= reinterpret_cast<T
*>(&next_
))
55 T
operator[](uptr Idx
) const {
56 CHECK_LT(Idx
, size());
57 sptr IdxNext
= Idx
+ 1;
58 if (IdxNext
> last_
- next_
)
60 return next_
[IdxNext
];
66 RingBuffer(const RingBuffer
&) = delete;
71 // L: last_, always points to the last data element.
72 // N: next_, initially equals to last_, is decremented on every push,
73 // wraps around if it's less or equal than its own address.
76 T data_
[1]; // flexible array.
79 // A ring buffer with externally provided storage that encodes its state in 8
80 // bytes. Has significant constraints on size and alignment of storage.
81 // See a comment in hwasan/hwasan_thread_list.h for the motivation behind this.
82 #if SANITIZER_WORDSIZE == 64
84 class CompactRingBuffer
{
85 // Top byte of long_ stores the buffer size in pages.
86 // Lower bytes store the address of the next buffer element.
87 static constexpr int kPageSizeBits
= 12;
88 static constexpr int kSizeShift
= 56;
89 static constexpr int kSizeBits
= 64 - kSizeShift
;
90 static constexpr uptr kNextMask
= (1ULL << kSizeShift
) - 1;
92 uptr
GetStorageSize() const { return (long_
>> kSizeShift
) << kPageSizeBits
; }
94 static uptr
SignExtend(uptr x
) { return ((sptr
)x
) << kSizeBits
>> kSizeBits
; }
96 void Init(void *storage
, uptr size
) {
97 CHECK_EQ(sizeof(CompactRingBuffer
<T
>), sizeof(void *));
98 CHECK(IsPowerOfTwo(size
));
99 CHECK_GE(size
, 1 << kPageSizeBits
);
100 CHECK_LE(size
, 128 << kPageSizeBits
);
101 CHECK_EQ(size
% 4096, 0);
102 CHECK_EQ(size
% sizeof(T
), 0);
103 uptr st
= (uptr
)storage
;
104 CHECK_EQ(st
% (size
* 2), 0);
105 CHECK_EQ(st
, SignExtend(st
& kNextMask
));
106 long_
= (st
& kNextMask
) | ((size
>> kPageSizeBits
) << kSizeShift
);
109 void SetNext(const T
*next
) {
110 long_
= (long_
& ~kNextMask
) | ((uptr
)next
& kNextMask
);
114 CompactRingBuffer(void *storage
, uptr size
) {
118 // A copy constructor of sorts.
119 CompactRingBuffer(const CompactRingBuffer
&other
, void *storage
) {
120 uptr size
= other
.GetStorageSize();
121 internal_memcpy(storage
, other
.StartOfStorage(), size
);
123 uptr Idx
= other
.Next() - (const T
*)other
.StartOfStorage();
124 SetNext((const T
*)storage
+ Idx
);
127 T
*Next() const { return (T
*)(SignExtend(long_
& kNextMask
)); }
129 void *StartOfStorage() const {
130 return (void *)((uptr
)Next() & ~(GetStorageSize() - 1));
133 void *EndOfStorage() const {
134 return (void *)((uptr
)StartOfStorage() + GetStorageSize());
137 uptr
size() const { return GetStorageSize() / sizeof(T
); }
143 next
= (T
*)((uptr
)next
& ~GetStorageSize());
147 const T
&operator[](uptr Idx
) const {
148 CHECK_LT(Idx
, size());
149 const T
*Begin
= (const T
*)StartOfStorage();
150 sptr StorageIdx
= Next() - Begin
;
151 StorageIdx
-= (sptr
)(Idx
+ 1);
153 StorageIdx
+= size();
154 return Begin
[StorageIdx
];
158 ~CompactRingBuffer() {}
159 CompactRingBuffer(const CompactRingBuffer
&) = delete;
164 } // namespace __sanitizer
166 #endif // SANITIZER_RING_BUFFER_H