From f2646300d816fc4edf98c40a8d70ab64d71e1d41 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Mon, 23 May 2022 09:51:35 +0200 Subject: [PATCH] add debug output toggle --- c_os/kernel/Globals.h | 2 ++ c_os/kernel/allocator/TreeAllocator.cc | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/c_os/kernel/Globals.h b/c_os/kernel/Globals.h index b448a45..29f7e12 100755 --- a/c_os/kernel/Globals.h +++ b/c_os/kernel/Globals.h @@ -32,4 +32,6 @@ extern unsigned int total_mem; // RAM total // extern LinkedListAllocator allocator; extern TreeAllocator allocator; +constexpr bool DEBUG = true; + #endif diff --git a/c_os/kernel/allocator/TreeAllocator.cc b/c_os/kernel/allocator/TreeAllocator.cc index 4eee697..0238c77 100755 --- a/c_os/kernel/allocator/TreeAllocator.cc +++ b/c_os/kernel/allocator/TreeAllocator.cc @@ -41,12 +41,12 @@ void* TreeAllocator::alloc(unsigned int req_size) { // the list_block_t is part of every block, but when freeing // memory we need enough space to store the rbt metadata rreq_size = sizeof(tree_block_t) - sizeof(list_block_t); - // kout << " - Increased block size for rbt metadata" << endl; + if (DEBUG) kout << " - Increased block size for rbt metadata" << endl; } unsigned int req_size_diff = (BASIC_ALIGN - rreq_size % BASIC_ALIGN) % BASIC_ALIGN; rreq_size = rreq_size + req_size_diff; if (req_size_diff > 0) { - // kout << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl; + if (DEBUG) kout << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl; } // Finds smallest block that is large enough @@ -57,19 +57,19 @@ void* TreeAllocator::alloc(unsigned int req_size) { } if (best_fit->allocated) { // Something went really wrong - // kout << " - Block already allocated :(" << endl; + kout << " - Block already allocated :(" << endl; return NULL; } best_fit->allocated = true; unsigned int size = this->get_size(best_fit); - // kout << " - Found best-fit: " << hex << (unsigned int)best_fit << endl; + if (DEBUG) kout << " - Found best-fit: " << hex << (unsigned int)best_fit << endl; // Remove the block first so we can insert correctly when cutting // kout << " - Removing block from freelist" << endl; this->rbt_remove(best_fit); if (size > HEAP_MIN_FREE_BLOCK_SIZE + rreq_size + sizeof(list_block_t)) { // Block can be cut - // kout << " - Allocating " << dec << rreq_size << " Bytes with cutting" << endl; + if (DEBUG) kout << " - Allocating " << dec << rreq_size << " Bytes with cutting" << endl; // [best_fit_start | sizeof(list_block_t) | rreq_size | new_block_start] tree_block_t* new_block = (tree_block_t*)((char*)best_fit + sizeof(list_block_t) + rreq_size); @@ -80,10 +80,10 @@ void* TreeAllocator::alloc(unsigned int req_size) { // Don't cut block // The block is already correctly positioned in the linked list so we only // need to remove it from the freelist, which is done for both cases - // kout << " - Allocating " << dec << rreq_size << " Bytes without cutting" << endl; + if (DEBUG) kout << " - Allocating " << dec << rreq_size << " Bytes without cutting" << endl; } - // kout << " - Returned address " << hex << (unsigned int)((char*)best_fit + sizeof(list_block_t)) << endl; + if (DEBUG) kout << " - Returned address " << hex << (unsigned int)((char*)best_fit + sizeof(list_block_t)) << endl; return (void*)((char*)best_fit + sizeof(list_block_t)); } @@ -107,7 +107,7 @@ void TreeAllocator::free(void* ptr) { if (!next->allocated) { // Merge forward - // kout << " - Merging forward" << endl; + if (DEBUG) kout << " - Merging forward" << endl; // Remove the next block from all lists as it is now part of our freed block this->dll_remove(next); @@ -120,13 +120,13 @@ void TreeAllocator::free(void* ptr) { if (!previous->allocated) { // Merge backward - // kout << " - Merging backward" << endl; + if (DEBUG) kout << " - Merging backward" << endl; // Remove the current block from all lists as it is now part of the previous block // It doesn't have to be removed from rbt as it wasn't in there as it was allocated before this->dll_remove(block); this->rbt_remove((tree_block_t*)previous); - this->rbt_insert((tree_block_t*)previous); + this->rbt_insert((tree_block_t*)previous); // Reinsert with new size } }