From f57b8fef27b251aee4bce908e9f2e06d41511c21 Mon Sep 17 00:00:00 2001 From: churl Date: Mon, 9 May 2022 16:45:01 +0200 Subject: [PATCH] implement rounding to word borders when allocating --- c_os/kernel/allocator/LinkedListAllocator.cc | 19 +++++++++++++------ c_os/user/HeapDemo.cc | 7 +++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/c_os/kernel/allocator/LinkedListAllocator.cc b/c_os/kernel/allocator/LinkedListAllocator.cc index dca0d92..0d3e2e1 100755 --- a/c_os/kernel/allocator/LinkedListAllocator.cc +++ b/c_os/kernel/allocator/LinkedListAllocator.cc @@ -88,14 +88,21 @@ void* LinkedListAllocator::alloc(unsigned int req_size) { return NULL; } + // Round to word borders + unsigned int req_size_diff = (4 - req_size % 4) % 4; + unsigned int rreq_size = req_size + req_size_diff; + if (req_size_diff > 0) { + kout << " - Rounded to word border (+" << dec << req_size_diff << " bytes)" << endl; + } + struct free_block* current = this->free_start; do { - if (current->size >= req_size) { + if (current->size >= rreq_size) { // Current block large enough // We now have: [<> | current | <>] // Don't subtract to prevent underflow - if (current->size >= req_size + sizeof(struct free_block) + HEAP_MIN_FREE_BLOCK_SIZE) { + if (current->size >= rreq_size + sizeof(struct free_block) + HEAP_MIN_FREE_BLOCK_SIZE) { // Block so large it can be cut // Create new header after allocated memory and rearrange pointers @@ -103,20 +110,20 @@ void* LinkedListAllocator::alloc(unsigned int req_size) { // In case of only one freeblock: // [current | new_next] struct free_block* new_next = - (struct free_block*)((unsigned int)current + sizeof(struct free_block) + req_size); + (struct free_block*)((unsigned int)current + sizeof(struct free_block) + rreq_size); // If only one block exists, current->next is current // This shouldn't be a problem since the block gets removed from the list later new_next->next = current->next; - new_next->size = current->size - (req_size + sizeof(struct free_block)); + new_next->size = current->size - (rreq_size + sizeof(struct free_block)); current->next = new_next; // We want to reach the next free block from the allocated block - current->size = req_size; + current->size = rreq_size; // Next-fit this->free_start = new_next; - kout << " - Allocated " << hex << req_size << " Bytes with cutting" << endl; + kout << " - Allocated " << hex << rreq_size << " Bytes with cutting" << endl; } else { // Block too small to be cut, allocate whole block diff --git a/c_os/user/HeapDemo.cc b/c_os/user/HeapDemo.cc index 94cb778..ce9561e 100755 --- a/c_os/user/HeapDemo.cc +++ b/c_os/user/HeapDemo.cc @@ -30,6 +30,13 @@ void heap_demo() { /* hier muss Code eingefuegt werden */ allocator.dump_free_memory(); + // Rounding to word border + kout << "ROUNDING ====================================================================" << endl; + void* alloc = allocator.alloc(1); + allocator.dump_free_memory(); + allocator.free(alloc); + allocator.dump_free_memory(); + // Some objects and forward/backward merging kout << "SOME OBJECTS ================================================================" << endl; MyObj* a = new MyObj(5);