Added a way to manually test the Malloc function from the shell's command line.
This commit is contained in:
+31
-2
@@ -1,5 +1,6 @@
|
||||
#include "../include/memorymanager.h"
|
||||
#include "../include/system.h"
|
||||
#include "../include/scrn.h"
|
||||
|
||||
static struct MemoryChunk* First;
|
||||
|
||||
@@ -10,6 +11,7 @@ void MemoryManager(size_t start, size_t size)
|
||||
if(size < sizeof(struct MemoryChunk))
|
||||
{
|
||||
First = 0;
|
||||
puts("First memory block is null\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -19,6 +21,9 @@ void MemoryManager(size_t start, size_t size)
|
||||
First->Prev = 0;
|
||||
First->Next = 0;
|
||||
First->Size = size - sizeof(struct MemoryChunk);
|
||||
puts("First block size ");
|
||||
puts(int_to_string(First->Size, 10));
|
||||
puts(" bytes.\n");
|
||||
}
|
||||
|
||||
void* Malloc(size_t size)
|
||||
@@ -28,7 +33,9 @@ void* Malloc(size_t size)
|
||||
for(struct MemoryChunk* chunk = First; chunk != 0 && freeChunk == 0; chunk = chunk->Next)
|
||||
{
|
||||
if(chunk->Size > size && !chunk->Allocated)
|
||||
{
|
||||
freeChunk = chunk;
|
||||
}
|
||||
}
|
||||
|
||||
if(freeChunk == 0)
|
||||
@@ -51,7 +58,9 @@ void* Malloc(size_t size)
|
||||
}
|
||||
|
||||
freeChunk->Allocated = true;
|
||||
|
||||
puts("Address of new chunk: 0x");
|
||||
puts(int_to_string((size_t)freeChunk + sizeof(struct MemoryChunk), 16));
|
||||
puts("\n");
|
||||
return (void*)(((size_t) freeChunk) + sizeof(struct MemoryChunk));
|
||||
}
|
||||
|
||||
@@ -67,4 +76,24 @@ void Free(void* ptr)
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ListBlocks()
|
||||
{
|
||||
struct MemoryChunk *freeChunk = 0;
|
||||
int count = 0;
|
||||
|
||||
for(struct MemoryChunk* chunk = First; chunk != 0 && freeChunk == 0; chunk = chunk->Next)
|
||||
{
|
||||
puts("\nChunk ");
|
||||
puts(int_to_string(count, 10));
|
||||
puts(" is ");
|
||||
puts(int_to_string(chunk->Size, 10));
|
||||
puts(" bytes. Allocated? ");
|
||||
if(chunk->Allocated)
|
||||
puts("True");
|
||||
else
|
||||
puts("False");
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user