Sfoglia il codice sorgente

Better debug assertions and consistency checks

ticki 9 anni fa
parent
commit
c541cec1d6
1 ha cambiato i file con 24 aggiunte e 7 eliminazioni
  1. 24 7
      src/bookkeeper.rs

+ 24 - 7
src/bookkeeper.rs

@@ -144,6 +144,8 @@ impl BlockList {
     /// necessary to avoid meta-circular dependency.
     // TODO can this be done in a more elegant way?
     fn init(&mut self) {
+        debug_assert!(self.cap == 0, "Capacity is non-zero on initialization.");
+
         /// The initial capacity.
         const INITIAL_CAPACITY: usize = 16;
 
@@ -162,6 +164,8 @@ impl BlockList {
             size: aligner,
             ptr: brk,
         });
+
+        self.check();
     }
 
     /// *[See `Bookkeeper`'s respective method.](./struct.Bookkeeper.html#method.alloc)*
@@ -258,11 +262,17 @@ impl BlockList {
     /// This will append a block entry to the end of the block list. Make sure that this entry has
     /// a value higher than any of the elements in the list, to keep it sorted.
     fn push(&mut self, block: Block) {
-        let len = self.len;
-        // This is guaranteed not to overflow, since `len` is bounded by the address space, since
-        // each entry represent at minimum one byte, meaning that `len` is bounded by the address
-        // space.
-        self.reserve(len + 1);
+        // Some assertions.
+        debug_assert!(block.size != 0, "Pushing a zero sized block.");
+        debug_assert!(self.last().map_or(0, |x| *x.ptr as usize) <= *block.ptr as usize, "The previous last block is higher than the new.");
+
+        {
+            let len = self.len;
+            // This is guaranteed not to overflow, since `len` is bounded by the address space, since
+            // each entry represent at minimum one byte, meaning that `len` is bounded by the address
+            // space.
+            self.reserve(len + 1);
+        }
 
         unsafe {
             ptr::write((*self.ptr as usize + size_of::<Block>() * self.len) as *mut _, block);
@@ -302,8 +312,8 @@ impl BlockList {
             ptr: ptr,
         };
         let res = Block {
-            ptr: alignment_block.end(),
             size: size,
+            ptr: alignment_block.end(),
         };
 
         // Add it to the list. This will not change the order, since the pointer is higher than all
@@ -554,6 +564,7 @@ impl BlockList {
     ///
     /// See [`free`](#method.free) for more information.
     fn free_ind(&mut self, ind: usize, block: Block) {
+        // Make some handy assertions.
         debug_assert!(*self[ind].ptr != *block.ptr || !self[ind].is_free(), "Double free.");
 
         // Try to merge right.
@@ -627,6 +638,10 @@ impl BlockList {
     ///
     /// The insertion is now completed.
     fn insert(&mut self, ind: usize, block: Block) {
+        // Some assertions...
+        debug_assert!(block >= self[ind.saturating_sub(1)], "Inserting at {} will make the list unsorted.", ind);
+        debug_assert!(self.find(&block) == ind, "Block is not inserted at the appropriate index.");
+
         // TODO consider moving right before searching left.
 
         // Find the next gap, where a used block were.
@@ -670,10 +685,12 @@ impl BlockList {
     /// 3. The length does not exceed the capacity.
     #[cfg(debug_assertions)]
     fn check(&self) {
+        if self.len == 0 { return; }
+
         // Check if sorted.
         let mut prev = *self[0].ptr;
         for (n, i) in self.iter().enumerate().skip(1) {
-            assert!(*i.ptr > prev, "The block list is not sorted at index, {}.", n);
+            assert!(*i.ptr > prev, "The block list is not sorted at index, {}: 0x{:x} ≤ 0x{:x}.", n, *i.ptr as usize, prev as usize);
             prev = *i.ptr;
         }
         // Check if overlapping.