Skip to content

Commit c8becea

Browse files
Destroy heap array elements in reverse order (#684)
Heap `delete[]` calls nontrivial element destructors in forward order in both output models, so later elements can be torn down after objects they depend on. The generated loops now run from the last index to the first, before releasing the allocation. --- Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás Senart](https://github.com/tsenart). --------- Signed-off-by: Perfloop Agent <agent@perfloop.ai>
1 parent b52465f commit c8becea

5 files changed

Lines changed: 146 additions & 5 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4161,10 +4161,11 @@ bool Converter::VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) {
41614161
}
41624162
auto record_name = GetRecordName(destroyed_type->getAsCXXRecordDecl());
41634163
if (expr->isArrayForm()) {
4164-
StrCat(std::format("for __i in 0..libcc2rs::malloc_usable_size(__p as *mut "
4165-
"::libc::c_void) / ::std::mem::size_of::<{0}>() {{ "
4166-
"{0}::{1}(&mut *__p.add(__i)); }}",
4167-
record_name, kDestructorName));
4164+
StrCat(
4165+
std::format("for __i in (0..libcc2rs::malloc_usable_size(__p as *mut "
4166+
"::libc::c_void) / ::std::mem::size_of::<{0}>()).rev() {{ "
4167+
"{0}::{1}(&mut *__p.add(__i)); }}",
4168+
record_name, kDestructorName));
41684169
} else {
41694170
StrCat(std::format("{}::{}(&mut *__p)", record_name, kDestructorName),
41704171
token::kSemiColon);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ bool ConverterRefCount::VisitCXXDeleteExpr(clang::CXXDeleteExpr *expr) {
20252025
StrCat(keyword::kLet, "__p", token::kAssign, ToString(expr->getArgument()),
20262026
token::kSemiColon);
20272027
if (expr->isArrayForm()) {
2028-
StrCat(std::format("for __i in 0..__p.len() {{ __p.offset(__i as "
2028+
StrCat(std::format("for __i in (0..__p.len()).rev() {{ __p.offset(__i as "
20292029
"isize).{}(); }}",
20302030
kDestructorName));
20312031
} else {

‎tests/unit/delete_array_order.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <cstdio>
2+
3+
struct Element {
4+
long id;
5+
long value;
6+
~Element() { printf("%ld:%ld ", id, value); }
7+
};
8+
9+
int main() {
10+
Element *a = new Element[3]{{1, 10}, {2, 20}, {3, 30}};
11+
delete[] a;
12+
printf("\n");
13+
14+
return 0;
15+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)]
10+
#[byte_size(16)]
11+
pub struct Element {
12+
#[offset(0)]
13+
pub id: i64,
14+
#[offset(8)]
15+
pub value: i64,
16+
}
17+
pub fn main() {
18+
__cpp2rust_init_globals();
19+
libcc2rs::cpp2rust_exit(main_0());
20+
}
21+
fn main_0() -> i32 {
22+
let mut a: Ptr<Element> = Ptr::alloc_array(Box::new([
23+
Element {
24+
id: 1_i64,
25+
value: 10_i64,
26+
},
27+
Element {
28+
id: 2_i64,
29+
value: 20_i64,
30+
},
31+
Element {
32+
id: 3_i64,
33+
value: 30_i64,
34+
},
35+
]));
36+
{
37+
let __p = a;
38+
for __i in (0..__p.len()).rev() {
39+
__p.offset(__i as isize).destructor();
40+
}
41+
__p.delete();
42+
};
43+
printf!(b"\n");
44+
return 0;
45+
}
46+
pub trait ElementImpl {
47+
fn destructor(&self);
48+
}
49+
impl ElementImpl for Ptr<Element> {
50+
fn destructor(&self) {
51+
printf!(
52+
b"%ld:%ld ",
53+
(*self).with(|__s| __s.id),
54+
(*self).with(|__s| __s.value)
55+
);
56+
}
57+
}
58+
pub fn __cpp2rust_init_globals() {}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
#[repr(C)]
10+
#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)]
11+
pub struct Element {
12+
pub id: i64,
13+
pub value: i64,
14+
}
15+
impl Element {
16+
pub unsafe fn destructor(&mut self) {
17+
(unsafe {
18+
libc::printf(
19+
c"%ld:%ld ".as_ptr() as *const libc::c_char,
20+
(self.id),
21+
(self.value),
22+
)
23+
});
24+
}
25+
}
26+
pub fn main() {
27+
unsafe {
28+
__cpp2rust_init_globals();
29+
libcc2rs::cpp2rust_exit(main_0() as i32);
30+
}
31+
}
32+
unsafe fn main_0() -> i32 {
33+
let mut a: *mut Element = Box::leak(Box::new([
34+
Element {
35+
id: 1_i64,
36+
value: 10_i64,
37+
},
38+
Element {
39+
id: 2_i64,
40+
value: 20_i64,
41+
},
42+
Element {
43+
id: 3_i64,
44+
value: 30_i64,
45+
},
46+
]))
47+
.as_mut_ptr();
48+
{
49+
let __p = a;
50+
if !__p.is_null() {
51+
for __i in (0..libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void)
52+
/ ::std::mem::size_of::<Element>())
53+
.rev()
54+
{
55+
Element::destructor(&mut *__p.add(__i));
56+
}
57+
::std::mem::drop(Box::from_raw(::std::slice::from_raw_parts_mut(
58+
__p,
59+
libcc2rs::malloc_usable_size(__p as *mut ::libc::c_void)
60+
/ ::std::mem::size_of::<Element>(),
61+
)))
62+
}
63+
};
64+
(unsafe { libc::printf(c"\n".as_ptr() as *const libc::c_char) });
65+
return 0;
66+
}
67+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)