From 4d2dd9bce2644324f2ed759cf9ab2525d0bfe1cc Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Thu, 23 Jan 2014 20:43:55 +0100 Subject: [PATCH] Cleanup malloc --- kernel/include/{memory.hpp => malloc.hpp} | 12 +- kernel/src/ata.cpp | 2 +- kernel/src/kernel.cpp | 4 +- kernel/src/{memory.cpp => malloc.cpp} | 140 +++++++++++----------- kernel/src/shell.cpp | 16 +-- kernel/src/thor.cpp | 10 +- 6 files changed, 94 insertions(+), 90 deletions(-) rename kernel/include/{memory.hpp => malloc.hpp} (87%) rename kernel/src/{memory.cpp => malloc.cpp} (97%) diff --git a/kernel/include/memory.hpp b/kernel/include/malloc.hpp similarity index 87% rename from kernel/include/memory.hpp rename to kernel/include/malloc.hpp index 1a8f7d79..fb9c8797 100644 --- a/kernel/include/memory.hpp +++ b/kernel/include/malloc.hpp @@ -5,12 +5,14 @@ // http://www.boost.org/LICENSE_1_0.txt) //======================================================================= -#ifndef MEMORY_H -#define MEMORY_H +#ifndef MALLOC_H +#define MALLOC_H #include "stl/types.hpp" -void init_memory_manager(); +namespace malloc { + +void init(); void* k_malloc(uint64_t bytes); void k_free(void* block); @@ -24,6 +26,8 @@ uint64_t allocated_memory(); uint64_t used_memory(); uint64_t free_memory(); -void malloc_debug(); +void debug(); + +} #endif diff --git a/kernel/src/ata.cpp b/kernel/src/ata.cpp index bf16c5b5..9fb8c3f6 100644 --- a/kernel/src/ata.cpp +++ b/kernel/src/ata.cpp @@ -8,7 +8,7 @@ #include "ata.hpp" #include "kernel_utils.hpp" #include "timer.hpp" -#include "memory.hpp" +#include "malloc.hpp" #include "thor.hpp" #include "interrupts.hpp" #include "console.hpp" diff --git a/kernel/src/kernel.cpp b/kernel/src/kernel.cpp index d9b2ca12..a902fdb4 100644 --- a/kernel/src/kernel.cpp +++ b/kernel/src/kernel.cpp @@ -9,7 +9,7 @@ #include "physical_allocator.hpp" #include "virtual_allocator.hpp" #include "paging.hpp" -#include "memory.hpp" +#include "malloc.hpp" #include "timer.hpp" #include "shell.hpp" #include "keyboard.hpp" @@ -39,7 +39,7 @@ void kernel_main(){ virtual_allocator::init(); physical_allocator::init(); - init_memory_manager(); + malloc::init(); //Install drivers install_timer(); diff --git a/kernel/src/memory.cpp b/kernel/src/malloc.cpp similarity index 97% rename from kernel/src/memory.cpp rename to kernel/src/malloc.cpp index 0c4926c9..9c1fa062 100644 --- a/kernel/src/memory.cpp +++ b/kernel/src/malloc.cpp @@ -5,7 +5,7 @@ // http://www.boost.org/LICENSE_1_0.txt) //======================================================================= -#include "memory.hpp" +#include "malloc.hpp" #include "console.hpp" #include "physical_allocator.hpp" #include "paging.hpp" @@ -58,7 +58,7 @@ public: __prev = reinterpret_cast(reinterpret_cast(__prev) & (~0l - 1)); } - bool is_free(){ + bool is_free() const { return reinterpret_cast(__prev) & 0x1; } @@ -215,9 +215,70 @@ void expand_heap(malloc_header_chunk* current){ insert_after(current, header); } +malloc_header_chunk* left_block(malloc_header_chunk* b){ + auto left_footer = reinterpret_cast( + reinterpret_cast(b) - sizeof(malloc_footer_chunk)); + + if(reinterpret_cast(left_footer)>= min_address){ + auto left_size = left_footer->size(); + + auto left_header = reinterpret_cast( + reinterpret_cast(left_footer) - left_size - sizeof(malloc_header_chunk)); + + if(reinterpret_cast(left_header) >= min_address){ + return left_header; + } + } + + return nullptr; +} + +malloc_header_chunk* right_block(malloc_header_chunk* b){ + auto right_header = reinterpret_cast( + reinterpret_cast(b) + META_SIZE + b->size()); + + if(reinterpret_cast(right_header) < max_address){ + auto right_footer = right_header->footer(); + if(reinterpret_cast(right_footer) < max_address){ + return right_header; + } + } + + return nullptr; +} + +malloc_header_chunk* coalesce(malloc_header_chunk* b){ + auto a = left_block(b); + auto c = right_block(b); + + if(a && a->is_free()){ + auto new_size = a->size() + b->size() + META_SIZE; + + //Remove a from the free list + remove(a); + + b = a; + + b->size() = new_size; + b->footer()->size() = new_size; + } + + if(c && c->is_free()){ + auto new_size = b->size() + c->size() + META_SIZE; + + //Remove c from the free list + remove(c); + + b->size() = new_size; + b->footer()->size() = new_size; + } + + return b; +} + } //end of anonymous namespace -void init_memory_manager(){ +void malloc::init(){ //Init the fake head init_head(); @@ -225,7 +286,7 @@ void init_memory_manager(){ expand_heap(malloc_head); } -void* k_malloc(uint64_t bytes){ +void* malloc::k_malloc(uint64_t bytes){ auto current = malloc_head->next(); //Try not to create too small blocks @@ -297,68 +358,7 @@ void* k_malloc(uint64_t bytes){ return b; } -malloc_header_chunk* left_block(malloc_header_chunk* b){ - auto left_footer = reinterpret_cast( - reinterpret_cast(b) - sizeof(malloc_footer_chunk)); - - if(reinterpret_cast(left_footer)>= min_address){ - auto left_size = left_footer->size(); - - auto left_header = reinterpret_cast( - reinterpret_cast(left_footer) - left_size - sizeof(malloc_header_chunk)); - - if(reinterpret_cast(left_header) >= min_address){ - return left_header; - } - } - - return nullptr; -} - -malloc_header_chunk* right_block(malloc_header_chunk* b){ - auto right_header = reinterpret_cast( - reinterpret_cast(b) + META_SIZE + b->size()); - - if(reinterpret_cast(right_header) < max_address){ - auto right_footer = right_header->footer(); - if(reinterpret_cast(right_footer) < max_address){ - return right_header; - } - } - - return nullptr; -} - -malloc_header_chunk* coalesce(malloc_header_chunk* b){ - auto a = left_block(b); - auto c = right_block(b); - - if(a && a->is_free()){ - auto new_size = a->size() + b->size() + META_SIZE; - - //Remove a from the free list - remove(a); - - b = a; - - b->size() = new_size; - b->footer()->size() = new_size; - } - - if(c && c->is_free()){ - auto new_size = b->size() + c->size() + META_SIZE; - - //Remove c from the free list - remove(c); - - b->size() = new_size; - b->footer()->size() = new_size; - } - - return b; -} - -void k_free(void* block){ +void malloc::k_free(void* block){ auto free_header = reinterpret_cast( reinterpret_cast(block) - sizeof(malloc_header_chunk)); @@ -382,15 +382,15 @@ void k_free(void* block){ debug_malloc("after free"); } -size_t allocated_memory(){ +size_t malloc::allocated_memory(){ return _allocated_memory; } -size_t used_memory(){ +size_t malloc::used_memory(){ return _used_memory; } -size_t free_memory(){ +size_t malloc::free_memory(){ size_t memory_free = 0; auto it = malloc_head; @@ -403,7 +403,7 @@ size_t free_memory(){ return memory_free; } -void malloc_debug(){ +void malloc::debug(){ size_t memory_free = 0; size_t non_free_blocks = 0; size_t inconsistent = 0; diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp index ef5515a0..dbe6adf8 100644 --- a/kernel/src/shell.cpp +++ b/kernel/src/shell.cpp @@ -26,7 +26,7 @@ #include "physical_allocator.hpp" #include "virtual_allocator.hpp" -#include "memory.hpp" +#include "malloc.hpp" #include "e820.hpp" //Commands @@ -295,13 +295,13 @@ void memory_command(const std::vector&){ k_printf("\tFree: %m (%h)\n", virtual_allocator::free(), virtual_allocator::free()); k_print_line("Dynamic:"); - k_printf("\tAllocated: %m (%h)\n", allocated_memory(), allocated_memory()); - k_printf("\tUsed: %m (%h)\n", used_memory(), used_memory()); - k_printf("\tFree: %m (%h)\n", free_memory(), free_memory()); + k_printf("\tAllocated: %m (%h)\n", malloc::allocated_memory(), malloc::allocated_memory()); + k_printf("\tUsed: %m (%h)\n", malloc::used_memory(), malloc::used_memory()); + k_printf("\tFree: %m (%h)\n", malloc::free_memory(), malloc::free_memory()); } void mallocdebug_command(const std::vector&){ - malloc_debug(); + malloc::debug(); } void disks_command(const std::vector& params){ @@ -781,7 +781,7 @@ bool allocate_segments(char* buffer, void** allocated_segments, uint8_t flags){ } //2. Get enough physical memory - auto memory = k_malloc(bytes); + auto memory = malloc::k_malloc(bytes); if(!memory){ k_print_line("Cannot allocate memory, probably out of memory"); @@ -833,7 +833,7 @@ void* allocate_user_stack(size_t stack_address, size_t stack_size, uint8_t flags } //2. Get enough physical memory - auto memory = k_malloc(bytes); + auto memory = malloc::k_malloc(bytes); if(!memory){ k_print_line("Cannot allocate memory, probably out of memory"); @@ -871,7 +871,7 @@ void release_segments(char* buffer, void** allocated_segments){ auto a = allocated_segments[p]; if(a){ - k_free(a); + malloc::k_free(a); auto address = p_header.p_vaddr; auto first_page = reinterpret_cast(paging::page_align(reinterpret_cast(address))); diff --git a/kernel/src/thor.cpp b/kernel/src/thor.cpp index 1ee2d599..ae0c8e25 100644 --- a/kernel/src/thor.cpp +++ b/kernel/src/thor.cpp @@ -6,22 +6,22 @@ //======================================================================= #include "thor.hpp" -#include "memory.hpp" +#include "malloc.hpp" void* operator new(uint64_t size){ - return k_malloc(size); + return malloc::k_malloc(size); } void operator delete(void* p){ - k_free(p); + malloc::k_free(p); } void* operator new[](uint64_t size){ - return k_malloc(size); + return malloc::k_malloc(size); } void operator delete[](void* p){ - return k_free(p); + return malloc::k_free(p); } extern "C" {