Permalink
Browse files

Files for part 16, https://www.youtube.com/watch?v=BeSpPd3C3J8

  • Loading branch information...
AlgorithMan-de committed Jul 9, 2016
1 parent 49a2efa commit e095bd02562a78dd30ebc4576e30fe87ea126f40
View
@@ -16,6 +16,7 @@ namespace myos
typedef unsigned long long int uint64_t;
typedef const char* string;
typedef uint32_t size_t;
}
}
@@ -7,6 +7,8 @@
#include <common/types.h>
#include <hardwarecommunication/interrupts.h>
#include <memorymanagement.h>
namespace myos
{
namespace hardwarecommunication
View
@@ -0,0 +1,49 @@
#ifndef __MYOS__MEMORYMANAGEMENT_H
#define __MYOS__MEMORYMANAGEMENT_H
#include <common/types.h>
namespace myos
{
struct MemoryChunk
{
MemoryChunk *next;
MemoryChunk *prev;
bool allocated;
common::size_t size;
};
class MemoryManager
{
protected:
MemoryChunk* first;
public:
static MemoryManager *activeMemoryManager;
MemoryManager(common::size_t first, common::size_t size);
~MemoryManager();
void* malloc(common::size_t size);
void free(void* ptr);
};
}
void* operator new(unsigned size);
void* operator new[](unsigned size);
// placement new
void* operator new(unsigned size, void* ptr);
void* operator new[](unsigned size, void* ptr);
void operator delete(void* ptr);
void operator delete[](void* ptr);
#endif
View
@@ -8,6 +8,7 @@ LDPARAMS = -melf_i386
objects = obj/loader.o \
obj/gdt.o \
obj/memorymanagement.o \
obj/drivers/driver.o \
obj/hardwarecommunication/port.o \
obj/hardwarecommunication/interruptstubs.o \
@@ -157,12 +157,18 @@ BaseAddressRegister PeripheralComponentInterconnectController::GetBaseAddressReg
Driver* PeripheralComponentInterconnectController::GetDriver(PeripheralComponentInterconnectDeviceDescriptor dev, InterruptManager* interrupts)
{
Driver *driver = 0;
switch(dev.vendor_id)
{
case 0x1022: // AMD
switch(dev.device_id)
{
case 0x2000: // am79c973
/*
driver = (amd_am79c973*)MemoryManager::activeMemoryManager->malloc(sizeof(amd_am79c973));
if(driver != 0)
new (driver) amd_am79c973(...);
*/
printf("AMD am79c973 ");
break;
}
@@ -186,7 +192,7 @@ Driver* PeripheralComponentInterconnectController::GetDriver(PeripheralComponent
}
return 0;
return driver;
}
View
@@ -1,6 +1,7 @@
#include <common/types.h>
#include <gdt.h>
#include <memorymanagement.h>
#include <hardwarecommunication/interrupts.h>
#include <hardwarecommunication/pci.h>
#include <drivers/driver.h>
@@ -156,11 +157,32 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
GlobalDescriptorTable gdt;
uint32_t* memupper = (uint32_t*)(((size_t)multiboot_structure) + 8);
size_t heap = 10*1024*1024;
MemoryManager memoryManager(heap, (*memupper)*1024 - heap - 10*1024);
printf("heap: 0x");
printfHex((heap >> 24) & 0xFF);
printfHex((heap >> 16) & 0xFF);
printfHex((heap >> 8 ) & 0xFF);
printfHex((heap ) & 0xFF);
void* allocated = memoryManager.malloc(1024);
printf("\nallocated: 0x");
printfHex(((size_t)allocated >> 24) & 0xFF);
printfHex(((size_t)allocated >> 16) & 0xFF);
printfHex(((size_t)allocated >> 8 ) & 0xFF);
printfHex(((size_t)allocated ) & 0xFF);
printf("\n");
TaskManager taskManager;
/*
Task task1(&gdt, taskA);
Task task2(&gdt, taskB);
taskManager.AddTask(&task1);
taskManager.AddTask(&task2);
*/
InterruptManager interrupts(0x20, &gdt, &taskManager);
View
@@ -0,0 +1,128 @@
#include <memorymanagement.h>
using namespace myos;
using namespace myos::common;
MemoryManager* MemoryManager::activeMemoryManager = 0;
MemoryManager::MemoryManager(size_t start, size_t size)
{
activeMemoryManager = this;
if(size < sizeof(MemoryChunk))
{
first = 0;
}
else
{
first = (MemoryChunk*)start;
first -> allocated = false;
first -> prev = 0;
first -> next = 0;
first -> size = size - sizeof(MemoryChunk);
}
}
MemoryManager::~MemoryManager()
{
if(activeMemoryManager == this)
activeMemoryManager = 0;
}
void* MemoryManager::malloc(size_t size)
{
MemoryChunk *result = 0;
for(MemoryChunk* chunk = first; chunk != 0 && result == 0; chunk = chunk->next)
if(chunk->size > size && !chunk->allocated)
result = chunk;
if(result == 0)
return 0;
if(result->size >= size + sizeof(MemoryChunk) + 1)
{
MemoryChunk* temp = (MemoryChunk*)((size_t)result + sizeof(MemoryChunk) + size);
temp->allocated = false;
temp->size = result->size - size - sizeof(MemoryChunk);
temp->prev = result;
temp->next = result->next;
if(temp->next != 0)
temp->next->prev = temp;
result->size = size;
result->next = temp;
}
result->allocated = true;
return (void*)(((size_t)result) + sizeof(MemoryChunk));
}
void MemoryManager::free(void* ptr)
{
MemoryChunk* chunk = (MemoryChunk*)((size_t)ptr - sizeof(MemoryChunk));
chunk -> allocated = false;
if(chunk->prev != 0 && !chunk->prev->allocated)
{
chunk->prev->next = chunk->next;
chunk->prev->size += chunk->size + sizeof(MemoryChunk);
if(chunk->next != 0)
chunk->next->prev = chunk->prev;
chunk = chunk->prev;
}
if(chunk->next != 0 && !chunk->next->allocated)
{
chunk->size += chunk->next->size + sizeof(MemoryChunk);
chunk->next = chunk->next->next;
if(chunk->next != 0)
chunk->next->prev = chunk;
}
}
void* operator new(unsigned size)
{
if(myos::MemoryManager::activeMemoryManager == 0)
return 0;
return myos::MemoryManager::activeMemoryManager->malloc(size);
}
void* operator new[](unsigned size)
{
if(myos::MemoryManager::activeMemoryManager == 0)
return 0;
return myos::MemoryManager::activeMemoryManager->malloc(size);
}
void* operator new(unsigned size, void* ptr)
{
return ptr;
}
void* operator new[](unsigned size, void* ptr)
{
return ptr;
}
void operator delete(void* ptr)
{
if(myos::MemoryManager::activeMemoryManager != 0)
myos::MemoryManager::activeMemoryManager->free(ptr);
}
void operator delete[](void* ptr)
{
if(myos::MemoryManager::activeMemoryManager != 0)
myos::MemoryManager::activeMemoryManager->free(ptr);
}

0 comments on commit e095bd0

Please sign in to comment.