Add DEBUG() macro for debugging that can be turned on and off with a
[pintos-anon] / src / threads / palloc.c
1 #include "palloc.h"
2 #include <stddef.h>
3 #include <stdint.h>
4 #include "debug.h"
5 #include "init.h"
6 #include "loader.h"
7 #include "lib.h"
8 #include "mmu.h"
9
10 /* A free page owned by the page allocator. */
11 struct page
12   {
13     struct page *next;  /* Next free page, or null at end of chain. */
14   };
15
16 static struct page *free_pages;
17 static uint8_t *uninit_start, *uninit_end;
18
19 void
20 palloc_init (void) 
21 {
22   /* Kernel static code and data, in 4 kB pages.
23      
24      We can figure this out because the linker records the start
25      and end of the kernel as _start and _end.  See
26      kernel.lds. */
27   extern char _start, _end;
28   size_t kernel_pages;  
29   kernel_pages = (&_end - &_start + 4095) / 4096;
30
31   /* Then we know how much is available to allocate. */
32   uninit_start = ptov (LOADER_KERN_BASE + kernel_pages * PGSIZE);
33   uninit_end = ptov (ram_pages * PGSIZE);
34 }
35
36 void *
37 palloc_get (enum palloc_flags flags)
38 {
39   struct page *page;
40
41   if (free_pages == NULL && uninit_start < uninit_end) 
42     {
43       palloc_free (uninit_start);
44       uninit_start += PGSIZE;
45     }
46
47   page = free_pages;
48   if (page != NULL) 
49     {
50       free_pages = page->next;
51       if (flags & PAL_ZERO)
52         memset (page, 0, PGSIZE);
53     }
54   else 
55     {
56       if (flags & PAL_ASSERT)
57         PANIC ("palloc_get: out of pages");
58     }
59   
60   return page;
61 }
62
63 void
64 palloc_free (void *page_) 
65 {
66   struct page *page = page_;
67   ASSERT((uintptr_t) page % PGSIZE == 0);
68 #ifndef NDEBUG
69   memset (page, 0xcc, PGSIZE);
70 #endif
71   page->next = free_pages;
72   free_pages = page;
73 }