Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,14 @@ const char *GetOverloadedOperator(const clang::FunctionDecl *decl) {
return "operator_call";
case clang::OO_Subscript:
return "operator_index";
case clang::OO_New:
return "operator_new";
case clang::OO_Delete:
return "operator_delete";
case clang::OO_Array_New:
return "operator_new_arr";
case clang::OO_Array_Delete:
return "operator_delete_arr";
default:
assert(0 && "unsupported overloaded operator");
return "";
Expand Down
71 changes: 71 additions & 0 deletions tests/std/class/class.mem/class.free.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
#include <cassert>
#include <cstddef>
#include <new>

// N4950 [class.free]/1: any allocation function for a class T is a static
// member (even if not explicitly declared static).
namespace p1_allocation_function_is_static {
int allocations = 0;

struct Allocates {
void *operator new(std::size_t size) {
++allocations;
return ::operator new(size);
}

void *operator new[](std::size_t size) {
allocations += 10;
return ::operator new[](size);
}
};

void test() {
auto one = static_cast<int *>(Allocates::operator new(sizeof(int)));
*one = 7;
assert(*one == 7);
assert(allocations == 1);
::operator delete(one);

auto many = static_cast<int *>(Allocates::operator new[](sizeof(int) * 2));
many[0] = 1;
many[1] = 2;
assert(many[0] + many[1] == 3);
assert(allocations == 11);
::operator delete[](many);
}
} // namespace p1_allocation_function_is_static

// N4950 [class.free]/3: any deallocation function for a class X is a static
// member (even if not explicitly declared static).
namespace p3_deallocation_function_is_static {
int deallocations = 0;

struct Deallocates {
void operator delete(void *ptr) {
++deallocations;
::operator delete(ptr);
}

void operator delete[](void *ptr) {
deallocations += 10;
::operator delete[](ptr);
}
};

void test() {
auto one = static_cast<int *>(::operator new(sizeof(int)));
Deallocates::operator delete(one);
assert(deallocations == 1);

auto many = static_cast<int *>(::operator new[](sizeof(int) * 2));
Deallocates::operator delete[](many);
assert(deallocations == 11);
}
} // namespace p3_deallocation_function_is_static

int main() {
p1_allocation_function_is_static::test();
p3_deallocation_function_is_static::test();
return 0;
}
85 changes: 85 additions & 0 deletions tests/std/class/class.mem/out/refcount/class.free.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
thread_local!(
pub static allocations_0: Value<i32> = Rc::new(RefCell::new(0));
);
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct p1_allocation_function_is_static_Allocates {}
impl p1_allocation_function_is_static_Allocates {
pub fn operator_new(mut size: usize) -> AnyPtr {
(*allocations_0.with(Value::clone).borrow_mut()).prefix_inc();
return libcc2rs::malloc_refcount(size);
}
pub fn operator_new_arr(mut size: usize) -> AnyPtr {
allocations_0.with(|rc| *rc.borrow_mut() += 10);
return libcc2rs::malloc_refcount(size);
}
}
pub fn test_1() {
let mut one: Ptr<i32> = ({
p1_allocation_function_is_static_Allocates::operator_new(::std::mem::size_of::<i32>())
})
.reinterpret_cast::<i32>();
one.write(7);
assert!(((one.read()) == 7));
assert!((allocations_0.with(|rc| *rc.borrow()) == 1));
libcc2rs::free_refcount((one).to_any());
let mut many: Ptr<i32> = ({
p1_allocation_function_is_static_Allocates::operator_new_arr(
(::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize),
)
})
.reinterpret_cast::<i32>();
elem!(many, 0).write(1);
elem!(many, 1).write(2);
assert!((((elem!(many, 0).read()) + (elem!(many, 1).read())) == 3));
assert!((allocations_0.with(|rc| *rc.borrow()) == 11));
libcc2rs::free_refcount((many).to_any());
}
thread_local!(
pub static deallocations_2: Value<i32> = Rc::new(RefCell::new(0));
);
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct p3_deallocation_function_is_static_Deallocates {}
impl p3_deallocation_function_is_static_Deallocates {
pub fn operator_delete(mut ptr: AnyPtr) {
(*deallocations_2.with(Value::clone).borrow_mut()).prefix_inc();
libcc2rs::free_refcount((ptr).clone());
}
pub fn operator_delete_arr(mut ptr: AnyPtr) {
deallocations_2.with(|rc| *rc.borrow_mut() += 10);
libcc2rs::free_refcount((ptr).clone());
}
}
pub fn test_3() {
let mut one: Ptr<i32> =
libcc2rs::malloc_refcount(::std::mem::size_of::<i32>()).reinterpret_cast::<i32>();
({ p3_deallocation_function_is_static_Deallocates::operator_delete((one).to_any()) });
assert!((deallocations_2.with(|rc| *rc.borrow()) == 1));
let mut many: Ptr<i32> =
libcc2rs::malloc_refcount((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize))
.reinterpret_cast::<i32>();
({ p3_deallocation_function_is_static_Deallocates::operator_delete_arr((many).to_any()) });
assert!((deallocations_2.with(|rc| *rc.borrow()) == 11));
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ test_1() });
({ test_3() });
return 0;
}
pub fn __cpp2rust_init_globals() {
let _ = allocations_0.with(|_| ());
let _ = deallocations_2.with(|_| ());
}
89 changes: 89 additions & 0 deletions tests/std/class/class.mem/out/unsafe/class.free.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub static mut allocations_0: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 0 });
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct p1_allocation_function_is_static_Allocates {}
impl p1_allocation_function_is_static_Allocates {
pub unsafe fn operator_new(mut size: usize) -> *mut ::libc::c_void {
(*std::cell::LazyCell::force_mut(&mut *&raw mut allocations_0)).prefix_inc();
return libcc2rs::malloc_unsafe(size);
}
pub unsafe fn operator_new_arr(mut size: usize) -> *mut ::libc::c_void {
(*std::cell::LazyCell::force_mut(&mut *&raw mut allocations_0)) += 10;
return libcc2rs::malloc_unsafe(size);
}
}
pub unsafe fn test_1() {
let mut one: *mut i32 = ((unsafe {
p1_allocation_function_is_static_Allocates::operator_new(::std::mem::size_of::<i32>())
}) as *mut i32);
(*one) = 7;
assert!(((*one) == (7)));
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut allocations_0)) == (1)));
libcc2rs::free_unsafe((one as *mut ::libc::c_void));
let mut many: *mut i32 = ((unsafe {
p1_allocation_function_is_static_Allocates::operator_new_arr(
(::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize),
)
}) as *mut i32);
(*many.offset((0) as isize)) = 1;
(*many.offset((1) as isize)) = 2;
assert!((((*many.offset((0) as isize)) + (*many.offset((1) as isize))) == (3)));
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut allocations_0)) == (11)));
libcc2rs::free_unsafe((many as *mut ::libc::c_void));
}
pub static mut deallocations_2: std::cell::LazyCell<i32> =
std::cell::LazyCell::new(|| unsafe { 0 });
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct p3_deallocation_function_is_static_Deallocates {}
impl p3_deallocation_function_is_static_Deallocates {
pub unsafe fn operator_delete(mut ptr: *mut ::libc::c_void) {
(*std::cell::LazyCell::force_mut(&mut *&raw mut deallocations_2)).prefix_inc();
libcc2rs::free_unsafe(ptr);
}
pub unsafe fn operator_delete_arr(mut ptr: *mut ::libc::c_void) {
(*std::cell::LazyCell::force_mut(&mut *&raw mut deallocations_2)) += 10;
libcc2rs::free_unsafe(ptr);
}
}
pub unsafe fn test_3() {
let mut one: *mut i32 = (libcc2rs::malloc_unsafe(::std::mem::size_of::<i32>()) as *mut i32);
(unsafe {
p3_deallocation_function_is_static_Deallocates::operator_delete(
(one as *mut ::libc::c_void),
)
});
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut deallocations_2)) == (1)));
let mut many: *mut i32 =
(libcc2rs::malloc_unsafe((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize))
as *mut i32);
(unsafe {
p3_deallocation_function_is_static_Deallocates::operator_delete_arr(
(many as *mut ::libc::c_void),
)
});
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut deallocations_2)) == (11)));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
(unsafe { test_1() });
(unsafe { test_3() });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {
std::cell::LazyCell::force(&*&raw const allocations_0);
std::cell::LazyCell::force(&*&raw const deallocations_2);
}
Loading