Change resources to classes in libstd and rustc

This commit is contained in:
Tim Chevalier 2012-06-22 11:53:25 -07:00
parent 25aa360595
commit 21399dca12
8 changed files with 85 additions and 52 deletions

View File

@ -41,11 +41,15 @@ enum c_vec<T> {
c_vec_({ base: *mut T, len: uint, rsrc: @dtor_res})
}
resource dtor_res(dtor: option<fn@()>) {
alt dtor {
class dtor_res {
let dtor: option<fn@()>;
new(dtor: option<fn@()>) { self.dtor = dtor; }
drop {
alt self.dtor {
option::none { }
option::some(f) { f(); }
}
}
}
/*

View File

@ -35,33 +35,41 @@ Encapsulates an open TCP/IP connection through libuv
underlying libuv data structures when it goes out of scope. This is the
data structure that is used for read/write operations over a TCP stream.
"]
resource tcp_socket(socket_data: @tcp_socket_data)
class tcp_socket {
let socket_data: @tcp_socket_data;
new(socket_data: @tcp_socket_data) { self.socket_data = socket_data; }
drop {
unsafe {
let closed_po = comm::port::<()>();
let closed_ch = comm::chan(closed_po);
let close_data = {
closed_ch: closed_ch
};
let close_data_ptr = ptr::addr_of(close_data);
let stream_handle_ptr = (*socket_data).stream_handle_ptr;
iotask::interact((*socket_data).iotask) {|loop_ptr|
log(debug, #fmt("interact dtor for tcp_socket stream %? loop %?",
let closed_po = comm::port::<()>();
let closed_ch = comm::chan(closed_po);
let close_data = {
closed_ch: closed_ch
};
let close_data_ptr = ptr::addr_of(close_data);
let stream_handle_ptr = (*(self.socket_data)).stream_handle_ptr;
iotask::interact((*(self.socket_data)).iotask) {|loop_ptr|
log(debug, #fmt("interact dtor for tcp_socket stream %? loop %?",
stream_handle_ptr, loop_ptr));
uv::ll::set_data_for_uv_handle(stream_handle_ptr,
uv::ll::set_data_for_uv_handle(stream_handle_ptr,
close_data_ptr);
uv::ll::close(stream_handle_ptr, tcp_socket_dtor_close_cb);
};
comm::recv(closed_po);
log(debug, #fmt("about to free socket_data at %?", socket_data));
rustrt::rust_uv_current_kernel_free(stream_handle_ptr
as *libc::c_void);
log(debug, "exiting dtor for tcp_socket");
uv::ll::close(stream_handle_ptr, tcp_socket_dtor_close_cb);
};
comm::recv(closed_po);
log(debug, #fmt("about to free socket_data at %?", self.socket_data));
rustrt::rust_uv_current_kernel_free(stream_handle_ptr
as *libc::c_void);
log(debug, "exiting dtor for tcp_socket");
}
}
}
resource tcp_conn_port(conn_data: @tcp_conn_port_data) unsafe {
let conn_data_ptr = ptr::addr_of(*conn_data);
class tcp_conn_port {
let conn_data: @tcp_conn_port_data;
new(conn_data: @tcp_conn_port_data) { self.conn_data = conn_data; }
drop unsafe {
let conn_data_ptr = ptr::addr_of(*(self.conn_data));
let server_stream_ptr = ptr::addr_of((*conn_data_ptr).server_stream);
let stream_closed_po = (*conn_data).stream_closed_po;
let stream_closed_po = (*(self.conn_data)).stream_closed_po;
let iotask = (*conn_data_ptr).iotask;
iotask::interact(iotask) {|loop_ptr|
log(debug, #fmt("dtor for tcp_conn_port loop: %?",
@ -69,6 +77,7 @@ resource tcp_conn_port(conn_data: @tcp_conn_port_data) unsafe {
uv::ll::close(server_stream_ptr, tcp_nl_close_cb);
}
comm::recv(stream_closed_po);
}
}
#[doc="
@ -207,7 +216,7 @@ value as the `err` variant
"]
fn write(sock: tcp_socket, raw_write_data: [u8])
-> result::result<(), tcp_err_data> unsafe {
let socket_data_ptr = ptr::addr_of(**sock);
let socket_data_ptr = ptr::addr_of(*(sock.socket_data));
write_common_impl(socket_data_ptr, raw_write_data)
}
@ -240,7 +249,7 @@ value as the `err` variant
"]
fn write_future(sock: tcp_socket, raw_write_data: [u8])
-> future::future<result::result<(), tcp_err_data>> unsafe {
let socket_data_ptr = ptr::addr_of(**sock);
let socket_data_ptr = ptr::addr_of(*(sock.socket_data));
future::spawn {||
write_common_impl(socket_data_ptr, raw_write_data)
}
@ -262,7 +271,7 @@ on) from until `read_stop` is called, or a `tcp_err_data` record
fn read_start(sock: tcp_socket)
-> result::result<comm::port<
result::result<[u8], tcp_err_data>>, tcp_err_data> unsafe {
let socket_data = ptr::addr_of(**sock);
let socket_data = ptr::addr_of(*(sock.socket_data));
read_start_common_impl(socket_data)
}
@ -275,7 +284,7 @@ Stop reading from an open TCP connection; used with `read_start`
"]
fn read_stop(sock: tcp_socket) ->
result::result<(), tcp_err_data> unsafe {
let socket_data = ptr::addr_of(**sock);
let socket_data = ptr::addr_of(*(sock.socket_data));
read_stop_common_impl(socket_data)
}
@ -295,7 +304,7 @@ read attempt. Pass `0u` to wait indefinitely
"]
fn read(sock: tcp_socket, timeout_msecs: uint)
-> result::result<[u8],tcp_err_data> {
let socket_data = ptr::addr_of(**sock);
let socket_data = ptr::addr_of(*(sock.socket_data));
read_common_impl(socket_data, timeout_msecs)
}
@ -329,7 +338,7 @@ read attempt. Pass `0u` to wait indefinitely
"]
fn read_future(sock: tcp_socket, timeout_msecs: uint)
-> future::future<result::result<[u8],tcp_err_data>> {
let socket_data = ptr::addr_of(**sock);
let socket_data = ptr::addr_of(*(sock.socket_data));
future::spawn {||
read_common_impl(socket_data, timeout_msecs)
}
@ -444,8 +453,8 @@ variant
"]
fn conn_recv(server_port: tcp_conn_port)
-> result::result<tcp_socket, tcp_err_data> {
let new_conn_po = (**server_port).new_conn_po;
let iotask = (**server_port).iotask;
let new_conn_po = (*(server_port.conn_data)).new_conn_po;
let iotask = (*(server_port.conn_data)).iotask;
let new_conn_result = comm::recv(new_conn_po);
alt new_conn_result {
ok(client_stream_ptr) {
@ -475,8 +484,8 @@ once a new connection is recv'd. Its parameter:
"]
fn conn_recv_spawn(server_port: tcp_conn_port,
+cb: fn~(result::result<tcp_socket, tcp_err_data>)) {
let new_conn_po = (**server_port).new_conn_po;
let iotask = (**server_port).iotask;
let new_conn_po = (*(server_port.conn_data)).new_conn_po;
let iotask = (*(server_port.conn_data)).iotask;
let new_conn_result = comm::recv(new_conn_po);
task::spawn {||
let sock_create_result = alt new_conn_result {
@ -507,7 +516,7 @@ connection
none.
"]
fn conn_peek(server_port: tcp_conn_port) -> bool {
let new_conn_po = (**server_port).new_conn_po;
let new_conn_po = (*(server_port.conn_data)).new_conn_po;
comm::peek(new_conn_po)
}

View File

@ -223,8 +223,10 @@ fn monitor(+f: fn~(diagnostic::emitter)) {
diagnostic::emit(cmsp, msg, lvl);
};
resource finally(ch: comm::chan<monitor_msg>) {
comm::send(ch, done);
class finally {
let ch: comm::chan<monitor_msg>;
new(ch: comm::chan<monitor_msg>) { self.ch = ch; }
drop { comm::send(self.ch, done); }
}
let _finally = finally(ch);

View File

@ -1092,8 +1092,10 @@ fn fn_ty_param_tys(fn_ty: TypeRef) -> [TypeRef] unsafe {
/* Memory-managed interface to target data. */
resource target_data_res(TD: TargetDataRef) {
llvm::LLVMDisposeTargetData(TD);
class target_data_res {
let TD: TargetDataRef;
new(TD: TargetDataRef) { self.TD = TD; }
drop { llvm::LLVMDisposeTargetData(self.TD); }
}
type target_data = {lltd: TargetDataRef, dtor: @target_data_res};
@ -1106,8 +1108,10 @@ fn mk_target_data(string_rep: str) -> target_data {
/* Memory-managed interface to pass managers. */
resource pass_manager_res(PM: PassManagerRef) {
llvm::LLVMDisposePassManager(PM);
class pass_manager_res {
let PM: PassManagerRef;
new(PM: PassManagerRef) { self.PM = PM; }
drop { llvm::LLVMDisposePassManager(self.PM); }
}
type pass_manager = {llpm: PassManagerRef, dtor: @pass_manager_res};
@ -1119,8 +1123,10 @@ fn mk_pass_manager() -> pass_manager {
/* Memory-managed interface to object files. */
resource object_file_res(ObjectFile: ObjectFileRef) {
llvm::LLVMDisposeObjectFile(ObjectFile);
class object_file_res {
let ObjectFile: ObjectFileRef;
new(ObjectFile: ObjectFileRef) { self.ObjectFile = ObjectFile; }
drop { llvm::LLVMDisposeObjectFile(self.ObjectFile); }
}
type object_file = {llof: ObjectFileRef, dtor: @object_file_res};
@ -1133,8 +1139,10 @@ fn mk_object_file(llmb: MemoryBufferRef) -> option<object_file> {
/* Memory-managed interface to section iterators. */
resource section_iter_res(SI: SectionIteratorRef) {
llvm::LLVMDisposeSectionIterator(SI);
class section_iter_res {
let SI: SectionIteratorRef;
new(SI: SectionIteratorRef) { self.SI = SI; }
drop { llvm::LLVMDisposeSectionIterator(self.SI); }
}
type section_iter = {llsi: SectionIteratorRef, dtor: @section_iter_res};

View File

@ -88,9 +88,13 @@ fn dup_for_join(dest: dest) -> dest {
}
}
resource icx_popper(ccx: @crate_ctxt) {
if ccx.sess.count_llvm_insns() {
vec::pop(*ccx.stats.llvm_insn_ctxt);
class icx_popper {
let ccx: @crate_ctxt;
new(ccx: @crate_ctxt) { self.ccx = ccx; }
drop {
if self.ccx.sess.count_llvm_insns() {
vec::pop(*(self.ccx.stats.llvm_insn_ctxt));
}
}
}
@ -5186,7 +5190,7 @@ fn create_main_wrapper(ccx: @crate_ctxt, sp: span, main_llfn: ValueRef,
let llbb = str::as_c_str("top", {|buf|
llvm::LLVMAppendBasicBlock(llfn, buf)
});
let bld = *ccx.builder;
let bld = ccx.builder.B;
llvm::LLVMPositionBuilderAtEnd(bld, llbb);
let crate_map = ccx.crate_map;
let start_ty = T_fn([val_ty(rust_main), ccx.int_type, ccx.int_type,

View File

@ -10,7 +10,7 @@ import common::*;
import driver::session::session;
fn B(cx: block) -> BuilderRef {
let b = *cx.fcx.ccx.builder;
let b = cx.fcx.ccx.builder.B;
llvm::LLVMPositionBuilderAtEnd(b, cx.llbb);
ret b;
}

View File

@ -62,7 +62,11 @@ type stats =
llvm_insns: hashmap<str, uint>,
fn_times: @mut [{ident: str, time: int}]};
resource BuilderRef_res(B: BuilderRef) { llvm::LLVMDisposeBuilder(B); }
class BuilderRef_res {
let B: BuilderRef;
new(B: BuilderRef) { self.B = B; }
drop { llvm::LLVMDisposeBuilder(self.B); }
}
// Crate context. Every crate we compile has one of these.
type crate_ctxt = {

View File

@ -14,8 +14,10 @@ fn indent<R>(op: fn() -> R) -> R {
ret r;
}
resource _indenter(_i: ()) {
#debug["<<"];
class _indenter {
let _i: ();
new(_i: ()) { self._i = (); }
drop { #debug["<<"]; }
}
fn indenter() -> _indenter {