Skip to content

Commit 823b74c

Browse files
committed
globalize ALIGN_CELL/ALIGN, fix compute_pstr_size
1 parent b5338a1 commit 823b74c

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

src/machine/heap.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use super::MachineState;
1515
use bitvec::prelude::*;
1616
use bitvec::slice::BitSlice;
1717

18+
const ALIGN: usize = Heap::heap_cell_alignment();
19+
1820
#[derive(Debug)]
1921
pub struct Heap {
2022
inner: InnerHeap,
@@ -94,10 +96,9 @@ static RESOURCE_ERROR_OFFSET_INIT: Once = Once::new();
9496
fn scan_slice_to_str(orig_ptr: *const u8, pstr_vec: &BitSlice) -> (&str, usize) {
9597
unsafe {
9698
debug_assert_eq!(pstr_vec[0], true);
97-
const ALIGN_CELL: usize = Heap::heap_cell_alignment();
9899

99100
let tail_cell_offset = pstr_vec[0..].first_zero().unwrap();
100-
let offset = (ALIGN_CELL - orig_ptr.align_offset(ALIGN_CELL)) % 8;
101+
let offset = (ALIGN - orig_ptr.align_offset(ALIGN)) % 8;
101102
let buf_len = heap_index!(tail_cell_offset) - offset;
102103
let slice = std::slice::from_raw_parts(orig_ptr, buf_len);
103104

@@ -342,8 +343,6 @@ impl<'a> ReservedHeapSection<'a> {
342343
let cells_written;
343344
let str_byte_len = src.len();
344345

345-
const ALIGN_CELL: usize = Heap::heap_cell_alignment();
346-
347346
unsafe {
348347
ptr::copy_nonoverlapping(
349348
src.as_ptr(),
@@ -477,8 +476,6 @@ impl<'a> Index<usize> for ReservedHeapSection<'a> {
477476
/// with zeroes, such that `chunk_len + pstr_sentinel_length(chunk_len)` is a
478477
/// multiple of `Heap::heap_cell_alignement()`.
479478
fn pstr_sentinel_length(chunk_len: usize) -> usize {
480-
const ALIGN: usize = Heap::heap_cell_alignment();
481-
482479
let res = chunk_len.next_multiple_of(ALIGN) - chunk_len;
483480

484481
// No bytes available in last chunk
@@ -924,8 +921,7 @@ impl Heap {
924921
// takes a byte offset into the Heap ptr.
925922
#[inline(always)]
926923
pub(crate) const fn neighboring_cell_offset(offset: usize) -> usize {
927-
const ALIGN_CELL: usize = Heap::heap_cell_alignment();
928-
cell_index!((offset & !(ALIGN_CELL - 1)) + ALIGN_CELL)
924+
cell_index!((offset & !(ALIGN - 1)) + ALIGN)
929925
}
930926

931927
#[inline]
@@ -978,10 +974,7 @@ impl Heap {
978974
let (s, tail_loc) = self.scan_slice_to_str(pstr_loc);
979975
let s_len = s.len();
980976

981-
const ALIGN_CELL: usize = Heap::heap_cell_alignment();
982-
983977
let align_offset = pstr_sentinel_length(s_len);
984-
985978
let copy_size = s_len + align_offset;
986979

987980
unsafe {
@@ -1044,10 +1037,8 @@ impl Heap {
10441037
}
10451038

10461039
/// Returns the number of bytes needed to store `src` as a `PStr`.
1047-
/// Assumes the string will be allocated on a ALIGN_CELL-byte boundary.
1040+
/// Assumes the string will be allocated on a ALIGN-byte boundary.
10481041
pub(crate) fn compute_pstr_size(src: &str) -> usize {
1049-
const ALIGN_CELL: usize = Heap::heap_cell_alignment();
1050-
10511042
if src.is_empty() {
10521043
return 0;
10531044
}
@@ -1066,9 +1057,9 @@ impl Heap {
10661057
null_idx += 1;
10671058
}
10681059

1069-
byte_size += null_idx.next_multiple_of(ALIGN_CELL);
1060+
byte_size += null_idx + pstr_sentinel_length(null_idx);
10701061

1071-
if (null_idx + 1) % ALIGN_CELL == 0 {
1062+
if (null_idx + 1) % ALIGN == 0 {
10721063
byte_size += 2 * mem::size_of::<HeapCellValue>();
10731064
} else {
10741065
byte_size += mem::size_of::<HeapCellValue>();

0 commit comments

Comments
 (0)