diff --git a/examples/memmap.c b/examples/memmap.c index e1ed73a..8ead360 100644 --- a/examples/memmap.c +++ b/examples/memmap.c @@ -1,9 +1,12 @@ #include #include +#include #include -#define SIZE_1GiB (1024 * 1024 * 1024) +#define SIZE_256MiB ( 256 * 1024 * 1024) +#define SIZE_512MiB ( 512 * 1024 * 1024) +#define SIZE_1GiB (1024 * 1024 * 1024) KernAux_MemMap memmap; @@ -11,5 +14,9 @@ int main() { KernAux_MemMap_init(memmap, SIZE_1GiB); + assert(KernAux_MemMap_add_entry(memmap, true, NULL, 0, SIZE_256MiB)); + assert(KernAux_MemMap_add_entry(memmap, false, "foo", SIZE_256MiB, SIZE_256MiB)); + assert(KernAux_MemMap_add_entry(memmap, true, "bar", SIZE_512MiB, SIZE_512MiB)); + return 0; } diff --git a/include/kernaux/memmap.h b/include/kernaux/memmap.h index d75604f..bb1ad8a 100644 --- a/include/kernaux/memmap.h +++ b/include/kernaux/memmap.h @@ -28,6 +28,14 @@ typedef struct KernAux_MemMap { void KernAux_MemMap_init(KernAux_MemMap memmap, size_t memory_size); +bool KernAux_MemMap_add_entry( + KernAux_MemMap memmap, + bool is_available, + const char *tag, + size_t start, + size_t size +); + #ifdef __cplusplus } #endif diff --git a/src/memmap.c b/src/memmap.c index fa13d24..f06622c 100644 --- a/src/memmap.c +++ b/src/memmap.c @@ -7,6 +7,7 @@ #include #include +#include #include #define MEMMAP (*memmap) @@ -18,3 +19,41 @@ void KernAux_MemMap_init(KernAux_MemMap memmap, const size_t memory_size) MEMMAP.entries_count = 0; memset(MEMMAP.entries, 0, sizeof(MEMMAP.entries)); } + +bool KernAux_MemMap_add_entry( + KernAux_MemMap memmap, + const bool is_available, + const char *const tag, + const size_t start, + const size_t size +) { + if (MEMMAP.is_finished) { + KERNAUX_PANIC("memmap is finished"); + 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; + } + + memset(MEMMAP.entries[MEMMAP.entries_count], 0, sizeof(MEMMAP.entries[MEMMAP.entries_count])); + const size_t index = MEMMAP.entries_count++; + + MEMMAP.entries[index]->is_available = is_available; + if (tag) { + memset (MEMMAP.entries[index]->tag, 0, KERNAUX_MEMMAP_ENTRY_TAG_SIZE_MAX); + strncpy(MEMMAP.entries[index]->tag, tag, KERNAUX_MEMMAP_ENTRY_TAG_SLEN_MAX); + } + MEMMAP.entries[index]->start = start; + MEMMAP.entries[index]->size = size; + MEMMAP.entries[index]->end = start + size - 1; + MEMMAP.entries[index]->limit = start + size; + + return true; +}