Skip to content

Commit

Permalink
Complete new storage
Browse files Browse the repository at this point in the history
  • Loading branch information
agerasev committed Jan 20, 2024
1 parent c8078f9 commit c6250bd
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 25 deletions.
6 changes: 3 additions & 3 deletions async/src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{
};
#[cfg(feature = "alloc")]
use alloc::sync::Arc;
use ringbuf::{storage::Array, SharedRb};
#[cfg(feature = "alloc")]
use ringbuf::{storage::Heap, HeapRb};
use ringbuf::{storage::Static, SharedRb};

#[cfg(feature = "alloc")]
pub type AsyncHeapRb<T> = AsyncRb<Heap<T>>;
Expand All @@ -22,11 +22,11 @@ impl<T> AsyncHeapRb<T> {
}
}

pub type AsyncStaticRb<T, const N: usize> = AsyncRb<Static<T, N>>;
pub type AsyncStaticRb<T, const N: usize> = AsyncRb<Array<T, N>>;
pub type AsyncStaticProd<'a, T, const N: usize> = AsyncProd<&'a AsyncStaticRb<T, N>>;
pub type AsyncStaticCons<'a, T, const N: usize> = AsyncCons<&'a AsyncStaticRb<T, N>>;

impl<T, const N: usize> Default for AsyncRb<Static<T, N>> {
impl<T, const N: usize> Default for AsyncRb<Array<T, N>> {
fn default() -> Self {
AsyncRb::from(SharedRb::default())
}
Expand Down
5 changes: 4 additions & 1 deletion async/src/rb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ impl<S: Storage> Observer for AsyncRb<S> {
self.base.write_index()
}

unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>]) {
unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&[MaybeUninit<S::Item>], &[MaybeUninit<S::Item>]) {
self.base.unsafe_slices(start, end)
}
unsafe fn unsafe_slices_mut(&self, start: usize, end: usize) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>]) {
self.base.unsafe_slices_mut(start, end)
}

#[inline]
fn read_is_held(&self) -> bool {
Expand Down
6 changes: 3 additions & 3 deletions async/src/traits/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub trait AsyncConsumer: Consumer {
}
}

pub struct PopFuture<'a, A: AsyncConsumer> {
pub struct PopFuture<'a, A: AsyncConsumer + ?Sized> {
owner: &'a mut A,
done: bool,
}
Expand Down Expand Up @@ -130,7 +130,7 @@ impl<'a, A: AsyncConsumer> Future for PopFuture<'a, A> {
}
}

pub struct PopSliceFuture<'a, 'b, A: AsyncConsumer>
pub struct PopSliceFuture<'a, 'b, A: AsyncConsumer + ?Sized>
where
A::Item: Copy,
{
Expand Down Expand Up @@ -177,7 +177,7 @@ where
}
}

pub struct WaitOccupiedFuture<'a, A: AsyncConsumer> {
pub struct WaitOccupiedFuture<'a, A: AsyncConsumer + ?Sized> {
owner: &'a A,
count: usize,
done: bool,
Expand Down
8 changes: 4 additions & 4 deletions async/src/traits/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub trait AsyncProducer: Producer {
}
}

pub struct PushFuture<'a, A: AsyncProducer> {
pub struct PushFuture<'a, A: AsyncProducer + ?Sized> {
owner: &'a mut A,
item: Option<A::Item>,
}
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<'a, A: AsyncProducer> Future for PushFuture<'a, A> {
}
}

pub struct PushSliceFuture<'a, 'b, A: AsyncProducer>
pub struct PushSliceFuture<'a, 'b, A: AsyncProducer + ?Sized>
where
A::Item: Copy,
{
Expand Down Expand Up @@ -190,7 +190,7 @@ where
}
}

pub struct PushIterFuture<'a, A: AsyncProducer, I: Iterator<Item = A::Item>> {
pub struct PushIterFuture<'a, A: AsyncProducer + ?Sized, I: Iterator<Item = A::Item>> {
owner: &'a mut A,
iter: Option<Peekable<I>>,
}
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<'a, A: AsyncProducer, I: Iterator<Item = A::Item>> Future for PushIterFutur
}
}

pub struct WaitVacantFuture<'a, A: AsyncProducer> {
pub struct WaitVacantFuture<'a, A: AsyncProducer + ?Sized> {
owner: &'a A,
count: usize,
done: bool,
Expand Down
8 changes: 4 additions & 4 deletions blocking/src/alias.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(feature = "std")]
use crate::sync::StdSemaphore;
use crate::{rb::BlockingRb, sync::Semaphore};
use ringbuf::{storage::Array, SharedRb};
#[cfg(feature = "alloc")]
use ringbuf::{storage::Heap, HeapRb};
use ringbuf::{storage::Static, SharedRb};

#[cfg(feature = "std")]
pub type BlockingHeapRb<T, X = StdSemaphore> = BlockingRb<Heap<T>, X>;
Expand All @@ -18,11 +18,11 @@ impl<T, X: Semaphore> BlockingHeapRb<T, X> {
}

#[cfg(feature = "std")]
pub type BlockingStaticRb<T, const N: usize, X = StdSemaphore> = BlockingRb<Static<T, N>, X>;
pub type BlockingStaticRb<T, const N: usize, X = StdSemaphore> = BlockingRb<Array<T, N>, X>;
#[cfg(all(feature = "alloc", not(feature = "std")))]
pub type BlockingStaticRb<T, const N: usize, X> = BlockingRb<Static<T, N>, X>;
pub type BlockingStaticRb<T, const N: usize, X> = BlockingRb<Array<T, N>, X>;

