Review memory command to display more results

This commit is contained in:
Baptiste Wicht 2014-01-23 20:33:26 +01:00
parent fa055eded0
commit d5ff8f3b6a
8 changed files with 108 additions and 50 deletions

View File

@ -20,9 +20,9 @@ T* k_malloc(){
return reinterpret_cast<T*>(k_malloc(sizeof(T)));
}
uint64_t free_memory();
uint64_t used_memory();
uint64_t allocated_memory();
uint64_t used_memory();
uint64_t free_memory();
void malloc_debug();

View File

@ -18,6 +18,10 @@ void init();
size_t early_allocate(size_t pages);
size_t allocate(size_t pages);
size_t available();
size_t allocated();
size_t free();
} //end of physical_allocator namespace
#endif

View File

@ -20,6 +20,10 @@ void init();
size_t allocate(size_t pages);
size_t available();
size_t allocated();
size_t free();
} //end of virtual_allocator namespace
#endif

View File

@ -7,6 +7,7 @@
#include "kernel.hpp"
#include "physical_allocator.hpp"
#include "virtual_allocator.hpp"
#include "paging.hpp"
#include "memory.hpp"
#include "timer.hpp"
@ -35,6 +36,7 @@ void kernel_main(){
//Prepare memory
physical_allocator::early_init();
paging::init();
virtual_allocator::init();
physical_allocator::init();
init_memory_manager();

View File

@ -18,8 +18,8 @@ namespace {
const bool DEBUG_MALLOC = false;
const bool TRACE_MALLOC = false;
uint64_t _used_memory;
uint64_t _allocated_memory;
size_t _used_memory;
size_t _allocated_memory;
struct malloc_footer_chunk;
@ -129,12 +129,9 @@ uint64_t* allocate_block(uint64_t blocks){
max_address = std::max(max_address, virtual_memory);
auto block = reinterpret_cast<uint64_t*>(virtual_memory);
//TODO Remove
_allocated_memory += blocks * BLOCK_SIZE;
return block;
return reinterpret_cast<uint64_t*>(virtual_memory);
}
template<bool Debug>
@ -385,16 +382,25 @@ void k_free(void* block){
debug_malloc<DEBUG_MALLOC>("after free");
}
uint64_t used_memory(){
return _used_memory;
}
uint64_t allocated_memory(){
size_t allocated_memory(){
return _allocated_memory;
}
uint64_t free_memory(){
return e820::available_memory() - _used_memory;
size_t used_memory(){
return _used_memory;
}
size_t free_memory(){
size_t memory_free = 0;
auto it = malloc_head;
do {
memory_free += it->size();
it = it->next();
} while(it != malloc_head);
return memory_free;
}
void malloc_debug(){

View File

@ -9,26 +9,29 @@
#include "e820.hpp"
#include "paging.hpp"
//For problems during boot
#include "kernel.hpp"
#include "console.hpp"
namespace {
const e820::mmapentry* current_mmap_entry = 0;
uintptr_t current_mmap_entry_position = 0;
size_t allocated_memory = 0;
} //End of anonymous namespace
void physical_allocator::early_init(){
e820::finalize_memory_detection();
}
void physical_allocator::init(){
//TODO
if(e820::mmap_failed()){
k_print_line("e820 failed, no way to allocate memory");
suspend_boot();
}
}
size_t physical_allocator::early_allocate(size_t blocks){
return allocate(blocks);
}
size_t physical_allocator::allocate(size_t blocks){
if(!current_mmap_entry){
for(uint64_t i = 0; i < e820::mmap_entry_count(); ++i){
auto& entry = e820::mmap_entry(i);
@ -46,6 +49,8 @@ size_t physical_allocator::allocate(size_t blocks){
return 0;
}
allocated_memory += blocks * paging::PAGE_SIZE;
auto address = current_mmap_entry_position;
current_mmap_entry_position += blocks * paging::PAGE_SIZE;
@ -54,3 +59,23 @@ size_t physical_allocator::allocate(size_t blocks){
return address;
}
void physical_allocator::init(){
//TODO
}
size_t physical_allocator::allocate(size_t blocks){
return early_allocate(blocks);
}
size_t physical_allocator::available(){
return e820::available_memory();
}
size_t physical_allocator::allocated(){
return allocated_memory;
}
size_t physical_allocator::free(){
return available() - allocated();
}

View File

@ -4,31 +4,34 @@
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include "stl/types.hpp"
#include "stl/algorithms.hpp"
#include "stl/vector.hpp"
#include "stl/string.hpp"
#include "stl/optional.hpp"
#include "shell.hpp"
#include "keyboard.hpp"
#include "kernel_utils.hpp"
#include "console.hpp"
#include "shell.hpp"
#include "timer.hpp"
#include "memory.hpp"
#include "disks.hpp"
#include "ata.hpp"
#include "acpi.hpp"
#include "e820.hpp"
#include "rtc.hpp"
#include "elf.hpp"
#include "paging.hpp"
#include "gdt.hpp"
#include "vesa.hpp"
#include "physical_allocator.hpp"
#include "virtual_allocator.hpp"
#include "memory.hpp"
#include "e820.hpp"
//Commands
#include "sysinfo.hpp"
#include "stl/types.hpp"
#include "stl/algorithms.hpp"
#include "stl/vector.hpp"
#include "stl/string.hpp"
#include "stl/optional.hpp"
namespace {
#ifdef CONFIG_HISTORY
@ -269,30 +272,32 @@ void echo_command(const std::vector<std::string>& params){
}
void mmap_command(const std::vector<std::string>&){
if(e820::mmap_failed()){
k_print_line("The mmap was not correctly loaded from e820");
} else {
k_printf("There are %u mmap entry\n", e820::mmap_entry_count());
k_printf("There are %u mmap entry\n", e820::mmap_entry_count());
k_print_line("Base End Size Type");
for(uint64_t i = 0; i < e820::mmap_entry_count(); ++i){
auto& entry = e820::mmap_entry(i);
k_print_line("Base End Size Type");
for(uint64_t i = 0; i < e820::mmap_entry_count(); ++i){
auto& entry = e820::mmap_entry(i);
k_printf("%.10h %.10h %.10h %8m %s\n",
entry.base, entry.base + entry.size, entry.size, entry.size, e820::str_e820_type(entry.type));
}
k_printf("%.10h %.10h %.10h %8m %s\n",
entry.base, entry.base + entry.size, entry.size, entry.size, e820::str_e820_type(entry.type));
}
}
void memory_command(const std::vector<std::string>&){
if(e820::mmap_failed()){
k_print_line("The mmap was not correctly loaded from e820");
} else {
k_printf("Total available memory: %m\n", e820::available_memory());
k_printf("Total used memory: %m\n", used_memory());
k_printf("Total free memory: %m\n", free_memory());
k_printf("Total allocated memory: %m\n", allocated_memory());
}
k_print_line("Physical:");
k_printf("\tAvailable: %m (%h)\n", physical_allocator::available(), physical_allocator::available());
k_printf("\tAllocated: %m (%h)\n", physical_allocator::allocated(), physical_allocator::allocated());
k_printf("\tFree: %m (%h)\n", physical_allocator::free(), physical_allocator::free());
k_print_line("Virtual:");
k_printf("\tAvailable: %m (%h)\n", virtual_allocator::available(), virtual_allocator::available());
k_printf("\tAllocated: %m (%h)\n", virtual_allocator::allocated(), virtual_allocator::allocated());
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());
}
void mallocdebug_command(const std::vector<std::string>&){

View File

@ -17,7 +17,7 @@ size_t allocated_pages = 0;
void virtual_allocator::init(){
auto start = paging::virtual_paging_start;
start += paging::physical_memory_pages;
start += paging::physical_memory_pages * paging::PAGE_SIZE;
if(start % 0x100000 == 0){
next_virtual_address = start;
@ -35,3 +35,15 @@ size_t virtual_allocator::allocate(size_t pages){
next_virtual_address += pages * paging::PAGE_SIZE;
return address;
}
size_t virtual_allocator::available(){
return kernel_virtual_size;
}
size_t virtual_allocator::allocated(){
return allocated_pages * paging::PAGE_SIZE;
}
size_t virtual_allocator::free(){
return available() - allocated();
}