Skip to content

Commit

Permalink
Offset from (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed May 11, 2024
1 parent 86694b0 commit 4950c50
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 39 deletions.
25 changes: 4 additions & 21 deletions src/bytes.rs
Expand Up @@ -15,7 +15,7 @@ use crate::buf::IntoIter;
#[allow(unused)]
use crate::loom::sync::atomic::AtomicMut;
use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use crate::{Buf, BytesMut};
use crate::{offset_from, Buf, BytesMut};

/// A cheaply cloneable and sliceable chunk of contiguous memory.
///
Expand Down Expand Up @@ -1037,7 +1037,7 @@ unsafe fn promotable_to_vec(

let buf = f(shared);

let cap = (ptr as usize - buf as usize) + len;
let cap = offset_from(ptr, buf) + len;

// Copy back buffer
ptr::copy(ptr, buf, len);
Expand Down Expand Up @@ -1150,7 +1150,7 @@ unsafe fn promotable_is_unique(data: &AtomicPtr<()>) -> bool {
}

unsafe fn free_boxed_slice(buf: *mut u8, offset: *const u8, len: usize) {
let cap = (offset as usize - buf as usize) + len;
let cap = offset_from(offset, buf) + len;
dealloc(buf, Layout::from_size_align(cap, 1).unwrap())
}

Expand Down Expand Up @@ -1312,7 +1312,7 @@ unsafe fn shallow_clone_vec(
// vector.
let shared = Box::new(Shared {
buf,
cap: (offset as usize - buf as usize) + len,
cap: offset_from(offset, buf) + len,
// Initialize refcount to 2. One for this reference, and one
// for the new clone that will be returned from
// `shallow_clone`.
Expand Down Expand Up @@ -1422,23 +1422,6 @@ where
new_addr as *mut u8
}

/// Precondition: dst >= original
///
/// The following line is equivalent to:
///
/// ```rust,ignore
/// self.ptr.as_ptr().offset_from(ptr) as usize;
/// ```
///
/// But due to min rust is 1.39 and it is only stabilized
/// in 1.47, we cannot use it.
#[inline]
fn offset_from(dst: *const u8, original: *const u8) -> usize {
debug_assert!(dst >= original);

dst as usize - original as usize
}

// compile-fails

/// ```compile_fail
Expand Down
19 changes: 1 addition & 18 deletions src/bytes_mut.rs
Expand Up @@ -17,7 +17,7 @@ use crate::bytes::Vtable;
#[allow(unused)]
use crate::loom::sync::atomic::AtomicMut;
use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use crate::{Buf, BufMut, Bytes};
use crate::{offset_from, Buf, BufMut, Bytes};

/// A unique reference to a contiguous slice of memory.
///
Expand Down Expand Up @@ -1683,23 +1683,6 @@ fn invalid_ptr<T>(addr: usize) -> *mut T {
ptr.cast::<T>()
}

/// Precondition: dst >= original
///
/// The following line is equivalent to:
///
/// ```rust,ignore
/// self.ptr.as_ptr().offset_from(ptr) as usize;
/// ```
///
/// But due to min rust is 1.39 and it is only stabilized
/// in 1.47, we cannot use it.
#[inline]
fn offset_from(dst: *mut u8, original: *mut u8) -> usize {
debug_assert!(dst >= original);

dst as usize - original as usize
}

unsafe fn rebuild_vec(ptr: *mut u8, mut len: usize, mut cap: usize, off: usize) -> Vec<u8> {
let ptr = ptr.sub(off);
len += off;
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Expand Up @@ -148,3 +148,18 @@ fn panic_does_not_fit(size: usize, nbytes: usize) -> ! {
size, nbytes
);
}

/// Precondition: dst >= original
///
/// The following line is equivalent to:
///
/// ```rust,ignore
/// self.ptr.as_ptr().offset_from(ptr) as usize;
/// ```
///
/// But due to min rust is 1.39 and it is only stabilized
/// in 1.47, we cannot use it.
#[inline]
fn offset_from(dst: *const u8, original: *const u8) -> usize {
dst as usize - original as usize
}

0 comments on commit 4950c50

Please sign in to comment.