1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-10-30 11:54:01 -04:00
libkernaux/examples/memmap.c

46 lines
2.4 KiB
C
Raw Normal View History

2022-06-15 03:58:14 -04:00
#include <kernaux/memmap.h>
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
2022-06-15 03:58:14 -04:00
#include <string.h>
#define SIZE_256MiB ( 256 * 1024 * 1024)
#define SIZE_512MiB ( 512 * 1024 * 1024)
#define SIZE_1GiB (1024 * 1024 * 1024)
2022-06-15 03:58:14 -04:00
void example_main()
2022-06-15 03:58:14 -04:00
{
2022-06-15 06:10:44 -04:00
KernAux_MemMap memmap = { KernAux_MemMap_create(SIZE_1GiB) };
2022-06-15 03:58:14 -04:00
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));
assert(KernAux_MemMap_finish(memmap));
// 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);
assert( KernAux_MemMap_entry_by_index(memmap, 0)->size == SIZE_256MiB);
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 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);
assert( KernAux_MemMap_entry_by_start(memmap, SIZE_256MiB)->size == SIZE_256MiB);
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);
2022-06-15 03:58:14 -04:00
}