12 /* Page allocator. Hands out memory in page-size chunks.
13 See malloc.h for an allocator that hands out smaller
16 /* A free page owned by the page allocator. */
19 list_elem free_elem; /* Free list element. */
22 static struct lock lock;
23 static struct list free_pages;
24 static uint8_t *uninit_start, *uninit_end;
29 /* Kernel static code and data, in 4 kB pages.
31 We can figure this out because the linker records the start
32 and end of the kernel as _start and _end. See
34 extern char _start, _end;
35 size_t kernel_pages = (&_end - &_start + 4095) / 4096;
37 /* Then we know how much is available to allocate. */
38 uninit_start = ptov (LOADER_KERN_BASE + kernel_pages * PGSIZE);
39 uninit_end = ptov (ram_pages * PGSIZE);
41 /* Initialize other variables. */
42 lock_init (&lock, "palloc");
43 list_init (&free_pages);
47 palloc_get (enum palloc_flags flags)
53 if (!list_empty (&free_pages))
54 page = list_entry (list_pop_front (&free_pages), struct page, free_elem);
55 else if (uninit_start < uninit_end)
57 page = (struct page *) uninit_start;
58 uninit_start += PGSIZE;
66 memset (page, 0, PGSIZE);
70 if (flags & PAL_ASSERT)
71 PANIC ("palloc_get: out of pages");
80 palloc_free (void *page_)
82 struct page *page = page_;
84 ASSERT (page == pg_round_down (page));
86 memset (page, 0xcc, PGSIZE);
90 list_push_front (&free_pages, &page->free_elem);