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.
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions | Variables
kalloc.c File Reference

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>
Include dependency graph for kalloc.c:

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_talloc_management_page (void)
 Allocates and zero-initializes a new management page.
 
static allocated_block_desc_tfind_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_tremove_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_tallocated = NULL
 Head of the linked list of live allocation descriptors.
 
static kalloc_management_pages_tkalloc_page_head = NULL
 Head of the linked list of management pages.
 

Detailed Description

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.

Author
Abhin Parekadan Jose
Date
2026-07-05

Macro Definition Documentation

◆ MAX_MANAGEMENT_BLOCKS_PER_PAGE

#define MAX_MANAGEMENT_BLOCKS_PER_PAGE
Value:
((PAGE_SIZE - sizeof(struct kalloc_management_pages_t *)) / \
#define PAGE_SIZE
Size of each memory page in bytes.
Definition page_table.h:25
Bookkeeping descriptor for a single kmalloc() allocation.
Definition kalloc.c:37
A page of descriptors used to track live kmalloc() allocations.
Definition kalloc.c:48

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.

Function Documentation

◆ add_to_allocated_list()

static void add_to_allocated_list ( allocated_block_desc_t block)
static

Appends a block descriptor to the tail of the allocated list.

Parameters
blockThe descriptor to add. A NULL block is ignored.

◆ alloc_management_page()

static kalloc_management_pages_t * alloc_management_page ( void  )
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.

Returns
Pointer to the new management page, or NULL on failure.

◆ find_free_management_block()

static allocated_block_desc_t * find_free_management_block ( void  )
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.

Returns
Pointer to a reserved block descriptor, or NULL on failure.

◆ free_management_page_if_empty()

static void free_management_page_if_empty ( allocated_block_desc_t removed)
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.

Parameters
removedA descriptor that was just marked free. A NULL removed is ignored.

◆ kfree()

void kfree ( void *  ptr)

Frees a block of memory previously returned by kmalloc().

Returns the block starting at ptr to the kernel heap so it can be reused. Passing NULL is a no-op.

Parameters
ptrPointer to the block to free, as returned by kmalloc().

◆ kmalloc()

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.

Parameters
sizeThe number of bytes to allocate.
Returns
void* Pointer to the allocated block, or NULL if the request could not be satisfied.

◆ remove_from_allocated_list()

static allocated_block_desc_t * remove_from_allocated_list ( void *  ptr)
static

Removes the descriptor for ptr from the allocated list.

Unlinks the descriptor whose allocation starts at ptr and marks it free.

Parameters
ptrVirtual address returned by a previous kmalloc().
Returns
The removed descriptor, or NULL if ptr was not found.