1

fixed wrong bump allocator init

This commit is contained in:
churl
2022-05-07 14:30:40 +02:00
parent f6789b3950
commit 7f3c70f87a
10 changed files with 61 additions and 73 deletions

52
c_os/kernel/Allocator.cc Normal file → Executable file
View File

@ -17,13 +17,13 @@
* setup.asm * * setup.asm *
* 0x1000: System-Code (max. 512K) geladen * * 0x1000: System-Code (max. 512K) geladen *
* System-Code * * System-Code *
* 0x100000: System-Code, kopiert nach Umschalten in * * 0x100000: System-Code, kopiert nach Umschalten in *
* den Protected Mode kopiert (GRUB kann nur * * den Protected Mode kopiert (GRUB kann nur *
* an Adressen >1M laden) * * an Adressen >1M laden) *
* Globale Variablen: Direkt nach dem Code liegen die globalen * * Globale Variablen: Direkt nach dem Code liegen die globalen *
* Variablen. * * Variablen. *
* Heap: * * Heap: *
* 0x300000: Start-Adresse der Heap-Verwaltung * * 0x300000: Start-Adresse der Heap-Verwaltung *
* 0x400000: Letzte Adresse des Heaps * * 0x400000: Letzte Adresse des Heaps *
* * * *
* Achtung: Benötigt einen PC mit mindestens 8 MB RAM! * * Achtung: Benötigt einen PC mit mindestens 8 MB RAM! *
@ -31,53 +31,51 @@
* Autor: Michael Schoettner, HHU, 1.3.2022 * * Autor: Michael Schoettner, HHU, 1.3.2022 *
*****************************************************************************/ *****************************************************************************/
#include <stddef.h>
#include "kernel/Globals.h"
#include "kernel/Allocator.h" #include "kernel/Allocator.h"
#include "kernel/Globals.h"
#include <stddef.h>
#define MEM_SIZE_DEF 8 * 1024 * 1024 // Groesse des Speichers = 8 MB
#define MEM_SIZE_DEF 8*1024*102 // Groesse des Speichers = 8 MB #define HEAP_START 0x300000 // Startadresse des Heaps
#define HEAP_SIZE 1024 * 1024 // Default-Groesse des Heaps, falls \
#define HEAP_START 0x300000 // Startadresse des Heaps // nicht über das BIOS ermittelbar
#define HEAP_SIZE 1024*1024 // Default-Groesse des Heaps, falls
// nicht über das BIOS ermittelbar
/***************************************************************************** /*****************************************************************************
* Konstruktor: Allocator::Allocator * * Konstruktor: Allocator::Allocator *
*****************************************************************************/ *****************************************************************************/
Allocator::Allocator() { Allocator::Allocator() {
// Groesse des Hauptspeichers (kann über das BIOS abgefragt werden, // Groesse des Hauptspeichers (kann über das BIOS abgefragt werden,
// aber sehr umstaendlich, daher hier fest eingetragen // aber sehr umstaendlich, daher hier fest eingetragen
total_mem = MEM_SIZE_DEF; total_mem = MEM_SIZE_DEF;
heap_start = HEAP_START; heap_start = HEAP_START;
heap_end = HEAP_START + HEAP_SIZE; heap_end = HEAP_START + HEAP_SIZE;
heap_size = HEAP_SIZE; heap_size = HEAP_SIZE;
initialized = 1; initialized = 1;
} }
/***************************************************************************** /*****************************************************************************
* Nachfolgend sind die Operatoren von C++, die wir hier ueberschreiben * * Nachfolgend sind die Operatoren von C++, die wir hier ueberschreiben *
* und entsprechend 'mm_alloc' und 'mm_free' aufrufen. * * und entsprechend 'mm_alloc' und 'mm_free' aufrufen. *
*****************************************************************************/ *****************************************************************************/
void* operator new ( size_t size ) { void* operator new(size_t size) {
return allocator.alloc(size); return allocator.alloc(size);
} }
void* operator new[]( size_t count ) { void* operator new[](size_t count) {
return allocator.alloc(count); return allocator.alloc(count);
} }
void operator delete ( void* ptr ) { void operator delete(void* ptr) {
allocator.free(ptr);
}
void operator delete[] ( void* ptr ) {
allocator.free(ptr); allocator.free(ptr);
} }
void operator delete(void*ptr, unsigned int sz) { void operator delete[](void* ptr) {
allocator.free(ptr);
}
void operator delete(void* ptr, unsigned int sz) {
allocator.free(ptr); allocator.free(ptr);
} }

