7
7
// Media Patent License 1.0 was not distributed with this source code in the
8
8
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9
9
10
- use std:: alloc:: { alloc, dealloc, Layout } ;
11
10
use std:: mem:: MaybeUninit ;
12
- use std:: ptr;
13
- use std:: { fmt, mem} ;
14
11
15
12
#[ repr( align( 64 ) ) ]
16
13
pub struct Align64 ;
@@ -64,101 +61,6 @@ impl<T> Aligned<T> {
64
61
}
65
62
}
66
63
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
-
162
64
#[ cfg( test) ]
163
65
mod test {
164
66
use super :: * ;
@@ -172,10 +74,4 @@ mod test {
172
74
let a: Aligned < _ > = Aligned :: new ( [ 0u8 ; 3 ] ) ;
173
75
assert ! ( is_aligned( a. data. as_ptr( ) , 4 ) ) ;
174
76
}
175
-
176
- #[ test]
177
- fn sanity_heap ( ) {
178
- let a: AlignedBoxedSlice < _ > = AlignedBoxedSlice :: new ( 3 , 0u8 ) ;
179
- assert ! ( is_aligned( a. as_ptr( ) , 4 ) ) ;
180
- }
181
77
}
0 commit comments