Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
778d5f2
Add Cow<str> -> Box<Error> impls.
Sep 9, 2017
7859c9e
std: Document thread builder panics for nul bytes in thread names
bluss Sep 17, 2017
7b932d2
stabilized vec_splice (fixes #32310)
budziq Sep 16, 2017
6b167f9
Updated tracking issue for String::splice and its unstable-book entry
budziq Sep 17, 2017
2633b85
Replace str's transmute() calls with pointer casts
Ixrec Sep 17, 2017
38fa340
missed a 'mut'
Ixrec Sep 17, 2017
2787a28
Add `<*const T>::align_offset` and use it in `memchr`
oli-obk Sep 13, 2017
e47279f
Add more links and put the link character to the left
GuillaumeGomez Sep 17, 2017
ebd0e4f
Add Example of `IntoIterator` as Trait Bound to Docs
iwillspeak Sep 18, 2017
3cf28f3
Use DefId instead of NodeId as identifier in resolve_lifetime::Region.
michaelwoerister Aug 15, 2017
e6c9a53
incr.comp.: Compute hashes of all query results.
michaelwoerister Aug 14, 2017
3cc3ae2
incr.comp.: Move result fingerprinting to DepGraph::with_task().
michaelwoerister Sep 7, 2017
e3f9131
Fix issues uncovered by rebasing:
michaelwoerister Sep 12, 2017
b9816c5
incr.comp.: Already hash HIR bodies during metadata export so they do…
michaelwoerister Sep 13, 2017
67c84e0
incr.comp.: Use StableHash impls instead of functions for hashing mos…
michaelwoerister Sep 13, 2017
dd50173
incr.comp.: Initialize the CachingCodemapView in StableHashingContext…
michaelwoerister Sep 14, 2017
e567afb
incr.comp.: Initialize IGNORED_ATTRS in StableHashingContext lazily.
michaelwoerister Sep 14, 2017
ba6f93c
incr.comp.: Make the StableHashingContext mostly independent of the tcx.
michaelwoerister Sep 14, 2017
d5b1fee
incr.comp.: Remove tcx from StableHashingContext.
michaelwoerister Sep 14, 2017
74d6b85
incr.comp.: Fix rebase fallout.
michaelwoerister Sep 18, 2017
4961a8e
incr.comp.: Fix ICE caused by trying to hash INVALID_CRATE_NUM.
michaelwoerister Sep 18, 2017
90ce24a
Fix run button
GuillaumeGomez Sep 18, 2017
1f9b02b
ci: Use multiple codegen units on non-dist bots
alexcrichton Sep 18, 2017
2ab4840
Rollup merge of #44364 - michaelwoerister:hash-all-the-things2, r=nik…
alexcrichton Sep 18, 2017
0a3de32
Rollup merge of #44466 - clarcharr:cow_error, r=alexcrichton
alexcrichton Sep 18, 2017
1907827
Rollup merge of #44537 - oli-obk:memchr, r=alexcrichton
alexcrichton Sep 18, 2017
054a46e
Rollup merge of #44640 - budziq:stabilize_splice, r=dtolnay
alexcrichton Sep 18, 2017
fba954b
Rollup merge of #44651 - bluss:document-thread-name-no-nuls, r=stevek…
alexcrichton Sep 18, 2017
97990cd
Rollup merge of #44657 - Ixrec:patch-1, r=eddyb
alexcrichton Sep 18, 2017
8f998d3
Rollup merge of #44661 - GuillaumeGomez:more-links, r=QuietMisdreavus
alexcrichton Sep 18, 2017
196e8ae
Rollup merge of #44668 - iwillspeak:into-iterator-docs, r=steveklabnik
alexcrichton Sep 18, 2017
cfa32bf
Rollup merge of #44671 - GuillaumeGomez:run-button, r=steveklabnik
alexcrichton Sep 18, 2017
91f3adf
Rollup merge of #44675 - alexcrichton:many-cgu, r=aidanhs
alexcrichton Sep 18, 2017
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
Prev Previous commit
Next Next commit
Add <*const T>::align_offset and use it in memchr
  • Loading branch information
oli-obk committed Sep 17, 2017
commit 2787a285bd211ecbf75fd95d990226242005d848
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
75 changes: 74 additions & 1 deletion src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,43 @@ impl<T: ?Sized> *const T {
copy_nonoverlapping(self, dest, count)
}


/// Computes the byte offset that needs to be applied in order to
/// make the pointer aligned to `align`.
/// If it is not possible to align the pointer, the implementation returns
/// `usize::max_value()`.
///
/// There are no guarantees whatsover that offsetting the pointer will not
/// overflow or go beyond the allocation that the pointer 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(align_offset)]
/// # fn foo(n: usize) {
/// # use std::mem::align_of;
/// # unsafe {
/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
/// let ptr = &x[n] as *const u8;
/// let offset = ptr.align_offset(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
/// }
/// # } }
/// ```
#[unstable(feature = "align_offset", issue = "44488")]
pub fn align_offset(self, align: usize) -> usize {
unsafe {
intrinsics::align_offset(self as *const _, align)
}
}
}

#[lang = "mut_ptr"]
Expand Down Expand Up @@ -1284,6 +1320,43 @@ impl<T: ?Sized> *mut T {
}
}

/// Computes the byte offset that needs to be applied in order to
/// make the pointer aligned to `align`.
/// If it is not possible to align the pointer, the implementation returns
/// `usize::max_value()`.
///
/// There are no guarantees whatsover that offsetting the pointer will not
/// overflow or go beyond the allocation that the pointer 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(align_offset)]
/// # fn foo(n: usize) {
/// # use std::mem::align_of;
/// # unsafe {
/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
/// let ptr = &x[n] as *const u8;
/// let offset = ptr.align_offset(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
/// }
/// # } }
/// ```
#[unstable(feature = "align_offset", issue = "44488")]
pub fn align_offset(self, align: usize) -> usize {
unsafe {
intrinsics::align_offset(self as *const _, align)
}
}

/// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
///
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 @@ -1515,7 +1514,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)]
#![feature(align_offset)]
#![feature(asm)]
#![feature(box_syntax)]
#![feature(cfg_target_has_atomic)]
Expand Down
9 changes: 3 additions & 6 deletions src/libstd/sys_common/memchr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,12 @@ pub mod fallback {
let usize_bytes = mem::size_of::<usize>();

// search up to an aligned boundary
let align = (ptr as usize) & (usize_bytes- 1);
let mut offset;
if align > 0 {
offset = cmp::min(usize_bytes - align, len);
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) {
return Some(index);
}
} else {
offset = 0;
}

// search the body of the text
Expand Down