add a shnitton of loggers
This commit is contained in:
@ -25,7 +25,7 @@ void BumpAllocator::init() {
|
||||
this->allocations = 0;
|
||||
this->next = (unsigned char*)heap_start;
|
||||
|
||||
if constexpr (DEBUG) { kout << "Initialized Bump Allocator" << endl; }
|
||||
log << INFO << "Initialized Bump Allocator" << endl;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
@ -51,10 +51,10 @@ void* BumpAllocator::alloc(unsigned int req_size) {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
if constexpr (DEBUG) { kout << "Requested " << hex << req_size << " Bytes" << endl; }
|
||||
log << DEBUG << "Requested " << hex << req_size << " Bytes" << endl;
|
||||
|
||||
if (req_size + (unsigned int)this->next > this->heap_end) {
|
||||
if constexpr (DEBUG) { kout << " - More memory requested than available :(" << endl; }
|
||||
log << ERROR << " - More memory requested than available :(" << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ void* BumpAllocator::alloc(unsigned int req_size) {
|
||||
this->next = (unsigned char*)((unsigned int)this->next + req_size);
|
||||
this->allocations = this->allocations + 1;
|
||||
|
||||
if constexpr (DEBUG) { kout << " - Allocated " << hex << req_size << " Bytes." << endl; }
|
||||
log << TRACE << " - Allocated " << hex << req_size << " Bytes." << endl;
|
||||
|
||||
return allocated;
|
||||
}
|
||||
@ -73,5 +73,5 @@ void* BumpAllocator::alloc(unsigned int req_size) {
|
||||
* Beschreibung: Nicht implementiert. *
|
||||
*****************************************************************************/
|
||||
void BumpAllocator::free(void* ptr) {
|
||||
if constexpr (DEBUG) { kout << " mm_free: ptr= " << hex << (unsigned int)ptr << ", not supported" << endl; }
|
||||
log << ERROR << " mm_free: ptr= " << hex << (unsigned int)ptr << ", not supported" << endl;
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
#define __BumpAllocator_include__
|
||||
|
||||
#include "kernel/Allocator.h"
|
||||
#include "user/lib/Logger.h"
|
||||
|
||||
class BumpAllocator : Allocator {
|
||||
|
||||
@ -20,10 +21,12 @@ private:
|
||||
unsigned char* next;
|
||||
unsigned int allocations;
|
||||
|
||||
Logger log;
|
||||
|
||||
BumpAllocator(Allocator& copy) = delete; // Verhindere Kopieren
|
||||
|
||||
public:
|
||||
BumpAllocator() {}; // Allocator() called implicitely in C++
|
||||
BumpAllocator() : log("BMP-Alloc") {}; // Allocator() called implicitely in C++
|
||||
|
||||
void init() override;
|
||||
void dump_free_memory() override;
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
|
||||
#include "kernel/allocator/LinkedListAllocator.h"
|
||||
#include "kernel/Globals.h"
|
||||
#include "user/lib/Logger.h"
|
||||
#include <stddef.h>
|
||||
|
||||
// I don't order the list by size so that the block order corresponds to the location in memory
|
||||
@ -37,7 +38,7 @@ void LinkedListAllocator::init() {
|
||||
this->free_start->size = this->heap_size - sizeof(free_block_t);
|
||||
this->free_start->next = this->free_start; // Only one block, points to itself
|
||||
|
||||
if constexpr (DEBUG) { kout << "Initialized LinkedList Allocator" << endl; }
|
||||
log << INFO << "Initialized LinkedList Allocator" << endl;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
@ -75,10 +76,10 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
// NOTE: next pointer zeigt auf headeranfang, returned wird zeiger auf anfang des nutzbaren freispeichers
|
||||
|
||||
if constexpr (DEBUG) { kout << "Requested " << hex << req_size << " Bytes" << endl; }
|
||||
log << DEBUG << "Requested " << hex << req_size << " Bytes" << endl;
|
||||
|
||||
if (this->free_start == NULL) {
|
||||
if constexpr (DEBUG) { kout << " - No free memory remaining :(" << endl; }
|
||||
log << ERROR << " - No free memory remaining :(" << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -86,7 +87,7 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
|
||||
unsigned int req_size_diff = (BASIC_ALIGN - req_size % BASIC_ALIGN) % BASIC_ALIGN;
|
||||
unsigned int rreq_size = req_size + req_size_diff;
|
||||
if (req_size_diff > 0) {
|
||||
if constexpr (DEBUG) { kout << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl; }
|
||||
log << TRACE << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl;
|
||||
}
|
||||
|
||||
free_block_t* current = this->free_start;
|
||||
@ -117,7 +118,7 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
|
||||
// Next-fit
|
||||
this->free_start = new_next;
|
||||
|
||||
if constexpr (DEBUG) { kout << " - Allocated " << hex << rreq_size << " Bytes with cutting" << endl; }
|
||||
log << TRACE << " - Allocated " << hex << rreq_size << " Bytes with cutting" << endl;
|
||||
} else {
|
||||
// Block too small to be cut, allocate whole block
|
||||
|
||||
@ -125,11 +126,11 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
|
||||
this->free_start = current->next; // Pointer keeps pointing to current if last block
|
||||
if (this->free_start == current) {
|
||||
// No free block remaining
|
||||
if constexpr (DEBUG) { kout << " - Disabled freelist" << endl; }
|
||||
log << TRACE << " - Disabled freelist" << endl;
|
||||
this->free_start = NULL;
|
||||
}
|
||||
|
||||
if constexpr (DEBUG) { kout << " - Allocated " << hex << current->size << " Bytes without cutting" << endl; }
|
||||
log << TRACE << " - Allocated " << hex << current->size << " Bytes without cutting" << endl;
|
||||
}
|
||||
|
||||
// Block aushängen
|
||||
@ -146,7 +147,7 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
|
||||
current = current->next;
|
||||
} while (current != this->free_start); // Stop when arriving at the first block again
|
||||
|
||||
if constexpr (DEBUG) { kout << " - More memory requested than available :(" << endl; }
|
||||
log << ERROR << " - More memory requested than available :(" << endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -159,7 +160,7 @@ void LinkedListAllocator::free(void* ptr) {
|
||||
|
||||
/* Hier muess Code eingefuegt werden */
|
||||
|
||||
if constexpr (DEBUG) { kout << "Freeing " << hex << (unsigned int)ptr << endl; }
|
||||
log << DEBUG << "Freeing " << hex << (unsigned int)ptr << endl;
|
||||
|
||||
free_block_t* block_start = (free_block_t*)((unsigned int)ptr - sizeof(free_block_t));
|
||||
|
||||
@ -170,8 +171,7 @@ void LinkedListAllocator::free(void* ptr) {
|
||||
block_start->allocated = false;
|
||||
block_start->next = block_start;
|
||||
|
||||
if constexpr (DEBUG) { kout << " - Enabling freelist with one block" << endl; }
|
||||
|
||||
log << TRACE << " - Enabling freelist with one block" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -201,7 +201,7 @@ void LinkedListAllocator::free(void* ptr) {
|
||||
|
||||
// Try to merge forward ========================================================================
|
||||
if (next_block == next_free) {
|
||||
if constexpr (DEBUG) { kout << " - Merging block forward" << endl; }
|
||||
log << TRACE << " - Merging block forward" << endl;
|
||||
|
||||
// Current and next adjacent block can be merged
|
||||
// [previous_free | previous_free_next | <> | block_start | next_free]
|
||||
@ -220,7 +220,7 @@ void LinkedListAllocator::free(void* ptr) {
|
||||
|
||||
if (this->free_start == next_free) {
|
||||
// next_free is now invalid after merge
|
||||
if constexpr (DEBUG) { kout << " - Moving freelist start to " << hex << (unsigned int)block_start << endl; }
|
||||
log << TRACE << " - Moving freelist start to " << hex << (unsigned int)block_start << endl;
|
||||
this->free_start = block_start;
|
||||
}
|
||||
} else {
|
||||
@ -236,7 +236,7 @@ void LinkedListAllocator::free(void* ptr) {
|
||||
|
||||
// Try to merge backward =====================================================================
|
||||
if (previous_free_next == block_start) {
|
||||
if constexpr (DEBUG) { kout << " - Merging block backward" << endl; }
|
||||
log << TRACE << " - Merging block backward" << endl;
|
||||
|
||||
// Current and previous adjacent block can be merged
|
||||
// [previous_free | block_start]
|
||||
@ -249,7 +249,7 @@ void LinkedListAllocator::free(void* ptr) {
|
||||
|
||||
if (this->free_start == block_start) {
|
||||
// block_start is now invalid after merge
|
||||
if constexpr (DEBUG) { kout << " - Moving freelist start to " << hex << (unsigned int)previous_free << endl; }
|
||||
log << TRACE << " - Moving freelist start to " << hex << (unsigned int)previous_free << endl;
|
||||
this->free_start = previous_free;
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
#define __LinkedListAllocator_include__
|
||||
|
||||
#include "kernel/Allocator.h"
|
||||
#include "user/lib/Logger.h"
|
||||
|
||||
// Format eines freien Blocks, 4 + 4 + 4 Byte
|
||||
typedef struct free_block {
|
||||
@ -28,21 +29,21 @@ typedef struct free_block {
|
||||
} free_block_t;
|
||||
|
||||
class LinkedListAllocator : Allocator {
|
||||
|
||||
private:
|
||||
// freie Bloecke werden verkettet
|
||||
struct free_block* free_start;
|
||||
|
||||
LinkedListAllocator(Allocator& copy) = delete; // Verhindere Kopieren
|
||||
|
||||
// NOTE: I added this
|
||||
// Traverses the whole list forward till previous block is reached.
|
||||
// This can only be called on free blocks as allocated blocks
|
||||
// aren't reachable from the freelist.
|
||||
static struct free_block* find_previous_block(struct free_block*);
|
||||
|
||||
Logger log;
|
||||
|
||||
public:
|
||||
LinkedListAllocator() {}
|
||||
LinkedListAllocator() : log("LL-Alloc") {}
|
||||
|
||||
void init() override;
|
||||
void dump_free_memory() override;
|
||||
|
||||
Reference in New Issue
Block a user