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

Merge promotable implementations #489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 36 additions & 35 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,30 +862,55 @@ unsafe fn static_drop(_: &mut AtomicPtr<()>, _: *const u8, _: usize) {

// ===== impl PromotableVtable =====

trait PromotableEvenness {
const SLICE_PTR_IS_ODD: bool;
}

struct PromotableEven;
struct PromotableOdd;

impl PromotableEvenness for PromotableEven {
const SLICE_PTR_IS_ODD: bool = false;
}
impl PromotableEvenness for PromotableOdd {
const SLICE_PTR_IS_ODD: bool = true;
}

static PROMOTABLE_EVEN_VTABLE: Vtable = Vtable {
clone: promotable_even_clone,
drop: promotable_even_drop,
clone: promotable_clone::<PromotableEven>,
drop: promotable_drop::<PromotableEven>,
};

static PROMOTABLE_ODD_VTABLE: Vtable = Vtable {
clone: promotable_odd_clone,
drop: promotable_odd_drop,
clone: promotable_clone::<PromotableOdd>,
drop: promotable_drop::<PromotableOdd>,
};

unsafe fn promotable_even_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes {
unsafe fn promotable_clone<E: PromotableEvenness>(
data: &AtomicPtr<()>,
ptr: *const u8,
len: usize,
) -> Bytes {
let shared = data.load(Ordering::Acquire);
let kind = shared as usize & KIND_MASK;

if kind == KIND_ARC {
shallow_clone_arc(shared as _, ptr, len)
} else {
debug_assert_eq!(kind, KIND_VEC);
let buf = (shared as usize & !KIND_MASK) as *mut u8;
let buf = match E::SLICE_PTR_IS_ODD {
false => (shared as usize & !KIND_MASK) as *mut u8,
true => shared as *mut u8,
};
shallow_clone_vec(data, shared, buf, ptr, len)
}
}

unsafe fn promotable_even_drop(data: &mut AtomicPtr<()>, ptr: *const u8, len: usize) {
unsafe fn promotable_drop<E: PromotableEvenness>(
data: &mut AtomicPtr<()>,
ptr: *const u8,
len: usize,
) {
data.with_mut(|shared| {
let shared = *shared;
let kind = shared as usize & KIND_MASK;
Expand All @@ -894,39 +919,15 @@ unsafe fn promotable_even_drop(data: &mut AtomicPtr<()>, ptr: *const u8, len: us
release_shared(shared as *mut Shared);
} else {
debug_assert_eq!(kind, KIND_VEC);
let buf = (shared as usize & !KIND_MASK) as *mut u8;
let buf = match E::SLICE_PTR_IS_ODD {
false => (shared as usize & !KIND_MASK) as *mut u8,
true => shared as *mut u8,
};
drop(rebuild_boxed_slice(buf, ptr, len));
}
});
}

unsafe fn promotable_odd_clone(data: &AtomicPtr<()>, ptr: *const u8, len: usize) -> Bytes {
let shared = data.load(Ordering::Acquire);
let kind = shared as usize & KIND_MASK;

if kind == KIND_ARC {
shallow_clone_arc(shared as _, ptr, len)
} else {
debug_assert_eq!(kind, KIND_VEC);
shallow_clone_vec(data, shared, shared as *mut u8, ptr, len)
}
}

unsafe fn promotable_odd_drop(data: &mut AtomicPtr<()>, ptr: *const u8, len: usize) {
data.with_mut(|shared| {
let shared = *shared;
let kind = shared as usize & KIND_MASK;

if kind == KIND_ARC {
release_shared(shared as *mut Shared);
} else {
debug_assert_eq!(kind, KIND_VEC);

drop(rebuild_boxed_slice(shared as *mut u8, ptr, len));
}
});
}

unsafe fn rebuild_boxed_slice(buf: *mut u8, offset: *const u8, len: usize) -> Box<[u8]> {
let cap = (offset as usize - buf as usize) + len;
Box::from_raw(slice::from_raw_parts_mut(buf, cap))
Expand Down