randomx_rust_wrapper/
dataset.rs1use std::sync::Arc;
18
19use crate::bindings::dataset::*;
20use crate::cache::{Cache, CacheRawAPI};
21use crate::errors::RandomXError::DatasetAllocationError;
22use crate::flags::RandomXFlags;
23use crate::try_alloc;
24use crate::RResult;
25
26#[derive(Debug)]
27pub struct Dataset {
28 inner: Arc<DatasetInner>,
29}
30
31#[derive(Debug)]
32struct DatasetInner {
33 dataset: *mut randomx_dataset,
34}
35
36unsafe impl Send for DatasetInner {}
37unsafe impl Sync for DatasetInner {}
38
39#[derive(Clone, Debug)]
42pub struct DatasetHandle {
43 inner: Arc<DatasetInner>,
44}
45
46impl Dataset {
47 pub fn new(global_nonce: &[u8], flags: RandomXFlags) -> RResult<Self> {
51 let cache = Cache::new(global_nonce, flags)?;
52 Self::from_cache(&cache, flags.contains(RandomXFlags::LARGE_PAGES))
53 }
54
55 pub fn from_cache(cache: &Cache, large_pages_enabled: bool) -> RResult<Self> {
58 let mut dataset = Self::allocate(large_pages_enabled)?;
59 let items_count = dataset.items_count();
60 dataset.initialize(cache, 0, items_count);
61
62 Ok(dataset)
63 }
64
65 pub fn allocate(large_pages_enabled: bool) -> RResult<Self> {
67 let flags = if large_pages_enabled {
68 RandomXFlags::LARGE_PAGES
69 } else {
70 RandomXFlags::default()
71 };
72
73 let dataset =
74 try_alloc! { randomx_alloc_dataset(flags.bits()), DatasetAllocationError { flags } };
75 let dataset_inner = DatasetInner { dataset };
76 let dataset = Self {
77 inner: Arc::new(dataset_inner),
78 };
79 Ok(dataset)
80 }
81
82 pub fn items_count(&self) -> u64 {
84 unsafe { randomx_dataset_item_count() }
85 }
86
87 pub fn initialize(&mut self, cache: &impl CacheRawAPI, start_item: u64, items_count: u64) {
89 unsafe { randomx_init_dataset(self.raw(), cache.raw(), start_item, items_count) };
90 }
91
92 pub fn handle(&self) -> DatasetHandle {
93 DatasetHandle {
94 inner: self.inner.clone(),
95 }
96 }
97
98 pub(crate) fn raw(&self) -> *mut randomx_dataset {
99 self.inner.dataset
100 }
101}
102
103impl DatasetHandle {
104 pub fn items_count(&self) -> u64 {
106 unsafe { randomx_dataset_item_count() }
107 }
108
109 pub fn initialize(&mut self, cache: &impl CacheRawAPI, start_item: u64, items_count: u64) {
111 unsafe { randomx_init_dataset(self.raw(), cache.raw(), start_item, items_count) };
112 }
113
114 pub(crate) fn raw(&self) -> *mut randomx_dataset {
115 self.inner.dataset
116 }
117}
118
119impl Drop for DatasetInner {
120 fn drop(&mut self) {
121 unsafe { randomx_release_dataset(self.dataset) }
122 }
123}
124
125pub trait DatasetRawAPI {
126 fn raw(&self) -> *mut randomx_dataset;
127}
128
129impl DatasetRawAPI for Dataset {
130 fn raw(&self) -> *mut randomx_dataset {
131 self.raw()
132 }
133}
134
135impl DatasetRawAPI for DatasetHandle {
136 fn raw(&self) -> *mut randomx_dataset {
137 self.raw()
138 }
139}