PatchworkOS
966e257
A non-POSIX operating system.
Theme:
Default
Round
Robot
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
*/
21
typedef
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[];
31
}
page_buffer_t
;
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
*/
41
typedef
struct
42
{
43
/**
44
* @brief Pointer to the last page buffer in the stack.
45
*/
46
page_buffer_t
*
last
;
47
/**
48
* @brief Current index within the `pages` array of the `last` page buffer.
49
*/
50
uint64_t
index
;
51
/**
52
* @brief The number of free pages in the stack.
53
*/
54
uint64_t
free
;
55
}
pmm_stack_t
;
56
57
/**
58
* @brief Initializes a PMM stack.
59
*
60
* @param stack The stack to initialize.
61
*/
62
void
pmm_stack_init
(
pmm_stack_t
*
stack
);
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
*/
70
void
*
pmm_stack_alloc
(
pmm_stack_t
*
stack
);
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
*/
78
void
pmm_stack_free
(
pmm_stack_t
*
stack
,
void
*
address
);
79
80
/** @} */
pmm_stack_alloc
void * pmm_stack_alloc(pmm_stack_t *stack)
Allocates a single page from the stack.
Definition
pmm_stack.c:12
pmm_stack_init
void pmm_stack_init(pmm_stack_t *stack)
Initializes a PMM stack.
Definition
pmm_stack.c:5
pmm_stack_free
void pmm_stack_free(pmm_stack_t *stack, void *address)
Frees a single page, returning it to the stack.
Definition
pmm_stack.c:36
address
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition
hpet.c:95
stack
static pmm_stack_t stack
Definition
pmm.c:38
proc.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
page_buffer_t
Structure for a page buffer in the PMM stack.
Definition
pmm_stack.h:22
page_buffer_t::prev
struct page_buffer * prev
Pointer to the previous page buffer in the stack.
Definition
pmm_stack.h:26
pmm_stack_t
PMM stack structure for managing higher physical memory.
Definition
pmm_stack.h:42
pmm_stack_t::free
uint64_t free
The number of free pages in the stack.
Definition
pmm_stack.h:54
pmm_stack_t::last
page_buffer_t * last
Pointer to the last page buffer in the stack.
Definition
pmm_stack.h:46
pmm_stack_t::index
uint64_t index
Current index within the pages array of the last page buffer.
Definition
pmm_stack.h:50
include
kernel
mem
pmm_stack.h
Generated on Mon Dec 15 2025 21:55:53 for PatchworkOS by
1.9.8