Main: include/kernaux/memmap.h: Less panics and documentation

This commit is contained in:
Alex Kotov 2022-06-15 12:33:04 +03:00
parent 44b31501b7
commit 0495da5145
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
2 changed files with 8 additions and 12 deletions

View File

@ -28,6 +28,7 @@ typedef struct KernAux_MemMap {
void KernAux_MemMap_init(KernAux_MemMap memmap, size_t memory_size);
/// @warning Must only be called with unfinished memmap, otherwise panics.
bool KernAux_MemMap_add_entry(
KernAux_MemMap memmap,
bool is_available,
@ -36,6 +37,7 @@ bool KernAux_MemMap_add_entry(
size_t size
);
/// @warning Must only be called with unfinished memmap, otherwise panics.
bool KernAux_MemMap_finish(KernAux_MemMap memmap);
#ifdef __cplusplus

View File

@ -32,15 +32,8 @@ bool KernAux_MemMap_add_entry(
return false;
}
if (MEMMAP.entries_count >= KERNAUX_MEMMAP_ENTRIES_MAX) {
KERNAUX_PANIC("memmap is full");
return false;
}
if (SIZE_MAX - start < size) {
KERNAUX_PANIC("memmap entry limit is too high");
return false;
}
if (MEMMAP.entries_count >= KERNAUX_MEMMAP_ENTRIES_MAX) return false;
if (SIZE_MAX - start < size) return false;
memset(MEMMAP.entries[MEMMAP.entries_count], 0, sizeof(MEMMAP.entries[MEMMAP.entries_count]));
const size_t index = MEMMAP.entries_count++;
@ -70,10 +63,10 @@ bool KernAux_MemMap_finish(KernAux_MemMap memmap)
MEMMAP.entries[0]->start != 0 ||
MEMMAP.entries[MEMMAP.entries_count - 1]->limit != MEMMAP.memory_size)
{
KERNAUX_PANIC("memmap is invalid");
return false;
}
// At first, let's validate the individual entries.
for (size_t index = 0; index < MEMMAP.entries_count; ++index) {
if (SIZE_MAX - MEMMAP.entries[index]->start <
MEMMAP.entries[index]->size
@ -84,14 +77,15 @@ bool KernAux_MemMap_finish(KernAux_MemMap memmap)
MEMMAP.entries[index]->limit !=
MEMMAP.entries[index]->start + MEMMAP.entries[index]->size)
{
KERNAUX_PANIC("memmap is invalid");
return false;
}
}
// TODO: Next, let's sort the entries.
// Finally, let's validate that the entries fit each other properly.
for (size_t index = 1; index < MEMMAP.entries_count; ++index) {
if (MEMMAP.entries[index - 1]->limit != MEMMAP.entries[index]->start) {
KERNAUX_PANIC("memmap is invalid");
return false;
}
}