Skip to content

Commit 2427859

Browse files
committed
Add UnionStore
1 parent 5b56999 commit 2427859

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

libcc2rs/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ pub use reinterpret::ByteRepr;
77
mod rc;
88
pub use rc::*;
99

10+
mod union;
11+
pub use union::*;
12+
1013
mod fn_ptr;
1114
pub use fn_ptr::FnPtr;
1215

libcc2rs/src/rc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,15 @@ impl<T: ?Sized> AsPointerDyn<T> for Rc<RefCell<T>> {
12461246

12471247
impl<T: 'static> ByteRepr for Ptr<T> {}
12481248

1249+
impl<T> Ptr<T> {
1250+
pub(crate) fn reinterpreted(alloc: Rc<dyn OriginalAlloc>, byte_offset: usize) -> Self {
1251+
Ptr {
1252+
offset: byte_offset,
1253+
kind: PtrKind::Reinterpreted(alloc),
1254+
}
1255+
}
1256+
}
1257+
12491258
#[cfg(test)]
12501259
mod tests {
12511260
use super::*;

libcc2rs/src/union.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use std::{cell::RefCell, rc::Rc};
5+
6+
use crate::Ptr;
7+
use crate::reinterpret::{ByteRepr, OriginalAlloc, SliceOriginalAlloc};
8+
9+
pub struct UnionStore {
10+
bytes: Rc<RefCell<Vec<u8>>>,
11+
}
12+
13+
impl UnionStore {
14+
pub fn new(size: usize) -> Self {
15+
UnionStore {
16+
bytes: Rc::new(RefCell::new(vec![0u8; size])),
17+
}
18+
}
19+
20+
pub fn pod<T: ByteRepr>(&self, offset: usize) -> Ptr<T> {
21+
let alloc: Rc<dyn OriginalAlloc> = Rc::new(SliceOriginalAlloc {
22+
weak: Rc::downgrade(&self.bytes),
23+
});
24+
Ptr::reinterpreted(alloc, offset)
25+
}
26+
}
27+
28+
impl Clone for UnionStore {
29+
fn clone(&self) -> Self {
30+
UnionStore {
31+
bytes: Rc::new(RefCell::new(self.bytes.borrow().clone())),
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)