Skip to content

Commit 576f395

Browse files
committed
auto merge of #8121 : thestinger/rust/offset, r=alexcrichton
Closes #8118, #7136 ~~~rust extern mod extra; use std::vec; use std::ptr; fn bench_from_elem(b: &mut extra::test::BenchHarness) { do b.iter { let v: ~[u8] = vec::from_elem(1024, 0u8); } } fn bench_set_memory(b: &mut extra::test::BenchHarness) { do b.iter { let mut v: ~[u8] = vec::with_capacity(1024); unsafe { let vp = vec::raw::to_mut_ptr(v); ptr::set_memory(vp, 0, 1024); vec::raw::set_len(&mut v, 1024); } } } fn bench_vec_repeat(b: &mut extra::test::BenchHarness) { do b.iter { let v: ~[u8] = ~[0u8, ..1024]; } } ~~~ Before: test bench_from_elem ... bench: 415 ns/iter (+/- 17) test bench_set_memory ... bench: 85 ns/iter (+/- 4) test bench_vec_repeat ... bench: 83 ns/iter (+/- 3) After: test bench_from_elem ... bench: 84 ns/iter (+/- 2) test bench_set_memory ... bench: 84 ns/iter (+/- 5) test bench_vec_repeat ... bench: 84 ns/iter (+/- 3)
2 parents 7fc8c14 + ef870d3 commit 576f395

File tree

19 files changed

+131
-80
lines changed

19 files changed

+131
-80
lines changed

src/libextra/arena.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
116116
let fill = chunk.fill;
117117

118118
while idx < fill {
119-
let tydesc_data: *uint = transmute(ptr::offset(buf, idx));
119+
let tydesc_data: *uint = transmute(ptr::offset(buf, idx as int));
120120
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
121121
let (size, align) = ((*tydesc).size, (*tydesc).align);
122122

@@ -127,7 +127,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
127127
//debug!("freeing object: idx = %u, size = %u, align = %u, done = %b",
128128
// start, size, align, is_done);
129129
if is_done {
130-
((*tydesc).drop_glue)(ptr::offset(buf, start) as *i8);
130+
((*tydesc).drop_glue)(ptr::offset(buf, start as int) as *i8);
131131
}
132132

133133
// Find where the next tydesc lives
@@ -176,7 +176,7 @@ impl Arena {
176176
//debug!("idx = %u, size = %u, align = %u, fill = %u",
177177
// start, n_bytes, align, head.fill);
178178

179-
ptr::offset(vec::raw::to_ptr(this.pod_head.data), start)
179+
ptr::offset(vec::raw::to_ptr(this.pod_head.data), start as int)
180180
}
181181
}
182182

@@ -233,7 +233,7 @@ impl Arena {
233233
// start, n_bytes, align, head.fill);
234234

235235
let buf = vec::raw::to_ptr(self.head.data);
236-
return (ptr::offset(buf, tydesc_start), ptr::offset(buf, start));
236+
return (ptr::offset(buf, tydesc_start as int), ptr::offset(buf, start as int));
237237
}
238238
}
239239

src/libextra/c_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub unsafe fn c_vec_with_dtor<T>(base: *mut T, len: uint, dtor: @fn())
122122
pub fn get<T:Clone>(t: CVec<T>, ofs: uint) -> T {
123123
assert!(ofs < len(t));
124124
return unsafe {
125-
(*ptr::mut_offset(t.base, ofs)).clone()
125+
(*ptr::mut_offset(t.base, ofs as int)).clone()
126126
};
127127
}
128128

