]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libsanitizer/sanitizer_common/sanitizer_ring_buffer.h
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 uptr kNextMask
= (1ULL << kSizeShift
) - 1;
91 uptr
GetStorageSize() const { return (long_
>> kSizeShift
) << kPageSizeBits
; }
93 void Init(void *storage
, uptr size
) {
94 CHECK_EQ(sizeof(CompactRingBuffer
<T
>), sizeof(void *));
95 CHECK(IsPowerOfTwo(size
));
96 CHECK_GE(size
, 1 << kPageSizeBits
);
97 CHECK_LE(size
, 128 << kPageSizeBits
);
98 CHECK_EQ(size
% 4096, 0);
99 CHECK_EQ(size
% sizeof(T
), 0);
100 CHECK_EQ((uptr
)storage
% (size
* 2), 0);
101 long_
= (uptr
)storage
| ((size
>> kPageSizeBits
) << kSizeShift
);
104 void SetNext(const T
*next
) {
105 long_
= (long_
& ~kNextMask
) | (uptr
)next
;
109 CompactRingBuffer(void *storage
, uptr size
) {
113 // A copy constructor of sorts.
114 CompactRingBuffer(const CompactRingBuffer
&other
, void *storage
) {
115 uptr size
= other
.GetStorageSize();
116 internal_memcpy(storage
, other
.StartOfStorage(), size
);
118 uptr Idx
= other
.Next() - (const T
*)other
.StartOfStorage();
119 SetNext((const T
*)storage
+ Idx
);
122 T
*Next() const { return (T
*)(long_
& kNextMask
); }
124 void *StartOfStorage() const {
125 return (void *)((uptr
)Next() & ~(GetStorageSize() - 1));
128 void *EndOfStorage() const {
129 return (void *)((uptr
)StartOfStorage() + GetStorageSize());
132 uptr
size() const { return GetStorageSize() / sizeof(T
); }
138 next
= (T
*)((uptr
)next
& ~GetStorageSize());
142 const T
&operator[](uptr Idx
) const {
143 CHECK_LT(Idx
, size());
144 const T
*Begin
= (const T
*)StartOfStorage();
145 sptr StorageIdx
= Next() - Begin
;
146 StorageIdx
-= (sptr
)(Idx
+ 1);
148 StorageIdx
+= size();
149 return Begin
[StorageIdx
];
153 ~CompactRingBuffer() {}
154 CompactRingBuffer(const CompactRingBuffer
&) = delete;
159 } // namespace __sanitizer
161 #endif // SANITIZER_RING_BUFFER_H