remove RESOURCE_ERROR_OFFSET_INIT

With it when using multiple Machine in one process only the first would store the pre-allocated error.
Instead Heap.resource_err_loc is now Option<NonZero<usize>> instead of usize using None for uninitialized.
The cell at index 0 should alredy be used by a runtime reserved interstitial cell that is allocated prior. So requiring the offset to be non zero should be fine.
This commit is contained in:
Skgland
2025-11-20 21:44:29 +01:00
committed by Bennet Bleßmann
parent a05dc79505
commit d4f2f7ba2b

View File

@@ -5,9 +5,9 @@ use crate::types::*;
use std::alloc; use std::alloc;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::num::NonZero;
use std::ops::{Bound, Index, IndexMut, Range, RangeBounds}; use std::ops::{Bound, Index, IndexMut, Range, RangeBounds};
use std::ptr; use std::ptr;
use std::sync::Once;
const ALIGN: usize = Heap::heap_cell_alignment(); const ALIGN: usize = Heap::heap_cell_alignment();
@@ -23,7 +23,7 @@ impl AllocError {
#[derive(Debug)] #[derive(Debug)]
pub struct Heap { pub struct Heap {
inner: InnerHeap, inner: InnerHeap,
resource_err_loc: usize, resource_err_loc: Option<NonZero<usize>>,
} }
impl Drop for Heap { impl Drop for Heap {
@@ -94,8 +94,6 @@ impl InnerHeap {
unsafe impl Send for Heap {} unsafe impl Send for Heap {}
unsafe impl Sync for Heap {} unsafe impl Sync for Heap {}
static RESOURCE_ERROR_OFFSET_INIT: Once = Once::new();
#[derive(Debug)] #[derive(Debug)]
pub struct HeapStringScan<'a> { pub struct HeapStringScan<'a> {
pub string: &'a str, pub string: &'a str,
@@ -572,7 +570,7 @@ impl Heap {
byte_len: 0, byte_len: 0,
byte_cap: 0, byte_cap: 0,
}, },
resource_err_loc: 0, resource_err_loc: None,
} }
} }
@@ -594,6 +592,8 @@ impl Heap {
#[inline] #[inline]
fn resource_error_offset(&self) -> usize { fn resource_error_offset(&self) -> usize {
self.resource_err_loc self.resource_err_loc
.expect("`error(resource_error(memory), [])` should be stored at the start of the heap")
.get()
} }
pub(crate) fn with_cell_capacity(cap: usize) -> Result<Self, AllocError> { pub(crate) fn with_cell_capacity(cap: usize) -> Result<Self, AllocError> {
@@ -616,7 +616,7 @@ impl Heap {
byte_cap: heap_index!(cap), byte_cap: heap_index!(cap),
}, },
// pstr_vec: bitvec![], // pstr_vec: bitvec![],
resource_err_loc: 0, resource_err_loc: None,
}) })
} }
} }
@@ -700,7 +700,7 @@ impl Heap {
} }
pub(crate) fn store_resource_error(&mut self) { pub(crate) fn store_resource_error(&mut self) {
RESOURCE_ERROR_OFFSET_INIT.call_once(move || { if self.resource_err_loc.is_none() {
let stub = functor!( let stub = functor!(
atom!("error"), atom!("error"),
[ [
@@ -708,11 +708,14 @@ impl Heap {
atom_as_cell((atom!("[]"))) atom_as_cell((atom!("[]")))
] ]
); );
self.resource_err_loc = cell_index!(self.inner.byte_len);
self.resource_err_loc = Some(NonZero::new(cell_index!(self.inner.byte_len)).expect(
"index 0 should already be taken by an interstitial cell reserved by the runtime",
));
let mut writer = Heap::functor_writer(stub); let mut writer = Heap::functor_writer(stub);
writer(self).unwrap(); writer(self).unwrap();
}); }
} }
#[inline] #[inline]