mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-07-02 10:59:15 +00:00
fix(userspace/libc): interpreter didn't worked at all
This commit is contained in:
@ -25,6 +25,11 @@
|
||||
#include "elf.h"
|
||||
#include "misc.h"
|
||||
|
||||
#undef PAGE_SIZE
|
||||
#define PAGE_SIZE 0x1000
|
||||
#undef ALIGN_UP
|
||||
#define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
|
||||
|
||||
typedef struct MemoryBlock
|
||||
{
|
||||
struct MemoryBlock *next;
|
||||
@ -32,14 +37,15 @@ typedef struct MemoryBlock
|
||||
size_t slot_size;
|
||||
size_t slots_per_block;
|
||||
uint8_t *bitmap;
|
||||
size_t bitmap_size;
|
||||
size_t total_size;
|
||||
} MemoryBlock;
|
||||
|
||||
MemoryBlock *memory_pool = NULL;
|
||||
#define PAGE_SIZE 0x1000
|
||||
static MemoryBlock *memory_pool = NULL;
|
||||
|
||||
void *request_page(size_t size)
|
||||
{
|
||||
size_t aligned_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
||||
size_t aligned_size = ALIGN_UP(size, PAGE_SIZE);
|
||||
void *addr = (void *)sysdep(MemoryMap)(NULL, aligned_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||
if ((intptr_t)addr < 0)
|
||||
return NULL;
|
||||
@ -48,42 +54,60 @@ void *request_page(size_t size)
|
||||
|
||||
void free_page(void *addr, size_t size)
|
||||
{
|
||||
size_t aligned_size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
||||
size_t aligned_size = ALIGN_UP(size, PAGE_SIZE);
|
||||
sysdep(MemoryUnmap)(addr, aligned_size);
|
||||
}
|
||||
|
||||
MemoryBlock *allocate_block(size_t slot_size)
|
||||
{
|
||||
size_t block_size = PAGE_SIZE;
|
||||
size_t slots_per_block = block_size / slot_size;
|
||||
size_t bitmap_size = (slots_per_block + 7) / 8;
|
||||
if (slot_size == 0 || slot_size > PAGE_SIZE / 2)
|
||||
return NULL;
|
||||
|
||||
MemoryBlock *block = request_page(block_size);
|
||||
size_t estimated_slots = (PAGE_SIZE - sizeof(MemoryBlock)) / (slot_size + 1.0 / 8.0);
|
||||
if (estimated_slots == 0)
|
||||
estimated_slots = 1;
|
||||
|
||||
size_t bitmap_size = ALIGN_UP((estimated_slots + 7) / 8, 8);
|
||||
size_t total_slots_size = estimated_slots * slot_size;
|
||||
size_t total_size = sizeof(MemoryBlock) + bitmap_size + total_slots_size;
|
||||
total_size = ALIGN_UP(total_size, PAGE_SIZE);
|
||||
|
||||
MemoryBlock *block = (MemoryBlock *)request_page(total_size);
|
||||
if (!block)
|
||||
return NULL;
|
||||
|
||||
block->slots = (void *)((uintptr_t)block + sizeof(MemoryBlock) + bitmap_size);
|
||||
uintptr_t base = (uintptr_t)block + sizeof(MemoryBlock);
|
||||
block->bitmap = (uint8_t *)base;
|
||||
block->bitmap_size = bitmap_size;
|
||||
|
||||
block->slots = (void *)ALIGN_UP(base + bitmap_size, 16);
|
||||
block->slot_size = slot_size;
|
||||
block->slots_per_block = slots_per_block;
|
||||
block->bitmap = (uint8_t *)((uintptr_t)block + sizeof(MemoryBlock));
|
||||
memset(block->bitmap, 0, bitmap_size);
|
||||
block->slots_per_block = (total_size - ((uintptr_t)block->slots - (uintptr_t)block)) / slot_size;
|
||||
block->total_size = total_size;
|
||||
block->next = NULL;
|
||||
|
||||
memset(block->bitmap, 0, bitmap_size);
|
||||
return block;
|
||||
}
|
||||
|
||||
void *mini_malloc(size_t size)
|
||||
{
|
||||
if (size == 0)
|
||||
return NULL;
|
||||
|
||||
MemoryBlock *block = memory_pool;
|
||||
while (block)
|
||||
{
|
||||
for (size_t i = 0; i < block->slots_per_block; i++)
|
||||
if (block->slot_size == size)
|
||||
{
|
||||
size_t byte = i / 8, bit = i % 8;
|
||||
if (!(block->bitmap[byte] & (1 << bit)))
|
||||
for (size_t i = 0; i < block->slots_per_block; i++)
|
||||
{
|
||||
block->bitmap[byte] |= (1 << bit);
|
||||
return (void *)((uintptr_t)block->slots + i * size);
|
||||
size_t byte = i / 8, bit = i % 8;
|
||||
if (!(block->bitmap[byte] & (1 << bit)))
|
||||
{
|
||||
block->bitmap[byte] |= (1 << bit);
|
||||
return (void *)((uintptr_t)block->slots + i * block->slot_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
block = block->next;
|
||||
@ -105,10 +129,12 @@ void mini_free(void *ptr)
|
||||
MemoryBlock *block = memory_pool;
|
||||
while (block)
|
||||
{
|
||||
if ((uintptr_t)ptr >= (uintptr_t)block->slots &&
|
||||
(uintptr_t)ptr < (uintptr_t)block->slots + block->slots_per_block * block->slot_size)
|
||||
uintptr_t start = (uintptr_t)block->slots;
|
||||
uintptr_t end = start + block->slots_per_block * block->slot_size;
|
||||
|
||||
if ((uintptr_t)ptr >= start && (uintptr_t)ptr < end)
|
||||
{
|
||||
size_t index = ((uintptr_t)ptr - (uintptr_t)block->slots) / block->slot_size;
|
||||
size_t index = ((uintptr_t)ptr - start) / block->slot_size;
|
||||
size_t byte = index / 8, bit = index % 8;
|
||||
block->bitmap[byte] &= ~(1 << bit);
|
||||
return;
|
||||
|
Reference in New Issue
Block a user