From 551a6bd623ec76ab490d5a617e2430c228a6b705 Mon Sep 17 00:00:00 2001 From: Tilen Majerle Date: Sat, 4 Jan 2025 23:05:09 +0100 Subject: [PATCH] lwmem: add available bytes check --- CMakeLists.txt | 1 + dev/main.cpp | 3 ++ tests/lwmem_test_available_mem.c | 54 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/lwmem_test_available_mem.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a483a6..a94b6c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ else() ${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test.c ${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_simple.c ${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_region.c + ${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_available_mem.c # win32 port ${CMAKE_CURRENT_LIST_DIR}/lwmem/src/system/lwmem_sys_win32.c diff --git a/dev/main.cpp b/dev/main.cpp index a96dbed..f0491c6 100644 --- a/dev/main.cpp +++ b/dev/main.cpp @@ -8,12 +8,15 @@ extern "C" void lwmem_test_run(void); extern "C" void lwmem_test_simple_run(void); extern "C" void lwmem_test_memory_structure(void); extern "C" void lwmem_test_region(void); +extern "C" void lwmem_test_available_mem(void); /* Setup manager */ static Lwmem::LwmemLight<1024> manager; int main(void) { + lwmem_test_available_mem(); + return 0; lwmem_test_region(); return 0; #if LWMEM_CFG_FULL diff --git a/tests/lwmem_test_available_mem.c b/tests/lwmem_test_available_mem.c new file mode 100644 index 0000000..a89537c --- /dev/null +++ b/tests/lwmem_test_available_mem.c @@ -0,0 +1,54 @@ +#include +#include "lwmem/lwmem.h" + +#if LWMEM_CFG_ALIGN_NUM != 4 +#error "Test shall run with LWMEM_CFG_ALIGN_NUM == 4" +#endif + +/* Test assert information */ +#define TEST_ASSERT(condition) \ + do { \ + if (!(condition)) { \ + printf("Assert failed on the line %d\r\n", __LINE__); \ + return; \ + } \ + } while (0) + +void +lwmem_test_available_mem(void) { + static lwmem_t lwmem; + uint32_t data_buff[256 >> 2]; + void *par1 = NULL, *par2 = NULL, *par3 = NULL; + const lwmem_region_t regions[] = { + {data_buff, sizeof(data_buff)}, + {NULL, 0}, + }; + + /* Setup the values */ + lwmem_assignmem_ex(&lwmem, regions); + TEST_ASSERT(lwmem.mem_available_bytes == 248); + + /* Allocate 10 bytes, that shall be properly aligned + header used*/ + par1 = lwmem_malloc_ex(&lwmem, NULL, 10); + TEST_ASSERT(par1 != NULL); + TEST_ASSERT(lwmem.mem_available_bytes == (228)); + par2 = lwmem_malloc_ex(&lwmem, NULL, 10); + TEST_ASSERT(par2 != NULL); + TEST_ASSERT(lwmem.mem_available_bytes == (208)); + par3 = lwmem_malloc_ex(&lwmem, NULL, 10); + TEST_ASSERT(par3 != NULL); + TEST_ASSERT(lwmem.mem_available_bytes == (188)); + + /* Free the entry and check the new available bytes */ + lwmem_free_s_ex(&lwmem, &par3); + TEST_ASSERT(par3 == NULL); + TEST_ASSERT(lwmem.mem_available_bytes == (208)); + + /* Free remaining 2 */ + lwmem_free_s_ex(&lwmem, &par2); + TEST_ASSERT(par2 == NULL); + TEST_ASSERT(lwmem.mem_available_bytes == (228)); + lwmem_free_s_ex(&lwmem, &par1); + TEST_ASSERT(par1 == NULL); + TEST_ASSERT(lwmem.mem_available_bytes == (248)); +}