Skip to content

Commit d5173b1

Browse files
committed
Use cstrcache in C_str, C_cstr, C_shape.
This fixes up the current leaks.
1 parent 4e136d1 commit d5173b1

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/comp/middle/trans.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6395,7 +6395,8 @@ fn trans_crate(sess: &session::session, crate: &@ast::crate, tcx: &ty::ctxt,
63956395
task_type: task_type,
63966396
builder: BuilderRef_res(llvm::LLVMCreateBuilder()),
63976397
shape_cx: shape::mk_ctxt(llmod),
6398-
gc_cx: gc::mk_ctxt()};
6398+
gc_cx: gc::mk_ctxt(),
6399+
cstrcache: cstrcache::mk()};
63996400
let cx = new_local_ctxt(ccx);
64006401
collect_items(ccx, crate);
64016402
collect_tag_ctors(ccx, crate);

src/comp/middle/trans_common.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ type crate_ctxt =
154154
task_type: TypeRef,
155155
builder: BuilderRef_res,
156156
shape_cx: shape::ctxt,
157-
gc_cx: gc::ctxt};
157+
gc_cx: gc::ctxt,
158+
cstrcache: cstrcache::t};
158159

159160
type local_ctxt =
160161
{path: [str],
@@ -842,10 +843,11 @@ fn C_u8(i: uint) -> ValueRef { ret C_integral(T_i8(), i, False); }
842843
// This is a 'c-like' raw string, which differs from
843844
// our boxed-and-length-annotated strings.
844845
fn C_cstr(cx: &@crate_ctxt, s: &str) -> ValueRef {
845-
let sc = llvm::LLVMConstString(str::buf(s), str::byte_len(s), False);
846+
let sc = llvm::LLVMConstString(safe_sbuf(cx, s),
847+
str::byte_len(s), False);
846848
let g =
847849
llvm::LLVMAddGlobal(cx.llmod, val_ty(sc),
848-
str::buf(cx.names.next("str")));
850+
safe_sbuf(cx, cx.names.next("str")));
849851
llvm::LLVMSetInitializer(g, sc);
850852
llvm::LLVMSetGlobalConstant(g, True);
851853
llvm::LLVMSetLinkage(g, lib::llvm::LLVMInternalLinkage as llvm::Linkage);
@@ -863,10 +865,10 @@ fn C_str(cx: &@crate_ctxt, s: &str) -> ValueRef {
863865
let box =
864866
C_struct([C_int(abi::const_refcount as int), C_int(len + 1u as int),
865867
C_int(len + 1u as int), C_int(0),
866-
llvm::LLVMConstString(str::buf(s), len, False)]);
868+
llvm::LLVMConstString(safe_sbuf(cx, s), len, False)]);
867869
let g =
868870
llvm::LLVMAddGlobal(cx.llmod, val_ty(box),
869-
str::buf(cx.names.next("str")));
871+
safe_sbuf(cx, cx.names.next("str")));
870872
llvm::LLVMSetInitializer(g, box);
871873
llvm::LLVMSetGlobalConstant(g, True);
872874
llvm::LLVMSetLinkage(g, lib::llvm::LLVMInternalLinkage as llvm::Linkage);
@@ -909,14 +911,18 @@ fn C_shape(ccx: &@crate_ctxt, bytes: &[u8]) -> ValueRef {
909911
let llshape = C_bytes(bytes);
910912
let llglobal =
911913
llvm::LLVMAddGlobal(ccx.llmod, val_ty(llshape),
912-
str::buf(ccx.names.next("shape")));
914+
safe_sbuf(ccx, ccx.names.next("shape")));
913915
llvm::LLVMSetInitializer(llglobal, llshape);
914916
llvm::LLVMSetGlobalConstant(llglobal, True);
915917
llvm::LLVMSetLinkage(llglobal,
916918
lib::llvm::LLVMInternalLinkage as llvm::Linkage);
917919
ret llvm::LLVMConstPointerCast(llglobal, T_ptr(T_i8()));
918920
}
919921

922+
fn safe_sbuf(ccx: &@crate_ctxt, s: &str) -> str::rustrt::sbuf {
923+
cstrcache::get_cstr(ccx.cstrcache, s)
924+
}
925+
920926
//
921927
// Local Variables:
922928
// mode: rust

0 commit comments

Comments
 (0)