51 size_t free_pages = (free_end - free_start) /
PGSIZE;
52 size_t user_pages = free_pages / 2;
56 kernel_pages = free_pages - user_pages;
59 init_pool (&kernel_pool, free_start, kernel_pages,
"kernel pool");
61 user_pages,
"user pool");
97 PANIC (
"palloc_get: out of pages");
124 if (pages ==
NULL || page_cnt == 0)
160 if (bm_pages > page_cnt)
161 PANIC (
"Not enough memory in %s for bitmap.",
name);
162 page_cnt -= bm_pages;
164 printf (
"%zu pages available in %s.\n", page_cnt,
name);
177 size_t page_no =
pg_no (page);
181 return page_no >= start_page && page_no < end_page;
size_t bitmap_size(const struct bitmap *b)
Bitmap size.
void bitmap_set_multiple(struct bitmap *b, size_t start, size_t cnt, bool value)
Sets the CNT bits starting at START in B to VALUE.
size_t bitmap_buf_size(size_t bit_cnt)
Returns the number of bytes required to accomodate a bitmap with BIT_CNT bits (for use with bitmap_cr...
size_t bitmap_scan_and_flip(struct bitmap *b, size_t start, size_t cnt, bool value)
Finds the first group of CNT consecutive bits in B at or after START that are all set to VALUE,...
bool bitmap_all(const struct bitmap *b, size_t start, size_t cnt)
Returns true if every bit in B between START and START + CNT, exclusive, is set to true,...
struct bitmap * bitmap_create_in_buf(size_t bit_cnt, void *block, size_t block_size UNUSED)
Creates and returns a bitmap with BIT_CNT bits in the BLOCK_SIZE bytes of storage preallocated at BLO...
#define BITMAP_ERROR
Finding set or unset bits.
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
#define NOT_REACHED()
lib/debug.h
#define PANIC(...)
Halts the OS, printing the source file name, line number, and function name, plus a user-specific mes...
static size_t user_page_limit
-ul: Maximum number of pages to put into palloc's user pool.
int printf(const char *format,...)
Writes formatted output to the console.
uint32_t init_ram_pages
Amount of physical memory, in 4 kB pages.
void * palloc_get_page(enum palloc_flags flags)
Obtains a single free page and returns its kernel virtual address.
static bool page_from_pool(const struct pool *, void *page)
Returns true if PAGE was allocated from POOL, false otherwise.
void * palloc_get_multiple(enum palloc_flags flags, size_t page_cnt)
Obtains and returns a group of PAGE_CNT contiguous free pages.
static struct pool kernel_pool user_pool
Two pools: one for kernel data, one for user pages.
void palloc_init(size_t user_page_limit)
Initializes the page allocator.
void palloc_free_page(void *page)
Frees the page at PAGE.
static void init_pool(struct pool *, void *base, size_t page_cnt, const char *name)
Initializes pool P as starting at START and ending at END, naming it NAME for debugging purposes.
void palloc_free_multiple(void *pages, size_t page_cnt)
Frees the PAGE_CNT pages starting at PAGES.
palloc_flags
How to allocate pages.
@ PAL_ZERO
Zero page contents.
@ PAL_ASSERT
Panic on failure.
#define DIV_ROUND_UP(X, STEP)
Yields X divided by STEP, rounded up.
void * memset(void *dst_, int value, size_t size)
Sets the SIZE bytes in DST to VALUE.
From the outside, a bitmap is an array of bits.
struct bitmap * used_map
Bitmap of free pages.
uint8_t * base
Base of pool.
struct lock lock
Mutual exclusion.
void lock_release(struct lock *lock)
Releases LOCK, which must be owned by the current thread.
void lock_init(struct lock *lock)
Initializes LOCK.
void lock_acquire(struct lock *lock)
Acquires LOCK, sleeping until it becomes available if necessary.
static uintptr_t pg_no(const void *va)
Virtual page number.
#define PGSIZE
Bytes in a page.
static unsigned pg_ofs(const void *va)
Offset within a page.
static void * ptov(uintptr_t paddr)
Returns kernel virtual address at which physical address PADDR is mapped.