2012-03-10 02:04:09 -06:00
|
|
|
#[doc = "Unsafe pointer utility functions"];
|
|
|
|
|
|
|
|
export addr_of;
|
|
|
|
export mut_addr_of;
|
|
|
|
export offset;
|
|
|
|
export mut_offset;
|
|
|
|
export null;
|
|
|
|
export memcpy;
|
|
|
|
export memmove;
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
#[abi = "rust-intrinsic"]
|
|
|
|
native mod rusti {
|
|
|
|
fn addr_of<T>(val: T) -> *T;
|
2012-03-12 22:04:27 -05:00
|
|
|
fn ptr_offset<T>(ptr: *T, count: libc::uintptr_t) -> *T;
|
|
|
|
fn memcpy<T>(dst: *T, src: *T, count: libc::uintptr_t);
|
|
|
|
fn memmove<T>(dst: *T, src: *T, count: libc::uintptr_t);
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Get an unsafe pointer to a value"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2011-12-13 18:25:51 -06:00
|
|
|
fn addr_of<T>(val: T) -> *T { ret rusti::addr_of(val); }
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Get an unsafe mutable pointer to a value"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2011-12-13 18:25:51 -06:00
|
|
|
fn mut_addr_of<T>(val: T) -> *mutable T unsafe {
|
|
|
|
ret unsafe::reinterpret_cast(rusti::addr_of(val));
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Calculate the offset from a pointer"]
|
2012-03-02 22:06:08 -06:00
|
|
|
#[inline(always)]
|
2011-12-13 18:25:51 -06:00
|
|
|
fn offset<T>(ptr: *T, count: uint) -> *T {
|
|
|
|
ret rusti::ptr_offset(ptr, count);
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Calculate the offset from a mutable pointer"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2011-12-13 18:25:51 -06:00
|
|
|
fn mut_offset<T>(ptr: *mutable T, count: uint) -> *mutable T {
|
|
|
|
ret rusti::ptr_offset(ptr as *T, count) as *mutable T;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Create an unsafe null pointer"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2011-12-13 18:25:51 -06:00
|
|
|
fn null<T>() -> *T unsafe { ret unsafe::reinterpret_cast(0u); }
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
|
|
|
Copies data from one location to another
|
2012-02-07 12:50:42 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
Copies `count` elements (not bytes) from `src` to `dst`. The source
|
|
|
|
and destination may not overlap.
|
|
|
|
"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2012-02-07 15:21:11 -06:00
|
|
|
unsafe fn memcpy<T>(dst: *T, src: *T, count: uint) {
|
|
|
|
rusti::memcpy(dst, src, count);
|
|
|
|
}
|
2012-02-07 12:50:42 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "
|
|
|
|
Copies data from one location to another
|
2012-02-07 12:50:42 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
Copies `count` elements (not bytes) from `src` to `dst`. The source
|
|
|
|
and destination may overlap.
|
|
|
|
"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2012-02-07 15:21:11 -06:00
|
|
|
unsafe fn memmove<T>(dst: *T, src: *T, count: uint) {
|
2012-02-09 14:47:12 -06:00
|
|
|
rusti::memmove(dst, src, count);
|
2012-02-07 15:21:11 -06:00
|
|
|
}
|
2012-02-07 12:50:42 -06:00
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
|
|
|
fn test() unsafe {
|
|
|
|
type pair = {mutable fst: int, mutable snd: int};
|
|
|
|
let p = {mutable fst: 10, mutable snd: 20};
|
|
|
|
let pptr: *mutable pair = mut_addr_of(p);
|
|
|
|
let iptr: *mutable int = unsafe::reinterpret_cast(pptr);
|
|
|
|
assert (*iptr == 10);;
|
|
|
|
*iptr = 30;
|
|
|
|
assert (*iptr == 30);
|
|
|
|
assert (p.fst == 30);;
|
|
|
|
|
|
|
|
*pptr = {mutable fst: 50, mutable snd: 60};
|
|
|
|
assert (*iptr == 50);
|
|
|
|
assert (p.fst == 50);
|
|
|
|
assert (p.snd == 60);
|
2012-02-07 12:50:42 -06:00
|
|
|
|
|
|
|
let v0 = [32000u16, 32001u16, 32002u16];
|
|
|
|
let v1 = [0u16, 0u16, 0u16];
|
2012-02-07 15:21:11 -06:00
|
|
|
|
|
|
|
ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 1u),
|
|
|
|
ptr::offset(vec::unsafe::to_ptr(v0), 1u), 1u);
|
2012-02-07 12:50:42 -06:00
|
|
|
assert (v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16);
|
2012-02-07 15:21:11 -06:00
|
|
|
ptr::memcpy(vec::unsafe::to_ptr(v1),
|
|
|
|
ptr::offset(vec::unsafe::to_ptr(v0), 2u), 1u);
|
2012-02-07 12:50:42 -06:00
|
|
|
assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 0u16);
|
2012-02-07 15:21:11 -06:00
|
|
|
ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 2u),
|
|
|
|
vec::unsafe::to_ptr(v0), 1u);
|
2012-02-07 12:50:42 -06:00
|
|
|
assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 32000u16);
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|