fixed wrong bump allocator init
This commit is contained in:
10
c_os/kernel/Allocator.cc
Normal file → Executable file
10
c_os/kernel/Allocator.cc
Normal file → Executable file
@ -31,15 +31,14 @@
|
||||
* Autor: Michael Schoettner, HHU, 1.3.2022 *
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stddef.h>
|
||||
#include "kernel/Globals.h"
|
||||
#include "kernel/Allocator.h"
|
||||
#include "kernel/Globals.h"
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
#define MEM_SIZE_DEF 8*1024*102 // Groesse des Speichers = 8 MB
|
||||
#define MEM_SIZE_DEF 8 * 1024 * 1024 // Groesse des Speichers = 8 MB
|
||||
|
||||
#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
|
||||
|
||||
/*****************************************************************************
|
||||
@ -57,7 +56,6 @@ Allocator::Allocator() {
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Nachfolgend sind die Operatoren von C++, die wir hier ueberschreiben *
|
||||
* und entsprechend 'mm_alloc' und 'mm_free' aufrufen. *
|
||||
|
||||
0
c_os/kernel/Allocator.h
Normal file → Executable file
0
c_os/kernel/Allocator.h
Normal file → Executable file
21
c_os/kernel/allocator/BumpAllocator.cc
Normal file → Executable file
21
c_os/kernel/allocator/BumpAllocator.cc
Normal file → Executable file
@ -22,13 +22,12 @@ void BumpAllocator::init() {
|
||||
|
||||
/* 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->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() {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
kout << "Freier Speicher:" << endl
|
||||
<< " Heap Start: " << this->heap_start << ", Heap End: " << this->heap_end << endl
|
||||
<< " Next: " << this->next << ", Allocations: " << this->allocations << endl;
|
||||
<< "Next: " << hex << this->next << ", Allocations: " << dec << this->allocations << endl;
|
||||
kout << endl;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
@ -53,13 +53,11 @@ void* BumpAllocator::alloc(unsigned int req_size) {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
if (!this->initialized) {
|
||||
kout << "Allocator not initialized :(" << endl;
|
||||
return NULL;
|
||||
}
|
||||
kout << "Requested " << dec << req_size << " Bytes" << endl;
|
||||
|
||||
if (req_size > this->heap_end - (unsigned int)this->next) {
|
||||
kout << "More memory requested than available :(" << endl;
|
||||
kout << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -67,6 +65,9 @@ void* BumpAllocator::alloc(unsigned int req_size) {
|
||||
this->next = this->next + req_size;
|
||||
this->allocations = this->allocations + 1;
|
||||
|
||||
kout << "Allocated " << dec << req_size << " Bytes." << endl;
|
||||
kout << endl;
|
||||
|
||||
return allocated;
|
||||
}
|
||||
|
||||
|
||||
2
c_os/kernel/allocator/BumpAllocator.h
Normal file → Executable file
2
c_os/kernel/allocator/BumpAllocator.h
Normal file → Executable file
@ -23,7 +23,7 @@ private:
|
||||
BumpAllocator(Allocator& copy); // Verhindere Kopieren
|
||||
|
||||
public:
|
||||
BumpAllocator() {}
|
||||
BumpAllocator() {}; // Allocator() called implicitely in C++
|
||||
|
||||
void init();
|
||||
void dump_free_memory();
|
||||
|
||||
13
c_os/kernel/allocator/LinkedListAllocator.cc
Normal file → Executable file
13
c_os/kernel/allocator/LinkedListAllocator.cc
Normal file → Executable file
@ -9,13 +9,12 @@
|
||||
* Autor: Michael Schoettner, HHU, 13.6.2020 *
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stddef.h>
|
||||
#include "kernel/Globals.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
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: LinkedListAllocator::init *
|
||||
*---------------------------------------------------------------------------*
|
||||
@ -29,10 +28,8 @@
|
||||
void LinkedListAllocator::init() {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: LinkedListAllocator::dump_free_memory *
|
||||
*---------------------------------------------------------------------------*
|
||||
@ -41,10 +38,8 @@ void LinkedListAllocator::init() {
|
||||
void LinkedListAllocator::dump_free_memory() {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: LinkedListAllocator::alloc *
|
||||
*---------------------------------------------------------------------------*
|
||||
@ -53,10 +48,8 @@ void LinkedListAllocator::dump_free_memory() {
|
||||
void* LinkedListAllocator::alloc(unsigned int req_size) {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: LinkedListAllocator::free *
|
||||
*---------------------------------------------------------------------------*
|
||||
@ -65,6 +58,4 @@ void * LinkedListAllocator::alloc(unsigned int req_size) {
|
||||
void LinkedListAllocator::free(void* ptr) {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
}
|
||||
|
||||
|
||||
3
c_os/kernel/allocator/LinkedListAllocator.h
Normal file → Executable file
3
c_os/kernel/allocator/LinkedListAllocator.h
Normal file → Executable file
@ -14,14 +14,12 @@
|
||||
|
||||
#include "kernel/Allocator.h"
|
||||
|
||||
|
||||
// Format eines freien Blocks
|
||||
struct free_block {
|
||||
unsigned int size;
|
||||
struct free_block* next;
|
||||
};
|
||||
|
||||
|
||||
class LinkedListAllocator : Allocator {
|
||||
|
||||
private:
|
||||
@ -37,7 +35,6 @@ public:
|
||||
void dump_free_memory();
|
||||
void* alloc(unsigned int req_size);
|
||||
void free(void* ptr);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -15,13 +15,11 @@
|
||||
#include "user/HeapDemo.h"
|
||||
|
||||
int main() {
|
||||
kout.clear();
|
||||
|
||||
// Speicherverwaltung initialisieren
|
||||
allocator.init();
|
||||
|
||||
// Bildschirm loeschen.
|
||||
kout.clear();
|
||||
|
||||
// text_demo();
|
||||
// sound_demo();
|
||||
// keyboard_demo();
|
||||
|
||||
3
c_os/user/HeapDemo.cc
Normal file → Executable file
3
c_os/user/HeapDemo.cc
Normal file → Executable file
@ -34,4 +34,7 @@ void heap_demo() {
|
||||
|
||||
MyObj* b = new MyObj(10);
|
||||
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
0
c_os/user/HeapDemo.h
Normal file → Executable file
0
c_os/user/MyObj.h
Normal file → Executable file
0
c_os/user/MyObj.h
Normal file → Executable file
Reference in New Issue
Block a user