Clean up disk layer.
[pintos-anon] / src / threads / palloc.c
index 0fe5f94b9e9f83c4553c537353957475c21f07aa..62e24cada4756dfa5917f74fac03512ce7626ad4 100644 (file)
@@ -1,8 +1,10 @@
 #include "palloc.h"
 #include <stddef.h>
 #include <stdint.h>
-#include <string.h>
 #include "debug.h"
+#include "init.h"
+#include "loader.h"
+#include "lib.h"
 #include "mmu.h"
 
 /* A free page owned by the page allocator. */
@@ -15,10 +17,20 @@ static struct page *free_pages;
 static uint8_t *uninit_start, *uninit_end;
 
 void
-palloc_init (uint8_t *start, uint8_t *end) 
+palloc_init (void) 
 {
-  uninit_start = start;
-  uninit_end = end;
+  /* Kernel static code and data, in 4 kB pages.
+     
+     We can figure this out because the linker records the start
+     and end of the kernel as _start and _end.  See
+     kernel.lds. */
+  extern char _start, _end;
+  size_t kernel_pages;  
+  kernel_pages = (&_end - &_start + 4095) / 4096;
+
+  /* Then we know how much is available to allocate. */
+  uninit_start = ptov (LOADER_KERN_BASE + kernel_pages * PGSIZE);
+  uninit_end = ptov (ram_pages * PGSIZE);
 }
 
 void *
@@ -29,7 +41,7 @@ palloc_get (enum palloc_flags flags)
   if (free_pages == NULL && uninit_start < uninit_end) 
     {
       palloc_free (uninit_start);
-      uninit_start += NBPG;
+      uninit_start += PGSIZE;
     }
 
   page = free_pages;
@@ -37,12 +49,12 @@ palloc_get (enum palloc_flags flags)
     {
       free_pages = page->next;
       if (flags & PAL_ZERO)
-        memset (page, 0, NBPG);
+        memset (page, 0, PGSIZE);
     }
   else 
     {
       if (flags & PAL_ASSERT)
-        panic ("palloc_get: out of pages");
+        PANIC ("palloc_get: out of pages");
     }
   
   return page;
@@ -52,9 +64,9 @@ void
 palloc_free (void *page_) 
 {
   struct page *page = page_;
-  ASSERT((uintptr_t) page % NBPG == 0);
+  ASSERT((uintptr_t) page % PGSIZE == 0);
 #ifndef NDEBUG
-  memset (page, 0xcc, NBPG);
+  memset (page, 0xcc, PGSIZE);
 #endif
   page->next = free_pages;
   free_pages = page;