Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: VecDeque #11

Merged
merged 9 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,35 @@ jobs:
# Test
- name: Test
run: cargo test ${{ matrix.profile }} ${{ matrix.features }}

miri_test:
name: Miri Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
- name: Install miri
run: rustup +nightly component add miri
- name: Run miri tests
run: cargo miri test

asan_test:
name: Address Sanitizer Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
- name: Install miri
run: rustup +nightly component add miri
- name: Run ASan tests
run: RUSTFLAGS="-Z sanitizer=address" cargo test
61 changes: 61 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,64 @@ extern crate core;
pub mod claim;
pub mod try_clone;
pub mod vec;
pub mod vec_deque;

#[cfg(test)]
pub(crate) mod test_util {
use crate::claim::Claim;
use alloc::alloc::Global;
use alloc::sync::Arc;
use core::alloc::{AllocError, Allocator, Layout};
use core::ptr::NonNull;
use core::sync::atomic::{AtomicUsize, Ordering};

#[derive(Clone)]
pub struct WatermarkAllocator {
watermark: usize,
in_use: Arc<AtomicUsize>,
}

impl Claim for WatermarkAllocator {}

impl WatermarkAllocator {
pub(crate) fn in_use(&self) -> usize {
self.in_use.load(Ordering::SeqCst)
}
}

impl WatermarkAllocator {
pub fn new(watermark: usize) -> Self {
Self {
watermark,
in_use: AtomicUsize::new(0).into(),
}
}
}

unsafe impl Allocator for WatermarkAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let current_in_use = self.in_use.load(Ordering::SeqCst);
let new_in_use = current_in_use + layout.size();
if new_in_use > self.watermark {
return Err(AllocError);
}
let allocated = Global.allocate(layout)?;
let true_new_in_use = self.in_use.fetch_add(allocated.len(), Ordering::SeqCst);
unsafe {
if true_new_in_use > self.watermark {
let ptr = allocated.as_ptr() as *mut u8;
let to_dealloc = NonNull::new_unchecked(ptr);
Global.deallocate(to_dealloc, layout);
Err(AllocError)
} else {
Ok(allocated)
}
}
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
Global.deallocate(ptr, layout);
self.in_use.fetch_sub(layout.size(), Ordering::SeqCst);
}
}
}
69 changes: 7 additions & 62 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ impl<T, A: Allocator> Vec<T, A> {
}
Ok(())
}

pub(crate) fn into_inner(self) -> InnerVec<T, A> {
self.inner
}
}

impl<T: Claim, A: Allocator> Vec<T, A> {
Expand Down Expand Up @@ -290,64 +294,11 @@ impl<T, A: Allocator> AsMut<[T]> for Vec<T, A> {
mod tests {
use super::*;
use crate::claim::Claim;
use crate::test_util::WatermarkAllocator;
use alloc::alloc::Global;
use alloc::boxed::Box;
use alloc::collections::TryReserveError;
use alloc::sync::Arc;
use alloc::{format, vec};
use core::alloc::{AllocError, Layout};
use core::ptr::NonNull;
use core::sync::atomic::{AtomicUsize, Ordering};

#[derive(Clone)]
struct WatermarkAllocator {
watermark: usize,
in_use: Arc<AtomicUsize>,
}

impl Claim for WatermarkAllocator {}

impl WatermarkAllocator {
pub(crate) fn in_use(&self) -> usize {
self.in_use.load(Ordering::SeqCst)
}
}

impl WatermarkAllocator {
fn new(watermark: usize) -> Self {
Self {
watermark,
in_use: AtomicUsize::new(0).into(),
}
}
}

unsafe impl Allocator for WatermarkAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let current_in_use = self.in_use.load(Ordering::SeqCst);
let new_in_use = current_in_use + layout.size();
if new_in_use > self.watermark {
return Err(AllocError);
}
let allocated = Global.allocate(layout)?;
let true_new_in_use = self.in_use.fetch_add(allocated.len(), Ordering::SeqCst);
unsafe {
if true_new_in_use > self.watermark {
let ptr = allocated.as_ptr() as *mut u8;
let to_dealloc = NonNull::new_unchecked(ptr);
Global.deallocate(to_dealloc, layout);
Err(AllocError)
} else {
Ok(allocated)
}
}
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
Global.deallocate(ptr, layout);
self.in_use.fetch_sub(layout.size(), Ordering::SeqCst);
}
}

#[test]
fn test_basics() {
Expand All @@ -364,14 +315,8 @@ mod tests {
vec.push(4).unwrap();
assert_eq!(vec.len(), 4);
assert_eq!(vec.capacity(), 4);
assert_eq!(
wma.in_use.load(Ordering::SeqCst),
vec.capacity() * size_of::<i32>()
);
assert_eq!(
vec.allocator().in_use.load(Ordering::SeqCst),
vec.capacity() * size_of::<i32>()
);
assert_eq!(wma.in_use(), vec.capacity() * size_of::<i32>());
assert_eq!(vec.allocator().in_use(), vec.capacity() * size_of::<i32>());
let _err: TryReserveError = vec.push(5).unwrap_err();
assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);
assert_eq!(vec.len(), 4);
Expand Down
Loading
Loading