From d6b9768dc3e9cd967df0acf3eafb10d02c9f0ba6 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Tue, 19 Sep 2017 20:21:04 -0600
Subject: [PATCH] More efficient live filesystem method Reduce kernel heap to
 64 MB Fix issue in build.rs

---
 build.rs           |  6 ++----
 src/consts.rs      |  5 +----
 src/scheme/live.rs | 29 ++++++++++++++++++++++-------
 3 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/build.rs b/build.rs
index 29667696..cddbfab0 100644
--- a/build.rs
+++ b/build.rs
@@ -87,6 +87,8 @@ fn fill_from_location(f: &mut fs::File, loc: &Path ) -> Result<(), (Error)> {
 }
 
 fn main() {
+    println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
+
     let out_dir = env::var("OUT_DIR").unwrap();
     let dest_path = Path::new(&out_dir).join("gen.rs");
     let mut f = fs::File::create(&dest_path).unwrap();
@@ -114,8 +116,4 @@ b"        files.clear();" // Silence mutability warning
     }
 }
     ").unwrap();
-
-    fn main() {
-        println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
-    }
 }
diff --git a/src/consts.rs b/src/consts.rs
index c8acb394..be856c35 100644
--- a/src/consts.rs
+++ b/src/consts.rs
@@ -15,10 +15,7 @@
     /// Offset to kernel heap
     pub const KERNEL_HEAP_OFFSET: usize = KERNEL_OFFSET + PML4_SIZE/2;
     /// Size of kernel heap
-    #[cfg(not(feature = "live"))]
-    pub const KERNEL_HEAP_SIZE: usize = 128 * 1024 * 1024; // 128 MB
-    #[cfg(feature = "live")]
-    pub const KERNEL_HEAP_SIZE: usize = 640 * 1024 * 1024; // 640 MB - 128 default + 512 for the live disk
+    pub const KERNEL_HEAP_SIZE: usize = 64 * 1024 * 1024; // 64 MB
 
     /// Offset to kernel percpu variables
     //TODO: Use 64-bit fs offset to enable this pub const KERNEL_PERCPU_OFFSET: usize = KERNEL_HEAP_OFFSET - PML4_SIZE;
diff --git a/src/scheme/live.rs b/src/scheme/live.rs
index 93835ca4..4ea1eef3 100644
--- a/src/scheme/live.rs
+++ b/src/scheme/live.rs
@@ -1,8 +1,8 @@
 /// Disk scheme replacement when making live disk
 
 use alloc::arc::Arc;
-use collections::{BTreeMap, Vec};
-use core::cmp;
+use collections::BTreeMap;
+use core::{cmp, slice};
 use core::sync::atomic::{AtomicUsize, Ordering};
 use spin::RwLock;
 
@@ -11,26 +11,41 @@ use syscall::error::*;
 use syscall::flag::{MODE_FILE, SEEK_SET, SEEK_CUR, SEEK_END};
 use syscall::scheme::Scheme;
 
-static FILESYSTEM: &'static [u8] = include_bytes!(env!("FILESYSTEM"));
-
 struct Handle {
     path: &'static [u8],
-    data: Arc<RwLock<Vec<u8>>>,
+    data: Arc<RwLock<&'static mut [u8]>>,
     mode: u16,
     seek: usize
 }
 
 pub struct DiskScheme {
     next_id: AtomicUsize,
-    data: Arc<RwLock<Vec<u8>>>,
+    data: Arc<RwLock<&'static mut [u8]>>,
     handles: RwLock<BTreeMap<usize, Handle>>
 }
 
 impl DiskScheme {
     pub fn new() -> DiskScheme {
+        let data;
+        unsafe {
+            extern {
+                static mut __live_start: u8;
+                static mut __live_end: u8;
+            }
+
+            let start = &mut __live_start as *mut u8;
+            let end = &mut __live_end as *mut u8;
+
+            if end as usize >= start as usize {
+                data = slice::from_raw_parts_mut(start, end as usize - start as usize);
+            } else {
+                data = &mut [];
+            };
+        }
+
         DiskScheme {
             next_id: AtomicUsize::new(0),
-            data: Arc::new(RwLock::new(FILESYSTEM.to_vec())),
+            data: Arc::new(RwLock::new(data)),
             handles: RwLock::new(BTreeMap::new())
         }
     }
-- 
GitLab