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
12 changes: 12 additions & 0 deletions rules/new/src.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <new>

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); }
20 changes: 20 additions & 0 deletions rules/new/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 18 additions & 0 deletions rules/new/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 17 additions & 0 deletions tests/unit/operator_new_delete.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <cassert>
#include <new>

int main() {
auto a = static_cast<int *>(::operator new(sizeof(int)));
*a = 42;
assert(*a == 42);
::operator delete(a);

auto arr = static_cast<int *>(::operator new[](sizeof(int) * 2));
arr[0] = 0;
arr[1] = 1;
assert(arr[0] + arr[1] == 1);
::operator delete[](arr);

return 0;
}
34 changes: 34 additions & 0 deletions tests/unit/out/refcount/operator_new_delete.rs
Original file line number Diff line number Diff line change
@@ -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<Ptr<i32>> = Rc::new(RefCell::new(
libcc2rs::malloc_refcount(::std::mem::size_of::<i32>()).reinterpret_cast::<i32>(),
));
(*a.borrow()).write(42);
assert!((((*a.borrow()).read()) == 42));
libcc2rs::free_refcount(((*a.borrow()).clone() as Ptr<i32>).to_any());
let arr: Value<Ptr<i32>> = Rc::new(RefCell::new(
libcc2rs::malloc_refcount((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize))
.reinterpret_cast::<i32>(),
));
(*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<i32>).to_any());
return 0;
}
pub fn __cpp2rust_init_globals() {}
29 changes: 29 additions & 0 deletions tests/unit/out/unsafe/operator_new_delete.rs
Original file line number Diff line number Diff line change
@@ -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::<i32>()) 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::<i32>() 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() {}
Loading