PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
pmm_stack.h
Go to the documentation of this file.
1#pragma once
2
3#include <sys/proc.h>
4
5/**
6 * @brief A generic free stack page allocator.
7 * @defgroup kernel_mem_pmm_stack PMM Stack
8 * @ingroup kernel_mem
9 *
10 * The PMM stack provides a fast, O(1) allocator for single pages. It uses freed pages to store metadata about other
11 * free pages, forming a stack of page buffers.
12 *
13 * @{
14 */
15
16/**
17 * @brief Structure for a page buffer in the PMM stack.
18 *
19 * The `page_buffer_t` structure is stored in free pages and keeps track of pages that are currently freed.
20 */
21typedef struct page_buffer
22{
23 /**
24 * @brief Pointer to the previous page buffer in the stack.
25 */
26 struct page_buffer* prev;
27 /**
28 * @brief Flexible array member to store free physical pages.
29 */
30 void* pages[];
32
33/**
34 * @brief The maximum number of pages that can be stored in a `page_buffer_t`.
35 */
36#define PMM_BUFFER_MAX ((PAGE_SIZE - sizeof(page_buffer_t)) / sizeof(void*))
37
38/**
39 * @brief PMM stack structure for managing higher physical memory.
40 */
41typedef struct
42{
43 /**
44 * @brief Pointer to the last page buffer in the stack.
45 */
47 /**
48 * @brief Current index within the `pages` array of the `last` page buffer.
49 */
51 /**
52 * @brief The number of free pages in the stack.
53 */
56
57/**
58 * @brief Initializes a PMM stack.
59 *
60 * @param stack The stack to initialize.
61 */
63
64/**
65 * @brief Allocates a single page from the stack.
66 *
67 * @param stack The stack to allocate from.
68 * @return On success, a pointer to the allocated page. On failure `NULL` and errno is set.
69 */
71
72/**
73 * @brief Frees a single page, returning it to the stack.
74 *
75 * @param stack The stack to free to.
76 * @param address The address of the page to free.
77 */
79
80/** @} */
void * pmm_stack_alloc(pmm_stack_t *stack)
Allocates a single page from the stack.
Definition pmm_stack.c:12
void pmm_stack_init(pmm_stack_t *stack)
Initializes a PMM stack.
Definition pmm_stack.c:5
void pmm_stack_free(pmm_stack_t *stack, void *address)
Frees a single page, returning it to the stack.
Definition pmm_stack.c:36
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:95
static pmm_stack_t stack
Definition pmm.c:38
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Structure for a page buffer in the PMM stack.
Definition pmm_stack.h:22
struct page_buffer * prev
Pointer to the previous page buffer in the stack.
Definition pmm_stack.h:26
PMM stack structure for managing higher physical memory.
Definition pmm_stack.h:42
uint64_t free
The number of free pages in the stack.
Definition pmm_stack.h:54
page_buffer_t * last
Pointer to the last page buffer in the stack.
Definition pmm_stack.h:46
uint64_t index
Current index within the pages array of the last page buffer.
Definition pmm_stack.h:50