diff options
| author | bors <bors@rust-lang.org> | 2013-08-28 11:00:41 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-08-28 11:00:41 -0700 |
| commit | 64ed3721f74c699d931e1d5ccf41d5db7cea0218 (patch) | |
| tree | ace0551feb3ef6398fcc787e621f027c00efea03 /src/libstd | |
| parent | 9708ef03a6c9d52daf424c933428ccdfc4e29a56 (diff) | |
| parent | e3662b188041b50c732896ded3de823144ae6556 (diff) | |
| download | rust-64ed3721f74c699d931e1d5ccf41d5db7cea0218.tar.gz rust-64ed3721f74c699d931e1d5ccf41d5db7cea0218.zip | |
auto merge of #8807 : alexcrichton/rust/remove-two-offsets, r=thestinger
Everything that we do is actually inbounds, so there's no reason for us to be exposing two of these functions
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/c_str.rs | 4 | ||||
| -rw-r--r-- | src/libstd/ptr.rs | 98 | ||||
| -rw-r--r-- | src/libstd/repr.rs | 2 | ||||
| -rw-r--r-- | src/libstd/rt/stack.rs | 4 | ||||
| -rw-r--r-- | src/libstd/str.rs | 2 | ||||
| -rw-r--r-- | src/libstd/unstable/intrinsics.rs | 12 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 8 |
7 files changed, 36 insertions, 94 deletions
diff --git a/src/libstd/c_str.rs b/src/libstd/c_str.rs index df2fe70ff0e..70b04e37ee1 100644 --- a/src/libstd/c_str.rs +++ b/src/libstd/c_str.rs @@ -179,7 +179,7 @@ impl<'self> ToCStr for &'self [u8] { do cs.with_mut_ref |buf| { for i in range(0, self.len()) { unsafe { - let p = buf.offset_inbounds(i as int); + let p = buf.offset(i as int); if *p == 0 { match null_byte::cond.raise(self.to_owned()) { Truncate => break, @@ -222,7 +222,7 @@ impl<'self> Iterator<libc::c_char> for CStringIterator<'self> { if ch == 0 { None } else { - self.ptr = ptr::offset(self.ptr, 1); + self.ptr = unsafe { ptr::offset(self.ptr, 1) }; Some(ch) } } diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 860b1f4b768..02469527b7a 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -20,37 +20,36 @@ use sys; use unstable::intrinsics; use util::swap; -#[cfg(not(test))] use ops::{Add,Sub}; -#[cfg(not(test))] use num::Int; - #[cfg(not(test))] use cmp::{Eq, Ord}; -/// Calculate the offset from a pointer +/// Calculate the offset from a pointer. The count *must* be in bounds or +/// otherwise the loads of this address are undefined. #[inline] #[cfg(stage0)] -pub fn offset<T>(ptr: *T, count: int) -> *T { +pub unsafe fn offset<T>(ptr: *T, count: int) -> *T { (ptr as uint + (count as uint) * sys::size_of::<T>()) as *T } /// Calculate the offset from a mut pointer #[inline] #[cfg(stage0)] -pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T { +pub unsafe fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T { (ptr as uint + (count as uint) * sys::size_of::<T>()) as *mut T } /// Calculate the offset from a pointer #[inline] #[cfg(not(stage0))] -pub fn offset<T>(ptr: *T, count: int) -> *T { - unsafe { intrinsics::offset(ptr, count) } +pub unsafe fn offset<T>(ptr: *T, count: int) -> *T { + intrinsics::offset(ptr, count) } -/// Calculate the offset from a mut pointer +/// Calculate the offset from a mut pointer. The count *must* be in bounds or +/// otherwise the loads of this address are undefined. #[inline] #[cfg(not(stage0))] -pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T { - unsafe { intrinsics::offset(ptr as *T, count) as *mut T } +pub unsafe fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T { + intrinsics::offset(ptr as *T, count) as *mut T } /// Return the offset of the first null pointer in `buf`. @@ -293,8 +292,7 @@ pub trait RawPtr<T> { fn is_not_null(&self) -> bool; fn to_uint(&self) -> uint; unsafe fn to_option(&self) -> Option<&T>; - fn offset(&self, count: int) -> Self; - unsafe fn offset_inbounds(self, count: int) -> Self; + unsafe fn offset(self, count: int) -> Self; } /// Extension methods for immutable pointers @@ -332,16 +330,10 @@ impl<T> RawPtr<T> for *T { } } - /// Calculates the offset from a pointer. - #[inline] - fn offset(&self, count: int) -> *T { offset(*self, count) } - /// Calculates the offset from a pointer. The offset *must* be in-bounds of /// the object, or one-byte-past-the-end. #[inline] - unsafe fn offset_inbounds(self, count: int) -> *T { - intrinsics::offset_inbounds(self, count) - } + unsafe fn offset(self, count: int) -> *T { offset(self, count) } } /// Extension methods for mutable pointers @@ -379,10 +371,6 @@ impl<T> RawPtr<T> for *mut T { } } - /// Calculates the offset from a mutable pointer. - #[inline] - fn offset(&self, count: int) -> *mut T { mut_offset(*self, count) } - /// Calculates the offset from a pointer. The offset *must* be in-bounds of /// the object, or one-byte-past-the-end. An arithmetic overflow is also /// undefined behaviour. @@ -390,9 +378,7 @@ impl<T> RawPtr<T> for *mut T { /// This method should be preferred over `offset` when the guarantee can be /// satisfied, to enable better optimization. #[inline] - unsafe fn offset_inbounds(self, count: int) -> *mut T { - intrinsics::offset_inbounds(self as *T, count) as *mut T - } + unsafe fn offset(self, count: int) -> *mut T { mut_offset(self, count) } } // Equality for pointers @@ -513,46 +499,6 @@ impl<T> Ord for *mut T { } } -#[cfg(not(test))] -impl<T, I: Int> Add<I, *T> for *T { - /// Add an integer value to a pointer to get an offset pointer. - /// Is calculated according to the size of the type pointed to. - #[inline] - fn add(&self, rhs: &I) -> *T { - self.offset(rhs.to_int() as int) - } -} - -#[cfg(not(test))] -impl<T, I: Int> Sub<I, *T> for *T { - /// Subtract an integer value from a pointer to get an offset pointer. - /// Is calculated according to the size of the type pointed to. - #[inline] - fn sub(&self, rhs: &I) -> *T { - self.offset(-rhs.to_int() as int) - } -} - -#[cfg(not(test))] -impl<T, I: Int> Add<I, *mut T> for *mut T { - /// Add an integer value to a pointer to get an offset pointer. - /// Is calculated according to the size of the type pointed to. - #[inline] - fn add(&self, rhs: &I) -> *mut T { - self.offset(rhs.to_int() as int) - } -} - -#[cfg(not(test))] -impl<T, I: Int> Sub<I, *mut T> for *mut T { - /// Subtract an integer value from a pointer to get an offset pointer. - /// Is calculated according to the size of the type pointed to. - #[inline] - fn sub(&self, rhs: &I) -> *mut T { - self.offset(-rhs.to_int() as int) - } -} - #[cfg(test)] pub mod ptr_tests { use super::*; @@ -635,7 +581,7 @@ pub mod ptr_tests { assert!(p.is_null()); assert!(!p.is_not_null()); - let q = offset(p, 1); + let q = unsafe { offset(p, 1) }; assert!(!q.is_null()); assert!(q.is_not_null()); @@ -643,7 +589,7 @@ pub mod ptr_tests { assert!(mp.is_null()); assert!(!mp.is_not_null()); - let mq = mp.offset(1); + let mq = unsafe { mp.offset(1) }; assert!(!mq.is_null()); assert!(mq.is_not_null()); } @@ -672,20 +618,20 @@ pub mod ptr_tests { unsafe { let xs = ~[5, ..16]; let mut ptr = to_ptr(xs); - let end = ptr + 16; + let end = ptr.offset(16); while ptr < end { assert_eq!(*ptr, 5); - ptr = ptr + 1u; + ptr = ptr.offset(1); } let mut xs_mut = xs.clone(); let mut m_ptr = to_mut_ptr(xs_mut); - let m_end = m_ptr + 16i16; + let m_end = m_ptr.offset(16); while m_ptr < m_end { *m_ptr += 5; - m_ptr = m_ptr + 1u8; + m_ptr = m_ptr.offset(1); } assert_eq!(xs_mut, ~[10, ..16]); @@ -702,17 +648,17 @@ pub mod ptr_tests { let ptr = to_ptr(xs); while idx >= 0i8 { - assert_eq!(*(ptr + idx), idx as int); + assert_eq!(*(ptr.offset(idx as int)), idx as int); idx = idx - 1i8; } let mut xs_mut = xs.clone(); let m_start = to_mut_ptr(xs_mut); - let mut m_ptr = m_start + 9u32; + let mut m_ptr = m_start.offset(9); while m_ptr >= m_start { *m_ptr += *m_ptr; - m_ptr = m_ptr - 1i8; + m_ptr = m_ptr.offset(-1); } assert_eq!(xs_mut, ~[0,2,4,6,8,10,12,14,16,18]); diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs index ad4658d2f42..588010bd5b1 100644 --- a/src/libstd/repr.rs +++ b/src/libstd/repr.rs @@ -228,7 +228,7 @@ impl ReprVisitor { self.writer.write_str(", "); } self.visit_ptr_inner(p as *c_void, inner); - p = align(ptr::offset(p, sz as int) as uint, al) as *u8; + p = align(unsafe { ptr::offset(p, sz as int) as uint }, al) as *u8; left -= dec; } self.writer.write_char(']'); diff --git a/src/libstd/rt/stack.rs b/src/libstd/rt/stack.rs index 4b2a9b7a6cc..da70659acec 100644 --- a/src/libstd/rt/stack.rs +++ b/src/libstd/rt/stack.rs @@ -46,7 +46,9 @@ impl StackSegment { /// Point one word beyond the high end of the allocated stack pub fn end(&self) -> *uint { - vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint + unsafe { + vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint + } } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index e6cadd04a5b..9ca6e8ad089 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1092,7 +1092,7 @@ pub mod raw { pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str { do s.as_imm_buf |sbuf, _n| { cast::transmute(Slice { - data: sbuf.offset_inbounds(begin as int), + data: sbuf.offset(begin as int), len: end - begin, }) } diff --git a/src/libstd/unstable/intrinsics.rs b/src/libstd/unstable/intrinsics.rs index 796567bd561..cdd93ce87c3 100644 --- a/src/libstd/unstable/intrinsics.rs +++ b/src/libstd/unstable/intrinsics.rs @@ -332,19 +332,13 @@ extern "rust-intrinsic" { /// Get the address of the `__morestack` stack growth function. pub fn morestack_addr() -> *(); - /// Calculates the offset from a pointer. - /// - /// This is implemented as an intrinsic to avoid converting to and from an - /// integer, since the conversion would throw away aliasing information. - pub fn offset<T>(dst: *T, offset: int) -> *T; - /// Calculates the offset from a pointer. The offset *must* be in-bounds of /// the object, or one-byte-past-the-end. An arithmetic overflow is also /// undefined behaviour. /// - /// This intrinsic should be preferred over `offset` when the guarantee can - /// be satisfied, to enable better optimization. - pub fn offset_inbounds<T>(dst: *T, offset: int) -> *T; + /// This is implemented as an intrinsic to avoid converting to and from an + /// integer, since the conversion would throw away aliasing information. + pub fn offset<T>(dst: *T, offset: int) -> *T; /// Equivalent to the `llvm.memcpy.p0i8.0i8.i32` intrinsic, with a size of /// `count` * `size_of::<T>()` and an alignment of `min_align_of::<T>()` diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 8cd1b09468d..12aebe20161 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -896,7 +896,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { lifetime: cast::transmute(p)} } else { VecIterator{ptr: p, - end: p.offset_inbounds(self.len() as int), + end: p.offset(self.len() as int), lifetime: cast::transmute(p)} } } @@ -1884,7 +1884,7 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] { lifetime: cast::transmute(p)} } else { VecMutIterator{ptr: p, - end: p.offset_inbounds(self.len() as int), + end: p.offset(self.len() as int), lifetime: cast::transmute(p)} } } @@ -2247,7 +2247,7 @@ macro_rules! iterator { // same pointer. cast::transmute(self.ptr as uint + 1) } else { - self.ptr.offset_inbounds(1) + self.ptr.offset(1) }; Some(cast::transmute(old)) @@ -2279,7 +2279,7 @@ macro_rules! double_ended_iterator { // See above for why 'ptr.offset' isn't used cast::transmute(self.end as uint - 1) } else { - self.end.offset_inbounds(-1) + self.end.offset(-1) }; Some(cast::transmute(self.end)) } |
