]> Repositorios git - scryer-prolog.git/commitdiff
allow the frontier of the RawVec to be offset by a trait function
authorMark Thom <[email protected]>
Thu, 13 Feb 2020 06:52:30 +0000 (23:52 -0700)
committerMark Thom <[email protected]>
Thu, 13 Feb 2020 06:52:30 +0000 (23:52 -0700)
src/prolog/machine/raw_vec.rs
src/prolog/machine/stack.rs

index d902d4cf7d7cdc26d0b1611540017a0b1067e627..f482dfa405c2fd1dae8b551b3abe03943064719a 100644 (file)
@@ -7,6 +7,7 @@ use std::ptr;
 pub(crate) trait RawVecTraits {
     fn init_size() -> usize;
     fn align() -> usize;
+    fn base_offset(base: *const u8) -> *const u8;
 }
 
 pub(crate) struct RawVec<T: RawVecTraits> {
@@ -24,7 +25,7 @@ impl<T: RawVecTraits> RawVec<T> {
                                top: ptr::null(),
                                _marker: PhantomData };
 
-        unsafe { 
+        unsafe {
             vec.grow();
         }
 
@@ -39,11 +40,11 @@ impl<T: RawVecTraits> RawVec<T> {
             self.base = alloc::alloc(layout) as *const _;
             self.size = T::init_size();
 
-            self.top = self.base.offset(T::align() as isize);
+            self.top = T::base_offset(self.base);
         } else {
             let layout = alloc::Layout::from_size_align_unchecked(T::init_size(), T::align());
             let top_dist = self.top as usize - self.base as usize;
-            
+
             self.base = alloc::realloc(self.base as *mut _, layout, self.size*2) as *const _;
             self.top = (self.base as usize + top_dist) as *const _;
             self.size *= 2;
@@ -75,10 +76,10 @@ impl<T: RawVecTraits> RawVec<T> {
 
     #[inline]
     pub(crate)
-    unsafe fn new_block(&mut self, block_size: usize) -> *const u8 {
+    unsafe fn new_block(&mut self, size: usize) -> *const u8 {
         loop {
-            if self.free_space() >= block_size {
-                return (self.top as usize + block_size) as *const _;
+            if self.free_space() >= size {
+                return (self.top as usize + size) as *const _;
             } else {
                 self.grow();
             }
@@ -96,5 +97,5 @@ impl<T: RawVecTraits> RawVec<T> {
             self.base = ptr::null();
             self.size = 0;
         }
-    }    
+    }
 }
index 15b96ad94b498912ea300afa007c3c82dcd1e3cd..5dca3cee34260391fc7f020500f6b542d93584e2 100644 (file)
@@ -19,6 +19,13 @@ impl RawVecTraits for StackTraits {
     fn align() -> usize {
         mem::align_of::<Addr>()
     }
+
+    #[inline]
+    fn base_offset(base: *const u8) -> *const u8 {
+        unsafe {
+            base.offset(Self::align() as isize)
+        }
+    }
 }
 
 const fn prelude_size<Prelude>() -> usize {