diff options
| author | Fabian Freyer <fabian.freyer@physik.tu-berlin.de> | 2022-01-31 06:24:07 +0100 |
|---|---|---|
| committer | KyleMiles <krm504@nyu.edu> | 2022-02-14 18:45:09 -0500 |
| commit | 181ad567199e5685578ecf7ad400257098c2e175 (patch) | |
| tree | 9e45c986cc8157dfe0fd6f5a8e80898d4a128309 /rust/src | |
| parent | ae7ae4f63f931342e155d77bb4cf6d75289682bf (diff) | |
rust: add ArrayGuard and for non-owned arrays
To make use of shard functionality, we split the CoreOwnedArrayProvider
into CoreArrayProvider and CoreOwnedArrayProvider. Array makes use of
the CoreOwnedArrayProvider, which depends on CoreArrayProvider, while
the new ArrayGuard only requires CoreArrayProvider and represents
a non-owned array.
Diffstat (limited to 'rust/src')
| -rw-r--r-- | rust/src/backgroundtask.rs | 6 | ||||
| -rw-r--r-- | rust/src/basicblock.rs | 12 | ||||
| -rw-r--r-- | rust/src/custombinaryview.rs | 6 | ||||
| -rw-r--r-- | rust/src/debuginfo.rs | 4 | ||||
| -rw-r--r-- | rust/src/function.rs | 6 | ||||
| -rw-r--r-- | rust/src/platform.rs | 6 | ||||
| -rw-r--r-- | rust/src/rc.rs | 92 | ||||
| -rw-r--r-- | rust/src/section.rs | 6 | ||||
| -rw-r--r-- | rust/src/segment.rs | 6 | ||||
| -rw-r--r-- | rust/src/string.rs | 6 | ||||
| -rw-r--r-- | rust/src/symbol.rs | 6 | ||||
| -rw-r--r-- | rust/src/types.rs | 28 |
12 files changed, 138 insertions, 46 deletions
diff --git a/rust/src/backgroundtask.rs b/rust/src/backgroundtask.rs index 26480e7e..451ecd07 100644 --- a/rust/src/backgroundtask.rs +++ b/rust/src/backgroundtask.rs @@ -99,16 +99,18 @@ unsafe impl RefCountable for BackgroundTask { } } -unsafe impl CoreOwnedArrayProvider for BackgroundTask { +impl CoreArrayProvider for BackgroundTask { type Raw = *mut BNBackgroundTask; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for BackgroundTask { unsafe fn free(raw: *mut *mut BNBackgroundTask, count: usize, _context: &()) { BNFreeBackgroundTaskList(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for BackgroundTask { +unsafe impl<'a> CoreArrayWrapper<'a> for BackgroundTask { type Wrapped = Guard<'a, BackgroundTask>; unsafe fn wrap_raw( diff --git a/rust/src/basicblock.rs b/rust/src/basicblock.rs index 0bd32af3..a1a2e244 100644 --- a/rust/src/basicblock.rs +++ b/rust/src/basicblock.rs @@ -65,16 +65,18 @@ pub struct EdgeContext<'a, C: 'a + BlockContext> { orig_block: &'a BasicBlock<C>, } -unsafe impl<'a, C: 'a + BlockContext> CoreOwnedArrayProvider for Edge<'a, C> { +impl<'a, C: 'a + BlockContext> CoreArrayProvider for Edge<'a, C> { type Raw = BNBasicBlockEdge; type Context = EdgeContext<'a, C>; +} +unsafe impl<'a, C: 'a + BlockContext> CoreOwnedArrayProvider for Edge<'a, C> { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { BNFreeBasicBlockEdgeList(raw, count); } } -unsafe impl<'a, C: 'a + BlockContext> CoreOwnedArrayWrapper<'a> for Edge<'a, C> { +unsafe impl<'a, C: 'a + BlockContext> CoreArrayWrapper<'a> for Edge<'a, C> { type Wrapped = Edge<'a, C>; unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Edge<'a, C> { @@ -296,16 +298,18 @@ unsafe impl<C: BlockContext> RefCountable for BasicBlock<C> { } } -unsafe impl<C: BlockContext> CoreOwnedArrayProvider for BasicBlock<C> { +impl<C: BlockContext> CoreArrayProvider for BasicBlock<C> { type Raw = *mut BNBasicBlock; type Context = C; +} +unsafe impl<C: BlockContext> CoreOwnedArrayProvider for BasicBlock<C> { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { BNFreeBasicBlockList(raw, count); } } -unsafe impl<'a, C: 'a + BlockContext> CoreOwnedArrayWrapper<'a> for BasicBlock<C> { +unsafe impl<'a, C: 'a + BlockContext> CoreArrayWrapper<'a> for BasicBlock<C> { type Wrapped = Guard<'a, BasicBlock<C>>; unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped { diff --git a/rust/src/custombinaryview.rs b/rust/src/custombinaryview.rs index b1fda73a..48274fe4 100644 --- a/rust/src/custombinaryview.rs +++ b/rust/src/custombinaryview.rs @@ -245,16 +245,18 @@ impl BinaryViewTypeBase for BinaryViewType { } } -unsafe impl CoreOwnedArrayProvider for BinaryViewType { +impl CoreArrayProvider for BinaryViewType { type Raw = *mut BNBinaryViewType; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for BinaryViewType { unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) { BNFreeBinaryViewTypeList(raw); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for BinaryViewType { +unsafe impl<'a> CoreArrayWrapper<'a> for BinaryViewType { type Wrapped = BinaryViewType; unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs index b01aca6a..56786d19 100644 --- a/rust/src/debuginfo.rs +++ b/rust/src/debuginfo.rs @@ -226,10 +226,12 @@ impl ToOwned for DebugInfoParser { } } -unsafe impl CoreOwnedArrayProvider for DebugInfoParser { +impl CoreArrayProvider for DebugInfoParser { type Raw = *mut BNDebugInfoParser; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for DebugInfoParser { unsafe fn free(raw: *mut Self::Raw, count: usize, _: &Self::Context) { BNFreeDebugInfoParserList(raw, count); } diff --git a/rust/src/function.rs b/rust/src/function.rs index 51c9e634..865a22db 100644 --- a/rust/src/function.rs +++ b/rust/src/function.rs @@ -269,16 +269,18 @@ unsafe impl RefCountable for Function { } } -unsafe impl CoreOwnedArrayProvider for Function { +impl CoreArrayProvider for Function { type Raw = *mut BNFunction; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for Function { unsafe fn free(raw: *mut *mut BNFunction, count: usize, _context: &()) { BNFreeFunctionList(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for Function { +unsafe impl<'a> CoreArrayWrapper<'a> for Function { type Wrapped = Guard<'a, Function>; unsafe fn wrap_raw(raw: &'a *mut BNFunction, context: &'a ()) -> Guard<'a, Function> { diff --git a/rust/src/platform.rs b/rust/src/platform.rs index b79b9611..9d7987c4 100644 --- a/rust/src/platform.rs +++ b/rust/src/platform.rs @@ -347,16 +347,18 @@ unsafe impl RefCountable for Platform { } } -unsafe impl CoreOwnedArrayProvider for Platform { +impl CoreArrayProvider for Platform { type Raw = *mut BNPlatform; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for Platform { unsafe fn free(raw: *mut *mut BNPlatform, count: usize, _context: &()) { BNFreePlatformList(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for Platform { +unsafe impl<'a> CoreArrayWrapper<'a> for Platform { type Wrapped = Guard<'a, Platform>; unsafe fn wrap_raw(raw: &'a *mut BNPlatform, context: &'a ()) -> Guard<'a, Platform> { diff --git a/rust/src/rc.rs b/rust/src/rc.rs index 7c5d610f..bc60bf94 100644 --- a/rust/src/rc.rs +++ b/rust/src/rc.rs @@ -146,14 +146,16 @@ impl<'a, T> Borrow<T> for Guard<'a, T> { } } -pub unsafe trait CoreOwnedArrayProvider { +pub trait CoreArrayProvider { type Raw; type Context; +} +pub unsafe trait CoreOwnedArrayProvider: CoreArrayProvider { unsafe fn free(raw: *mut Self::Raw, count: usize, context: &Self::Context); } -pub unsafe trait CoreOwnedArrayWrapper<'a>: CoreOwnedArrayProvider +pub unsafe trait CoreArrayWrapper<'a>: CoreArrayProvider where Self::Raw: 'a, Self::Context: 'a, @@ -197,7 +199,7 @@ impl<P: CoreOwnedArrayProvider> Array<P> { } } -impl<'a, P: 'a + CoreOwnedArrayWrapper<'a>> Array<P> { +impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider> Array<P> { #[inline] pub fn get(&'a self, index: usize) -> P::Wrapped { unsafe { @@ -214,7 +216,7 @@ impl<'a, P: 'a + CoreOwnedArrayWrapper<'a>> Array<P> { } } -impl<'a, P: 'a + CoreOwnedArrayWrapper<'a>> IntoIterator for &'a Array<P> { +impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreOwnedArrayProvider> IntoIterator for &'a Array<P> { type Item = P::Wrapped; type IntoIter = ArrayIter<'a, P>; @@ -231,9 +233,69 @@ impl<P: CoreOwnedArrayProvider> Drop for Array<P> { } } +pub struct ArrayGuard<P: CoreArrayProvider> { + contents: *mut P::Raw, + count: usize, + context: P::Context, +} + +unsafe impl<P> Sync for ArrayGuard<P> +where + P: CoreArrayProvider, + P::Context: Sync, +{ +} +unsafe impl<P> Send for ArrayGuard<P> +where + P: CoreArrayProvider, + P::Context: Send, +{ +} + +impl<P: CoreArrayProvider> ArrayGuard<P> { + pub(crate) unsafe fn new(raw: *mut P::Raw, count: usize, context: P::Context) -> Self { + Self { + contents: raw, + count, + context, + } + } + + #[inline] + pub fn len(&self) -> usize { + self.count + } +} + +impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> ArrayGuard<P> { + #[inline] + pub fn get(&'a self, index: usize) -> P::Wrapped { + unsafe { + let backing = slice::from_raw_parts(self.contents, self.count); + P::wrap_raw(&backing[index], &self.context) + } + } + + pub fn iter(&'a self) -> ArrayIter<'a, P> { + ArrayIter { + it: unsafe { slice::from_raw_parts(self.contents, self.count).iter() }, + context: &self.context, + } + } +} + +impl<'a, P: 'a + CoreArrayWrapper<'a> + CoreArrayProvider> IntoIterator for &'a ArrayGuard<P> { + type Item = P::Wrapped; + type IntoIter = ArrayIter<'a, P>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + pub struct ArrayIter<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, { it: slice::Iter<'a, P::Raw>, context: &'a P::Context, @@ -241,14 +303,14 @@ where unsafe impl<'a, P> Send for ArrayIter<'a, P> where - P: CoreOwnedArrayWrapper<'a>, + P: CoreArrayWrapper<'a>, P::Context: Sync, { } impl<'a, P> Iterator for ArrayIter<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, { type Item = P::Wrapped; @@ -267,7 +329,7 @@ where impl<'a, P> ExactSizeIterator for ArrayIter<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, { #[inline] fn len(&self) -> usize { @@ -277,7 +339,7 @@ where impl<'a, P> DoubleEndedIterator for ArrayIter<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, { #[inline] fn next_back(&mut self) -> Option<P::Wrapped> { @@ -296,7 +358,7 @@ use rayon::iter::plumbing::*; #[cfg(feature = "rayon")] impl<'a, P> Array<P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, P::Context: Sync, P::Wrapped: Send, { @@ -307,7 +369,7 @@ where #[cfg(feature = "rayon")] pub struct ParArrayIter<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, ArrayIter<'a, P>: Send, { it: ArrayIter<'a, P>, @@ -316,7 +378,7 @@ where #[cfg(feature = "rayon")] impl<'a, P> ParallelIterator for ParArrayIter<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, P::Wrapped: Send, ArrayIter<'a, P>: Send, { @@ -337,7 +399,7 @@ where #[cfg(feature = "rayon")] impl<'a, P> IndexedParallelIterator for ParArrayIter<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, P::Wrapped: Send, ArrayIter<'a, P>: Send, { @@ -363,7 +425,7 @@ where #[cfg(feature = "rayon")] struct ArrayIterProducer<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, ArrayIter<'a, P>: Send, { it: ArrayIter<'a, P>, @@ -372,7 +434,7 @@ where #[cfg(feature = "rayon")] impl<'a, P> Producer for ArrayIterProducer<'a, P> where - P: 'a + CoreOwnedArrayWrapper<'a>, + P: 'a + CoreArrayWrapper<'a>, ArrayIter<'a, P>: Send, { type Item = P::Wrapped; diff --git a/rust/src/section.rs b/rust/src/section.rs index eb742e54..cfad97c0 100644 --- a/rust/src/section.rs +++ b/rust/src/section.rs @@ -168,16 +168,18 @@ unsafe impl RefCountable for Section { } } -unsafe impl CoreOwnedArrayProvider for Section { +impl CoreArrayProvider for Section { type Raw = *mut BNSection; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for Section { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { BNFreeSectionList(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for Section { +unsafe impl<'a> CoreArrayWrapper<'a> for Section { type Wrapped = Guard<'a, Section>; unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped { diff --git a/rust/src/segment.rs b/rust/src/segment.rs index b583e0e0..1dd3fb7f 100644 --- a/rust/src/segment.rs +++ b/rust/src/segment.rs @@ -187,16 +187,18 @@ unsafe impl RefCountable for Segment { } } -unsafe impl CoreOwnedArrayProvider for Segment { +impl CoreArrayProvider for Segment { type Raw = *mut BNSegment; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for Segment { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { BNFreeSegmentList(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for Segment { +unsafe impl<'a> CoreArrayWrapper<'a> for Segment { type Wrapped = Guard<'a, Segment>; unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped { diff --git a/rust/src/string.rs b/rust/src/string.rs index 198c764b..647ef9b0 100644 --- a/rust/src/string.rs +++ b/rust/src/string.rs @@ -155,17 +155,19 @@ impl fmt::Display for BnString { } } -unsafe impl CoreOwnedArrayProvider for BnString { +impl CoreArrayProvider for BnString { type Raw = *mut raw::c_char; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for BnString { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { use binaryninjacore_sys::BNFreeStringList; BNFreeStringList(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for BnString { +unsafe impl<'a> CoreArrayWrapper<'a> for BnString { type Wrapped = &'a BnStr; unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { diff --git a/rust/src/symbol.rs b/rust/src/symbol.rs index c545d962..28e655e0 100644 --- a/rust/src/symbol.rs +++ b/rust/src/symbol.rs @@ -257,16 +257,18 @@ unsafe impl RefCountable for Symbol { } } -unsafe impl CoreOwnedArrayProvider for Symbol { +impl CoreArrayProvider for Symbol { type Raw = *mut BNSymbol; type Context = (); +} +unsafe impl CoreOwnedArrayProvider for Symbol { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { BNFreeSymbolList(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for Symbol { +unsafe impl<'a> CoreArrayWrapper<'a> for Symbol { type Wrapped = Guard<'a, Symbol>; unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped { diff --git a/rust/src/types.rs b/rust/src/types.rs index 221fcb17..102d064c 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -1547,16 +1547,17 @@ impl Drop for QualifiedNameAndType { } } -unsafe impl CoreOwnedArrayProvider for QualifiedNameAndType { +impl CoreArrayProvider for QualifiedNameAndType { type Raw = BNQualifiedNameAndType; type Context = (); - +} +unsafe impl CoreOwnedArrayProvider for QualifiedNameAndType { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { BNFreeTypeList(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for QualifiedNameAndType { +unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedNameAndType { type Wrapped = &'a QualifiedNameAndType; unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { @@ -1595,16 +1596,18 @@ impl<S: BnStrCompatible> NameAndType<S> { } } -unsafe impl<S: BnStrCompatible> CoreOwnedArrayProvider for NameAndType<S> { +impl<S: BnStrCompatible> CoreArrayProvider for NameAndType<S> { type Raw = BNNameAndType; type Context = (); +} +unsafe impl<S: BnStrCompatible> CoreOwnedArrayProvider for NameAndType<S> { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { BNFreeNameAndTypeList(raw, count); } } -unsafe impl<'a, S: 'a + BnStrCompatible> CoreOwnedArrayWrapper<'a> for NameAndType<S> { +unsafe impl<'a, S: 'a + BnStrCompatible> CoreArrayWrapper<'a> for NameAndType<S> { type Wrapped = &'a NameAndType<S>; unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { @@ -1637,16 +1640,17 @@ impl DataVariable { } } -unsafe impl CoreOwnedArrayProvider for DataVariable { +impl CoreArrayProvider for DataVariable { type Raw = BNDataVariable; type Context = (); - +} +unsafe impl CoreOwnedArrayProvider for DataVariable { unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { BNFreeDataVariables(raw, count); } } -unsafe impl<'a> CoreOwnedArrayWrapper<'a> for DataVariable { +unsafe impl<'a> CoreArrayWrapper<'a> for DataVariable { type Wrapped = &'a DataVariable; unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { @@ -1681,18 +1685,22 @@ impl<S: BnStrCompatible> DataVariableAndName<S> { } } -// unsafe impl<S: BnStrCompatible> CoreOwnedArrayProvider for DataVariableAndName<S> { +// unsafe impl<S: BnStrCompatible> CoreArrayProvider for DataVariableAndName<S> { // type Raw = BNDataVariableAndName; // type Context = (); +// } +// unsafe impl<S: BnStrCompatible> CoreOwnedArrayProvider for DataVariableAndName<S> { // unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) { // BNFreeDataVariablesAndName(raw, count); // } // } -// unsafe impl<'a, S: 'a + BnStrCompatible> CoreOwnedArrayWrapper<'a> for DataVariableAndName<S> { +// unsafe impl<'a, S: 'a + BnStrCompatible> CoreArrayWrapper<'a> for DataVariableAndName<S> { // type Wrapped = &'a DataVariableAndName<S>; +// } +// unsafe impl<'a, S: 'a + BnStrCompatible> CoreArrayWrapper<'a> for DataVariableAndName<S> { // unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { // mem::transmute(raw) // } |
