Skip to content

Commit 9a96361

Browse files
committed
Delete elem_byte_size
1 parent 6c74dba commit 9a96361

4 files changed

Lines changed: 79 additions & 71 deletions

File tree

libcc2rs/src/rc.rs

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ use crate::reinterpret::{ByteRepr, OriginalAlloc, SingleOriginalAlloc, SliceOrig
1616

1717
pub type Value<T> = Rc<RefCell<T>>;
1818

19-
struct ReinterpretedView {
20-
// Pointer to the source of reinterpret
21-
alloc: Rc<dyn OriginalAlloc>,
22-
// C++ size of the reinterpreted view
23-
elem_byte_size: usize,
24-
}
25-
2619
#[derive(Default)]
2720
enum PtrKind<T> {
2821
#[default]
@@ -32,7 +25,7 @@ enum PtrKind<T> {
3225
HeapSingle(Weak<RefCell<T>>),
3326
HeapArray(Weak<RefCell<Box<[T]>>>),
3427
Vec(Weak<RefCell<Vec<T>>>),
35-
Reinterpreted(Rc<ReinterpretedView>),
28+
Reinterpreted(Rc<dyn OriginalAlloc>),
3629
}
3730

3831
pub enum StrongPtr<T> {
@@ -84,8 +77,8 @@ impl<T> fmt::Debug for PtrKind<T> {
8477
PtrKind::HeapSingle(w) => write!(f, "HeapSingle({:?})", w.as_ptr()),
8578
PtrKind::StackArray(w) => write!(f, "StackArray({:?})", w.as_ptr()),
8679
PtrKind::HeapArray(w) => write!(f, "HeapArray({:?})", w.as_ptr()),
87-
PtrKind::Reinterpreted(view) => {
88-
write!(f, "Reinterpreted(0x{:x})", view.alloc.address())
80+
PtrKind::Reinterpreted(alloc) => {
81+
write!(f, "Reinterpreted(0x{:x})", alloc.address())
8982
}
9083
}
9184
}
@@ -100,7 +93,7 @@ impl<T> Clone for PtrKind<T> {
10093
PtrKind::HeapSingle(weak) => PtrKind::HeapSingle(weak.clone()),
10194
PtrKind::StackArray(weak) => PtrKind::StackArray(weak.clone()),
10295
PtrKind::HeapArray(weak) => PtrKind::HeapArray(weak.clone()),
103-
PtrKind::Reinterpreted(view) => PtrKind::Reinterpreted(Rc::clone(view)),
96+
PtrKind::Reinterpreted(alloc) => PtrKind::Reinterpreted(Rc::clone(alloc)),
10497
}
10598
}
10699
}
@@ -112,7 +105,7 @@ impl<T> PtrKind<T> {
112105
PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => w.as_ptr() as usize,
113106
PtrKind::Vec(w) => w.as_ptr() as usize,
114107
PtrKind::StackArray(w) | PtrKind::HeapArray(w) => w.as_ptr() as usize,
115-
PtrKind::Reinterpreted(view) => view.alloc.address(),
108+
PtrKind::Reinterpreted(alloc) => alloc.address(),
116109
}
117110
}
118111
}
@@ -256,23 +249,29 @@ impl<T> Ptr<T> {
256249
// Ptr::offset is in elements (step = 1). This helper converts between
257250
// user-facing element counts and the internal offset units.
258251
#[inline]
259-
fn elem_step(&self) -> usize {
252+
fn elem_step(&self) -> usize
253+
where
254+
T: ByteRepr,
255+
{
260256
match &self.kind {
261-
PtrKind::Reinterpreted(view) => view.elem_byte_size,
257+
PtrKind::Reinterpreted(_) => T::byte_size(),
262258
_ => 1,
263259
}
264260
}
265261

266262
#[inline]
267-
pub fn len(&self) -> usize {
263+
pub fn len(&self) -> usize
264+
where
265+
T: ByteRepr,
266+
{
268267
match &self.kind {
269268
PtrKind::Null => 0,
270269
PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => 1,
271270
PtrKind::Vec(weak) => weak.upgrade().expect("ub: dangling pointer").borrow().len(),
272271
PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => {
273272
weak.upgrade().expect("ub: dangling pointer").borrow().len()
274273
}
275-
PtrKind::Reinterpreted(view) => view.alloc.total_byte_len() / view.elem_byte_size,
274+
PtrKind::Reinterpreted(alloc) => alloc.total_byte_len() / T::byte_size(),
276275
}
277276
}
278277

@@ -291,12 +290,15 @@ impl<T> Ptr<T> {
291290
.expect("ub: dangling pointer")
292291
.borrow()
293292
.is_empty(),
294-
PtrKind::Reinterpreted(view) => self.offset >= view.alloc.total_byte_len(),
293+
PtrKind::Reinterpreted(alloc) => self.offset >= alloc.total_byte_len(),
295294
}
296295
}
297296

298297
#[inline]
299-
pub fn offset(&self, offset: isize) -> Self {
298+
pub fn offset(&self, offset: isize) -> Self
299+
where
300+
T: ByteRepr,
301+
{
300302
let step = self.elem_step();
301303
Self {
302304
kind: self.kind.clone(),
@@ -307,20 +309,29 @@ impl<T> Ptr<T> {
307309
}
308310

309311
#[inline]
310-
pub fn get_offset(&self) -> usize {
312+
pub fn get_offset(&self) -> usize
313+
where
314+
T: ByteRepr,
315+
{
311316
self.offset / self.elem_step()
312317
}
313318

314319
#[inline]
315-
pub fn to_last(&self) -> Self {
320+
pub fn to_last(&self) -> Self
321+
where
322+
T: ByteRepr,
323+
{
316324
Self {
317325
kind: self.kind.clone(),
318326
offset: self.len().wrapping_sub(1).wrapping_mul(self.elem_step()),
319327
}
320328
}
321329

322330
#[inline]
323-
pub fn to_end(&self) -> Self {
331+
pub fn to_end(&self) -> Self
332+
where
333+
T: ByteRepr,
334+
{
324335
Self {
325336
kind: self.kind.clone(),
326337
offset: self.len().wrapping_mul(self.elem_step()),
@@ -346,8 +357,8 @@ impl<T> Ptr<T> {
346357
rc: weak.upgrade().expect("ub: dangling pointer"),
347358
offset: self.offset,
348359
},
349-
PtrKind::Reinterpreted(view) => StrongPtr::Reinterpreted {
350-
alloc: Rc::clone(&view.alloc),
360+
PtrKind::Reinterpreted(alloc) => StrongPtr::Reinterpreted {
361+
alloc: Rc::clone(alloc),
351362
byte_offset: self.offset,
352363
cell: RefCell::new(None),
353364
},
@@ -372,10 +383,10 @@ impl<T> Ptr<T> {
372383
let rc = weak.upgrade().expect("ub: dangling pointer");
373384
rc.borrow_mut()[self.offset] = value;
374385
}
375-
PtrKind::Reinterpreted(view) => {
386+
PtrKind::Reinterpreted(alloc) => {
376387
let mut buf = vec![0u8; T::byte_size()];
377388
value.to_bytes(&mut buf);
378-
view.alloc.write_bytes(self.offset, &buf);
389+
alloc.write_bytes(self.offset, &buf);
379390
}
380391
}
381392
}
@@ -417,15 +428,12 @@ impl<T> Ptr<T> {
417428
Rc::new(SliceOriginalAlloc { weak: weak.clone() }),
418429
src_byte_off,
419430
),
420-
PtrKind::Reinterpreted(view) => (Rc::clone(&view.alloc), self.offset),
431+
PtrKind::Reinterpreted(alloc) => (Rc::clone(alloc), self.offset),
421432
};
422433

423434
Ptr {
424435
offset: abs_byte_off,
425-
kind: PtrKind::Reinterpreted(Rc::new(ReinterpretedView {
426-
alloc,
427-
elem_byte_size: U::byte_size(),
428-
})),
436+
kind: PtrKind::Reinterpreted(alloc),
429437
}
430438
}
431439
}
@@ -452,13 +460,13 @@ impl<T> Ptr<T> {
452460
let mut borrow = rc.borrow_mut();
453461
f(&mut borrow[self.offset])
454462
}
455-
PtrKind::Reinterpreted(view) => {
463+
PtrKind::Reinterpreted(alloc) => {
456464
let mut buf = vec![0u8; T::byte_size()];
457-
view.alloc.read_bytes(self.offset, &mut buf);
465+
alloc.read_bytes(self.offset, &mut buf);
458466
let mut val = T::from_bytes(&buf);
459467
let ret = f(&mut val);
460468
val.to_bytes(&mut buf);
461-
view.alloc.write_bytes(self.offset, &buf);
469+
alloc.write_bytes(self.offset, &buf);
462470
ret
463471
}
464472
}
@@ -485,9 +493,9 @@ impl<T> Ptr<T> {
485493
let borrow = rc.borrow();
486494
f(&borrow[self.offset])
487495
}
488-
PtrKind::Reinterpreted(view) => {
496+
PtrKind::Reinterpreted(alloc) => {
489497
let mut buf = vec![0u8; T::byte_size()];
490-
view.alloc.read_bytes(self.offset, &mut buf);
498+
alloc.read_bytes(self.offset, &mut buf);
491499
let val = T::from_bytes(&buf);
492500
f(&val)
493501
}
@@ -512,9 +520,9 @@ impl<T: Clone + ByteRepr> Ptr<T> {
512520
PtrKind::StackArray(ref weak) | PtrKind::HeapArray(ref weak) => {
513521
weak.upgrade().expect("ub: dangling pointer").borrow()[self.offset].clone()
514522
}
515-
PtrKind::Reinterpreted(ref view) => {
523+
PtrKind::Reinterpreted(ref alloc) => {
516524
let mut buf = vec![0u8; T::byte_size()];
517-
view.alloc.read_bytes(self.offset, &mut buf);
525+
alloc.read_bytes(self.offset, &mut buf);
518526
T::from_bytes(&buf)
519527
}
520528
}
@@ -531,7 +539,7 @@ impl<T: std::io::Write + ByteRepr> Ptr<T> {
531539
}
532540
}
533541

534-
impl<T: std::cmp::Ord> Ptr<T> {
542+
impl<T: std::cmp::Ord + ByteRepr> Ptr<T> {
535543
pub fn sort(&self, last: usize) {
536544
match self.kind {
537545
PtrKind::Null => panic!("ub: dereference of null pointer"),
@@ -553,7 +561,7 @@ impl<T: std::cmp::Ord> Ptr<T> {
553561
}
554562
}
555563

556-
impl<T: Clone> Ptr<T> {
564+
impl<T: Clone + ByteRepr> Ptr<T> {
557565
pub fn sort_with_cmp<F>(&self, last: usize, mut cmp: F)
558566
where
559567
F: FnMut(Ptr<T>, Ptr<T>) -> bool,
@@ -600,7 +608,7 @@ impl<T: Clone> Ptr<T> {
600608

601609
impl<T> IntoIterator for &Ptr<T>
602610
where
603-
T: Clone,
611+
T: Clone + ByteRepr,
604612
{
605613
type Item = Ptr<T>;
606614
type IntoIter = Ptr<T>;
@@ -610,7 +618,7 @@ where
610618
}
611619
}
612620

613-
impl<T> Iterator for Ptr<T> {
621+
impl<T: ByteRepr> Iterator for Ptr<T> {
614622
type Item = Ptr<T>;
615623
fn next(&mut self) -> Option<Self::Item> {
616624
if self.get_offset() < self.len() {
@@ -664,7 +672,7 @@ pub struct StringIterator<T> {
664672
ptr: Ptr<T>,
665673
}
666674

667-
impl<T> Iterator for StringIterator<T> {
675+
impl<T: ByteRepr> Iterator for StringIterator<T> {
668676
type Item = Ptr<T>;
669677
fn next(&mut self) -> Option<Self::Item> {
670678
// stop before the null terminator at the last position
@@ -696,7 +704,7 @@ impl Iterator for CStringIterator {
696704
}
697705
}
698706

699-
impl<T> Sub for Ptr<T> {
707+
impl<T: ByteRepr> Sub for Ptr<T> {
700708
type Output = isize;
701709
fn sub(self, other: Self) -> Self::Output {
702710
assert!(self.kind == other.kind, "ub: invalid subtraction");
@@ -706,7 +714,7 @@ impl<T> Sub for Ptr<T> {
706714

707715
macro_rules! impl_ptr_add_sub_assign {
708716
($($rhs:ty),+) => { $(
709-
impl<T> std::ops::AddAssign<$rhs> for Ptr<T> {
717+
impl<T: ByteRepr> std::ops::AddAssign<$rhs> for Ptr<T> {
710718
#[inline]
711719
fn add_assign(&mut self, other: $rhs) {
712720
let step = self.elem_step();
@@ -715,7 +723,7 @@ macro_rules! impl_ptr_add_sub_assign {
715723
);
716724
}
717725
}
718-
impl<T> std::ops::SubAssign<$rhs> for Ptr<T> {
726+
impl<T: ByteRepr> std::ops::SubAssign<$rhs> for Ptr<T> {
719727
#[inline]
720728
fn sub_assign(&mut self, other: $rhs) {
721729
let step = self.elem_step();
@@ -730,22 +738,22 @@ impl_ptr_add_sub_assign!(i32, u32, u64, isize, usize);
730738

731739
macro_rules! impl_ptr_add_sub {
732740
($($rhs:ty),+) => { $(
733-
impl<T> std::ops::Add<$rhs> for &Ptr<T> {
741+
impl<T: ByteRepr> std::ops::Add<$rhs> for &Ptr<T> {
734742
type Output = Ptr<T>;
735743
#[inline]
736744
fn add(self, other: $rhs) -> Ptr<T> { let mut r = self.clone(); r += other; r }
737745
}
738-
impl<T> std::ops::Sub<$rhs> for &Ptr<T> {
746+
impl<T: ByteRepr> std::ops::Sub<$rhs> for &Ptr<T> {
739747
type Output = Ptr<T>;
740748
#[inline]
741749
fn sub(self, other: $rhs) -> Ptr<T> { let mut r = self.clone(); r += -(other as isize); r }
742750
}
743-
impl<T> std::ops::Add<$rhs> for Ptr<T> {
751+
impl<T: ByteRepr> std::ops::Add<$rhs> for Ptr<T> {
744752
type Output = Self;
745753
#[inline]
746754
fn add(mut self, other: $rhs) -> Self { self += other; self }
747755
}
748-
impl<T> std::ops::Sub<$rhs> for Ptr<T> {
756+
impl<T: ByteRepr> std::ops::Sub<$rhs> for Ptr<T> {
749757
type Output = Self;
750758
#[inline]
751759
fn sub(mut self, other: $rhs) -> Self { self += -(other as isize); self }
@@ -754,7 +762,7 @@ macro_rules! impl_ptr_add_sub {
754762
}
755763
impl_ptr_add_sub!(i32, u32, u64, isize, usize);
756764

757-
impl<T> PostfixInc for Ptr<T> {
765+
impl<T: ByteRepr> PostfixInc for Ptr<T> {
758766
#[inline]
759767
fn postfix_inc(&mut self) -> Self {
760768
let ret = self.clone();
@@ -763,7 +771,7 @@ impl<T> PostfixInc for Ptr<T> {
763771
}
764772
}
765773

766-
impl<T> PostfixDec for Ptr<T> {
774+
impl<T: ByteRepr> PostfixDec for Ptr<T> {
767775
#[inline]
768776
fn postfix_dec(&mut self) -> Self {
769777
let ret = self.clone();
@@ -772,15 +780,15 @@ impl<T> PostfixDec for Ptr<T> {
772780
}
773781
}
774782

775-
impl<T> PrefixInc for Ptr<T> {
783+
impl<T: ByteRepr> PrefixInc for Ptr<T> {
776784
#[inline]
777785
fn prefix_inc(&mut self) -> Self {
778786
self.offset = self.offset.wrapping_add(self.elem_step());
779787
self.clone()
780788
}
781789
}
782790

783-
impl<T> PrefixDec for Ptr<T> {
791+
impl<T: ByteRepr> PrefixDec for Ptr<T> {
784792
#[inline]
785793
fn prefix_dec(&mut self) -> Self {
786794
self.offset = self.offset.wrapping_sub(self.elem_step());
@@ -909,7 +917,7 @@ impl<T> fmt::Debug for Ptr<T> {
909917
(Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset())
910918
}
911919
PtrKind::Vec(w) => (Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset()),
912-
PtrKind::Reinterpreted(view) => view.alloc.address().wrapping_add(self.byte_offset()),
920+
PtrKind::Reinterpreted(alloc) => alloc.address().wrapping_add(self.byte_offset()),
913921
};
914922
write!(f, "0x{:x}", addr)
915923
}
@@ -1005,9 +1013,9 @@ impl Ptr<u8> {
10051013
let raw = strong.borrow();
10061014
raw[start..end].to_vec()
10071015
}
1008-
PtrKind::Reinterpreted(ref view) => {
1016+
PtrKind::Reinterpreted(ref alloc) => {
10091017
let mut buf = vec![0u8; end.wrapping_sub(start)];
1010-
view.alloc.read_bytes(start, &mut buf);
1018+
alloc.read_bytes(start, &mut buf);
10111019
buf
10121020
}
10131021
}

0 commit comments

Comments
 (0)