undo malloc improvement (it actually made it worse)

This commit is contained in:
Lauchmelder 2022-01-21 14:22:50 +01:00
parent aedba6e52b
commit c49ff2998d
3 changed files with 44 additions and 49 deletions

View file

@ -1,10 +1,8 @@
#pragma once #pragma once
#include "stdint.h"
#define NULL ((void*)0) #define NULL ((void*)0)
void heap_init(); void heap_init();
void* malloc(size_t size); void* malloc();
void free(void* ptr); void free(void* ptr);

View file

@ -30,7 +30,7 @@ char* ultoa(unsigned long number, char* string, int base)
return string; return string;
} }
char* buffer = (char*)malloc(1024); char* buffer = (char*)malloc();
char* temp = buffer; char* temp = buffer;
int i = 0; int i = 0;

View file

@ -1,11 +1,11 @@
#include "memory.h" #include "memory.h"
#include "stdint.h"
#include "io.h" #include "io.h"
#include "convert.h" #include "convert.h"
struct FreeBlock struct FreeBlock
{ {
size_t size;
struct FreeBlock* next; struct FreeBlock* next;
}; };
@ -15,66 +15,63 @@ struct Block
void* data; void* data;
}; };
extern const void* __heap_base; #define BLOCK_SIZE 4096
#define MAGIC_NUMBER 0xdeadbeef
extern char __heap_base;
static struct FreeBlock* head = NULL; static struct FreeBlock* head = NULL;
void heap_init() void heap_init()
{ {
head = (struct FreeBlock*)__heap_base; head = (struct FreeBlock*)&__heap_base;
head->next = head + sizeof(struct FreeBlock); head->next = NULL;
head->size = 0;
head->next->next = NULL;
head->next->size = -1;
} }
struct FreeBlock* find_free_block(struct FreeBlock** previous, size_t min_size) void* malloc()
{ {
struct FreeBlock* block = head; void* block = (void*)head;
while(block && min_size < block->size)
if(head->next == NULL)
{ {
*previous = block; head += BLOCK_SIZE;
block = block->next; head->next = NULL;
}
return block;
}
void* malloc(size_t size)
{
size_t physicalSize = size + sizeof(size_t);
struct Block* allocation = NULL;
struct FreeBlock* previous = head;
struct FreeBlock* block = find_free_block(&previous, physicalSize);
allocation = (struct Block*)block;
allocation->size = size;
if(block->size == physicalSize)
{
previous->next = block->next;
} }
else else
{ {
struct FreeBlock* shrunkBlock = block + physicalSize; head = head->next;
shrunkBlock->next = block->next;
shrunkBlock->size = shrunkBlock->next - shrunkBlock;
previous->next = shrunkBlock;
} }
return &(allocation->data); *(uint32_t*)block = MAGIC_NUMBER;
return block + sizeof(uint32_t);
} }
void free(void* ptr) void free(void* ptr)
{ {
struct FreeBlock* block = (struct FreeBlock*)((size_t*)ptr - 1); void* blockStart = ptr - sizeof(uint32_t);
if(*(uint32_t*)blockStart != MAGIC_NUMBER)
struct FreeBlock* previous = head; return;
while(previous && (void*)previous->next < ptr)
previous = previous->next;
previous->next = block; if((struct FreeBlock*)blockStart < head)
block->next = previous; {
struct FreeBlock* nextBlock = head->next;
head = blockStart;
head->next = nextBlock;
return;
}
struct FreeBlock* block = head;
while(block)
{
if(block < (struct FreeBlock*)blockStart && (struct FreeBlock*)blockStart < block->next)
{
struct FreeBlock* newBlock = (struct FreeBlock*)blockStart;
newBlock->next = block->next;
block->next = newBlock;
return;
}
block = block->next;
}
} }