Skip to content

Commit 51dc57c

Browse files
committed
Change main argv handling to allocate just twice
1 parent 4726559 commit 51dc57c

3 files changed

Lines changed: 38 additions & 24 deletions

File tree

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,13 +3220,21 @@ void ConverterRefCount::ConvertFunctionMain(
32203220
if (decl->getNumParams() != 0U) {
32213221
StrCat(std::format(R"(
32223222
pub fn main() {{
3223-
let argv: Vec<Value<Vec<i8>>> = ::std::env::args()
3224-
.map(|x| Rc::new(RefCell::new(x.bytes().map(|c| c as i8).collect())))
3225-
.collect();
3226-
let mut argv: Value<Vec<Ptr<i8>>> = Rc::new(RefCell::new(
3227-
argv.iter().map(|x| {{ x.borrow_mut().push(0); x.as_pointer() }}).collect(),
3223+
let size: usize = ::std::env::args().map(|arg| arg.len() + 1).sum();
3224+
let args: Value<Box<[i8]>> = Rc::new(RefCell::new(vec![0; size].into_boxed_slice()));
3225+
let args_ptr: Ptr<i8> = args.as_pointer();
3226+
let mut off = 0;
3227+
let argv: Value<Box<[Ptr<i8>]>> = Rc::new(RefCell::new(
3228+
::std::env::args()
3229+
.map(|arg| {{
3230+
let ptr = args_ptr.offset(off);
3231+
ptr.write_c_bytes(arg.as_bytes());
3232+
off += arg.len() + 1;
3233+
ptr
3234+
}})
3235+
.chain(::std::iter::once(Ptr::null()))
3236+
.collect(),
32283237
));
3229-
(*argv.borrow_mut()).push(Ptr::null());
32303238
__cpp2rust_init_globals();
32313239
::std::process::exit({}(::std::env::args().len() as i32,
32323240
argv.as_pointer()));

‎tests/unit/out/refcount/empty_main.rs‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99

1010
pub fn main() {
11-
let argv: Vec<Value<Vec<i8>>> = ::std::env::args()
12-
.map(|x| Rc::new(RefCell::new(x.bytes().map(|c| c as i8).collect())))
13-
.collect();
14-
let mut argv: Value<Vec<Ptr<i8>>> = Rc::new(RefCell::new(
15-
argv.iter()
16-
.map(|x| {
17-
x.borrow_mut().push(0);
18-
x.as_pointer()
11+
let size: usize = ::std::env::args().map(|arg| arg.len() + 1).sum();
12+
let args: Value<Box<[i8]>> = Rc::new(RefCell::new(vec![0; size].into_boxed_slice()));
13+
let args_ptr: Ptr<i8> = args.as_pointer();
14+
let mut off = 0;
15+
let argv: Value<Box<[Ptr<i8>]>> = Rc::new(RefCell::new(
16+
::std::env::args()
17+
.map(|arg| {
18+
let ptr = args_ptr.offset(off);
19+
ptr.write_c_bytes(arg.as_bytes());
20+
off += arg.len() + 1;
21+
ptr
1922
})
23+
.chain(::std::iter::once(Ptr::null()))
2024
.collect(),
2125
));
22-
(*argv.borrow_mut()).push(Ptr::null());
2326
__cpp2rust_init_globals();
2427
::std::process::exit(main_0(::std::env::args().len() as i32, argv.as_pointer()));
2528
}

‎tests/unit/out/refcount/main.rs‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99

1010
pub fn main() {
11-
let argv: Vec<Value<Vec<i8>>> = ::std::env::args()
12-
.map(|x| Rc::new(RefCell::new(x.bytes().map(|c| c as i8).collect())))
13-
.collect();
14-
let mut argv: Value<Vec<Ptr<i8>>> = Rc::new(RefCell::new(
15-
argv.iter()
16-
.map(|x| {
17-
x.borrow_mut().push(0);
18-
x.as_pointer()
11+
let size: usize = ::std::env::args().map(|arg| arg.len() + 1).sum();
12+
let args: Value<Box<[i8]>> = Rc::new(RefCell::new(vec![0; size].into_boxed_slice()));
13+
let args_ptr: Ptr<i8> = args.as_pointer();
14+
let mut off = 0;
15+
let argv: Value<Box<[Ptr<i8>]>> = Rc::new(RefCell::new(
16+
::std::env::args()
17+
.map(|arg| {
18+
let ptr = args_ptr.offset(off);
19+
ptr.write_c_bytes(arg.as_bytes());
20+
off += arg.len() + 1;
21+
ptr
1922
})
23+
.chain(::std::iter::once(Ptr::null()))
2024
.collect(),
2125
));
22-
(*argv.borrow_mut()).push(Ptr::null());
2326
__cpp2rust_init_globals();
2427
::std::process::exit(main_0(::std::env::args().len() as i32, argv.as_pointer()));
2528
}

0 commit comments

Comments
 (0)