Skip to content

Commit 54bb722

Browse files
committed
core: Simplify uvll bindings and strip out currently-unused bits
No more mapping uv structs to Rust structs
1 parent 723d224 commit 54bb722

File tree

5 files changed

+372
-1788
lines changed

5 files changed

+372
-1788
lines changed

src/libcore/rt/uv.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,28 @@ use cast::{transmute, transmute_mut_region};
4444
use ptr::null;
4545
use sys::size_of;
4646
use super::uvll;
47+
use super::uvll::*;
4748
use super::io::{IpAddr, Ipv4, Ipv6};
49+
use unstable::finally::Finally;
4850

4951
#[cfg(test)] use unstable::run_in_bare_thread;
5052
#[cfg(test)] use super::thread::Thread;
5153
#[cfg(test)] use cell::Cell;
5254

53-
fn ip4_to_uv_ip4(addr: IpAddr) -> uvll::sockaddr_in {
55+
fn ip4_as_uv_ip4(addr: IpAddr, f: &fn(*sockaddr_in)) {
5456
match addr {
5557
Ipv4(a, b, c, d, p) => {
5658
unsafe {
57-
uvll::ip4_addr(fmt!("%u.%u.%u.%u",
58-
a as uint,
59-
b as uint,
60-
c as uint,
61-
d as uint), p as int)
59+
let addr = malloc_ip4_addr(fmt!("%u.%u.%u.%u",
60+
a as uint,
61+
b as uint,
62+
c as uint,
63+
d as uint), p as int);
64+
do (|| {
65+
f(addr);
66+
}).finally {
67+
free_ip4_addr(addr);
68+
}
6269
}
6370
}
6471
Ipv6 => fail!()
@@ -301,7 +308,7 @@ pub impl StreamWatcher {
301308
data.close_cb.swap_unwrap()();
302309
}
303310
drop_watcher_data(&mut stream_watcher);
304-
unsafe { free(handle as *c_void) }
311+
unsafe { free_handle(handle as *c_void) }
305312
}
306313
}
307314
}
@@ -330,8 +337,7 @@ impl Callback for ConnectionCallback { }
330337
pub impl TcpWatcher {
331338
static fn new(loop_: &mut Loop) -> TcpWatcher {
332339
unsafe {
333-
let size = size_of::<uvll::uv_tcp_t>() as size_t;
334-
let handle = malloc(size) as *uvll::uv_tcp_t;
340+
let handle = malloc_handle(UV_TCP);
335341
fail_unless!(handle.is_not_null());
336342
fail_unless!(0 == uvll::tcp_init(loop_.native_handle(), handle));
337343
let mut watcher = NativeHandle::from_native_handle(handle);
@@ -343,12 +349,13 @@ pub impl TcpWatcher {
343349
fn bind(&mut self, address: IpAddr) {
344350
match address {
345351
Ipv4(*) => {
346-
let addr = ip4_to_uv_ip4(address);
347-
let result = unsafe {
348-
uvll::tcp_bind(self.native_handle(), &addr)
349-
};
350-
// XXX: bind is likely to fail. need real error handling
351-
fail_unless!(result == 0);
352+
do ip4_as_uv_ip4(address) |addr| {
353+
let result = unsafe {
354+
uvll::tcp_bind(self.native_handle(), addr)
355+
};
356+
// XXX: bind is likely to fail. need real error handling
357+
fail_unless!(result == 0);
358+
}
352359
}
353360
_ => fail!()
354361
}
@@ -363,11 +370,12 @@ pub impl TcpWatcher {
363370
let connect_handle = connect_watcher.native_handle();
364371
match address {
365372
Ipv4(*) => {
366-
let addr = ip4_to_uv_ip4(address);
367-
rtdebug!("connect_t: %x", connect_handle as uint);
368-
fail_unless!(0 == uvll::tcp_connect(connect_handle,
369-
self.native_handle(),
370-
&addr, connect_cb));
373+
do ip4_as_uv_ip4(address) |addr| {
374+
rtdebug!("connect_t: %x", connect_handle as uint);
375+
fail_unless!(0 == uvll::tcp_connect(connect_handle,
376+
self.native_handle(),
377+
addr, connect_cb));
378+
}
371379
}
372380
_ => fail!()
373381
}
@@ -443,7 +451,7 @@ impl ConnectRequest {
443451

444452
static fn new() -> ConnectRequest {
445453
let connect_handle = unsafe {
446-
malloc(size_of::<uvll::uv_connect_t>() as size_t)
454+
malloc_req(UV_CONNECT)
447455
};
448456
fail_unless!(connect_handle.is_not_null());
449457
let connect_handle = connect_handle as *uvll::uv_connect_t;
@@ -460,7 +468,7 @@ impl ConnectRequest {
460468
}
461469

462470
fn delete(self) {
463-
unsafe { free(self.native_handle() as *c_void) }
471+
unsafe { free_req(self.native_handle() as *c_void) }
464472
}
465473
}
466474

@@ -482,7 +490,7 @@ impl WriteRequest {
482490

483491
static fn new() -> WriteRequest {
484492
let write_handle = unsafe {
485-
malloc(size_of::<uvll::uv_write_t>() as size_t)
493+
malloc_req(UV_WRITE)
486494
};
487495
fail_unless!(write_handle.is_not_null());
488496
let write_handle = write_handle as *uvll::uv_write_t;
@@ -498,7 +506,7 @@ impl WriteRequest {
498506
}
499507

500508
fn delete(self) {
501-
unsafe { free(self.native_handle() as *c_void) }
509+
unsafe { free_req(self.native_handle() as *c_void) }
502510
}
503511
}
504512

0 commit comments

Comments
 (0)