1

remove <cstddef> and NULLs (use nullptr)

This commit is contained in:
2022-07-24 00:40:51 +02:00
parent e4469e51b2
commit d9b86355c7
10 changed files with 41 additions and 54 deletions

View File

@ -53,11 +53,11 @@ Allocator::Allocator() : heap_start(HEAP_START), heap_end(HEAP_START + HEAP_SIZE
* Nachfolgend sind die Operatoren von C++, die wir hier ueberschreiben * * Nachfolgend sind die Operatoren von C++, die wir hier ueberschreiben *
* und entsprechend 'mm_alloc' und 'mm_free' aufrufen. * * und entsprechend 'mm_alloc' und 'mm_free' aufrufen. *
*****************************************************************************/ *****************************************************************************/
void* operator new(size_t size) { void* operator new(std::size_t size) {
return allocator.alloc(size); return allocator.alloc(size);
} }
void* operator new[](size_t count) { void* operator new[](std::size_t count) {
return allocator.alloc(count); return allocator.alloc(count);
} }

View File

@ -10,8 +10,6 @@
*****************************************************************************/ *****************************************************************************/
#include "kernel/Globals.h" #include "kernel/Globals.h"
#include <stddef.h>
// #include "kernel/allocator/LinkedListAllocator.h"
/***************************************************************************** /*****************************************************************************
* Methode: BumpAllocator::init * * Methode: BumpAllocator::init *
@ -55,7 +53,7 @@ void* BumpAllocator::alloc(unsigned int req_size) {
if (req_size + (unsigned int)this->next > this->heap_end) { if (req_size + (unsigned int)this->next > this->heap_end) {
log.error() << " - More memory requested than available :(" << endl; log.error() << " - More memory requested than available :(" << endl;
return NULL; return nullptr;
} }
void* allocated = this->next; void* allocated = this->next;

View File

@ -12,7 +12,6 @@
#include "kernel/allocator/LinkedListAllocator.h" #include "kernel/allocator/LinkedListAllocator.h"
#include "kernel/Globals.h" #include "kernel/Globals.h"
#include "user/lib/Logger.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 // 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 // Then I can easily merge adjacent free blocks by finding the previous block without looking at
@ -52,7 +51,7 @@ void LinkedListAllocator::dump_free_memory() {
kout << "Freier Speicher:" << endl; kout << "Freier Speicher:" << endl;
if (this->free_start == NULL) { if (this->free_start == nullptr) {
kout << " - No free Blocks" << endl; kout << " - No free Blocks" << endl;
} else { } else {
kout << " - Freelist start: " << hex << (unsigned int)this->free_start << endl; kout << " - Freelist start: " << hex << (unsigned int)this->free_start << endl;
@ -79,10 +78,10 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
log.debug() << "Requested " << hex << req_size << " Bytes" << endl; log.debug() << "Requested " << hex << req_size << " Bytes" << endl;
if (this->free_start == NULL) { if (this->free_start == nullptr) {
log.error() << " - No free memory remaining :(" << endl; log.error() << " - No free memory remaining :(" << endl;
this->lock.release(); this->lock.release();
return NULL; return nullptr;
} }
// Round to word borders // Round to word borders
@ -130,7 +129,7 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
if (this->free_start == current) { if (this->free_start == current) {
// No free block remaining // No free block remaining
log.trace() << " - Disabled freelist" << endl; log.trace() << " - Disabled freelist" << endl;
this->free_start = NULL; this->free_start = nullptr;
} }
log.trace() << " - Allocated " << hex << current->size << " Bytes without cutting" << endl; log.trace() << " - Allocated " << hex << current->size << " Bytes without cutting" << endl;
@ -166,7 +165,7 @@ void* LinkedListAllocator::alloc(unsigned int req_size) {
log.error() << " - More memory requested than available :(" << endl; log.error() << " - More memory requested than available :(" << endl;
this->lock.release(); this->lock.release();
return NULL; return nullptr;
} }
/***************************************************************************** /*****************************************************************************
@ -192,7 +191,7 @@ void LinkedListAllocator::free(void* ptr) {
// Reenable the freelist if no block was available // Reenable the freelist if no block was available
// This also means that no merging can be done // This also means that no merging can be done
if (this->free_start == NULL) { if (this->free_start == nullptr) {
this->free_start = block_start; this->free_start = block_start;
block_start->allocated = false; block_start->allocated = false;
block_start->next = block_start; block_start->next = block_start;
@ -296,7 +295,6 @@ void LinkedListAllocator::free(void* ptr) {
this->lock.release(); this->lock.release();
} }
// NOTE: I added this
free_block_t* LinkedListAllocator::find_previous_block(free_block_t* next_block) { free_block_t* LinkedListAllocator::find_previous_block(free_block_t* next_block) {
// Durchlaufe die ganze freispeicherliste bis zum Block der auf next_block zeigt // Durchlaufe die ganze freispeicherliste bis zum Block der auf next_block zeigt
free_block_t* current = next_block; free_block_t* current = next_block;

View File

@ -32,7 +32,7 @@ typedef struct free_block {
class LinkedListAllocator : Allocator { class LinkedListAllocator : Allocator {
private: private:
// freie Bloecke werden verkettet // freie Bloecke werden verkettet
struct free_block* free_start; struct free_block* free_start = nullptr;
LinkedListAllocator(Allocator& copy) = delete; // Verhindere Kopieren LinkedListAllocator(Allocator& copy) = delete; // Verhindere Kopieren

View File

@ -1,15 +1,12 @@
#include "kernel/allocator/TreeAllocator.h" #include "kernel/allocator/TreeAllocator.h"
#include "kernel/Globals.h" #include "kernel/Globals.h"
#include <stddef.h>
// NOTE: I added this file
void TreeAllocator::init() { void TreeAllocator::init() {
this->free_start = (tree_block_t*)this->heap_start; this->free_start = (tree_block_t*)this->heap_start;
this->free_start->allocated = false; this->free_start->allocated = false;
this->free_start->left = NULL; this->free_start->left = nullptr;
this->free_start->right = NULL; this->free_start->right = nullptr;
this->free_start->parent = NULL; this->free_start->parent = nullptr;
this->free_start->red = false; // The root is always black this->free_start->red = false; // The root is always black
this->free_start->next = (list_block_t*)this->free_start; this->free_start->next = (list_block_t*)this->free_start;
this->free_start->previous = (list_block_t*)this->free_start; this->free_start->previous = (list_block_t*)this->free_start;
@ -47,14 +44,14 @@ void* TreeAllocator::alloc(unsigned int req_size) {
// Finds smallest block that is large enough // Finds smallest block that is large enough
tree_block_t* best_fit = this->rbt_search_bestfit(rreq_size); tree_block_t* best_fit = this->rbt_search_bestfit(rreq_size);
if (best_fit == NULL) { if (best_fit == nullptr) {
log.error() << " - No block found" << endl; log.error() << " - No block found" << endl;
return NULL; return nullptr;
} }
if (best_fit->allocated) { if (best_fit->allocated) {
// Something went really wrong // Something went really wrong
log.error() << " - Block already allocated :(" << endl; log.error() << " - Block already allocated :(" << endl;
return NULL; return nullptr;
} }
best_fit->allocated = true; best_fit->allocated = true;
unsigned int size = this->get_size(best_fit); unsigned int size = this->get_size(best_fit);

View File

@ -4,8 +4,7 @@
#include "kernel/Allocator.h" #include "kernel/Allocator.h"
#include "user/lib/Logger.h" #include "user/lib/Logger.h"
// NOTE: I added this file // I can't imagine that this is fast with all the tree logic?
// NOTE: I can't imagine that this is very fast with all the tree logic?
typedef struct list_block { typedef struct list_block {
// Doubly linked list for every block // Doubly linked list for every block

View File

@ -1,13 +1,11 @@
#include "kernel/allocator/TreeAllocator.h" #include "kernel/allocator/TreeAllocator.h"
#include "kernel/Globals.h" #include "kernel/Globals.h"
#include <stddef.h>
// NOTE: I added this file // RBT code taken from https://github.com/Bibeknam/algorithmtutorprograms
// NOTE: RBT code taken from https://github.com/Bibeknam/algorithmtutorprograms
// START copy from algorithmtutorprograms // START copy from algorithmtutorprograms
void TreeAllocator::rbt_transplant(tree_block_t* a, tree_block_t* b) { void TreeAllocator::rbt_transplant(tree_block_t* a, tree_block_t* b) {
if (a->parent == NULL) { if (a->parent == nullptr) {
this->free_start = b; this->free_start = b;
} else if (a == a->parent->left) { } else if (a == a->parent->left) {
a->parent->left = b; a->parent->left = b;
@ -21,15 +19,15 @@ void TreeAllocator::rbt_transplant(tree_block_t* a, tree_block_t* b) {
// and fix the tree // and fix the tree
void TreeAllocator::rbt_insert(tree_block_t* node) { void TreeAllocator::rbt_insert(tree_block_t* node) {
// Ordinary Binary Search Insertion // Ordinary Binary Search Insertion
node->parent = NULL; node->parent = nullptr;
node->left = NULL; node->left = nullptr;
node->right = NULL; node->right = nullptr;
node->red = true; // new node must be red node->red = true; // new node must be red
tree_block_t* y = NULL; tree_block_t* y = nullptr;
tree_block_t* x = this->free_start; tree_block_t* x = this->free_start;
while (x != NULL) { while (x != nullptr) {
y = x; y = x;
if (this->get_size(node) < this->get_size(x)) { if (this->get_size(node) < this->get_size(x)) {
x = x->left; x = x->left;
@ -40,7 +38,7 @@ void TreeAllocator::rbt_insert(tree_block_t* node) {
// y is parent of x // y is parent of x
node->parent = y; node->parent = y;
if (y == NULL) { if (y == nullptr) {
this->free_start = node; this->free_start = node;
} else if (this->get_size(node) < this->get_size(y)) { } else if (this->get_size(node) < this->get_size(y)) {
y->left = node; y->left = node;
@ -49,13 +47,13 @@ void TreeAllocator::rbt_insert(tree_block_t* node) {
} }
// if new node is a root node, simply return // if new node is a root node, simply return
if (node->parent == NULL) { if (node->parent == nullptr) {
node->red = false; node->red = false;
return; return;
} }
// if the grandparent is null, simply return // if the grandparent is null, simply return
if (node->parent->parent == NULL) { if (node->parent->parent == nullptr) {
return; return;
} }
@ -118,7 +116,7 @@ void TreeAllocator::rbt_fix_insert(tree_block_t* k) {
void TreeAllocator::rbt_rot_l(tree_block_t* x) { void TreeAllocator::rbt_rot_l(tree_block_t* x) {
tree_block_t* y = x->right; tree_block_t* y = x->right;
x->right = y->left; x->right = y->left;
if (y->left != NULL) { if (y->left != nullptr) {
y->left->parent = x; y->left->parent = x;
} }
y->parent = x->parent; y->parent = x->parent;
@ -137,7 +135,7 @@ void TreeAllocator::rbt_rot_l(tree_block_t* x) {
void TreeAllocator::rbt_rot_r(tree_block_t* x) { void TreeAllocator::rbt_rot_r(tree_block_t* x) {
tree_block_t* y = x->left; tree_block_t* y = x->left;
x->left = y->right; x->left = y->right;
if (y->right != NULL) { if (y->right != nullptr) {
y->right->parent = x; y->right->parent = x;
} }
y->parent = x->parent; y->parent = x->parent;
@ -154,7 +152,7 @@ void TreeAllocator::rbt_rot_r(tree_block_t* x) {
// find the node with the minimum key // find the node with the minimum key
tree_block_t* TreeAllocator::rbt_minimum(tree_block_t* node) { tree_block_t* TreeAllocator::rbt_minimum(tree_block_t* node) {
while (node->left != NULL) { while (node->left != nullptr) {
node = node->left; node = node->left;
} }
return node; return node;
@ -166,10 +164,10 @@ void TreeAllocator::rbt_remove(tree_block_t* z) {
y = z; y = z;
bool y_original_red = y->red; bool y_original_red = y->red;
if (z->left == NULL) { if (z->left == nullptr) {
x = z->right; x = z->right;
this->rbt_transplant(z, z->right); this->rbt_transplant(z, z->right);
} else if (z->right == NULL) { } else if (z->right == nullptr) {
x = z->left; x = z->left;
this->rbt_transplant(z, z->left); this->rbt_transplant(z, z->left);
} else { } else {
@ -264,14 +262,14 @@ void TreeAllocator::rbt_fix_remove(tree_block_t* x) {
} }
// END copy from algorithmtutorprograms // END copy from algorithmtutorprograms
// NOTE: This is recursive and depends on luck // This is recursive and depends on luck
tree_block_t* TreeAllocator::rbt_search_bestfit(tree_block_t* node, unsigned int req_size) { tree_block_t* TreeAllocator::rbt_search_bestfit(tree_block_t* node, unsigned int req_size) {
if (node == NULL) { if (node == nullptr) {
return NULL; return nullptr;
} }
if (req_size < this->get_size(node)) { if (req_size < this->get_size(node)) {
if (node->left != NULL && this->get_size(node->left) >= req_size) { if (node->left != nullptr && this->get_size(node->left) >= req_size) {
return this->rbt_search_bestfit(node->left, req_size); return this->rbt_search_bestfit(node->left, req_size);
} }
@ -279,12 +277,12 @@ tree_block_t* TreeAllocator::rbt_search_bestfit(tree_block_t* node, unsigned int
} }
if (req_size > this->get_size(node)) { if (req_size > this->get_size(node)) {
if (node->right != NULL && this->get_size(node->right) >= req_size) { if (node->right != nullptr && this->get_size(node->right) >= req_size) {
return this->rbt_search_bestfit(node->right, req_size); return this->rbt_search_bestfit(node->right, req_size);
} }
// Block doesn't fit // Block doesn't fit
return NULL; return nullptr;
} }
// Perfect fit // Perfect fit

View File

@ -11,7 +11,6 @@
*****************************************************************************/ *****************************************************************************/
#include "kernel/Globals.h" #include "kernel/Globals.h"
#include "kernel/Paging.h"
#include "user/MainMenu.h" #include "user/MainMenu.h"
void print_startup_message() { void print_startup_message() {
@ -90,7 +89,7 @@ int main() {
// DONE: Array wrapper // DONE: Array wrapper
// DONE: Rewrite Logging with a basic logger // DONE: Rewrite Logging with a basic logger
// DONE: Static Logger // DONE: Static Logger
// CANCELED: String wrapper // DONE: String wrapper
// DONE: Linked List // DONE: Linked List
// DONE: Iterator support for structures // DONE: Iterator support for structures
// DONE: Implement own basic managed pointers // DONE: Implement own basic managed pointers
@ -118,7 +117,7 @@ int main() {
// Or check if thread is still running // Or check if thread is still running
// TODO: Delete copy constructors that weren't already deleted // TODO: Delete copy constructors that weren't already deleted
// DONE: Switch out semaphore Queue with ArrayList? Or switch back Scheduler to Queue? // DONE: Switch out semaphore Queue with ArrayList? Or switch back Scheduler to Queue?
// TODO: Add virtual destructors and make sure to call them with delete when objects are removed // CANCELED: Add virtual destructors and make sure to call them with delete when objects are removed
// TODO: Replace empty constructors/destructors with default keyword // TODO: Replace empty constructors/destructors with default keyword
// DONE: Synchronize the outstream // DONE: Synchronize the outstream
// DONE: Remove Iterator from List.h // DONE: Remove Iterator from List.h

View File

@ -2,7 +2,6 @@
#define __ARRAY_INCLUDE_H #define __ARRAY_INCLUDE_H
#include "user/lib/Iterator.h" #include "user/lib/Iterator.h"
#include <cstddef>
#include <utility> #include <utility>
namespace bse { namespace bse {

View File

@ -2,8 +2,7 @@
#define __String_Include_H_ #define __String_Include_H_
#include "user/lib/Array.h" #include "user/lib/Array.h"
#include <cstddef> #include "user/lib/Iterator.h"
#include <user/lib/Iterator.h>
namespace bse { namespace bse {