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
use randomx4r_sys::*;
use std::convert::TryInto;

use crate::error::*;
use crate::flags::*;

/// Dataset cache for light-mode hashing.
pub struct RandomxCache {
    pub(crate) cache: *mut randomx_cache,
}

impl RandomxCache {
    pub fn new(flags: RandomxFlags, key: &[u8]) -> Result<Self, RandomxError> {
        let cache = unsafe { randomx_alloc_cache(flags.bits()) };

        if cache.is_null() {
            return Err(RandomxError::CacheAllocError);
        }

        unsafe {
            randomx_init_cache(
                cache,
                key.as_ptr() as *const std::ffi::c_void,
                key.len().try_into().unwrap(),
            );
        }

        Ok(RandomxCache { cache })
    }
}

impl Drop for RandomxCache {
    fn drop(&mut self) {
        unsafe { randomx_release_cache(self.cache) }
    }
}

unsafe impl Send for RandomxCache {}

unsafe impl Sync for RandomxCache {}