impl<T, const N: usize, X: Semaphore> Default for BlockingRb<Static<T, N>, X> {
impl<T, const N: usize, X: Semaphore> Default for BlockingRb<Array<T, N>, X> {
fn default() -> Self {
BlockingRb::from(SharedRb::default())
}
Expand Down
5 changes: 4 additions & 1 deletion blocking/src/rb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ impl<S: Storage, X: Semaphore> Observer for BlockingRb<S, X> {
self.base.write_index()
}

unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>]) {
unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&[MaybeUninit<S::Item>], &[MaybeUninit<S::Item>]) {
self.base.unsafe_slices(start, end)
}
unsafe fn unsafe_slices_mut(&self, start: usize, end: usize) -> (&mut [MaybeUninit<S::Item>], &mut [MaybeUninit<S::Item>]) {
self.base.unsafe_slices_mut(start, end)
}

#[inline]
fn read_is_held(&self) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions src/benchmarks/base.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{storage::Static, traits::*, LocalRb, SharedRb};
use crate::{storage::Array, traits::*, LocalRb, SharedRb};
use test::{black_box, Bencher};

const RB_SIZE: usize = 256;
const BATCH_SIZE: usize = 100;

#[bench]
fn push_pop_shared(b: &mut Bencher) {
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
let (mut prod, mut cons) = buf.split();
prod.push_slice(&[1; RB_SIZE / 2]);
b.iter(|| {
Expand All @@ -17,7 +17,7 @@ fn push_pop_shared(b: &mut Bencher) {

#[bench]
fn push_pop_local(b: &mut Bencher) {
let buf = LocalRb::<Static<u64, RB_SIZE>>::default();
let buf = LocalRb::<Array<u64, RB_SIZE>>::default();
let (mut prod, mut cons) = buf.split();
prod.push_slice(&[1; RB_SIZE / 2]);
b.iter(|| {
Expand All @@ -28,7 +28,7 @@ fn push_pop_local(b: &mut Bencher) {

#[bench]
fn push_pop_x100(b: &mut Bencher) {
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
let (mut prod, mut cons) = buf.split();
prod.push_slice(&[1; RB_SIZE / 2]);
b.iter(|| {
Expand Down
8 changes: 4 additions & 4 deletions src/benchmarks/parts.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{storage::Static, traits::*, SharedRb};
use crate::{storage::Array, traits::*, SharedRb};
use test::{black_box, Bencher};

const RB_SIZE: usize = 256;

#[bench]
fn advance(b: &mut Bencher) {
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
let (mut prod, cons) = buf.split();
prod.push_slice(&[1; RB_SIZE / 2]);
b.iter(|| {
Expand All @@ -16,7 +16,7 @@ fn advance(b: &mut Bencher) {

#[bench]
fn get_occupied_slices(b: &mut Bencher) {
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
let (mut prod, mut cons) = buf.split();
prod.push_slice(&[0; 3 * RB_SIZE / 4]);
cons.skip(RB_SIZE);
Expand All @@ -29,7 +29,7 @@ fn get_occupied_slices(b: &mut Bencher) {

#[bench]
fn get_vacant_slices(b: &mut Bencher) {
let buf = SharedRb::<Static<u64, RB_SIZE>>::default();
let buf = SharedRb::<Array<u64, RB_SIZE>>::default();
let (mut prod, mut cons) = buf.split();
prod.push_slice(&[0; 1 * RB_SIZE / 4]);
cons.skip(RB_SIZE);
Expand Down
11 changes: 10 additions & 1 deletion src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(feature = "alloc")]
use alloc::{boxed::Box, vec::Vec};
use core::{cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, ops::Range, ptr, ptr::NonNull, slice};
#[cfg(feature = "alloc")]
use core::ptr;
use core::{cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, ops::Range, ptr::NonNull, slice};

/// Abstract storage for the ring buffer.
///
Expand Down Expand Up @@ -128,12 +130,16 @@ unsafe impl<T> Storage for Slice<T> {
}
}

#[cfg(feature = "alloc")]
pub struct Heap<T> {
ptr: *mut MaybeUninit<T>,
len: usize,
}
#[cfg(feature = "alloc")]
unsafe impl<T> Send for Heap<T> where T: Send {}
#[cfg(feature = "alloc")]
unsafe impl<T> Sync for Heap<T> where T: Sync {}
#[cfg(feature = "alloc")]
unsafe impl<T> Storage for Heap<T> {
type Item = T;
#[inline]
Expand All @@ -154,6 +160,7 @@ impl<T> Heap<T> {
}
}
}
#[cfg(feature = "alloc")]
impl<T> From<Box<[MaybeUninit<T>]>> for Heap<T> {
fn from(value: Box<[MaybeUninit<T>]>) -> Self {
Self {
Expand All @@ -162,11 +169,13 @@ impl<T> From<Box<[MaybeUninit<T>]>> for Heap<T> {
}
}
}
#[cfg(feature = "alloc")]
impl<T> From<Heap<T>> for Box<[MaybeUninit<T>]> {
fn from(value: Heap<T>) -> Self {
unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(value.ptr, value.len)) }
}
}
#[cfg(feature = "alloc")]
impl<T> Drop for Heap<T> {
fn drop(&mut self) {
drop(unsafe { Box::from_raw(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) });
Expand Down

0 comments on commit c6250bd

Please sign in to comment.