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;
|
2012-04-04 00:32:55 -05:00
|
|
|
export is_null;
|
|
|
|
export is_not_null;
|
2012-03-10 02:04:09 -06:00
|
|
|
export memcpy;
|
|
|
|
export memmove;
|
2012-04-15 02:40:47 -05:00
|
|
|
export buf_len;
|
|
|
|
export position;
|
2012-04-15 23:46:29 -05:00
|
|
|
export extensions;
|
2012-03-10 02:04:09 -06:00
|
|
|
|
2012-03-22 06:30:10 -05:00
|
|
|
import libc::c_void;
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-22 06:30:10 -05:00
|
|
|
#[nolink]
|
|
|
|
#[abi = "cdecl"]
|
|
|
|
native mod libc_ {
|
2012-03-22 07:44:16 -05:00
|
|
|
#[rust_stack]
|
2012-03-22 06:30:10 -05:00
|
|
|
fn memcpy(dest: *c_void, src: *c_void, n: libc::size_t) -> *c_void;
|
2012-03-22 07:44:16 -05:00
|
|
|
#[rust_stack]
|
2012-03-22 06:30:10 -05:00
|
|
|
fn memmove(dest: *c_void, src: *c_void, n: libc::size_t) -> *c_void;
|
|
|
|
}
|
|
|
|
|
2012-03-23 09:05:16 -05:00
|
|
|
#[abi = "rust-intrinsic"]
|
2011-12-13 18:25:51 -06:00
|
|
|
native mod rusti {
|
|
|
|
fn addr_of<T>(val: T) -> *T;
|
|
|
|
}
|
|
|
|
|
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)]
|
2012-03-22 06:30:10 -05:00
|
|
|
fn addr_of<T>(val: T) -> *T { rusti::addr_of(val) }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-26 20:35:18 -05:00
|
|
|
#[doc = "Get an unsafe mut pointer to a value"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2012-03-26 20:35:18 -05:00
|
|
|
fn mut_addr_of<T>(val: T) -> *mut T unsafe {
|
2012-03-22 06:30:10 -05:00
|
|
|
unsafe::reinterpret_cast(rusti::addr_of(val))
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
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)]
|
2012-03-21 08:55:36 -05:00
|
|
|
fn offset<T>(ptr: *T, count: uint) -> *T unsafe {
|
|
|
|
(ptr as uint + count * sys::size_of::<T>()) as *T
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-03-26 20:35:18 -05:00
|
|
|
#[doc = "Calculate the offset from a mut pointer"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2012-03-26 20:35:18 -05:00
|
|
|
fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
|
|
|
|
(ptr as uint + count * sys::size_of::<T>()) as *mut T
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-04-11 17:46:51 -05:00
|
|
|
#[doc = "Return the offset of the first null pointer in `buf`."]
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn buf_len<T>(buf: **T) -> uint {
|
|
|
|
position(buf) {|i| i == null() }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc = "Return the first offset `i` such that `f(buf[i]) == true`."]
|
|
|
|
#[inline(always)]
|
|
|
|
unsafe fn position<T>(buf: *T, f: fn(T) -> bool) -> uint {
|
|
|
|
let mut i = 0u;
|
|
|
|
loop {
|
|
|
|
if f(*offset(buf, i)) { ret i; }
|
|
|
|
else { i += 1u; }
|
|
|
|
}
|
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-03-06 21:09:32 -06:00
|
|
|
#[doc = "Create an unsafe null pointer"]
|
2012-03-06 13:20:43 -06:00
|
|
|
#[inline(always)]
|
2012-04-03 23:56:16 -05:00
|
|
|
pure fn null<T>() -> *T unsafe { ret unsafe::reinterpret_cast(0u); }
|
|
|
|
|
2012-04-13 21:50:51 -05:00
|
|
|
#[doc = "Returns true if the pointer is equal to the null pointer."]
|
2012-04-03 23:56:16 -05:00
|
|
|
pure fn is_null<T>(ptr: *const T) -> bool { ptr == null() }
|
|
|
|
|
2012-04-13 21:50:51 -05:00
|
|
|
#[doc = "Returns true if the pointer is not equal to the null pointer."]
|
2012-04-03 23:56:16 -05:00
|
|
|
pure fn is_not_null<T>(ptr: *const T) -> bool { !is_null(ptr) }
|
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) {
|
2012-03-22 06:30:10 -05:00
|
|
|
let n = count * sys::size_of::<T>();
|
|
|
|
libc_::memcpy(dst as *c_void, src as *c_void, n);
|
2012-02-07 15:21:11 -06:00
|
|
|
}
|
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-03-22 06:30:10 -05:00
|
|
|
let n = count * sys::size_of::<T>();
|
|
|
|
libc_::memmove(dst as *c_void, src as *c_void, n);
|
2012-02-07 15:21:11 -06:00
|
|
|
}
|
2012-02-07 12:50:42 -06:00
|
|
|
|
2012-04-15 23:46:29 -05:00
|
|
|
#[doc = "Extension methods for pointers"]
|
|
|
|
impl extensions<T> for *T {
|
|
|
|
#[doc = "Returns true if the pointer is equal to the null pointer."]
|
|
|
|
pure fn is_null<T>() -> bool { is_null(self) }
|
|
|
|
|
|
|
|
#[doc = "Returns true if the pointer is not equal to the null pointer."]
|
|
|
|
pure fn is_not_null<T>() -> bool { is_not_null(self) }
|
|
|
|
}
|
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
|
|
|
fn test() unsafe {
|
2012-03-26 20:35:18 -05:00
|
|
|
type pair = {mut fst: int, mut snd: int};
|
|
|
|
let p = {mut fst: 10, mut snd: 20};
|
|
|
|
let pptr: *mut pair = mut_addr_of(p);
|
|
|
|
let iptr: *mut int = unsafe::reinterpret_cast(pptr);
|
2012-01-17 19:28:21 -06:00
|
|
|
assert (*iptr == 10);;
|
|
|
|
*iptr = 30;
|
|
|
|
assert (*iptr == 30);
|
|
|
|
assert (p.fst == 30);;
|
|
|
|
|
2012-03-26 20:35:18 -05:00
|
|
|
*pptr = {mut fst: 50, mut snd: 60};
|
2012-01-17 19:28:21 -06:00
|
|
|
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-04-11 17:46:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_position() unsafe {
|
|
|
|
import str::as_c_str;
|
|
|
|
import libc::c_char;
|
|
|
|
|
|
|
|
let s = "hello";
|
|
|
|
assert 2u == as_c_str(s) {|p| position(p) {|c| c == 'l' as c_char} };
|
|
|
|
assert 4u == as_c_str(s) {|p| position(p) {|c| c == 'o' as c_char} };
|
|
|
|
assert 5u == as_c_str(s) {|p| position(p) {|c| c == 0 as c_char } };
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_len() unsafe {
|
|
|
|
let s0 = "hello";
|
|
|
|
let s1 = "there";
|
|
|
|
let s2 = "thing";
|
|
|
|
str::as_c_str(s0) {|p0|
|
|
|
|
str::as_c_str(s1) {|p1|
|
|
|
|
str::as_c_str(s2) {|p2|
|
|
|
|
let v = [p0, p1, p2, null()];
|
|
|
|
vec::as_buf(v) {|vp|
|
|
|
|
assert buf_len(vp) == 3u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 02:40:47 -05:00
|
|
|
}
|