1

changed a lot of small clang-tidy warnings

This commit is contained in:
churl
2022-05-23 17:06:11 +02:00
parent b78faebdc5
commit 7927220247
30 changed files with 158 additions and 190 deletions

View File

@ -25,7 +25,7 @@ void BumpAllocator::init() {
this->allocations = 0;
this->next = (unsigned char*)heap_start;
if constexpr (DEBUG) kout << "Initialized Bump Allocator" << endl;
if constexpr (DEBUG) { kout << "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;
if constexpr (DEBUG) { kout << "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;
if constexpr (DEBUG) { kout << " - 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;
if constexpr (DEBUG) { kout << " - 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;
if constexpr (DEBUG) { kout << " mm_free: ptr= " << hex << (unsigned int)ptr << ", not supported" << endl; }
}

View File

@ -20,15 +20,15 @@ private:
unsigned char* next;
unsigned int allocations;
BumpAllocator(Allocator& copy); // Verhindere Kopieren
BumpAllocator(Allocator& copy) = delete; // Verhindere Kopieren
public:
BumpAllocator() {}; // Allocator() called implicitely in C++
void init();
void dump_free_memory();
void* alloc(unsigned int req_size);
void free(void* ptr);
void init() override;
void dump_free_memory() override;
void* alloc(unsigned int req_size) override;
void free(void* ptr) override;
};
#endif

View File

@ -37,7 +37,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;
if constexpr (DEBUG) { kout << "Initialized LinkedList Allocator" << endl; }
}
/*****************************************************************************
@ -75,10 +75,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;
if constexpr (DEBUG) { kout << "Requested " << hex << req_size << " Bytes" << endl; }
if (this->free_start == NULL) {
if constexpr (DEBUG) kout << " - No free memory remaining :(" << endl;
if constexpr (DEBUG) { kout << " - No free memory remaining :(" << endl; }
return NULL;
}
@ -86,7 +86,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;
if constexpr (DEBUG) { kout << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl; }
}
free_block_t* current = this->free_start;
@ -117,7 +117,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;
if constexpr (DEBUG) { kout << " - Allocated " << hex << rreq_size << " Bytes with cutting" << endl; }
} else {
// Block too small to be cut, allocate whole block
@ -125,15 +125,15 @@ 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;
if constexpr (DEBUG) { kout << " - Disabled freelist" << endl; }
this->free_start = NULL;
}
if constexpr (DEBUG) kout << " - Allocated " << hex << current->size << " Bytes without cutting" << endl;
if constexpr (DEBUG) { kout << " - Allocated " << hex << current->size << " Bytes without cutting" << endl; }
}
// Block aushängen
free_block_t* previous = this->find_previous_block(current);
free_block_t* previous = LinkedListAllocator::find_previous_block(current);
previous->next = current->next;
current->allocated = true;
@ -146,7 +146,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;
if constexpr (DEBUG) { kout << " - More memory requested than available :(" << endl; }
return NULL;
}
@ -159,7 +159,7 @@ void LinkedListAllocator::free(void* ptr) {
/* Hier muess Code eingefuegt werden */
if constexpr (DEBUG) kout << "Freeing " << hex << (unsigned int)ptr << endl;
if constexpr (DEBUG) { kout << "Freeing " << hex << (unsigned int)ptr << endl; }
free_block_t* block_start = (free_block_t*)((unsigned int)ptr - sizeof(free_block_t));
@ -170,7 +170,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;
if constexpr (DEBUG) { kout << " - Enabling freelist with one block" << endl; }
return;
}
@ -184,7 +184,7 @@ void LinkedListAllocator::free(void* ptr) {
next_free = next_free->next;
}
free_block_t* previous_free = this->find_previous_block(next_free);
free_block_t* previous_free = LinkedListAllocator::find_previous_block(next_free);
free_block_t* previous_free_next =
(free_block_t*)((unsigned int)previous_free + sizeof(free_block_t) + previous_free->size);
@ -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;
if constexpr (DEBUG) { kout << " - 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;
if constexpr (DEBUG) { kout << " - 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;
if constexpr (DEBUG) { kout << " - 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;
if constexpr (DEBUG) { kout << " - Moving freelist start to " << hex << (unsigned int)previous_free << endl; }
this->free_start = previous_free;
}
}

View File

@ -14,10 +14,6 @@
#include "kernel/Allocator.h"
// TODO: Is it 8 or 4?
#define BASIC_ALIGN 8
#define HEAP_MIN_FREE_BLOCK_SIZE 64 // min. Groesse eines freien Blocks
// Format eines freien Blocks, 4 + 4 + 4 Byte
typedef struct free_block {
bool allocated; // NOTE: I added this to allow easier merging of free blocks:
@ -37,21 +33,21 @@ private:
// freie Bloecke werden verkettet
struct free_block* free_start;
LinkedListAllocator(Allocator& copy); // Verhindere Kopieren
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.
struct free_block* find_previous_block(struct free_block*);
static struct free_block* find_previous_block(struct free_block*);
public:
LinkedListAllocator() {};
LinkedListAllocator() {}
void init();
void dump_free_memory();
void* alloc(unsigned int req_size);
void free(void* ptr);
void init() override;
void dump_free_memory() override;
void* alloc(unsigned int req_size) override;
void free(void* ptr) override;
};
#endif

View File

@ -14,26 +14,22 @@ void TreeAllocator::init() {
this->free_start->next = (list_block_t*)this->free_start;
this->free_start->previous = (list_block_t*)this->free_start;
if constexpr (DEBUG) kout << "Initialized Tree Allocator" << endl;
if constexpr (DEBUG) { kout << "Initialized Tree Allocator" << endl; }
}
void TreeAllocator::dump_free_memory() {
kout << "Free Memory:" << endl;
this->dump_free_memory(this->free_start);
}
void TreeAllocator::dump_free_memory(tree_block_t* node) {
if (node == NULL) {
return;
}
this->dump_free_memory(node->left);
kout << " - Free Block at " << hex << (unsigned int)node << " (" << hex << this->get_size(node) << " Byte)" << endl;
this->dump_free_memory(node->right);
list_block_t* current = (list_block_t*)this->heap_start;
do {
if (!current->allocated) {
kout << " - Free Block at " << (unsigned int)current << ", Size: " << (unsigned int)current->next - (unsigned int)current << endl;
}
current = current->next;
} while ((unsigned int)current != this->heap_start);
}
void* TreeAllocator::alloc(unsigned int req_size) {
if constexpr (DEBUG) kout << "Requested " << dec << req_size << " Bytes" << endl;
if constexpr (DEBUG) { kout << "Requested " << dec << req_size << " Bytes" << endl; }
// Round to word borders + tree_block size
unsigned int rreq_size = req_size;
@ -41,35 +37,35 @@ 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);
if constexpr (DEBUG) kout << " - Increased block size for rbt metadata" << endl;
if constexpr (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) {
if constexpr (DEBUG) kout << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl;
if constexpr (DEBUG) { kout << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl; }
}
// Finds smallest block that is large enough
tree_block_t* best_fit = this->rbt_search_bestfit(rreq_size);
if (best_fit == NULL) {
if constexpr (DEBUG) kout << " - No block found" << endl;
if constexpr (DEBUG) { kout << " - No block found" << endl; }
return NULL;
}
if (best_fit->allocated) {
// Something went really wrong
if constexpr (DEBUG) kout << " - Block already allocated :(" << endl;
if constexpr (DEBUG) { kout << " - Block already allocated :(" << endl; }
return NULL;
}
best_fit->allocated = true;
unsigned int size = this->get_size(best_fit);
if constexpr (DEBUG) kout << " - Found best-fit: " << hex << (unsigned int)best_fit << endl;
if constexpr (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
if constexpr (DEBUG) kout << " - Allocating " << dec << rreq_size << " Bytes with cutting" << endl;
if constexpr (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,15 +76,15 @@ 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
if constexpr (DEBUG) kout << " - Allocating " << dec << rreq_size << " Bytes without cutting" << endl;
if constexpr (DEBUG) { kout << " - Allocating " << dec << rreq_size << " Bytes without cutting" << endl; }
}
if constexpr (DEBUG) kout << " - Returned address " << hex << (unsigned int)((char*)best_fit + sizeof(list_block_t)) << endl;
if constexpr (DEBUG) { kout << " - Returned address " << hex << (unsigned int)((char*)best_fit + sizeof(list_block_t)) << endl; }
return (void*)((char*)best_fit + sizeof(list_block_t));
}
void TreeAllocator::free(void* ptr) {
if constexpr (DEBUG) kout << "Freeing " << hex << (unsigned int)ptr << endl;
if constexpr (DEBUG) { kout << "Freeing " << hex << (unsigned int)ptr << endl; }
list_block_t* block = (list_block_t*)((char*)ptr - sizeof(list_block_t));
if (!block->allocated) {
@ -107,7 +103,7 @@ void TreeAllocator::free(void* ptr) {
if (!next->allocated) {
// Merge forward
if constexpr (DEBUG) kout << " - Merging forward" << endl;
if constexpr (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,7 +116,7 @@ void TreeAllocator::free(void* ptr) {
if (!previous->allocated) {
// Merge backward
if constexpr (DEBUG) kout << " - Merging backward" << endl;
if constexpr (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

View File

@ -3,9 +3,6 @@
#include "kernel/Allocator.h"
#define HEAP_MIN_FREE_BLOCK_SIZE 64 // min. Groesse eines freien Blocks
#define BASIC_ALIGN 8
// NOTE: I added this file
// NOTE: I can't imagine that this is very fast with all the tree logic?
@ -40,7 +37,7 @@ private:
// Root of the rbt
tree_block_t* free_start;
TreeAllocator(Allocator& copy); // Verhindere Kopieren
TreeAllocator(Allocator& copy) = delete; // Verhindere Kopieren
// Returns the size of the usable memory of a block
unsigned int get_size(list_block_t* block) const;
@ -50,7 +47,7 @@ private:
// NOTE: Would be nice to have this stuff somewhere else for general use,
// but that would require different rbt_node/dll_node structures.
// If I need this again later I should look into it.
// If I need this again later I should move it.
void rbt_rot_l(tree_block_t* x);
void rbt_rot_r(tree_block_t* x);
void rbt_transplant(tree_block_t* a, tree_block_t* b);
@ -71,10 +68,10 @@ private:
public:
TreeAllocator() {};
void init();
void dump_free_memory();
void* alloc(unsigned int req_size);
void free(void* ptr);
void init() override;
void dump_free_memory() override;
void* alloc(unsigned int req_size) override;
void free(void* ptr) override;
};
#endif

View File

@ -197,7 +197,7 @@ void TreeAllocator::rbt_remove(tree_block_t* z) {
// fix the rb tree modified by the delete operation
void TreeAllocator::rbt_fix_remove(tree_block_t* x) {
tree_block_t* s;
while (x != this->free_start && x->red == false) {
while (x != this->free_start && !x->red) {
if (x == x->parent->left) {
s = x->parent->right;
if (s->red) {
@ -238,7 +238,7 @@ void TreeAllocator::rbt_fix_remove(tree_block_t* x) {
s = x->parent->left;
}
if (!s->right->red && !s->right->red) {
if (!s->right->red) {
// case 3.2
s->red = true;
x = x->parent;
@ -264,6 +264,7 @@ void TreeAllocator::rbt_fix_remove(tree_block_t* x) {
}
// END copy from algorithmtutorprograms
// NOTE: This is recursive and depends on luck
tree_block_t* TreeAllocator::rbt_search_bestfit(tree_block_t* node, unsigned int req_size) {
if (node == NULL) {
return NULL;
@ -275,7 +276,9 @@ tree_block_t* TreeAllocator::rbt_search_bestfit(tree_block_t* node, unsigned int
}
return node;
} else 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) {
return this->rbt_search_bestfit(node->right, req_size);
}