2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-11-23 04:15:04 -06:00
|
|
|
Library to interface with chunks of memory allocated in C.
|
|
|
|
|
|
|
|
It is often desirable to safely interface with memory allocated from C,
|
|
|
|
encapsulating the unsafety into allocation and destruction time. Indeed,
|
|
|
|
allocating memory externally is currently the only way to give Rust shared
|
|
|
|
mutable state with C programs that keep their own references; vectors are
|
|
|
|
unsuitable because they could be reallocated or moved at any time, and
|
|
|
|
importing C memory into a vector takes a one-time snapshot of the memory.
|
|
|
|
|
|
|
|
This module simplifies the usage of such external blocks of memory. Memory
|
|
|
|
is encapsulated into an opaque object after creation; the lifecycle of the
|
|
|
|
memory can be optionally managed by Rust, if an appropriate destructor
|
|
|
|
closure is provided. Safety is ensured by bounds-checking accesses, which
|
|
|
|
are marshalled through get and set functions.
|
|
|
|
|
|
|
|
There are three unsafe functions: the two introduction forms, and the
|
|
|
|
pointer elimination form. The introduction forms are unsafe for the obvious
|
|
|
|
reason (they act on a pointer that cannot be checked inside the method), but
|
|
|
|
the elimination form is somewhat more subtle in its unsafety. By using a
|
|
|
|
pointer taken from a c_vec::t without keeping a reference to the c_vec::t
|
|
|
|
itself around, the c_vec could be garbage collected, and the memory within
|
|
|
|
could be destroyed. There are legitimate uses for the pointer elimination
|
|
|
|
form -- for instance, to pass memory back into C -- but great care must be
|
|
|
|
taken to ensure that a reference to the c_vec::t is still held if needed.
|
2012-03-07 20:17:30 -06:00
|
|
|
"];
|
2011-11-23 04:15:04 -06:00
|
|
|
|
|
|
|
export t;
|
2012-03-12 17:52:30 -05:00
|
|
|
export c_vec, c_vec_with_dtor;
|
2011-11-23 04:15:04 -06:00
|
|
|
export get, set;
|
2012-01-06 10:12:18 -06:00
|
|
|
export len;
|
2011-11-23 04:15:04 -06:00
|
|
|
export ptr;
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
|
|
|
The type representing a native chunk of memory
|
2011-11-23 04:15:04 -06:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
Wrapped in a enum for opacity; FIXME #818 when it is possible to have
|
|
|
|
truly opaque types, this should be revisited.
|
|
|
|
"]
|
2012-01-19 17:20:57 -06:00
|
|
|
enum t<T> {
|
2012-01-19 21:29:21 -06:00
|
|
|
t({ base: *mutable T, len: uint, rsrc: @dtor_res})
|
2011-11-23 04:15:04 -06:00
|
|
|
}
|
|
|
|
|
2012-01-31 19:05:20 -06:00
|
|
|
resource dtor_res(dtor: option<fn@()>) {
|
2011-11-23 04:15:04 -06:00
|
|
|
alt dtor {
|
2012-01-19 00:37:22 -06:00
|
|
|
option::none { }
|
2011-11-23 04:15:04 -06:00
|
|
|
option::some(f) { f(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Introduction forms
|
|
|
|
*/
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2012-01-06 10:12:18 -06:00
|
|
|
Create a c_vec::t from a native buffer with a given length.
|
2011-12-06 15:58:45 -06:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Arguments
|
2011-12-06 15:58:45 -06:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
* base - A native pointer to a buffer
|
|
|
|
* len - The number of elements in the buffer
|
|
|
|
"]
|
2012-03-12 17:52:30 -05:00
|
|
|
unsafe fn c_vec<T>(base: *mutable T, len: uint) -> t<T> {
|
2011-11-23 04:15:04 -06:00
|
|
|
ret t({base: base,
|
2012-01-06 10:12:18 -06:00
|
|
|
len: len,
|
2011-11-23 04:15:04 -06:00
|
|
|
rsrc: @dtor_res(option::none)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2012-01-06 10:12:18 -06:00
|
|
|
Create a c_vec::t from a native buffer, with a given length,
|
2011-12-06 15:58:45 -06:00
|
|
|
and a function to run upon destruction.
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
# Arguments
|
2011-12-06 15:58:45 -06:00
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
* base - A native pointer to a buffer
|
|
|
|
* len - The number of elements in the buffer
|
|
|
|
* dtor - A function to run when the value is destructed, useful
|
|
|
|
for freeing the buffer, etc.
|
|
|
|
"]
|
2012-03-12 17:52:30 -05:00
|
|
|
unsafe fn c_vec_with_dtor<T>(base: *mutable T, len: uint, dtor: fn@())
|
2011-11-25 01:42:56 -06:00
|
|
|
-> t<T> {
|
2011-11-23 04:15:04 -06:00
|
|
|
ret t({base: base,
|
2012-01-06 10:12:18 -06:00
|
|
|
len: len,
|
2011-11-23 04:15:04 -06:00
|
|
|
rsrc: @dtor_res(option::some(dtor))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Operations
|
|
|
|
*/
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-12-06 15:58:45 -06:00
|
|
|
Retrieves an element at a given index
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
Fails if `ofs` is greater or equal to the length of the vector
|
|
|
|
"]
|
2012-01-05 08:35:37 -06:00
|
|
|
fn get<T: copy>(t: t<T>, ofs: uint) -> T {
|
2012-01-06 10:12:18 -06:00
|
|
|
assert ofs < len(t);
|
2011-11-25 01:42:56 -06:00
|
|
|
ret unsafe { *ptr::mut_offset((*t).base, ofs) };
|
2011-11-23 04:15:04 -06:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "
|
2011-12-06 15:58:45 -06:00
|
|
|
Sets the value of an element at a given index
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
Fails if `ofs` is greater or equal to the length of the vector
|
|
|
|
"]
|
2012-01-05 08:35:37 -06:00
|
|
|
fn set<T: copy>(t: t<T>, ofs: uint, v: T) {
|
2012-01-06 10:12:18 -06:00
|
|
|
assert ofs < len(t);
|
2011-11-25 01:42:56 -06:00
|
|
|
unsafe { *ptr::mut_offset((*t).base, ofs) = v };
|
2011-11-23 04:15:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Section: Elimination forms
|
|
|
|
*/
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Returns the length of the vector"]
|
2012-01-06 10:12:18 -06:00
|
|
|
fn len<T>(t: t<T>) -> uint {
|
|
|
|
ret (*t).len;
|
2011-11-23 04:15:04 -06:00
|
|
|
}
|
|
|
|
|
2012-03-07 20:17:30 -06:00
|
|
|
#[doc = "Returns a pointer to the first element of the vector"]
|
2011-11-25 01:42:56 -06:00
|
|
|
unsafe fn ptr<T>(t: t<T>) -> *mutable T {
|
2011-11-23 04:15:04 -06:00
|
|
|
ret (*t).base;
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-03-12 22:04:27 -05:00
|
|
|
import libc::*;
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
fn malloc(n: size_t) -> t<u8> {
|
|
|
|
let mem = libc::malloc(n);
|
|
|
|
|
|
|
|
assert mem as int != 0;
|
|
|
|
|
2012-03-12 17:52:30 -05:00
|
|
|
ret unsafe { c_vec_with_dtor(mem as *mutable u8, n,
|
|
|
|
bind free(mem)) };
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basic() {
|
|
|
|
let cv = malloc(16u);
|
|
|
|
|
|
|
|
set(cv, 3u, 8u8);
|
|
|
|
set(cv, 4u, 9u8);
|
|
|
|
assert get(cv, 3u) == 8u8;
|
|
|
|
assert get(cv, 4u) == 9u8;
|
|
|
|
assert len(cv) == 16u;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
#[ignore(cfg(target_os = "win32"))]
|
|
|
|
fn test_overrun_get() {
|
|
|
|
let cv = malloc(16u);
|
|
|
|
|
|
|
|
get(cv, 17u);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
#[ignore(cfg(target_os = "win32"))]
|
|
|
|
fn test_overrun_set() {
|
|
|
|
let cv = malloc(16u);
|
|
|
|
|
|
|
|
set(cv, 17u, 0u8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_and_I_mean_it() {
|
|
|
|
let cv = malloc(16u);
|
|
|
|
let p = unsafe { ptr(cv) };
|
|
|
|
|
|
|
|
set(cv, 0u, 32u8);
|
|
|
|
set(cv, 1u, 33u8);
|
|
|
|
assert unsafe { *p } == 32u8;
|
|
|
|
set(cv, 2u, 34u8); /* safety */
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|