|
Kernelite 0.1.0
Kernelite is a minimalist, educational operating system kernel built from scratch. The project aims to recreate core components of the Linux architecture to explore the fundamentals of operating system design and hardware-software interaction.
|
Kernel heap allocator implementation. More...
#include "allocator/kalloc.h"#include "allocator/page_allocator.h"#include "page_table/page_table.h"#include "utils/kprintf.h"#include "utils/string.h"#include <stdbool.h>#include <stddef.h>#include <stdint.h>
Data Structures | |
| struct | allocated_block_desc_t |
| Bookkeeping descriptor for a single kmalloc() allocation. More... | |
| struct | kalloc_management_pages_t |
| A page of descriptors used to track live kmalloc() allocations. More... | |
Macros | |
| #define | MAX_MANAGEMENT_BLOCKS_PER_PAGE |
| Number of block descriptors that fit in a single management page. | |
Typedefs | |
| typedef struct allocated_block_desc_t | allocated_block_desc_t |
| Bookkeeping descriptor for a single kmalloc() allocation. | |
| typedef struct kalloc_management_pages_t | kalloc_management_pages_t |
| A page of descriptors used to track live kmalloc() allocations. | |
Functions | |
| static kalloc_management_pages_t * | alloc_management_page (void) |
| Allocates and zero-initializes a new management page. | |
| static allocated_block_desc_t * | find_free_management_block (void) |
| Finds an unused block descriptor, growing the pool if needed. | |
| static void | free_management_page_if_empty (allocated_block_desc_t *removed) |
| Returns a management page to the page allocator once it is empty. | |
| static void | add_to_allocated_list (allocated_block_desc_t *block) |
| Appends a block descriptor to the tail of the allocated list. | |
| static allocated_block_desc_t * | remove_from_allocated_list (void *ptr) |
Removes the descriptor for ptr from the allocated list. | |
| void * | kmalloc (size_t size) |
| Allocates a block of memory from the kernel heap. | |
| void | kfree (void *ptr) |
| Frees a block of memory previously returned by kmalloc(). | |
Variables | |
| static allocated_block_desc_t * | allocated = NULL |
| Head of the linked list of live allocation descriptors. | |
| static kalloc_management_pages_t * | kalloc_page_head = NULL |
| Head of the linked list of management pages. | |
Kernel heap allocator implementation.
This module implements the kernel-side dynamic memory API declared in kalloc.h. It hands out variable-sized allocations from the kernel heap and returns them for reuse, layering on top of the physical page allocator.
| #define MAX_MANAGEMENT_BLOCKS_PER_PAGE |
Number of block descriptors that fit in a single management page.
A management page holds an array of allocated_block_desc_t plus a pointer to the next page; this is how many descriptors fill the remaining space.
|
static |
Appends a block descriptor to the tail of the allocated list.
| block | The descriptor to add. A NULL block is ignored. |
|
static |
Allocates and zero-initializes a new management page.
Reserves one physical page for storing allocation descriptors and maps it to its kernel virtual address.
|
static |
Finds an unused block descriptor, growing the pool if needed.
Scans the existing management pages for a free descriptor and marks it used. If none are free, a new management page is allocated and appended.
|
static |
Returns a management page to the page allocator once it is empty.
Locates the management page holding removed by masking the descriptor address down to its page base. If none of that page's descriptors are still in use, the page is unlinked from the management-page chain and its physical page is freed.
| removed | A descriptor that was just marked free. A NULL removed is ignored. |
| void kfree | ( | void * | ptr | ) |
| void * kmalloc | ( | size_t | size | ) |
Allocates a block of memory from the kernel heap.
Reserves at least size bytes of contiguous memory for the caller.
| size | The number of bytes to allocate. |
|
static |
Removes the descriptor for ptr from the allocated list.
Unlinks the descriptor whose allocation starts at ptr and marks it free.
| ptr | Virtual address returned by a previous kmalloc(). |
ptr was not found.