Skip to content

Commit

Permalink
Remove offset_inbounds for an unsafe offset function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Aug 28, 2013
1 parent 578e680 commit e3662b1
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/type_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn type_uses_for(ccx: @mut CrateContext, fn_id: def_id, n_tps: uint)
"visit_tydesc" | "forget" | "frame_address" |
"morestack_addr" => 0,

"offset" | "offset_inbounds" |
"offset" |
"memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
"memset32" | "memset64" => use_repr,

Expand Down
14 changes: 0 additions & 14 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3663,20 +3663,6 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) {
mutbl: ast::m_imm
}))
}
"offset_inbounds" => {
(1,
~[
ty::mk_ptr(tcx, ty::mt {
ty: param(ccx, 0),
mutbl: ast::m_imm
}),
ty::mk_int()
],
ty::mk_ptr(tcx, ty::mt {
ty: param(ccx, 0),
mutbl: ast::m_imm
}))
}
"memcpy32" => {
(1,
~[
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
Expand Down
98 changes: 22 additions & 76 deletions src/libstd/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -379,20 +371,14 @@ 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.
///
/// 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
Expand Down Expand Up @@ -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::*;
Expand Down Expand Up @@ -635,15 +581,15 @@ 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());

let mp: *mut int = mut_null();
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());
}
Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(']');
Expand Down
4 changes: 3 additions & 1 deletion src/libstd/rt/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down
12 changes: 3 additions & 9 deletions src/libstd/unstable/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>()`
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
}
Expand Down Expand Up @@ -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)}
}
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
}
Expand Down

5 comments on commit e3662b1

@bors
Copy link
Contributor

@bors bors commented on e3662b1 Aug 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from thestinger
at alexcrichton@e3662b1

@bors
Copy link
Contributor

@bors bors commented on e3662b1 Aug 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/remove-two-offsets = e3662b1 into auto

@bors
Copy link
Contributor

@bors bors commented on e3662b1 Aug 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/remove-two-offsets = e3662b1 merged ok, testing candidate = 64ed372

@bors
Copy link
Contributor

@bors bors commented on e3662b1 Aug 28, 2013

@bors
Copy link
Contributor

@bors bors commented on e3662b1 Aug 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 64ed372

Please sign in to comment.