Skip to content

Commit 299831f

Browse files
committed
Add c_byte_offset and c_byte_len
1 parent 49bbc5b commit 299831f

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

libcc2rs/src/rc.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,24 @@ impl<T> Ptr<T> {
438438
}
439439
}
440440

441+
impl<T: ByteRepr> Ptr<T> {
442+
#[inline]
443+
fn c_byte_len(&self) -> usize {
444+
match &self.kind {
445+
PtrKind::Reinterpreted(data) => data.alloc.total_byte_len(),
446+
_ => self.len().wrapping_mul(T::byte_size()),
447+
}
448+
}
449+
450+
#[inline]
451+
fn c_byte_offset(&self) -> usize {
452+
match &self.kind {
453+
PtrKind::Reinterpreted(_) => self.offset,
454+
_ => self.offset.wrapping_mul(T::byte_size()),
455+
}
456+
}
457+
}
458+
441459
impl<T> Ptr<T> {
442460
pub fn with_mut<R>(&self, f: impl FnOnce(&mut T) -> R) -> R
443461
where
@@ -1314,22 +1332,18 @@ impl<T: ByteRepr> ByteRepr for Ptr<T> {
13141332
0usize.to_bytes(buf);
13151333
return;
13161334
}
1317-
let (byte_off, byte_len) = match &self.kind {
1318-
PtrKind::Reinterpreted(data) => (self.offset, data.alloc.total_byte_len()),
1319-
_ => (
1320-
self.offset.wrapping_mul(T::byte_size()),
1321-
self.len() * T::byte_size(),
1322-
),
1323-
};
1324-
let rebased = Ptr {
1325-
offset: 0,
1326-
kind: self.kind.clone(),
1327-
};
13281335
let base = PTR_REGISTRY.with(|r| {
1329-
r.borrow_mut()
1330-
.put(self.kind.address(), byte_len, rebased.to_any())
1336+
r.borrow_mut().put(
1337+
self.kind.address(),
1338+
self.c_byte_len(),
1339+
Ptr {
1340+
offset: 0,
1341+
kind: self.kind.clone(),
1342+
}
1343+
.to_any(),
1344+
)
13311345
});
1332-
base.wrapping_add(byte_off).to_bytes(buf);
1346+
base.wrapping_add(self.c_byte_offset()).to_bytes(buf);
13331347
}
13341348

13351349
fn from_bytes(buf: &[u8]) -> Self {

0 commit comments

Comments
 (0)