diff options
| author | Marijn Haverbeke <marijnh@gmail.com> | 2011-08-25 10:18:02 +0200 |
|---|---|---|
| committer | Marijn Haverbeke <marijnh@gmail.com> | 2011-08-29 09:07:53 +0200 |
| commit | c9c5ee252a8523778377f2832765442e611e85a4 (patch) | |
| tree | 85c0837af34b2431fc17da0a166254144aaa99c7 /src/lib | |
| parent | 855e0a471365c7c61a139e2437215028bd231af5 (diff) | |
| download | rust-c9c5ee252a8523778377f2832765442e611e85a4.tar.gz rust-c9c5ee252a8523778377f2832765442e611e85a4.zip | |
Implement non-internal ivecs
Vectors are now similar to our old, pre-internal vectors, except that they are uniquely owned, not refcounted. Their name should probably change too, then. I've renamed them to vec in the runtime, will do so throughout the compiler later.
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/aio.rs | 4 | ||||
| -rw-r--r-- | src/lib/io.rs | 7 | ||||
| -rw-r--r-- | src/lib/run_program.rs | 3 | ||||
| -rw-r--r-- | src/lib/str.rs | 6 | ||||
| -rw-r--r-- | src/lib/vec.rs | 54 |
5 files changed, 32 insertions, 42 deletions
diff --git a/src/lib/aio.rs b/src/lib/aio.rs index 0f0119cdd7c..92738d01f92 100644 --- a/src/lib/aio.rs +++ b/src/lib/aio.rs @@ -45,7 +45,7 @@ tag request { type ctx = chan<request>; fn ip_to_sbuf(ip: net::ip_addr) -> *u8 { - vec::to_ptr(str::bytes(net::format_addr(ip))) + vec::unsafe::to_ptr(str::bytes(net::format_addr(ip))) } fn connect_task(ip: net::ip_addr, portnum: int, evt: chan<socket_event>) { @@ -132,7 +132,7 @@ fn request_task(c: chan<ctx>) { task::spawn(bind server_task(ip, portnum, events, server)); } write(socket, v, status) { - rustrt::aio_writedata(socket, vec::to_ptr::<u8>(v), + rustrt::aio_writedata(socket, vec::unsafe::to_ptr::<u8>(v), vec::len::<u8>(v), status); } close_server(server, status) { diff --git a/src/lib/io.rs b/src/lib/io.rs index 4be9d380c8d..342d3bbd9c3 100644 --- a/src/lib/io.rs +++ b/src/lib/io.rs @@ -59,7 +59,8 @@ obj FILE_buf_reader(f: os::libc::FILE, res: option::t<@FILE_res>) { fn read(len: uint) -> [u8] { let buf = []; vec::reserve::<u8>(buf, len); - let read = os::libc::fread(vec::to_ptr::<u8>(buf), 1u, len, f); + let read = os::libc::fread(vec::unsafe::to_ptr::<u8>(buf), + 1u, len, f); vec::unsafe::set_len::<u8>(buf, read); ret buf; } @@ -237,7 +238,7 @@ type buf_writer = obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) { fn write(v: &[u8]) { let len = vec::len::<u8>(v); - let vbuf = vec::to_ptr::<u8>(v); + let vbuf = vec::unsafe::to_ptr::<u8>(v); let nout = os::libc::fwrite(vbuf, len, 1u, f); if nout < 1u { log_err "error dumping buffer"; } } @@ -255,7 +256,7 @@ obj fd_buf_writer(fd: int, res: option::t<@fd_res>) { let count = 0u; let vbuf; while count < len { - vbuf = ptr::offset(vec::to_ptr::<u8>(v), count); + vbuf = ptr::offset(vec::unsafe::to_ptr::<u8>(v), count); let nout = os::libc::write(fd, vbuf, len); if nout < 0 { log_err "error dumping buffer"; diff --git a/src/lib/run_program.rs b/src/lib/run_program.rs index 4482431f67c..6d6940db872 100644 --- a/src/lib/run_program.rs +++ b/src/lib/run_program.rs @@ -27,7 +27,8 @@ fn spawn_process(prog: &istr, args: &[istr], in_fd: int, out_fd: int, // pointer to its buffer let argv = arg_vec(prog, args); let pid = - rustrt::rust_run_program(vec::to_ptr(argv), in_fd, out_fd, err_fd); + rustrt::rust_run_program(vec::unsafe::to_ptr(argv), + in_fd, out_fd, err_fd); ret pid; } diff --git a/src/lib/str.rs b/src/lib/str.rs index eff9e9bfd28..54739e4188d 100644 --- a/src/lib/str.rs +++ b/src/lib/str.rs @@ -60,7 +60,7 @@ native "rust" mod rustrt { fn str_buf(s: str) -> sbuf; fn str_byte_len(s: str) -> uint; fn str_alloc(n_bytes: uint) -> str; - fn str_from_ivec(b: &[mutable? u8]) -> str; + fn str_from_vec(b: &[mutable? u8]) -> str; fn str_from_cstr(cstr: sbuf) -> str; fn str_from_buf(buf: sbuf, len: uint) -> str; fn str_push_byte(s: str, byte: uint) -> str; @@ -187,10 +187,10 @@ fn bytes(s: str) -> [u8] { } fn unsafe_from_bytes(v: &[mutable? u8]) -> str { - ret rustrt::str_from_ivec(v); + ret rustrt::str_from_vec(v); } -fn unsafe_from_byte(u: u8) -> str { ret rustrt::str_from_ivec([u]); } +fn unsafe_from_byte(u: u8) -> str { ret rustrt::str_from_vec([u]); } fn str_from_cstr(cstr: sbuf) -> str { ret rustrt::str_from_cstr(cstr); } diff --git a/src/lib/vec.rs b/src/lib/vec.rs index f387ec75b51..28c347cd2c8 100644 --- a/src/lib/vec.rs +++ b/src/lib/vec.rs @@ -6,27 +6,20 @@ import uint::next_power_of_two; import ptr::addr_of; native "rust-intrinsic" mod rusti { - fn ivec_len<T>(v: &[T]) -> uint; + fn vec_len<T>(v: &[T]) -> uint; } native "rust" mod rustrt { - fn ivec_reserve_shared<T>(v: &mutable [mutable? T], n: uint); - fn ivec_on_heap<T>(v: &[T]) -> uint; - fn ivec_to_ptr<T>(v: &[T]) -> *T; - fn ivec_copy_from_buf_shared<T>(v: &mutable [mutable? T], ptr: *T, - count: uint); + fn vec_reserve_shared<T>(v: &mutable [mutable? T], n: uint); + fn vec_from_buf_shared<T>(ptr: *T, count: uint) -> [T]; } /// Reserves space for `n` elements in the given vector. fn reserve<@T>(v: &mutable [mutable? T], n: uint) { - rustrt::ivec_reserve_shared(v, n); + rustrt::vec_reserve_shared(v, n); } -fn on_heap<T>(v: &[T]) -> bool { ret rustrt::ivec_on_heap(v) != 0u; } - -fn to_ptr<T>(v: &[T]) -> *T { ret rustrt::ivec_to_ptr(v); } - -fn len<T>(v: &[mutable? T]) -> uint { ret rusti::ivec_len(v); } +fn len<T>(v: &[mutable? T]) -> uint { ret rusti::vec_len(v); } type init_op<T> = fn(uint) -> T; @@ -310,32 +303,27 @@ iter iter2<@T>(v: &[T]) -> (uint, T) { } mod unsafe { - type ivec_repr = - {mutable fill: uint, - mutable alloc: uint, - heap_part: *mutable ivec_heap_part}; - type ivec_heap_part = {mutable fill: uint}; - - fn copy_from_buf<T>(v: &mutable [T], ptr: *T, count: uint) { - ret rustrt::ivec_copy_from_buf_shared(v, ptr, count); - } + type ivec_repr = {mutable fill: uint, + mutable alloc: uint, + data: u8}; - fn from_buf<T>(ptr: *T, bytes: uint) -> [T] { - let v = []; - copy_from_buf(v, ptr, bytes); - ret v; + fn from_buf<T>(ptr: *T, elts: uint) -> [T] { + ret rustrt::vec_from_buf_shared(ptr, elts); } fn set_len<T>(v: &mutable [T], new_len: uint) { - let new_fill = new_len * sys::size_of::<T>(); - let stack_part: *mutable ivec_repr = - ::unsafe::reinterpret_cast(addr_of(v)); - if (*stack_part).fill == 0u { - (*(*stack_part).heap_part).fill = new_fill; // On heap. - } else { - (*stack_part).fill = new_fill; // On stack. - } + let repr: **ivec_repr = ::unsafe::reinterpret_cast(addr_of(v)); + (**repr).fill = new_len * sys::size_of::<T>(); } + + fn to_ptr<T>(v: &[T]) -> *T { + let repr: **ivec_repr = ::unsafe::reinterpret_cast(addr_of(v)); + ret ::unsafe::reinterpret_cast(addr_of((**repr).data)); + } +} + +fn to_ptr<T>(v: &[T]) -> *T { + ret unsafe::to_ptr(v); } // Local Variables: |
