add vorgabe03 + initial bump allocator
This commit is contained in:
80
c_os/kernel/allocator/BumpAllocator.cc
Normal file
80
c_os/kernel/allocator/BumpAllocator.cc
Normal file
@ -0,0 +1,80 @@
|
||||
/*****************************************************************************
|
||||
* *
|
||||
* B U M P A L L O C A T O R *
|
||||
* *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Eine sehr einfache Heap-Verwaltung, welche freigegebenen *
|
||||
* Speicher nicht mehr nutzen kann. *
|
||||
* *
|
||||
* Autor: Michael Schoettner, HHU, 3.3.2022 *
|
||||
*****************************************************************************/
|
||||
|
||||
#include "kernel/Globals.h"
|
||||
#include <stddef.h>
|
||||
// #include "kernel/allocator/LinkedListAllocator.h"
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: BumpAllocator::init *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: BumpAllokartor intitialisieren. *
|
||||
*****************************************************************************/
|
||||
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;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: BumpAllocator::dump_free_memory *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Ausgabe der Freispeicherinfos. Zu Debuggingzwecken. *
|
||||
*****************************************************************************/
|
||||
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;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: BumpAllocator::alloc *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Einen neuen Speicherblock allozieren. *
|
||||
*****************************************************************************/
|
||||
void* BumpAllocator::alloc(unsigned int req_size) {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
if (!this->initialized) {
|
||||
kout << "Allocator not initialized :(" << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (req_size > this->heap_end - (unsigned int)this->next) {
|
||||
kout << "More memory requested than available :(" << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* allocated = this->next;
|
||||
this->next = this->next + req_size;
|
||||
this->allocations = this->allocations + 1;
|
||||
|
||||
return allocated;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: BumpAllocator::free *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Nicht implementiert. *
|
||||
*****************************************************************************/
|
||||
void BumpAllocator::free(void* ptr) {
|
||||
kout << " mm_free: ptr= " << hex << (unsigned int)ptr << ", not supported" << endl;
|
||||
}
|
||||
34
c_os/kernel/allocator/BumpAllocator.h
Normal file
34
c_os/kernel/allocator/BumpAllocator.h
Normal file
@ -0,0 +1,34 @@
|
||||
/*****************************************************************************
|
||||
* *
|
||||
* B U M P A L L O C A T O R *
|
||||
* *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Eine sehr einfache Heap-Verwaltung, welche freigegebenen *
|
||||
* Speicher nicht mehr nutzen kann. *
|
||||
* *
|
||||
* Autor: Michael Schoettner, HHU, 3.3.2022 *
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef __BumpAllocator_include__
|
||||
#define __BumpAllocator_include__
|
||||
|
||||
#include "kernel/Allocator.h"
|
||||
|
||||
class BumpAllocator : Allocator {
|
||||
|
||||
private:
|
||||
unsigned char* next;
|
||||
unsigned int allocations;
|
||||
|
||||
BumpAllocator(Allocator& copy); // Verhindere Kopieren
|
||||
|
||||
public:
|
||||
BumpAllocator() {}
|
||||
|
||||
void init();
|
||||
void dump_free_memory();
|
||||
void* alloc(unsigned int req_size);
|
||||
void free(void* ptr);
|
||||
};
|
||||
|
||||
#endif
|
||||
70
c_os/kernel/allocator/LinkedListAllocator.cc
Normal file
70
c_os/kernel/allocator/LinkedListAllocator.cc
Normal file
@ -0,0 +1,70 @@
|
||||
/*****************************************************************************
|
||||
* *
|
||||
* L I N K E D L I S T A L L O C A T O R *
|
||||
* *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Einfache Speicherverwaltung, welche den freien Speicher *
|
||||
* mithilfe einer einfach verketteten Liste verwaltet. *
|
||||
* *
|
||||
* Autor: Michael Schoettner, HHU, 13.6.2020 *
|
||||
*****************************************************************************/
|
||||
|
||||
#include <stddef.h>
|
||||
#include "kernel/Globals.h"
|
||||
#include "kernel/allocator/LinkedListAllocator.h"
|
||||
|
||||
#define HEAP_MIN_FREE_BLOCK_SIZE 64 // min. Groesse eines freien Blocks
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: LinkedListAllocator::init *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Liste der Freispeicherbloecke intitialisieren. *
|
||||
* Anker zeigt auf ein Dummy-Element. Danach folgt *
|
||||
* ein Block der den gesamten freien Speicher umfasst. *
|
||||
* *
|
||||
* Wird automatisch aufgerufen, sobald eine Funktion der *
|
||||
* Speicherverwaltung erstmalig gerufen wird. *
|
||||
*****************************************************************************/
|
||||
void LinkedListAllocator::init() {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: LinkedListAllocator::dump_free_memory *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Ausgabe der Freispeicherliste. Zu Debuggingzwecken. *
|
||||
*****************************************************************************/
|
||||
void LinkedListAllocator::dump_free_memory() {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: LinkedListAllocator::alloc *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Einen neuen Speicherblock allozieren. *
|
||||
*****************************************************************************/
|
||||
void * LinkedListAllocator::alloc(unsigned int req_size) {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: LinkedListAllocator::free *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Einen Speicherblock freigeben. *
|
||||
*****************************************************************************/
|
||||
void LinkedListAllocator::free(void *ptr) {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
}
|
||||
|
||||
43
c_os/kernel/allocator/LinkedListAllocator.h
Normal file
43
c_os/kernel/allocator/LinkedListAllocator.h
Normal file
@ -0,0 +1,43 @@
|
||||
/*****************************************************************************
|
||||
* *
|
||||
* L I N K E D L I S T A L L O C A T O R *
|
||||
* *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Einfache Speicherverwaltung, welche den freien Speicher *
|
||||
* mithilfe einer einfach verketteten Liste verwaltet. *
|
||||
* *
|
||||
* Autor: Michael Schoettner, HHU, 13.6.2020 *
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef __LinkedListAllocator_include__
|
||||
#define __LinkedListAllocator_include__
|
||||
|
||||
#include "kernel/Allocator.h"
|
||||
|
||||
|
||||
// Format eines freien Blocks
|
||||
struct free_block {
|
||||
unsigned int size;
|
||||
struct free_block *next;
|
||||
};
|
||||
|
||||
|
||||
class LinkedListAllocator : Allocator {
|
||||
|
||||
private:
|
||||
// freie Bloecke werden verkettet
|
||||
struct free_block *free_start;
|
||||
|
||||
LinkedListAllocator(Allocator ©); // Verhindere Kopieren
|
||||
|
||||
public:
|
||||
LinkedListAllocator () { }
|
||||
|
||||
void init ();
|
||||
void dump_free_memory ();
|
||||
void* alloc (unsigned int req_size);
|
||||
void free (void *ptr);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user