1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::update_counts;
use std::{
    mem::{size_of, ManuallyDrop},
    rc::Rc,
    usize,
};

#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn __quantum__rt__tuple_create(size: u64) -> *mut *const Vec<u8> {
    let mut mem = vec![
        0_u8;
        <usize as std::convert::TryFrom<u64>>::try_from(size)
            .expect("Tuple size too large for `usize` type on this platform.")
            + size_of::<*const Vec<u8>>()
    ];

    unsafe {
        let header = mem.as_mut_ptr().cast::<*const Vec<u8>>();
        *header = Rc::into_raw(Rc::new(mem));
        header.wrapping_add(1)
    }
}

#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn __quantum__rt__tuple_copy(
    raw_tup: *mut *const Vec<u8>,
    force: bool,
) -> *mut *const Vec<u8> {
    let rc = ManuallyDrop::new(Rc::from_raw(*(raw_tup).wrapping_sub(1)));
    if force || Rc::weak_count(&rc) > 0 {
        let mut copy = rc.as_ref().clone();
        let header = copy.as_mut_ptr().cast::<*const Vec<u8>>();
        *header = Rc::into_raw(Rc::new(copy));
        header.wrapping_add(1)
    } else {
        Rc::into_raw(Rc::clone(&rc));
        raw_tup
    }
}

#[no_mangle]
pub unsafe extern "C" fn __quantum__rt__tuple_update_reference_count(
    raw_tup: *mut *const Vec<u8>,
    update: i32,
) {
    update_counts(*raw_tup.wrapping_sub(1), update, false);
}

#[no_mangle]
pub unsafe extern "C" fn __quantum__rt__tuple_update_alias_count(
    raw_tup: *mut *const Vec<u8>,
    update: i32,
) {
    update_counts(*raw_tup.wrapping_sub(1), update, true);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tuple_create() {
        let tup = __quantum__rt__tuple_create(size_of::<u32>() as u64);
        unsafe {
            *tup.cast::<u32>() = 42;
            __quantum__rt__tuple_update_reference_count(tup, -1);
        }
    }

    #[test]
    fn test_tuple_update_reference_count() {
        let tup = __quantum__rt__tuple_create(size_of::<u32>() as u64);
        unsafe {
            let rc = ManuallyDrop::new(Rc::from_raw(*tup.cast::<*const Vec<u8>>().wrapping_sub(1)));
            assert_eq!(Rc::strong_count(&rc), 1);
            __quantum__rt__tuple_update_reference_count(tup, 2);
            assert_eq!(Rc::strong_count(&rc), 3);
            __quantum__rt__tuple_update_reference_count(tup, -2);
            assert_eq!(Rc::strong_count(&rc), 1);
            __quantum__rt__tuple_update_reference_count(tup, -1);
        }
    }

    #[test]
    fn test_tuple_update_alias_count() {
        let tup = __quantum__rt__tuple_create(size_of::<u32>() as u64);
        unsafe {
            let rc = ManuallyDrop::new(Rc::from_raw(*tup.cast::<*const Vec<u8>>().wrapping_sub(1)));
            assert_eq!(Rc::strong_count(&rc), 1);
            assert_eq!(Rc::weak_count(&rc), 0);
            __quantum__rt__tuple_update_alias_count(tup, 2);
            assert_eq!(Rc::weak_count(&rc), 2);
            __quantum__rt__tuple_update_alias_count(tup, -2);
            assert_eq!(Rc::weak_count(&rc), 0);
            __quantum__rt__tuple_update_reference_count(tup, -1);
        }
    }

    #[test]
    fn test_tuple_copy() {
        let tup1 = __quantum__rt__tuple_create(size_of::<u32>() as u64);
        unsafe {
            *tup1.cast::<u32>() = 42;
            let tup2 = __quantum__rt__tuple_copy(tup1, false);
            assert_eq!(tup2, tup1);
            assert_eq!(*tup2.cast::<u32>(), 42);
            __quantum__rt__tuple_update_reference_count(tup2, -1);
            assert_eq!(*tup1.cast::<u32>(), 42);
            let tup3 = __quantum__rt__tuple_copy(tup1, true);
            assert_ne!(tup3, tup1);
            assert_eq!(*tup3.cast::<u32>(), 42);
            __quantum__rt__tuple_update_reference_count(tup3, -1);
            assert_eq!(*tup1.cast::<u32>(), 42);
            __quantum__rt__tuple_update_alias_count(tup1, 1);
            let tup4 = __quantum__rt__tuple_copy(tup1, false);
            assert_ne!(tup4, tup1);
            assert_eq!(*tup4.cast::<u32>(), 42);
            __quantum__rt__tuple_update_reference_count(tup4, -1);
            assert_eq!(*tup1.cast::<u32>(), 42);
            __quantum__rt__tuple_update_alias_count(tup1, -1);
            __quantum__rt__tuple_update_reference_count(tup1, -1);
        }
    }
}