@@ -44,21 +44,28 @@ use cast::{transmute, transmute_mut_region};
44
44
use ptr:: null;
45
45
use sys:: size_of;
46
46
use super :: uvll;
47
+ use super :: uvll:: * ;
47
48
use super :: io:: { IpAddr , Ipv4 , Ipv6 } ;
49
+ use unstable:: finally:: Finally ;
48
50
49
51
#[ cfg( test) ] use unstable:: run_in_bare_thread;
50
52
#[ cfg( test) ] use super :: thread:: Thread ;
51
53
#[ cfg( test) ] use cell:: Cell ;
52
54
53
- fn ip4_to_uv_ip4 ( addr : IpAddr ) -> uvll :: sockaddr_in {
55
+ fn ip4_as_uv_ip4 ( addr : IpAddr , f : & fn ( * sockaddr_in ) ) {
54
56
match addr {
55
57
Ipv4 ( a, b, c, d, p) => {
56
58
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
+ }
62
69
}
63
70
}
64
71
Ipv6 => fail ! ( )
@@ -301,7 +308,7 @@ pub impl StreamWatcher {
301
308
data. close_cb . swap_unwrap ( ) ( ) ;
302
309
}
303
310
drop_watcher_data ( & mut stream_watcher) ;
304
- unsafe { free ( handle as * c_void ) }
311
+ unsafe { free_handle ( handle as * c_void ) }
305
312
}
306
313
}
307
314
}
@@ -330,8 +337,7 @@ impl Callback for ConnectionCallback { }
330
337
pub impl TcpWatcher {
331
338
static fn new( loop_: & mut Loop ) -> TcpWatcher {
332
339
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 ) ;
335
341
fail_unless ! ( handle. is_not_null( ) ) ;
336
342
fail_unless ! ( 0 == uvll:: tcp_init( loop_. native_handle( ) , handle) ) ;
337
343
let mut watcher = NativeHandle :: from_native_handle ( handle) ;
@@ -343,12 +349,13 @@ pub impl TcpWatcher {
343
349
fn bind ( & mut self , address : IpAddr ) {
344
350
match address {
345
351
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
+ }
352
359
}
353
360
_ => fail ! ( )
354
361
}
@@ -363,11 +370,12 @@ pub impl TcpWatcher {
363
370
let connect_handle = connect_watcher. native_handle ( ) ;
364
371
match address {
365
372
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
+ }
371
379
}
372
380
_ => fail ! ( )
373
381
}
@@ -443,7 +451,7 @@ impl ConnectRequest {
443
451
444
452
static fn new( ) -> ConnectRequest {
445
453
let connect_handle = unsafe {
446
- malloc ( size_of : : <uvll :: uv_connect_t > ( ) as size_t )
454
+ malloc_req ( UV_CONNECT )
447
455
} ;
448
456
fail_unless!( connect_handle. is_not_null( ) ) ;
449
457
let connect_handle = connect_handle as * uvll: : uv_connect_t ;
@@ -460,7 +468,7 @@ impl ConnectRequest {
460
468
}
461
469
462
470
fn delete ( self ) {
463
- unsafe { free ( self . native_handle ( ) as * c_void ) }
471
+ unsafe { free_req ( self . native_handle ( ) as * c_void ) }
464
472
}
465
473
}
466
474
@@ -482,7 +490,7 @@ impl WriteRequest {
482
490
483
491
static fn new( ) -> WriteRequest {
484
492
let write_handle = unsafe {
485
- malloc ( size_of : : <uvll :: uv_write_t > ( ) as size_t )
493
+ malloc_req ( UV_WRITE )
486
494
} ;
487
495
fail_unless!( write_handle. is_not_null( ) ) ;
488
496
let write_handle = write_handle as * uvll: : uv_write_t ;
@@ -498,7 +506,7 @@ impl WriteRequest {
498
506
}
499
507
500
508
fn delete ( self ) {
501
- unsafe { free ( self . native_handle ( ) as * c_void ) }
509
+ unsafe { free_req ( self . native_handle ( ) as * c_void ) }
502
510
}
503
511
}
504
512
0 commit comments