1 // Physical memory allocator, intended to allocate
2 // memory for user processes, kernel stacks, page table pages,
3 // and pipe buffers. Allocates 4096-byte pages.
20 extern char end
[]; // first address after kernel loaded from ELF file
22 // Initialize free list of physical pages.
28 initlock(&kmem
.lock
, "kmem");
29 p
= (char*)PGROUNDUP((uint
)end
);
30 for(; p
+ PGSIZE
<= (char*)PHYSTOP
; p
+= PGSIZE
)
34 // Free the page of physical memory pointed at by v,
35 // which normally should have been returned by a
36 // call to kalloc(). (The exception is when
37 // initializing the allocator; see kinit above.)
43 if((uint
)v
% PGSIZE
|| v
< end
|| (uint
)v
>= PHYSTOP
)
46 // Fill with junk to catch dangling refs.
51 r
->next
= kmem
.freelist
;
56 // Allocate one 4096-byte page of physical memory.
57 // Returns a pointer that the kernel can use.
58 // Returns 0 if the memory cannot be allocated.
67 kmem
.freelist
= r
->next
;