| 1 |
use crate::{consumer::Consumer, producer::Producer}; |
| 2 |
use alloc::{sync::Arc, vec::Vec}; |
| 3 |
use cache_padded::CachePadded; |
| 4 |
use core::{ |
| 5 |
cell::UnsafeCell, |
| 6 |
cmp::min, |
| 7 |
mem::MaybeUninit, |
| 8 |
ptr::{self, copy}, |
| 9 |
sync::atomic::{AtomicUsize, Ordering}, |
| 10 |
}; |
| 11 |
|
| 12 |
pub(crate) struct SharedVec<T: Sized> { |
| 13 |
cell: UnsafeCell<Vec<T>>, |
| 14 |
len: usize, |
| 15 |
} |
| 16 |
|
| 17 |
unsafe impl<T: Sized> Sync for SharedVec<T> {} |
| 18 |
|
| 19 |
impl<T: Sized> SharedVec<T> { |
| 20 |
pub fn new(data: Vec<T>) -> Self { |
| 21 |
Self { |
| 22 |
len: data.len(), |
| 23 |
cell: UnsafeCell::new(data), |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
pub fn len(&self) -> usize { |
| 28 |
self.len |
| 29 |
} |
| 30 |
pub unsafe fn get_ref(&self) -> &Vec<T> { |
| 31 |
&*self.cell.get() |
| 32 |
} |
| 33 |
#[allow(clippy::mut_from_ref)] |
| 34 |
pub unsafe fn get_mut(&self) -> &mut Vec<T> { |
| 35 |
&mut *self.cell.get() |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/// Ring buffer itself. |
| 40 |
pub struct RingBuffer<T: Sized> { |
| 41 |
pub(crate) data: SharedVec<MaybeUninit<T>>, |
| 42 |
pub(crate) head: CachePadded<AtomicUsize>, |
| 43 |
pub(crate) tail: CachePadded<AtomicUsize>, |
| 44 |
} |
| 45 |
|
| 46 |
impl<T: Sized> RingBuffer<T> { |
| 47 |
/// Creates a new instance of a ring buffer. |
| 48 |
pub fn new(capacity: usize) -> Self { |
| 49 |
let mut data = Vec::new(); |
| 50 |
data.resize_with(capacity + 1, MaybeUninit::uninit); |
| 51 |
Self { |
| 52 |
data: SharedVec::new(data), |
| 53 |
head: CachePadded::new(AtomicUsize::new(0)), |
| 54 |
tail: CachePadded::new(AtomicUsize::new(0)), |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/// Splits ring buffer into producer and consumer. |
| 59 |
pub fn split(self) -> (Producer<T>, Consumer<T>) { |
| 60 |
let arc = Arc::new(self); |
| 61 |
(Producer { rb: arc.clone() }, Consumer { rb: arc }) |
| 62 |
} |
| 63 |
|
| 64 |
/// Returns capacity of the ring buffer. |
| 65 |
pub fn capacity(&self) -> usize { |
| 66 |
self.data.len() - 1 |
| 67 |
} |
| 68 |
|
| 69 |
/// Checks if the ring buffer is empty. |
| 70 |
pub fn is_empty(&self) -> bool { |
| 71 |
let head = self.head.load(Ordering::Acquire); |
| 72 |
let tail = self.tail.load(Ordering::Acquire); |
| 73 |
head == tail |
| 74 |
} |
| 75 |
|
| 76 |
/// Checks if the ring buffer is full. |
| 77 |
pub fn is_full(&self) -> bool { |
| 78 |
let head = self.head.load(Ordering::Acquire); |
| 79 |
let tail = self.tail.load(Ordering::Acquire); |
| 80 |
(tail + 1) % self.data.len() == head |
| 81 |
} |
| 82 |
|
| 83 |
/// The length of the data in the buffer. |
| 84 |
pub fn len(&self) -> usize { |
| 85 |
let head = self.head.load(Ordering::Acquire); |
| 86 |
let tail = self.tail.load(Ordering::Acquire); |
| 87 |
(tail + self.data.len() - head) % self.data.len() |
| 88 |
} |
| 89 |
|
| 90 |
/// The remaining space in the buffer. |
| 91 |
pub fn remaining(&self) -> usize { |
| 92 |
self.capacity() - self.len() |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl<T: Sized> Drop for RingBuffer<T> { |
| 97 |
fn drop(&mut self) { |
| 98 |
let data = unsafe { self.data.get_mut() }; |
| 99 |
|
| 100 |
let head = self.head.load(Ordering::Acquire); |
| 101 |
let tail = self.tail.load(Ordering::Acquire); |
| 102 |
let len = data.len(); |
| 103 |
|
| 104 |
let slices = if head <= tail { |
| 105 |
(head..tail, 0..0) |
| 106 |
} else { |
| 107 |
(head..len, 0..tail) |
| 108 |
}; |
| 109 |
|
| 110 |
let drop = |elem_ref: &mut MaybeUninit<T>| unsafe { |
| 111 |
elem_ref.as_ptr().read(); |
| 112 |
}; |
| 113 |
for elem in data[slices.0].iter_mut() { |
| 114 |
drop(elem); |
| 115 |
} |
| 116 |
for elem in data[slices.1].iter_mut() { |
| 117 |
drop(elem); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
struct SlicePtr<T: Sized> { |
| 123 |
pub ptr: *mut T, |
| 124 |
pub len: usize, |
| 125 |
} |
| 126 |
|
| 127 |
impl<T> SlicePtr<T> { |
| 128 |
fn null() -> Self { |
| 129 |
Self { |
| 130 |
ptr: ptr::null_mut(), |
| 131 |
len: 0, |
| 132 |
} |
| 133 |
} |
| 134 |
fn new(slice: &mut [T]) -> Self { |
| 135 |
Self { |
| 136 |
ptr: slice.as_mut_ptr(), |
| 137 |
len: slice.len(), |
| 138 |
} |
| 139 |
} |
| 140 |
unsafe fn shift(&mut self, count: usize) { |
| 141 |
self.ptr = self.ptr.add(count); |
| 142 |
self.len -= count; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/// Moves at most `count` items from the `src` consumer to the `dst` producer. |
| 147 |
/// Consumer and producer may be of different buffers as well as of the same one. |
| 148 |
/// |
| 149 |
/// `count` is the number of items being moved, if `None` - as much as possible items will be moved. |
| 150 |
/// |
| 151 |
/// Returns number of items been moved. |
| 152 |
pub fn move_items<T>(src: &mut Consumer<T>, dst: &mut Producer<T>, count: Option<usize>) -> usize { |
| 153 |
unsafe { |
| 154 |
src.pop_access(|src_left, src_right| -> usize { |
| 155 |
dst.push_access(|dst_left, dst_right| -> usize { |
| 156 |
let n = count.unwrap_or_else(|| { |
| 157 |
min( |
| 158 |
src_left.len() + src_right.len(), |
| 159 |
dst_left.len() + dst_right.len(), |
| 160 |
) |
| 161 |
}); |
| 162 |
let mut m = 0; |
| 163 |
let mut src = (SlicePtr::new(src_left), SlicePtr::new(src_right)); |
| 164 |
let mut dst = (SlicePtr::new(dst_left), SlicePtr::new(dst_right)); |
| 165 |
|
| 166 |
loop { |
| 167 |
let k = min(n - m, min(src.0.len, dst.0.len)); |
| 168 |
if k == 0 { |
| 169 |
break; |
| 170 |
} |
| 171 |
copy(src.0.ptr, dst.0.ptr, k); |
| 172 |
if src.0.len == k { |
| 173 |
src.0 = src.1; |
| 174 |
src.1 = SlicePtr::null(); |
| 175 |
} else { |
| 176 |
src.0.shift(k); |
| 177 |
} |
| 178 |
if dst.0.len == k { |
| 179 |
dst.0 = dst.1; |
| 180 |
dst.1 = SlicePtr::null(); |
| 181 |
} else { |
| 182 |
dst.0.shift(k); |
| 183 |
} |
| 184 |
m += k |
| 185 |
} |
| 186 |
|
| 187 |
m |
| 188 |
}) |
| 189 |
}) |
| 190 |
} |
| 191 |
} |