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

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

@ -31,15 +31,14 @@
* 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_START 0x300000 // Startadresse des Heaps
#define HEAP_SIZE 1024*1024 // Default-Groesse des Heaps, falls #define HEAP_SIZE 1024 * 1024 // Default-Groesse des Heaps, falls \
// nicht über das BIOS ermittelbar // nicht über das BIOS ermittelbar
/***************************************************************************** /*****************************************************************************
@ -57,27 +56,26 @@ Allocator::Allocator() {
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); allocator.free(ptr);
} }
void operator delete[] ( void* 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, 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();

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

@ -9,13 +9,12 @@
* 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 *
*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*
@ -29,10 +28,8 @@
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 *
*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*
@ -41,30 +38,24 @@ 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 */
} }

19
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: private:
// freie Bloecke werden verkettet // freie Bloecke werden verkettet
struct free_block *free_start; struct free_block* free_start;
LinkedListAllocator(Allocator &copy); // Verhindere Kopieren 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