@@ -133,7 +133,7 @@ pub fn get<T:Clone>(t: CVec<T>, ofs: uint) -> T {
133133
*/
134134
pub fn set<T>(t: CVec<T>, ofs: uint, v: T) {
135135
assert!(ofs < len(t));
136-
unsafe { *ptr::mut_offset(t.base, ofs) = v };
136+
unsafe { *ptr::mut_offset(t.base, ofs as int) = v };
137137
}
138138

139139
/*

src/libextra/ebml.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub mod reader {
150150

151151
unsafe {
152152
let (ptr, _): (*u8, uint) = transmute(data);
153-
let ptr = offset(ptr, start);
153+
let ptr = offset(ptr, start as int);
154154
let ptr: *i32 = transmute(ptr);
155155
let val = bswap32(*ptr);
156156
let val: u32 = transmute(val);

src/libextra/par.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn map_slices<A:Clone + Send,B:Clone + Send>(
5858
let f = do future_spawn() || {
5959
unsafe {
6060
let len = end - base;
61-
let slice = (ptr::offset(p, base),
61+
let slice = (ptr::offset(p, base as int),
6262
len * sys::size_of::<A>());
6363
info!("pre-slice: %?", (base, slice));
6464
let slice : &[A] =

src/librustc/metadata/loader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ fn get_metadata_section(os: os,
215215
}
216216
if !version_ok { return None; }
217217

218-
let cvbuf1 = ptr::offset(cvbuf, vlen);
218+
let cvbuf1 = ptr::offset(cvbuf, vlen as int);
219219
debug!("inflating %u bytes of compressed metadata",
220220
csz - vlen);
221221
do vec::raw::buf_as_slice(cvbuf1, csz-vlen) |bytes| {

src/librustc/middle/trans/foreign.rs

+5
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,11 @@ pub fn trans_intrinsic(ccx: @mut CrateContext,
884884
let morestack_addr = PointerCast(bcx, morestack_addr, Type::nil().ptr_to());
885885
Ret(bcx, morestack_addr);
886886
}
887+
"offset" => {
888+
let ptr = get_param(decl, first_real_arg);
889+
let offset = get_param(decl, first_real_arg + 1);
890+
Ret(bcx, GEP(bcx, ptr, [offset]));
891+
}
887892
"memcpy32" => memcpy_intrinsic(bcx, "llvm.memcpy.p0i8.p0i8.i32", substs.tys[0], 32),
888893
"memcpy64" => memcpy_intrinsic(bcx, "llvm.memcpy.p0i8.p0i8.i64", substs.tys[0], 64),
889894
"memmove32" => memcpy_intrinsic(bcx, "llvm.memmove.p0i8.p0i8.i32", substs.tys[0], 32),

src/librustc/middle/trans/type_use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub fn type_uses_for(ccx: @mut CrateContext, fn_id: def_id, n_tps: uint)
149149
"visit_tydesc" | "forget" | "frame_address" |
150150
"morestack_addr" => 0,
151151

152-
"memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
152+
"offset" | "memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
153153
"memset32" | "memset64" => use_repr,
154154

155155
"sqrtf32" | "sqrtf64" | "powif32" | "powif64" |

src/librustc/middle/typeck/check/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -3606,6 +3606,20 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) {
36063606
"morestack_addr" => {
36073607
(0u, ~[], ty::mk_nil_ptr(ccx.tcx))
36083608
}
3609+
"offset" => {
3610+
(1,
3611+
~[
3612+
ty::mk_ptr(tcx, ty::mt {
3613+
ty: param(ccx, 0),
3614+
mutbl: ast::m_imm
3615+
}),
3616+
ty::mk_int()
3617+
],
3618+
ty::mk_ptr(tcx, ty::mt {
3619+
ty: param(ccx, 0),
3620+
mutbl: ast::m_imm
3621+
}))
3622+
}
36093623
"memcpy32" => {
36103624
(1,
36113625
~[

src/libstd/at_vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub mod raw {
233233
let repr: *mut Box<Vec<T>> = cast::transmute_copy(v);
234234
let amt = v.len();
235235
(*repr).data.fill += sys::size_of::<T>();
236-
let p = ptr::offset(&(*repr).data.data as *T, amt) as *mut T;
236+
let p = ptr::offset(&(*repr).data.data as *T, amt as int) as *mut T;
237237
move_val_init(&mut(*p), initval);
238238
}
239239

src/libstd/gc.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub mod rustrt {
7474
}
7575

7676
unsafe fn bump<T, U>(ptr: *T, count: uint) -> *U {
77-
return ptr::offset(ptr, count) as *U;
77+
return ptr::offset(ptr, count as int) as *U;
7878
}
7979

8080
unsafe fn align_to_pointer<T>(ptr: *T) -> *T {
@@ -140,11 +140,11 @@ unsafe fn _walk_safe_point(fp: *Word, sp: SafePoint, visitor: Visitor) -> bool {
140140
// Stack roots
141141
let mut sri = 0;
142142
while sri < num_stack_roots {
143-
if *ptr::offset(addrspaces, sri) >= 1 {
143+
if *ptr::offset(addrspaces, sri as int) >= 1 {
144144
let root =
145-
ptr::offset(fp_bytes, *ptr::offset(stack_roots, sri) as Word)
145+
ptr::offset(fp_bytes, *ptr::offset(stack_roots, sri as int) as int)
146146
as **Word;
147-
let tydescpp = ptr::offset(tydescs, sri);
147+
let tydescpp = ptr::offset(tydescs, sri as int);
148148
let tydesc = if ptr::is_not_null(tydescpp) &&
149149
ptr::is_not_null(*tydescpp) {
150150
**tydescpp
@@ -159,7 +159,7 @@ unsafe fn _walk_safe_point(fp: *Word, sp: SafePoint, visitor: Visitor) -> bool {
159159
// Register roots
160160
let mut rri = 0;
161161
while rri < num_reg_roots {
162-
if *ptr::offset(addrspaces, num_stack_roots + rri) == 1 {
162+
if *ptr::offset(addrspaces, (num_stack_roots + rri) as int) == 1 {
163163
// FIXME(#2997): Need to find callee saved registers on the stack.
164164
}
165165
rri += 1;
@@ -246,7 +246,7 @@ unsafe fn _walk_gc_roots(mem: Memory, sentinel: **Word, visitor: Visitor) -> boo
246246
// for this second return address is 3 greater than the
247247
// return address for morestack.
248248
let ret_offset = if boundary { 4 } else { 1 };
249-
last_ret = *ptr::offset(frame.fp, ret_offset) as *Word;
249+
last_ret = *ptr::offset(frame.fp, ret_offset as int) as *Word;
250250

251251
if ptr::is_null(pc) {
252252
loop;

src/libstd/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ impl Writer for fd_t {
12131213
let mut count = 0u;
12141214
do v.as_imm_buf |vbuf, len| {
12151215
while count < len {
1216-
let vb = ptr::offset(vbuf, count) as *c_void;
1216+
let vb = ptr::offset(vbuf, count as int) as *c_void;
12171217
let nout = libc::write(*self, vb, len as size_t);
12181218
if nout < 0 as ssize_t {
12191219
error!("error writing buffer");

src/libstd/os.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ pub fn set_exit_status(code: int) {
11141114
unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] {
11151115
let mut args = ~[];
11161116
for uint::range(0, argc as uint) |i| {
1117-
args.push(str::raw::from_c_str(*argv.offset(i)));
1117+
args.push(str::raw::from_c_str(*argv.offset(i as int)));
11181118
}
11191119
args
11201120
}
@@ -1165,9 +1165,9 @@ pub fn real_args() -> ~[~str] {
11651165
for uint::range(0, nArgs as uint) |i| {
11661166
unsafe {
11671167
// Determine the length of this argument.
1168-
let ptr = *szArgList.offset(i);
1168+
let ptr = *szArgList.offset(i as int);
11691169
let mut len = 0;
1170-
while *ptr.offset(len) != 0 { len += 1; }
1170+
while *ptr.offset(len as int) != 0 { len += 1; }
11711171

11721172
// Push it onto the list.
11731173
args.push(vec::raw::buf_as_slice(ptr, len,

src/libstd/ptr.rs

+46-21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use cast;
1414
use clone::Clone;
1515
use option::{Option, Some, None};
16+
#[cfg(stage0)]
1617
use sys;
1718
use unstable::intrinsics;
1819
use util::swap;
@@ -25,20 +26,44 @@ use uint;
2526

2627
/// Calculate the offset from a pointer
2728
#[inline]
28-
pub fn offset<T>(ptr: *T, count: uint) -> *T {
29-
(ptr as uint + count * sys::size_of::<T>()) as *T
29+
#[cfg(stage0)]
30+
pub fn offset<T>(ptr: *T, count: int) -> *T {
31+
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *T
3032
}
3133

3234
/// Calculate the offset from a const pointer
3335
#[inline]
34-
pub fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
35-
(ptr as uint + count * sys::size_of::<T>()) as *T
36+
#[cfg(stage0)]
37+
pub fn const_offset<T>(ptr: *const T, count: int) -> *const T {
38+
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *T
3639
}
3740

3841
/// Calculate the offset from a mut pointer
3942
#[inline]
40-
pub fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
41-
(ptr as uint + count * sys::size_of::<T>()) as *mut T
43+
#[cfg(stage0)]
44+
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
45+
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *mut T
46+
}
47+
48+
/// Calculate the offset from a pointer
49+
#[inline]
50+
#[cfg(not(stage0))]
51+
pub fn offset<T>(ptr: *T, count: int) -> *T {
52+
unsafe { intrinsics::offset(ptr, count) }
53+
}
54+
55+
/// Calculate the offset from a const pointer
56+
#[inline]
57+
#[cfg(not(stage0))]
58+
pub fn const_offset<T>(ptr: *const T, count: int) -> *const T {
59+
unsafe { intrinsics::offset(ptr as *T, count) }
60+
}
61+
62+
/// Calculate the offset from a mut pointer
63+
#[inline]
64+
#[cfg(not(stage0))]
65+
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
66+
unsafe { intrinsics::offset(ptr as *T, count) as *mut T }
4267
}
4368

4469
/// Return the offset of the first null pointer in `buf`.
@@ -58,7 +83,7 @@ impl<T> Clone for *T {
5883
pub unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint {
5984
let mut i = 0;
6085
loop {
61-
if f(&(*offset(buf, i))) { return i; }
86+
if f(&(*offset(buf, i as int))) { return i; }
6287
else { i += 1; }
6388
}
6489
}
@@ -242,7 +267,7 @@ pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: &fn(*T)) {
242267
}
243268
//let start_ptr = *arr;
244269
uint::iterate(0, len, |e| {
245-
let n = offset(arr, e);
270+
let n = offset(arr, e as int);
246271
cb(*n);
247272
true
248273
});
@@ -273,7 +298,7 @@ pub trait RawPtr<T> {
273298
fn is_null(&self) -> bool;
274299
fn is_not_null(&self) -> bool;
275300
unsafe fn to_option(&self) -> Option<&T>;
276-
fn offset(&self, count: uint) -> Self;
301+
fn offset(&self, count: int) -> Self;
277302
}
278303

279304
/// Extension methods for immutable pointers
@@ -305,7 +330,7 @@ impl<T> RawPtr<T> for *T {
305330

306331
/// Calculates the offset from a pointer.
307332
#[inline]
308-
fn offset(&self, count: uint) -> *T { offset(*self, count) }
333+
fn offset(&self, count: int) -> *T { offset(*self, count) }
309334
}
310335

311336
/// Extension methods for mutable pointers
@@ -337,7 +362,7 @@ impl<T> RawPtr<T> for *mut T {
337362

338363
/// Calculates the offset from a mutable pointer.
339364
#[inline]
340-
fn offset(&self, count: uint) -> *mut T { mut_offset(*self, count) }
365+
fn offset(&self, count: int) -> *mut T { mut_offset(*self, count) }
341366
}
342367

343368
// Equality for pointers
@@ -378,7 +403,7 @@ impl<T, I: Int> Add<I, *T> for *T {
378403
/// Is calculated according to the size of the type pointed to.
379404
#[inline]
380405
pub fn add(&self, rhs: &I) -> *T {
381-
self.offset(rhs.to_int() as uint)
406+
self.offset(rhs.to_int() as int)
382407
}
383408
}
384409

@@ -388,7 +413,7 @@ impl<T, I: Int> Sub<I, *T> for *T {
388413
/// Is calculated according to the size of the type pointed to.
389414
#[inline]
390415
pub fn sub(&self, rhs: &I) -> *T {
391-
self.offset(-rhs.to_int() as uint)
416+
self.offset(-rhs.to_int() as int)
392417
}
393418
}
394419

@@ -398,7 +423,7 @@ impl<T, I: Int> Add<I, *mut T> for *mut T {
398423
/// Is calculated according to the size of the type pointed to.
399424
#[inline]
400425
pub fn add(&self, rhs: &I) -> *mut T {
401-
self.offset(rhs.to_int() as uint)
426+
self.offset(rhs.to_int() as int)
402427
}
403428
}
404429

@@ -408,7 +433,7 @@ impl<T, I: Int> Sub<I, *mut T> for *mut T {
408433
/// Is calculated according to the size of the type pointed to.
409434
#[inline]
410435
pub fn sub(&self, rhs: &I) -> *mut T {
411-
self.offset(-rhs.to_int() as uint)
436+
self.offset(-rhs.to_int() as int)
412437
}
413438
}
414439

@@ -445,14 +470,14 @@ pub mod ptr_tests {
445470
let v0 = ~[32000u16, 32001u16, 32002u16];
446471
let mut v1 = ~[0u16, 0u16, 0u16];
447472

448-
copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 1u),
449-
offset(vec::raw::to_ptr(v0), 1u), 1u);
473+
copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 1),
474+
offset(vec::raw::to_ptr(v0), 1), 1);
450475
assert!((v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16));
451476
copy_memory(vec::raw::to_mut_ptr(v1),
452-
offset(vec::raw::to_ptr(v0), 2u), 1u);
477+
offset(vec::raw::to_ptr(v0), 2), 1);
453478
assert!((v1[0] == 32002u16 && v1[1] == 32001u16 &&
454479
v1[2] == 0u16));
455-
copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 2u),
480+
copy_memory(mut_offset(vec::raw::to_mut_ptr(v1), 2),
456481
vec::raw::to_ptr(v0), 1u);
457482
assert!((v1[0] == 32002u16 && v1[1] == 32001u16 &&
458483
v1[2] == 32000u16));
@@ -495,15 +520,15 @@ pub mod ptr_tests {
495520
assert!(p.is_null());
496521
assert!(!p.is_not_null());
497522

498-
let q = offset(p, 1u);
523+
let q = offset(p, 1);
499524
assert!(!q.is_null());
500525
assert!(q.is_not_null());
501526

502527
let mp: *mut int = mut_null();
503528
assert!(mp.is_null());
504529
assert!(!mp.is_not_null());
505530

506-
let mq = mp.offset(1u);
531+
let mq = mp.offset(1);
507532
assert!(!mq.is_null());
508533
assert!(mq.is_not_null());
509534
}

src/libstd/repr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl ReprVisitor {
212212
self.writer.write_str(", ");
213213
}
214214
self.visit_ptr_inner(p as *c_void, inner);
215-
p = align(ptr::offset(p, sz) as uint, al) as *u8;
215+
p = align(ptr::offset(p, sz as int) as uint, al) as *u8;
216216
left -= dec;
217217
}
218218
self.writer.write_char(']');

src/libstd/rt/args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ mod imp {
114114
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
115115
let mut args = ~[];
116116
for uint::range(0, argc as uint) |i| {
117-
args.push(str::raw::from_c_str(*(argv as **libc::c_char).offset(i)));
117+
args.push(str::raw::from_c_str(*(argv as **libc::c_char).offset(i as int)));
118118
}
119119
return args;
120120
}

0 commit comments

Comments
 (0)