commit 614de12ebcece88d74419a8125331733fa5b9217
parent a850c1620765b32079e35648570a7aa2a77b515b
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date: Wed, 14 Oct 2026 18:55:52 -0300
Memory.
Diffstat:
4 files changed, 252 insertions(+), 32 deletions(-)
diff --git a/kernel/memory.c b/kernel/memory.c
@@ -5,28 +5,243 @@
// Alocador básico de memória heap.
-// somente para a implementação trivial
-#include <stdlib.h>
+#include "ppos.h"
-#define NOERROR (0)
+#include <stdint.h>
+#include <string.h>
+
+void lock_kernel(void);
+void unlock_kernel(void);
+
+#define HEAP_BYTES (64u * 1024u * 1024u)
+#define BLOCK_ALIGNMENT 16u
+
+typedef struct block_t {
+ unsigned int size;
+ unsigned int used;
+ unsigned int reserved[2];
+} block_t;
+
+typedef char block_header_must_be_aligned
+ [sizeof(block_t) % BLOCK_ALIGNMENT == 0 ? 1 : -1];
+
+static unsigned char heap[HEAP_BYTES] __attribute__((aligned(BLOCK_ALIGNMENT)));
+
+static unsigned char* heap_end(void)
+{
+ return heap + HEAP_BYTES;
+}
+
+static block_t* next_block(block_t* block)
+{
+ return (block_t*) ((unsigned char*) (block + 1) + block->size);
+}
+
+static int valid_block(block_t* block)
+{
+ unsigned char* header = (unsigned char*) block;
+ unsigned char* payload = (unsigned char*) (block + 1);
+
+ if (header < heap || payload > heap_end() || block->size == 0)
+ return 0;
+
+ if (block->size % BLOCK_ALIGNMENT != 0)
+ return 0;
+
+ return block->size <= (unsigned int) (heap_end() - payload);
+}
+
+static unsigned int aligned_size(unsigned int size)
+{
+ return (size + BLOCK_ALIGNMENT - 1) & ~(BLOCK_ALIGNMENT - 1);
+}
+
+void mem_init(void)
+{
+ memset(heap, 0, sizeof(heap));
+
+ block_t* block = (block_t*) heap;
+ block->size = HEAP_BYTES - sizeof(*block);
+ block->used = 0;
+}
+
+void mem_term(void)
+{
+}
-// implementação trivial, a ser substituída
void* mem_alloc(int size)
{
- return (malloc(size));
+ if (size <= 0 || (unsigned int) size > HEAP_BYTES - sizeof(block_t))
+ return NULL;
+
+ unsigned int wanted = aligned_size((unsigned int) size);
+ if (wanted < (unsigned int) size)
+ return NULL;
+
+ lock_kernel();
+
+ block_t* block = (block_t*) heap;
+ while ((unsigned char*) block < heap_end()) {
+ if (!valid_block(block)) {
+ printk("heap: corrupted block at %p\n", block);
+ unlock_kernel();
+ return NULL;
+ }
+
+ if (!block->used && block->size >= wanted)
+ break;
+
+ block = next_block(block);
+ }
+
+ if ((unsigned char*) block >= heap_end()) {
+ unlock_kernel();
+ return NULL;
+ }
+
+ unsigned int remaining = block->size - wanted;
+ if (remaining >= sizeof(block_t) + BLOCK_ALIGNMENT) {
+ block_t* new = (block_t*) ((unsigned char*) (block + 1) + wanted);
+ new->size = remaining - sizeof(*new);
+ new->used = 0;
+ block->size = wanted;
+ }
+
+ block->used = 1;
+ void* addr = block + 1;
+
+ unlock_kernel();
+ return addr;
}
-// implementação trivial, a ser substituída
int mem_free(void* addr)
{
- free(addr);
- return (NOERROR);
+ if (addr == NULL || (unsigned char*) addr < heap + sizeof(block_t)
+ || (unsigned char*) addr >= heap_end()
+ || (uintptr_t) addr % BLOCK_ALIGNMENT != 0)
+ return ERROR;
+
+ lock_kernel();
+
+ block_t* block = (block_t*) heap;
+ block_t* prev = NULL;
+ while ((unsigned char*) block < heap_end()) {
+ if (!valid_block(block)) {
+ printk("heap: corrupted block at %p\n", block);
+ unlock_kernel();
+ return ERROR;
+ }
+
+ if ((void*) (block + 1) == addr)
+ break;
+
+ prev = block;
+ block = next_block(block);
+ }
+
+ if ((unsigned char*) block >= heap_end() || !block->used) {
+ unlock_kernel();
+ return ERROR;
+ }
+
+ block->used = 0;
+
+ block_t* next = next_block(block);
+ if ((unsigned char*) next < heap_end() && valid_block(next) && !next->used)
+ block->size += sizeof(*next) + next->size;
+
+ if (prev != NULL && !prev->used)
+ prev->size += sizeof(*block) + block->size;
+
+ unlock_kernel();
+ return NOERROR;
}
-void mem_init()
+int mem_size(void)
{
+ return HEAP_BYTES;
}
-void mem_term()
+int mem_avail(void)
{
+ lock_kernel();
+
+ unsigned int size = 0;
+ block_t* block = (block_t*) heap;
+ while ((unsigned char*) block < heap_end()) {
+ if (!valid_block(block)) {
+ printk("heap: corrupted block at %p\n", block);
+ break;
+ }
+
+ if (!block->used)
+ size += block->size;
+
+ block = next_block(block);
+ }
+
+ unlock_kernel();
+ return (int) size;
+}
+
+void mem_report(void)
+{
+ lock_kernel();
+
+ unsigned int used = 0;
+ unsigned int free = 0;
+ int nblks = 0;
+ int nnblks = 0;
+ int total_blocks = 0;
+ int corrupted = 0;
+
+ block_t* block = (block_t*) heap;
+ while ((unsigned char*) block < heap_end()) {
+ if (!valid_block(block)) {
+ printk("heap: corrupted block at %p\n", block);
+ corrupted = 1;
+ break;
+ }
+
+ if (block->used) {
+ used += block->size;
+ nblks++;
+ } else {
+ free += block->size;
+ nnblks++;
+ }
+
+ total_blocks++;
+ block = next_block(block);
+ }
+
+ printk(
+ "heap: %d KB allocated (%d blocks), %d KB free (%d blocks)\n",
+ used / 1000,
+ nblks,
+ free / 1000,
+ nnblks
+ );
+
+ if (!corrupted) {
+ int i = 0;
+ block = (block_t*) heap;
+ while ((unsigned char*) block < heap_end()) {
+ unsigned int total_size = sizeof(*block) + block->size;
+ printk(
+ "heap: block %5d: %p - %p %s prev %5d next %5d size %9d\n",
+ i,
+ block,
+ (unsigned char*) block + total_size - 1,
+ block->used ? "aloc" : "FREE",
+ i > 0 ? i - 1 : total_blocks - 1,
+ i + 1 < total_blocks ? i + 1 : 0,
+ total_size
+ );
+ i++;
+ block = next_block(block);
+ }
+ }
+
+ unlock_kernel();
}
diff --git a/kernel/mqueue.c b/kernel/mqueue.c
@@ -5,11 +5,11 @@
// Gerência de filas de mensagens
#include "dispatcher.h"
+#include "memory.h"
#include "semaphore.h"
#include <map.h>
#include <queue.h>
-#include <stdlib.h>
#include <string.h>
void lock_kernel();
@@ -38,10 +38,12 @@ void mqueue_term()
int mqueue_create(int max_msgs, int msg_size)
{
lock_kernel();
- mqueue_t* mq = calloc(1, sizeof(*mq));
+ mqueue_t* mq = mem_alloc(sizeof(*mq));
unlock_kernel();
if (mq == NULL)
return -1;
+ memset(mq, 0, sizeof(*mq));
+ mq->msg_size = msg_size;
lock_kernel();
mq->buf = queue_create();
@@ -76,7 +78,7 @@ free_buf:
unlock_kernel();
free_mq:
lock_kernel();
- free(mq);
+ mem_free(mq);
unlock_kernel();
return -1;
}
@@ -93,7 +95,7 @@ int mqueue_destroy(int id)
map_del(mqueues, id);
for (void* buffer = queue_head(mq->buf); buffer != NULL;
buffer = queue_next(mq->buf)) {
- free(buffer);
+ mem_free(buffer);
}
queue_destroy(mq->buf);
unlock_kernel();
@@ -103,7 +105,7 @@ int mqueue_destroy(int id)
sem_destroy(mq->sem_produce);
lock_kernel();
- free(mq);
+ mem_free(mq);
unlock_kernel();
return 0;
@@ -133,8 +135,8 @@ int mqueue_send(int id, void* msg)
mq = (mqueue_t*) map_get(mqueues, id);
if (mq == NULL)
goto release;
- char* msg_on_queue = calloc(1, mq->msg_size);
- if (msg != NULL) {
+ char* msg_on_queue = mem_alloc(mq->msg_size);
+ if (msg_on_queue != NULL) {
memcpy(msg_on_queue, msg, mq->msg_size);
queue_add(mq->buf, msg_on_queue);
ret = 0;
@@ -177,7 +179,7 @@ int mqueue_recv(int id, void* msg)
queue_del(mq->buf, buffer);
if (buffer != NULL) {
memcpy(msg, buffer, mq->msg_size);
- free(buffer);
+ mem_free(buffer);
ret = 0;
}
diff --git a/kernel/semaphore.c b/kernel/semaphore.c
@@ -5,11 +5,12 @@
// Semáforos e spinlocks
#include "dispatcher.h"
+#include "memory.h"
#include "tcb.h"
#include <map.h>
#include <queue.h>
-#include <stdlib.h>
+#include <string.h>
extern struct task_t kernel_task;
extern struct task_t* current;
@@ -51,9 +52,10 @@ int sem_create(int value)
int ret = -1;
- struct semaphore_t* semaphore = calloc(1, sizeof(*semaphore));
+ struct semaphore_t* semaphore = mem_alloc(sizeof(*semaphore));
if (semaphore == NULL)
goto release;
+ memset(semaphore, 0, sizeof(*semaphore));
semaphore->awaiting = queue_create();
if (semaphore->awaiting == NULL)
@@ -67,7 +69,7 @@ int sem_create(int value)
queue_destroy(semaphore->awaiting);
free_semaphore:
- free(semaphore);
+ mem_free(semaphore);
release:
unlock_kernel();
return ret;
@@ -91,7 +93,7 @@ int sem_destroy(int id)
}
queue_destroy(semaphore->awaiting);
- free(semaphore);
+ mem_free(semaphore);
map_del(semaphores, id);
ret = 0;
diff --git a/kernel/task.c b/kernel/task.c
@@ -5,14 +5,13 @@
// Gerência básica de tarefas.
+#include "memory.h"
#include "task.h"
#include "time.h"
#include <assert.h>
#include <queue.h>
#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include <valgrind/valgrind.h>
@@ -51,17 +50,19 @@ struct task_t* task_create(char* name, void (*entry)(void*), void* arg)
{
lock_kernel();
- struct task_t* task = calloc(1, sizeof(*task));
+ struct task_t* task = mem_alloc(sizeof(*task));
if (task == NULL)
goto release;
+ memset(task, 0, sizeof(*task));
- // Calloc so we don't spill memory from other tasks. Not needed in a toy OS
- // without memory protection but it just *feels* right to.
- void* stack = calloc(PPOS_STACK_SIZE, 1);
+ void* stack = mem_alloc(PPOS_STACK_SIZE);
if (stack == NULL) {
- free(task);
+ mem_free(task);
goto release;
}
+ // Memset so we don't spill memory from other tasks. Not needed in a toy OS
+ // without memory protection but it just *feels* right to.
+ memset(stack, 0, PPOS_STACK_SIZE);
task->valgrind_id =
VALGRIND_STACK_REGISTER(stack, stack + (PPOS_STACK_SIZE - 1));
@@ -96,12 +97,12 @@ int task_destroy(struct task_t* task)
}
if (task->context.stack != NULL)
- free(task->context.stack);
+ mem_free(task->context.stack);
VALGRIND_STACK_DEREGISTER(task->valgrind_id);
- free(task->waiting_on_queue);
- free(task);
+ mem_free(task->waiting_on_queue);
+ mem_free(task);
release:
unlock_kernel();