From e0193dac6edae6c4c5dd1a8fffeb3d9f922769bd Mon Sep 17 00:00:00 2001 From: Jeff Olson Date: Thu, 22 Mar 2012 21:49:48 -0700 Subject: [PATCH] uv_buf_t's for uv_write() passed by-val .. no more mallocs or ptr cop-outs so we're now adhering the libuv C api and passing structs by-val where it is expected, instead of pulling pointer trickery (or worse having to malloc structs in c++ to be passed back to rust and then into C again) --- src/libstd/uv.rs | 22 +++++----------------- src/rt/rust_uv.cpp | 18 ++++-------------- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/libstd/uv.rs b/src/libstd/uv.rs index e52b1121a6f..33676bc470d 100644 --- a/src/libstd/uv.rs +++ b/src/libstd/uv.rs @@ -271,7 +271,7 @@ native mod rustrt { loop_handle: *libc::c_void, handle_ptr: *uv_tcp_t) -> libc::c_int; fn rust_uv_buf_init(base: *u8, len: libc::size_t) - -> *libc::c_void; + -> uv_buf_t; fn rust_uv_last_error(loop_handle: *libc::c_void) -> uv_err_t; fn rust_uv_ip4_test_verify_port_val(++addr: sockaddr_in, expected: libc::c_uint) @@ -283,7 +283,7 @@ native mod rustrt { ++addr: sockaddr_in, after_cb: *u8) -> libc::c_int; fn rust_uv_write(req: *libc::c_void, stream: *libc::c_void, - buf_in: **libc::c_void, buf_cnt: libc::c_int, + ++buf_in: *uv_buf_t, buf_cnt: libc::c_int, cb: *u8) -> libc::c_int; // sizeof testing helpers @@ -342,7 +342,7 @@ mod direct { // to malloc'd buffers .. these will have to be translated // back into their value types in c. sigh. unsafe fn write(req: *libc::c_void, stream: *libc::c_void, - buf_in: *[*libc::c_void], cb: *u8) -> libc::c_int { + buf_in: *[uv_buf_t], cb: *u8) -> libc::c_int { let buf_ptr = vec::unsafe::to_ptr(*buf_in); let buf_cnt = vec::len(*buf_in) as i32; ret rustrt::rust_uv_write(req, stream, buf_ptr, buf_cnt, cb); @@ -379,7 +379,7 @@ mod direct { rustrt::rust_uv_set_data_for_req(req, data); } // TODO: see github issue #1402 - unsafe fn buf_init(input: *u8, len: uint) -> *libc::c_void { + unsafe fn buf_init(input: *u8, len: uint) -> uv_buf_t { ret rustrt::rust_uv_buf_init(input, len); } unsafe fn ip4_addr(ip: str, port: int) @@ -950,21 +950,9 @@ fn test_uv_timer() { type request_wrapper = { write_req: *uv_write_t, - req_buf: *[*libc::c_void] + req_buf: *[uv_buf_t] }; -crust fn on_alloc(handle: *libc::c_void, - suggested_size: libc::size_t) -> uv_buf_t - unsafe { - io::println("beginning on_alloc..."); - io::println("ending on_alloc..."); - let new_vec: @[u8] = @[]; - let ptr = vec::unsafe::to_ptr(*new_vec); - let buf = direct::buf_init(ptr, vec::len(*new_vec)); - ret *(buf as *uv_buf_t); - -} - crust fn on_write_complete_cb(write_handle: *uv_write_t, status: libc::c_int) unsafe { io::println(#fmt("beginning on_write_complete_cb status: %d", diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp index cfe3b78b108..b4694ccf37c 100644 --- a/src/rt/rust_uv.cpp +++ b/src/rt/rust_uv.cpp @@ -244,13 +244,9 @@ current_kernel_malloc_alloc_cb(uv_handle_t* handle, } // FIXME see issue #1402 -extern "C" void* +extern "C" uv_buf_t rust_uv_buf_init(char* base, size_t len) { - uv_buf_t* buf_ptr = (uv_buf_t*)current_kernel_malloc( - sizeof(uv_buf_t), - "uv_buf_t_1402"); - *buf_ptr = uv_buf_init(base, len); - return buf_ptr; + return uv_buf_init(base, len); } extern "C" uv_loop_t* @@ -302,15 +298,9 @@ rust_uv_tcp_connect(uv_connect_t* connect_ptr, extern "C" int rust_uv_write(uv_write_t* req, uv_stream_t* handle, - void** bufs, int buf_cnt, + uv_buf_t* bufs, int buf_cnt, uv_write_cb cb) { - // TODO github #1402 -- convert this array of pointers to - // uv_buf_t into an array of uv_buf_t values - uv_buf_t buf_vals[buf_cnt]; - for(int ctr = 0; ctr < buf_cnt; ctr++) { - buf_vals[ctr] = *((uv_buf_t*)bufs[ctr]); - } - return uv_write(req, handle, buf_vals, buf_cnt, cb); + return uv_write(req, handle, bufs, buf_cnt, cb); } extern "C" struct sockaddr_in