0
c_os/kernel/Allocator.h Normal file → Executable file
View File

21
c_os/kernel/allocator/BumpAllocator.cc Normal file → Executable file
View File

@ -22,13 +22,12 @@ void BumpAllocator::init() {
/* Hier muess Code eingefuegt werden */ /* Hier muess Code eingefuegt werden */
this->heap_start = 3 * 1024 * 1024; // 3 MB
this->heap_size = 1 * 1024 * 1024; // 1 MB
this->heap_end = this->heap_start + this->heap_size; // 4 MB
this->allocations = 0; this->allocations = 0;
this->next = (unsigned char*)heap_start; this->next = (unsigned char*)heap_start;
this->initialized = true; kout << "Initialized Bump Allocator" << endl
<< "Heap Start: " << hex << this->heap_start << ", Heap End: " << this->heap_end << endl;
kout << endl;
} }
/***************************************************************************** /*****************************************************************************
@ -39,9 +38,10 @@ void BumpAllocator::init() {
void BumpAllocator::dump_free_memory() { void BumpAllocator::dump_free_memory() {
/* Hier muess Code eingefuegt werden */ /* Hier muess Code eingefuegt werden */
kout << "Freier Speicher:" << endl kout << "Freier Speicher:" << endl
<< " Heap Start: " << this->heap_start << ", Heap End: " << this->heap_end << endl << "Next: " << hex << this->next << ", Allocations: " << dec << this->allocations << endl;
<< " Next: " << this->next << ", Allocations: " << this->allocations << endl; kout << endl;
} }
/***************************************************************************** /*****************************************************************************
@ -53,13 +53,11 @@ void* BumpAllocator::alloc(unsigned int req_size) {
/* Hier muess Code eingefuegt werden */ /* Hier muess Code eingefuegt werden */
if (!this->initialized) { kout << "Requested " << dec << req_size << " Bytes" << endl;
kout << "Allocator not initialized :(" << endl;
return NULL;
}
if (req_size > this->heap_end - (unsigned int)this->next) { if (req_size > this->heap_end - (unsigned int)this->next) {
kout << "More memory requested than available :(" << endl; kout << "More memory requested than available :(" << endl;
kout << endl;
return NULL; return NULL;
} }
@ -67,6 +65,9 @@ void* BumpAllocator::alloc(unsigned int req_size) {
this->next = this->next + req_size; this->next = this->next + req_size;
this->allocations = this->allocations + 1; this->allocations = this->allocations + 1;
kout << "Allocated " << dec << req_size << " Bytes." << endl;
kout << endl;
return allocated; return allocated;
} }

2
c_os/kernel/allocator/BumpAllocator.h Normal file → Executable file
View File

@ -23,7 +23,7 @@ private:
BumpAllocator(Allocator& copy); // Verhindere Kopieren BumpAllocator(Allocator& copy); // Verhindere Kopieren
public: public:
BumpAllocator() {} BumpAllocator() {}; // Allocator() called implicitely in C++
void init(); void init();
void dump_free_memory(); void dump_free_memory();

27
c_os/kernel/allocator/LinkedListAllocator.cc Normal file → Executable file
View File

