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

This commit is contained in:
Alex Kotov 2022-06-15 11:50:16 +03:00
parent 5f668d05fe
commit d030b9435e
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 55 additions and 1 deletions

View File

@ -1,9 +1,12 @@
#include <kernaux/memmap.h>
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#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;
}

View File

@ -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

View File

@ -7,6 +7,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#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;
}