Remove offset_inbounds for an unsafe offset function
This commit is contained in:
@@ -149,7 +149,7 @@ pub fn type_uses_for(ccx: @mut CrateContext, fn_id: def_id, n_tps: uint)
|
|||||||
"visit_tydesc" | "forget" | "frame_address" |
|
"visit_tydesc" | "forget" | "frame_address" |
|
||||||
"morestack_addr" => 0,
|
"morestack_addr" => 0,
|
||||||
|
|
||||||
"offset" | "offset_inbounds" |
|
"offset" |
|
||||||
"memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
|
"memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
|
||||||
"memset32" | "memset64" => use_repr,
|
"memset32" | "memset64" => use_repr,
|
||||||
|
|
||||||
|
|||||||
@@ -3663,20 +3663,6 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) {
|
|||||||
mutbl: ast::m_imm
|
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" => {
|
"memcpy32" => {
|
||||||
(1,
|
(1,
|
||||||
~[
|
~[
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ impl<'self> ToCStr for &'self [u8] {
|
|||||||
do cs.with_mut_ref |buf| {
|
do cs.with_mut_ref |buf| {
|
||||||
for i in range(0, self.len()) {
|
for i in range(0, self.len()) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let p = buf.offset_inbounds(i as int);
|
let p = buf.offset(i as int);
|
||||||
if *p == 0 {
|
if *p == 0 {
|
||||||
match null_byte::cond.raise(self.to_owned()) {
|
match null_byte::cond.raise(self.to_owned()) {
|
||||||
Truncate => break,
|
Truncate => break,
|
||||||
@@ -222,7 +222,7 @@ impl<'self> Iterator<libc::c_char> for CStringIterator<'self> {
|
|||||||
if ch == 0 {
|
if ch == 0 {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
self.ptr = ptr::offset(self.ptr, 1);
|
self.ptr = unsafe { ptr::offset(self.ptr, 1) };
|
||||||
Some(ch)
|
Some(ch)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,37 +20,36 @@ use sys;
|
|||||||
use unstable::intrinsics;
|
use unstable::intrinsics;
|
||||||
use util::swap;
|
use util::swap;
|
||||||
|
|
||||||
#[cfg(not(test))] use ops::{Add,Sub};
|
|
||||||
#[cfg(not(test))] use num::Int;
|
|
||||||
|
|
||||||
#[cfg(not(test))] use cmp::{Eq, Ord};
|
#[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]
|
#[inline]
|
||||||
#[cfg(stage0)]
|
#[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
|
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *T
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate the offset from a mut pointer
|
/// Calculate the offset from a mut pointer
|
||||||
#[inline]
|
#[inline]
|
||||||
#[cfg(stage0)]
|
#[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
|
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *mut T
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate the offset from a pointer
|
/// Calculate the offset from a pointer
|
||||||
#[inline]
|
#[inline]
|
||||||
#[cfg(not(stage0))]
|
#[cfg(not(stage0))]
|
||||||
pub fn offset<T>(ptr: *T, count: int) -> *T {
|
pub unsafe fn offset<T>(ptr: *T, count: int) -> *T {
|
||||||
unsafe { intrinsics::offset(ptr, count) }
|
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]
|
#[inline]
|
||||||
#[cfg(not(stage0))]
|
#[cfg(not(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 {
|
||||||
unsafe { intrinsics::offset(ptr as *T, count) as *mut T }
|
intrinsics::offset(ptr as *T, count) as *mut T
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the offset of the first null pointer in `buf`.
|
/// 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 is_not_null(&self) -> bool;
|
||||||
fn to_uint(&self) -> uint;
|
fn to_uint(&self) -> uint;
|
||||||
unsafe fn to_option(&self) -> Option<&T>;
|
unsafe fn to_option(&self) -> Option<&T>;
|
||||||
fn offset(&self, count: int) -> Self;
|
unsafe fn offset(self, count: int) -> Self;
|
||||||
unsafe fn offset_inbounds(self, count: int) -> Self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extension methods for immutable pointers
|
/// 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
|
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
|
||||||
/// the object, or one-byte-past-the-end.
|
/// the object, or one-byte-past-the-end.
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn offset_inbounds(self, count: int) -> *T {
|
unsafe fn offset(self, count: int) -> *T { offset(self, count) }
|
||||||
intrinsics::offset_inbounds(self, count)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extension methods for mutable pointers
|
/// 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
|
/// 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
|
/// the object, or one-byte-past-the-end. An arithmetic overflow is also
|
||||||
/// undefined behaviour.
|
/// 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
|
/// This method should be preferred over `offset` when the guarantee can be
|
||||||
/// satisfied, to enable better optimization.
|
/// satisfied, to enable better optimization.
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn offset_inbounds(self, count: int) -> *mut T {
|
unsafe fn offset(self, count: int) -> *mut T { mut_offset(self, count) }
|
||||||
intrinsics::offset_inbounds(self as *T, count) as *mut T
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equality for pointers
|
// 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)]
|
#[cfg(test)]
|
||||||
pub mod ptr_tests {
|
pub mod ptr_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -635,7 +581,7 @@ pub mod ptr_tests {
|
|||||||
assert!(p.is_null());
|
assert!(p.is_null());
|
||||||
assert!(!p.is_not_null());
|
assert!(!p.is_not_null());
|
||||||
|
|
||||||
let q = offset(p, 1);
|
let q = unsafe { offset(p, 1) };
|
||||||
assert!(!q.is_null());
|
assert!(!q.is_null());
|
||||||
assert!(q.is_not_null());
|
assert!(q.is_not_null());
|
||||||
|
|
||||||
@@ -643,7 +589,7 @@ pub mod ptr_tests {
|
|||||||
assert!(mp.is_null());
|
assert!(mp.is_null());
|
||||||
assert!(!mp.is_not_null());
|
assert!(!mp.is_not_null());
|
||||||
|
|
||||||
let mq = mp.offset(1);
|
let mq = unsafe { mp.offset(1) };
|
||||||
assert!(!mq.is_null());
|
assert!(!mq.is_null());
|
||||||
assert!(mq.is_not_null());
|
assert!(mq.is_not_null());
|
||||||
}
|
}
|
||||||
@@ -672,20 +618,20 @@ pub mod ptr_tests {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let xs = ~[5, ..16];
|
let xs = ~[5, ..16];
|
||||||
let mut ptr = to_ptr(xs);
|
let mut ptr = to_ptr(xs);
|
||||||
let end = ptr + 16;
|
let end = ptr.offset(16);
|
||||||
|
|
||||||
while ptr < end {
|
while ptr < end {
|
||||||
assert_eq!(*ptr, 5);
|
assert_eq!(*ptr, 5);
|
||||||
ptr = ptr + 1u;
|
ptr = ptr.offset(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut xs_mut = xs.clone();
|
let mut xs_mut = xs.clone();
|
||||||
let mut m_ptr = to_mut_ptr(xs_mut);
|
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 {
|
while m_ptr < m_end {
|
||||||
*m_ptr += 5;
|
*m_ptr += 5;
|
||||||
m_ptr = m_ptr + 1u8;
|
m_ptr = m_ptr.offset(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(xs_mut, ~[10, ..16]);
|
assert_eq!(xs_mut, ~[10, ..16]);
|
||||||
@@ -702,17 +648,17 @@ pub mod ptr_tests {
|
|||||||
let ptr = to_ptr(xs);
|
let ptr = to_ptr(xs);
|
||||||
|
|
||||||
while idx >= 0i8 {
|
while idx >= 0i8 {
|
||||||
assert_eq!(*(ptr + idx), idx as int);
|
assert_eq!(*(ptr.offset(idx as int)), idx as int);
|
||||||
idx = idx - 1i8;
|
idx = idx - 1i8;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut xs_mut = xs.clone();
|
let mut xs_mut = xs.clone();
|
||||||
let m_start = to_mut_ptr(xs_mut);
|
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 {
|
while m_ptr >= m_start {
|
||||||
*m_ptr += *m_ptr;
|
*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]);
|
assert_eq!(xs_mut, ~[0,2,4,6,8,10,12,14,16,18]);
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ impl ReprVisitor {
|
|||||||
self.writer.write_str(", ");
|
self.writer.write_str(", ");
|
||||||
}
|
}
|
||||||
self.visit_ptr_inner(p as *c_void, inner);
|
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;
|
left -= dec;
|
||||||
}
|
}
|
||||||
self.writer.write_char(']');
|
self.writer.write_char(']');
|
||||||
|
|||||||
@@ -46,9 +46,11 @@ impl StackSegment {
|
|||||||
|
|
||||||
/// Point one word beyond the high end of the allocated stack
|
/// Point one word beyond the high end of the allocated stack
|
||||||
pub fn end(&self) -> *uint {
|
pub fn end(&self) -> *uint {
|
||||||
|
unsafe {
|
||||||
vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint
|
vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Drop for StackSegment {
|
impl Drop for StackSegment {
|
||||||
fn drop(&self) {
|
fn drop(&self) {
|
||||||
|
|||||||
@@ -1092,7 +1092,7 @@ pub mod raw {
|
|||||||
pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
|
pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
|
||||||
do s.as_imm_buf |sbuf, _n| {
|
do s.as_imm_buf |sbuf, _n| {
|
||||||
cast::transmute(Slice {
|
cast::transmute(Slice {
|
||||||
data: sbuf.offset_inbounds(begin as int),
|
data: sbuf.offset(begin as int),
|
||||||
len: end - begin,
|
len: end - begin,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -332,19 +332,13 @@ extern "rust-intrinsic" {
|
|||||||
/// Get the address of the `__morestack` stack growth function.
|
/// Get the address of the `__morestack` stack growth function.
|
||||||
pub fn morestack_addr() -> *();
|
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
|
/// 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
|
/// the object, or one-byte-past-the-end. An arithmetic overflow is also
|
||||||
/// undefined behaviour.
|
/// undefined behaviour.
|
||||||
///
|
///
|
||||||
/// This intrinsic should be preferred over `offset` when the guarantee can
|
/// This is implemented as an intrinsic to avoid converting to and from an
|
||||||
/// be satisfied, to enable better optimization.
|
/// integer, since the conversion would throw away aliasing information.
|
||||||
pub fn offset_inbounds<T>(dst: *T, offset: int) -> *T;
|
pub fn offset<T>(dst: *T, offset: int) -> *T;
|
||||||
|
|
||||||
/// Equivalent to the `llvm.memcpy.p0i8.0i8.i32` intrinsic, with a size of
|
/// 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>()`
|
/// `count` * `size_of::<T>()` and an alignment of `min_align_of::<T>()`
|
||||||
|
|||||||
@@ -896,7 +896,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
|
|||||||
lifetime: cast::transmute(p)}
|
lifetime: cast::transmute(p)}
|
||||||
} else {
|
} else {
|
||||||
VecIterator{ptr: p,
|
VecIterator{ptr: p,
|
||||||
end: p.offset_inbounds(self.len() as int),
|
end: p.offset(self.len() as int),
|
||||||
lifetime: cast::transmute(p)}
|
lifetime: cast::transmute(p)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1884,7 +1884,7 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
|
|||||||
lifetime: cast::transmute(p)}
|
lifetime: cast::transmute(p)}
|
||||||
} else {
|
} else {
|
||||||
VecMutIterator{ptr: p,
|
VecMutIterator{ptr: p,
|
||||||
end: p.offset_inbounds(self.len() as int),
|
end: p.offset(self.len() as int),
|
||||||
lifetime: cast::transmute(p)}
|
lifetime: cast::transmute(p)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2247,7 +2247,7 @@ macro_rules! iterator {
|
|||||||
// same pointer.
|
// same pointer.
|
||||||
cast::transmute(self.ptr as uint + 1)
|
cast::transmute(self.ptr as uint + 1)
|
||||||
} else {
|
} else {
|
||||||
self.ptr.offset_inbounds(1)
|
self.ptr.offset(1)
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(cast::transmute(old))
|
Some(cast::transmute(old))
|
||||||
@@ -2279,7 +2279,7 @@ macro_rules! double_ended_iterator {
|
|||||||
// See above for why 'ptr.offset' isn't used
|
// See above for why 'ptr.offset' isn't used
|
||||||
cast::transmute(self.end as uint - 1)
|
cast::transmute(self.end as uint - 1)
|
||||||
} else {
|
} else {
|
||||||
self.end.offset_inbounds(-1)
|
self.end.offset(-1)
|
||||||
};
|
};
|
||||||
Some(cast::transmute(self.end))
|
Some(cast::transmute(self.end))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user