1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#[doc = "Unsafe pointer utility functions"];
export addr_of;
export mut_addr_of;
export offset;
export mut_offset;
export null;
export memcpy;
export memmove;
import libc::c_void;
#[nolink]
#[abi = "cdecl"]
native mod libc_ {
#[rust_stack]
fn memcpy(dest: *c_void, src: *c_void, n: libc::size_t) -> *c_void;
#[rust_stack]
fn memmove(dest: *c_void, src: *c_void, n: libc::size_t) -> *c_void;
}
#[abi = "rust-intrinsic"]
native mod rusti {
fn addr_of<T>(val: T) -> *T;
}
#[doc = "Get an unsafe pointer to a value"]
#[inline(always)]
fn addr_of<T>(val: T) -> *T { rusti::addr_of(val) }
#[doc = "Get an unsafe mut pointer to a value"]
#[inline(always)]
fn mut_addr_of<T>(val: T) -> *mut T unsafe {
unsafe::reinterpret_cast(rusti::addr_of(val))
}
#[doc = "Calculate the offset from a pointer"]
#[inline(always)]
fn offset<T>(ptr: *T, count: uint) -> *T unsafe {
(ptr as uint + count * sys::size_of::<T>()) as *T
}
#[doc = "Calculate the offset from a mut pointer"]
#[inline(always)]
fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
(ptr as uint + count * sys::size_of::<T>()) as *mut T
}
#[doc = "Create an unsafe null pointer"]
#[inline(always)]
fn null<T>() -> *T unsafe { ret unsafe::reinterpret_cast(0u); }
#[doc = "
Copies data from one location to another
Copies `count` elements (not bytes) from `src` to `dst`. The source
and destination may not overlap.
"]
#[inline(always)]
unsafe fn memcpy<T>(dst: *T, src: *T, count: uint) {
let n = count * sys::size_of::<T>();
libc_::memcpy(dst as *c_void, src as *c_void, n);
}
#[doc = "
Copies data from one location to another
Copies `count` elements (not bytes) from `src` to `dst`. The source
and destination may overlap.
"]
#[inline(always)]
unsafe fn memmove<T>(dst: *T, src: *T, count: uint) {
let n = count * sys::size_of::<T>();
libc_::memmove(dst as *c_void, src as *c_void, n);
}
#[test]
fn test() unsafe {
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);
assert (*iptr == 10);;
*iptr = 30;
assert (*iptr == 30);
assert (p.fst == 30);;
*pptr = {mut fst: 50, mut snd: 60};
assert (*iptr == 50);
assert (p.fst == 50);
assert (p.snd == 60);
let v0 = [32000u16, 32001u16, 32002u16];
let v1 = [0u16, 0u16, 0u16];
ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 1u),
ptr::offset(vec::unsafe::to_ptr(v0), 1u), 1u);
assert (v1[0] == 0u16 && v1[1] == 32001u16 && v1[2] == 0u16);
ptr::memcpy(vec::unsafe::to_ptr(v1),
ptr::offset(vec::unsafe::to_ptr(v0), 2u), 1u);
assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 0u16);
ptr::memcpy(ptr::offset(vec::unsafe::to_ptr(v1), 2u),
vec::unsafe::to_ptr(v0), 1u);
assert (v1[0] == 32002u16 && v1[1] == 32001u16 && v1[2] == 32000u16);
}
|