Issue 3223: Second phase of migration to Rust Edition
Reformat via `cargo fmt`
This commit is contained in:
@@ -57,38 +57,41 @@ struct InnerHeap {
|
||||
}
|
||||
|
||||
impl InnerHeap {
|
||||
unsafe fn grow(&mut self) -> bool { unsafe {
|
||||
let new_cap = if self.byte_cap == 0 {
|
||||
256 * 256 * 8
|
||||
} else {
|
||||
2 * self.byte_cap
|
||||
};
|
||||
unsafe fn grow(&mut self) -> bool {
|
||||
unsafe {
|
||||
let new_cap = if self.byte_cap == 0 {
|
||||
256 * 256 * 8
|
||||
} else {
|
||||
2 * self.byte_cap
|
||||
};
|
||||
|
||||
let new_layout =
|
||||
alloc::Layout::from_size_align(new_cap, size_of::<HeapCellValue>()).unwrap();
|
||||
let new_layout =
|
||||
alloc::Layout::from_size_align(new_cap, size_of::<HeapCellValue>()).unwrap();
|
||||
|
||||
assert!(
|
||||
new_layout.size() <= isize::MAX as usize,
|
||||
"Allocation too large. We should probably GC (TODO)"
|
||||
);
|
||||
assert!(
|
||||
new_layout.size() <= isize::MAX as usize,
|
||||
"Allocation too large. We should probably GC (TODO)"
|
||||
);
|
||||
|
||||
let new_ptr = if self.byte_cap == 0 {
|
||||
alloc::alloc(new_layout)
|
||||
} else {
|
||||
let old_layout =
|
||||
alloc::Layout::from_size_align(self.byte_cap, size_of::<HeapCellValue>()).unwrap();
|
||||
alloc::realloc(self.ptr, old_layout, new_layout.size())
|
||||
};
|
||||
let new_ptr = if self.byte_cap == 0 {
|
||||
alloc::alloc(new_layout)
|
||||
} else {
|
||||
let old_layout =
|
||||
alloc::Layout::from_size_align(self.byte_cap, size_of::<HeapCellValue>())
|
||||
.unwrap();
|
||||
alloc::realloc(self.ptr, old_layout, new_layout.size())
|
||||
};
|
||||
|
||||
if !new_ptr.is_null() {
|
||||
self.ptr = new_ptr;
|
||||
self.byte_cap = new_cap;
|
||||
if !new_ptr.is_null() {
|
||||
self.ptr = new_ptr;
|
||||
self.byte_cap = new_cap;
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for Heap {}
|
||||
@@ -101,48 +104,52 @@ pub struct HeapStringScan<'a> {
|
||||
}
|
||||
|
||||
// The heap_slice should be inside the heap
|
||||
unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan<'_> { unsafe {
|
||||
let string_len = heap_slice
|
||||
.iter()
|
||||
.position(|b| *b == 0u8)
|
||||
.unwrap_or(heap_slice.len());
|
||||
let zero_byte_addr = heap_slice.as_ptr().add(string_len);
|
||||
unsafe fn scan_slice_to_str(heap_slice: &[u8]) -> HeapStringScan<'_> {
|
||||
unsafe {
|
||||
let string_len = heap_slice
|
||||
.iter()
|
||||
.position(|b| *b == 0u8)
|
||||
.unwrap_or(heap_slice.len());
|
||||
let zero_byte_addr = heap_slice.as_ptr().add(string_len);
|
||||
|
||||
let sentinel_len = pstr_sentinel_length(zero_byte_addr.addr());
|
||||
let tail_idx = cell_index!(
|
||||
(string_len + sentinel_len).next_multiple_of(ALIGN)
|
||||
+ if sentinel_len <= 1 { heap_index!(1) } else { 0 }
|
||||
);
|
||||
let sentinel_len = pstr_sentinel_length(zero_byte_addr.addr());
|
||||
let tail_idx = cell_index!(
|
||||
(string_len + sentinel_len).next_multiple_of(ALIGN)
|
||||
+ if sentinel_len <= 1 { heap_index!(1) } else { 0 }
|
||||
);
|
||||
|
||||
let str_slice = &heap_slice[..string_len];
|
||||
let str_slice = &heap_slice[..string_len];
|
||||
|
||||
HeapStringScan {
|
||||
string: std::str::from_utf8_unchecked(str_slice),
|
||||
tail_idx,
|
||||
HeapStringScan {
|
||||
string: std::str::from_utf8_unchecked(str_slice),
|
||||
tail_idx,
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
// Same as scan_slice_to_str but assumes that the slice is from the start of a string.
|
||||
// Can be used on strings out of the heap.
|
||||
unsafe fn scan_slice_to_str_from_start(heap_slice: &[u8]) -> HeapStringScan<'_> { unsafe {
|
||||
let string_len = heap_slice
|
||||
.iter()
|
||||
.position(|b| *b == 0u8)
|
||||
.unwrap_or(heap_slice.len());
|
||||
unsafe fn scan_slice_to_str_from_start(heap_slice: &[u8]) -> HeapStringScan<'_> {
|
||||
unsafe {
|
||||
let string_len = heap_slice
|
||||
.iter()
|
||||
.position(|b| *b == 0u8)
|
||||
.unwrap_or(heap_slice.len());
|
||||
|
||||
let sentinel_len = pstr_sentinel_length(string_len);
|
||||
let tail_idx = cell_index!(
|
||||
(string_len + sentinel_len).next_multiple_of(ALIGN)
|
||||
+ if sentinel_len <= 1 { heap_index!(1) } else { 0 }
|
||||
);
|
||||
let sentinel_len = pstr_sentinel_length(string_len);
|
||||
let tail_idx = cell_index!(
|
||||
(string_len + sentinel_len).next_multiple_of(ALIGN)
|
||||
+ if sentinel_len <= 1 { heap_index!(1) } else { 0 }
|
||||
);
|
||||
|
||||
let str_slice = &heap_slice[..string_len];
|
||||
let str_slice = &heap_slice[..string_len];
|
||||
|
||||
HeapStringScan {
|
||||
string: std::str::from_utf8_unchecked(str_slice),
|
||||
tail_idx,
|
||||
HeapStringScan {
|
||||
string: std::str::from_utf8_unchecked(str_slice),
|
||||
tail_idx,
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub(crate) enum PStrContinuable {
|
||||
@@ -450,11 +457,7 @@ fn pstr_sentinel_length(chunk_len: usize) -> usize {
|
||||
let res = chunk_len.next_multiple_of(ALIGN) - chunk_len;
|
||||
|
||||
// No bytes available in last chunk
|
||||
if res == 0 {
|
||||
ALIGN
|
||||
} else {
|
||||
res
|
||||
}
|
||||
if res == 0 { ALIGN } else { res }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
@@ -585,9 +588,9 @@ impl Heap {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
unsafe fn grow(&mut self) -> bool { unsafe {
|
||||
self.inner.grow()
|
||||
}}
|
||||
unsafe fn grow(&mut self) -> bool {
|
||||
unsafe { self.inner.grow() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn resource_error_offset(&self) -> usize {
|
||||
|
||||
Reference in New Issue
Block a user