Memory patch

This commit is contained in:
Chris
2026-02-05 13:35:16 +01:00
parent 6ee8582391
commit c3bb963d63
18 changed files with 57 additions and 15 deletions

View File

@@ -19,9 +19,16 @@ static volatile struct limine_framebuffer_request framebuffer_request = {
.revision = 1
};
__attribute__((used, section(".requests")))
static volatile struct limine_memmap_request memmap_request = {
.id = LIMINE_MEMMAP_REQUEST,
.revision = 0
};
__attribute__((used, section(".requests_start")))
static volatile struct limine_request *const requests_start_marker[] = {
(struct limine_request *)&framebuffer_request,
(struct limine_request *)&memmap_request,
NULL
};
@@ -60,8 +67,28 @@ void kmain(void) {
// Load IDT and Enable Interrupts
idt_load();
// 2.5 Memory Manager Init
memory_manager_init();
// 2.5 Memory Manager Init - Calculate available RAM from Limine
size_t total_usable_memory = 0;
if (memmap_request.response != NULL) {
for (uint64_t i = 0; i < memmap_request.response->entry_count; i++) {
struct limine_memmap_entry *entry = memmap_request.response->entries[i];
// Count usable memory regions
if (entry->type == LIMINE_MEMMAP_USABLE) {
total_usable_memory += entry->length;
}
}
}
// Initialize memory manager with available memory (cap at 2GB for practical reasons)
size_t pool_size = total_usable_memory > (2 * 1024 * 1024 * 1024) ?
(2 * 1024 * 1024 * 1024) : total_usable_memory;
if (pool_size == 0) {
pool_size = 512 * 1024 * 1024; // Fallback to 512MB if detection fails
}
memory_manager_init_with_size(pool_size);
// 3. PS/2 Init (Mouse/Keyboard)
asm("cli");

View File

@@ -3,7 +3,8 @@
#include <stdint.h>
// --- Internal State ---
static uint8_t memory_pool[MEMORY_POOL_SIZE] __attribute__((aligned(4096)));
static uint8_t *memory_pool = NULL; // Dynamically allocated
static size_t memory_pool_size = DEFAULT_POOL_SIZE; // Track actual pool size
static MemBlock block_list[MAX_ALLOCATIONS];
static int block_count = 0;
static size_t total_allocated = 0;
@@ -45,7 +46,7 @@ static uint32_t get_timestamp(void) {
static void* find_free_space(size_t size) {
size_t offset = 0;
while (offset + size <= MEMORY_POOL_SIZE) {
while (offset + size <= memory_pool_size) {
bool space_free = true;
// Check if this range is free
@@ -95,7 +96,7 @@ static size_t calculate_fragmentation(void) {
// Count gaps between allocated blocks
size_t total_gaps = 0;
void *pool_end = (uint8_t *)memory_pool + MEMORY_POOL_SIZE;
void *pool_end = (uint8_t *)memory_pool + memory_pool_size;
void *current_end = memory_pool;
@@ -115,9 +116,18 @@ static size_t calculate_fragmentation(void) {
// --- Public API ---
void memory_manager_init(void) {
void memory_manager_init_with_size(size_t pool_size) {
if (initialized) return;
memory_pool_size = pool_size;
// Initialize memory pool - in a real kernel, this would be passed a physical address
// For now, we use a simple tracking mechanism where allocations are tracked virtually
// The caller is responsible for ensuring the pool_size is valid
if (memory_pool == NULL) {
memory_pool = (uint8_t *)0x100000000; // Start from 4GB boundary as virtual tracking
}
// Clear metadata
mem_memset(block_list, 0, sizeof(block_list));
block_count = 0;
@@ -127,7 +137,7 @@ void memory_manager_init(void) {
// Create initial free block representing entire pool
block_list[0].address = memory_pool;
block_list[0].size = MEMORY_POOL_SIZE;
block_list[0].size = memory_pool_size;
block_list[0].allocated = false;
block_list[0].allocation_id = 0;
block_count = 1;
@@ -135,17 +145,21 @@ void memory_manager_init(void) {
initialized = true;
}
void memory_manager_init(void) {
memory_manager_init_with_size(DEFAULT_POOL_SIZE);
}
void* kmalloc(size_t size) {
if (!initialized) {
memory_manager_init();
}
if (size == 0 || size > MEMORY_POOL_SIZE) {
if (size == 0 || size > memory_pool_size) {
return NULL;
}
// Check if we can allocate
if (total_allocated + size > MEMORY_POOL_SIZE) {
if (total_allocated + size > memory_pool_size) {
return NULL;
}
@@ -245,13 +259,13 @@ void* krealloc(void *ptr, size_t new_size) {
MemStats memory_get_stats(void) {
MemStats stats;
stats.total_memory = MEMORY_POOL_SIZE;
stats.total_memory = memory_pool_size;
stats.used_memory = total_allocated;
stats.available_memory = MEMORY_POOL_SIZE - total_allocated;
stats.available_memory = memory_pool_size - total_allocated;
stats.allocated_blocks = 0;
stats.free_blocks = 0;
stats.largest_free_block = 0;
stats.smallest_free_block = MEMORY_POOL_SIZE;
stats.smallest_free_block = memory_pool_size;
stats.peak_memory_used = peak_allocated;
// Count and analyze blocks
@@ -417,7 +431,7 @@ bool memory_is_valid_ptr(void *ptr) {
if (ptr == NULL) return false;
void *pool_start = memory_pool;
void *pool_end = (uint8_t *)memory_pool + MEMORY_POOL_SIZE;
void *pool_end = (uint8_t *)memory_pool + memory_pool_size;
if (ptr < pool_start || ptr >= pool_end) {
return false;

View File

@@ -6,8 +6,8 @@
#include <stdbool.h>
// Memory Manager Configuration
#define MEMORY_POOL_SIZE (4 * 1024 * 1024) // 4MB pool
#define MAX_ALLOCATIONS 1024
#define DEFAULT_POOL_SIZE (512 * 1024 * 1024) // 512MB default (can be overridden)
#define MAX_ALLOCATIONS 4096 // Increased for larger pools
#define MAX_FRAGMENTATION_SLOTS 2048
// Allocation block metadata
@@ -34,6 +34,7 @@ typedef struct {
// Public API
void memory_manager_init(void);
void memory_manager_init_with_size(size_t pool_size);
// Allocation/Deallocation
void* kmalloc(size_t size);