[SOLVED] Using memory map to calculate size of system RAM
Posted: Thu Dec 03, 2020 1:02 pm
I wrote a function to parse my memory map, namely to print to serial output the contents, and return the size of system RAM in bytes.
Aight so I have a function calling that function:
And it prints:
However, it should state that i have 4096MiB, as I am allocating 4GiB to QEMU.
I try allocating 6GiB:
I know it has to be a mathematical error, as my memory map is correct...
Forgive me if I am overlooking something, I am taking 8th grade pre-algebra at the moment...
...though this shouldn't be too complicated.
Code: Select all
// @brief Parses the memory map and returns the amount of RAM in the system.
uint64_t mem_map_parse(Memory_Map_Descriptor* memmap, uint64_t map_size, uint64_t desc_size)
{
uint8_t *startOfMemoryMap = (uint8_t *)memmap;
uint8_t *endOfMemoryMap = startOfMemoryMap + map_size;
uint8_t *offset = startOfMemoryMap;
uint32_t counter = 0;
serial_msg("\n\nMEMORY MAP:\n-=-=-=-=-=-\n");
while (offset < endOfMemoryMap)
{
Memory_Map_Descriptor *desc = (Memory_Map_Descriptor *)offset;
hex_str_serial(desc->physical_start);
serial_msg(": ");
serial_msg(memory_types[desc->type]);
serial_msg(" (");
dec_str_serial(desc->count);
serial_msg(")");
serial_msg("\n");
offset += desc_size;
counter++;
}
offset -= desc_size;
Memory_Map_Descriptor *desc = (Memory_Map_Descriptor *)offset;
hex_str_serial(desc->physical_start);
serial_msg(": ");
serial_msg(memory_types[desc->type]);
serial_msg(" (");
dec_str_serial(desc->count);
serial_msg(")");
serial_msg("\n");
return (((uint64_t)desc->physical_start) + ((desc->count * 0x1000)));
}
Code: Select all
void malloc_init(Memory_Map_Descriptor* memmap, uint64_t map_size, uint64_t desc_size)
{
serial_msg("\n");
dec_str_serial(((mem_map_parse(memmap, map_size, desc_size)) / 1024) / 1024);
serial_msg("MiB of RAM\n");
}
Code: Select all
5120MiB of RAM
I try allocating 6GiB:
Code: Select all
7168MiB of RAM
Forgive me if I am overlooking something, I am taking 8th grade pre-algebra at the moment...
...though this shouldn't be too complicated.