Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 15 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ keywords = [
"buffer",
"ring-buffer"
]
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "MIT"
authors = ["Sebastian Klose <mail@sklose.com>"]
repository = "https://github.com/sklose/magic-buffer"
readme = "README.md"

[dependencies]
thiserror = "1"
thiserror = "2"

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.48"
Expand All @@ -32,3 +32,7 @@ libc = "0.2"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
mach2 = "0.4"

[profile.test]
opt-level = 3
lto = "fat"
51 changes: 45 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![doc = include_str!("../README.md")]

use std::{
arch::asm,
ops::{
Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeTo, RangeToInclusive,
},
Expand Down Expand Up @@ -158,7 +159,10 @@ impl MagicBuffer {
/// }
/// ```
pub fn as_ptr(&self, offset: usize) -> *const u8 {
unsafe { self.addr.add(self.fast_mod(offset)).cast_const() }
unsafe {
asm!("/* {ptr} */", ptr = in(reg) self.addr, options(nostack, preserves_flags));
self.addr.add(self.fast_mod(offset)).cast_const()
}
}

/// Returns an unsafe mutable pointer to the [`MagicBuffer`]. The `offset` species the first
Expand All @@ -181,16 +185,21 @@ impl MagicBuffer {
/// }
/// ```
pub fn as_mut_ptr(&mut self, offset: usize) -> *mut u8 {
unsafe { self.addr.add(self.fast_mod(offset)) }
unsafe {
asm!("/* {ptr} */", ptr = in(reg) self.addr, options(nostack, preserves_flags));
self.addr.add(self.fast_mod(offset))
}
}

#[inline(always)]
unsafe fn as_slice(&self, offset: usize, len: usize) -> &[u8] {
asm!("/* {ptr} */", ptr = in(reg) self.addr, options(nostack, preserves_flags));
&*(slice_from_raw_parts(self.addr.add(offset), len))
}

#[inline(always)]
unsafe fn as_slice_mut(&mut self, offset: usize, len: usize) -> &mut [u8] {
asm!("/* {ptr} */", ptr = in(reg) self.addr, options(nostack, preserves_flags));
&mut *(slice_from_raw_parts_mut(self.addr.add(offset), len))
}

Expand Down Expand Up @@ -224,13 +233,19 @@ impl Index<usize> for MagicBuffer {
type Output = u8;

fn index(&self, index: usize) -> &Self::Output {
unsafe { &*self.addr.add(self.fast_mod(index)) }
unsafe {
asm!("/* {ptr} */", ptr = in(reg) self.addr, options(nostack, preserves_flags));
&*self.addr.add(self.fast_mod(index))
}
}
}

impl IndexMut<usize> for MagicBuffer {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
unsafe { &mut *self.addr.add(self.fast_mod(index)) }
unsafe {
asm!("/* {ptr} */", ptr = in(reg) self.addr, options(nostack, preserves_flags));
&mut *self.addr.add(self.fast_mod(index))
}
}
}

Expand Down Expand Up @@ -271,7 +286,11 @@ impl Index<isize> for MagicBuffer {
} else {
self.fast_mod(index as usize)
};
unsafe { &*self.addr.add(index) }

unsafe {
asm!("/* {ptr} */", ptr = in(reg) self.addr, options(nostack, preserves_flags));
&*self.addr.add(index)
}
}
}

Expand All @@ -282,7 +301,11 @@ impl IndexMut<isize> for MagicBuffer {
} else {
self.fast_mod(index as usize)
};
unsafe { &mut *self.addr.add(index) }

unsafe {
asm!("/* {ptr} */", ptr = in(reg) self.addr, options(nostack, preserves_flags));
&mut *self.addr.add(index)
}
}
}

Expand Down Expand Up @@ -516,4 +539,20 @@ mod tests {
buf[-1] = b'2';
assert_eq!(b'2', buf[VALID_BUF_LEN - 1]);
}

#[cfg(target_os = "linux")]
#[test]
fn github_issue_6() {
let mut buf = MagicBuffer::new(4096).unwrap();

let a = buf[0..4096][0];
buf[1..4097][4095] = 1;

let b = buf[0..4096][0];
let c = buf[0..4096][0];

assert_eq!(0, a);
assert_eq!(1, b);
assert_eq!(1, c);
}
}