diff --git a/ChangeLog b/ChangeLog index 59cc1a2..5cdf0dc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ 2022-12-12 Alex Kotov * examples/Makefile.am: Fix builds without pthreads + * include/kernaux/memmap.h: The old API has been removed 2022-12-11 Alex Kotov diff --git a/Makefile.am b/Makefile.am index b3aea18..7ee4c96 100644 --- a/Makefile.am +++ b/Makefile.am @@ -92,7 +92,6 @@ libkernaux_la_SOURCES += \ src/multiboot2/header_helpers.c \ src/multiboot2/header_is_valid.c \ src/multiboot2/header_print.c \ - src/multiboot2/info_convert.c \ src/multiboot2/info_helpers.c \ src/multiboot2/info_is_valid.c \ src/multiboot2/info_print.c diff --git a/README.md b/README.md index 4161746..421b8ae 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ zero). Work-in-progress APIs can change at any time. * Utilities * [Measurement units utils](/include/kernaux/units.h) (*work in progress*) * [Example: To human](/examples/units_human.c) - * [Memory map](/include/kernaux/memmap.h) (*non-breaking since* **0.4.0**) + * [Memory map](/include/kernaux/memmap.h) (*non-breaking since* **?.?.?**) * [Example](/examples/memmap.c) * [printf format parser](/include/kernaux/printf_fmt.h) (*non-breaking since* **0.6.0**) * [Example](/examples/printf_fmt.c) diff --git a/examples/memmap.c b/examples/memmap.c index 307fb48..18e64bd 100644 --- a/examples/memmap.c +++ b/examples/memmap.c @@ -1,45 +1,3 @@ #include -#include -#include -#include -#include - -#define SIZE_256MiB ( 256 * 1024 * 1024) -#define SIZE_512MiB ( 512 * 1024 * 1024) -#define SIZE_1GiB (1024 * 1024 * 1024) - -void example_main() -{ - KernAux_MemMap memmap = { KernAux_MemMap_create(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)); - - 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); -} +void example_main() {} diff --git a/include/kernaux/memmap.h b/include/kernaux/memmap.h index 27ca5bf..2a7eceb 100644 --- a/include/kernaux/memmap.h +++ b/include/kernaux/memmap.h @@ -5,56 +5,6 @@ extern "C" { #endif -#include - -#include -#include - -#define KERNAUX_MEMMAP_ENTRIES_MAX 100 - -#define KERNAUX_MEMMAP_ENTRY_TAG_SLEN_MAX 24 -#define KERNAUX_MEMMAP_ENTRY_TAG_SIZE_MAX (KERNAUX_MEMMAP_ENTRY_TAG_SLEN_MAX + 1) - -typedef const struct KernAux_MemMap_Entry { - bool is_available; - char tag[KERNAUX_MEMMAP_ENTRY_TAG_SIZE_MAX]; - size_t start, size, end, limit; -} *KernAux_MemMap_Entry; - -typedef struct KernAux_MemMap { - bool KERNAUX_PRIVATE_FIELD(is_finished); - size_t KERNAUX_PRIVATE_FIELD(memory_size); - size_t KERNAUX_PRIVATE_FIELD(entries_count); - struct KernAux_MemMap_Entry KERNAUX_PRIVATE_FIELD(entries)[KERNAUX_MEMMAP_ENTRIES_MAX]; -} KernAux_MemMap[1]; - -struct KernAux_MemMap KernAux_MemMap_create(size_t memory_size); -void KernAux_MemMap_init(KernAux_MemMap memmap, size_t memory_size); - -/// @warning Must only be called with NOT finished memmap, otherwise panics. -bool KernAux_MemMap_add_entry( - KernAux_MemMap memmap, - bool is_available, - const char *tag, - size_t start, - size_t size -); - -/// @warning Must only be called with NOT finished memmap, otherwise panics. -bool KernAux_MemMap_finish(KernAux_MemMap memmap); - -/// @warning Must only be called with finished memmap, otherwise panics. -KernAux_MemMap_Entry -KernAux_MemMap_entry_by_index(KernAux_MemMap memmap, size_t index); - -/// @warning Must only be called with finished memmap, otherwise panics. -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/include/kernaux/multiboot2.h.in b/include/kernaux/multiboot2.h.in index ad5941e..63a6539 100644 --- a/include/kernaux/multiboot2.h.in +++ b/include/kernaux/multiboot2.h.in @@ -7,7 +7,6 @@ extern "C" { #include #include -@comment_line_memmap@#include #include #include @@ -581,15 +580,6 @@ const char *KernAux_Multiboot2_HTag_RelocatableHeader_Preference_to_str( enum KernAux_Multiboot2_HTag_RelocatableHeader_Preference pref ); -/************************************ - * Information conversion functions * - ************************************/ - -@comment_line_memmap@bool KernAux_Multiboot2_Info_to_memmap( -@comment_line_memmap@ const struct KernAux_Multiboot2_Info *multiboot2_info, -@comment_line_memmap@ KernAux_MemMap memmap -@comment_line_memmap@); - /*************************** * Header helper functions * ***************************/ diff --git a/src/memmap.c b/src/memmap.c index 04534a9..da32a75 100644 --- a/src/memmap.c +++ b/src/memmap.c @@ -2,152 +2,8 @@ #include "config.h" #endif -#include +#include #include -#include -#include -#include -#include - -#define MEMMAP (*memmap) - -struct KernAux_MemMap KernAux_MemMap_create(const size_t memory_size) -{ - struct KernAux_MemMap memmap; - KernAux_MemMap_init(&memmap, memory_size); - return memmap; -} - -void KernAux_MemMap_init(KernAux_MemMap memmap, const size_t memory_size) -{ - MEMMAP.is_finished = false; - MEMMAP.memory_size = 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) return false; - if (SIZE_MAX - start < size) return false; - if (size == 0) return false; - - const size_t index = MEMMAP.entries_count++; - - memset(&MEMMAP.entries[index], 0, sizeof(MEMMAP.entries[index])); - - MEMMAP.entries[index].is_available = is_available; - MEMMAP.entries[index].start = start; - MEMMAP.entries[index].size = size; - MEMMAP.entries[index].end = start + size - 1; - MEMMAP.entries[index].limit = start + size; - - if (tag) { - strncpy( - MEMMAP.entries[index].tag, - tag, - KERNAUX_MEMMAP_ENTRY_TAG_SLEN_MAX - ); - } - - return true; -} - -bool KernAux_MemMap_finish(KernAux_MemMap memmap) -{ - if (MEMMAP.is_finished) { - KERNAUX_PANIC("memmap is finished"); - return false; - } - - if ((MEMMAP.entries_count == 0 && MEMMAP.memory_size != 0) || - MEMMAP.entries_count > KERNAUX_MEMMAP_ENTRIES_MAX || - MEMMAP.entries[0].start != 0 || - MEMMAP.entries[MEMMAP.entries_count - 1].limit != MEMMAP.memory_size) - { - 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 - || - MEMMAP.entries[index].end != - MEMMAP.entries[index].start + MEMMAP.entries[index].size - 1 - || - MEMMAP.entries[index].limit != - MEMMAP.entries[index].start + MEMMAP.entries[index].size) - { - 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) { - return false; - } - } - - return MEMMAP.is_finished = true; -} - -KernAux_MemMap_Entry -KernAux_MemMap_entry_by_index(KernAux_MemMap memmap, const size_t index) -{ - if (!MEMMAP.is_finished) { - KERNAUX_PANIC("memmap is not finished"); - return NULL; - } - - if (index >= MEMMAP.entries_count) return NULL; - return &MEMMAP.entries[index]; -} - -KernAux_MemMap_Entry -KernAux_MemMap_entry_by_start(KernAux_MemMap memmap, const size_t start) -{ - if (!MEMMAP.is_finished) { - KERNAUX_PANIC("memmap is not finished"); - return NULL; - } - - for (size_t index = 0; index < MEMMAP.entries_count; ++index) { - if (MEMMAP.entries[index].start == start) return &MEMMAP.entries[index]; - } - - 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; -} +KERNAUX_UNUSED +static const int foobar = 0; diff --git a/src/multiboot2/info_convert.c b/src/multiboot2/info_convert.c deleted file mode 100644 index b195494..0000000 --- a/src/multiboot2/info_convert.c +++ /dev/null @@ -1,63 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include - -#ifdef WITH_MEMMAP -#include -#endif - -#include - -#ifdef WITH_MEMMAP -// TODO: implement this -bool KernAux_Multiboot2_Info_to_memmap( - const struct KernAux_Multiboot2_Info *multiboot2_info, - KernAux_MemMap memmap -) { - KERNAUX_ASSERT(multiboot2_info); - - KernAux_MemMap_init(memmap, 0); - if (!KernAux_Multiboot2_Info_is_valid(multiboot2_info)) return false; - - const struct KernAux_Multiboot2_ITagBase *const basic_memory_info_tag_base = - KernAux_Multiboot2_Info_first_tag_with_type( - multiboot2_info, - KERNAUX_MULTIBOOT2_ITAG_BASIC_MEMORY_INFO - ); - const struct KernAux_Multiboot2_ITagBase *const memory_map_tag_base = - KernAux_Multiboot2_Info_first_tag_with_type( - multiboot2_info, - KERNAUX_MULTIBOOT2_ITAG_MEMORY_MAP - ); - - // FIXME: Basic memory info tag may not be provided by some boot loaders on - // EFI platforms if EFI boot services are enabled and available for the - // loaded image (EFI boot services not terminated tag exists in Multiboot2 - // information structure). - if (basic_memory_info_tag_base == NULL || memory_map_tag_base == NULL) { - return false; - } - - const struct KernAux_Multiboot2_ITag_BasicMemoryInfo *const - basic_memory_info_tag = - (const struct KernAux_Multiboot2_ITag_BasicMemoryInfo*) - basic_memory_info_tag_base; - const struct KernAux_Multiboot2_ITag_MemoryMap *const - memory_map_tag = - (const struct KernAux_Multiboot2_ITag_MemoryMap*) - memory_map_tag_base; - - // FIXME: The value returned for upper memory is maximally the address of - // the first upper memory hole minus 1 megabyte. It is not guaranteed to be - // this value. - const size_t memory_size = (size_t)basic_memory_info_tag->mem_upper * 1024; - KernAux_MemMap_init(memmap, memory_size); - - (void)memory_map_tag; - - return false; -} -#endif diff --git a/tests/test_memmap.c b/tests/test_memmap.c index ff35279..ff4ee3d 100644 --- a/tests/test_memmap.c +++ b/tests/test_memmap.c @@ -2,232 +2,6 @@ #include "config.h" #endif -#define KERNAUX_ACCESS_PRIVATE - -#include -#include #include -#include -#include -#include -#include - -static KernAux_MemMap memmap; - -static unsigned int assert_count_exp = 0; -static unsigned int assert_count_ctr = 0; - -static const char *assert_last_file = NULL; - -static void assert_cb( - const char *const file, - const int line KERNAUX_UNUSED, - const char *const msg KERNAUX_UNUSED -) { - ++assert_count_ctr; - assert_last_file = file; -} - -static void before_assert() -{ - assert(assert_count_ctr == assert_count_exp); -} - -static void expect_assert() -{ -#ifdef ENABLE_DEBUG - // cppcheck-suppress assignmentInAssert - assert(assert_count_ctr == ++assert_count_exp); - assert(strstr(assert_last_file, "src/memmap.c") != NULL); - assert_last_file = NULL; -#else - assert(assert_count_ctr == 0); - assert(assert_last_file == NULL); -#endif -} - -#define MEMSET memset(memmap, 0xff, sizeof(memmap)) -#define MEMMAP (*memmap) - -void test_main() -{ - kernaux_assert_cb = assert_cb; - - { - MEMSET; - KernAux_MemMap_init(memmap, 0); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 0); - assert(MEMMAP.entries_count == 0); - - assert(KernAux_MemMap_finish(memmap)); - - assert(MEMMAP.is_finished); - assert(MEMMAP.memory_size == 0); - assert(MEMMAP.entries_count == 0); - - assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL); - - before_assert(); - assert(!KernAux_MemMap_finish(memmap)); - expect_assert(); - } - - { - MEMSET; - KernAux_MemMap_init(memmap, 0); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 0); - assert(MEMMAP.entries_count == 0); - - assert(!KernAux_MemMap_add_entry(memmap, false, NULL, 0, 0)); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 0); - assert(MEMMAP.entries_count == 0); - - assert(KernAux_MemMap_finish(memmap)); - - assert(MEMMAP.is_finished); - assert(MEMMAP.memory_size == 0); - assert(MEMMAP.entries_count == 0); - - assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL); - } - - { - MEMSET; - KernAux_MemMap_init(memmap, 1); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 1); - assert(MEMMAP.entries_count == 0); - - assert(KernAux_MemMap_add_entry(memmap, false, NULL, 0, 1)); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 1); - assert(MEMMAP.entries_count == 1); - - assert(KernAux_MemMap_finish(memmap)); - - assert(MEMMAP.is_finished); - assert(MEMMAP.memory_size == 1); - assert(MEMMAP.entries_count == 1); - assert(MEMMAP.entries[0].is_available == false); - assert(MEMMAP.entries[0].tag[0] == '\0'); - assert(MEMMAP.entries[0].start == 0); - assert(MEMMAP.entries[0].size == 1); - assert(MEMMAP.entries[0].end == 0); - assert(MEMMAP.entries[0].limit == 1); - - assert(KernAux_MemMap_entry_by_index(memmap, 0) == &MEMMAP.entries[0]); - } - - { - MEMSET; - KernAux_MemMap_init(memmap, 2); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 2); - assert(MEMMAP.entries_count == 0); - - assert(KernAux_MemMap_add_entry(memmap, false, NULL, 0, 2)); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 2); - assert(MEMMAP.entries_count == 1); - - assert(KernAux_MemMap_finish(memmap)); - - assert(MEMMAP.is_finished); - assert(MEMMAP.memory_size == 2); - assert(MEMMAP.entries_count == 1); - assert(MEMMAP.entries[0].is_available == false); - assert(MEMMAP.entries[0].tag[0] == '\0'); - assert(MEMMAP.entries[0].start == 0); - assert(MEMMAP.entries[0].size == 2); - assert(MEMMAP.entries[0].end == 1); - assert(MEMMAP.entries[0].limit == 2); - - assert(KernAux_MemMap_entry_by_index(memmap, 0) == &MEMMAP.entries[0]); - } - - { - MEMSET; - KernAux_MemMap_init(memmap, 1); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 1); - assert(MEMMAP.entries_count == 0); - - assert(KernAux_MemMap_add_entry(memmap, false, NULL, 0, 2)); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 1); - assert(MEMMAP.entries_count == 1); - - assert(!KernAux_MemMap_finish(memmap)); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 1); - assert(MEMMAP.entries_count == 1); - assert(MEMMAP.entries[0].is_available == false); - assert(MEMMAP.entries[0].tag[0] == '\0'); - assert(MEMMAP.entries[0].start == 0); - assert(MEMMAP.entries[0].size == 2); - assert(MEMMAP.entries[0].end == 1); - assert(MEMMAP.entries[0].limit == 2); - - before_assert(); - assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL); - expect_assert(); - } - - { - MEMSET; - KernAux_MemMap_init(memmap, 2); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 2); - assert(MEMMAP.entries_count == 0); - - assert(KernAux_MemMap_add_entry(memmap, false, NULL, 0, 1)); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 2); - assert(MEMMAP.entries_count == 1); - - assert(KernAux_MemMap_add_entry(memmap, false, NULL, 1, 1)); - - assert(MEMMAP.is_finished == false); - assert(MEMMAP.memory_size == 2); - assert(MEMMAP.entries_count == 2); - - assert(KernAux_MemMap_finish(memmap)); - - assert(MEMMAP.is_finished); - assert(MEMMAP.memory_size == 2); - assert(MEMMAP.entries_count == 2); - assert(MEMMAP.entries[0].is_available == false); - assert(MEMMAP.entries[0].tag[0] == '\0'); - assert(MEMMAP.entries[0].start == 0); - assert(MEMMAP.entries[0].size == 1); - assert(MEMMAP.entries[0].end == 0); - assert(MEMMAP.entries[0].limit == 1); - assert(MEMMAP.entries[1].is_available == false); - assert(MEMMAP.entries[1].tag[0] == '\0'); - assert(MEMMAP.entries[1].start == 1); - assert(MEMMAP.entries[1].size == 1); - assert(MEMMAP.entries[1].end == 1); - assert(MEMMAP.entries[1].limit == 2); - - assert(KernAux_MemMap_entry_by_index(memmap, 0) == &MEMMAP.entries[0]); - assert(KernAux_MemMap_entry_by_index(memmap, 1) == &MEMMAP.entries[1]); - } - - assert(assert_count_ctr == assert_count_exp); -} +void test_main() {}