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
// SPDX-License-Identifier: CC0-1.0

//! High level APIs to creating read/write CFrames
//!

use super::frame_ffi;
use super::frame_ffi::CFrameItem;
use std::mem;

impl CFrameItem {
    /// Allocate a new frame item with dummy values
    unsafe fn new_unchecked() -> Self {
        Self {
            edge: std::ptr::null(),
            len: 0,
        }
    }

    /// Initialize a new read frame.
    /// 'n' is the number of cells for the read frame.
    /// 'from' is a pointer to the beginning of the new slice for the array of u8 to hold the frame's cells.
    ///
    /// Note: The C implementation uses array of UWORD for `from`. UWORD maps to uint_fast16_t which
    /// maps to usize on both 32-bit and 64-bit platforms.
    ///
    /// # Safety
    ///
    /// `from` must be a valid pointer to a contiguous allocation of at least `n` usizes.
    pub unsafe fn new_read(n: usize, from: *const usize) -> Self {
        // Allocate a new vector of required size
        let mut frame = CFrameItem::new_unchecked();
        frame_ffi::c_initReadFrame(&mut frame, n, from);
        frame
    }

    /// Initialize a new write frame.
    /// 'n' is the number of cells for the write frame.
    /// 'from' is a pointer to the one-past-the-end of the new slice for the array of UWORDS to hold the frame's cells.
    ///
    /// # Safety
    ///
    /// `from` must be a valid pointer **one past the end** of a contiguous allocation
    /// of at least `n` usizes.
    pub unsafe fn new_write(n: usize, from: *mut usize) -> Self {
        // Allocate a new vector of required size
        let mut frame = CFrameItem::new_unchecked();
        frame_ffi::c_initWriteFrame(&mut frame, n, from);
        frame
    }
}

// Number of uwords required to hold n bits
pub fn round_u_word(n: usize) -> usize {
    (n + 8 * mem::size_of::<crate::ffi::UWORD>() - 1) / (8 * mem::size_of::<crate::ffi::UWORD>())
}

pub fn ffi_bytes_size(n: usize) -> usize {
    round_u_word(n) * mem::size_of::<crate::ffi::UWORD>()
}