@ -9,12 +9,11 @@
* Autor: Michael Schoettner, HHU, 13.6.2020 * * Autor: Michael Schoettner, HHU, 13.6.2020 *
*****************************************************************************/ *****************************************************************************/
#include <stddef.h>
#include "kernel/Globals.h"
#include "kernel/allocator/LinkedListAllocator.h" #include "kernel/allocator/LinkedListAllocator.h"
#include "kernel/Globals.h"
#include <stddef.h>
#define HEAP_MIN_FREE_BLOCK_SIZE 64 // min. Groesse eines freien Blocks #define HEAP_MIN_FREE_BLOCK_SIZE 64 // min. Groesse eines freien Blocks
/***************************************************************************** /*****************************************************************************
* Methode: LinkedListAllocator::init * * Methode: LinkedListAllocator::init *
@ -28,11 +27,9 @@
*****************************************************************************/ *****************************************************************************/
void LinkedListAllocator::init() { void LinkedListAllocator::init() {
/* Hier muess Code eingefuegt werden */ /* Hier muess Code eingefuegt werden */
} }
/***************************************************************************** /*****************************************************************************
* Methode: LinkedListAllocator::dump_free_memory * * Methode: LinkedListAllocator::dump_free_memory *
*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*
@ -40,31 +37,25 @@ void LinkedListAllocator::init() {
*****************************************************************************/ *****************************************************************************/
void LinkedListAllocator::dump_free_memory() { void LinkedListAllocator::dump_free_memory() {
/* Hier muess Code eingefuegt werden */ /* Hier muess Code eingefuegt werden */
} }
/***************************************************************************** /*****************************************************************************
* Methode: LinkedListAllocator::alloc * * Methode: LinkedListAllocator::alloc *
*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*
* Beschreibung: Einen neuen Speicherblock allozieren. * * Beschreibung: Einen neuen Speicherblock allozieren. *
*****************************************************************************/ *****************************************************************************/
void * LinkedListAllocator::alloc(unsigned int req_size) { void* LinkedListAllocator::alloc(unsigned int req_size) {
/* Hier muess Code eingefuegt werden */
/* Hier muess Code eingefuegt werden */
} }
/***************************************************************************** /*****************************************************************************
* Methode: LinkedListAllocator::free * * Methode: LinkedListAllocator::free *
*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*
* Beschreibung: Einen Speicherblock freigeben. * * Beschreibung: Einen Speicherblock freigeben. *
*****************************************************************************/ *****************************************************************************/
void LinkedListAllocator::free(void *ptr) { void LinkedListAllocator::free(void* ptr) {
/* Hier muess Code eingefuegt werden */
/* Hier muess Code eingefuegt werden */
} }

25
c_os/kernel/allocator/LinkedListAllocator.h Normal file → Executable file
View File

@ -14,30 +14,27 @@
#include "kernel/Allocator.h" #include "kernel/Allocator.h"
// Format eines freien Blocks // Format eines freien Blocks
struct free_block { struct free_block {
unsigned int size; unsigned int size;
struct free_block *next; struct free_block* next;
}; };
class LinkedListAllocator : Allocator { class LinkedListAllocator : Allocator {
private:
// freie Bloecke werden verkettet
struct free_block *free_start;
LinkedListAllocator(Allocator &copy); // Verhindere Kopieren private:
// freie Bloecke werden verkettet
struct free_block* free_start;
LinkedListAllocator(Allocator& copy); // Verhindere Kopieren
public: public:
LinkedListAllocator () { } LinkedListAllocator() {}
void init ();
void dump_free_memory ();
void* alloc (unsigned int req_size);
void free (void *ptr);
void init();
void dump_free_memory();
void* alloc(unsigned int req_size);
void free(void* ptr);
}; };
#endif #endif

View File

@ -15,13 +15,11 @@
#include "user/HeapDemo.h" #include "user/HeapDemo.h"
int main() { int main() {
kout.clear();
// Speicherverwaltung initialisieren // Speicherverwaltung initialisieren
allocator.init(); allocator.init();
// Bildschirm loeschen.
kout.clear();
// text_demo(); // text_demo();
// sound_demo(); // sound_demo();
// keyboard_demo(); // keyboard_demo();

3
c_os/user/HeapDemo.cc Normal file → Executable file
View File

@ -34,4 +34,7 @@ void heap_demo() {
MyObj* b = new MyObj(10); MyObj* b = new MyObj(10);
allocator.dump_free_memory(); allocator.dump_free_memory();
allocator.alloc(1024 * 1024); // should fail as only 1024 * 1024 - (4 + 4) bytes remain
allocator.dump_free_memory();
} }

0
c_os/user/HeapDemo.h Normal file → Executable file
View File

0
c_os/user/MyObj.h Normal file → Executable file
View File