Skip to content

Commit

Permalink
Use "stable" version of the intrinsic on stage0
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Sep 13, 2017
1 parent 18ab95f commit 2184663
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
33 changes: 1 addition & 32 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,38 +1381,7 @@ extern "rust-intrinsic" {
}

#[cfg(stage0)]
/// Computes the byte offset that needs to be applied to `ptr` in order to
/// make it aligned to `align`.
/// If it is not possible to align `ptr`, the implementation returns
/// `usize::max_value()`.
///
/// There are no guarantees whatsover that offsetting the pointer will not
/// overflow or go beyond the allocation that `ptr` points into.
/// It is up to the caller to ensure that the returned offset is correct
/// in all terms other than alignment.
///
/// # Examples
///
/// Accessing adjacent `u8` as `u16`
///
/// ```
/// # #![feature(core_intrinsics)]
/// # fn foo(n: usize) {
/// # use std::intrinsics::align_offset;
/// # use std::mem::align_of;
/// # unsafe {
/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
/// let ptr = &x[n] as *const u8;
/// let offset = align_offset(ptr as *const (), align_of::<u16>());
/// if offset < x.len() - n - 1 {
/// let u16_ptr = ptr.offset(offset as isize) as *const u16;
/// assert_ne!(*u16_ptr, 500);
/// } else {
/// // while the pointer can be aligned via `offset`, it would point
/// // outside the allocation
/// }
/// # } }
/// ```
/// remove me after the next release
pub unsafe fn align_offset(ptr: *const (), align: usize) -> usize {
let offset = ptr as usize % align;
if offset == 0 {
Expand Down
20 changes: 20 additions & 0 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,22 @@ impl<T: ?Sized> *const T {
/// }
/// # } }
/// ```
#[cfg(not(stage0))]
#[unstable(feature = "align_offset", issue = "44488")]
pub fn align_offset(self, align: usize) -> usize {
unsafe {
intrinsics::align_offset(self as *const _, align)
}
}

#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
/// remove me after the next release
pub fn align_offset(self, align: usize) -> usize {
unsafe {
intrinsics::align_offset(self as *const _, align)
}
}
}

#[lang = "mut_ptr"]
Expand Down Expand Up @@ -891,12 +901,22 @@ impl<T: ?Sized> *mut T {
/// }
/// # } }
/// ```
#[cfg(not(stage0))]
#[unstable(feature = "align_offset", issue = "44488")]
pub fn align_offset(self, align: usize) -> usize {
unsafe {
intrinsics::align_offset(self as *const _, align)
}
}

#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
/// remove me after the next release
pub fn align_offset(self, align: usize) -> usize {
unsafe {
intrinsics::align_offset(self as *const _, align)
}
}
}

// Equality for pointers
Expand Down
3 changes: 1 addition & 2 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use fmt;
use iter::{Map, Cloned, FusedIterator};
use slice::{self, SliceIndex};
use mem;
use intrinsics::align_offset;

pub mod pattern;

Expand Down Expand Up @@ -1471,7 +1470,7 @@ fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
let ptr = v.as_ptr();
let align = unsafe {
// the offset is safe, because `index` is guaranteed inbounds
align_offset(ptr.offset(index as isize) as *const (), usize_bytes)
ptr.offset(index as isize).align_offset(usize_bytes)
};
if align == 0 {
while index < blocks_end {
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
#![feature(allocator_internals)]
#![feature(allow_internal_unsafe)]
#![feature(allow_internal_unstable)]
#![cfg_attr(not(stage0), feature(align_offset))]
#![feature(asm)]
#![feature(box_syntax)]
#![feature(cfg_target_has_atomic)]
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys_common/memchr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
pub mod fallback {
use cmp;
use mem;
use intrinsics::align_offset;

const LO_U64: u64 = 0x0101010101010101;
const HI_U64: u64 = 0x8080808080808080;
Expand Down Expand Up @@ -66,7 +65,7 @@ pub mod fallback {
let usize_bytes = mem::size_of::<usize>();

// search up to an aligned boundary
let mut offset = unsafe { align_offset(ptr as *const _, usize_bytes) };
let mut offset = ptr.align_offset(usize_bytes);
if offset > 0 {
offset = cmp::min(offset, len);
if let Some(index) = text[..offset].iter().position(|elt| *elt == x) {
Expand Down

0 comments on commit 2184663

Please sign in to comment.