-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Use Partial instead of PartialArray
Partial is just a contiguous slice, and much simpler than PartialArray; Partial is all that's needed, because the area written will always be contiguous.
- Loading branch information
Showing
5 changed files
with
104 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2020 bluss and ndarray developers. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use std::ptr; | ||
|
||
/// Partial is a partially written contiguous slice of data; | ||
/// it is the owner of the elements, but not the allocation, | ||
/// and will drop the elements on drop. | ||
#[must_use] | ||
pub(crate) struct Partial<T> { | ||
/// Data pointer | ||
ptr: *mut T, | ||
/// Current length | ||
pub(crate) len: usize, | ||
} | ||
|
||
impl<T> Partial<T> { | ||
/// Create an empty partial for this data pointer | ||
/// | ||
/// ## Safety | ||
/// | ||
/// Unless ownership is released, the Partial acts as an owner of the slice of data (not the | ||
/// allocation); and will free the elements on drop; the pointer must be dereferenceable and | ||
/// the `len` elements following it valid. | ||
/// | ||
/// The Partial has an accessible length field which must only be modified in trusted code. | ||
pub(crate) unsafe fn new(ptr: *mut T) -> Self { | ||
Self { | ||
ptr, | ||
len: 0, | ||
} | ||
} | ||
|
||
#[cfg(feature = "rayon")] | ||
pub(crate) fn stub() -> Self { | ||
Self { len: 0, ptr: 0 as *mut _ } | ||
} | ||
|
||
#[cfg(feature = "rayon")] | ||
pub(crate) fn is_stub(&self) -> bool { | ||
self.ptr.is_null() | ||
} | ||
|
||
/// Release Partial's ownership of the written elements, and return the current length | ||
pub(crate) fn release_ownership(mut self) -> usize { | ||
let ret = self.len; | ||
self.len = 0; | ||
ret | ||
} | ||
|
||
#[cfg(feature = "rayon")] | ||
/// Merge if they are in order (left to right) and contiguous. | ||
/// Skips merge if T does not need drop. | ||
pub(crate) fn try_merge(mut left: Self, right: Self) -> Self { | ||
if !std::mem::needs_drop::<T>() { | ||
return left; | ||
} | ||
// Merge the partial collect results; the final result will be a slice that | ||
// covers the whole output. | ||
if left.is_stub() { | ||
right | ||
} else if left.ptr.wrapping_add(left.len) == right.ptr { | ||
left.len += right.release_ownership(); | ||
left | ||
} else { | ||
// failure to merge; this is a bug in collect, so we will never reach this | ||
debug_assert!(false, "Partial: failure to merge left and right parts"); | ||
left | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<T> Send for Partial<T> where T: Send { } | ||
|
||
impl<T> Drop for Partial<T> { | ||
fn drop(&mut self) { | ||
if !self.ptr.is_null() { | ||
unsafe { | ||
ptr::drop_in_place(std::slice::from_raw_parts_mut(self.ptr, self.len)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.