mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-03 17:48:43 +00:00
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.
This commit is contained in:
29
src/comp/middle/cstrcache.rs
Normal file
29
src/comp/middle/cstrcache.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
// Provides a safe way to get C pointers to strings, because in many
|
||||
// places LLVM wants a string but doesn't take a copy.
|
||||
|
||||
import std::str;
|
||||
|
||||
export t;
|
||||
export mk;
|
||||
export get_cstr;
|
||||
|
||||
type t_ = @{
|
||||
// This string is boxed so that I remember that it has to be boxed
|
||||
// after the ivec conversion.
|
||||
mutable cache: [@str]
|
||||
};
|
||||
|
||||
tag t {
|
||||
private(t_);
|
||||
}
|
||||
|
||||
fn mk() -> t {
|
||||
ret private(@{mutable cache: []});
|
||||
}
|
||||
|
||||
fn get_cstr(t: &t, s: &str) -> str::rustrt::sbuf {
|
||||
let boxed = @s;
|
||||
let buf = str::buf(*boxed);
|
||||
(*t).cache += [boxed];
|
||||
ret buf;
|
||||
}
|
||||
@@ -30,6 +30,7 @@ mod middle {
|
||||
mod freevars;
|
||||
mod shape;
|
||||
mod gc;
|
||||
mod cstrcache;
|
||||
|
||||
mod tstate {
|
||||
mod ck;
|
||||
|
||||
Reference in New Issue
Block a user