411
src/arena.rs
411
src/arena.rs
@@ -1,30 +1,39 @@
|
||||
#![allow(clippy::new_without_default)] // annotating structs annotated with #[bitfield] doesn't work
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
use crate::http::{HttpListener, HttpResponse};
|
||||
use crate::machine::loader::LiveLoadState;
|
||||
use crate::machine::machine_indices::*;
|
||||
use crate::machine::streams::*;
|
||||
use crate::raw_block::*;
|
||||
use crate::rcu::Rcu;
|
||||
use crate::rcu::RcuRef;
|
||||
use crate::read::*;
|
||||
use crate::types::UntypedArenaPtr;
|
||||
|
||||
use crate::parser::dashu::{Integer, Rational};
|
||||
use arcu::atomic::Arcu;
|
||||
use arcu::epoch_counters::GlobalEpochCounterPool;
|
||||
use arcu::rcu_ref::RcuRef;
|
||||
use arcu::Rcu;
|
||||
use ordered_float::OrderedFloat;
|
||||
|
||||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::fmt::Debug;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::mem;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::net::TcpListener;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::ptr;
|
||||
use std::ptr::addr_of_mut;
|
||||
use std::ptr::NonNull;
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! arena_alloc {
|
||||
($e:expr, $arena:expr) => {{
|
||||
let result = $e;
|
||||
ArenaAllocated::alloc($arena, result)
|
||||
$crate::arena::AllocateInArena::arena_allocate(result, $arena)
|
||||
}};
|
||||
}
|
||||
|
||||
@@ -36,29 +45,18 @@ macro_rules! float_alloc {
|
||||
}};
|
||||
}
|
||||
|
||||
pub fn header_offset_from_payload<Payload: Sized>() -> usize {
|
||||
let payload_offset = mem::offset_of!(TypedAllocSlab<Payload>, payload);
|
||||
let slab_offset = mem::offset_of!(TypedAllocSlab<Payload>, slab);
|
||||
pub fn header_offset_from_payload<T: ?Sized + ArenaAllocated>() -> usize
|
||||
where
|
||||
T::Payload: Sized,
|
||||
{
|
||||
let payload_offset = mem::offset_of!(TypedAllocSlab<T>, payload);
|
||||
let slab_offset = mem::offset_of!(TypedAllocSlab<T>, slab);
|
||||
let header_offset = slab_offset + mem::offset_of!(AllocSlab, header);
|
||||
|
||||
debug_assert!(payload_offset > header_offset);
|
||||
payload_offset - header_offset
|
||||
}
|
||||
|
||||
pub fn ptr_to_allocated<Payload: ArenaAllocated>(slab: &mut AllocSlab) -> TypedArenaPtr<Payload> {
|
||||
let typed_slab: &mut TypedAllocSlab<Payload> = unsafe { mem::transmute(slab) };
|
||||
typed_slab.to_typed_arena_ptr()
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! gen_ptr_to_allocated {
|
||||
($payload: ty) => {
|
||||
fn ptr_to_allocated(slab: &mut AllocSlab) -> TypedArenaPtr<$payload> {
|
||||
ptr_to_allocated::<$payload>(slab)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::Weak;
|
||||
@@ -68,19 +66,8 @@ const F64_TABLE_ALIGN: usize = 8;
|
||||
|
||||
#[inline(always)]
|
||||
fn global_f64table() -> &'static RwLock<Weak<F64Table>> {
|
||||
#[cfg(feature = "rust_beta_channel")]
|
||||
{
|
||||
// const Weak::new will be stabilized in 1.73 which is currently in beta,
|
||||
// till then we need a OnceLock for initialization
|
||||
static GLOBAL_ATOM_TABLE: RwLock<Weak<F64Table>> = RwLock::const_new(Weak::new());
|
||||
&GLOBAL_ATOM_TABLE
|
||||
}
|
||||
#[cfg(not(feature = "rust_beta_channel"))]
|
||||
{
|
||||
use std::sync::OnceLock;
|
||||
static GLOBAL_ATOM_TABLE: OnceLock<RwLock<Weak<F64Table>>> = OnceLock::new();
|
||||
GLOBAL_ATOM_TABLE.get_or_init(|| RwLock::new(Weak::new()))
|
||||
}
|
||||
static GLOBAL_ATOM_TABLE: RwLock<Weak<F64Table>> = RwLock::new(Weak::new());
|
||||
&GLOBAL_ATOM_TABLE
|
||||
}
|
||||
|
||||
impl RawBlockTraits for F64Table {
|
||||
@@ -97,7 +84,7 @@ impl RawBlockTraits for F64Table {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct F64Table {
|
||||
block: Rcu<RawBlock<F64Table>>,
|
||||
block: Arcu<RawBlock<F64Table>, GlobalEpochCounterPool>,
|
||||
update: Mutex<()>,
|
||||
}
|
||||
|
||||
@@ -111,7 +98,7 @@ pub fn lookup_float(
|
||||
.upgrade()
|
||||
.expect("We should only be looking up floats while there is a float table");
|
||||
|
||||
RcuRef::try_map(f64table.block.active_epoch(), |raw_block| unsafe {
|
||||
RcuRef::try_map(f64table.block.read(), |raw_block| unsafe {
|
||||
raw_block
|
||||
.base
|
||||
.add(offset.0)
|
||||
@@ -136,7 +123,7 @@ impl F64Table {
|
||||
atom_table
|
||||
} else {
|
||||
let atom_table = Arc::new(Self {
|
||||
block: Rcu::new(RawBlock::new()),
|
||||
block: Arcu::new(RawBlock::new(), GlobalEpochCounterPool),
|
||||
update: Mutex::new(()),
|
||||
});
|
||||
*guard = Arc::downgrade(&atom_table);
|
||||
@@ -151,7 +138,7 @@ impl F64Table {
|
||||
|
||||
// we don't have an index table for lookups as AtomTable does so
|
||||
// just get the epoch after we take the upgrade lock
|
||||
let mut block_epoch = self.block.active_epoch();
|
||||
let mut block_epoch = self.block.read();
|
||||
|
||||
let mut ptr;
|
||||
|
||||
@@ -161,7 +148,7 @@ impl F64Table {
|
||||
if ptr.is_null() {
|
||||
let new_block = block_epoch.grow_new().unwrap();
|
||||
self.block.replace(new_block);
|
||||
block_epoch = self.block.active_epoch();
|
||||
block_epoch = self.block.read();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -210,6 +197,7 @@ pub enum ArenaHeaderTag {
|
||||
}
|
||||
|
||||
#[bitfield]
|
||||
#[repr(align(8))]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct ArenaHeader {
|
||||
#[allow(dead_code)]
|
||||
@@ -236,76 +224,96 @@ impl ArenaHeader {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TypedArenaPtr<T: ?Sized>(ptr::NonNull<T>);
|
||||
pub struct TypedArenaPtr<T: ?Sized + ArenaAllocated>(ptr::NonNull<T::Payload>);
|
||||
|
||||
impl<T: ?Sized + PartialOrd> PartialOrd for TypedArenaPtr<T> {
|
||||
impl<T: ?Sized + ArenaAllocated> PartialOrd for TypedArenaPtr<T>
|
||||
where
|
||||
T::Payload: PartialOrd,
|
||||
{
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
(**self).partial_cmp(&**other)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + PartialEq> PartialEq for TypedArenaPtr<T> {
|
||||
impl<T: ?Sized + ArenaAllocated> PartialEq for TypedArenaPtr<T>
|
||||
where
|
||||
T::Payload: PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &TypedArenaPtr<T>) -> bool {
|
||||
self.0 == other.0 || **self == **other
|
||||
std::ptr::addr_eq(self.0.as_ptr(), other.0.as_ptr()) || **self == **other
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + PartialEq> Eq for TypedArenaPtr<T> {}
|
||||
impl<T: ?Sized + ArenaAllocated> Eq for TypedArenaPtr<T> where T::Payload: Eq {}
|
||||
|
||||
impl<T: ?Sized + Ord> Ord for TypedArenaPtr<T> {
|
||||
impl<T: ?Sized + ArenaAllocated> Ord for TypedArenaPtr<T>
|
||||
where
|
||||
T::Payload: Ord,
|
||||
{
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
(**self).cmp(&**other)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Hash> Hash for TypedArenaPtr<T> {
|
||||
impl<T: ?Sized + ArenaAllocated> Hash for TypedArenaPtr<T>
|
||||
where
|
||||
T::Payload: Hash,
|
||||
{
|
||||
#[inline(always)]
|
||||
fn hash<H: Hasher>(&self, hasher: &mut H) {
|
||||
(self as &T).hash(hasher)
|
||||
(self as &T::Payload).hash(hasher)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Clone for TypedArenaPtr<T> {
|
||||
impl<T: ?Sized + ArenaAllocated> Clone for TypedArenaPtr<T> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Copy for TypedArenaPtr<T> {}
|
||||
impl<T: ?Sized + ArenaAllocated> Copy for TypedArenaPtr<T> {}
|
||||
|
||||
impl<T: ?Sized> Deref for TypedArenaPtr<T> {
|
||||
type Target = T;
|
||||
impl<T: ?Sized + ArenaAllocated> Deref for TypedArenaPtr<T> {
|
||||
type Target = T::Payload;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { self.0.as_ref() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> DerefMut for TypedArenaPtr<T> {
|
||||
impl<T: ?Sized + ArenaAllocated> DerefMut for TypedArenaPtr<T> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
unsafe { self.0.as_mut() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Display> fmt::Display for TypedArenaPtr<T> {
|
||||
impl<T: ArenaAllocated> fmt::Display for TypedArenaPtr<T>
|
||||
where
|
||||
T::Payload: fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", **self)
|
||||
write!(f, "{}", (self as &T::Payload))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + ArenaAllocated> TypedArenaPtr<T> {
|
||||
// data must be allocated in the arena already.
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[inline]
|
||||
pub const fn new(data: *mut T) -> Self {
|
||||
unsafe { TypedArenaPtr(ptr::NonNull::new_unchecked(data)) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_ptr(&self) -> *mut T {
|
||||
pub fn as_ptr(&self) -> *mut T::Payload {
|
||||
self.0.as_ptr()
|
||||
}
|
||||
}
|
||||
|
||||
impl<P, T: ?Sized + ArenaAllocated<Payload = ManuallyDrop<P>>> TypedArenaPtr<T> {
|
||||
pub fn drop_payload(&mut self) {
|
||||
self.set_tag(ArenaHeaderTag::Dropped);
|
||||
unsafe { ManuallyDrop::drop(&mut *self.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + ArenaAllocated> TypedArenaPtr<T>
|
||||
where
|
||||
T::Payload: Sized,
|
||||
{
|
||||
#[inline]
|
||||
pub fn header_ptr(&self) -> *const ArenaHeader {
|
||||
unsafe { self.as_ptr().byte_sub(T::header_offset_from_payload()) as *const _ }
|
||||
@@ -348,36 +356,77 @@ impl<T: ?Sized + ArenaAllocated> TypedArenaPtr<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ArenaAllocated: Sized {
|
||||
type PtrToAllocated;
|
||||
pub trait AllocateInArena<AllocFor>
|
||||
where
|
||||
AllocFor: ArenaAllocated,
|
||||
{
|
||||
fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<AllocFor>;
|
||||
}
|
||||
|
||||
impl<P, T: ArenaAllocated<Payload = P>> AllocateInArena<T> for P {
|
||||
fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<T> {
|
||||
T::alloc(arena, self)
|
||||
}
|
||||
}
|
||||
|
||||
/* apparently this overlaps the planket impl above somehow
|
||||
impl<P, T: ArenaAllocated<Payload = ManuallyDrop<P>>> AllocateInArena<T> for P {
|
||||
fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<T> {
|
||||
T::alloc(arena, ManuallyDrop::new(self))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub trait ArenaAllocated {
|
||||
type Payload: ?Sized;
|
||||
|
||||
fn tag() -> ArenaHeaderTag;
|
||||
fn ptr_to_allocated(slab: &mut AllocSlab) -> Self::PtrToAllocated;
|
||||
|
||||
fn header_offset_from_payload() -> usize {
|
||||
fn header_offset_from_payload() -> usize
|
||||
where
|
||||
Self::Payload: Sized,
|
||||
{
|
||||
header_offset_from_payload::<Self>()
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// - the caller must guarantee that the pointee type of UntypedArenaPtr is Self
|
||||
/// - the pointer must be non-null
|
||||
unsafe fn typed_ptr(ptr: UntypedArenaPtr) -> TypedArenaPtr<Self>
|
||||
where
|
||||
Self::Payload: Sized,
|
||||
{
|
||||
TypedArenaPtr(NonNull::new_unchecked(
|
||||
ptr.payload_offset().cast_mut().cast::<Self::Payload>(),
|
||||
))
|
||||
}
|
||||
|
||||
#[allow(clippy::missing_safety_doc)]
|
||||
fn alloc(arena: &mut Arena, value: Self) -> Self::PtrToAllocated {
|
||||
fn alloc(arena: &mut Arena, value: Self::Payload) -> TypedArenaPtr<Self>
|
||||
where
|
||||
Self::Payload: Sized,
|
||||
{
|
||||
let size = mem::size_of::<TypedAllocSlab<Self>>();
|
||||
let slab = Box::new(TypedAllocSlab {
|
||||
slab: AllocSlab {
|
||||
next: arena.base.take(),
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
_padding: 0,
|
||||
header: ArenaHeader::build_with(size as u64, Self::tag()),
|
||||
},
|
||||
payload: value,
|
||||
});
|
||||
|
||||
let mut untyped_slab = unsafe { Box::from_raw(Box::into_raw(slab) as *mut AllocSlab) };
|
||||
let allocated_ptr = Self::ptr_to_allocated(untyped_slab.as_mut());
|
||||
let (allocated_ptr, untyped_slab) = slab.to_untyped();
|
||||
|
||||
arena.base = Some(untyped_slab);
|
||||
|
||||
allocated_ptr
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// - ptr points to an allocated slab of the correct kind
|
||||
unsafe fn dealloc(ptr: NonNull<TypedAllocSlab<Self>>) {
|
||||
drop(unsafe { Box::from_raw(ptr.as_ptr()) });
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -512,10 +561,7 @@ impl fmt::Display for F64Offset {
|
||||
}
|
||||
|
||||
impl ArenaAllocated for Integer {
|
||||
type PtrToAllocated = TypedArenaPtr<Integer>;
|
||||
|
||||
gen_ptr_to_allocated!(Integer);
|
||||
|
||||
type Payload = Self;
|
||||
#[inline]
|
||||
fn tag() -> ArenaHeaderTag {
|
||||
ArenaHeaderTag::Integer
|
||||
@@ -523,32 +569,50 @@ impl ArenaAllocated for Integer {
|
||||
}
|
||||
|
||||
impl ArenaAllocated for Rational {
|
||||
type PtrToAllocated = TypedArenaPtr<Rational>;
|
||||
|
||||
gen_ptr_to_allocated!(Rational);
|
||||
|
||||
type Payload = Self;
|
||||
#[inline]
|
||||
fn tag() -> ArenaHeaderTag {
|
||||
ArenaHeaderTag::Rational
|
||||
}
|
||||
}
|
||||
|
||||
impl AllocateInArena<LiveLoadState> for LiveLoadState {
|
||||
fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<LiveLoadState> {
|
||||
LiveLoadState::alloc(arena, ManuallyDrop::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ArenaAllocated for LiveLoadState {
|
||||
type PtrToAllocated = TypedArenaPtr<LiveLoadState>;
|
||||
|
||||
gen_ptr_to_allocated!(LiveLoadState);
|
||||
|
||||
type Payload = ManuallyDrop<Self>;
|
||||
#[inline]
|
||||
fn tag() -> ArenaHeaderTag {
|
||||
ArenaHeaderTag::LiveLoadState
|
||||
}
|
||||
|
||||
unsafe fn dealloc(ptr: NonNull<TypedAllocSlab<Self>>) {
|
||||
let mut slab = unsafe { Box::from_raw(ptr.as_ptr()) };
|
||||
|
||||
match slab.tag() {
|
||||
ArenaHeaderTag::LiveLoadState | ArenaHeaderTag::InactiveLoadState => {
|
||||
unsafe { ManuallyDrop::drop(&mut slab.payload) };
|
||||
}
|
||||
ArenaHeaderTag::Dropped => {}
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
drop(slab);
|
||||
}
|
||||
}
|
||||
|
||||
impl AllocateInArena<TcpListener> for TcpListener {
|
||||
fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<TcpListener> {
|
||||
TcpListener::alloc(arena, ManuallyDrop::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ArenaAllocated for TcpListener {
|
||||
type PtrToAllocated = TypedArenaPtr<TcpListener>;
|
||||
|
||||
gen_ptr_to_allocated!(TcpListener);
|
||||
|
||||
type Payload = ManuallyDrop<Self>;
|
||||
#[inline]
|
||||
fn tag() -> ArenaHeaderTag {
|
||||
ArenaHeaderTag::TcpListener
|
||||
@@ -557,10 +621,7 @@ impl ArenaAllocated for TcpListener {
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
impl ArenaAllocated for HttpListener {
|
||||
type PtrToAllocated = TypedArenaPtr<HttpListener>;
|
||||
|
||||
gen_ptr_to_allocated!(HttpListener);
|
||||
|
||||
type Payload = Self;
|
||||
#[inline]
|
||||
fn tag() -> ArenaHeaderTag {
|
||||
ArenaHeaderTag::HttpListener
|
||||
@@ -569,10 +630,7 @@ impl ArenaAllocated for HttpListener {
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
impl ArenaAllocated for HttpResponse {
|
||||
type PtrToAllocated = TypedArenaPtr<HttpResponse>;
|
||||
|
||||
gen_ptr_to_allocated!(HttpResponse);
|
||||
|
||||
type Payload = Self;
|
||||
#[inline]
|
||||
fn tag() -> ArenaHeaderTag {
|
||||
ArenaHeaderTag::HttpResponse
|
||||
@@ -580,65 +638,147 @@ impl ArenaAllocated for HttpResponse {
|
||||
}
|
||||
|
||||
impl ArenaAllocated for IndexPtr {
|
||||
type PtrToAllocated = TypedArenaPtr<IndexPtr>;
|
||||
|
||||
type Payload = Self;
|
||||
#[inline]
|
||||
fn tag() -> ArenaHeaderTag {
|
||||
ArenaHeaderTag::IndexPtrUndefined
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn ptr_to_allocated(slab: &mut AllocSlab) -> Self::PtrToAllocated {
|
||||
TypedArenaPtr::new(ptr::addr_of_mut!(slab.header) as *mut _)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn header_offset_from_payload() -> usize {
|
||||
0
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// - the caller must guarantee that the pointee type of UntypedArenaPtr is T
|
||||
/// - the pointer must be non-null
|
||||
unsafe fn typed_ptr(ptr: UntypedArenaPtr) -> TypedArenaPtr<Self> {
|
||||
TypedArenaPtr(NonNull::new_unchecked(
|
||||
ptr.get_ptr().cast_mut().cast::<IndexPtr>(),
|
||||
))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn alloc(arena: &mut Arena, value: Self) -> Self::PtrToAllocated {
|
||||
let mut slab = Box::new(AllocSlab {
|
||||
fn alloc(arena: &mut Arena, value: Self) -> TypedArenaPtr<Self> {
|
||||
let slab = Box::new(IndexPtrSlab {
|
||||
next: arena.base.take(),
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
_padding: 0,
|
||||
header: unsafe { mem::transmute(value) },
|
||||
index_ptr: value,
|
||||
});
|
||||
|
||||
let allocated_ptr =
|
||||
TypedArenaPtr::new(unsafe { mem::transmute(ptr::addr_of_mut!(slab.header)) });
|
||||
arena.base = Some(slab);
|
||||
let (allocated_ptr, untyped_slab) = slab.to_untyped();
|
||||
arena.base = Some(untyped_slab);
|
||||
allocated_ptr
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// - ptr points to an allocated slab of the correct kind
|
||||
unsafe fn dealloc(ptr: NonNull<TypedAllocSlab<Self>>) {
|
||||
drop(unsafe { Box::from_raw(ptr.as_ptr().cast::<IndexPtrSlab>()) });
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Debug)]
|
||||
pub struct AllocSlab {
|
||||
next: Option<Box<AllocSlab>>,
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
_padding: u32,
|
||||
next: Option<UntypedArenaSlab>,
|
||||
header: ArenaHeader,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TypedAllocSlab<Payload> {
|
||||
slab: AllocSlab,
|
||||
payload: Payload,
|
||||
#[derive(Debug)]
|
||||
pub struct IndexPtrSlab {
|
||||
next: Option<UntypedArenaSlab>,
|
||||
index_ptr: IndexPtr,
|
||||
}
|
||||
|
||||
impl<Payload: ArenaAllocated> TypedAllocSlab<Payload> {
|
||||
const _: () = {
|
||||
if std::mem::align_of::<AllocSlab>() < std::mem::align_of::<*const ()>() {
|
||||
panic!("alignment of AllocSlab is too low");
|
||||
}
|
||||
|
||||
if std::mem::offset_of!(AllocSlab, header) % std::mem::align_of::<*const ()>() != 0 {
|
||||
panic!("alignment of header not a multiple of pointers alignment");
|
||||
}
|
||||
|
||||
if std::mem::offset_of!(AllocSlab, header) != std::mem::offset_of!(IndexPtrSlab, index_ptr) {
|
||||
panic!("IndexPtrSlab.index_ptr and AllocSlab.header are at different offsets");
|
||||
}
|
||||
};
|
||||
|
||||
impl IndexPtrSlab {
|
||||
#[inline]
|
||||
pub fn to_typed_arena_ptr(&mut self) -> TypedArenaPtr<Payload> {
|
||||
TypedArenaPtr::new(&mut self.payload as *mut _)
|
||||
pub fn to_untyped(self: Box<Self>) -> (TypedArenaPtr<IndexPtr>, UntypedArenaSlab) {
|
||||
let raw_box = Box::into_raw(self);
|
||||
|
||||
// safety: the pointer from Box::into_raw fullfills addr_of_mut's saftey requirements
|
||||
let index_ptr_ptr = unsafe { ptr::addr_of_mut!((*raw_box).index_ptr) };
|
||||
let allocated_ptr = TypedArenaPtr(
|
||||
// safety: the pointer points into a valid allocation so it is non null
|
||||
unsafe { NonNull::new_unchecked(index_ptr_ptr) },
|
||||
);
|
||||
|
||||
let untyped_arena = UntypedArenaSlab {
|
||||
// safety: pointer from Box::into_raw is never null
|
||||
slab: unsafe { NonNull::new_unchecked(raw_box.cast::<AllocSlab>()) },
|
||||
tag: <IndexPtr as ArenaAllocated>::tag(),
|
||||
};
|
||||
|
||||
(allocated_ptr, untyped_arena)
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct TypedAllocSlab<T: ?Sized + ArenaAllocated> {
|
||||
slab: AllocSlab,
|
||||
payload: T::Payload,
|
||||
}
|
||||
|
||||
impl<T: ?Sized + ArenaAllocated> TypedAllocSlab<T> {
|
||||
pub fn tag(&self) -> ArenaHeaderTag {
|
||||
self.slab.header.tag()
|
||||
}
|
||||
|
||||
pub fn payload(&mut self) -> &mut T::Payload {
|
||||
&mut self.payload
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_untyped(self: Box<Self>) -> (TypedArenaPtr<T>, UntypedArenaSlab) {
|
||||
let raw_box = Box::into_raw(self);
|
||||
|
||||
// safety: the pointer from Box::into_raw fullfills addr_of_mut's saftey requirements
|
||||
let payload_ptr = unsafe { addr_of_mut!((*raw_box).payload) };
|
||||
|
||||
(
|
||||
TypedArenaPtr(unsafe {
|
||||
// safety: the pointer points into a valid allocation so it is non null
|
||||
ptr::NonNull::new_unchecked(payload_ptr)
|
||||
}),
|
||||
UntypedArenaSlab {
|
||||
// safety: pointer from Box::into_raw is never null
|
||||
slab: unsafe { NonNull::new_unchecked(raw_box.cast::<AllocSlab>()) },
|
||||
tag: T::tag(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UntypedArenaSlab {
|
||||
slab: NonNull<AllocSlab>,
|
||||
tag: ArenaHeaderTag,
|
||||
}
|
||||
|
||||
impl Drop for UntypedArenaSlab {
|
||||
fn drop(&mut self) {
|
||||
unsafe { drop_slab_in_place(self.slab, self.tag) };
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Arena {
|
||||
base: Option<Box<AllocSlab>>,
|
||||
base: Option<UntypedArenaSlab>,
|
||||
pub f64_tbl: Arc<F64Table>,
|
||||
}
|
||||
|
||||
@@ -656,15 +796,14 @@ impl Arena {
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn drop_slab_in_place(value: &mut AllocSlab) {
|
||||
unsafe fn drop_slab_in_place(value: NonNull<AllocSlab>, tag: ArenaHeaderTag) {
|
||||
macro_rules! drop_typed_slab_in_place {
|
||||
($payload: ty, $value: expr) => {
|
||||
let slab: &mut TypedAllocSlab<$payload> = mem::transmute($value);
|
||||
ptr::drop_in_place(&mut slab.payload);
|
||||
<$payload as ArenaAllocated>::dealloc($value.cast::<TypedAllocSlab<$payload>>())
|
||||
};
|
||||
}
|
||||
|
||||
match value.header.tag() {
|
||||
match tag {
|
||||
ArenaHeaderTag::Integer => {
|
||||
drop_typed_slab_in_place!(Integer, value);
|
||||
}
|
||||
@@ -722,28 +861,32 @@ unsafe fn drop_slab_in_place(value: &mut AllocSlab) {
|
||||
ArenaHeaderTag::StandardErrorStream => {
|
||||
drop_typed_slab_in_place!(StandardErrorStream, value);
|
||||
}
|
||||
ArenaHeaderTag::NullStream
|
||||
| ArenaHeaderTag::IndexPtrUndefined
|
||||
ArenaHeaderTag::IndexPtrUndefined
|
||||
| ArenaHeaderTag::IndexPtrDynamicUndefined
|
||||
| ArenaHeaderTag::IndexPtrDynamicIndex
|
||||
| ArenaHeaderTag::IndexPtrIndex => {}
|
||||
| ArenaHeaderTag::IndexPtrIndex => {
|
||||
drop_typed_slab_in_place!(IndexPtr, value);
|
||||
}
|
||||
ArenaHeaderTag::NullStream => {
|
||||
unreachable!("NullStream is never arena allocated!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Arena {
|
||||
fn drop(&mut self) {
|
||||
// we un-nest UntypedArenaSlab to prevent stackoverflow due to the recursive drop
|
||||
|
||||
let mut ptr = self.base.take();
|
||||
|
||||
while let Some(mut slab) = ptr {
|
||||
unsafe {
|
||||
drop_slab_in_place(&mut slab);
|
||||
ptr = slab.next;
|
||||
}
|
||||
ptr = unsafe { slab.slab.as_mut() }.next.take();
|
||||
drop(slab);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const_assert!(mem::size_of::<AllocSlab>() == 16);
|
||||
const_assert!(mem::size_of::<AllocSlab>() <= 24);
|
||||
const_assert!(mem::size_of::<OrderedFloat<f64>>() == 8);
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -781,7 +924,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
fn heap_cell_value_const_cast() {
|
||||
let mut wam = MockWAM::new();
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
@@ -825,7 +967,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on arena.rs UB")]
|
||||
fn heap_put_literal_tests() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![allow(clippy::new_without_default)] // annotating structs annotated with #[bitfield] doesn't work
|
||||
|
||||
use crate::allocator::*;
|
||||
use crate::arena::*;
|
||||
use crate::atom_table::*;
|
||||
@@ -166,7 +168,7 @@ fn push_literal(interm: &mut Vec<ArithmeticTerm>, c: &Literal) -> Result<(), Ari
|
||||
Number::Float(OrderedFloat(std::f64::consts::PI)),
|
||||
)),
|
||||
Literal::Atom(name) if name == &atom!("epsilon") => interm.push(ArithmeticTerm::Number(
|
||||
Number::Float(OrderedFloat(std::f64::EPSILON)),
|
||||
Number::Float(OrderedFloat(f64::EPSILON)),
|
||||
)),
|
||||
_ => return Err(ArithmeticError::NonEvaluableFunctor(*c, 0)),
|
||||
}
|
||||
@@ -545,26 +547,8 @@ impl PartialEq for Number {
|
||||
(&Number::Float(n1), Number::Integer(ref n2)) => {
|
||||
n1.eq(&OrderedFloat(n2.to_f64().value()))
|
||||
}
|
||||
(Number::Integer(ref n1), Number::Rational(ref n2)) => {
|
||||
#[cfg(feature = "num")]
|
||||
{
|
||||
&Rational::from(&**n1) == &**n2
|
||||
}
|
||||
#[cfg(not(feature = "num"))]
|
||||
{
|
||||
n1.num_eq(&**n2)
|
||||
}
|
||||
}
|
||||
(Number::Rational(ref n1), Number::Integer(ref n2)) => {
|
||||
#[cfg(feature = "num")]
|
||||
{
|
||||
n1 == &Rational::from(&**n2)
|
||||
}
|
||||
#[cfg(not(feature = "num"))]
|
||||
{
|
||||
n1.num_eq(&**n2)
|
||||
}
|
||||
}
|
||||
(Number::Integer(ref n1), Number::Rational(ref n2)) => n1.num_eq(&**n2),
|
||||
(Number::Rational(ref n1), Number::Integer(ref n2)) => n1.num_eq(&**n2),
|
||||
(Number::Rational(ref n1), &Number::Float(n2)) => {
|
||||
OrderedFloat(n1.to_f64().value()).eq(&n2)
|
||||
}
|
||||
@@ -643,24 +627,10 @@ impl Ord for Number {
|
||||
n1.cmp(&OrderedFloat(n2.to_f64().value()))
|
||||
}
|
||||
(&Number::Integer(n1), &Number::Rational(n2)) => {
|
||||
#[cfg(feature = "num")]
|
||||
{
|
||||
Rational::from(&**n1).cmp(n2)
|
||||
}
|
||||
#[cfg(not(feature = "num"))]
|
||||
{
|
||||
(*n1).num_partial_cmp(&*n2).unwrap_or(Ordering::Less)
|
||||
}
|
||||
(*n1).num_partial_cmp(&*n2).unwrap_or(Ordering::Less)
|
||||
}
|
||||
(&Number::Rational(n1), &Number::Integer(n2)) => {
|
||||
#[cfg(feature = "num")]
|
||||
{
|
||||
(&**n1).cmp(&Rational::from(&**n2))
|
||||
}
|
||||
#[cfg(not(feature = "num"))]
|
||||
{
|
||||
(*n1).num_partial_cmp(&*n2).unwrap_or(Ordering::Less)
|
||||
}
|
||||
(*n1).num_partial_cmp(&*n2).unwrap_or(Ordering::Less)
|
||||
}
|
||||
(&Number::Rational(n1), &Number::Float(n2)) => {
|
||||
OrderedFloat(n1.to_f64().value()).cmp(&n2)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#![allow(clippy::new_without_default)] // annotating structs annotated with #[bitfield] doesn't work
|
||||
|
||||
use crate::parser::ast::MAX_ARITY;
|
||||
use crate::raw_block::*;
|
||||
use crate::rcu::{Rcu, RcuRef};
|
||||
use crate::types::*;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
@@ -8,13 +9,16 @@ use std::hash::{Hash, Hasher};
|
||||
use std::mem;
|
||||
use std::ops::Deref;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
use std::str;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::RwLock;
|
||||
use std::sync::Weak;
|
||||
|
||||
use arcu::atomic::Arcu;
|
||||
use arcu::epoch_counters::GlobalEpochCounterPool;
|
||||
use arcu::rcu_ref::RcuRef;
|
||||
use arcu::Rcu;
|
||||
use indexmap::IndexSet;
|
||||
|
||||
use scryer_modular_bitfield::prelude::*;
|
||||
@@ -57,19 +61,8 @@ const ATOM_TABLE_ALIGN: usize = 8;
|
||||
|
||||
#[inline(always)]
|
||||
fn global_atom_table() -> &'static RwLock<Weak<AtomTable>> {
|
||||
#[cfg(feature = "rust_beta_channel")]
|
||||
{
|
||||
// const Weak::new will be stabilized in 1.73 which is currently in beta,
|
||||
// till then we need a OnceLock for initialization
|
||||
static GLOBAL_ATOM_TABLE: RwLock<Weak<AtomTable>> = RwLock::const_new(Weak::new());
|
||||
&GLOBAL_ATOM_TABLE
|
||||
}
|
||||
#[cfg(not(feature = "rust_beta_channel"))]
|
||||
{
|
||||
use std::sync::OnceLock;
|
||||
static GLOBAL_ATOM_TABLE: OnceLock<RwLock<Weak<AtomTable>>> = OnceLock::new();
|
||||
GLOBAL_ATOM_TABLE.get_or_init(|| RwLock::new(Weak::new()))
|
||||
}
|
||||
static GLOBAL_ATOM_TABLE: RwLock<Weak<AtomTable>> = RwLock::new(Weak::new());
|
||||
&GLOBAL_ATOM_TABLE
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -99,6 +92,12 @@ struct AtomHeader {
|
||||
padding: B13,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct AtomData {
|
||||
header: AtomHeader,
|
||||
data: str,
|
||||
}
|
||||
|
||||
impl AtomHeader {
|
||||
fn build_with(len: u64) -> Self {
|
||||
AtomHeader::new().with_len(len).with_m(false)
|
||||
@@ -177,19 +176,23 @@ impl Atom {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn as_ptr(self) -> Option<AtomTableRef<u8>> {
|
||||
pub fn as_ptr(self) -> Option<AtomTableRef<AtomData>> {
|
||||
if self.is_static() {
|
||||
None
|
||||
} else {
|
||||
let atom_table =
|
||||
arc_atom_table().expect("We should only have an Atom while there is an AtomTable");
|
||||
unsafe {
|
||||
AtomTableRef::try_map(atom_table.buf(), |buf| {
|
||||
(buf as *const u8)
|
||||
.add((self.index as usize) - (STRINGS.len() << 3))
|
||||
.as_ref()
|
||||
})
|
||||
}
|
||||
|
||||
AtomTableRef::try_map(atom_table.inner.read(), |buf| unsafe {
|
||||
let ptr = buf
|
||||
.block
|
||||
.base
|
||||
.add((self.index as usize) - (STRINGS.len() << 3));
|
||||
// TODO use std::ptr::from_raw_parts instead when feature ptr_metadata is stable rust-lang/rust#81513
|
||||
let atom_data = &*(std::ptr::slice_from_raw_parts(ptr, 0) as *const AtomData);
|
||||
let len = atom_data.header.len();
|
||||
Some(&*(std::ptr::slice_from_raw_parts(ptr, len as usize) as *const AtomData))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,9 +206,8 @@ impl Atom {
|
||||
if self.is_static() {
|
||||
STRINGS[(self.index >> 3) as usize].len()
|
||||
} else {
|
||||
let ptr = self.as_ptr().unwrap();
|
||||
let ptr = ptr.deref() as *const u8 as *const AtomHeader;
|
||||
unsafe { ptr::read(ptr) }.len() as _
|
||||
let len: u64 = self.as_ptr().unwrap().header.len();
|
||||
len as usize
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,15 +239,7 @@ impl Atom {
|
||||
if self.is_static() {
|
||||
AtomString::Static(STRINGS[(self.index >> 3) as usize])
|
||||
} else if let Some(ptr) = self.as_ptr() {
|
||||
AtomString::Dynamic(AtomTableRef::map(ptr, |ptr| {
|
||||
let header =
|
||||
// Miri seems to hit this line a lot
|
||||
unsafe { ptr::read::<AtomHeader>(ptr as *const u8 as *const AtomHeader) };
|
||||
let len = header.len() as usize;
|
||||
let buf = unsafe { (ptr as *const u8).add(mem::size_of::<AtomHeader>()) };
|
||||
|
||||
unsafe { str::from_utf8_unchecked(slice::from_raw_parts(buf, len)) }
|
||||
}))
|
||||
AtomString::Dynamic(AtomTableRef::map(ptr, |ptr| &ptr.data))
|
||||
} else {
|
||||
AtomString::Static(STRINGS[(self.index >> 3) as usize])
|
||||
}
|
||||
@@ -287,17 +281,17 @@ impl Ord for Atom {
|
||||
#[derive(Debug)]
|
||||
pub struct InnerAtomTable {
|
||||
block: RawBlock<AtomTable>,
|
||||
pub table: Rcu<IndexSet<Atom>>,
|
||||
pub table: Arcu<IndexSet<Atom>, GlobalEpochCounterPool>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AtomTable {
|
||||
inner: Rcu<InnerAtomTable>,
|
||||
inner: Arcu<InnerAtomTable, GlobalEpochCounterPool>,
|
||||
// this lock is taking during resizing
|
||||
update: Mutex<()>,
|
||||
}
|
||||
|
||||
pub type AtomTableRef<M> = RcuRef<InnerAtomTable, M>;
|
||||
pub type AtomTableRef<M> = arcu::rcu_ref::RcuRef<InnerAtomTable, M>;
|
||||
|
||||
impl InnerAtomTable {
|
||||
#[inline(always)]
|
||||
@@ -305,7 +299,7 @@ impl InnerAtomTable {
|
||||
STATIC_ATOMS_MAP
|
||||
.get(string)
|
||||
.cloned()
|
||||
.or_else(|| self.table.active_epoch().get(string).cloned())
|
||||
.or_else(|| self.table.read().get(string).cloned())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,10 +317,13 @@ impl AtomTable {
|
||||
atom_table
|
||||
} else {
|
||||
let atom_table = Arc::new(Self {
|
||||
inner: Rcu::new(InnerAtomTable {
|
||||
block: RawBlock::new(),
|
||||
table: Rcu::new(IndexSet::new()),
|
||||
}),
|
||||
inner: Arcu::new(
|
||||
InnerAtomTable {
|
||||
block: RawBlock::new(),
|
||||
table: Arcu::new(IndexSet::new(), GlobalEpochCounterPool),
|
||||
},
|
||||
GlobalEpochCounterPool,
|
||||
),
|
||||
update: Mutex::new(()),
|
||||
});
|
||||
*guard = Arc::downgrade(&atom_table);
|
||||
@@ -335,21 +332,14 @@ impl AtomTable {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn buf(&self) -> AtomTableRef<u8> {
|
||||
AtomTableRef::<InnerAtomTable>::map(self.inner.active_epoch(), |inner| {
|
||||
unsafe { inner.block.base.as_ref() }.unwrap()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn active_table(&self) -> RcuRef<IndexSet<Atom>, IndexSet<Atom>> {
|
||||
self.inner.active_epoch().table.active_epoch()
|
||||
self.inner.read().table.read()
|
||||
}
|
||||
|
||||
pub fn build_with(atom_table: &AtomTable, string: &str) -> Atom {
|
||||
loop {
|
||||
let mut block_epoch = atom_table.inner.active_epoch();
|
||||
let mut table_epoch = block_epoch.table.active_epoch();
|
||||
let mut block_epoch = atom_table.inner.read();
|
||||
let mut table_epoch = block_epoch.table.read();
|
||||
|
||||
if let Some(atom) = block_epoch.lookup_str(string) {
|
||||
return atom;
|
||||
@@ -358,10 +348,8 @@ impl AtomTable {
|
||||
// take a lock to prevent concurrent updates
|
||||
let update_guard = atom_table.update.lock().unwrap();
|
||||
|
||||
let is_same_allocation =
|
||||
RcuRef::same_epoch(&block_epoch, &atom_table.inner.active_epoch());
|
||||
let is_same_atom_list =
|
||||
RcuRef::same_epoch(&table_epoch, &block_epoch.table.active_epoch());
|
||||
let is_same_allocation = RcuRef::same_epoch(&block_epoch, &atom_table.inner.read());
|
||||
let is_same_atom_list = RcuRef::same_epoch(&table_epoch, &block_epoch.table.read());
|
||||
|
||||
if !(is_same_allocation && is_same_atom_list) {
|
||||
// some other thread raced us between our lookup and
|
||||
@@ -371,8 +359,7 @@ impl AtomTable {
|
||||
}
|
||||
|
||||
let size = mem::size_of::<AtomHeader>() + string.len();
|
||||
let align_offset = 8 * mem::align_of::<AtomHeader>();
|
||||
let size = (size & !(align_offset - 1)) + align_offset;
|
||||
let size = size.next_multiple_of(AtomTable::align());
|
||||
|
||||
unsafe {
|
||||
let len_ptr = loop {
|
||||
@@ -381,14 +368,14 @@ impl AtomTable {
|
||||
if ptr.is_null() {
|
||||
// garbage collection would go here
|
||||
let new_block = block_epoch.block.grow_new().unwrap();
|
||||
let new_table = Rcu::new(table_epoch.clone());
|
||||
let new_table = Arcu::new(table_epoch.clone(), GlobalEpochCounterPool);
|
||||
let new_alloc = InnerAtomTable {
|
||||
block: new_block,
|
||||
table: new_table,
|
||||
};
|
||||
atom_table.inner.replace(new_alloc);
|
||||
block_epoch = atom_table.inner.active_epoch();
|
||||
table_epoch = block_epoch.table.active_epoch();
|
||||
block_epoch = atom_table.inner.read();
|
||||
table_epoch = block_epoch.table.read();
|
||||
} else {
|
||||
break ptr;
|
||||
}
|
||||
|
||||
26
src/ffi.rs
26
src/ffi.rs
@@ -436,18 +436,20 @@ impl ForeignFunctionTable {
|
||||
}
|
||||
libffi::raw::FFI_TYPE_SINT64 => read_and_push_int!(i64),
|
||||
libffi::raw::FFI_TYPE_POINTER => read_and_push_int!(i64),
|
||||
libffi::raw::FFI_TYPE_FLOAT => {
|
||||
field_ptr = field_ptr.add(field_ptr.align_offset(std::mem::align_of::<f32>()));
|
||||
let n = std::ptr::read(field_ptr as *mut f32);
|
||||
returns.push(Value::Float(f32::from(n).into()));
|
||||
field_ptr = field_ptr.add(std::mem::size_of::<f32>());
|
||||
}
|
||||
libffi::raw::FFI_TYPE_DOUBLE => {
|
||||
field_ptr = field_ptr.add(field_ptr.align_offset(std::mem::align_of::<f64>()));
|
||||
let n = std::ptr::read(field_ptr as *mut f64);
|
||||
returns.push(Value::Float(f64::from(n)));
|
||||
field_ptr = field_ptr.add(std::mem::size_of::<f64>());
|
||||
}
|
||||
libffi::raw::FFI_TYPE_FLOAT => {
|
||||
field_ptr =
|
||||
field_ptr.add(field_ptr.align_offset(std::mem::align_of::<f32>()));
|
||||
let n: f32 = std::ptr::read(field_ptr as *mut f32);
|
||||
returns.push(Value::Float(n.into()));
|
||||
field_ptr = field_ptr.add(std::mem::size_of::<f32>());
|
||||
}
|
||||
libffi::raw::FFI_TYPE_DOUBLE => {
|
||||
field_ptr =
|
||||
field_ptr.add(field_ptr.align_offset(std::mem::align_of::<f64>()));
|
||||
let n: f64 = std::ptr::read(field_ptr as *mut f64);
|
||||
returns.push(Value::Float(n));
|
||||
field_ptr = field_ptr.add(std::mem::size_of::<f64>());
|
||||
}
|
||||
libffi::raw::FFI_TYPE_STRUCT => {
|
||||
let substruct = struct_type.atom_fields[i].as_str();
|
||||
let struct_type = self
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![allow(clippy::new_without_default)] // annotating structs annotated with #[bitfield] doesn't work
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use crate::machine::gc::StacklessPreOrderHeapIter;
|
||||
|
||||
@@ -1758,7 +1760,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on atom_table.rs UB")]
|
||||
fn heap_stackful_iter_tests() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
@@ -2351,7 +2352,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on atom_table.rs UB")]
|
||||
fn heap_stackful_post_order_iter() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
@@ -2835,7 +2835,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on atom_table.rs UB")]
|
||||
fn heap_stackless_post_order_iter() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
|
||||
@@ -1841,7 +1841,7 @@ mod tests {
|
||||
use crate::machine::mock_wam::*;
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn term_printing_tests() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
|
||||
@@ -42,8 +42,6 @@ pub mod types;
|
||||
|
||||
use instructions::instr;
|
||||
|
||||
mod rcu;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
|
||||
@@ -641,10 +641,17 @@ pub(crate) fn shr(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number, M
|
||||
let n1_i = n1.get_num();
|
||||
let n2_i = n2.get_num();
|
||||
|
||||
// FIXME(arithmetic_overflow)
|
||||
// what should this do for too large n2,
|
||||
// - logical right shift should probably turn to 0
|
||||
// - arithmetic right shift should maybe differ for negative numbers
|
||||
//
|
||||
// note: negaitve n2 is already handled above
|
||||
#[allow(arithmetic_overflow)]
|
||||
if let Ok(n2) = usize::try_from(n2_i) {
|
||||
Ok(Number::arena_from(n1_i >> n2, arena))
|
||||
} else {
|
||||
Ok(Number::arena_from(n1_i >> usize::max_value(), arena))
|
||||
Ok(Number::arena_from(n1_i >> usize::MAX, arena))
|
||||
}
|
||||
}
|
||||
(Number::Fixnum(n1), Number::Integer(n2)) => {
|
||||
@@ -654,25 +661,19 @@ pub(crate) fn shr(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number, M
|
||||
|
||||
match result {
|
||||
Ok(n2) => Ok(Number::arena_from(n1 >> n2, arena)),
|
||||
Err(_) => Ok(Number::arena_from(n1 >> usize::max_value(), arena)),
|
||||
Err(_) => Ok(Number::arena_from(n1 >> usize::MAX, arena)),
|
||||
}
|
||||
}
|
||||
(Number::Integer(n1), Number::Fixnum(n2)) => match usize::try_from(n2.get_num()) {
|
||||
Ok(n2) => Ok(Number::arena_from(Integer::from(&*n1 >> n2), arena)),
|
||||
_ => Ok(Number::arena_from(
|
||||
Integer::from(&*n1 >> usize::max_value()),
|
||||
arena,
|
||||
)),
|
||||
_ => Ok(Number::arena_from(Integer::from(&*n1 >> usize::MAX), arena)),
|
||||
},
|
||||
(Number::Integer(n1), Number::Integer(n2)) => {
|
||||
let result: Result<usize, _> = (&*n2).try_into();
|
||||
|
||||
match result {
|
||||
Ok(n2) => Ok(Number::arena_from(Integer::from(&*n1 >> n2), arena)),
|
||||
Err(_) => Ok(Number::arena_from(
|
||||
Integer::from(&*n1 >> usize::max_value()),
|
||||
arena,
|
||||
)),
|
||||
Err(_) => Ok(Number::arena_from(Integer::from(&*n1 >> usize::MAX), arena)),
|
||||
}
|
||||
}
|
||||
(Number::Integer(_), n2) => Err(numerical_type_error(ValidType::Integer, n2, stub_gen)),
|
||||
@@ -700,7 +701,7 @@ pub(crate) fn shl(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number, M
|
||||
Ok(Number::arena_from(n1_i << n2, arena))
|
||||
} else {
|
||||
let n1 = Integer::from(n1_i);
|
||||
Ok(Number::arena_from(n1 << usize::max_value(), arena))
|
||||
Ok(Number::arena_from(n1 << usize::MAX, arena))
|
||||
}
|
||||
}
|
||||
(Number::Fixnum(n1), Number::Integer(n2)) => {
|
||||
@@ -708,22 +709,16 @@ pub(crate) fn shl(n1: Number, n2: Number, arena: &mut Arena) -> Result<Number, M
|
||||
|
||||
match (&*n2).try_into() as Result<usize, _> {
|
||||
Ok(n2) => Ok(Number::arena_from(n1 << n2, arena)),
|
||||
_ => Ok(Number::arena_from(n1 << usize::max_value(), arena)),
|
||||
_ => Ok(Number::arena_from(n1 << usize::MAX, arena)),
|
||||
}
|
||||
}
|
||||
(Number::Integer(n1), Number::Fixnum(n2)) => match usize::try_from(n2.get_num()) {
|
||||
Ok(n2) => Ok(Number::arena_from(Integer::from(&*n1 << n2), arena)),
|
||||
_ => Ok(Number::arena_from(
|
||||
Integer::from(&*n1 << usize::max_value()),
|
||||
arena,
|
||||
)),
|
||||
_ => Ok(Number::arena_from(Integer::from(&*n1 << usize::MAX), arena)),
|
||||
},
|
||||
(Number::Integer(n1), Number::Integer(n2)) => match (&*n2).try_into() as Result<usize, _> {
|
||||
Ok(n2) => Ok(Number::arena_from(Integer::from(&*n1 << n2), arena)),
|
||||
_ => Ok(Number::arena_from(
|
||||
Integer::from(&*n1 << usize::max_value()),
|
||||
arena,
|
||||
)),
|
||||
_ => Ok(Number::arena_from(Integer::from(&*n1 << usize::MAX), arena)),
|
||||
},
|
||||
(Number::Integer(_), n2) => Err(numerical_type_error(ValidType::Integer, n2, stub_gen)),
|
||||
(Number::Fixnum(_), n2) => Err(numerical_type_error(ValidType::Integer, n2, stub_gen)),
|
||||
@@ -1420,7 +1415,6 @@ mod tests {
|
||||
use crate::machine::mock_wam::*;
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
fn arith_eval_by_metacall_tests() {
|
||||
let mut wam = MachineState::new();
|
||||
let mut op_dir = default_op_dir();
|
||||
|
||||
@@ -398,7 +398,6 @@ mod tests {
|
||||
use crate::machine::mock_wam::*;
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on atom_table.rs UB")]
|
||||
fn copier_tests() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
|
||||
@@ -369,7 +369,6 @@ mod tests {
|
||||
use crate::machine::mock_wam::*;
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
fn heap_marking_tests() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
|
||||
@@ -69,8 +69,8 @@ impl TryFrom<HeapCellValue> for Literal {
|
||||
(ArenaHeaderTag::Rational, n) => {
|
||||
Ok(Literal::Rational(n))
|
||||
}
|
||||
(ArenaHeaderTag::IndexPtr, _ip) => {
|
||||
Ok(Literal::CodeIndex(CodeIndex::from(cons_ptr)))
|
||||
(ArenaHeaderTag::IndexPtr, ip) => {
|
||||
Ok(Literal::CodeIndex(CodeIndex::from(ip)))
|
||||
}
|
||||
_ => {
|
||||
Err(())
|
||||
|
||||
@@ -191,7 +191,7 @@ impl Machine {
|
||||
printer.quoted = true;
|
||||
printer.max_depth = 1000; // NOTE: set this to 0 for unbounded depth
|
||||
printer.double_quotes = true;
|
||||
printer.var_names = var_names.clone();
|
||||
printer.var_names.clone_from(&var_names);
|
||||
|
||||
let outputter = printer.print();
|
||||
|
||||
@@ -238,7 +238,7 @@ mod tests {
|
||||
use crate::machine::{QueryMatch, QueryResolution, Value};
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn programatic_query() {
|
||||
let mut machine = Machine::new_lib();
|
||||
|
||||
@@ -278,7 +278,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn failing_query() {
|
||||
let mut machine = Machine::new_lib();
|
||||
let query = String::from(r#"triple("a",P,"b")."#);
|
||||
@@ -292,7 +292,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore)]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn complex_results() {
|
||||
let mut machine = Machine::new_lib();
|
||||
machine.load_module_string(
|
||||
@@ -349,7 +349,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn empty_predicate() {
|
||||
let mut machine = Machine::new_lib();
|
||||
machine.load_module_string(
|
||||
@@ -365,7 +365,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn list_results() {
|
||||
let mut machine = Machine::new_lib();
|
||||
machine.load_module_string(
|
||||
@@ -394,7 +394,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn consult() {
|
||||
let mut machine = Machine::new_lib();
|
||||
|
||||
@@ -453,7 +453,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn integration_test() {
|
||||
let mut machine = Machine::new_lib();
|
||||
|
||||
@@ -488,19 +488,15 @@ mod tests {
|
||||
} else if let Some(result) = block.strip_prefix("result") {
|
||||
i += 1;
|
||||
if let Some(Ok(ref last_result)) = last_result {
|
||||
println!(
|
||||
"\n\n=====Result No. {}=======\n{}\n===============",
|
||||
i,
|
||||
last_result.to_string().trim()
|
||||
);
|
||||
assert_eq!(last_result.to_string().trim(), result.to_string().trim(),)
|
||||
println!("\n\n=====Result No. {i}=======\n{last_result}\n===============");
|
||||
assert_eq!(last_result.to_string(), result.to_string().trim(),)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn findall() {
|
||||
let mut machine = Machine::new_lib();
|
||||
|
||||
@@ -533,6 +529,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn dont_return_partial_matches() {
|
||||
let mut machine = Machine::new_lib();
|
||||
|
||||
@@ -556,6 +553,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn dont_return_partial_matches_without_discountiguous() {
|
||||
let mut machine = Machine::new_lib();
|
||||
|
||||
@@ -587,6 +585,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn non_existent_predicate_should_not_cause_panic_when_other_predicates_are_defined() {
|
||||
let mut machine = Machine::new_lib();
|
||||
|
||||
@@ -611,6 +610,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn issue_2341() {
|
||||
let mut machine = Machine::new_lib();
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::parser::ast::*;
|
||||
|
||||
use fxhash::FxBuildHasher;
|
||||
use indexmap::IndexSet;
|
||||
pub use ref_thread_local::RefThreadLocal;
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::fs::File;
|
||||
@@ -1176,7 +1175,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
|
||||
ListingSource::File(filename, path_buf),
|
||||
)
|
||||
}
|
||||
ModuleSource::Library(library) => match LIBRARIES.borrow().get(&*library.as_str()) {
|
||||
ModuleSource::Library(library) => match libraries::get(&library.as_str()) {
|
||||
Some(code) => {
|
||||
if let Some(module) = self.wam_prelude.indices.modules.get(&library) {
|
||||
if let ListingSource::DynamicallyGenerated = &module.listing_src {
|
||||
@@ -1257,7 +1256,7 @@ impl<'a, LS: LoadState<'a>> Loader<'a, LS> {
|
||||
ListingSource::File(filename, path_buf),
|
||||
)
|
||||
}
|
||||
ModuleSource::Library(library) => match LIBRARIES.borrow().get(&*library.as_str()) {
|
||||
ModuleSource::Library(library) => match libraries::get(&library.as_str()) {
|
||||
Some(code) => {
|
||||
if self.wam_prelude.indices.modules.contains_key(&library) {
|
||||
return self.import_qualified_module(library, exports);
|
||||
|
||||
@@ -304,11 +304,15 @@ impl<'a> LoadState<'a> for LiveLoadAndMachineState<'a> {
|
||||
|
||||
#[inline(always)]
|
||||
fn evacuate(mut loader: Loader<'a, Self>) -> Result<Self::Evacuable, SessionError> {
|
||||
loader
|
||||
.payload
|
||||
.load_state
|
||||
.set_tag(ArenaHeaderTag::InactiveLoadState);
|
||||
Ok(loader.payload.load_state)
|
||||
if loader.payload.load_state.get_tag() != ArenaHeaderTag::Dropped {
|
||||
loader
|
||||
.payload
|
||||
.load_state
|
||||
.set_tag(ArenaHeaderTag::InactiveLoadState);
|
||||
Ok(loader.payload.load_state)
|
||||
} else {
|
||||
unreachable!("we never evacuate after dropping")
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -319,7 +323,7 @@ impl<'a> LoadState<'a> for LiveLoadAndMachineState<'a> {
|
||||
#[inline(always)]
|
||||
fn reset_machine(loader: &mut Loader<'a, Self>) {
|
||||
if loader.payload.load_state.get_tag() != ArenaHeaderTag::Dropped {
|
||||
loader.payload.load_state.set_tag(ArenaHeaderTag::Dropped);
|
||||
loader.payload.load_state.drop_payload();
|
||||
loader.reset_machine();
|
||||
}
|
||||
}
|
||||
@@ -353,7 +357,7 @@ impl<'a> LoadState<'a> for LiveLoadAndMachineState<'a> {
|
||||
|
||||
#[inline]
|
||||
fn err_on_builtin_module_overwrite(module_name: Atom) -> Result<(), SessionError> {
|
||||
if LIBRARIES.borrow().contains_key(&*module_name.as_str()) {
|
||||
if libraries::contains(&module_name.as_str()) {
|
||||
Err(SessionError::CannotOverwriteBuiltInModule(module_name))
|
||||
} else {
|
||||
Ok(())
|
||||
@@ -1757,7 +1761,7 @@ impl Machine {
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn push_load_state_payload(&mut self) {
|
||||
let payload = arena_alloc!(
|
||||
let payload: TypedArenaPtr<LiveLoadState> = arena_alloc!(
|
||||
LoadStatePayload::new(self.code.len(), LiveTermStream::new(ListingSource::User),),
|
||||
&mut self.machine_st.arena
|
||||
);
|
||||
@@ -1784,11 +1788,8 @@ impl Machine {
|
||||
(HeapCellValueTag::Cons, cons_ptr) => {
|
||||
match_untyped_arena_ptr!(cons_ptr,
|
||||
(ArenaHeaderTag::LiveLoadState, payload) => {
|
||||
unsafe {
|
||||
std::ptr::drop_in_place(
|
||||
payload.as_ptr() as *mut LiveLoadState,
|
||||
);
|
||||
}
|
||||
let mut payload = payload;
|
||||
payload.drop_payload()
|
||||
}
|
||||
_ => {}
|
||||
);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![allow(clippy::new_without_default)] // annotating structs annotated with #[bitfield] doesn't work
|
||||
|
||||
use crate::parser::ast::*;
|
||||
|
||||
use crate::arena::*;
|
||||
@@ -157,13 +159,6 @@ impl From<CodeIndex> for UntypedArenaPtr {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UntypedArenaPtr> for CodeIndex {
|
||||
#[inline(always)]
|
||||
fn from(ptr: UntypedArenaPtr) -> CodeIndex {
|
||||
CodeIndex(TypedArenaPtr::new(ptr.get_ptr() as *mut IndexPtr))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TypedArenaPtr<IndexPtr>> for CodeIndex {
|
||||
#[inline(always)]
|
||||
fn from(ptr: TypedArenaPtr<IndexPtr>) -> CodeIndex {
|
||||
|
||||
@@ -679,15 +679,13 @@ impl MachineState {
|
||||
indices: &mut IndexStore,
|
||||
) -> CallResult {
|
||||
if let Stream::Readline(ptr) = stream {
|
||||
unsafe {
|
||||
let readline = ptr.as_ptr().as_mut().unwrap();
|
||||
readline.set_atoms_for_completion(&self.atom_tbl);
|
||||
return self.read_term(
|
||||
stream,
|
||||
indices,
|
||||
MachineState::read_term_from_user_input_eof_handler,
|
||||
);
|
||||
}
|
||||
let readline = unsafe { ptr.as_ptr().as_mut() }.unwrap();
|
||||
readline.set_atoms_for_completion(&self.atom_tbl);
|
||||
return self.read_term(
|
||||
stream,
|
||||
indices,
|
||||
MachineState::read_term_from_user_input_eof_handler,
|
||||
);
|
||||
}
|
||||
|
||||
if let Stream::Byte(_) = stream {
|
||||
|
||||
@@ -260,7 +260,6 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
fn unify_tests() {
|
||||
let mut wam = MachineState::new();
|
||||
let mut op_dir = default_op_dir();
|
||||
@@ -482,7 +481,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
fn test_unify_with_occurs_check() {
|
||||
let mut wam = MachineState::new();
|
||||
let mut op_dir = default_op_dir();
|
||||
|
||||
@@ -60,6 +60,7 @@ use std::env;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use self::config::MachineConfig;
|
||||
use self::parsed_results::*;
|
||||
@@ -110,10 +111,34 @@ impl LoadContext {
|
||||
|
||||
#[inline]
|
||||
fn current_dir() -> PathBuf {
|
||||
env::current_dir().unwrap_or(PathBuf::from("./"))
|
||||
if !cfg!(miri) {
|
||||
env::current_dir().unwrap_or(PathBuf::from("./"))
|
||||
} else {
|
||||
PathBuf::from("./")
|
||||
}
|
||||
}
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
|
||||
mod libraries {
|
||||
include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
|
||||
|
||||
pub(crate) fn contains(name: &str) -> bool {
|
||||
LIBRARIES.with(|libs| libs.contains_key(name))
|
||||
}
|
||||
|
||||
pub(crate) fn get(name: &str) -> Option<&'static str> {
|
||||
LIBRARIES.with(|libs| libs.get(name).copied())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
std::thread_local! {
|
||||
#[allow(dead_code)]
|
||||
static LIBRARIES2 : IndexMap<&'static str, &'static str> = {
|
||||
let mut m = IndexMap::new();
|
||||
m.insert("test", "test2");
|
||||
m
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub static BREAK_FROM_DISPATCH_LOOP_LOC: usize = 0;
|
||||
pub static INSTALL_VERIFY_ATTR_INTERRUPT: usize = 1;
|
||||
@@ -448,8 +473,6 @@ impl Machine {
|
||||
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new(config: MachineConfig) -> Self {
|
||||
use ref_thread_local::RefThreadLocal;
|
||||
|
||||
let args = MachineArgs::new();
|
||||
let mut machine_st = MachineState::new();
|
||||
|
||||
@@ -488,7 +511,8 @@ impl Machine {
|
||||
|
||||
bootstrapping_compile(
|
||||
Stream::from_static_string(
|
||||
LIBRARIES.borrow()["ops_and_meta_predicates"],
|
||||
libraries::get("ops_and_meta_predicates")
|
||||
.expect("library ops_and_meta_predicates should exist"),
|
||||
&mut wam.machine_st.arena,
|
||||
),
|
||||
&mut wam,
|
||||
@@ -500,7 +524,10 @@ impl Machine {
|
||||
.unwrap();
|
||||
|
||||
bootstrapping_compile(
|
||||
Stream::from_static_string(LIBRARIES.borrow()["builtins"], &mut wam.machine_st.arena),
|
||||
Stream::from_static_string(
|
||||
libraries::get("builtins").expect("library builtins should exist"),
|
||||
&mut wam.machine_st.arena,
|
||||
),
|
||||
&mut wam,
|
||||
ListingSource::from_file_and_path(atom!("builtins.pl"), lib_path.clone()),
|
||||
)
|
||||
@@ -1235,33 +1262,25 @@ impl Machine {
|
||||
|
||||
#[inline(always)]
|
||||
fn run_cleaners(&mut self) -> bool {
|
||||
use std::sync::Once;
|
||||
static CLEANER_INIT: OnceLock<(usize, usize)> = OnceLock::new();
|
||||
|
||||
static CLEANER_INIT: Once = Once::new();
|
||||
let (r_c_w_h, r_c_wo_h) = *CLEANER_INIT.get_or_init(|| {
|
||||
let r_c_w_h_atom = atom!("run_cleaners_with_handling");
|
||||
let r_c_wo_h_atom = atom!("run_cleaners_without_handling");
|
||||
let iso_ext = atom!("iso_ext");
|
||||
|
||||
static mut RCWH: usize = 0;
|
||||
static mut RCWOH: usize = 0;
|
||||
|
||||
let (r_c_w_h, r_c_wo_h) = unsafe {
|
||||
CLEANER_INIT.call_once(|| {
|
||||
let r_c_w_h_atom = atom!("run_cleaners_with_handling");
|
||||
let r_c_wo_h_atom = atom!("run_cleaners_without_handling");
|
||||
let iso_ext = atom!("iso_ext");
|
||||
|
||||
RCWH = self
|
||||
.indices
|
||||
.get_predicate_code_index(r_c_w_h_atom, 0, iso_ext)
|
||||
.and_then(|item| item.local())
|
||||
.unwrap();
|
||||
RCWOH = self
|
||||
.indices
|
||||
.get_predicate_code_index(r_c_wo_h_atom, 1, iso_ext)
|
||||
.and_then(|item| item.local())
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
(RCWH, RCWOH)
|
||||
};
|
||||
let r_c_w_h = self
|
||||
.indices
|
||||
.get_predicate_code_index(r_c_w_h_atom, 0, iso_ext)
|
||||
.and_then(|item| item.local())
|
||||
.unwrap();
|
||||
let r_c_wo_h = self
|
||||
.indices
|
||||
.get_predicate_code_index(r_c_wo_h_atom, 1, iso_ext)
|
||||
.and_then(|item| item.local())
|
||||
.unwrap();
|
||||
(r_c_w_h, r_c_wo_h)
|
||||
});
|
||||
|
||||
if let Some(&(_, b_cutoff, prev_block)) = self.machine_st.cont_pts.last() {
|
||||
if self.machine_st.b < b_cutoff {
|
||||
|
||||
@@ -3,6 +3,8 @@ use dashu::*;
|
||||
use ordered_float::OrderedFloat;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::Display;
|
||||
use std::fmt::Write;
|
||||
|
||||
pub type QueryResult = Result<QueryResolution, String>;
|
||||
|
||||
@@ -13,17 +15,21 @@ pub enum QueryResolution {
|
||||
Matches(Vec<QueryMatch>),
|
||||
}
|
||||
|
||||
pub fn prolog_value_to_json_string(value: Value) -> String {
|
||||
pub fn write_prolog_value_as_json<W: Write>(
|
||||
writer: &mut W,
|
||||
value: &Value,
|
||||
) -> Result<(), std::fmt::Error> {
|
||||
match value {
|
||||
Value::Integer(i) => format!("{}", i),
|
||||
Value::Float(f) => format!("{}", f),
|
||||
Value::Rational(r) => format!("{}", r),
|
||||
Value::Atom(a) => format!("{}", a.as_str()),
|
||||
Value::Integer(i) => write!(writer, "{}", i),
|
||||
Value::Float(f) => write!(writer, "{}", f),
|
||||
Value::Rational(r) => write!(writer, "{}", r),
|
||||
Value::Atom(a) => writer.write_str(&a.as_str()),
|
||||
Value::String(s) => {
|
||||
if let Err(_e) = serde_json::from_str::<serde_json::Value>(s.as_str()) {
|
||||
//treat as string literal
|
||||
//escape double quotes
|
||||
format!(
|
||||
write!(
|
||||
writer,
|
||||
"\"{}\"",
|
||||
s.replace('\"', "\\\"")
|
||||
.replace('\n', "\\n")
|
||||
@@ -32,60 +38,71 @@ pub fn prolog_value_to_json_string(value: Value) -> String {
|
||||
)
|
||||
} else {
|
||||
//return valid json string
|
||||
s
|
||||
writer.write_str(s)
|
||||
}
|
||||
}
|
||||
Value::List(l) => {
|
||||
let mut string_result = "[".to_string();
|
||||
for (i, v) in l.iter().enumerate() {
|
||||
if i > 0 {
|
||||
string_result.push(',');
|
||||
writer.write_char('[')?;
|
||||
if let Some((first, rest)) = l.split_first() {
|
||||
write_prolog_value_as_json(writer, first)?;
|
||||
|
||||
for other in rest {
|
||||
writer.write_char(',')?;
|
||||
write_prolog_value_as_json(writer, other)?;
|
||||
}
|
||||
string_result.push_str(&prolog_value_to_json_string(v.clone()));
|
||||
}
|
||||
string_result.push(']');
|
||||
string_result
|
||||
writer.write_char(']')
|
||||
}
|
||||
Value::Structure(s, l) => {
|
||||
let mut string_result = format!("\"{}\":[", s.as_str());
|
||||
for (i, v) in l.iter().enumerate() {
|
||||
if i > 0 {
|
||||
string_result.push(',');
|
||||
write!(writer, "\"{}\":[", s.as_str())?;
|
||||
|
||||
if let Some((first, rest)) = l.split_first() {
|
||||
write_prolog_value_as_json(writer, first)?;
|
||||
for other in rest {
|
||||
writer.write_char(',')?;
|
||||
write_prolog_value_as_json(writer, other)?;
|
||||
}
|
||||
string_result.push_str(&prolog_value_to_json_string(v.clone()));
|
||||
}
|
||||
string_result.push(']');
|
||||
string_result
|
||||
writer.write_char(']')
|
||||
}
|
||||
_ => "null".to_string(),
|
||||
_ => writer.write_str("null"),
|
||||
}
|
||||
}
|
||||
|
||||
fn prolog_match_to_json_string(query_match: &QueryMatch) -> String {
|
||||
let mut string_result = "{".to_string();
|
||||
for (i, (k, v)) in query_match.bindings.iter().enumerate() {
|
||||
if i > 0 {
|
||||
string_result.push(',');
|
||||
fn write_prolog_match_as_json<W: std::fmt::Write>(
|
||||
writer: &mut W,
|
||||
query_match: &QueryMatch,
|
||||
) -> Result<(), std::fmt::Error> {
|
||||
writer.write_char('{')?;
|
||||
let mut iter = query_match.bindings.iter();
|
||||
|
||||
if let Some((k, v)) = iter.next() {
|
||||
write!(writer, "\"{k}\":")?;
|
||||
write_prolog_value_as_json(writer, v)?;
|
||||
|
||||
for (k, v) in iter {
|
||||
write!(writer, ",\"{k}\":")?;
|
||||
write_prolog_value_as_json(writer, v)?;
|
||||
}
|
||||
string_result.push_str(&format!(
|
||||
"\"{}\":{}",
|
||||
k,
|
||||
prolog_value_to_json_string(v.clone())
|
||||
));
|
||||
}
|
||||
string_result.push('}');
|
||||
string_result
|
||||
writer.write_char('}')
|
||||
}
|
||||
|
||||
impl ToString for QueryResolution {
|
||||
fn to_string(&self) -> String {
|
||||
impl Display for QueryResolution {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
QueryResolution::True => "true".to_string(),
|
||||
QueryResolution::False => "false".to_string(),
|
||||
QueryResolution::True => f.write_str("true"),
|
||||
QueryResolution::False => f.write_str("false"),
|
||||
QueryResolution::Matches(matches) => {
|
||||
let matches_json: Vec<String> =
|
||||
matches.iter().map(prolog_match_to_json_string).collect();
|
||||
format!("[{}]", matches_json.join(","))
|
||||
f.write_char('[')?;
|
||||
if let Some((first, rest)) = matches.split_first() {
|
||||
write_prolog_match_as_json(f, first)?;
|
||||
for other in rest {
|
||||
f.write_char(',')?;
|
||||
write_prolog_match_as_json(f, other)?;
|
||||
}
|
||||
}
|
||||
f.write_char(']')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -801,7 +801,7 @@ mod test {
|
||||
use crate::machine::mock_wam::*;
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "blocked on streams.rs UB")]
|
||||
#[cfg_attr(miri, ignore = "it takes too long to run")]
|
||||
fn pstr_iter_tests() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
|
||||
@@ -15,7 +15,9 @@ impl RawBlockTraits for Stack {
|
||||
|
||||
#[inline]
|
||||
fn align() -> usize {
|
||||
mem::align_of::<HeapCellValue>()
|
||||
mem::align_of::<OrFrame>()
|
||||
.max(mem::align_of::<AndFrame>())
|
||||
.max(mem::align_of::<HeapCellValue>())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,7 +283,6 @@ mod tests {
|
||||
use crate::machine::mock_wam::*;
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore)]
|
||||
fn stack_tests() {
|
||||
let mut wam = MockWAM::new();
|
||||
|
||||
|
||||
@@ -324,6 +324,9 @@ impl Write for HttpWriteStream {
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
impl HttpWriteStream {
|
||||
// TODO why is this suddenly dead code and should it be used somewhere?
|
||||
// Should this be impl Drop for HttpWriteStream?
|
||||
#[allow(dead_code)]
|
||||
fn drop(&mut self) {
|
||||
let headers = unsafe { std::mem::ManuallyDrop::take(&mut self.headers) };
|
||||
let buffer = unsafe { std::mem::ManuallyDrop::take(&mut self.buffer) };
|
||||
@@ -452,15 +455,34 @@ impl<T> DerefMut for StreamLayout<T> {
|
||||
|
||||
macro_rules! arena_allocated_impl_for_stream {
|
||||
($stream_type:ty, $stream_tag:ident) => {
|
||||
impl ArenaAllocated for StreamLayout<$stream_type> {
|
||||
type PtrToAllocated = TypedArenaPtr<StreamLayout<$stream_type>>;
|
||||
impl $crate::arena::AllocateInArena<$stream_tag> for StreamLayout<$stream_type> {
|
||||
fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<$stream_tag> {
|
||||
$stream_tag::alloc(arena, core::mem::ManuallyDrop::new(self))
|
||||
}
|
||||
}
|
||||
|
||||
gen_ptr_to_allocated!(StreamLayout<$stream_type>);
|
||||
impl ArenaAllocated for $stream_tag {
|
||||
type Payload = core::mem::ManuallyDrop<StreamLayout<$stream_type>>;
|
||||
|
||||
#[inline]
|
||||
fn tag() -> ArenaHeaderTag {
|
||||
ArenaHeaderTag::$stream_tag
|
||||
}
|
||||
|
||||
unsafe fn dealloc(ptr: std::ptr::NonNull<TypedAllocSlab<Self>>) {
|
||||
let mut slab = unsafe { Box::from_raw(ptr.as_ptr()) };
|
||||
|
||||
match slab.tag() {
|
||||
ArenaHeaderTag::$stream_tag => {
|
||||
unsafe { std::mem::ManuallyDrop::drop(slab.payload()) };
|
||||
}
|
||||
ArenaHeaderTag::Dropped => {}
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
drop(slab);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -482,26 +504,26 @@ arena_allocated_impl_for_stream!(StandardErrorStream, StandardErrorStream);
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum Stream {
|
||||
Byte(TypedArenaPtr<StreamLayout<CharReader<ByteStream>>>),
|
||||
InputFile(TypedArenaPtr<StreamLayout<CharReader<InputFileStream>>>),
|
||||
OutputFile(TypedArenaPtr<StreamLayout<OutputFileStream>>),
|
||||
StaticString(TypedArenaPtr<StreamLayout<StaticStringStream>>),
|
||||
NamedTcp(TypedArenaPtr<StreamLayout<CharReader<NamedTcpStream>>>),
|
||||
Byte(TypedArenaPtr<ByteStream>),
|
||||
InputFile(TypedArenaPtr<InputFileStream>),
|
||||
OutputFile(TypedArenaPtr<OutputFileStream>),
|
||||
StaticString(TypedArenaPtr<StaticStringStream>),
|
||||
NamedTcp(TypedArenaPtr<NamedTcpStream>),
|
||||
#[cfg(feature = "tls")]
|
||||
NamedTls(TypedArenaPtr<StreamLayout<CharReader<NamedTlsStream>>>),
|
||||
NamedTls(TypedArenaPtr<NamedTlsStream>),
|
||||
#[cfg(feature = "http")]
|
||||
HttpRead(TypedArenaPtr<StreamLayout<CharReader<HttpReadStream>>>),
|
||||
HttpRead(TypedArenaPtr<HttpReadStream>),
|
||||
#[cfg(feature = "http")]
|
||||
HttpWrite(TypedArenaPtr<StreamLayout<CharReader<HttpWriteStream>>>),
|
||||
HttpWrite(TypedArenaPtr<HttpWriteStream>),
|
||||
Null(StreamOptions),
|
||||
Readline(TypedArenaPtr<StreamLayout<ReadlineStream>>),
|
||||
StandardOutput(TypedArenaPtr<StreamLayout<StandardOutputStream>>),
|
||||
StandardError(TypedArenaPtr<StreamLayout<StandardErrorStream>>),
|
||||
Readline(TypedArenaPtr<ReadlineStream>),
|
||||
StandardOutput(TypedArenaPtr<StandardOutputStream>),
|
||||
StandardError(TypedArenaPtr<StandardErrorStream>),
|
||||
}
|
||||
|
||||
impl From<TypedArenaPtr<StreamLayout<ReadlineStream>>> for Stream {
|
||||
impl From<TypedArenaPtr<ReadlineStream>> for Stream {
|
||||
#[inline]
|
||||
fn from(stream: TypedArenaPtr<StreamLayout<ReadlineStream>>) -> Stream {
|
||||
fn from(stream: TypedArenaPtr<ReadlineStream>) -> Stream {
|
||||
Stream::Readline(stream)
|
||||
}
|
||||
}
|
||||
@@ -540,29 +562,27 @@ impl Stream {
|
||||
))
|
||||
}
|
||||
|
||||
pub fn from_tag(tag: ArenaHeaderTag, ptr: *const u8) -> Self {
|
||||
pub fn from_tag(tag: ArenaHeaderTag, ptr: UntypedArenaPtr) -> Self {
|
||||
match tag {
|
||||
ArenaHeaderTag::ByteStream => Stream::Byte(TypedArenaPtr::new(ptr as *mut _)),
|
||||
ArenaHeaderTag::InputFileStream => Stream::InputFile(TypedArenaPtr::new(ptr as *mut _)),
|
||||
ArenaHeaderTag::OutputFileStream => {
|
||||
Stream::OutputFile(TypedArenaPtr::new(ptr as *mut _))
|
||||
}
|
||||
ArenaHeaderTag::NamedTcpStream => Stream::NamedTcp(TypedArenaPtr::new(ptr as *mut _)),
|
||||
ArenaHeaderTag::ByteStream => Stream::Byte(unsafe { ptr.as_typed_ptr() }),
|
||||
ArenaHeaderTag::InputFileStream => Stream::InputFile(unsafe { ptr.as_typed_ptr() }),
|
||||
ArenaHeaderTag::OutputFileStream => Stream::OutputFile(unsafe { ptr.as_typed_ptr() }),
|
||||
ArenaHeaderTag::NamedTcpStream => Stream::NamedTcp(unsafe { ptr.as_typed_ptr() }),
|
||||
#[cfg(feature = "tls")]
|
||||
ArenaHeaderTag::NamedTlsStream => Stream::NamedTls(TypedArenaPtr::new(ptr as *mut _)),
|
||||
ArenaHeaderTag::NamedTlsStream => Stream::NamedTls(unsafe { ptr.as_typed_ptr() }),
|
||||
#[cfg(feature = "http")]
|
||||
ArenaHeaderTag::HttpReadStream => Stream::HttpRead(TypedArenaPtr::new(ptr as *mut _)),
|
||||
ArenaHeaderTag::HttpReadStream => Stream::HttpRead(unsafe { ptr.as_typed_ptr() }),
|
||||
#[cfg(feature = "http")]
|
||||
ArenaHeaderTag::HttpWriteStream => Stream::HttpWrite(TypedArenaPtr::new(ptr as *mut _)),
|
||||
ArenaHeaderTag::ReadlineStream => Stream::Readline(TypedArenaPtr::new(ptr as *mut _)),
|
||||
ArenaHeaderTag::HttpWriteStream => Stream::HttpWrite(unsafe { ptr.as_typed_ptr() }),
|
||||
ArenaHeaderTag::ReadlineStream => Stream::Readline(unsafe { ptr.as_typed_ptr() }),
|
||||
ArenaHeaderTag::StaticStringStream => {
|
||||
Stream::StaticString(TypedArenaPtr::new(ptr as *mut _))
|
||||
Stream::StaticString(unsafe { ptr.as_typed_ptr() })
|
||||
}
|
||||
ArenaHeaderTag::StandardOutputStream => {
|
||||
Stream::StandardOutput(TypedArenaPtr::new(ptr as *mut _))
|
||||
Stream::StandardOutput(unsafe { ptr.as_typed_ptr() })
|
||||
}
|
||||
ArenaHeaderTag::StandardErrorStream => {
|
||||
Stream::StandardError(TypedArenaPtr::new(ptr as *mut _))
|
||||
Stream::StandardError(unsafe { ptr.as_typed_ptr() })
|
||||
}
|
||||
ArenaHeaderTag::Dropped | ArenaHeaderTag::NullStream => {
|
||||
Stream::Null(StreamOptions::default())
|
||||
@@ -996,7 +1016,7 @@ impl Stream {
|
||||
past_end_of_stream,
|
||||
stream,
|
||||
..
|
||||
} = &mut **stream_layout;
|
||||
} = &mut ***stream_layout;
|
||||
|
||||
stream
|
||||
.get_mut()
|
||||
@@ -1070,7 +1090,7 @@ impl Stream {
|
||||
past_end_of_stream,
|
||||
stream,
|
||||
..
|
||||
} = &mut **stream_layout;
|
||||
} = &mut ***stream_layout;
|
||||
|
||||
let cursor_len = stream.get_ref().0.get_ref().len() as u64;
|
||||
cursor_position(past_end_of_stream, &stream.get_ref().0, cursor_len)
|
||||
@@ -1080,7 +1100,7 @@ impl Stream {
|
||||
past_end_of_stream,
|
||||
stream,
|
||||
..
|
||||
} = &mut **stream_layout;
|
||||
} = &mut ***stream_layout;
|
||||
|
||||
let cursor_len = stream.stream.get_ref().len() as u64;
|
||||
cursor_position(past_end_of_stream, &stream.stream, cursor_len)
|
||||
@@ -1092,7 +1112,7 @@ impl Stream {
|
||||
past_end_of_stream,
|
||||
stream,
|
||||
..
|
||||
} = &mut **stream_layout;
|
||||
} = &mut ***stream_layout;
|
||||
|
||||
match stream.get_ref().file.metadata() {
|
||||
Ok(metadata) => {
|
||||
@@ -1279,38 +1299,25 @@ impl Stream {
|
||||
Stream::NamedTls(ref mut tls_stream) => tls_stream.inner_mut().tls_stream.shutdown(),
|
||||
#[cfg(feature = "http")]
|
||||
Stream::HttpRead(ref mut http_stream) => {
|
||||
unsafe {
|
||||
http_stream.set_tag(ArenaHeaderTag::Dropped);
|
||||
std::ptr::drop_in_place(&mut http_stream.inner_mut().body_reader as *mut _);
|
||||
}
|
||||
http_stream.drop_payload();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(feature = "http")]
|
||||
Stream::HttpWrite(ref mut http_stream) => {
|
||||
http_stream.inner_mut().drop();
|
||||
unsafe {
|
||||
http_stream.set_tag(ArenaHeaderTag::Dropped);
|
||||
std::ptr::drop_in_place(&mut http_stream.inner_mut().buffer as *mut _);
|
||||
}
|
||||
Stream::HttpWrite(mut http_stream) => {
|
||||
http_stream.drop_payload();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Stream::InputFile(mut file_stream) => {
|
||||
// close the stream by dropping the inner File.
|
||||
unsafe {
|
||||
file_stream.set_tag(ArenaHeaderTag::Dropped);
|
||||
std::ptr::drop_in_place(&mut file_stream.inner_mut().file as *mut _);
|
||||
}
|
||||
file_stream.drop_payload();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Stream::OutputFile(mut file_stream) => {
|
||||
// close the stream by dropping the inner File.
|
||||
unsafe {
|
||||
file_stream.set_tag(ArenaHeaderTag::Dropped);
|
||||
std::ptr::drop_in_place(&mut file_stream.file as *mut _);
|
||||
}
|
||||
file_stream.drop_payload();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -39,8 +39,6 @@ use ordered_float::OrderedFloat;
|
||||
use fxhash::{FxBuildHasher, FxHasher};
|
||||
use indexmap::IndexSet;
|
||||
|
||||
pub(crate) use ref_thread_local::RefThreadLocal;
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::BTreeSet;
|
||||
@@ -103,6 +101,8 @@ use warp::hyper::{HeaderMap, Method};
|
||||
#[cfg(feature = "http")]
|
||||
use warp::{Buf, Filter};
|
||||
|
||||
use super::libraries;
|
||||
|
||||
#[cfg(feature = "repl")]
|
||||
pub(crate) fn get_key() -> KeyEvent {
|
||||
let key;
|
||||
@@ -4513,7 +4513,8 @@ impl Machine {
|
||||
});
|
||||
|
||||
let http_listener = HttpListener { incoming: rx };
|
||||
let http_listener = arena_alloc!(http_listener, &mut self.machine_st.arena);
|
||||
let http_listener: TypedArenaPtr<HttpListener> =
|
||||
arena_alloc!(http_listener, &mut self.machine_st.arena);
|
||||
|
||||
let addr = self.deref_register(2);
|
||||
self.machine_st.bind(
|
||||
@@ -4578,7 +4579,7 @@ impl Machine {
|
||||
self.indices.streams.insert(stream);
|
||||
let stream = stream_as_cell!(stream);
|
||||
|
||||
let handle = arena_alloc!(request.response, &mut self.machine_st.arena);
|
||||
let handle: TypedArenaPtr<HttpResponse> = arena_alloc!(request.response, &mut self.machine_st.arena);
|
||||
|
||||
self.machine_st.bind(method.as_var().unwrap(), atom_as_cell!(method_atom));
|
||||
self.machine_st.bind(path.as_var().unwrap(), path_cell);
|
||||
@@ -6510,32 +6511,33 @@ impl Machine {
|
||||
format!("{}:{}", socket_atom.as_str(), port)
|
||||
};
|
||||
|
||||
let (tcp_listener, port) = match TcpListener::bind(server_addr).map_err(|e| e.kind()) {
|
||||
Ok(tcp_listener) => {
|
||||
let port = tcp_listener.local_addr().map(|addr| addr.port()).ok();
|
||||
let (tcp_listener, port): (TypedArenaPtr<TcpListener>, _) =
|
||||
match TcpListener::bind(server_addr).map_err(|e| e.kind()) {
|
||||
Ok(tcp_listener) => {
|
||||
let port = tcp_listener.local_addr().map(|addr| addr.port()).ok();
|
||||
|
||||
if let Some(port) = port {
|
||||
(
|
||||
arena_alloc!(tcp_listener, &mut self.machine_st.arena),
|
||||
port as usize,
|
||||
)
|
||||
} else {
|
||||
if let Some(port) = port {
|
||||
(
|
||||
arena_alloc!(tcp_listener, &mut self.machine_st.arena),
|
||||
port as usize,
|
||||
)
|
||||
} else {
|
||||
self.machine_st.fail = true;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(ErrorKind::PermissionDenied) => {
|
||||
return Err(self.machine_st.open_permission_error(
|
||||
addr,
|
||||
atom!("socket_server_open"),
|
||||
2,
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
self.machine_st.fail = true;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(ErrorKind::PermissionDenied) => {
|
||||
return Err(self.machine_st.open_permission_error(
|
||||
addr,
|
||||
atom!("socket_server_open"),
|
||||
2,
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
self.machine_st.fail = true;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
let addr = self.deref_register(3);
|
||||
self.machine_st.bind(
|
||||
@@ -6729,12 +6731,8 @@ impl Machine {
|
||||
(HeapCellValueTag::Cons, cons_ptr) => {
|
||||
match_untyped_arena_ptr!(cons_ptr,
|
||||
(ArenaHeaderTag::TcpListener, tcp_listener) => {
|
||||
unsafe {
|
||||
// dropping closes the instance.
|
||||
std::ptr::drop_in_place(&mut tcp_listener as *mut _);
|
||||
}
|
||||
tcp_listener.drop_payload();
|
||||
|
||||
tcp_listener.set_tag(ArenaHeaderTag::Dropped);
|
||||
return Ok(());
|
||||
}
|
||||
_ => {
|
||||
@@ -7990,10 +7988,7 @@ impl Machine {
|
||||
pub(crate) fn load_library_as_stream(&mut self) -> CallResult {
|
||||
let library_name = cell_as_atom!(self.deref_register(1));
|
||||
|
||||
use crate::machine::LIBRARIES;
|
||||
|
||||
let lib_ref = LIBRARIES.borrow();
|
||||
let lib = lib_ref.get(&*library_name.as_str());
|
||||
let lib = libraries::get(&library_name.as_str());
|
||||
match lib {
|
||||
Some(library) => {
|
||||
let lib_stream = Stream::from_static_string(library, &mut self.machine_st.arena);
|
||||
|
||||
@@ -171,15 +171,18 @@ macro_rules! typed_arena_ptr_as_cell {
|
||||
}
|
||||
|
||||
macro_rules! raw_ptr_as_cell {
|
||||
($ptr:expr) => {
|
||||
($ptr:expr) => {{
|
||||
// Cell is 64-bit, but raw ptr is 32-bit in 32-bit systems
|
||||
HeapCellValue::from_raw_ptr_bytes(unsafe { std::mem::transmute($ptr) })
|
||||
};
|
||||
// TODO use <*{const,mut} _>::addr instead of as when the strict_provenance feature is stable rust-lang/rust#95228
|
||||
// we might need <*{const,mut} _>::expose_provenance for strict provenance, dependening on how we recreate a pointer later
|
||||
let ptr : *const _ = $ptr;
|
||||
HeapCellValue::from_ptr_addr(ptr as usize)
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! untyped_arena_ptr_as_cell {
|
||||
($ptr:expr) => {
|
||||
HeapCellValue::from_bytes(unsafe { std::mem::transmute($ptr) })
|
||||
HeapCellValue::from_bytes(UntypedArenaPtr::into_bytes($ptr))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -224,86 +227,69 @@ macro_rules! stream_as_cell {
|
||||
macro_rules! cell_as_stream {
|
||||
($cell:expr) => {{
|
||||
let ptr = cell_as_untyped_arena_ptr!($cell);
|
||||
Stream::from_tag(ptr.get_tag(), ptr.payload_offset())
|
||||
Stream::from_tag(ptr.get_tag(), ptr)
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! cell_as_load_state_payload {
|
||||
($cell:expr) => {
|
||||
unsafe {
|
||||
let ptr = cell_as_untyped_arena_ptr!($cell);
|
||||
let ptr = std::mem::transmute::<_, *mut LiveLoadState>(ptr.payload_offset());
|
||||
|
||||
TypedArenaPtr::new(ptr)
|
||||
}
|
||||
};
|
||||
($cell:expr) => {{
|
||||
let ptr = cell_as_untyped_arena_ptr!($cell);
|
||||
unsafe { ptr.as_typed_ptr::<LiveLoadState>() }
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! match_untyped_arena_ptr_pat_body {
|
||||
($ptr:ident, Integer, $n:ident, $code:expr) => {{
|
||||
let payload_ptr = unsafe { std::mem::transmute::<_, *mut Integer>($ptr.payload_offset()) };
|
||||
let $n = TypedArenaPtr::new(payload_ptr);
|
||||
let $n = unsafe { $ptr.as_typed_ptr::<Integer>() };
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, Rational, $n:ident, $code:expr) => {{
|
||||
let payload_ptr = unsafe { std::mem::transmute::<_, *mut Rational>($ptr.payload_offset()) };
|
||||
let $n = TypedArenaPtr::new(payload_ptr);
|
||||
let $n = unsafe { $ptr.as_typed_ptr::<Rational>() };
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, OssifiedOpDir, $n:ident, $code:expr) => {{
|
||||
let payload_ptr =
|
||||
unsafe { std::mem::transmute::<_, *mut OssifiedOpDir>($ptr.payload_offset()) };
|
||||
let $n = TypedArenaPtr::new(payload_ptr);
|
||||
let $n = unsafe { $ptr.as_typed_ptr::<OssifiedOpDir>() };
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, LiveLoadState, $n:ident, $code:expr) => {{
|
||||
let payload_ptr =
|
||||
unsafe { std::mem::transmute::<_, *mut LiveLoadState>($ptr.payload_offset()) };
|
||||
let $n = TypedArenaPtr::new(payload_ptr);
|
||||
let $n = unsafe { $ptr.as_typed_ptr::<LiveLoadState>() };
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, Stream, $s:ident, $code:expr) => {{
|
||||
let $s = Stream::from_tag($ptr.get_tag(), $ptr.payload_offset());
|
||||
let $s = Stream::from_tag($ptr.get_tag(), $ptr);
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, TcpListener, $listener:ident, $code:expr) => {{
|
||||
let payload_ptr =
|
||||
unsafe { std::mem::transmute::<_, *mut TcpListener>($ptr.payload_offset()) };
|
||||
#[allow(unused_mut)]
|
||||
let mut $listener = TypedArenaPtr::new(payload_ptr);
|
||||
let mut $listener = unsafe { $ptr.as_typed_ptr::<TcpListener>() };
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, HttpListener, $listener:ident, $code:expr) => {{
|
||||
let payload_ptr =
|
||||
unsafe { std::mem::transmute::<_, *mut HttpListener>($ptr.payload_offset()) };
|
||||
#[allow(unused_mut)]
|
||||
let mut $listener = TypedArenaPtr::new(payload_ptr);
|
||||
let mut $listener = unsafe { $ptr.as_typed_ptr::<HttpListener>() };
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, HttpResponse, $listener:ident, $code:expr) => {{
|
||||
let payload_ptr =
|
||||
unsafe { std::mem::transmute::<_, *mut HttpResponse>($ptr.payload_offset()) };
|
||||
#[allow(unused_mut)]
|
||||
let mut $listener = TypedArenaPtr::new(payload_ptr);
|
||||
let mut $listener = unsafe { $ptr.as_typed_ptr::<HttpResponse>() };
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, IndexPtr, $ip:ident, $code:expr) => {{
|
||||
#[allow(unused_mut)]
|
||||
let mut $ip =
|
||||
TypedArenaPtr::new(unsafe { std::mem::transmute::<_, *mut IndexPtr>($ptr.get_ptr()) });
|
||||
let mut $ip = unsafe { $ptr.as_typed_ptr::<IndexPtr>() };
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
($ptr:ident, $($tags:tt)|+, $s:ident, $code:expr) => {{
|
||||
let $s = Stream::from_tag($ptr.get_tag(), $ptr.payload_offset());
|
||||
let $s = Stream::from_tag($ptr.get_tag(), $ptr);
|
||||
#[allow(unused_braces)]
|
||||
$code
|
||||
}};
|
||||
@@ -338,6 +324,7 @@ macro_rules! match_untyped_arena_ptr {
|
||||
($ptr:expr, $( ($(ArenaHeaderTag::$tag:tt)|+, $n:ident) => $code:block $(,)?)+ $(_ => $misc_code:expr $(,)?)?) => ({
|
||||
let ptr_id = $ptr;
|
||||
|
||||
#[allow(clippy::toplevel_ref_arg)]
|
||||
match ptr_id.get_tag() {
|
||||
$($(match_untyped_arena_ptr_pat!($tag) => {
|
||||
match_untyped_arena_ptr_pat_body!(ptr_id, $tag, $n, $code)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![allow(clippy::new_without_default)] // annotating structs annotated with #[bitfield] doesn't work
|
||||
|
||||
use crate::arena::*;
|
||||
use crate::atom_table::*;
|
||||
use crate::machine::machine_indices::*;
|
||||
|
||||
@@ -379,7 +379,6 @@ mod tests {
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "slow and not very relevant")]
|
||||
fn plain_string() {
|
||||
let mut read_string = CharReader::new(Cursor::new("a string"));
|
||||
|
||||
@@ -392,7 +391,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "slow and not very relevant")]
|
||||
fn greek_string() {
|
||||
let mut read_string = CharReader::new(Cursor::new("λέξη"));
|
||||
|
||||
@@ -405,7 +403,6 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore = "slow and not very relevant")]
|
||||
fn russian_string() {
|
||||
let mut read_string = CharReader::new(Cursor::new("слово"));
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use lexical::parse_lossy;
|
||||
|
||||
use crate::arena::ArenaAllocated;
|
||||
use crate::atom_table::*;
|
||||
pub use crate::machine::machine_state::*;
|
||||
use crate::parser::ast::*;
|
||||
|
||||
@@ -96,9 +96,10 @@ impl<T: RawBlockTraits> RawBlock<T> {
|
||||
}
|
||||
|
||||
pub unsafe fn alloc(&self, size: usize) -> *mut u8 {
|
||||
if self.free_space() >= size {
|
||||
let aligned_size = size.next_multiple_of(size);
|
||||
if self.free_space() >= aligned_size {
|
||||
let ptr = *self.ptr.get();
|
||||
*self.ptr.get() = ptr.add(size) as *mut _;
|
||||
*self.ptr.get() = ptr.add(aligned_size) as *mut _;
|
||||
ptr
|
||||
} else {
|
||||
ptr::null_mut()
|
||||
|
||||
220
src/rcu.rs
220
src/rcu.rs
@@ -1,220 +0,0 @@
|
||||
use std::{
|
||||
cell::OnceCell,
|
||||
fmt::Debug,
|
||||
mem::ManuallyDrop,
|
||||
ops::Deref,
|
||||
ptr::NonNull,
|
||||
sync::{
|
||||
atomic::{AtomicPtr, AtomicU8},
|
||||
Arc, RwLock, Weak,
|
||||
},
|
||||
};
|
||||
|
||||
// the epoch counters of all threads that have ever accessed an Rcu
|
||||
// threads that have finished will have a dangling Weak reference and can be cleand up
|
||||
// having this be shared between all Rcu's is a tradeof,
|
||||
// writes will be slower as more epoch counters need to be waited for
|
||||
// reads should be faster as a thread only needs to register itself once on the first read
|
||||
//
|
||||
static EPOCH_COUNTERS: RwLock<Vec<Weak<AtomicU8>>> = RwLock::new(Vec::new());
|
||||
|
||||
thread_local! {
|
||||
// odd value means the current thread is about to access the active_epoch of an Rcu
|
||||
// a thread has a single epoch counter for all Rcu it accesses,
|
||||
// as a thread can only access one Rcu at a time
|
||||
static THREAD_EPOCH_COUNTER: OnceCell<Arc<AtomicU8>> = const { OnceCell::new() };
|
||||
}
|
||||
|
||||
pub struct Rcu<T> {
|
||||
active_value: AtomicPtr<T>,
|
||||
}
|
||||
|
||||
impl<T: std::fmt::Debug> std::fmt::Debug for Rcu<T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let active_epoch = self.active_epoch();
|
||||
f.debug_struct("Rcu")
|
||||
.field("active_value", &active_epoch)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Rcu<T> {
|
||||
pub fn new(initial_value: T) -> Self {
|
||||
Rcu {
|
||||
active_value: AtomicPtr::new(Arc::into_raw(Arc::new(initial_value)).cast_mut()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn active_epoch(&self) -> RcuRef<T, T> {
|
||||
THREAD_EPOCH_COUNTER.with(|epoch_counter| {
|
||||
let epoch_counter = epoch_counter.get_or_init(|| {
|
||||
let epoch_counter = Arc::new(AtomicU8::new(0));
|
||||
// register the current threads epoch counter on init
|
||||
EPOCH_COUNTERS
|
||||
.write()
|
||||
.unwrap()
|
||||
.push(Arc::downgrade(&epoch_counter));
|
||||
epoch_counter
|
||||
});
|
||||
|
||||
let old = epoch_counter.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
|
||||
assert!(old % 2 == 0, "Old Epoch counter value should be even!");
|
||||
});
|
||||
|
||||
let arc_ptr = self.active_value.load(std::sync::atomic::Ordering::Acquire);
|
||||
|
||||
let arc = unsafe {
|
||||
// Safety:
|
||||
// - the ptr was created in Rcu::new or Rcu::replace with Arc::into_raw
|
||||
// - the Rcu is responsible for of the arc's strong refrences
|
||||
// - the Rcu is alive as this function takes a reference to the Rcu
|
||||
// - replace will wait with decrementing the old values strong count until our epoich counter is even again
|
||||
Arc::increment_strong_count(arc_ptr);
|
||||
// Safety:
|
||||
// - the ptr was created in Rcu::new or Rcu::replace with Arc::into_raw
|
||||
// - we have just ensured an additional strong count by incrementing the count
|
||||
Arc::from_raw(arc_ptr)
|
||||
};
|
||||
|
||||
THREAD_EPOCH_COUNTER.with(|epoch_counter| {
|
||||
let old = epoch_counter
|
||||
.get().expect("we initialized the OnceCell when we incremented the epoch counter the fist time")
|
||||
.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
|
||||
assert!(old % 2 != 0, "Old Epoch counter value should be odd!");
|
||||
});
|
||||
|
||||
RcuRef {
|
||||
data: arc.deref().into(),
|
||||
arc,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* replace the Rcu'S content with a new value
|
||||
*
|
||||
* This does not syncronize write and last to update the active_value pointer wins,
|
||||
* all writes that do not win will be lost, though not leaked.
|
||||
* This will block untill the old value can be reclaimed,
|
||||
* i.e. all threads whitnest to be in the read critical sections
|
||||
* have been witnest to have left the critical section at least once
|
||||
*/
|
||||
pub fn replace(&self, new_value: T) {
|
||||
let arc_ptr = self.active_value.swap(
|
||||
Arc::into_raw(Arc::new(new_value)).cast_mut(),
|
||||
std::sync::atomic::Ordering::AcqRel,
|
||||
);
|
||||
|
||||
// maually drop as we need to ensure not to drop the arc while
|
||||
// we have not witnest all threads to be or have been outside the read critical section
|
||||
// i.e. even epoch counter or different odd epoch counter
|
||||
// Safety:
|
||||
// - the ptr was created in Rcu::new or Rcu::replace with Arc::into_raw
|
||||
// - the Rcu itself holds one strong count
|
||||
let arc = unsafe { ManuallyDrop::new(Arc::from_raw(arc_ptr)) };
|
||||
|
||||
let epochs = EPOCH_COUNTERS.read().unwrap().clone();
|
||||
let mut epochs = epochs
|
||||
.into_iter()
|
||||
.flat_map(|elem| {
|
||||
let arc = elem.upgrade()?;
|
||||
let init_val = arc.load(std::sync::atomic::Ordering::Acquire);
|
||||
if init_val % 2 == 0 {
|
||||
// already even can be ignored
|
||||
return None;
|
||||
}
|
||||
// odd initial value thread is in read critical section
|
||||
// need to wait for the value to change before we can drop the arc
|
||||
Some((init_val, elem))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
while !epochs.is_empty() {
|
||||
epochs.retain(|elem| {
|
||||
let Some(arc) = elem.1.upgrade() else {
|
||||
// as the thread is dead it can't have a ref to old arc
|
||||
return false;
|
||||
};
|
||||
// the epoch counter has not changed so the thread is still in the same instance of the critical section
|
||||
// any different value is ok as
|
||||
// - even values indicate the thread is outside the critical section
|
||||
// - a diffrent odd value indicates the thread has left the critical section and can subsequently only read the new active_value
|
||||
arc.load(std::sync::atomic::Ordering::Acquire) == elem.0
|
||||
})
|
||||
}
|
||||
|
||||
// Safety:
|
||||
// - we have not dropped the arc another way
|
||||
// - we witnessed all threads either with an even epoch count or with a new odd count
|
||||
// as such they must have left the critical section at some point
|
||||
ManuallyDrop::into_inner(arc);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RcuRef<T, M>
|
||||
where
|
||||
T: ?Sized,
|
||||
M: ?Sized,
|
||||
{
|
||||
arc: Arc<T>,
|
||||
data: NonNull<M>,
|
||||
}
|
||||
|
||||
impl<T: ?Sized, M: ?Sized + Debug> Debug for RcuRef<T, M> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("RcuRef")
|
||||
.field("data", &self.deref())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
// use assoiated functions rather than methods so that we don't overlap
|
||||
// with functions of the Deref Target type
|
||||
impl<T: ?Sized, M: ?Sized> RcuRef<T, M> {
|
||||
pub fn map<N: ?Sized, F: for<'a> FnOnce(&'a M) -> &'a N>(referece: Self, f: F) -> RcuRef<T, N> {
|
||||
RcuRef {
|
||||
arc: referece.arc,
|
||||
data: f(unsafe { referece.data.as_ref() }).into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_map<N: ?Sized, F: for<'a> FnOnce(&'a M) -> Option<&'a N>>(
|
||||
referece: Self,
|
||||
f: F,
|
||||
) -> Option<RcuRef<T, N>> {
|
||||
let val = f(unsafe { referece.data.as_ref() })?;
|
||||
Some(RcuRef {
|
||||
arc: Arc::clone(&referece.arc),
|
||||
data: val.into(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn same_epoch<M2>(this: &Self, other: &RcuRef<T, M2>) -> bool {
|
||||
Arc::ptr_eq(&this.arc, &other.arc)
|
||||
}
|
||||
|
||||
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
|
||||
this.data == other.data
|
||||
}
|
||||
|
||||
pub fn clone(this: &Self) -> Self {
|
||||
Self {
|
||||
arc: Arc::clone(&this.arc),
|
||||
data: this.data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_root(this: &Self) -> &T {
|
||||
&this.arc
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized, M: ?Sized> Deref for RcuRef<T, M> {
|
||||
type Target = M;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// Safety: The pointer points into the arc we are holding
|
||||
// while we are alive so is the target
|
||||
// as the content is in an Rcu no mutable acess is given out
|
||||
unsafe { self.data.as_ref() }
|
||||
}
|
||||
}
|
||||
76
src/types.rs
76
src/types.rs
@@ -1,3 +1,5 @@
|
||||
#![allow(clippy::new_without_default)] // annotating structs annotated with #[bitfield] doesn't work
|
||||
|
||||
use crate::arena::*;
|
||||
use crate::atom_table::*;
|
||||
use crate::forms::*;
|
||||
@@ -88,18 +90,10 @@ impl ConsPtr {
|
||||
.with_tag(tag)
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[inline(always)]
|
||||
pub fn as_ptr(self) -> *mut u8 {
|
||||
let bytes = self.into_bytes();
|
||||
let raw_ptr_bytes = [bytes[1], bytes[2], bytes[3], bytes[4]];
|
||||
unsafe { mem::transmute(raw_ptr_bytes) }
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
#[inline(always)]
|
||||
pub fn as_ptr(self) -> *mut u8 {
|
||||
self.ptr() as *mut _
|
||||
let addr: u64 = self.ptr();
|
||||
addr as usize as *mut _
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@@ -194,7 +188,7 @@ pub enum TrailRef {
|
||||
BlackboardOffset(Atom, HeapCellValue), // key atom, key value
|
||||
}
|
||||
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
#[allow(clippy::enum_variant_names)] // allow the common "Trailed" prefix
|
||||
#[derive(BitfieldSpecifier, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[bits = 6]
|
||||
pub(crate) enum TrailEntryTag {
|
||||
@@ -306,7 +300,10 @@ impl fmt::Debug for HeapCellValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ArenaAllocated> From<TypedArenaPtr<T>> for HeapCellValue {
|
||||
impl<T: ArenaAllocated> From<TypedArenaPtr<T>> for HeapCellValue
|
||||
where
|
||||
T::Payload: Sized,
|
||||
{
|
||||
#[inline]
|
||||
fn from(arena_ptr: TypedArenaPtr<T>) -> HeapCellValue {
|
||||
HeapCellValue::from(arena_ptr.header_ptr() as u64)
|
||||
@@ -534,37 +531,13 @@ impl HeapCellValue {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[inline]
|
||||
pub fn from_raw_ptr_bytes(ptr_bytes: [u8; 4]) -> Self {
|
||||
HeapCellValue::from_bytes([
|
||||
ptr_bytes[0],
|
||||
ptr_bytes[1],
|
||||
ptr_bytes[2],
|
||||
ptr_bytes[3],
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
])
|
||||
}
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
#[inline]
|
||||
pub fn from_raw_ptr_bytes(ptr_bytes: [u8; 8]) -> Self {
|
||||
HeapCellValue::from_bytes(ptr_bytes)
|
||||
pub fn from_ptr_addr(ptr_bytes: usize) -> Self {
|
||||
HeapCellValue::from_bytes((ptr_bytes as u64).to_ne_bytes())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
pub fn to_raw_ptr_bytes(self) -> [u8; 4] {
|
||||
let bytes = self.into_bytes();
|
||||
[bytes[0], bytes[1], bytes[2], bytes[3]]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
pub fn to_raw_ptr_bytes(self) -> [u8; 8] {
|
||||
self.into_bytes()
|
||||
pub fn to_ptr_addr(self) -> usize {
|
||||
u64::from_ne_bytes(self.into_bytes()) as usize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -716,18 +689,10 @@ impl UntypedArenaPtr {
|
||||
self.set_m(m);
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
#[inline]
|
||||
pub fn get_ptr(self) -> *const u8 {
|
||||
let bytes = self.into_bytes();
|
||||
let raw_ptr_bytes = [bytes[0], bytes[1], bytes[2], bytes[3]];
|
||||
unsafe { mem::transmute(raw_ptr_bytes) }
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
#[inline]
|
||||
pub fn get_ptr(self) -> *const u8 {
|
||||
self.ptr() as *const u8
|
||||
let addr: u64 = self.ptr();
|
||||
addr as usize as *const u8
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -743,6 +708,17 @@ impl UntypedArenaPtr {
|
||||
unsafe { self.get_ptr().add(mem::size_of::<ArenaHeader>()) }
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// - this UntypedArenaPtr actuall pointee type is T
|
||||
/// - the pointer must be non-null
|
||||
#[inline]
|
||||
pub unsafe fn as_typed_ptr<T: ?Sized + ArenaAllocated>(self) -> TypedArenaPtr<T>
|
||||
where
|
||||
T::Payload: Sized,
|
||||
{
|
||||
T::typed_ptr(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_mark_bit(self) -> bool {
|
||||
self.m()
|
||||
|
||||
Reference in New Issue
Block a user