Skip to content

Commit 732162e

Browse files
FreezyLemonbarrbrain
authored andcommitted
Replace AlignedBoxedSlice with aligned_vec::ABox
1 parent f3dd049 commit 732162e

File tree

4 files changed

+12
-106
lines changed

4 files changed

+12
-106
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ profiling = { version = "1" }
115115
tracing-subscriber = { version = "0.3.18", optional = true }
116116
tracing-chrome = { version = "0.7.1", optional = true }
117117
tracing = { version = "0.1.40", optional = true }
118+
aligned-vec = "0.5.0"
118119

119120
[dependencies.image]
120121
version = "0.24.8"

src/predict.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ cfg_if::cfg_if! {
2323
}
2424
}
2525

26+
use aligned_vec::{avec, ABox};
27+
2628
use crate::context::{TileBlockOffset, MAX_SB_SIZE_LOG2, MAX_TX_SIZE};
2729
use crate::cpu_features::CpuFeatureLevel;
2830
use crate::encoder::FrameInvariants;
@@ -423,7 +425,7 @@ impl PredictionMode {
423425
/// compound inter prediction.
424426
#[derive(Debug)]
425427
pub struct InterCompoundBuffers {
426-
data: AlignedBoxedSlice<i16>,
428+
data: ABox<[i16]>,
427429
}
428430

429431
impl InterCompoundBuffers {
@@ -452,7 +454,7 @@ impl InterCompoundBuffers {
452454

453455
impl Default for InterCompoundBuffers {
454456
fn default() -> Self {
455-
Self { data: AlignedBoxedSlice::new(2 * Self::BUFFER_SIZE, 0) }
457+
Self { data: avec![0; 2 * Self::BUFFER_SIZE].into_boxed_slice() }
456458
}
457459
}
458460

src/util/align.rs

-104
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
// Media Patent License 1.0 was not distributed with this source code in the
88
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
99

10-
use std::alloc::{alloc, dealloc, Layout};
1110
use std::mem::MaybeUninit;
12-
use std::ptr;
13-
use std::{fmt, mem};
1411

1512
#[repr(align(64))]
1613
pub struct Align64;
@@ -64,101 +61,6 @@ impl<T> Aligned<T> {
6461
}
6562
}
6663

67-
/// An analog to a Box<[T]> where the underlying slice is aligned.
68-
/// Alignment is according to the architecture-specific SIMD constraints.
69-
pub struct AlignedBoxedSlice<T> {
70-
ptr: std::ptr::NonNull<T>,
71-
len: usize,
72-
}
73-
74-
impl<T> AlignedBoxedSlice<T> {
75-
// Data alignment in bytes.
76-
cfg_if::cfg_if! {
77-
if #[cfg(target_arch = "wasm32")] {
78-
// FIXME: wasm32 allocator fails for alignment larger than 3
79-
const DATA_ALIGNMENT_LOG2: usize = 3;
80-
} else {
81-
const DATA_ALIGNMENT_LOG2: usize = 6;
82-
}
83-
}
84-
85-
const fn layout(len: usize) -> Layout {
86-
// SAFETY: We are ensuring that `align` is non-zero and is a multiple of 2.
87-
unsafe {
88-
Layout::from_size_align_unchecked(
89-
len * mem::size_of::<T>(),
90-
1 << Self::DATA_ALIGNMENT_LOG2,
91-
)
92-
}
93-
}
94-
95-
fn alloc(len: usize) -> std::ptr::NonNull<T> {
96-
// SAFETY: We are not calling this with a null pointer, so it's safe.
97-
unsafe { ptr::NonNull::new_unchecked(alloc(Self::layout(len)) as *mut T) }
98-
}
99-
100-
/// Creates a [`AlignedBoxedSlice`] with a slice of length [`len`] filled with
101-
/// [`val`].
102-
pub fn new(len: usize, val: T) -> Self
103-
where
104-
T: Clone,
105-
{
106-
let mut output = Self { ptr: Self::alloc(len), len };
107-
108-
for a in output.iter_mut() {
109-
*a = val.clone();
110-
}
111-
112-
output
113-
}
114-
}
115-
116-
impl<T: fmt::Debug> fmt::Debug for AlignedBoxedSlice<T> {
117-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118-
fmt::Debug::fmt(&**self, f)
119-
}
120-
}
121-
122-
impl<T> std::ops::Deref for AlignedBoxedSlice<T> {
123-
type Target = [T];
124-
125-
fn deref(&self) -> &[T] {
126-
// SAFETY: We know that `self.ptr` is not null, and we know its length.
127-
unsafe {
128-
let p = self.ptr.as_ptr();
129-
130-
std::slice::from_raw_parts(p, self.len)
131-
}
132-
}
133-
}
134-
135-
impl<T> std::ops::DerefMut for AlignedBoxedSlice<T> {
136-
fn deref_mut(&mut self) -> &mut [T] {
137-
// SAFETY: We know that `self.ptr` is not null, and we know its length.
138-
unsafe {
139-
let p = self.ptr.as_ptr();
140-
141-
std::slice::from_raw_parts_mut(p, self.len)
142-
}
143-
}
144-
}
145-
146-
impl<T> std::ops::Drop for AlignedBoxedSlice<T> {
147-
fn drop(&mut self) {
148-
// SAFETY: We know that the contents of this struct are aligned and valid to drop.
149-
unsafe {
150-
for a in self.iter_mut() {
151-
ptr::drop_in_place(a)
152-
}
153-
154-
dealloc(self.ptr.as_ptr() as *mut u8, Self::layout(self.len));
155-
}
156-
}
157-
}
158-
159-
unsafe impl<T> Send for AlignedBoxedSlice<T> where T: Send {}
160-
unsafe impl<T> Sync for AlignedBoxedSlice<T> where T: Sync {}
161-
16264
#[cfg(test)]
16365
mod test {
16466
use super::*;
@@ -172,10 +74,4 @@ mod test {
17274
let a: Aligned<_> = Aligned::new([0u8; 3]);
17375
assert!(is_aligned(a.data.as_ptr(), 4));
17476
}
175-
176-
#[test]
177-
fn sanity_heap() {
178-
let a: AlignedBoxedSlice<_> = AlignedBoxedSlice::new(3, 0u8);
179-
assert!(is_aligned(a.as_ptr(), 4));
180-
}
18177
}

0 commit comments

Comments
 (0)