From 663fabf074dccf447ca4f95ab8c18c9bfedc1698 Mon Sep 17 00:00:00 2001 From: ChUrl Date: Thu, 8 Dec 2022 02:19:52 +0100 Subject: [PATCH] Implement Util::System to keep system utility functions like streams --- cmake/lib/util/CMakeLists.txt | 5 ++ src/kernel/memory/BumpAllocator.cc | 7 +- src/kernel/memory/LinkedListAllocator.cc | 13 +-- src/kernel/memory/TreeAllocator.cc | 8 +- src/lib/demo/ArrayDemo.cc | 39 ++++----- src/lib/demo/HeapDemo.cc | 29 +++---- src/lib/demo/KeyboardDemo.cc | 22 ++--- src/lib/demo/KeyboardDemo.h | 7 +- src/lib/demo/MainMenu.cc | 100 +++++++++++------------ src/lib/demo/PCSPKdemo.cc | 15 ++-- src/lib/demo/PagingDemo.cc | 28 ++++--- src/lib/demo/PreemptiveThreadDemo.cc | 21 ++--- src/lib/demo/SmartPointerDemo.cc | 13 +-- src/lib/demo/StringDemo.cc | 43 +++++----- src/lib/demo/TextDemo.cc | 37 +++++---- src/lib/demo/VectorDemo.cc | 15 ++-- src/lib/util/System.cpp | 7 ++ src/lib/util/System.h | 18 ++++ 18 files changed, 236 insertions(+), 191 deletions(-) create mode 100644 cmake/lib/util/CMakeLists.txt create mode 100644 src/lib/util/System.cpp create mode 100644 src/lib/util/System.h diff --git a/cmake/lib/util/CMakeLists.txt b/cmake/lib/util/CMakeLists.txt new file mode 100644 index 0000000..da7bd3d --- /dev/null +++ b/cmake/lib/util/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.14) + +target_sources(lib PUBLIC + ${CHURLOS_SRC_DIR}/lib/util/System.cpp + ) diff --git a/src/kernel/memory/BumpAllocator.cc b/src/kernel/memory/BumpAllocator.cc index c44ab6b..6bad194 100755 --- a/src/kernel/memory/BumpAllocator.cc +++ b/src/kernel/memory/BumpAllocator.cc @@ -11,6 +11,7 @@ #include "BumpAllocator.h" #include "kernel/system/Globals.h" +#include "lib/util/System.h" namespace Kernel { @@ -38,9 +39,9 @@ void BumpAllocator::dump_free_memory() { /* Hier muess Code eingefuegt werden */ - Kernel::kout << "Freier Speicher:" << endl - << " - Next: " << hex << reinterpret_cast(next) - << ", Allocations: " << dec << allocations << endl; + Util::System::out << "Freier Speicher:" << endl + << " - Next: " << hex << reinterpret_cast(next) + << ", Allocations: " << dec << allocations << endl; } /***************************************************************************** diff --git a/src/kernel/memory/LinkedListAllocator.cc b/src/kernel/memory/LinkedListAllocator.cc index d80f600..ced7999 100755 --- a/src/kernel/memory/LinkedListAllocator.cc +++ b/src/kernel/memory/LinkedListAllocator.cc @@ -11,6 +11,7 @@ #include "LinkedListAllocator.h" #include "kernel/system/Globals.h" +#include "lib/util/System.h" // I don't order the list by size so that the block order corresponds to the location in memory // Then I can easily merge adjacent free blocks by finding the previous block without looking at @@ -50,17 +51,17 @@ void LinkedListAllocator::dump_free_memory() { /* Hier muess Code eingefuegt werden */ - Kernel::kout << "Freier Speicher:" << endl; + Util::System::out << "Freier Speicher:" << endl; if (free_start == nullptr) { - Kernel::kout << " - No free Blocks" << endl; + Util::System::out << " - No free Blocks" << endl; } else { - Kernel::kout << " - Freelist start: " << hex << reinterpret_cast(free_start) << endl; + Util::System::out << " - Freelist start: " << hex << reinterpret_cast(free_start) << endl; free_block_t *current = free_start; do { - Kernel::kout << " - Free Block (Start: " << hex << reinterpret_cast(current) - << " Size: " << hex << current->size << ")" << endl; + Util::System::out << " - Free Block (Start: " << hex << reinterpret_cast(current) + << " Size: " << hex << current->size << ")" << endl; current = current->next; } while (current != free_start); } @@ -312,7 +313,7 @@ free_block_t *LinkedListAllocator::find_previous_block(free_block_t *next_block) } // if (current == next_block) { - // Kernel::kout << "LinkedListAllocator::find_previous_block returned the input block" << endl; + // Util::System::out << "LinkedListAllocator::find_previous_block returned the input block" << endl; // } return current; diff --git a/src/kernel/memory/TreeAllocator.cc b/src/kernel/memory/TreeAllocator.cc index 1a69a3a..6dd0d7b 100755 --- a/src/kernel/memory/TreeAllocator.cc +++ b/src/kernel/memory/TreeAllocator.cc @@ -17,13 +17,13 @@ void TreeAllocator::init() { } void TreeAllocator::dump_free_memory() { - Kernel::kout << "Free Memory:" << endl; + Util::System::out << "Free Memory:" << endl; list_block_t *current = reinterpret_cast(heap_start); do { if (!current->allocated) { - Kernel::kout << " - Free Block at " << reinterpret_cast(current) << ", Size: " - << reinterpret_cast(current->next) - reinterpret_cast(current) - << endl; + Util::System::out << " - Free Block at " << reinterpret_cast(current) << ", Size: " + << reinterpret_cast(current->next) - reinterpret_cast(current) + << endl; } current = current->next; } while (reinterpret_cast(current) != heap_start); diff --git a/src/lib/demo/ArrayDemo.cc b/src/lib/demo/ArrayDemo.cc index 82fec6c..11a2520 100644 --- a/src/lib/demo/ArrayDemo.cc +++ b/src/lib/demo/ArrayDemo.cc @@ -1,40 +1,41 @@ #include "ArrayDemo.h" +#include "lib/util/System.h" void ArrayDemo::run() { - Container::Array arr1 {}; - Container::Array arr2 {}; + Container::Array arr1{}; + Container::Array arr2{}; - Kernel::kout.lock(); - Kernel::kout.clear(); + Util::System::out.lock(); + Util::System::out.clear(); - Kernel::kout << "Adding..." << endl; + Util::System::out << "Adding..." << endl; for (int i = 0; i < 10; ++i) { arr1[i] = i; } - Kernel::kout << "Iterator printing arr1:" << endl; - for (int i : arr1) { - Kernel::kout << i << " "; + Util::System::out << "Iterator printing arr1:" << endl; + for (int i: arr1) { + Util::System::out << i << " "; } - Kernel::kout << endl; + Util::System::out << endl; - Kernel::kout << "Swapping arr1 and arr2..." << endl; + Util::System::out << "Swapping arr1 and arr2..." << endl; arr1.swap(arr2); - Kernel::kout << "Iterator printing arr1:" << endl; - for (int i : arr1) { - Kernel::kout << i << " "; + Util::System::out << "Iterator printing arr1:" << endl; + for (int i: arr1) { + Util::System::out << i << " "; } - Kernel::kout << endl; + Util::System::out << endl; - Kernel::kout << "Iterator printing arr2:" << endl; - for (int i : arr2) { - Kernel::kout << i << " "; + Util::System::out << "Iterator printing arr2:" << endl; + for (int i: arr2) { + Util::System::out << i << " "; } - Kernel::kout << endl; + Util::System::out << endl; // arr1.swap(arr3); // Not possible as type/size has to match - Kernel::kout.unlock(); + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/demo/HeapDemo.cc b/src/lib/demo/HeapDemo.cc index 9a14f98..14f88f7 100755 --- a/src/lib/demo/HeapDemo.cc +++ b/src/lib/demo/HeapDemo.cc @@ -9,30 +9,31 @@ *****************************************************************************/ #include "HeapDemo.h" +#include "lib/util/System.h" void HeapDemo::run() { - Kernel::kout.lock(); - Kernel::kout.clear(); - Kernel::kout << "HEAP_DEMO ===================================================================" << endl; + Util::System::out.lock(); + Util::System::out.clear(); + Util::System::out << "HEAP_DEMO ===================================================================" << endl; /* hier muss Code eingefuegt werden */ Kernel::allocator.dump_free_memory(); // Rounding to word border - Kernel::kout << "ROUNDING ====================================================================" << endl; - void* alloc = Kernel::allocator.alloc(1); // 1 Byte + Util::System::out << "ROUNDING ====================================================================" << endl; + void *alloc = Kernel::allocator.alloc(1); // 1 Byte Kernel::allocator.dump_free_memory(); Kernel::allocator.free(alloc); Kernel::allocator.dump_free_memory(); // Some objects and forward/backward merging - Kernel::kout << "SOME OBJECTS ================================================================" << endl; - MyObj* a = new MyObj(5); + Util::System::out << "SOME OBJECTS ================================================================" << endl; + MyObj *a = new MyObj(5); Kernel::allocator.dump_free_memory(); - MyObj* b = new MyObj(10); + MyObj *b = new MyObj(10); Kernel::allocator.dump_free_memory(); - MyObj* c = new MyObj(15); + MyObj *c = new MyObj(15); Kernel::allocator.dump_free_memory(); delete b; // No merge Kernel::allocator.dump_free_memory(); @@ -48,7 +49,7 @@ void HeapDemo::run() { // allocator.dump_free_memory(); // Allocate too much - Kernel::kout << "TOO MUCH ====================================================================" << endl; + Util::System::out << "TOO MUCH ====================================================================" << endl; Kernel::allocator.alloc(1024 * 1024); // should fail as only 1024 * 1024 - Headersize bytes are available Kernel::allocator.dump_free_memory(); @@ -65,14 +66,14 @@ void HeapDemo::run() { // allocator.dump_free_memory(); // Array allocation - Kernel::kout << "ARRAY =======================================================================" << endl; - MyObj* objs = new MyObj[1024]; + Util::System::out << "ARRAY =======================================================================" << endl; + MyObj *objs = new MyObj[1024]; Kernel::allocator.dump_free_memory(); delete[] objs; Kernel::allocator.dump_free_memory(); - Kernel::kout << "HEAP_DEMO END ===============================================================" << endl; + Util::System::out << "HEAP_DEMO END ===============================================================" << endl; - Kernel::kout.unlock(); + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/demo/KeyboardDemo.cc b/src/lib/demo/KeyboardDemo.cc index d8a889a..7d1582e 100755 --- a/src/lib/demo/KeyboardDemo.cc +++ b/src/lib/demo/KeyboardDemo.cc @@ -14,21 +14,21 @@ void KeyboardDemo::run() { /* Hier muess Code eingefuegt werden */ - Kernel::kout << "Keyboard Demo: " << endl; + Util::System::out << "Keyboard Demo: " << endl; - Kernel::kout.lock(); - Kernel::kout.clear(); - Kernel::kout << "Info: Die Keyboard Demo sperrt den Output Stream:\n" - << " Wenn die Preemption Demo laeuft wird diese also erst\n" - << " fortfahren wenn die Keyboard Demo wieder beendet ist." << endl; - Kernel::kout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nInput: "; - Kernel::kout.flush(); + Util::System::out.lock(); + Util::System::out.clear(); + Util::System::out << "Info: Die Keyboard Demo sperrt den Output Stream:\n" + << " Wenn die Preemption Demo laeuft wird diese also erst\n" + << " fortfahren wenn die Keyboard Demo wieder beendet ist." << endl; + Util::System::out << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nInput: "; + Util::System::out.flush(); while (running) { - Kernel::kout << listener.waitForKeyEvent(); - Kernel::kout.flush(); + Util::System::out << listener.waitForKeyEvent(); + Util::System::out.flush(); } - Kernel::kout.unlock(); + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/demo/KeyboardDemo.h b/src/lib/demo/KeyboardDemo.h index 5c71c4b..f4edcc8 100755 --- a/src/lib/demo/KeyboardDemo.h +++ b/src/lib/demo/KeyboardDemo.h @@ -14,13 +14,14 @@ #include "kernel/system/Globals.h" #include "kernel/process/Thread.h" #include "kernel/event/KeyEventListener.h" +#include "lib/util/System.h" class KeyboardDemo : public Kernel::Thread { private: Kernel::KeyEventListener listener; public: - KeyboardDemo(const KeyboardDemo& copy) = delete; + KeyboardDemo(const KeyboardDemo ©) = delete; KeyboardDemo() : Thread("KeyboardDemo"), listener(tid) { Kernel::kevman.subscribe(listener); @@ -30,10 +31,10 @@ public: ~KeyboardDemo() override { if (running) { // NOTE: If the thread was exited nicely it can unlock before destructor, - // but on forced kill Kernel::kout has to be unlocked in the destructor. + // but on forced kill Util::System::out has to be unlocked in the destructor. // This is bad since it could release the lock although some other // thread set it (so use nice_kill) - Kernel::kout.unlock(); + Util::System::out.unlock(); } Kernel::kevman.unsubscribe(listener); } diff --git a/src/lib/demo/MainMenu.cc b/src/lib/demo/MainMenu.cc index f2b0ab2..70a768d 100644 --- a/src/lib/demo/MainMenu.cc +++ b/src/lib/demo/MainMenu.cc @@ -12,23 +12,23 @@ #include "VectorDemo.h" void print_demo_menu() { - Kernel::kout.lock(); - Kernel::kout.clear(); - Kernel::kout << "Demo Menu, press number to start, k/K to kill:\n" - << "1 - Text Demo\n" - << "2 - PCSPK Demo\n" - << "3 - Keyboard Demo\n" - << "4 - Heap Demo\n" - << "5 - VBE Demo\n" - << "6 - Paging Demo\n" - << "7 - Preemption Demo\n" - << "Extra demos:\n" - << "8 - bse::vector demo\n" - << "9 - bse::array demo\n" - << "0 - bse::unique_ptr demo\n" - << "! - bse::string demo\n" - << endl; - Kernel::kout.unlock(); + Util::System::out.lock(); + Util::System::out.clear(); + Util::System::out << "Demo Menu, press number to start, k/K to kill:\n" + << "1 - Text Demo\n" + << "2 - PCSPK Demo\n" + << "3 - Keyboard Demo\n" + << "4 - Heap Demo\n" + << "5 - VBE Demo\n" + << "6 - Paging Demo\n" + << "7 - Preemption Demo\n" + << "Extra demos:\n" + << "8 - bse::vector demo\n" + << "9 - bse::array demo\n" + << "0 - bse::unique_ptr demo\n" + << "! - bse::string demo\n" + << endl; + Util::System::out.unlock(); } void MainMenu::run() { @@ -41,40 +41,40 @@ void MainMenu::run() { if ((input >= '0' && input <= '9') || input == '!') { switch (input) { - case '1': - running_demo = Kernel::scheduler.ready(); - break; - case '2': - running_demo = Kernel::scheduler.ready(&Device::PCSPK::aerodynamic); - break; - case '3': - running_demo = Kernel::scheduler.ready(); - break; - case '4': - running_demo = Kernel::scheduler.ready(); - break; - case '5': - running_demo = Kernel::scheduler.ready(); - break; - case '6': - running_demo = Kernel::scheduler.ready(); - break; - case '7': - running_demo = Kernel::scheduler.ready(3); - break; + case '1': + running_demo = Kernel::scheduler.ready(); + break; + case '2': + running_demo = Kernel::scheduler.ready(&Device::PCSPK::aerodynamic); + break; + case '3': + running_demo = Kernel::scheduler.ready(); + break; + case '4': + running_demo = Kernel::scheduler.ready(); + break; + case '5': + running_demo = Kernel::scheduler.ready(); + break; + case '6': + running_demo = Kernel::scheduler.ready(); + break; + case '7': + running_demo = Kernel::scheduler.ready(3); + break; - case '8': - running_demo = Kernel::scheduler.ready(); - break; - case '9': - running_demo = Kernel::scheduler.ready(); - break; - case '0': - running_demo = Kernel::scheduler.ready(); - break; - case '!': - running_demo = Kernel::scheduler.ready(); - break; + case '8': + running_demo = Kernel::scheduler.ready(); + break; + case '9': + running_demo = Kernel::scheduler.ready(); + break; + case '0': + running_demo = Kernel::scheduler.ready(); + break; + case '!': + running_demo = Kernel::scheduler.ready(); + break; } } else if (input == 'k') { Kernel::scheduler.nice_kill(running_demo); // NOTE: If thread exits itself this will throw error diff --git a/src/lib/demo/PCSPKdemo.cc b/src/lib/demo/PCSPKdemo.cc index 4cf9140..418aaef 100644 --- a/src/lib/demo/PCSPKdemo.cc +++ b/src/lib/demo/PCSPKdemo.cc @@ -1,16 +1,17 @@ #include "PCSPKdemo.h" +#include "lib/util/System.h" void PCSPKdemo::run() { - Kernel::kout.lock(); - Kernel::kout.clear(); - Kernel::kout << "Playing..." << endl; - Kernel::kout.unlock(); + Util::System::out.lock(); + Util::System::out.clear(); + Util::System::out << "Playing..." << endl; + Util::System::out.unlock(); (*melody)(); // This syntax is confusing as hell - Kernel::kout.lock(); - Kernel::kout << "Finished" << endl; - Kernel::kout.unlock(); + Util::System::out.lock(); + Util::System::out << "Finished" << endl; + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/demo/PagingDemo.cc b/src/lib/demo/PagingDemo.cc index 8d00b77..a1b594b 100644 --- a/src/lib/demo/PagingDemo.cc +++ b/src/lib/demo/PagingDemo.cc @@ -1,14 +1,16 @@ #include "PagingDemo.h" +#include "lib/util/System.h" void PagingDemo::writeprotect_page() { - Kernel::kout << "Accessing a writeprotected page triggers bluescreen,\nif you can read this it didn't work" << endl; + Util::System::out << "Accessing a writeprotected page triggers bluescreen,\nif you can read this it didn't work" + << endl; // BlueScreen 1 // asm("int $3"); // BlueScreen 2 log.info() << "Allocating page" << endl; - uint32_t* page = Kernel::pg_alloc_page(); + uint32_t *page = Kernel::pg_alloc_page(); *page = 42; log.info() << "Writeprotecting page..." << endl; Kernel::pg_write_protect_page(page); @@ -20,30 +22,30 @@ void PagingDemo::writeprotect_page() { } void PagingDemo::notpresent_page() { - Kernel::kout << "Produces pagefault, if you can read this it didn't work" << endl; + Util::System::out << "Produces pagefault, if you can read this it didn't work" << endl; log.info() << "Allocating page" << endl; - uint32_t* page = Kernel::pg_alloc_page(); + uint32_t *page = Kernel::pg_alloc_page(); *page = 42; log.info() << "Marking page notpresent..." << endl; Kernel::pg_notpresent_page(page); log.info() << "Accessing page" << endl; - Kernel::kout << "Page not accessible: " << *page << endl; + Util::System::out << "Page not accessible: " << *page << endl; // No free because bluescreen } void PagingDemo::run() { - Kernel::kout << "Press w for writeprotect demo, n for notpresent demo" << endl; - switch(listener.waitForKeyEvent()) { - case 'w': - writeprotect_page(); - break; - case 'n': - notpresent_page(); - break; + Util::System::out << "Press w for writeprotect demo, n for notpresent demo" << endl; + switch (listener.waitForKeyEvent()) { + case 'w': + writeprotect_page(); + break; + case 'n': + notpresent_page(); + break; } Kernel::scheduler.exit(); diff --git a/src/lib/demo/PreemptiveThreadDemo.cc b/src/lib/demo/PreemptiveThreadDemo.cc index 991c3e3..7329ebc 100644 --- a/src/lib/demo/PreemptiveThreadDemo.cc +++ b/src/lib/demo/PreemptiveThreadDemo.cc @@ -1,34 +1,35 @@ #include "PreemptiveThreadDemo.h" +#include "lib/util/System.h" void PreemptiveLoopThread::run() { int cnt = 0; while (running) { // Basic synchronization by semaphore - Kernel::kout.lock(); + Util::System::out.lock(); - // Saving + restoring Kernel::kout position doesn't help much as preemption still occurs + // Saving + restoring Util::System::out position doesn't help much as preemption still occurs CGA_Stream::setpos(55, id); - Kernel::kout << fillw(3) << id << fillw(0) << ": " << dec << cnt++ << endl; + Util::System::out << fillw(3) << id << fillw(0) << ": " << dec << cnt++ << endl; - Kernel::kout.unlock(); + Util::System::out.unlock(); } Kernel::scheduler.exit(); } void PreemptiveThreadDemo::run() { - Kernel::kout.lock(); - Kernel::kout.clear(); + Util::System::out.lock(); + Util::System::out.clear(); - Kernel::kout << "Preemptive Thread Demo:" << endl; + Util::System::out << "Preemptive Thread Demo:" << endl; - Kernel::kout << "Readying LoopThreads" << endl; + Util::System::out << "Readying LoopThreads" << endl; for (unsigned int i = 0; i < number_of_threads; ++i) { Kernel::scheduler.ready(i); } - Kernel::kout << "Exiting main thread" << endl; - Kernel::kout.unlock(); + Util::System::out << "Exiting main thread" << endl; + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/demo/SmartPointerDemo.cc b/src/lib/demo/SmartPointerDemo.cc index 30045c2..99fe967 100644 --- a/src/lib/demo/SmartPointerDemo.cc +++ b/src/lib/demo/SmartPointerDemo.cc @@ -1,11 +1,12 @@ #include "SmartPointerDemo.h" #include "kernel/process/IdleThread.h" +#include "lib/util/System.h" void SmartPointerDemo::run() { - Kernel::kout.lock(); - Kernel::kout.clear(); + Util::System::out.lock(); + Util::System::out.clear(); - Kernel::kout << "Output is written to log to be able to trace memory allocations/deallocations" << endl; + Util::System::out << "Output is written to log to be able to trace memory allocations/deallocations" << endl; { log.info() << "Allocating new unique_ptr..." << endl; @@ -76,7 +77,7 @@ void SmartPointerDemo::run() { // NOTE: This wasn't working because of a missing operator[] delete in the allocator log.info() << "Allocating unique_ptr*..." << endl; - Memory::unique_ptr* ptrptr = new Memory::unique_ptr[10]; + Memory::unique_ptr *ptrptr = new Memory::unique_ptr[10]; delete[] ptrptr; log.info() << "Should be deleted by now..." << endl; @@ -95,7 +96,7 @@ void SmartPointerDemo::run() { { log.info() << "Heapallocating Array, 10>..." << endl; - Container::Array, 10>* arr = new Container::Array, 10>; + Container::Array, 10> *arr = new Container::Array, 10>; log.info() << "Populating slot 0..." << endl; (*arr)[0] = Memory::make_unique(1); log.info() << "Moving slot 0 to slot 1..." << endl; @@ -117,6 +118,6 @@ void SmartPointerDemo::run() { } log.info() << "Should be deleted by now..." << endl; - Kernel::kout.unlock(); + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/demo/StringDemo.cc b/src/lib/demo/StringDemo.cc index 8270a02..f29cb3e 100644 --- a/src/lib/demo/StringDemo.cc +++ b/src/lib/demo/StringDemo.cc @@ -1,43 +1,44 @@ #include "StringDemo.h" +#include "lib/util/System.h" void StringDemo::run() { - Kernel::kout.lock(); - Kernel::kout.clear(); + Util::System::out.lock(); + Util::System::out.clear(); log.info() << "Allocating new string" << endl; String::string str1 = "This is a dynamically allocated string!"; - Kernel::kout << str1 << endl; + Util::System::out << str1 << endl; log.info() << "Reassign string" << endl; str1 = "Hello"; - Kernel::kout << str1 << " has length " << dec << str1.size() << endl; - Kernel::kout << "Again with strlen: Hello has length " << dec << String::strlen("Hello") << endl; + Util::System::out << str1 << " has length " << dec << str1.size() << endl; + Util::System::out << "Again with strlen: Hello has length " << dec << String::strlen("Hello") << endl; - Kernel::kout << "Adding strings: " << str1 << " + World" << endl; + Util::System::out << "Adding strings: " << str1 << " + World" << endl; log.info() << "Adding strings" << endl; str1 = str1 + " World"; - Kernel::kout << str1 << endl; + Util::System::out << str1 << endl; - Kernel::kout << "Hello += World" << endl; + Util::System::out << "Hello += World" << endl; log.info() << "Hello += World" << endl; String::string str3 = "Hello"; str3 += " World"; - Kernel::kout << str3 << endl; + Util::System::out << str3 << endl; - Kernel::kout << "Hello World *= 3" << endl; + Util::System::out << "Hello World *= 3" << endl; str3 *= 3; - Kernel::kout << str3 << endl; + Util::System::out << str3 << endl; - Kernel::kout << "String iterator!" << endl; - for (const char c : str1) { - Kernel::kout << c << " "; + Util::System::out << "String iterator!" << endl; + for (const char c: str1) { + Util::System::out << c << " "; } - Kernel::kout << endl; + Util::System::out << endl; log.info() << "Allocating new string" << endl; String::string str2 = "Hello World"; - Kernel::kout << "str1 == str2: " << static_cast(str1 == str2) << endl; - Kernel::kout << "strcmp(Hello, Hello): " << String::strcmp("Hello", "Hello") << endl; + Util::System::out << "str1 == str2: " << static_cast(str1 == str2) << endl; + Util::System::out << "strcmp(Hello, Hello): " << String::strcmp("Hello", "Hello") << endl; log.info() << "Reassign str2" << endl; str2 = "Hello"; @@ -48,9 +49,11 @@ void StringDemo::run() { arr[2] = 'l'; arr[3] = 'l'; arr[4] = 'o'; - Kernel::kout << "bse::array to bse::string: " << static_cast(arr) << ", size: " << (String::string(arr)).size() << endl; - Kernel::kout << "(bse::string)arr (" << static_cast(arr) << ") == str2 (" << str2 << "): " << static_cast(String::string(arr) == str2) << endl; + Util::System::out << "bse::array to bse::string: " << static_cast(arr) << ", size: " + << (String::string(arr)).size() << endl; + Util::System::out << "(bse::string)arr (" << static_cast(arr) << ") == str2 (" << str2 << "): " + << static_cast(String::string(arr) == str2) << endl; - Kernel::kout.unlock(); + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/demo/TextDemo.cc b/src/lib/demo/TextDemo.cc index 7292467..4db9185 100755 --- a/src/lib/demo/TextDemo.cc +++ b/src/lib/demo/TextDemo.cc @@ -9,35 +9,36 @@ *****************************************************************************/ #include "TextDemo.h" +#include "lib/util/System.h" void TextDemo::run() { /* Hier muess Code eingefuegt werden */ - Kernel::kout.lock(); - Kernel::kout.clear(); - Kernel::kout << "TextDemo\n" - << endl; + Util::System::out.lock(); + Util::System::out.clear(); + Util::System::out << "TextDemo\n" + << endl; - Kernel::kout << "Attribut (GREEN on WHITE): " - << bgc(Device::CGA::WHITE) << green << "GREEN on WHITE" << endl - << "Attribut (WHITE on BLACK): " - << bgc(Device::CGA::BLACK) << white << "WHITE on BLACK" << endl; - Kernel::kout << endl; + Util::System::out << "Attribut (GREEN on WHITE): " + << bgc(Device::CGA::WHITE) << green << "GREEN on WHITE" << endl + << "Attribut (WHITE on BLACK): " + << bgc(Device::CGA::BLACK) << white << "WHITE on BLACK" << endl; + Util::System::out << endl; - Kernel::kout << "Test der Zahlenausgabefunktion:" << endl - << "| dec | hex | bin |" << endl - << "+-------+-------+-------+" << endl; + Util::System::out << "Test der Zahlenausgabefunktion:" << endl + << "| dec | hex | bin |" << endl + << "+-------+-------+-------+" << endl; for (uint16_t num = 0; num < 17; ++num) { - Kernel::kout << fillw(0) << "| " << fillw(6) << dec << num - << fillw(0) << "| " << fillw(6) << hex << num - << fillw(0) << "| " << fillw(6) << bin << num - << fillw(0) << "|" << endl; + Util::System::out << fillw(0) << "| " << fillw(6) << dec << num + << fillw(0) << "| " << fillw(6) << hex << num + << fillw(0) << "| " << fillw(6) << bin << num + << fillw(0) << "|" << endl; } - Kernel::kout << endl; + Util::System::out << endl; - Kernel::kout.unlock(); + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/demo/VectorDemo.cc b/src/lib/demo/VectorDemo.cc index f897601..007c109 100644 --- a/src/lib/demo/VectorDemo.cc +++ b/src/lib/demo/VectorDemo.cc @@ -1,8 +1,9 @@ #include "VectorDemo.h" +#include "lib/util/System.h" -void print(OutStream& os, const Container::Vector& list) { +void print(OutStream &os, const Container::Vector &list) { os << "Printing List: "; - for (const int i : list) { + for (const int i: list) { os << i << " "; } os << endl; @@ -11,9 +12,9 @@ void print(OutStream& os, const Container::Vector& list) { void VectorDemo::run() { Container::Vector list; - Kernel::kout.lock(); - Kernel::kout.clear(); - Kernel::kout << "Logs are written to serial to see the memory interactions" << endl; + Util::System::out.lock(); + Util::System::out.clear(); + Util::System::out << "Logs are written to serial to see the memory interactions" << endl; log.info() << "Initial list size: " << dec << list.size() << endl; @@ -101,10 +102,10 @@ void VectorDemo::run() { // ============================================================ log.info() << "Range based for support" << endl; - for (int i : list) { + for (int i: list) { log.info() << "List contains element: " << dec << i << endl; } - Kernel::kout.unlock(); + Util::System::out.unlock(); Kernel::scheduler.exit(); } diff --git a/src/lib/util/System.cpp b/src/lib/util/System.cpp new file mode 100644 index 0000000..f6b308c --- /dev/null +++ b/src/lib/util/System.cpp @@ -0,0 +1,7 @@ +#include "System.h" + +namespace Util { + +CGA_Stream System::out; + +} diff --git a/src/lib/util/System.h b/src/lib/util/System.h new file mode 100644 index 0000000..63ff14f --- /dev/null +++ b/src/lib/util/System.h @@ -0,0 +1,18 @@ +#ifndef CHURLOS_LIB_SYSTEM_H +#define CHURLOS_LIB_SYSTEM_H + +#include "lib/stream/CGA_Stream.h" + +namespace Util { + +class System { +public: + // TODO: Something like "CGA_Stream" shouldn't exists, the stream should not be coupled to the output device + // TODO: There should be an "in" and "error" stream + static CGA_Stream out; + +}; + +} + +#endif //CHURLOS_LIB_SYSTEM_H