Skip to content

Commit 58673b2

Browse files
committed
use Ref to own types
1 parent 9004050 commit 58673b2

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

rust/src/architecture.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub trait Intrinsic: Sized + Clone + Copy {
313313
fn id(&self) -> u32;
314314

315315
/// Reeturns the list of the input names and types for this intrinsic.
316-
fn inputs(&self) -> Vec<NameAndType>;
316+
fn inputs(&self) -> Vec<Ref<NameAndType>>;
317317

318318
/// Returns the list of the output types for this intrinsic.
319319
fn outputs(&self) -> Vec<Conf<Ref<Type>>>;
@@ -650,7 +650,7 @@ impl Intrinsic for UnusedIntrinsic {
650650
fn id(&self) -> u32 {
651651
unreachable!()
652652
}
653-
fn inputs(&self) -> Vec<NameAndType> {
653+
fn inputs(&self) -> Vec<Ref<NameAndType>> {
654654
unreachable!()
655655
}
656656
fn outputs(&self) -> Vec<Conf<Ref<Type>>> {
@@ -992,7 +992,7 @@ impl Intrinsic for crate::architecture::CoreIntrinsic {
992992
self.1
993993
}
994994

995-
fn inputs(&self) -> Vec<NameAndType> {
995+
fn inputs(&self) -> Vec<Ref<NameAndType>> {
996996
let mut count: usize = 0;
997997

998998
unsafe {
@@ -1172,7 +1172,7 @@ impl Architecture for CoreArchitecture {
11721172
}
11731173
}
11741174
}
1175-
1175+
11761176
fn instruction_llil(
11771177
&self,
11781178
data: &[u8],
@@ -2424,7 +2424,7 @@ where
24242424
let inputs = intrinsic.inputs();
24252425
let mut res = Vec::with_capacity(inputs.len());
24262426
for input in inputs {
2427-
res.push(input.into_raw());
2427+
res.push(unsafe { Ref::into_raw(input) }.into_raw());
24282428
}
24292429

24302430
unsafe {

rust/src/binaryview.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ pub trait BinaryViewExt: BinaryViewBase {
574574
}
575575
}
576576

577-
fn define_auto_data_var(&self, dv: DataVariable) {
577+
fn define_auto_data_var(&self, dv: Ref<DataVariable>) {
578578
unsafe {
579579
BNDefineDataVariable(
580580
self.as_ref().handle,
@@ -585,7 +585,7 @@ pub trait BinaryViewExt: BinaryViewBase {
585585
}
586586

587587
/// You likely would also like to call [`Self::define_user_symbol`] to bind this data variable with a name
588-
fn define_user_data_var(&self, dv: DataVariable) {
588+
fn define_user_data_var(&self, dv: Ref<DataVariable>) {
589589
unsafe {
590590
BNDefineUserDataVariable(
591591
self.as_ref().handle,

rust/src/debuginfo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl DebugInfo {
376376
}
377377

378378
/// Returns a generator of all types provided by a named DebugInfoParser
379-
pub fn types_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<NameAndType> {
379+
pub fn types_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<Ref<NameAndType>> {
380380
let parser_name = parser_name.into_bytes_with_nul();
381381

382382
let mut count: usize = 0;
@@ -387,7 +387,7 @@ impl DebugInfo {
387387
&mut count,
388388
)
389389
};
390-
let result: Vec<NameAndType> = unsafe {
390+
let result: Vec<Ref<NameAndType>> = unsafe {
391391
slice::from_raw_parts_mut(debug_types_ptr, count)
392392
.iter()
393393
.map(NameAndType::from_raw)
@@ -399,10 +399,10 @@ impl DebugInfo {
399399
}
400400

401401
/// A generator of all types provided by DebugInfoParsers
402-
pub fn types(&self) -> Vec<NameAndType> {
402+
pub fn types(&self) -> Vec<Ref<NameAndType>> {
403403
let mut count: usize = 0;
404404
let debug_types_ptr = unsafe { BNGetDebugTypes(self.handle, ptr::null_mut(), &mut count) };
405-
let result: Vec<NameAndType> = unsafe {
405+
let result: Vec<Ref<NameAndType>> = unsafe {
406406
slice::from_raw_parts_mut(debug_types_ptr, count)
407407
.iter()
408408
.map(NameAndType::from_raw)

rust/src/types.rs

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,7 +2454,7 @@ unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedNameTypeAndId {
24542454
pub struct NameAndType(pub(crate) BNNameAndType);
24552455

24562456
impl NameAndType {
2457-
pub(crate) fn from_raw(raw: &BNNameAndType) -> Self {
2457+
pub(crate) fn from_raw(raw: &BNNameAndType) -> Ref<Self> {
24582458
Self::new(
24592459
raw_to_string(raw.name).unwrap(),
24602460
unsafe { &Type::ref_from_raw(raw.type_) },
@@ -2464,12 +2464,14 @@ impl NameAndType {
24642464
}
24652465

24662466
impl NameAndType {
2467-
pub fn new<S: BnStrCompatible>(name: S, t: &Ref<Type>, confidence: u8) -> Self {
2468-
Self(BNNameAndType {
2469-
name: unsafe { BNAllocString(name.into_bytes_with_nul().as_ref().as_ptr() as *mut _) },
2470-
type_: unsafe { Ref::into_raw(t.to_owned()).handle },
2471-
typeConfidence: confidence,
2472-
})
2467+
pub fn new<S: BnStrCompatible>(name: S, t: &Type, confidence: u8) -> Ref<Self> {
2468+
unsafe {
2469+
Ref::new(Self(BNNameAndType {
2470+
name: BNAllocString(name.into_bytes_with_nul().as_ref().as_ptr() as *mut _),
2471+
type_: Ref::into_raw(t.to_owned()).handle,
2472+
typeConfidence: confidence,
2473+
}))
2474+
}
24732475
}
24742476

24752477
pub(crate) fn into_raw(self) -> BNNameAndType {
@@ -2491,11 +2493,27 @@ impl NameAndType {
24912493
}
24922494
}
24932495

2494-
impl Drop for NameAndType {
2495-
fn drop(&mut self) {
2496+
impl ToOwned for NameAndType {
2497+
type Owned = Ref<Self>;
2498+
2499+
fn to_owned(&self) -> Self::Owned {
2500+
unsafe { RefCountable::inc_ref(self) }
2501+
}
2502+
}
2503+
2504+
unsafe impl RefCountable for NameAndType {
2505+
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
2506+
Self::new(
2507+
CStr::from_ptr(handle.0.name),
2508+
handle.t(),
2509+
handle.type_with_confidence().confidence,
2510+
)
2511+
}
2512+
2513+
unsafe fn dec_ref(handle: &Self) {
24962514
unsafe {
2497-
BNFreeString(self.0.name);
2498-
BNFreeType(self.0.type_);
2515+
BNFreeString(handle.0.name);
2516+
RefCountable::dec_ref(handle.t());
24992517
}
25002518
}
25012519
}
@@ -2559,6 +2577,29 @@ impl DataVariable {
25592577
}
25602578
}
25612579

2580+
impl ToOwned for DataVariable {
2581+
type Owned = Ref<Self>;
2582+
2583+
fn to_owned(&self) -> Self::Owned {
2584+
unsafe { RefCountable::inc_ref(self) }
2585+
}
2586+
}
2587+
2588+
unsafe impl RefCountable for DataVariable {
2589+
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
2590+
unsafe {
2591+
Ref::new(Self(BNDataVariable {
2592+
type_: Ref::into_raw(handle.t().to_owned()).handle,
2593+
..handle.0
2594+
}))
2595+
}
2596+
}
2597+
2598+
unsafe fn dec_ref(handle: &Self) {
2599+
unsafe { BNFreeType(handle.0.type_) }
2600+
}
2601+
}
2602+
25622603
impl CoreArrayProvider for DataVariable {
25632604
type Raw = BNDataVariable;
25642605
type Context = ();

0 commit comments

Comments
 (0)