Use lib.h instead of <string.h>.
[pintos-anon] / src / threads / palloc.c
1 #include "palloc.h"
2 #include <stddef.h>
3 #include <stdint.h>
4 #include "debug.h"
5 #include "lib.h"
6 #include "mmu.h"
7
8 /* A free page owned by the page allocator. */
9 struct page
10   {
11     struct page *next;  /* Next free page, or null at end of chain. */
12   };
13
14 static struct page *free_pages;
15 static uint8_t *uninit_start, *uninit_end;
16
17 void
18 palloc_init (uint8_t *start, uint8_t *end) 
19 {
20   uninit_start = start;
21   uninit_end = end;
22 }
23
24 void *
25 palloc_get (enum palloc_flags flags)
26 {
27   struct page *page;
28
29   if (free_pages == NULL && uninit_start < uninit_end) 
30     {
31       palloc_free (uninit_start);
32       uninit_start += PGSIZE;
33     }
34
35   page = free_pages;
36   if (page != NULL) 
37     {
38       free_pages = page->next;
39       if (flags & PAL_ZERO)
40         memset (page, 0, PGSIZE);
41     }
42   else 
43     {
44       if (flags & PAL_ASSERT)
45         panic ("palloc_get: out of pages");
46     }
47   
48   return page;
49 }
50
51 void
52 palloc_free (void *page_) 
53 {
54   struct page *page = page_;
55   ASSERT((uintptr_t) page % PGSIZE == 0);
56 #ifndef NDEBUG
57   memset (page, 0xcc, PGSIZE);
58 #endif
59   page->next = free_pages;
60   free_pages = page;
61 }