diff --git a/examples/memmap.c b/examples/memmap.c index 83d66b8..35bcced 100644 --- a/examples/memmap.c +++ b/examples/memmap.c @@ -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; } diff --git a/include/kernaux/memmap.h b/include/kernaux/memmap.h index 27e1edb..c0537a9 100644 --- a/include/kernaux/memmap.h +++ b/include/kernaux/memmap.h @@ -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 diff --git a/src/memmap.c b/src/memmap.c index d4391ba..5f0cdba 100644 --- a/src/memmap.c +++ b/src/memmap.c @@ -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; +}