diff --git a/rules/new/src.cpp b/rules/new/src.cpp new file mode 100644 index 000000000..ca62a01af --- /dev/null +++ b/rules/new/src.cpp @@ -0,0 +1,12 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +#include + +void *f1(std::size_t a0) { return ::operator new(a0); } + +void f2(void *a0) { return ::operator delete(a0); } + +void *f3(std::size_t a0) { return ::operator new[](a0); } + +void f4(void *a0) { return ::operator delete[](a0); } diff --git a/rules/new/tgt_refcount.rs b/rules/new/tgt_refcount.rs new file mode 100644 index 000000000..e430854b5 --- /dev/null +++ b/rules/new/tgt_refcount.rs @@ -0,0 +1,20 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f1(a0: usize) -> AnyPtr { + libcc2rs::malloc_refcount(a0) +} + +fn f2(a0: AnyPtr) { + libcc2rs::free_refcount(a0) +} + +fn f3(a0: usize) -> AnyPtr { + libcc2rs::malloc_refcount(a0) +} + +fn f4(a0: AnyPtr) { + libcc2rs::free_refcount(a0) +} diff --git a/rules/new/tgt_unsafe.rs b/rules/new/tgt_unsafe.rs new file mode 100644 index 000000000..9b86683ec --- /dev/null +++ b/rules/new/tgt_unsafe.rs @@ -0,0 +1,18 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +unsafe fn f1(a0: usize) -> *mut ::libc::c_void { + libcc2rs::malloc_unsafe(a0) +} + +unsafe fn f2(a0: *mut ::libc::c_void) { + libcc2rs::free_unsafe(a0) +} + +unsafe fn f3(a0: usize) -> *mut ::libc::c_void { + libcc2rs::malloc_unsafe(a0) +} + +unsafe fn f4(a0: *mut ::libc::c_void) { + libcc2rs::free_unsafe(a0) +} diff --git a/tests/unit/operator_new_delete.cpp b/tests/unit/operator_new_delete.cpp new file mode 100644 index 000000000..ac2a00e01 --- /dev/null +++ b/tests/unit/operator_new_delete.cpp @@ -0,0 +1,17 @@ +#include +#include + +int main() { + auto a = static_cast(::operator new(sizeof(int))); + *a = 42; + assert(*a == 42); + ::operator delete(a); + + auto arr = static_cast(::operator new[](sizeof(int) * 2)); + arr[0] = 0; + arr[1] = 1; + assert(arr[0] + arr[1] == 1); + ::operator delete[](arr); + + return 0; +} diff --git a/tests/unit/out/refcount/operator_new_delete.rs b/tests/unit/out/refcount/operator_new_delete.rs new file mode 100644 index 000000000..0170b75e6 --- /dev/null +++ b/tests/unit/out/refcount/operator_new_delete.rs @@ -0,0 +1,34 @@ +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}; +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let a: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount(::std::mem::size_of::()).reinterpret_cast::(), + )); + (*a.borrow()).write(42); + assert!((((*a.borrow()).read()) == 42)); + libcc2rs::free_refcount(((*a.borrow()).clone() as Ptr).to_any()); + let arr: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount((::std::mem::size_of::() as usize).wrapping_mul(2_usize)) + .reinterpret_cast::(), + )); + (*arr.borrow()).offset((0) as isize).write(0); + (*arr.borrow()).offset((1) as isize).write(1); + assert!( + ((((*arr.borrow()).offset((0) as isize).read()) + + ((*arr.borrow()).offset((1) as isize).read())) + == 1) + ); + libcc2rs::free_refcount(((*arr.borrow()).clone() as Ptr).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/operator_new_delete.rs b/tests/unit/out/unsafe/operator_new_delete.rs new file mode 100644 index 000000000..05006ca0f --- /dev/null +++ b/tests/unit/out/unsafe/operator_new_delete.rs @@ -0,0 +1,29 @@ +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 fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut a: *mut i32 = (libcc2rs::malloc_unsafe(::std::mem::size_of::()) as *mut i32); + (*a) = 42; + assert!(((*a) == (42))); + libcc2rs::free_unsafe((a as *mut i32 as *mut ::libc::c_void)); + let mut arr: *mut i32 = + (libcc2rs::malloc_unsafe((::std::mem::size_of::() as usize).wrapping_mul(2_usize)) + as *mut i32); + (*arr.offset((0) as isize)) = 0; + (*arr.offset((1) as isize)) = 1; + assert!((((*arr.offset((0) as isize)) + (*arr.offset((1) as isize))) == (1))); + libcc2rs::free_unsafe((arr as *mut i32 as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {}