Skip to content

Commit 4e136d1

Browse files
committed
Add rustc::middle::cstrcache for getting c string bufs safely
We continue to leak string buffers in trans so this creates a way to get c string buffers from strings while guaranteeing that they are not freed before use. Hopefully this can be made efficient in the istr regime.
1 parent 3aef2c1 commit 4e136d1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/comp/middle/cstrcache.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Provides a safe way to get C pointers to strings, because in many
2+
// places LLVM wants a string but doesn't take a copy.
3+
4+
import std::str;
5+
6+
export t;
7+
export mk;
8+
export get_cstr;
9+
10+
type t_ = @{
11+
// This string is boxed so that I remember that it has to be boxed
12+
// after the ivec conversion.
13+
mutable cache: [@str]
14+
};
15+
16+
tag t {
17+
private(t_);
18+
}
19+
20+
fn mk() -> t {
21+
ret private(@{mutable cache: []});
22+
}
23+
24+
fn get_cstr(t: &t, s: &str) -> str::rustrt::sbuf {
25+
let boxed = @s;
26+
let buf = str::buf(*boxed);
27+
(*t).cache += [boxed];
28+
ret buf;
29+
}

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod middle {
3030
mod freevars;
3131
mod shape;
3232
mod gc;
33+
mod cstrcache;
3334

3435
mod tstate {
3536
mod ck;

0 commit comments

Comments
 (0)