Main: include/kernaux/memmap.h: Added func "KernAux_MemMap_entry_by_addr"

This commit is contained in:
Alex Kotov 2022-06-15 13:41:10 +03:00
parent a75c952b10
commit 0c09da19f8
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 33 additions and 2 deletions

View File

@ -18,7 +18,7 @@ int main()
assert(KernAux_MemMap_finish(memmap));
// You can get entry by it's index:
// You can get the entry by it's index:
assert( KernAux_MemMap_entry_by_index(memmap, 0)->is_available == true);
assert(strcmp(KernAux_MemMap_entry_by_index(memmap, 0)->tag, "") == 0);
assert( KernAux_MemMap_entry_by_index(memmap, 0)->start == 0);
@ -26,7 +26,7 @@ int main()
assert( KernAux_MemMap_entry_by_index(memmap, 0)->end == SIZE_256MiB - 1);
assert( KernAux_MemMap_entry_by_index(memmap, 0)->limit == SIZE_256MiB);
// You can get entry by it's start address:
// You can get the entry by it's start address:
assert( KernAux_MemMap_entry_by_start(memmap, SIZE_256MiB)->is_available == false);
assert(strcmp(KernAux_MemMap_entry_by_start(memmap, SIZE_256MiB)->tag, "foo") == 0);
assert( KernAux_MemMap_entry_by_start(memmap, SIZE_256MiB)->start == SIZE_256MiB);
@ -34,5 +34,13 @@ int main()
assert( KernAux_MemMap_entry_by_start(memmap, SIZE_256MiB)->end == SIZE_512MiB - 1);
assert( KernAux_MemMap_entry_by_start(memmap, SIZE_256MiB)->limit == SIZE_512MiB);
// You can get the entry by any address inside it:
assert( KernAux_MemMap_entry_by_addr(memmap, SIZE_512MiB )->is_available == true);
assert(strcmp(KernAux_MemMap_entry_by_addr(memmap, SIZE_512MiB + 1 )->tag, "bar") == 0);
assert( KernAux_MemMap_entry_by_addr(memmap, SIZE_512MiB + SIZE_256MiB)->start == SIZE_512MiB);
assert( KernAux_MemMap_entry_by_addr(memmap, SIZE_1GiB - 3 )->size == SIZE_512MiB);
assert( KernAux_MemMap_entry_by_addr(memmap, SIZE_1GiB - 2 )->end == SIZE_1GiB - 1);
assert( KernAux_MemMap_entry_by_addr(memmap, SIZE_1GiB - 1 )->limit == SIZE_1GiB);
return 0;
}

View File

@ -49,6 +49,10 @@ KernAux_MemMap_entry_by_index(KernAux_MemMap memmap, size_t index);
KernAux_MemMap_Entry
KernAux_MemMap_entry_by_start(KernAux_MemMap memmap, size_t start);
/// @warning Must only be called with finished memmap, otherwise panics.
KernAux_MemMap_Entry
KernAux_MemMap_entry_by_addr(KernAux_MemMap memmap, size_t addr);
#ifdef __cplusplus
}
#endif

View File

@ -126,3 +126,22 @@ KernAux_MemMap_entry_by_start(KernAux_MemMap memmap, const size_t start)
return NULL;
}
KernAux_MemMap_Entry
KernAux_MemMap_entry_by_addr(KernAux_MemMap memmap, const size_t addr)
{
if (!MEMMAP.is_finished) {
KERNAUX_PANIC("memmap is not finished");
return NULL;
}
for (size_t index = 0; index < MEMMAP.entries_count; ++index) {
if (addr >= MEMMAP.entries[index].start &&
addr <= MEMMAP.entries[index].end)
{
return &MEMMAP.entries[index];
}
}
return NULL;
}