From 8d9befa820b6f8c7c0c7a63de6a645f89aa56a7b Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Wed, 18 Dec 2013 22:27:07 +0100 Subject: [PATCH] Set the blocks in the free list as free --- kernel/src/memory.cpp | 39 ++++++++++++++++++++++++++++++++++----- kernel/src/shell.cpp | 2 +- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/kernel/src/memory.cpp b/kernel/src/memory.cpp index e6265d38..e6f75714 100644 --- a/kernel/src/memory.cpp +++ b/kernel/src/memory.cpp @@ -20,11 +20,13 @@ const bool TRACE_MALLOC = false; uint64_t _used_memory; uint64_t _allocated_memory; -struct malloc_header_chunk { +class malloc_header_chunk { +private: size_t __size; malloc_header_chunk* __next; malloc_header_chunk* __prev; +public: size_t& size(){ return __size; } @@ -134,14 +136,14 @@ template void debug_malloc(const char* point = nullptr){ if(Debug){ if(point){ - k_print_line(point); + k_print(point); } auto it = malloc_head; - k_print("next: "); + k_print(" next: "); do { - k_printf("%h -> ", reinterpret_cast(it)); + k_printf("%d%h -> ", static_cast(it->is_free()), reinterpret_cast(it)); it = it->next(); } while(it != malloc_head); @@ -176,6 +178,7 @@ void init_memory_manager(){ header->size() = MIN_BLOCKS * BLOCK_SIZE - META_SIZE; header->next_ref() = malloc_head; header->prev_ref() = malloc_head; + header->set_free(); auto footer = reinterpret_cast( reinterpret_cast(block) + header->size() + sizeof(malloc_header_chunk)); @@ -183,6 +186,8 @@ void init_memory_manager(){ malloc_head->next_ref() = header; malloc_head->prev_ref() = header; + + malloc_head->set_free(); } void* k_malloc(uint64_t bytes){ @@ -204,10 +209,14 @@ void* k_malloc(uint64_t bytes){ header->next_ref() = current->next(); header->prev_ref() = current; + header->set_free(); current->next()->prev_ref() = header; current->next_ref() = header; + //Because the value gets erased just before + current->next()->set_free(); + auto footer = reinterpret_cast( reinterpret_cast(block) + header->size() + sizeof(malloc_header_chunk)); footer->size() = header->size(); @@ -237,6 +246,9 @@ void* k_malloc(uint64_t bytes){ current->prev()->next_ref() = new_block; current->next()->prev_ref() = new_block; + new_block->set_free(); + current->next()->set_free(); + auto new_footer = reinterpret_cast( reinterpret_cast(new_block) + new_block_size + sizeof(malloc_header_chunk)); new_footer->size() = new_block_size; @@ -249,6 +261,8 @@ void* k_malloc(uint64_t bytes){ current->prev()->next_ref() = current->next(); current->next()->prev_ref() = current->prev(); + current->next()->set_free(); + debug_malloc("after malloc no split"); break; @@ -294,6 +308,13 @@ void k_free(void* block){ header->next()->prev_ref() = free_header; header->next_ref() = free_header; + //Mark the freed block as free + free_header->set_free(); + + //The header free value is erased before + header->next()->set_free(); + free_header->next()->set_free(); + debug_malloc("after free"); } @@ -311,17 +332,25 @@ uint64_t free_memory(){ void memory_debug(){ size_t memory_free = 0; + size_t non_free_blocks = 0; auto it = malloc_head; k_printf("malloc overhead: %d\n", META_SIZE); k_print("Free blocks:"); do { + if(!it->is_free()){ + ++non_free_blocks; + k_print("NF"); + } + k_printf("b(%d) ", it->size()); memory_free += it->size(); + it = it->next(); } while(it != malloc_head); k_print_line(); - k_printf("memory free in malloc chain: %m (%d)", memory_free, memory_free); + k_printf("memory free in malloc chain: %m (%d)\n", memory_free, memory_free); + k_printf("There are %d non free blocks in the free list\n", non_free_blocks); } diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp index 2da89980..a8714a7c 100644 --- a/kernel/src/shell.cpp +++ b/kernel/src/shell.cpp @@ -589,7 +589,7 @@ void shutdown_command(const vector&){ } //end of anonymous namespace void init_shell(){ - wipeout(); + //wipeout(); k_print("thor> ");