@@ -209,21 +209,18 @@ impl<Prov> Allocation<Prov> {
209
209
) -> Self {
210
210
let slice: Cow < ' a , [ u8 ] > = slice. into ( ) ;
211
211
let size = Size :: from_bytes ( slice. len ( ) ) ;
212
- // TODO come up with a cleaner way to align the box based on the alignment parameter
213
- let count128 = ( slice. len ( ) / 16 ) + 1 ;
214
- // println!("ALIGN: {:?}, {:?}", (align.bytes())+1, count128);
215
- let mut vec128: Vec < u128 > = Vec :: with_capacity ( count128) ;
212
+ let align_usize: usize = align. bytes ( ) . try_into ( ) . unwrap ( ) ;
213
+ let count_align = ( ( slice. len ( ) / align_usize) + 1 ) * align_usize;
214
+
215
+ let mut vec_align: Vec < u8 > = Vec :: with_capacity ( count_align) ;
216
+ vec_align. resize ( count_align, 0 ) ;
216
217
// TODO avoid excess initialization
217
- vec128. resize ( count128, 0 ) ;
218
- let ( buf, _len, _capacity) = vec128. into_raw_parts ( ) ;
219
- assert ! ( ( buf as u64 ) % 16 == 0 ) ;
220
- let vec8: Vec < u8 > = unsafe { Vec :: from_raw_parts ( buf as * mut u8 , size. bytes_usize ( ) , size. bytes_usize ( ) ) } ;
221
- assert ! ( vec8. as_ptr( ) as u64 % 16 == 0 ) ;
222
- //let mut vec8: Vec<u8> = unsafe {Vec::from_raw_parts(buf as *mut u8, len * 16, capacity * 16)};
223
- //vec8.truncate(size.bytes_usize());
224
- let mut bytes = vec8. into_boxed_slice ( ) ;
225
- assert ! ( bytes. as_ptr( ) as u64 % 16 == 0 ) ;
226
- bytes. copy_from_slice ( & slice) ;
218
+ let ( buf, _len, _capacity) = vec_align. into_raw_parts ( ) ;
219
+ vec_align = unsafe { Vec :: from_raw_parts ( buf as * mut u8 , size. bytes_usize ( ) , size. bytes_usize ( ) ) } ;
220
+
221
+ let mut bytes = vec_align. into_boxed_slice ( ) ;
222
+ assert ! ( bytes. as_ptr( ) as u64 % align. bytes( ) == 0 ) ;
223
+ bytes[ ..slice. len ( ) ] . copy_from_slice ( & slice) ;
227
224
228
225
Self {
229
226
bytes,
@@ -244,16 +241,16 @@ impl<Prov> Allocation<Prov> {
244
241
///
245
242
/// If `panic_on_fail` is true, this will never return `Err`.
246
243
pub fn uninit < ' tcx > ( size : Size , align : Align , panic_on_fail : bool ) -> InterpResult < ' tcx , Self > {
247
- let count128 = ( size. bytes_usize ( ) / 16 ) + 1 ;
244
+ let align_usize: usize = align. bytes ( ) . try_into ( ) . unwrap ( ) ;
245
+ let count_align = ( ( size. bytes_usize ( ) / align_usize) + 1 ) * align_usize;
246
+
248
247
// TODO this one is supposed to handle allocation failure, so do so.
249
- let mut vec128: Vec < u128 > = Vec :: with_capacity ( count128) ;
250
- vec128. resize ( count128, 0 ) ;
251
- let ( buf, _len, _capacity) = vec128. into_raw_parts ( ) ;
252
- assert ! ( ( buf as u64 ) % 16 == 0 ) ;
253
- let vec8: Vec < u8 > = unsafe { Vec :: from_raw_parts ( buf as * mut u8 , size. bytes_usize ( ) , size. bytes_usize ( ) ) } ;
254
- assert ! ( vec8. as_ptr( ) as u64 % 16 == 0 ) ;
255
- let bytes = vec8. into_boxed_slice ( ) ;
256
- assert ! ( bytes. as_ptr( ) as u64 % 16 == 0 ) ;
248
+ let mut vec_align: Vec < u8 > = Vec :: with_capacity ( count_align) ;
249
+ vec_align. resize ( count_align, 0 ) ;
250
+ let ( buf, _len, _capacity) = vec_align. into_raw_parts ( ) ;
251
+ vec_align = unsafe { Vec :: from_raw_parts ( buf as * mut u8 , size. bytes_usize ( ) , size. bytes_usize ( ) ) } ;
252
+
253
+ let bytes = vec_align. into_boxed_slice ( ) ;
257
254
Ok ( ( ) ) . map_err ( |_: ( ) | {
258
255
// This results in an error that can happen non-deterministically, since the memory
259
256
// available to the compiler can change between runs. Normally queries are always
@@ -290,14 +287,15 @@ impl Allocation {
290
287
) -> Result < Allocation < Prov , Extra > , Err > {
291
288
// Compute new pointer provenance, which also adjusts the bytes.
292
289
// Realign the pointer
293
- // println!("adjusting: {:?}, {:?}", self.align, self.bytes.len());
294
- let count128 = ( self . bytes . len ( ) / 16 ) + 1 ;
295
- let mut vec128: Vec < u128 > = Vec :: with_capacity ( count128) ;
296
- vec128. resize ( count128, 0 ) ;
297
- let ( buf, _len, _capacity) = vec128. into_raw_parts ( ) ;
298
- let mut vec8: Vec < u8 > = unsafe { Vec :: from_raw_parts ( buf as * mut u8 , self . bytes . len ( ) , self . bytes . len ( ) ) } ;
299
- vec8[ ..self . bytes . len ( ) ] . copy_from_slice ( & self . bytes ) ;
300
- let mut bytes = vec8. into_boxed_slice ( ) ;
290
+ let align_usize: usize = self . align . bytes ( ) . try_into ( ) . unwrap ( ) ;
291
+ let count_align = ( ( self . bytes . len ( ) / align_usize) + 1 ) * align_usize;
292
+
293
+ let mut vec_align: Vec < u8 > = Vec :: with_capacity ( count_align) ;
294
+ vec_align. resize ( count_align, 0 ) ;
295
+ assert ! ( vec_align. as_ptr( ) as u64 % self . align. bytes( ) == 0 ) ;
296
+
297
+ vec_align[ ..self . bytes . len ( ) ] . copy_from_slice ( & self . bytes ) ;
298
+ let mut bytes = vec_align. into_boxed_slice ( ) ;
301
299
let mut new_relocations = Vec :: with_capacity ( self . relocations . 0 . len ( ) ) ;
302
300
let ptr_size = cx. data_layout ( ) . pointer_size . bytes_usize ( ) ;
303
301
let endian = cx. data_layout ( ) . endian ;
@@ -310,7 +308,8 @@ impl Allocation {
310
308
write_target_uint ( endian, ptr_bytes, ptr_offset. bytes ( ) . into ( ) ) . unwrap ( ) ;
311
309
new_relocations. push ( ( offset, ptr_prov) ) ;
312
310
}
313
- assert ! ( bytes. as_ptr( ) as u64 % 16 == 0 ) ;
311
+ assert ! ( bytes. as_ptr( ) as u64 % self . align. bytes( ) == 0 ) ;
312
+
314
313
// Create allocation.
315
314
Ok ( Allocation {
316
315
bytes,
0 commit comments